How to choose one between two constraint conditions

clear;clc;
x = optimvar('x',1,1,'LowerBound',0)
prob=optimproblem;
prob.Constraints.con1=x<=10 or prob.Constraints.con1=x^2<=10
[sol,faval,exit]=solve(prob,'Solver','ga')

1 Comment

Have you ever encountered the same problem? I have been troubled by this problem many times

Sign in to comment.

 Accepted Answer

Considering Walter's answer, your example may not have captured your real question. If you really do have a feasible set of the form region A or region B, where A and B are disjoint in the space of x, such as in this modified example,
clear;clc;
x = optimvar('x',1,'Lower',0);
prob=optimproblem;
prob.Constraints.con = (x<=1 | x>=5)
[sol,faval,exit]=solve(prob,'Solver',_____)
you normally have to deal with such situations by solving the optimization twice, once over A and once over B, and selecting the better of the two results. This is because local optimization solvers like fmincon can generally only search incrementally over a contiguous feasible set.
In the case of global, non-derivative-based solvers like 'ga', you might, however, be able to get away with the following,
prob.Constraints.con = fnc2optimexpr( @(z) min(z-1, 5-z) ,x)<=0;

7 Comments

clear;clc;
x = optimvar('x',2,2);
y=optimvar('y',2,2)
prob=optimproblem('Objective',sum(x.^2+y.^2,'all'));
prob.Constraints.con1=x(1,1)>=10 or prob.Constraints.con1=y(1,1)>=15;
prob.Constraints.con2=x(1,2)>=6 or prob.Constraints.con1=y(1,2)>=3;
prob.Constraints.con3=x(2,1)>=10 or prob.Constraints.con1=y(2,1)>=15;
prob.Constraints.con1=x(2,2)>=4 or prob.Constraints.con1=y(2,2)>=2;
%(i mean x>=some matirx or y>=some matrix)
%what if this problem what can i do?
[sol,faval,exit]=solve(prob,'Solver','ga')
for this problem i have tried a solution and it's successed!
clear;clc;
x = optimvar('x',2,2);
y=optimvar('y',2,2);
c=optimvar('c',2,2,'Type','integer','LowerBound',0,'UpperBound',1);
prob=optimproblem('Objective',sum(x.^2+y.^2,"all"));
%prob.Constraints.con1=x(1,1)>=10 or prob.Constraints.con1=y(1,1)>=15;
%prob.Constraints.con2=x(1,2)>=6 or prob.Constraints.con1=y(1,2)>=3;
%prob.Constraints.con3=x(2,1)>=10 or prob.Constraints.con1=y(2,1)>=15;
%prob.Constraints.con1=x(2,2)>=4 or prob.Constraints.con1=y(2,2)>=2;
%(i mean x>=some matirx or y>=some matrix)
%what if this problem what can i do?
xx=[10 6;10 4];yy=[15 3;15 2];
prob.Constraints.con=(c.*x+(1-c).*y)>=(c.*xx+(1-c).*yy);
x0.x=xx;x0.y=yy;x0.c=ones(2,2);
[sol,faval,exit]=solve(prob,x0,"Solver","ga")
Solving problem using ga. ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
sol = struct with fields:
c: [2x2 double] x: [2x2 double] y: [2x2 double]
faval = 715.0000
exit =
SolverConvergedSuccessfully
There are 2^4 = 16 possibilities to combine the constraints
prob.Constraints.con1=x(1,1)>=10 or prob.Constraints.con1=y(1,1)>=15;
prob.Constraints.con2=x(1,2)>=6 or prob.Constraints.con1=y(1,2)>=3;
prob.Constraints.con3=x(2,1)>=10 or prob.Constraints.con1=y(2,1)>=15;
prob.Constraints.con1=x(2,2)>=4 or prob.Constraints.con1=y(2,2)>=2;
Thus call "solve" 16 times and choose the solution with the lowest objective value.
of ocourse if these are 16 constraint you have you can do that,but if your matrix size is 10x10,you have to run 2^10 times........and actrully my real engineer's matrix is unfortunately 10x10
The number of necessary runs would be 2^100, not 2^10.
2^100
ans = 1.2677e+30
Another way is described here:
It requires introducing 100 binary variables.
xb=[10 6; 10 4];
yb=[15 3;15 2];
x = optimvar('x',2,2);
y=optimvar('y',2,2);
prob=optimproblem('Objective',sum(x.^2+y.^2,'all'));
prob.Constraints.con=fcn2optimexpr( @(x,y) min(xb-x,yb-y) , x,y )<=0;
soltemp=solve(prob,'Solver','ga');
Solving problem using ga. Optimization finished: average change in the fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
[sol,faval,exit]=solve(prob,soltemp,'Solver','fmincon');
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol.x,
ans = 2x2
10.0000 -0.0000 10.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sol.y
ans = 2x2
-0.0000 3.0000 0.0000 2.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
unbelivable! yes it's a very good solution for this problem

Sign in to comment.

More Answers (1)

prob.Constraints.con1=x<=10 or prob.Constraints.con1=x^2<=10
You have a lower bound of 0 on x. Under the conditions, x^2<=10 is the more restrictive condition, so just use
prob.Constraints.con1=x<=sqrt(10)

Products

Release

R2022a

Asked:

on 8 May 2024

Commented:

on 10 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!