issatisfied
Constraint satisfaction of an optimization problem at a set of points
Since R2024a
Syntax
Description
Examples
Check Constraint Satisfaction in Optimization Problem
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, and determine where all the constraints are satisfied among the points.
vals = optimvalues(prob,x=xvals,y=yvals); allsat = issatisfied(prob,vals);
Plot the feasible (satisfied) points with green circles and the infeasible points with red x marks.
xsat = xvals(allsat); ysat = yvals(allsat); xnosat = xvals(~allsat); ynosat = yvals(~allsat); plot(xsat,ysat,"go",xnosat,ynosat,"rx")
Change Constraint Satisfaction Tolerance
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, and determine where all the constraints are satisfied among the points.
vals = optimvalues(prob,x=xvals,y=yvals); allsat = issatisfied(prob,vals);
Plot the feasible (satisfied) points with green circles and the infeasible points with red x marks.
xsat = xvals(allsat); ysat = yvals(allsat); xnosat = xvals(~allsat); ynosat = yvals(~allsat); plot(xsat,ysat,"go",xnosat,ynosat,"rx")
Determine which points are feasible with respect to a tolerance of 1 rather than the default 1e-6.
tol = 1; allsat1 = issatisfied(prob,vals,tol);
Plot the feasible (satisfied) points with green circles and the infeasible points with red x marks.
xsat1 = xvals(allsat1); ysat1 = yvals(allsat1); xnosat1 = xvals(~allsat1); ynosat1 = yvals(~allsat1); plot(xsat1,ysat1,"go",xnosat1,ynosat1,"rx")
With a looser definition of constraint satisfaction, more points are feasible.
Determine Which Constraints are Satisfied
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.
vals = optimvalues(prob,x=xvals,y=yvals);
Evaluate the constraints at the points. issatisfied
evaluates all the constraint satisfactions at all the test points simultaneously.
[~,sat] = issatisfied(prob,vals);
Plot the feasible (satisfied) points for the first constraint with green circles and the infeasible points with red x marks.
x1sat = xvals(sat.cons1); y1sat = yvals(sat.cons1); x1nosat = xvals(~sat.cons1); y1nosat = yvals(~sat.cons1); plot(x1sat,y1sat,"go",x1nosat,y1nosat,"rx") title("Constraint 1 Satisfaction")
Repeat this process for the other three constraints.
x2sat = xvals(sat.cons2); y2sat = yvals(sat.cons2); x2nosat = xvals(~sat.cons2); y2nosat = yvals(~sat.cons2); plot(x2sat,y2sat,"go",x2nosat,y2nosat,"rx") title("Constraint 2 Satisfaction")
x3sat = xvals(sat.cons3); y3sat = yvals(sat.cons3); x3nosat = xvals(~sat.cons3); y3nosat = yvals(~sat.cons3); plot(x3sat,y3sat,"go",x3nosat,y3nosat,"rx") title("Constraint 3 Satisfaction")
x4sat = xvals(sat.cons4); y4sat = yvals(sat.cons4); x4nosat = xvals(~sat.cons4); y4nosat = yvals(~sat.cons4); plot(x4sat,y4sat,"go",x4nosat,y4nosat,"rx") title("Constraint 4 Satisfaction")
The plots show the different regions of satisfaction for each constraint.
Input Arguments
prob
— Optimization problem
OptimizationProblem
object
Optimization problem, specified as an OptimizationProblem
object. Create prob
using optimproblem
. The issatisfied
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)
tol
— Tolerance for constraint satisfaction
1e-6
(default) | nonnegative scalar
Tolerance for constraint satisfaction, specified as a nonnegative scalar. A
constraint is considered to be satisfied if it evaluates to no more than
tol
. Otherwise, the constraint is unsatisfied.
For example, if c(x)
is a nonlinear inequality constraint
function, then when c(pt)
≤ tol
,
issatisfied
returns true
for that constraint
at the point pt
. Similarly, if ceq(x)
is a
nonlinear equality constraint function, then when abs(ceq(pt))
≤
tol
, issatisfied
returns
true
for that constraint at the point pt
.
Data Types: double
Output Arguments
allsat
— Indication that all constraints are satisfied
logical vector
Indication that all constraints are satisfied at the given points, returned as a
logical vector. allsat(i)
corresponds to the query point
pts(i)
. The returned value is true
if all values
of the constraint functions are no more than tol
.
sat
— Indication that a constraint is satisfied at the given points
OptimizationValues
vector
Indication that a constraint is satisfied at the given points, returned as an OptimizationValues
vector. Index into
sat
using the constraint names. For example, if a constraint is
named mycons
, then sat.mycons
is a logical array
with true
values for satisfied constraints.
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 R2024a
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)