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!

Answers (1)

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 Comments

Thank you for your response!
Our vector x is the control variable vector, containing both x and y values. So the vector x with control variables is:
x= [x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 y1 y2 y3 y4 y5 y6 y7 y8 y9 y10]
where (x1,y1) represents one position, (x2,y2) the next position and so on. Both x and y values should be varied. The constraint that I want to formulate should be able to limit the points from being too close to each other in a coordinate system. Each point with x and y value represents a wind turbine in a park, which cannot be placed too close to another turbine. I want to know more in detail how to write such a constraint. I really can't understand how this should be formulated in an easy way, and I have already looked at the link above. I hope this made the problem more clear :)
Thanks!
(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.

Sign in to comment.

Asked:

on 14 Feb 2017

Edited:

on 15 Feb 2017

Community Treasure Hunt

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

Start Hunting!