How to write constraints for optimization problem?
显示 更早的评论
Hi,
I have a question regarding how to write constraints used for nonlinear optimization algorithms, such as fmincon.
[x, fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub)
I have this vector with x and y values
x= [x1 x2 x3 x4 y1 y2 y3 y4]
where (x1,y1) represents one position, (x2,y2) the next position and so on. I want to write a constraint that creates a minimum allowed distance between each position. Can anyone help me? I'm unfortunately quite new to this kind of optimization problem.
Thanks!
回答(1 个)
Alan Weiss
2017-2-14
0 个投票
It is not clear to me what the control variables of the optimization are, meaning which variables you are allowed to move to optimize something. The something you optimize is the objective function.
So, what are the variables? If part of the problem is that you don't know the number of variables that you are going to optimize over, then fmincon cannot help you all by itself, because it only optimizes over a fixed number of continuous variables.
To be slightly more specific, your description of a "minimum allowed distance between each position" is not clear to me. If you can move both x and y variables, then it might mean one thing, if x is fixed but y can vary it might mean another, and if every variable can be counted as being in the "distance" then it might mean something else. Basically, you need to be able to write all the control variables as a single vector, usually called x, and write the constraint(s) in terms of that variable. It could be that both your x and y variables are control variables, in which case you would make your control variable x represent both x and y.
Once you figure out your control variables, write your nonlinear constraint function so that it returns a c and ceq output, as documented.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
2 个评论
Lovisa Gelotte
2017-2-15
(x_i-x_j)^2+(y_i-y_j)^2 >= dmin^2 for i=1,...,10 and all j > i.
(alltogether 9+8+7+...+1 = 9*10/2 = 45 constraints)
dmin = ...;
[x, fval] = fmincon(@myfun,x0,[],[],[],[],[],[],@(x)nonlcon(x,dim))
function [c ceq] = nonlcon(x,dmin)
ceq=[];
c(1) = -((x(1)-x(2))^2 + (x(11)-x(12))^2) + dmin^2;
c(2) = -((x(1)-x(3))^2 + (x(11)-x(13))^2) + dmin^2;
...
c(9) = -((x(1)-x(10))^2 + (x(11)-x(20))^2) + dmin^2;
c(10) = -((x(2)-x(3))^2 + (x(12)-x(13))^2) + dmin^2;
c(11) = -((x(2)-x(4))^2 + (x(12)-x(14))^2) + dmin^2;
...
c(17) = -((x(2)-x(10))^2 + (x(12)-x(20))^2) + dmin^2;
...
c(45) = -((x(9)-x(10))^2 + (x(19)-x(20))^2) + dmin^2;
Best wishes
Torsten.
类别
在 帮助中心 和 File Exchange 中查找有关 Nonlinear Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!