({{, (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization])[f,cons,vars]
finds values of variables vars that minimize the quadratic objective f subject to linear constraints cons.}, {, (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization])[{q,c},{a,b}]
finds a vector x that minimizes the quadratic objective 1/2x.q.x+c.x subject to the linear inequality constraints a.x+b⪰0. }, {, (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization])[{q,c},{a,b},{(Subscript[a, eq]),(Subscript[b, eq])}]
includes the linear equality constraints (Subscript[a, eq]).x+(Subscript[b, eq])==0.}, {, (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization])[…,"prop"]
specifies what solution property "prop" should be returned. }})

OpenersNotesSection(Spacer1)Details and Options

Quadratic optimization is also known as quadratic programming (QP) or linearly constrained quadratic optimization.

Quadratic optimization is typically used in problems such as parameter fitting, portfolio optimization and geometric distance problems.

Quadratic optimization is a convex optimization problem that can be solved globally and efficiently.

Quadratic optimization finds x that solves the primal problem: »

The space (Subscript[𝒮, +]^n) consist of symmetric positive semidefinite matrices.

The constraints cons can be specified by:

With (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization])[f,cons,vars], parameter equations of the form par==val, where par is not in vars and val is numerical or an array with numerical values, may be included in the constraints to define parameters used in f or cons.

The objective function may be specified in the following ways:

In the factored form, (Subscript[q, f])∈(ℝ^(p×n)) and (Subscript[c, f])∈(ℝ^p).

The primal minimization problem has a related maximization problem that is the Lagrangian dual problem. The dual maximum value is always less than or equal to the primal minimum value, so it provides a lower bound. The dual maximizer provides information about the primal problem, including sensitivity of the minimum value to changes in the constraints.

The Lagrangian dual problem for quadratic optimization with objective 1/2x.q.x+c.x is given by: »

With a factored quadratic objective 1/2(x.(Transpose[(Subscript[q, f])])+(Subscript[c, f])).((Subscript[q, f]).x+(Subscript[c, f]))+c.x, the dual problem may also be expressed as:

The relationship between the factored dual vector (Subscript[ζ, f]) and the unfactored dual vector ζ is (Subscript[ζ, f])=(Subscript[q, f]).ζ-(Subscript[c, f]).

For quadratic optimization, strong duality holds if q is positive semidefinite. This means that if there is a solution to the primal minimization problem, then there is a solution to the dual maximization problem, and the dual maximum value is equal to the primal minimum value.

The possible solution properties "prop" include:

The dual maximizer component ζ is a function of λ and ν, given by, ζ(λ,ν)=(q^(-1))(c-aᵀ.λ-(Subscript[a, eq])ᵀ .ν).

The following options may be given:

The option (RefLink[Method, paclet:ref/Method])->method may be used to specify the method to use.  Available methods include:

Computations are limited to (RefLink[MachinePrecision, paclet:ref/MachinePrecision]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (65)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Minimize 2 (x^2)+20(y^2)+6x y+5 x subject to the constraint -x+y≥2:

obj=2(x^2)+20(y^2)+6x y+5x;
res=QuadraticOptimization[obj,-x+y≥2,{x,y}]

The optimal point lies in a region defined by the constraints and where 2 (x^2)+20(y^2)+6x y+5 x is smallest:

Show[Plot3D[obj,{x,-2,0},{y,0,1},],Graphics3D[{Blue, PointSize[0.05],Point[{x,y,obj}/.res]}]]

Minimize (x^2)+(y^2) subject to the equality constraint x+y=2 and the inequality constraints 1≤y≤2:

res=QuadraticOptimization[(x^2)+(y^2),{x+y==2,1≤y≤2},{x,y}]

Define objective as 1/2x.q.x+c.x and constraints as a.x+b\[VectorGreaterEqual]0 and (Subscript[a, eq]).x+(Subscript[b, eq])==0:

{q,c}={{{2,0},{0,2}},{0,0}};
{a,b}={{{0,1},{0,-1}},{-1,2}};
{aeq,beq}= {{{1,1}},{-2}};

Solve using matrix-vector inputs:

QuadraticOptimization[{q,c},{a,b},{aeq,beq}]

The optimal point lies where a level curve of (x^2)+(y^2) is tangent to the equality constraint:

Show[RegionPlot[Evaluate[(x^2)+(y^2)≤((x^2)+(y^2)/.res)],],ContourPlot[x+y==2,{x,0.5,1.5},{y,0.5,1.5}, ContourStyle->Black]]

OpenersExampleSection1(Spacer1)Scope  (19)

OpenersExampleSubsection0(Spacer1)Basic Uses  (8)

Minimize (x^2)+(y^2) subject to the constraints x+y≥1:

QuadraticOptimization[(x^2)+(y^2),x+y≥1,{x,y}]

Get the minimizing value using solution property "PrimalMinimumValue":

QuadraticOptimization[(x^2)+(y^2),x+y≥1,{x,y},"PrimalMinimumValue"]

Minimize 2 (x^2)+20(y^2)+6x y+5 x subject to the constraint -x+y≥ 2,y≥0:

QuadraticOptimization[2(x^2)+20(y^2)+6x y+5x,{-x+y≥2,y≥0},{x,y}]

Get the minimizing value and minimizing vector using solution property:

QuadraticOptimization[2(x^2)+20(y^2)+6x y+5x,{-x+y≥2,y≥0},{x,y},{"PrimalMinimumValue","PrimalMinimizer"}]

Minimize (x^2)+(y^2) subject to the constraint x+y≤ 2,1≤y≤2:

QuadraticOptimization[(x^2)+(y^2),{x+y≤2,1≤y≤2},{x,y}]

Define the objective as 1/2x.q.x+c.x and constraints as a.x+b\[VectorGreaterEqual]0:

{q,c}={{{2,0},{0,2}},{0,0}};
{a,b} = {{{-1,-1},{0,1},{0,-1}},{2,-1,2}};

Solve using matrix-vector inputs:

QuadraticOptimization[{q,c},{a,b}]

Minimize (x^2)+(y^2) subject to the equality constraint x+y=2 and the inequality constraint 1≤y≤2:

QuadraticOptimization[(x^2)+(y^2),{x+y==2,1≤y≤2},{x,y}]

Define objective as 1/2x.q.x+c.x and constraints as a.x+b\[VectorGreaterEqual]0 and (Subscript[a, eq]).x+(Subscript[b, eq])==0:

{q,c}={{{2,0},{0,2}},{0,0}};
{a,b} = {{{0,1},{0,-1}},{-1,2}};
{(Subscript[a, eq]),(Subscript[b, eq])}= {{{1,1}},{-2}};

Solve using matrix-vector inputs:

QuadraticOptimization[{q,c},{a,b},{(Subscript[a, eq]),(Subscript[b, eq])}]

Minimize 2(x^2)+5(y^2)+4x y subject to the constraints x+y≥1, x-2y≤0:

QuadraticOptimization[2(x^2)+5(y^2)+4x y,{x+y≥1,x-2y≤0},{x,y}]

Define the objective as 1/2((Subscript[q, f]).x).((Subscript[q, f]).x)+c.x and constraints as a.x+b\[VectorGreaterEqual]0:

{qf,c}={Sqrt[2]*{{1,0},{0,1},{1,2}}, {0,0}};
{a,b}={{{1,1},{-1,2}},{-1,0}};

Solve using matrix-vector inputs:

QuadraticOptimization[{{qf},c},{a,b}]

Minimize 2(x^2)+5(y^2)+4x y subject to the constraints x+y≥1, x-2y≤0,0≤x≤1,-1≤y≤1:

QuadraticOptimization[2(x^2)+5(y^2)+4x y,{x+y≥1,x-2y≤0,0≤x≤1,-1≤y≤1},{x,y}]

Specify constraints 0≤x≤1,-1≤y≤1 using (RefLink[VectorGreaterEqual, paclet:ref/VectorGreaterEqual]) (\[VectorGreaterEqual]) and (RefLink[VectorLessEqual, paclet:ref/VectorLessEqual]) (\[VectorLessEqual]):

QuadraticOptimization[2(x^2)+5(y^2)+4x y,{x+y≥1,x-2y≤0,{0,-1}\[VectorLessEqual]{x,y}\[VectorLessEqual]{1,1}},{x,y}]

Minimize x.q.x subject to a.x+b\[VectorGreaterEqual]0. Use vector variable with parameter equations to specify input:

peqns={q=={{2,2},{2,5}}, a=={{1,1},{-1,2},{1,0},{0,1}},b=={-1,0,0,0}};

QuadraticOptimization[x.q.x,{peqns,a.x+b\[VectorGreaterEqual]0},x]

Minimize x.q.x subject to a.x+b\[VectorGreaterEqual]0,x\[VectorGreaterEqual]0. Use (RefLink[NonNegativeReals, paclet:ref/NonNegativeReals]) to specify the constraint x\[VectorGreaterEqual]0:

peqns={q=={{2,2},{2,5}}, a=={{1,1},{-1,2}},b=={-1,0}};

QuadraticOptimization[x.q.x,{peqns,a.x+b\[VectorGreaterEqual]0},x∈Vectors[2,NonNegativeReals]]

OpenersExampleSubsection1(Spacer1)Primal Model Properties  (4)

Minimize (((x-1))^2)+(((y-5/2))^2)-(((3/2))^2) subject to the constraint x+2y≤4:

QuadraticOptimization[(((x-1))^2)+(((y-5/2))^2)-(((3/2))^2),x+2y≤4,{x,y}]

Get vector output using "PrimalMinimizer":

QuadraticOptimization[(((x-1))^2)+(((y-5/2))^2)-(((3/2))^2),x+2y≤4,{x,y},"PrimalMinimizer"]

Get the rule-based result using "PrimalMinimizerRules":

QuadraticOptimization[(((x-1))^2)+(((y-5/2))^2)-(((3/2))^2),x+2y≤4,{x,y},"PrimalMinimizerRules"]

Get the minimizing value of the optimization using "PrimalMinimumValue":

QuadraticOptimization[(((x-1))^2)+(((y-5/2))^2)-(((3/2))^2),x+2y≤4,{x,y}, "PrimalMinimumValue"]

Obtain the primal minimum value using symbolic inputs:

obj=(((x-1))^2)+(((y-5/2))^2)-(((3/2))^2);
cons=x+2y≤4;

(Subscript[p, sym])=QuadraticOptimization[obj,cons,{x,y},"PrimalMinimumValue"]

Extract the matrix-vector inputs of the optimization problem:

{q,c,{a,b}}=QuadraticOptimization[obj,cons,{x,y},{"ObjectiveMatrix","ObjectiveVector","LinearInequalityConstraints"}];

Solve using the matrix-vector form:

(Subscript[p, m])=QuadraticOptimization[{q,c},{a,b},"PrimalMinimumValue"]

Recover the symbolic primal value by adding the objective constant:

const=QuadraticOptimization[obj,cons,{x,y},"ObjectiveConstant"]

(Subscript[p, sym])==(Subscript[p, m])+const

Find the slack for inequalities and equalities associated with a minimization problem:

obj=2(x^2)+20(y^2)+6x y+5x;
cons={-x+y≥2,x+y==1,y≥1};

{(Subscript[s, ineq]),(Subscript[s, eq])} = QuadraticOptimization[obj,cons,{x,y},"Slack"]

Get the minimum value and the constraint matrices:

{(SuperStar[x]),{a,b},{(Subscript[a, eq]),(Subscript[b, eq])}}=QuadraticOptimization[obj,cons,{x,y},];

The slack for inequality constraints a.(SuperStar[x])+b\[VectorGreaterEqual]0 is a vector (Subscript[s, ineq]) such that a.(SuperStar[x])+b-(Subscript[s, ineq])==0:

Norm[a.(SuperStar[x])+b-(Subscript[s, ineq])]==0

The slack for the equality constraints a.(SuperStar[x])+b==0 is a vector (Subscript[s, eq]) such that a.(SuperStar[x])+b-(Subscript[s, eq])==0:

Norm[(Subscript[a, eq]).(SuperStar[x])+(Subscript[b, eq])-(Subscript[s, eq])]==0

The equality slack (Subscript[s, eq]) is typically a zero vector:

(Subscript[s, eq])//Chop

OpenersExampleSubsection2(Spacer1)Dual Model Properties  (3)

Minimize 1/2x.q.x+c.x subject to a.x+b\[VectorGreaterEqual]0 and (Subscript[a, eq]).x+(Subscript[b, eq])==0:

{q,c}={{{2,0},{0,2}},{1,1}};
{a,b}={{{-1,1},{0,1}},{-2,-1}};
{(Subscript[a, eq]),(Subscript[b, eq])}={{{1,1}},{-1}};
xv = Table[Indexed[x,i],{i,2}];

psol=QuadraticOptimization[(xv.q.xv)/2+c.xv,{a.xv+b\[VectorGreaterEqual]0,(Subscript[a, eq]).xv+(Subscript[b, eq])==0},xv]

The dual problem is to maximize -1/2ζ.q.ζ-b.λ-(Subscript[b, eq]).ν subject to q.ζ==c-(a^Τ).λ-((Subscript[a, eq])^Τ) .ν,λ\[VectorGreaterEqual]0:

{ζv,λv,νv}={Table[Indexed[ζ,i],{i,2}],Table[Indexed[λ,i],{i,2}],Table[Indexed[ν,i],{i,1}]};

dsol=QuadraticOptimization[(ζv.q.ζv)/2+b.λv+(Subscript[b, eq]).νv,{q.ζv+a\[Transpose].λv+(Subscript[a, eq])\[Transpose].νv==c,λv\[VectorGreaterEqual]0},Join[ζv,λv,νv]]

The primal minimum value and the dual maximum value coincide because of strong duality:

{(xv.q.xv)/2+c.xv /.psol,-((ζv.q.ζv)/2+b.λv+(Subscript[b, eq]).νv)/.dsol}

So it has a duality gap of zero. In general, 1/2x.q.x+c.x≤-1/2ζ.q.ζ-b.λ-(Subscript[b, eq]).ν at optimal points:

QuadraticOptimization[(xv.q.xv)/2+c.xv,{a.xv+b\[VectorGreaterEqual]0,(Subscript[a, eq]).xv+(Subscript[b, eq])==0},xv,"DualityGap"]

Construct the dual problem using constraint matrices extracted from the primal problem:

obj=x^2+y^2+x+y;
constraints={-x+y≥2,y≥1,x+y==1};

QuadraticOptimization[obj,constraints,{x,y}]

Extract the objective and constraint matrices and vectors:

{q,c,{a,b},{(Subscript[a, eq]),(Subscript[b, eq])}}=QuadraticOptimization[obj,constraints,{x,y},
 {"ObjectiveMatrix", "ObjectiveVector","LinearInequalityConstraints", "LinearEqualityConstraints"}];

The dual problem is to maximize -1/2ζ.q.ζ-b.λ-(Subscript[b, eq]).ν subject to q.ζ==c-(a^Τ).λ-((Subscript[a, eq])^Τ) .ν,λ\[VectorGreaterEqual]0:

QuadraticOptimization[((ζ.q.ζ)/2+b.λ+(Subscript[b, eq]).ν),{q.ζ+a\[Transpose].λ+(Subscript[a, eq])\[Transpose].ν==c,λ\[VectorGreaterEqual]0},{ζ,λ,ν}]

Get the dual maximum value directly using solution properties:

QuadraticOptimization[x^2+y^2+x+y,{-x+y≥2,y≥1,x+y==1},{x,y},"DualMaximumValue"]

Get the dual maximizer directly using solution properties:

QuadraticOptimization[x^2+y^2+x+y,{-x+y≥2,y≥1,x+y==1},{x,y},"DualMaximizer"]

OpenersExampleSubsection3(Spacer1)Sensitivity Properties  (4)

Use "ConstraintSensitivity" to find the change in optimal value due to constraint relaxations:

{q,c}={{{2,0},{0,2}},{0,0}};
{a,b}={{{-1,-1},{-1,2}},{-1,0}};
(SuperStar[p])=QuadraticOptimization[{q,c},{a,b},"PrimalMinimumValue"];

The first vector is inequality sensitivity and the second is equality sensitivity:

{(Subscript[s, λ]),(Subscript[s, ν])}=QuadraticOptimization[{q,c},{a,b},"ConstraintSensitivity"]

Consider new constraints a.x+b+δb\[VectorGreaterEqual]0 where δb is the relaxation:

δb={0.01,0.01};

The approximate new optimal value is given by:

(SuperStar[p])+δb.(Subscript[s, λ])

Compare to directly solving the relaxed problem:

QuadraticOptimization[{q,c},{a,b+δb},"PrimalMinimumValue"]

Each sensitivity is associated with an inequality or equality constraint:

{(Subscript[s, λ]),(Subscript[s, ν])}=QuadraticOptimization[(x^2)+(y^2)+(z^2)-2x-6y+z,{x+2y≥4,x≤3,x+y+z==1,x-y==2},{x,y,z},"ConstraintSensitivity"]

Extract the constraints:

{{a,b},{(Subscript[a, eq]),(Subscript[b, eq])}}=QuadraticOptimization[(x^2)+(y^2)+(z^2)-2x-6y+z,{x+2y≥4,x≤3,x+y+z==1,x-y==2},{x,y,z},{"LinearInequalityConstraints","LinearEqualityConstraints"}];

The inequality constraints and their associated sensitivity:

Table[{a[[i]].{x,y,z}+b[[i]]≥0,(Subscript[s, λ])[[i]]},{i,2}]

The equality constraints and their associated sensitivity:

Table[{(Subscript[a, eq])[[i]].{x,y,z}+(Subscript[b, eq])[[i]]==0,(Subscript[s, ν])[[i]]},{i,2}]

The change in optimal value due to constraint relaxation is proportional to the sensitivity value:

q={{2,0,0},{0,2,0},{0,0,2}};
c={-2,-6,1};
{a,b}={{{1,2,0},{-1,0,0}},{-4,3}};
{(Subscript[a, eq]),(Subscript[b, eq])}={{{1,1,1},{1,-1,0}},{-1,-2}};

Compute the minimal value and constraint sensitivity:

{(SuperStar[p]),{(Subscript[s, λ]),(Subscript[s, ν])}}=QuadraticOptimization[{q,c},{a,b},{(Subscript[a, eq]),(Subscript[b, eq])},{"PrimalMinimumValue","ConstraintSensitivity"}]

A zero sensitivity will not change the optimal value if the constraint is relaxed:

(Subscript[s, λ])[[2]]

Chop[(SuperStar[p])-QuadraticOptimization[{q,c},{a,b+{0,0.01}},{(Subscript[a, eq]),(Subscript[b, eq])},"PrimalMinimumValue"]]==0

A negative sensitivity will decrease the optimal value:

(Subscript[s, λ])[[1]]

(SuperStar[p])>QuadraticOptimization[{q,c},{a,b+{0.1,0}},{(Subscript[a, eq]),(Subscript[b, eq])},"PrimalMinimumValue"]

A positive sensitivity will increase the optimal value:

(Subscript[s, ν])[[1]]

(SuperStar[p])<QuadraticOptimization[{q,c},{a,b},{(Subscript[a, eq]),(Subscript[b, eq])+{0.1,0}},"PrimalMinimumValue"]

The "ConstraintSensitivity" is related to the dual maximizer of the problem:

{{(Subscript[s, λ]),(Subscript[s, ν])},{ζ,(SuperStar[λ]),(SuperStar[ν])}}=QuadraticOptimization[(x^2)+(y^2)+(z^2)-2x-6y+z,{x+2y≥4,x≤3,x+y+z==1,x-y==2},{x,y,z},{"ConstraintSensitivity","DualMaximizer"}]

The inequality sensitivity (Subscript[s, λ])==((∂(SuperStar[p]))/(∂b)) satisfies (Subscript[s, λ])==-(SuperStar[λ]), where (SuperStar[λ]) is the dual inequality maximizer:

(Subscript[s, λ])==-(SuperStar[λ])

The equality sensitivity (Subscript[s, ν])==((∂(SuperStar[p]))/(∂(Subscript[b, eq]))) satisfies (Subscript[s, ν])==-(SuperStar[ν]), where (SuperStar[ν]) is the dual equality maximizer:

(Subscript[s, ν])==-(SuperStar[ν])

OpenersExampleSection2(Spacer1)Options  (7)

OpenersExampleSubsection4(Spacer1)Method  (4)

The method "COIN" uses the COIN library:

obj=2(x^2)+20(y^2)+6x y+5x;
cons={-x+y≥2,y≥0};

QuadraticOptimization[obj,cons,{x,y},Method->"COIN"]

"CSDP" and "DSDP" reduce to semidefinite optimization:

QuadraticOptimization[obj,cons,{x,y},Method->"CSDP"]

QuadraticOptimization[obj,cons,{x,y},Method->"DSDP"]

"SCS" reduces to conic optimization:

QuadraticOptimization[obj,cons,{x,y},Method->"SCS"]

For least-squares-type quadratic problems, "CSDP" and "DSDP" will be slower than "COIN" or "SCS":

SeedRandom[123];
mat=RandomReal[{-1,1},{400,10}];
rhs=RandomReal[{-1,1},400];

Solve the least-squares problem using method "COIN":

AbsoluteTiming[(Subscript[x, COIN])=QuadraticOptimization[{{mat,-rhs},{}},{},Method->"COIN"];]

Solve the problem using method "CSDP":

AbsoluteTiming[(Subscript[x, CSDP])=QuadraticOptimization[{{mat,-rhs},{}},{},Method->"CSDP"];]

Norm[(Subscript[x, COIN])-(Subscript[x, CSDP])]

Different methods may give different results for problems with more than one optimal solution:

QuadraticOptimization[x^2,{0≤x≤1},{x,y},Method->"SCS"]

QuadraticOptimization[x^2,{0≤x≤1},{x,y},Method->"COIN"]

QuadraticOptimization[x^2,{0≤x≤1},{x,y},Method->"DSDP"]

Minimizing a concave function can be done only using (RefLink[Method, paclet:ref/Method])->"COIN":

QuadraticOptimization[-(x^2),0≤x≤1,x,Method->"COIN"]

Other methods cannot be used because they require the factorization of the objective matrix:

QuadraticOptimization[-(x^2),0≤x≤1,x,Method->"CSDP"]

OpenersExampleSubsection5(Spacer1)Tolerance  (2)

A smaller (RefLink[Tolerance, paclet:ref/Tolerance]) setting gives a more precise result:

SeedRandom[123];
mat=RandomReal[{-1,1},{800,10}];
rhs =mat.RandomReal[{-1,1},10];
(Subscript[p, exact])=0;

Find the error between computed and exact minimum value using different (RefLink[Tolerance, paclet:ref/Tolerance]) settings:

err=Table[{tol,Abs[QuadraticOptimization[{{mat,-rhs},{}},{},
"PrimalMinimumValue",Tolerance->tol,Method->"SCS"]-(Subscript[p, exact])]},{tol,10.^-Range[7]}]

Visualize the change in minimum value error with respect to tolerance:

ListLogLogPlot[err,PlotRange->All,Joined->True,PlotMarkers->Automatic]

A smaller (RefLink[Tolerance, paclet:ref/Tolerance]) setting gives a more precise answer, but typically takes longer to compute:

SeedRandom[123];
mat=RandomReal[{-1,1},{500,10}];
rhs=mat.RandomReal[{-1,1},10];

A smaller tolerance takes longer:

AbsoluteTiming[ptight=QuadraticOptimization[{{mat,-rhs},{}},{},"PrimalMinimumValue",Method->"CSDP",Tolerance-> 10^-6];]

AbsoluteTiming[ploose=QuadraticOptimization[{{mat,-rhs},{}},{},"PrimalMinimumValue",Method->"CSDP",Tolerance-> 10^-3];]

The tighter tolerance gives a more precise answer:

{ptight,ploose}

OpenersExampleSubsection6(Spacer1)PerformanceGoal  (1)

Get more accurate results at the cost of higher computation time with "Quality" setting:

SeedRandom[123];
mat=RandomReal[{-1,1},{400,10}];
rhs =mat.RandomReal[{-1,1},10];
{a,b}={{UnitVector[10,1]},{0.1}};

AbsoluteTiming[pQuality=QuadraticOptimization[{{mat,-rhs},{}},{a,b},"PrimalMinimumValue",PerformanceGoal->"Quality"];]

Use "Speed" to get results quicker but at the cost of quality:

AbsoluteTiming[pSpeed=QuadraticOptimization[{{mat,-rhs},{}},{a,b},"PrimalMinimumValue",PerformanceGoal->"Speed"];]

{pQuality,pSpeed}

OpenersExampleSection3(Spacer1)Applications  (24)

OpenersExampleSubsection7(Spacer1)Basic Modeling Transformations  (7)

Maximize -((x^2)+(y^2)+1) subject to x+y≤1. Solve the maximization problem by negating the objective function:

QuadraticOptimization[((x^2)+(y^2)+1),x+y≤1,{x,y}]

Negate the primal minimum value to get the corresponding maximum value:

-QuadraticOptimization[((x^2)+(y^2)+1),x+y≤1,{x,y},"PrimalMinimumValue"]

Minimize f(x)=||(Subscript[q, f]).x-(Subscript[c, f])|| by converting the objective function into ||(Subscript[q, f]).x-(Subscript[c, f])((||)^2):

{qf,cf}=;

Construct the objective function by expanding g(x)=||(Subscript[q, f]).x-(Subscript[c, f])((||)^2)=((((Subscript[q, f]).x-(Subscript[c, f])))^Τ).((Subscript[q, f]).x-(Subscript[c, f])):

q=Transpose[qf].qf;
c=-2cf.qf;

Since (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]) minimizes 1/2 x.q.x+c.x, the matrix is multiplied by 2:

{(Subscript[x, min]),(Subscript[g, min])}=QuadraticOptimization[{2q,c},{},{"PrimalMinimizer","PrimalMinimumValue"}]

The minimal value of the original function is recovered as (Subscript[f, min])=√((Subscript[g, min])+(Subscript[c, f]).(Subscript[c, f])):

{Norm[qf.(Subscript[x, min])-cf],Sqrt[(Subscript[g, min])+cf.cf]}

(RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]) directly performs this transformation. Construct the objective function using (RefLink[Inactive, paclet:ref/Inactive]) to avoid threading:

QuadraticOptimization[Norm[Inactive[Plus][qf.x,-cf]],{},x,{"PrimalMinimizer","PrimalMinimumValue"}]

Minimize f(x)=|x+1| subject to the constraints x+y≤3,x≥1,y≥1. Transform the objective function to g(x)=(((x+1))^2) and solve the problem:

QuadraticOptimization[(x+1)^2,{x+y≤3,x≥1,y≥1},{x,y}]

Recover the original minimum value using transformation (Subscript[f, min])=√(Subscript[g, min]):

minval=QuadraticOptimization[(x+1)^2,{x+y≤3,x≥1,y≥1},{x,y},"PrimalMinimumValue"]

Sqrt[minval]

Minimize (((x+y))^2) subject to the constraints |2x+y|≤3,x≥1,y≥1. The constraint can be transformed to -3≤2x+y≤3,x≥1,y≥1:

QuadraticOptimization[(x+y)^2,{-3≤2x+y≤3,x≥1,y≥1},{x,y}]

Minimize (((x+y))^2) subject to the constraints |2x+y|≥1,x≤1,y≥1. The constraint |2x+y|≥1 can be interpreted as 2x+y≥1 || 2x+y≤-1. Solve the problem with each constraint:

res1=QuadraticOptimization[(x+y)^2,{2x+y≥1,x≤1,y≥1},{x,y},{"PrimalMinimumValue","PrimalMinimizer"}]

res2=QuadraticOptimization[(x+y)^2,{2x+y≤-1,x≤1,y≥1},{x,y},{"PrimalMinimumValue","PrimalMinimizer"}]

The optimal solution is the minimum of the two solutions:

First@MinimalBy[{res1,res2},First]

Minimize f(x.q.x+c.x) subject to a.x⪯b, where f is a non-decreasing function by instead minimizing x.q.x+c.x. The primal minimizer (SuperStar[x]) will remain the same for both problems. Consider minimizing f(x,y)=(e^(((x+y))^2)) subject to 2x-y≤1,x≥ 0,y≥0:

f=Function[{s},Exp[s]];

{minval,(Subscript[X, min])}=QuadraticOptimization[(x+y)^2,{2x-y≥1,x≥0,y≥0},{x,y},{"PrimalMinimumValue","PrimalMinimizerRules"}]

The true minimum value can be obtained by applying the function f to the primal minimum value:

f[minval]

Minimize f(z)=√z, z=x y, z≥0, subject to 2x+y≥1,-2≤y≤2:

f=Function[{s},Sqrt[s]];

{minval, (Subscript[X, min])}=QuadraticOptimization[x y,{2x+y≥1,-2≤y≤2},{x,y},{"PrimalMinimumValue","PrimalMinimizerRules"}]

Since z≥0, The solution is the true solution only if the primal minimum value is greater than 0. The true minimum value can be obtained by applying the function f to the primal minimum value:

f[minval]

OpenersExampleSubsection8(Spacer1)Data-Fitting Problems  (5)

Find a linear fit to discrete data by minimizing ||(Subscript[q, f]).x-(Subscript[c, f])((||)^2):

data=;
dataplot=ListPlot[data, PlotRange->All]

Construct the factored quadratic matrix using (RefLink[DesignMatrix, paclet:ref/DesignMatrix]):

qf=DesignMatrix[data,{1,x},x];
cf=-data[[All,2]];

Find the coefficients of the line:

res=QuadraticOptimization[{{qf,cf},0},{}]

Compare fit with data:

Show[dataplot, Plot[Evaluate[res.{1,x}],{x,0,1}]]

Find a quadratic fit to discrete data by minimizing ||(Subscript[q, f]).x-(Subscript[c, f])((||)^2):

data=;
dataplot=ListPlot[data, PlotRange->All]

Construct the factored quadratic matrix using (RefLink[DesignMatrix, paclet:ref/DesignMatrix]):

qf=DesignMatrix[data,{1,x,(x^2)},x];
cf=-data[[All,2]];

Find the coefficients of the quadratic curve:

res=QuadraticOptimization[{{qf,cf},0},{}]

Compare fit with data:

Show[dataplot, Plot[Evaluate[res.{1,x,(x^2)}],{x,0,1}]]

Fit a quadratic curve discrete data such that the first and last points of the data lie on the curve:

data=;
dataplot=ListPlot[data, PlotRange->All]

Construct the factored quadratic matrix using (RefLink[DesignMatrix, paclet:ref/DesignMatrix]):

qf=DesignMatrix[data,{1,x,(x^2)},x];
cf=-data[[All,2]];

The two equality constraints are:

aeq=qf[[{1,-1}]];
beq=cf[[{1,-1}]];

Find the coefficients of the line:

res=QuadraticOptimization[{{qf,cf},0},{},{aeq,beq}]

Compare fit with data:

Show[Plot[Evaluate[res.{1,x,(x^2)}],{x,0,1},Epilog->{Red,PointSize[0.03],Point[data[[{1,-1}]]]}],dataplot]

Find an interpolating function to noisy data using bases (Subscript[ϕ, i])(x)=(((1+(((x-(Subscript[c, i])))^2)))^(1/2)):

data=;
dataPlot = ListPlot[data, PlotRange->All]

The interpolating function will be s(x)=(∑_(i=1))(Subscript[λ, i])(Subscript[ϕ, i])(x):

bases = Sqrt[1+(x-#)^2]&/@Subdivide[-3,3,10];
qf=DesignMatrix[data,bases,x,IncludeConstantBasis->False];
cf=-data[[All,2]];

Find the coefficients of the interpolating function:

λ=QuadraticOptimization[{{qf,cf},0},{}]

Compare the fit to the data:

Show[dataPlot,Plot[Evaluate[bases.λ],{x,-3,3}]]

Minimize ||(Subscript[q, f]).x-(Subscript[c, f])|| subject to the constraints (Subscript[x, i])≥1, i=1,2,..,m:

{qf,cf}= ;

QuadraticOptimization[Norm[Inactive[Plus][qf.x,-cf]],VectorGreaterEqual[{x,1}],x]

Compare with the unconstrained minimum of ||(Subscript[q, f]).x-(Subscript[c, f])||:

LeastSquares[qf,cf]

OpenersExampleSubsection9(Spacer1)Classification Problems  (2)

Find a plane a.x+b that separates two groups of 3D points P={(Subscript[p, 1]),(Subscript[p, 2]),..(Subscript[p, n])} and Q={(Subscript[q, 1]),(Subscript[q, 2]),..(Subscript[q, m])}:

{set1,set2}=;

dataplot=ListPointPlot3D[{set1,set2},]

For separation, set 1 must satisfy a.(Subscript[p, i])+b≥1 and set 2 must satisfy a.(Subscript[q, j])+b≤-1. Find the hyperplane by minimizing ||a((||)^2):

res=QuadraticOptimization[((Norm[a])^2),{set1.a+b\[VectorGreaterEqual]1,set2.a+b\[VectorLessEqual]-1},{a,b},Method->"COIN"]

The distance between the planes a.x+b=1 and a.x+b==-1 is 2/||a||:

(2/Norm[a])/.res

The plane separating the two groups of points is:

plane=(a.{x,y,z}+b)/.res

Plot the plane separating the two datasets:

Show[dataplot, ContourPlot3D[plane==0,{x,-1,1},{y,-1,1},{z,-1,1},]]

Find a quadratic polynomial that separates two groups of 3D points P={(Subscript[p, 1]),(Subscript[p, 2]),..(Subscript[p, n])} and Q={(Subscript[q, 1]),(Subscript[q, 2]),..(Subscript[q, m])}:

{set1,set2}=;
dataplot=ListPointPlot3D[{set1,set2}, ]

Construct the quadratic polynomial data matrices for the two sets using (RefLink[DesignMatrix, paclet:ref/DesignMatrix]):

{data1,data2}=DesignMatrix[#,{x,(x^2),y,(y^2),x y},{x,y}]&/@{set1,set2};

For separation, set 1 must satisfy a.(Subscript[p, i])+b≥1 and set 2 must satisfy a.(Subscript[q, j])+b≤-1. Find the separating surface by minimizing ||a((||)^2):

res=QuadraticOptimization[((Norm[a])^2),{data1.a+b\[VectorGreaterEqual]1,data2.a+b\[VectorLessEqual]-1},{a,b},Method->"COIN"]

The polynomial separating the two groups of points is:

poly=(a.{1,x,(x^2),y,(y^2),x y}+b)/.res

Plot the polynomial separating the two datasets:

Show[dataplot, ContourPlot3D[poly==0,{x,-1.5,1.5},{y,-1.5,1.5},{z,-1,1},]]

OpenersExampleSubsection10(Spacer1)Geometric Problems  (3)

Find a point ((Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3])) closest to the point (Subscript[p, 0])=(1/2,-1,1) that lies on the planes (Subscript[x, 3])=0 and (Subscript[x, 1])-2(Subscript[x, 2])+3(Subscript[x, 3])=1:

p0={1/2,-1,1};

The the point closest to (Subscript[p, 0]) by minimizing ||(Subscript[p, 0])-x((||)^2). Use (RefLink[Inactive, paclet:ref/Inactive]) (RefLink[Plus, paclet:ref/Plus])when constructing the objective:

res=QuadraticOptimization[((Norm[-p0 +x])^2),{{0,0,1},{1,-2,3}}.x=={0,1},x]

Show[ContourPlot3D[{(Subscript[x, 3])==0 , (Subscript[x, 1])-2(Subscript[x, 2])+3(Subscript[x, 3])==1},{(Subscript[x, 1]),-1,1},{(Subscript[x, 2]),-1,1},{(Subscript[x, 3]),-1,1},],]

Find the distance between two convex polyhedra:

{region1,region2}=;

{dist,minPts}=QuadraticOptimization[Norm[x1-x2]^2,{x1∈region1,x2∈region2},{x1,x2},{"PrimalMinimumValue", "PrimalMinimizerRules"}]

Show the nearest points with the line connecting them:

Show[region1,region2,Graphics3D[{Line[{x1,x2}],Blue,PointSize[0.025],Point[{x1,x2}]} /. minPts]]

Find the radius r and center (Subscript[c, 0]) of a minimal enclosing ball that encompasses a given region:

region=;
pts=MeshCoordinates[region];

The original minimization problem is to minimize r subject to ||(Subscript[p, i])-(Subscript[c, 0])((||)^2)≤(r^2), i=1,2,.n. The dual of this problem is to maximize -(∑_i)((((Subscript[w, i])(Subscript[p, i])))^2)+(∑_i)(Subscript[w, i])(∑_(j=1))(((Subscript[p, i])((Subscript[x, j])))^2) subject to (∑_(i-1))(Subscript[w, i])=1,(Subscript[w, i])≥0:

obj=-((Norm[w.pts])^2)+Total[(pts^2),{2}].w;
constraints={w\[VectorGreaterEqual]0,Total[w]==1};

Solve the dual maximization problem:

{weights,minval}=QuadraticOptimization[-obj,constraints,w∈Vectors[Length[pts]], {"PrimalMinimizer","PrimalMinimumValue"}];
Short[weights,3]

The center of the minimal enclosing ball is (Subscript[c, 0])=(∑_(i=1))(Subscript[w, i])(Subscript[p, i]):

center=weights.pts

The radius of the minimal enclosing ball is (RefLink[Sqrt, paclet:ref/Sqrt]) of the maximum value:

radius=Sqrt[-minval]

Visualize the enclosing ball:

Show[Graphics3D[{Opacity[0.2],Red,Ball[center,radius]}], region]

The minimal enclosing ball can be found efficiently using (RefLink[BoundingRegion, paclet:ref/BoundingRegion]):

BoundingRegion[pts,"MinBall"]

OpenersExampleSubsection11(Spacer1)Investment Problems  (2)

Find the allocation of $10,000 capital between four stocks such that a minimum $1000 dividend is received. The expected return value and the covariance matrix R associated with the stocks are:

{returnValue,R}=;

The total amount to spend must be less than $10,000:

capitalConstraint=x\[VectorLessEqual]10000;

The investment must yield a minimum of $1000:

roiConstraint=returnValue.x\[VectorGreaterEqual]1000;

A negative stock cannot be bought:

minStock = x\[VectorGreaterEqual]0;

The total amount to spend on each stock is found by minimizing the risk given by x.R.x:

res=QuadraticOptimization[x.R.x,{capitalConstraint,roiConstraint,minStock},x]

The total investment to get a minimum of $1000 is:

Total[x/.res]

Find the allocation of $10,000 capital among four stocks with an option to short-sell such that a minimum dividend of $1000 is received:

{returnValue,R}=;

The capital constraint and return on investment constraints are:

constraints={x\[VectorLessEqual]10000,returnValue.x\[VectorGreaterEqual]1000};

A short-sale option allows the stock to be sold. The optimal number of stocks to buy/short-sell is found by minimizing the risk given by the objective x.R.x:

res=QuadraticOptimization[x.R.x,constraints,x]

The second stock can be short-sold. The total investment to get a minimum of $1000 due to the short-selling is:

Total[x/.res]

Without short-selling, the initial investment will be significantly more:

Total[x/.QuadraticOptimization[x.R.x,{constraints,x\[VectorGreaterEqual]0},x]]

OpenersExampleSubsection12(Spacer1)Trajectory Optimization Problems  (1)

Minimize ((∫_0)^1) (u^2)(τ) dτ subject to x''=u(t),x(0)=0, x(1)=1,x'(0)=x'(1)=0. The minimizing function integral can be approximated using the trapezoidal rule. The discretized objective function will be u.W.u:

{ti,wts}=Most[];
W=DiagonalMatrix[SparseArray[wts]];
obj = u.W.u;

The constraint x''=u can be discretized using finite differences:

mat=;
ode=mat.x==u;

The constraints x(0)=0, x(1)=1 can be represented using the (RefLink[Indexed, paclet:ref/Indexed]) function:

bc1={Indexed[x,1]==0,Indexed[x,Length[ti]]==1};

The constraints x'(0)=x'(1)=0 can be discretized using finite differences, and only the first and last rows are used:

mat = ;
bc2= mat[[{1,-1}]].x==0;

Solve the discretized trajectory problem:

res=QuadraticOptimization[obj,{ode,bc1,bc2},{u,x}];
Short[res,6]

Convert the discretized result into an (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction]):

{xsol,usol} = ListInterpolation[#,ti]&/@({x,u}/.res);

Compare the result with the analytic solution:

Row[{Plot[{xsol[t],3(t^2)-2(t^3)},{t,0,1},],Plot[{usol[t],6-12t},{t,0,1},]}]

OpenersExampleSubsection13(Spacer1)Optimal Control Problems  (2)

Minimize ((∫_0)^10)(((x(10)-x(t)))^T)(x(10)-x(t))+((u(t))^2)dt, subject to x'(t)=A.x(t)+u(t) and  x(0)=(1,0), x(10)=(0,1):

A=(({{-1, 0.5}, {0.3, -1}}));

The integral can be discretized using the trapezoidal method:

{ti,wts}=10*Most[];
W=DiagonalMatrix[SparseArray[wts]];

((∫_0)^10)(((x(10)-x(t)))^T)(x(10)-x(t))+((u(t))^2)dt=((∫_0)^10)(((Subscript[x, 1])(t))^2)dt+((∫_0)^10)(((1-(Subscript[x, 2])(t)))^2)dt+((∫_0)^10)((u(t))^2)dt . The objective function is:

objective = x1.W.x1 + s.W.s + u.W.u;
auxConstraint = s==(1-x2);

The time derivative in x'(t)=A.x(t)+u(t) is discretized using finite differences:

dTMat = ;
ode1=dTMat.x1== A[[1]].{x1,x2} + u;
ode2=dTMat.x2== A[[2]].{x1,x2} + u;

The end-condition constraints  x(0)=(1,0), x(10)=(0,1) can be specified using (RefLink[Indexed, paclet:ref/Indexed]):

bc1 = {Indexed[x1,1]==1,Indexed[x2,1]==0};
bc2={ Indexed[x1,Length[ti]]==0,Indexed[x2,Length[ti]]==1};

Solve the discretized problem:

res=QuadraticOptimization[objective,{ode1,ode2,bc1,bc2,auxConstraint},{u,x1,x2,s}];

Convert the discretized result into (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction]):

{usol,x1sol,x2sol}=Map[ListInterpolation[#,ti]&,{u,x1,x2}/.res];

Plot the control variable:

Plot[usol[t],{t,0,10}]

Plot the state variables:

Plot[{x1sol[t],x2sol[t]},{t,0,10}]

Minimize ((∫_0)^10)(((x(10)-x(t)))^T)(x(10)-x(t))+((u(t))^2)dt subject to x'(t)=A.x(t)+u(t) and  x(0)=(1,0), x(10)=(1/2,1) and -10≤u(t)≤5 on the control variable:

A=(({{-1, 0.5}, {0.3, -1}}));

The integral can be discretized using the trapezoidal method:

{ti,wts}=10*Most[];
W=DiagonalMatrix[SparseArray[wts]];

((∫_0)^10)(((x(10)-x(t)))^T)(x(10)-x(t))+((u(t))^2)dt=((∫_0)^10)(((1/2-(Subscript[x, 1])(t)))^2)dt+((∫_0)^10)(((1-(Subscript[x, 2])(t)))^2)dt+((∫_0)^10)((u(t))^2)dt. The objective function is:

objective = g.W.g + s.W.s + u.W.u;
auxConstraint1=g==(1/2-x1);
auxConstraint2 = s==(1-x2);

The time derivative in x'(t)=A.x(t)+u(t) is discretized using finite differences:

dTMat = ;
ode1=dTMat.x1== A[[1]].{x1,x2} + u;
ode2=dTMat.x2== A[[2]].{x1,x2} + u;

The end-condition constraints  x(0)=(1,0), x(10)=(1/2,1) can be specified using (RefLink[Indexed, paclet:ref/Indexed]):

bc1 = {Indexed[x1,1]==1,Indexed[x2,1]==0};
bc2={ Indexed[x1,Length[ti]]==1/2,Indexed[x2,Length[ti]]==1};

The constraint on the control variable u(t) is:

controlConstraint={u\[VectorGreaterEqual]-10,u\[VectorLessEqual]5};

Solve the discretized problem:

res=QuadraticOptimization[objective,{ode1,ode2,bc1,bc2,auxConstraint1,auxConstraint2, controlConstraint},{u,x1,x2,s,g}];

Convert the discretized result into (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction]):

{usol,x1sol,x2sol}=Map[ListInterpolation[#,ti]&,{u,x1,x2}/.res];

The control variable is now restricted between -10 and 5:

Plot[usol[t],{t,0,10}, PlotRange->All]

Plot the state variables:

Plot[{x1sol[t],x2sol[t]},{t,0,10}]

OpenersExampleSubsection14(Spacer1)Sequential Quadratic Optimization  (2)

Minimize a nonlinear function f(x) subject to nonlinear constraints g(x)≥0. The minimization can be done by approximating f(x) as f(x+d)≈f(x)+(∇f).d+(1/2)d.((∇^2)f(x)).d and the constraints as g(x) as g(x+d)=g(x)+(∇g).d≥ 0. This leads to a quadratic minimization problem that can be solved iteratively. Consider a case where f=1-Exp((((((Sin(x))^2)+((Sin(y-1/2))^2)))^2)) and g={x≤0.5, y≥- 0.5, -x+y≤ 1}:

f[{x_,y_}]:=1-Exp[-(Sin[x]^2+Sin[y-1/2]^2)];
g[{x_,y_}] := {-x+0.5,y+0.5,x-y+1};

The gradient and Hessian of the minimizing function are:

df[{x_,y_}]= D[f[{x,y}],{{x,y}}];
hf[{x_,y_}]=D[f[{x,y}],{{x,y},2}];

The gradient of the constraints is:

dg[{x_,y_}]= D[g[{x,y}],{{x,y}}];

The subproblem is to find d by minimizing (1/2)d.((∇^2)f((SuperStar[x]))).d+(∇f).d subject to (Subscript[g, i])((SuperStar[x]))+(∇(Subscript[g, i])((SuperStar[x]))). d≥0:

QuadraticSubProblem[xiter_]:= Module[{q,c,a,b},
{q,c,a,b}=#[xiter]&/@{hf,df,dg,g};
QuadraticOptimization[{q,c},{a,b}]
];

Iterate starting with an initial guess of (SuperStar[x])=(-1, -0.4). The next iterate is (Subscript[x, new])=(SuperStar[x])+s.d, where s∈[0,1] is the step length to ensure that the constraints are always satisfied:

(SuperStar[x])={-1,-0.4};
xp = Last@Reap[Do[
Sow[(SuperStar[x]),"IterationPoints"];
d = QuadraticSubProblem[(SuperStar[x])];
If[Norm[d]≤10^-8, Break[]];
s=1;
While[Min[g[(SuperStar[x])+s*d]]≤0,s/=2];
(SuperStar[x])+=s*d;
,{20}],"IterationPoints"]

Visualize the convergence of the result. The final result is the green point:

ContourPlot[f[{x,y}],{x,-1.5,0.5},{y,-0.5,1.5},]

Minimize f(x,y)=√(1+(x^2)+((Cos(y/2))^2)) subject to x+Sin(y)≥1,y≤2, x y=2:

f[{x_,y_}]:=Sqrt[(1+x^2+Cos[y/2]^2)];
g[{x_,y_}] := {x+Sin[2y]-1,-y+2};
gEq[{x_,y_}]:={x y-2};

The gradient and Hessian of the minimizing function are:

df[{x_,y_}]= D[f[{x,y}],{{x,y}}];
hf[{x_,y_}]=D[f[{x,y}],{{x,y},2}];

The gradient of the constraints is:

dg[{x_,y_}]= D[g[{x,y}],{{x,y}}];
dgEq[{x_,y_}]=D[gEq[{x,y}],{{x,y}}];

The subproblem is to minimize (1/2)d.((∇^2)f((SuperStar[x]))).d+(∇f).d, subject to (Subscript[g, i])((SuperStar[x]))+(∇(Subscript[g, i])((SuperStar[x]))). d≥0 and (Subscript[(g^eq), i])((SuperStar[x]))+(∇(Subscript[(g^eq), i])((SuperStar[x]))). d=0:

QuadraticSubProblem[xiter_]:= Module[{q,c,a,b,aeq,beq},
{q,c,a,b,aeq,beq}=#[xiter]&/@{hf,df,dg,g,dgEq,gEq};
QuadraticOptimization[{q,c},{a,b},{aeq,beq}]
];

Iterate starting with the initial guess (SuperStar[x])=(0.5,0.5):

(SuperStar[x])={0.5,0.5};
xp=Last@Reap[Do[
Sow[(SuperStar[x]),"IterationPoints"];
d = QuadraticSubProblem[(SuperStar[x])];
If[Norm[d]≤10^-8, Break[]];
s=1;
While[Min[g[(SuperStar[x])+s*d]]≤0 && s>10^-9,s/=2];
(SuperStar[x])+=s*d;
,{30}],"IterationPoints"]

Visualize the convergence of the result. The final result is the green point:

ContourPlot[f[{x,y}],{x,0,2.5},{y,0,2},]

OpenersExampleSection4(Spacer1)Properties & Relations  (9)

(RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]) gives the global minimum of the objective function:

{(Subscript[x, min]),minval} = QuadraticOptimization[(x^2)+(y^2)-2x-6y,{x+2y≥4,x≤3},{x,y},{"PrimalMinimizer","PrimalMinimumValue"}]

Visualize the objective function:

Show[Plot3D[(x^2)+(y^2)-2x-6y,],Graphics3D[{Blue,PointSize[0.05],Point[Append[(Subscript[x, min]),minval]]}]]

The minimizer can be in the interior or at the boundary of the feasible region:

Table[{(Subscript[x, min]), minval}= QuadraticOptimization[(x^2)+(y^2)+c.{x,y},];
,{c,{{-2,-6},{-10,-6},{-10,6}}}]

(RefLink[Minimize, paclet:ref/Minimize]) gives global exact results for quadratic optimization problems:

Minimize[{2(x^2)+20(y^2)+6x y+5x,{-x+y≥2,y≥0}},{x,y}]

(RefLink[NMinimize, paclet:ref/NMinimize]) can be used to obtain approximate results using global methods:

NMinimize[{2(x^2)+20(y^2)+6x y+5x,{-x+y≥2,y≥0}},{x,y}]

(RefLink[FindMinimum, paclet:ref/FindMinimum]) can be used to obtain approximate results using local methods:

FindMinimum[{2(x^2)+20(y^2)+6x y+5x,{-x+y≥2,y≥0}},{x,y}]

(RefLink[LinearOptimization, paclet:ref/LinearOptimization]) is a special case of (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]):

QuadraticOptimization[x+2y,{3x+4y≥5&&x≥0&&y≥0},{x,y}]

LinearOptimization[x+2y,{3x+4y≥5&&x≥0&&y≥0},{x,y}]//N

In matrix-vector form, the quadratic term is set to 0:

QuadraticOptimization[{0,{1,2}},{{{3,4},{1,0},{0,1}},{-5,0,0}}]

(RefLink[SecondOrderConeOptimization, paclet:ref/SecondOrderConeOptimization]) is a generalization of (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]):

obj=2(x^2)+20(y^2)+6x y+5x;
cons={-x+y≥2,y≥0};

QuadraticOptimization[obj,cons,{x,y}]

Use auxiliary variable t and minimize t with additional constraint 2(x^2)+20(y^2)+6x y+5x≤t:

SecondOrderConeOptimization[t,{obj≤t,cons},{x,y,t}]

(RefLink[SemidefiniteOptimization, paclet:ref/SemidefiniteOptimization]) is a generalization of (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]):

obj=2(x^2)+20(y^2)+6x y+5x;
cons={-x+y≥2,y≥0};

QuadraticOptimization[obj,cons,{x,y}]

Use auxiliary variable t and minimize t with additional constraint 2(x^2)+20(y^2)+6x y+5x≤t:

SemidefiniteOptimization[t,{obj≤t,cons},{x,y,t}]

(RefLink[ConicOptimization, paclet:ref/ConicOptimization]) is a generalization of (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]):

obj=2(x^2)+20(y^2)+6x y+5x;
cons={-x+y≥2,y≥0};

QuadraticOptimization[obj,cons,{x,y}]

Use auxiliary variable t and minimize t with additional constraint 2(x^2)+20(y^2)+6x y+5x≤t:

ConicOptimization[t,{obj≤t,cons},{x,y,t}]

OpenersExampleSection5(Spacer1)Possible Issues  (4)

Constraints specified using strict inequalities may not be satisfied for certain methods:

{obj,cons}={x^2+x+y,{x-2y≤3,x>1,y>0}};

The reason is that (RefLink[QuadraticOptimization, paclet:ref/QuadraticOptimization]) solves inf {(x^2)+x+y|x-2 y≤3,x>1,y>0}:

cons/.QuadraticOptimization[obj,cons,{x,y},Method->"CSDP"]

The minimum value of an empty set or infeasible problem is defined to be ∞:

QuadraticOptimization[x^2,{x≤0,x≥1},x,"PrimalMinimumValue"]

The minimizer is (RefLink[Indeterminate, paclet:ref/Indeterminate]):

QuadraticOptimization[x^2,{x≤0,x≥1},x]

The minimum value for an unbounded set or unbounded problem is -∞:

QuadraticOptimization[x^2+y,0≤x≤1,{x,y},"PrimalMinimumValue",Method->"CSDP"]

The minimizer is (RefLink[Indeterminate, paclet:ref/Indeterminate]):

QuadraticOptimization[x^2+y,0≤x≤1,{x,y},Method->"CSDP"]

Certain solution properties are not available for symbolic input:

obj=(((x-1))^2)+(((y-5/2))^2);
cons=x+2y≤4;

QuadraticOptimization[obj,cons,{x,y},"FactoredObjectiveMatrix"]

QuadraticOptimization[obj,cons,{x,y},"FactoredObjectiveVector"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[LinearOptimization, paclet:ref/LinearOptimization])  ▪  (RefLink[SecondOrderConeOptimization, paclet:ref/SecondOrderConeOptimization])  ▪  (RefLink[SemidefiniteOptimization, paclet:ref/SemidefiniteOptimization])  ▪  (RefLink[FindMinimum, paclet:ref/FindMinimum])  ▪  (RefLink[NMinimize, paclet:ref/NMinimize])  ▪  (RefLink[Minimize, paclet:ref/Minimize])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Optimization, paclet:guide/Optimization])}, {▪(RefLinkPlain[Convex Optimization, paclet:guide/ConvexOptimization])}})}})