How to write constraints for optimization problem?
Afficher commentaires plus anciens
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!
Réponses (1)
Alan Weiss
le 14 Fév 2017
0 votes
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 commentaires
Lovisa Gelotte
le 15 Fév 2017
(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.
Catégories
En savoir plus sur Nonlinear Optimization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!