Re: Equal constraints for decision values

41 views
Skip to first unread message
Message has been deleted

Johan Löfberg

unread,
Oct 24, 2017, 1:58:27 PM10/24/17
to yal...@googlegroups.com
A naive model would be

intvar a
desired = [ zeros(1,34) a a  1.05*a 1.05*a 1.1*a 1.1*a];
sol=optimize([F, sort(I)==desired],f);

Probably very inefficient compared to something smarter

bin sun

unread,
Oct 24, 2017, 2:03:24 PM10/24/17
to YALMIP
Dear Johan,

Thank you very much. So I think my code should be:
d=[1*40 matrix];
c=[40*40 matrix]

I=intvar(1,40);  
pos=binvar(1,40);  
intvar a;      %new here
f=-sum(I);
desired = [ zeros(1,34) a a  1.05*a 1.05*a 1.1*a 1.1*a];  %new here

%pos==1, I==0, means empty
F=[implies(pos,I== 0)]; 

F=F+[pos(1:2:39) + pos(2:2:40) >= 1];  % nearby two have one or two empty, empty is 1. So sum is 1 or 2 here.

sum(pos(1:2:39))==17   %type I have: 17 empty so 3 not empty
sum(pos(2:2:40))==17   %type II have: 17 empty so 3 not empty
F=F+[(c*(I.^2)')./d' <= 1];  % important constraint
F=F+[sort(I)==desired];    %new here


sol=optimize(F,f);
I=value(I)   
pos=value(pos)

bin sun

unread,
Oct 24, 2017, 2:07:51 PM10/24/17
to YALMIP
So if the new constraint is the 6 I are all equal, I should write something like:

intvar a
desired = [ zeros(1,34) a a  a a a a];
sol=optimize([F, sort(I)==desired],f);

or this will have some simple method to write?

Thank you very much for your time,
Bin

Johan Löfberg

unread,
Oct 24, 2017, 2:08:48 PM10/24/17
to YALMIP
Yes, nothing special

Johan Löfberg

unread,
Oct 24, 2017, 2:10:15 PM10/24/17
to YALMIP
should be possible to simplify to ismember(I,[0 a])

bin sun

unread,
Oct 24, 2017, 2:11:50 PM10/24/17
to YALMIP
Thank you very much. So I think my code should be:
d=[1*40 matrix];
c=[40*40 matrix]

I=intvar(1,40);  
pos=binvar(1,40);  
intvar a;      %new here
f=-sum(I);
%pos==1, I==0, means empty
F=[implies(pos,I== 0)]; 

F=F+[pos(1:2:39) + pos(2:2:40) >= 1];  % nearby two have one or two empty, empty is 1. So sum is 1 or 2 here.

sum(pos(1:2:39))==17   %type I have: 17 empty so 3 not empty
sum(pos(2:2:40))==17   %type II have: 17 empty so 3 not empty
F=F+[(c*(I.^2)')./d' <= 1];  % important constraint
F=F+[ ismember(I,[0 a])];    %new here


sol=optimize(F,f);
I=value(I)   
pos=value(pos)

Johan Löfberg

unread,
Oct 24, 2017, 2:17:58 PM10/24/17
to YALMIP
sure.

but you know that you don't have to place things in any particular order. the code I showed you is fully valid

However, a possible alternative is  (might be more efficient)

case1 = binvar(1,40);
case2 = binvar(1,40);
case3 = binvar(1,40);

with constraints
[sum(case1) == 2, implies(case1, I == a), sum(case2) == 2, implies(case2, I == 1.05*a), etc


bin sun

unread,
Oct 24, 2017, 2:19:41 PM10/24/17
to YALMIP
Since you said. After I run the code. It's very slow. If I want to accelerate it. Do you have any method to recommend?

bin sun

unread,
Oct 24, 2017, 2:21:50 PM10/24/17
to YALMIP
Ok. understand. So basically, in order to improve the efficiency, I need to use 3 decision variables rather than one and reorder them.

Johan Löfberg

unread,
Oct 24, 2017, 2:24:40 PM10/24/17
to YALMIP
no

post the data for testing

Johan Löfberg

unread,
Oct 24, 2017, 2:24:57 PM10/24/17
to YALMIP
I have no idea what that means. With sort, you will internally introduce 1600 binary variables. Here you introduce 120 variables. The combinatoric structures are completely different though

bin sun

unread,
Oct 24, 2017, 2:35:14 PM10/24/17
to YALMIP
d=[349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826,349261.312324434,772899.067110826];
c=[1,1,0.564033458725656,0.181585455277500,0.434284140650439,0.139813839373960,0.359132838508948,0.115619559401826,0.306587801988595,0.0987031615684801,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.358208012571609,0.115321820081071,0.309728258569085,0.0997142030751947,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.342107202827250,0.110138310446072,0.302897121832938,0.0975149805732202,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119,0.319704465118280,0.102925952272256,0.290008596289165,0.0933656366956258;1,1,1.24817985469688,0.401840181069079,0.961050638376900,0.309401534633576,0.794744296197877,0.255860716455266,0.678464570173827,0.218425513520097,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,0.792697699340491,0.255201833163205,0.685414254767314,0.220662901429308,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.757067354976486,0.243730967024075,0.670297266356730,0.215796124147757,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912,0.707491136640836,0.227770353272978,0.641775557774353,0.206613818810899;0.564033458725656,0.181585455277500,1,1,0.564033458725656,0.181585455277500,0.434284140650439,0.139813839373960,0.359132838508948,0.115619559401826,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.358208012571609,0.115321820081071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.342107202827250,0.110138310446072,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119,0.319704465118280,0.102925952272256;1.24817985469688,0.401840181069079,1,1,1.24817985469688,0.401840181069079,0.961050638376900,0.309401534633576,0.794744296197877,0.255860716455266,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,0.792697699340491,0.255201833163205,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.757067354976486,0.243730967024075,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912,0.707491136640836,0.227770353272978;0.434284140650439,0.139813839373960,0.564033458725656,0.181585455277500,1,1,0.564033458725656,0.181585455277500,0.434284140650439,0.139813839373960,0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119;0.961050638376900,0.309401534633576,1.24817985469688,0.401840181069079,1,1,1.24817985469688,0.401840181069079,0.961050638376900,0.309401534633576,0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912;0.359132838508948,0.115619559401826,0.434284140650439,0.139813839373960,0.564033458725656,0.181585455277500,1,1,0.564033458725656,0.181585455277500,0.358208012571609,0.115321820081071,0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.342107202827250,0.110138310446072,0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.319704465118280,0.102925952272256,0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831;0.794744296197877,0.255860716455266,0.961050638376900,0.309401534633576,1.24817985469688,0.401840181069079,1,1,1.24817985469688,0.401840181069079,0.792697699340491,0.255201833163205,0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.757067354976486,0.243730967024075,0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.707491136640836,0.227770353272978,0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377;0.306587801988595,0.0987031615684801,0.359132838508948,0.115619559401826,0.434284140650439,0.139813839373960,0.564033458725656,0.181585455277500,1,1,0.309728258569085,0.0997142030751947,0.358208012571609,0.115321820081071,0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.302897121832938,0.0975149805732202,0.342107202827250,0.110138310446072,0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.290008596289165,0.0933656366956258,0.319704465118280,0.102925952272256,0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815;0.678464570173827,0.218425513520097,0.794744296197877,0.255860716455266,0.961050638376900,0.309401534633576,1.24817985469688,0.401840181069079,1,1,0.685414254767314,0.220662901429308,0.792697699340491,0.255201833163205,0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,0.670297266356730,0.215796124147757,0.757067354976486,0.243730967024075,0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.641775557774353,0.206613818810899,0.707491136640836,0.227770353272978,0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024;0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.358208012571609,0.115321820081071,0.309728258569085,0.0997142030751947,1,1,0.581930152626506,0.187347133524905,0.452059733535744,0.145536530248006,0.376710208220031,0.121278434123158,0.323894992868060,0.104275054665962,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.375015355856950,0.120732791780107,0.326299544369365,0.105049178208365,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813,0.358208012571609,0.115321820081071,0.318790521690379,0.102631716476692;1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,0.792697699340491,0.255201833163205,0.685414254767314,0.220662901429308,1,1,1.28778440731187,0.414590507501669,1.00038720006751,0.322065583819203,0.833642199207926,0.268383543458079,0.716764579974574,0.230755854228101,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,0.829891569624598,0.267176062288425,0.722085740797711,0.232468953682976,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836,0.792697699340491,0.255201833163205,0.705468622271537,0.227119223119486;0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.358208012571609,0.115321820081071,0.581930152626506,0.187347133524905,1,1,0.581930152626506,0.187347133524905,0.452059733535744,0.145536530248006,0.376710208220031,0.121278434123158,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.375015355856950,0.120732791780107,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813,0.358208012571609,0.115321820081071;1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,0.792697699340491,0.255201833163205,1.28778440731187,0.414590507501669,1,1,1.28778440731187,0.414590507501669,1.00038720006751,0.322065583819203,0.833642199207926,0.268383543458079,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,0.829891569624598,0.267176062288425,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836,0.792697699340491,0.255201833163205;0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.422381946778634,0.135982036030421,0.452059733535744,0.145536530248006,0.581930152626506,0.187347133524905,1,1,0.581930152626506,0.187347133524905,0.452059733535744,0.145536530248006,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813;0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.934711635986779,0.300921931754394,1.00038720006751,0.322065583819203,1.28778440731187,0.414590507501669,1,1,1.28778440731187,0.414590507501669,1.00038720006751,0.322065583819203,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836;0.358208012571609,0.115321820081071,0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.507968630800660,0.163535892532027,0.376710208220031,0.121278434123158,0.452059733535744,0.145536530248006,0.581930152626506,0.187347133524905,1,1,0.581930152626506,0.187347133524905,0.375015355856950,0.120732791780107,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.358208012571609,0.115321820081071,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725;0.792697699340491,0.255201833163205,0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,1.12411099372694,0.361897336799010,0.833642199207926,0.268383543458079,1.00038720006751,0.322065583819203,1.28778440731187,0.414590507501669,1,1,1.28778440731187,0.414590507501669,0.829891569624598,0.267176062288425,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.792697699340491,0.255201833163205,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684;0.309728258569085,0.0997142030751947,0.358208012571609,0.115321820081071,0.422381946778634,0.135982036030421,0.507968630800660,0.163535892532027,0.572980837979350,0.184465982860018,0.323894992868060,0.104275054665962,0.376710208220031,0.121278434123158,0.452059733535744,0.145536530248006,0.581930152626506,0.187347133524905,1,1,0.326299544369365,0.105049178208365,0.375015355856950,0.120732791780107,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.318790521690379,0.102631716476692,0.358208012571609,0.115321820081071,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197;0.685414254767314,0.220662901429308,0.792697699340491,0.255201833163205,0.934711635986779,0.300921931754394,1.12411099372694,0.361897336799010,1.26797998953644,0.408214654859200,0.716764579974574,0.230755854228101,0.833642199207926,0.268383543458079,1.00038720006751,0.322065583819203,1.28778440731187,0.414590507501669,1,1,0.722085740797711,0.232468953682976,0.829891569624598,0.267176062288425,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,0.705468622271537,0.227119223119486,0.792697699340491,0.255201833163205,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396;0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.342107202827250,0.110138310446072,0.302897121832938,0.0975149805732202,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.375015355856950,0.120732791780107,0.326299544369365,0.105049178208365,1,1,0.598274748194724,0.192609127794335,0.468312047038901,0.150768815143748,0.392811017964391,0.126461943758156,0.339788392725501,0.109391790569433,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304,0.390465718208435,0.125706895777652,0.341566718689586,0.109964306481078;0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.757067354976486,0.243730967024075,0.670297266356730,0.215796124147757,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,0.829891569624598,0.267176062288425,0.722085740797711,0.232468953682976,1,1,1.32395423838450,0.426235056492563,1.03635281521505,0.333644387345603,0.869272543571931,0.279854409597208,0.751935935889381,0.242078953199830,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632,0.864082504110046,0.278183523475091,0.755871288675847,0.243345904328975;0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.342107202827250,0.110138310446072,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.375015355856950,0.120732791780107,0.598274748194724,0.192609127794335,1,1,0.598274748194724,0.192609127794335,0.468312047038901,0.150768815143748,0.392811017964391,0.126461943758156,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304,0.390465718208435,0.125706895777652;0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.757067354976486,0.243730967024075,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,0.829891569624598,0.267176062288425,1.32395423838450,0.426235056492563,1,1,1.32395423838450,0.426235056492563,1.03635281521505,0.333644387345603,0.869272543571931,0.279854409597208,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632,0.864082504110046,0.278183523475091;0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.386834388595071,0.124537822151071,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.439362073670264,0.141448633844055,0.468312047038901,0.150768815143748,0.598274748194724,0.192609127794335,1,1,0.598274748194724,0.192609127794335,0.468312047038901,0.150768815143748,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304;0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.856046540287252,0.275596417822436,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.972287868368832,0.313019258888362,1.03635281521505,0.333644387345603,1.32395423838450,0.426235056492563,1,1,1.32395423838450,0.426235056492563,1.03635281521505,0.333644387345603,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632;0.342107202827250,0.110138310446072,0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.430481591710036,0.138589643238294,0.375015355856950,0.120732791780107,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.525054148358093,0.169036419914441,0.392811017964391,0.126461943758156,0.468312047038901,0.150768815143748,0.598274748194724,0.192609127794335,1,1,0.598274748194724,0.192609127794335,0.390465718208435,0.125706895777652,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821;0.757067354976486,0.243730967024075,0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.952635774133504,0.306692445427791,0.829891569624598,0.267176062288425,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,1.16192045075887,0.374069748493255,0.869272543571931,0.279854409597208,1.03635281521505,0.333644387345603,1.32395423838450,0.426235056492563,1,1,1.32395423838450,0.426235056492563,0.864082504110046,0.278183523475091,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442;0.302897121832938,0.0975149805732202,0.342107202827250,0.110138310446072,0.386834388595071,0.124537822151071,0.430481591710036,0.138589643238294,0.451285241248581,0.145287189482138,0.326299544369365,0.105049178208365,0.375015355856950,0.120732791780107,0.439362073670264,0.141448633844055,0.525054148358093,0.169036419914441,0.590101777888653,0.189977914147275,0.339788392725501,0.109391790569433,0.392811017964391,0.126461943758156,0.468312047038901,0.150768815143748,0.598274748194724,0.192609127794335,1,1,0.341566718689586,0.109964306481078,0.390465718208435,0.125706895777652,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440;0.670297266356730,0.215796124147757,0.757067354976486,0.243730967024075,0.856046540287252,0.275596417822436,0.952635774133504,0.306692445427791,0.998673284597604,0.321513804281845,0.722085740797711,0.232468953682976,0.829891569624598,0.267176062288425,0.972287868368832,0.313019258888362,1.16192045075887,0.374069748493255,1.30586783458831,0.420412302865350,0.751935935889381,0.242078953199830,0.869272543571931,0.279854409597208,1.03635281521505,0.333644387345603,1.32395423838450,0.426235056492563,1,1,0.755871288675847,0.243345904328975,0.864082504110046,0.278183523475091,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515;0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119,0.319704465118280,0.102925952272256,0.290008596289165,0.0933656366956258,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813,0.358208012571609,0.115321820081071,0.318790521690379,0.102631716476692,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304,0.390465718208435,0.125706895777652,0.341566718689586,0.109964306481078,1,1,0.613314702718980,0.197451104715013,0.483280079609882,0.155587637444025,0.407660700272397,0.131242664264911,0.354475500771926,0.114120171767502;0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912,0.707491136640836,0.227770353272978,0.641775557774353,0.206613818810899,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836,0.792697699340491,0.255201833163205,0.705468622271537,0.227119223119486,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632,0.864082504110046,0.278183523475091,0.755871288675847,0.243345904328975,1,1,1.35723695940454,0.436950126593106,1.06947637629200,0.344308217346327,0.902134201012217,0.290433922098026,0.784437824037512,0.252542641240769;0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119,0.319704465118280,0.102925952272256,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813,0.358208012571609,0.115321820081071,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304,0.390465718208435,0.125706895777652,0.613314702718980,0.197451104715013,1,1,0.613314702718980,0.197451104715013,0.483280079609882,0.155587637444025,0.407660700272397,0.131242664264911;0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912,0.707491136640836,0.227770353272978,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836,0.792697699340491,0.255201833163205,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632,0.864082504110046,0.278183523475091,1.35723695940454,0.436950126593106,1,1,1.35723695940454,0.436950126593106,1.06947637629200,0.344308217346327,0.902134201012217,0.290433922098026;0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.349448177538811,0.112501670659119,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.403086702098228,0.129770107046813,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.454946005561771,0.146465739343304,0.483280079609882,0.155587637444025,0.613314702718980,0.197451104715013,1,1,0.613314702718980,0.197451104715013,0.483280079609882,0.155587637444025;0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.773312591153631,0.248960973438912,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.892012155434785,0.287175221348836,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,1.00677438604439,0.324121880401632,1.06947637629200,0.344308217346327,1.35723695940454,0.436950126593106,1,1,1.35723695940454,0.436950126593106,1.06947637629200,0.344308217346327;0.319704465118280,0.102925952272256,0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.373605587441624,0.120278929627831,0.358208012571609,0.115321820081071,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.446826187278255,0.143851637507725,0.390465718208435,0.125706895777652,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.540719338855865,0.174079685884821,0.407660700272397,0.131242664264911,0.483280079609882,0.155587637444025,0.613314702718980,0.197451104715013,1,1,0.613314702718980,0.197451104715013;0.707491136640836,0.227770353272978,0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.826771817580502,0.266171686419377,0.792697699340491,0.255201833163205,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.988805605206135,0.318336994418684,0.864082504110046,0.278183523475091,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,1.19658678995705,0.385230261914442,0.902134201012217,0.290433922098026,1.06947637629200,0.344308217346327,1.35723695940454,0.436950126593106,1,1,1.35723695940454,0.436950126593106;0.290008596289165,0.0933656366956258,0.319704465118280,0.102925952272256,0.349448177538811,0.112501670659119,0.373605587441624,0.120278929627831,0.383342326233513,0.123413584352815,0.318790521690379,0.102631716476692,0.358208012571609,0.115321820081071,0.403086702098228,0.129770107046813,0.446826187278255,0.143851637507725,0.467660831417834,0.150559162182197,0.341566718689586,0.109964306481078,0.390465718208435,0.125706895777652,0.454946005561771,0.146465739343304,0.540719338855865,0.174079685884821,0.605794243665876,0.195029961146440,0.354475500771926,0.114120171767502,0.407660700272397,0.131242664264911,0.483280079609882,0.155587637444025,0.613314702718980,0.197451104715013,1,1;0.641775557774353,0.206613818810899,0.707491136640836,0.227770353272978,0.773312591153631,0.248960973438912,0.826771817580502,0.266171686419377,0.848318768426183,0.273108531776024,0.705468622271537,0.227119223119486,0.792697699340491,0.255201833163205,0.892012155434785,0.287175221348836,0.988805605206135,0.318336994418684,1.03491170528317,0.333180435076396,0.755871288675847,0.243345904328975,0.864082504110046,0.278183523475091,1.00677438604439,0.324121880401632,1.19658678995705,0.385230261914442,1.34059453271346,0.431592248295515,0.784437824037512,0.252542641240769,0.902134201012217,0.290433922098026,1.06947637629200,0.344308217346327,1.35723695940454,0.436950126593106,1,1]


I=intvar(1,40);  
pos=binvar(1,40); 
intvar Ibase;     %new
case1 = binvar(1,40);  %new
case2 = binvar(1,40);
case3 = binvar(1,40);

f=-sum(I);

F=[implies(pos,I== 0)]; 

F=F+[pos(1:2:39) + pos(2:2:40) >= 1];  % sum could be 1or2; nearby two have one or two empty.
F=F+[pos(1)+pos(3)+pos(5)+pos(7)+pos(9)+pos(11)+pos(13)+pos(15)+pos(17)+pos(19)+pos(21)+pos(23)+pos(25)+pos(27)+pos(29)+pos(31)+pos(33)+pos(35)+pos(37)+pos(39)==17];
F=F+[pos(2)+pos(4)+pos(6)+pos(8)+pos(10)+pos(12)+pos(14)+pos(16)+pos(18)+pos(20)+pos(22)+pos(24)+pos(26)+pos(28)+pos(30)+pos(32)+pos(34)+pos(36)+pos(38)+pos(40)==17];
F=F+[(c*(I.^2)')./d' <= 1]; 
F=F+[sum(case1) == 2, implies(case1, I == Ibase), sum(case2) == 2, implies(case2, I == 1.05*Ibase), sum(case3) == 2, implies(case3, I == 1.1*Ibase)];      %new

sol=optimize(F,f);
I=value(I)   
pos=value(pos)

So case is some new decision variable, not pos, right?
Message has been deleted
Message has been deleted

bin sun

unread,
Oct 24, 2017, 2:53:26 PM10/24/17
to YALMIP
Dear Johan,

So I rewrite the code like this:

d=[1*40 matrix];
c=[40*40 matrix];
I=intvar(1,40);  
pos=binvar(1,40); 
intvar Ibase;     %new
case1 = binvar(1,40);  %new
case2 = binvar(1,40);
case3 = binvar(1,40);

f=-sum(I);

F=[implies(pos,I== 0)]; 

F=F+[pos(1:2:39) + pos(2:2:40) >= 1];  % sum could be 1or2; nearby two have one or two empty.
F=F+[pos(1)+pos(3)+pos(5)+pos(7)+pos(9)+pos(11)+pos(13)+pos(15)+pos(17)+pos(19)+pos(21)+pos(23)+pos(25)+pos(27)+pos(29)+pos(31)+pos(33)+pos(35)+pos(37)+pos(39)==17];
F=F+[pos(2)+pos(4)+pos(6)+pos(8)+pos(10)+pos(12)+pos(14)+pos(16)+pos(18)+pos(20)+pos(22)+pos(24)+pos(26)+pos(28)+pos(30)+pos(32)+pos(34)+pos(36)+pos(38)+pos(40)==17];
F=F+[(c*(I.^2)')./d' <= 1]; 
F=F+[sum(case1) == 2, implies(case1, I == Ibase), sum(case2) == 2, implies(case2, I == 1.05*Ibase), sum(case3) == 2, implies(case3, I == 1.1*Ibase)];      %new

sol=optimize(F,f);
I=value(I)   
pos=value(pos)
Ibase=value(Ibase)    %new
Is this code looks correct?

When I use the code, it's much faster. But I get the result like: 
I=[0 403 0 0 0 0 0 0 405 0 0 385 0 0 0 0 0 0 354 0 0 0 0 0 0 0 0 0 0 0 0 396 0 0 0 0 0 0 419 0]
Ibase=0;
So it didn't give me the result I need here.

One more thing,  When I  write code like: F=F+[ ismember(I,[0 a])];  MATLAB said:"Both arguments must be constraints". So can you tell me how to write this correctly?

Johan Löfberg

unread,
Oct 24, 2017, 3:12:41 PM10/24/17
to YALMIP
The problem is probably the poor numerics as you don't have any bounds on I leading to nasty big-M models

Bounds are easily derived with
[box,L,U] = boundingbox((c*(I.^2)')./d' <= 1,sdpsettings('relax',2,'verbose',0))


and then adding either box, or simply L<= I<=U, and L/1.1<= Ibase <= U/1.1 

You got various other things to play with. For instance, your model implies ismember(Ibase,20:20:U) since 1.05*Ibase isn't an integer otherwise, you also must have case1+case2+case3 == 1-pos

Johan Löfberg

unread,
Oct 24, 2017, 3:21:55 PM10/24/17
to YALMIP
and Ibase >= 1 (which means >=20 here just as well) should be added to as those variables shouldn't be 0

Johan Löfberg

unread,
Oct 24, 2017, 3:23:58 PM10/24/17
to YALMIP
should be Ibase of course


Message has been deleted

bin sun

unread,
Oct 24, 2017, 3:52:57 PM10/24/17
to YALMIP
So I will have something like this:
d=[1*40 matrix];
c=[40*40 matrix];
I=intvar(1,40);  
pos=binvar(1,40); 
intvar Ibase;     %new
case1 = binvar(1,40);  %new
case2 = binvar(1,40);
case3 = binvar(1,40);

f=-sum(I);

F=[implies(pos,I== 0)]; 

F=F+[pos(1:2:39) + pos(2:2:40) >= 1];  % sum could be 1or2; nearby two have one or two empty.
F=F+[pos(1)+pos(3)+pos(5)+pos(7)+pos(9)+pos(11)+pos(13)+pos(15)+pos(17)+pos(19)+pos(21)+pos(23)+pos(25)+pos(27)+pos(29)+pos(31)+pos(33)+pos(35)+pos(37)+pos(39)==17];
F=F+[pos(2)+pos(4)+pos(6)+pos(8)+pos(10)+pos(12)+pos(14)+pos(16)+pos(18)+pos(20)+pos(22)+pos(24)+pos(26)+pos(28)+pos(30)+pos(32)+pos(34)+pos(36)+pos(38)+pos(40)==17];
F=F+[(c*(I.^2)')./d' <= 1]; 
F=F+[sum(case1) == 2, implies(case1, I == Ibase), sum(case2) == 2, implies(case2, I == 1.05*Ibase), sum(case3) == 2, implies(case3, I == 1.1*Ibase)];      %new
F=F+[1<=Ibase<=400];  %new
F=F+[case1+case2+case3==1-pos];

sol=optimize(F,f);
I=value(I)   
pos=value(pos)
Ibase=value(Ibase)    %new

Is this code looks correct?
I try to add constraint like: F=F+[1<=I<=400]; but MATLAB said "the model is infeasible"
I don't really understand the part of "case1+case2+case3 == 1-pos" could you explain what this mean?

bin sun

unread,
Oct 24, 2017, 4:39:22 PM10/24/17
to YALMIP
No matter I use this code or use sort(I), my optimization all stop at 11.1% and stay there like this. Is that possible to use the code to find the result?


Johan Löfberg

unread,
Oct 24, 2017, 4:42:38 PM10/24/17
to YALMIP
of course it is infeasible, most of the elements in I are supposed to be zero

and the upper bound on Ibase set to 400, is that based on a-priori knowledge? When I computed the bounding box, it was close to 600

case1+case2+case3 being 1 means that the value is a,1.05a or 1.1*a, i.e. not zero, which you already encode with the pos indicator

Johan Löfberg

unread,
Oct 24, 2017, 4:43:37 PM10/24/17
to YALMIP
you tweak the various stopping option in your solver, such as relative gap, absolute gap etc
Message has been deleted

Johan Löfberg

unread,
Oct 24, 2017, 4:53:35 PM10/24/17
to YALMIP
I have no idea if it will good results. It models what you want it to model, but if it does that in a good way is of course impossible to answer

600 came from the boundingbox computations earlier

You should add as many valid constraints you can, that's the whole trick in integer programming.
Reply all
Reply to author
Forward
0 new messages