How to choose one between two constraint conditions
3 ビュー (過去 30 日間)
古いコメントを表示
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')
採用された回答
Matt J
2024 年 5 月 8 日
編集済み: Matt J
2024 年 5 月 9 日
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 件のコメント
Matt J
2024 年 5 月 9 日
編集済み: Matt J
2024 年 5 月 9 日
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');
[sol,faval,exit]=solve(prob,soltemp,'Solver','fmincon');
sol.x,
sol.y
その他の回答 (1 件)
Walter Roberson
2024 年 5 月 8 日
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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with Optimization Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!