Clear Filters
Clear Filters

How to limit optimization variables to a certain area?

1 view (last 30 days)
Helllo everyone,
I want to limit my optimization variable S in a certain area, and the coordinate data of the area boundary points are saved in 'bound_points', so I use inPoly to judge whether S is within the boundary, but there are some errors occured when I run the code. What should I do to limit the optimization variables within the boundary?
S = optimvar('S', [2 1]);
cons_eq=inPoly(S(1:2,1)',bound_points)==1;
prob.Constraints.constr=cons_eq;
ERROR:
Operator '&' is not supported for operands oftype 'optim.problemddef.OptimizationEquality'.
Error in InPoly(line 20)
if ~isempty(find(poly(:,1)==p(i,1)& poly(:,2)==p(i,2)))

Answers (1)

Walter Roberson
Walter Roberson on 16 Jun 2021
S = optimvar('S', [2 1]);
At that point, S is an optimization variable
cons_eq=inPoly(S(1:2,1)',bound_points)==1;
At that point, the optimization variable is being passed to inPoly() with the expectation that it will be processed in a way that is compatible with creating an optimization constraint that can be stored.
But inPoly is expecting numeric values, not optimization variables, so it fails.
In some cases fcn2optimexpr could be used to convert a function to an optimization constraint, but when you look at https://www.mathworks.com/help/optim/ug/supported-operations-on-optimization-variables-expressions.html I do not see any comparison operators being supported there
You might need to convert the problem based optimization into a solver based optimization and then attach nonlinear constraints to that.
... Or find a way to convert the inPoly tests into constraints. That might not be bad for simple polygons, but could be messy for an arbitrary area such as a map of a country.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!