How to choose one between two constraint conditions

11 vues (au cours des 30 derniers jours)
ocean
ocean le 8 Mai 2024
Commenté : ocean le 10 Mai 2024
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 commentaire
ocean
ocean le 8 Mai 2024
Have you ever encountered the same problem? I have been troubled by this problem many times

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 8 Mai 2024
Modifié(e) : Matt J le 9 Mai 2024
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 commentaires
Matt J
Matt J le 9 Mai 2024
Modifié(e) : Matt J le 9 Mai 2024
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>
ocean
ocean le 10 Mai 2024
unbelivable! yes it's a very good solution for this problem

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 8 Mai 2024
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)

Catégories

En savoir plus sur Nonlinear Optimization dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by