evaluate
Evaluate optimization expression or objectives and constraints in problem
Description
Use evaluate
to find the numeric value of an optimization
expression at a point, or to find the values of objective and constraint expressions in
an optimization problem at a set of points.
Tip
For the full workflow, see Problem-Based Optimization Workflow or Problem-Based Workflow for Solving Equations.
Examples
Evaluate Optimization Expression at Point
Create an optimization expression of two variables.
x = optimvar("x",3,2); y = optimvar("y",1,2); expr = sum(x,1) - 2*y;
Evaluate the expression at a point.
xmat = [3,-1; 0,1; 2,6]; sol.x = xmat; sol.y = [4,-3]; val = evaluate(expr,sol)
val = 1×2
-3 12
Evaluate Objective Function at Solution
Solve a linear programming problem.
x = optimvar('x'); y = optimvar('y'); prob = optimproblem; prob.Objective = -x -y/3; prob.Constraints.cons1 = x + y <= 2; prob.Constraints.cons2 = x + y/4 <= 1; prob.Constraints.cons3 = x - y <= 2; prob.Constraints.cons4 = x/4 + y >= -1; prob.Constraints.cons5 = x + y >= 1; prob.Constraints.cons6 = -x + y <= 2; sol = solve(prob)
Solving problem using linprog. Optimal solution found.
sol = struct with fields:
x: 0.6667
y: 1.3333
Find the value of the objective function at the solution.
val = evaluate(prob.Objective,sol)
val = -1.1111
Evaluate Optimization Problem Values
Create an optimization problem with several linear and nonlinear constraints.
x = optimvar("x"); y = optimvar("y"); obj = (10*(y - x^2))^2 + (1 - x)^2; cons1 = x^2 + y^2 <= 1; cons2 = x + y >= 0; cons3 = y <= sin(x); cons4 = 2*x + 3*y <= 2.5; prob = optimproblem(Objective=obj); prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2; prob.Constraints.cons3 = cons3; prob.Constraints.cons4 = cons4;
Create 100 test points randomly.
rng default % For reproducibility xvals = randn(1,100); yvals = randn(1,100);
Convert the points to an OptimizationValues
object for the problem.
pts = optimvalues(prob,x=xvals,y=yvals);
Evaluate the objective and constraint functions at the points pts
.
val = evaluate(prob,pts);
The objective function values are stored in val.Objective
, and the constraint function values are stored in val.cons1
through val.cons4
. Plot the log of 1 plus the objective function values.
figure
plot3(xvals,yvals,log(1 + val.Objective),"bo")
Plot the values of the constraints cons1
and cons4
. Recall that constraints are satisfied when they evaluate to a nonpositive number. Plot the nonpositive values with circles and the positive values with x marks.
neg1 = val.cons1 <= 0; pos1 = val.cons1 > 0; neg4 = val.cons4 <= 0; pos4 = val.cons4 > 0; figure plot3(xvals(neg1),yvals(neg1),val.cons1(neg1),"bo") hold on plot3(xvals(pos1),yvals(pos1),val.cons1(pos1),"rx") plot3(xvals(neg4),yvals(neg4),val.cons4(neg4),"ko") plot3(xvals(pos4),yvals(pos4),val.cons4(pos4),"gx") hold off
As the last figure shows, evaluate
enables you to calculate both the value and the feasibility of points. In contrast, issatisfied
calculates only the feasibility.
Input Arguments
expr
— Optimization expression
OptimizationExpression
object
Optimization expression, specified as an OptimizationExpression
object.
Example: expr = 5*x+3
, where x
is an
OptimizationVariable
pt
— Values of variables in expression
structure
Values of the variables in an expression, specified as a structure. The
structure pt
has the following requirements:
All variables in
expr
must match field names inpt
.The values of the matching field names must be numeric.
The sizes of the fields in
pt
must match the sizes of the corresponding variables inexpr
.
For example, pt
can be the solution to an optimization
problem, as returned by solve
.
Example: pt.x = 3, pt.y = -5
Data Types: struct
prob
— Optimization problem
OptimizationProblem
object
Optimization problem, specified as an OptimizationProblem
object. Create prob
using optimproblem
. The evaluate
function evaluates
the objectives and constraints in the properties of prob
at the
points in pts
.
Example: prob = optimproblem(Objective=obj,Constraints=constr)
pts
— Points to evaluate for prob
structure | OptimizationValues
object
Points to evaluate for prob
, specified as a structure or an OptimizationValues
object.
The field names in
pts
must match the corresponding variable names in the objective and constraint expressions inprob
.The values in
pts
must be numeric arrays of the same size as the corresponding variables inprob
.
If you use a structure for pts
, then pts
can
contain only one point. In other words, if you want to evaluate multiple points
simultaneously, pts
must be an OptimizationValues
object.
Example: pts = optimvalues(prob,x=xval,y=yval)
Output Arguments
val
— Evaluation result
double | OptimizationValues
object
Evaluation result, returned as a double or as an OptimizationValues
object. If val
is a
double, it contains the numeric value of the expression at
pt
. If val
is an
OptimizationValues
object, it contains values for the
objective and constraint functions in prob
evaluated at
the points in pts
.
Warning
The problem-based approach does not support complex values in the following: an objective function, nonlinear equalities, and nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result might be incorrect.
More About
Constraint Expression Values
For a constraint expression at a point pt
:
If the constraint is
L <= R
, the constraint value isevaluate(L,pt)
–evaluate(R,pt)
.If the constraint is
L >= R
, the constraint value isevaluate(R,pt)
–evaluate(L,pt)
.If the constraint is
L == R
, the constraint value isabs(evaluate(L,pt) – evaluate(R,pt))
.
Generally, a constraint is considered to be satisfied (or feasible) at a point if the constraint value is less than or equal to a tolerance.
Version History
Introduced in R2017bR2024a: Evaluate objective and constraint expressions in optimization problems at multiple points
The evaluate
function now applies to objective and constraint
expressions in OptimizationProblem
objects. If the evaluation
points are passed as an OptimizationValues
object, then the
function evaluates the expressions at all points in the object. For an example, see
Evaluate Optimization Problem Values.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)