Continuous derivative-free optimization: nlpsolve

### 6.53.3 Continuous derivative-free optimization: nlpsolve

nlpsolve computes the optimum of a continuous, not necessarily differentiable nonlinear (multivariate) objective function, subject to a set of nonlinear equality and/or inequality constraints, using the COBYLA algorithm.

- nlpsolve takes the following arguments:
  - obj, an expression to optimize.
  - Optionally, constr, a list of equality and inequality constraints. Double-bounded constraints in form a≤ f(x)≤ b can be entered as f(x)=a..b instead of two inequalities.
  - Optionally, bd, a sequence of variable boundaries x=a..b where a∈ℝ∪{−∞} and b∈ℝ∪{+∞}, b≥ a. If several variables have the same upper and lower bounds, one can enter [x,y,...]=a..b. If the bounds are stored in a two-column matrix, consider using the conversion routine box_constraints (see Section 6.38.8).
  - Optionally, opt, a sequence of options in which each option may be one of:
    - maximize=bool or nlp_maximize=bool, where bool can be true or false. Just maximize or nlp_maximize is equivalent to maximize=true. (By default, maximize=false, i.e. the objective is minimized.)
    - nlp_initialpoint=pt, where pt is a starting point for the solver, given in the form [x=x0,y=y0,…]. pt does not have to be feasible, but it must not be zero. If none is given or the given point is not feasible, then it is driven into the feasible region before optimizing the objective.
    - nlp_samples=n for a nonnegative integer n. If n>0, then n points are sampled at random in the feasible region. At most five points with the smallest/largest value of obj (in case of minimization/maximization) are chosen for the multistart heuristic which enables nlpsolve to reach global optimum in most cases. If n=0, then the randomized standard heuristic is used as a single starting point.3 Note that if nlp_initialpoint is set, these heuristics are not used. (By default, n=500.)
    - nlp_presolve=bool, where bool can be true or false. This option enables/disables the preprocessing step which attempts to reduce the number of decision variables by solving subsets of equality constraints which are all linear in certain subsets of variables, substituting the results, and repeating the process until no further reduction can be made. (In particular, fixed variables are removed.) After solving the presolved problem, the removed variables are substituted back to the solution. (By default, nlp_presolve=true.)
    - nlp_integervariables=lst, where lst is a list of problem variables that are supposed to take integral values. (By default, there are no integral variables.)
    - nlp_binaryvariables=lst, where lst is a list of problem variables that are supposed to take binary (0-1) values. (By default, there are no binary variables.)
    - nlp_iterationlimit=n for a positive integer n. This sets the maximum number of iterations allowed for the solver. (By default, n=105.)
    - nlp_precision=є for some є>0. This sets the default precision for the optimizer. (By default, nlp_precision is equal to the value of epsilon(), which can be set in the session settings in Xcas.)
    - nlp_feasibilitytolerance=є for some є>0. This sets the feasibility tolerance for the initial point generator. If this tolerance is not met, nlpsolve declares the problem to be infeasible and returns an empty list. (By default, є=10−6.)
    - nlp_integertolerance=є for some є>0. This sets the tolerance for evaluation of integrality constraints. (By default, є=10−6.)
    - assume=asmp, where asmp may be one of the following:
      - nlp_nonnegative, which restricts all variables to nonnegative values (existing positive lower bounds are kept, however),
      - nlp_nonnegint or nonnegint, which forces all variables to take nonnegative integer values,
      - nlp_integer or integer, which forces all variables to take integer values,
      - nlp_binary, which forces all variables to take 0-1 values.
- nlpsolve(obj ⟨constr,bd,opt⟩) returns a list [optobj,optdec], where optobj is the optimal value of obj and optobj is a list of of optimal values of the decision variables. If the optimization fails, then optobj is the warning message and optdec is the last point on the path the solver takes in attempt to find the optimal objective value. If the problem is infeasible or if the solver fails to find a feasible point, then an empty list is returned.
- nlpsolve is a local-search based solver which acts as a global optimizer when the initial point is not set. However, since it is based on a random-sampling heuristic, the global optimality of the solution is not guaranteed. For solving mixed-integer nonlinear problems (MINLP), the branch&bound method is used. Note that nlpsolve generally works best for problems with a small number of variables (say, less than ten).

Examples.

- The continuous function f defined by

  f(x)=min{ | √ |
  |x+4|
  | −1, | √ |
  |x+1|
  | −1.005, | √ |
  |x−3|
  | +0.5},

  has a unique global minimum in [−5,5] at x=−1, with f(−1)=1.005.
  Input:

  nlpsolve(min(sqrt(abs(x+4))-1,sqrt(abs(x+1))-1.005,
  sqrt(abs(x-3))+0.5),x=-5..5)

  Output:

  [−1.0049992998,[x=−1.0]]

- Minimize z=x1 x4 (x1+x2+x3)+x3 subject to

  x1 x2 x3 x4 | ≥ 25, |  |  |  |  |  |  |  |  |
  x12+x22+x32 | = 40, |  |  |  |  |  |  |  |  |

  where 1≤ xi≤ 5, i=1,2,3,4.
  Input:

  nlpsolve(x1*x4*(x1+x2+x3)+x3,
  [x1*x2*x3*x4-25>=0,x1^2+x2^2+x3^2+x4^2-40=0],
  [x1,x2,x3,x4]=1..5)

  Output:

  [17.0140172892,[x1 | =1.0,x2=4.74299973362, |  |  |  |  |  |  |  |  |
  x3 | =3.82114985829,x4=1.3794083106]] |  |  |  |  |  |  |  |  |

- Minimize z=(x1−1)2+(x1−x2)2+(x3−1)2+(x4−1)4+(x5−1)6 subject to

  x12 x4+sin(x4−x5) |
  =2 | √ |
  2
  | ,
  x2+x34 x42 |
  =8+ | √ |
  2
  | .

  Input:

  nlpsolve((x1-1)^2+(x1-x2)^2+(x3-1)^2+(x4-1)^4+(x5-1)^6,
  [x1^2*x4+sin(x4-x5)=2*sqrt(2),x2+x3^4*x4^2=8+sqrt(2)])

  Output:

  [0.241505128809,[x1 | =1.16617119669,x2=1.18211040318,x3=1.3802572722, |  |  |  |  |  |  |  |  |
  x4 | =1.50603586392,x5=0.610913318325]] |  |  |  |  |  |  |  |  |

- Maximize z=3x1 x2−x1+6x2 subject to 5x1 x2−4x1−4.5x2≤ 32, where 1≤ x1,x2≤ 5 are integers.
  Input:

  nlpsolve(3x1*x2-x1+6x2,[5x1*x2-4x1-4.5x2<=32],
  [x1,x2]=1..5,assume=integer,maximize)

  Output:

  [58.0,[x1=2.0,x2=5.0]]

- Maximize z=2x1+3x2+6x3−2x1 x2−x1 x3−4x2 x3 where all variables are binary.
  Input:

  nlpsolve(2x1+3x2+6x3-2x1*x2-x1*x3-4x2*x3,
  assume=nlp_binary,maximize)

  Output:

  [7.0,[x1=1.0,x2=0,x3=1.0]]

- Minimize z=5y−2ln(x+1) subject to

  ex/2− |
  1
  2
  | √ |
  y
  | ≤ 1, |  |  |  |  |  |  |  |  |
  −2ln(x+1)−y+2.5 | ≤ 0, |  |  |  |  |  |  |  |  |
  x+y | ≤ 4, |  |  |  |  |  |  |  |  |

  where x∈[0,2] and y∈[1,3] is integer.
  Input:

  nlpsolve(5y-2*ln(x+1),
  [exp(x/2)-sqrt(y)/2-1<=0,-2*ln(x+1)-y+2.5<=0,x+y-4<=0],
  x=0..2,y=1..3,nlp_integervariables=[y])

  Output:

  [8.54528930252,[x=1.06959999348,y=2.0]]

- Minimize z=x12+x22+3x32+4x42+2x52−8x1−2x2−3x3−x4−2x5 subject to

  x1+x2+x3+x4+x5 | ≤ 400, |  |  |  |  |  |  |  |  |
  x1+2x2+2x3+x4+6x5 | ≤ 800, |  |  |  |  |  |  |  |  |
  2x1+x2+6x3 | ≤ 200, |  |  |  |  |  |  |  |  |
  x3+x4+5x5 | ≤ 200, |  |  |  |  |  |  |  |  |
  x1+x2+x3+x4+x5 | ≥ 55, |  |  |  |  |  |  |  |  |
  x1+x2+x3+x4 | ≥ 48, |  |  |  |  |  |  |  |  |
  x2+x4+x5 | ≥ 34, |  |  |  |  |  |  |  |  |
  6x1+7x5 | ≥ 104, |  |  |  |  |  |  |  |  |

  where 0≤ xi≤ 99 are integers for i=1,…,5.
  Input:

  obj:=x1^2+x2^2+3x3^2+4x4^2+2x5^2-8x1-2x2-3x3-x4-2x5;
  constr:=[x1+x2+x3+x4+x5<=400,x1+2x2+2x3+x4+6x5<=800,
  2x1+x2+6x3<=200,x3+x4+5x5<=200,
  x1+x2+x3+x4+x5>=55,x1+x2+x3+x4>=48,
  x2+x4+x5>=34,6x1+7x5>=104];
  nlpsolve(obj,constr,[x1,x2,x3,x4,x5]=0..99,assume=integer)

  Output:

  [807.0,[x1=16.0,x2=22.0,x3=5.0,x4=5.0,x5=7.0]]