Hi, Johan,
I have a problem like what is shown in picture below. I have three lines, first two are put in position 1 and 2, but line3 can be putted in position 3/4/5. I want to the best position of line3. Now I know I need to use a binary variable to represent the position and use the implies to convert nonlinear to linear.
I write a yalmip code like this and use gurobi to solve:
clear all
close all
c=[0,0.6058,0.4665,0.3857,0.3293;0.6058,0,0.6058,0.4665,0.3857;0.4665,0.6058,0,0.6058,0.4665;0.3857,0.4665,0.6058,0,0.6058;0.3293,0.3857,0.4665,0.6058,0];
d=[4.019335684198116e+05,4.019335684198116e+05,4.019335684198116e+05,4.019335684198116e+05,4.019335684198116e+05];
I=sdpvar(1,3);
pos=binvar(1,3); % so three 0/1, which is 1 means the line3 is here
f=-I(1)-I(2)-I(3);
F=[sum(pos)==1]; % only choose one from three
for k=1:3 % k=1 means position 3 so k+2 later
F=[F,implies(pos{k}, 1/d(1)*I(1,1)*I(1,1)+c(1,2)/d(1)*I(1,2)*I(1,2)+c(1,k+2)/d(1)*I(1,3)*I(1,3)<=1,
c(2,1)/d(2)*I(1,1)*I(1,1)+1/d(2)*I(1,2)*I(1,2)+c(2,k+2)/d(2)*I(1,3)*I(1,3)<=1, c(k+2,1)/d(k+2)*I(1,1)*I(1,1)+c(k+2,2)/d(k+2)*I(1,2)*I(1,2)+1/d(k+2)*I(1,3)*I(1,3)<=1)];
% I use implies here means if pos{k}=1, then c(1,k); c(2,k) and d(k) in constraint can be decided and constraint can be write. if pos{k}=0, no constraints added because line3 no here
end
sol=optimize(F,f);
I=value(I)