Loop issue Topic is solved

Problems with modeling
matteoc5
User
User
Posts: 7
Joined: 3 years ago

Loop issue

Post by matteoc5 »

Dear All!
Can you please help me with this Loop?

...
Model MODELX /all/;
parameter eff(j);


*SET E(q);
*E(q)= no;

SET E(j);

*E(K) è vuoto inizialmente quindi

E(j) = no;
display E;

EQUATION
additionalrestriction(E(j));

*Poi fate il loop su K,

loop (j,
solve MODELX MAXIMIZING z using MIP;
eff(j)= k.l(j);
if (eff(j) = 1, E(j) = yes);
additionalrestriction(E(j)) .. sum (j,K(E(j)))=e=0;
);

Thank you!
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop issue

Post by Renger »

Hi
If I look at your code, it looks like you want to achieve the following:
- You want to solve your model multiple times
- As soon as the variable K is equal to 1, you want to make sure, that it stays zero.

In your code you could easily achieve that, by not defining K over your loop and set an if-condition ;

Code: Select all

loop(j, 
  solve model;
  if(K.L = 1, K.FX = 0);
);
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
matteoc5
User
User
Posts: 7
Joined: 3 years ago

Re: Loop issue

Post by matteoc5 »

Dear Renger, thanks for your help!
My problem is that I can't define the right loop for my MIP model. I should follow the Flowchart of the algoritm . Therefore, when I solve the model 9, I have the best DMU but I have to create the dynamic set E(j) and I have to add the additional restriction for each j in E. Should I create a subset and run the additional restriction for each j in the subset?. I tried to write the Loop and to add the additional restriction, but it doesn't work. Thank you!
The problem should be only with the loop. Can you give it a look ?

Renger wrote: 3 years ago Hi
If I look at your code, it looks like you want to achieve the following:
- You want to solve your model multiple times
- As soon as the variable K is equal to 1, you want to make sure, that it stays zero.

In your code you could easily achieve that, by not defining K over your loop and set an if-condition ;

Code: Select all

loop(j, 
  solve model;
  if(K.L = 1, K.FX = 0);
);
Cheers
Renger
Last edited by matteoc5 3 years ago, edited 2 times in total.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop issue

Post by Renger »

Hi

If you define f over q as a dynamic set (and not an alias), I think you can do it as follows:

Code: Select all

set f(q)  dynamic set;

...

additionalrestriction.. sum(f, K(f))=e=0;
...
loop (q,

solve Model9 MAXIMIZING z using MIP;
	if (z.l gt 0,
    		f(q)$k.l(q) = yes;
	);
);

...
Hope this helps
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
matteoc5
User
User
Posts: 7
Joined: 3 years ago

Re: Loop issue

Post by matteoc5 »

Dear Henger,
I tried with your suggestion, but it still doesn't work. Do you have any idea?
Thank you again!

Code: Select all

$title Model9

*Definiamo il set j che indica le 14 DMU prese in considerazione
SET j DMU /1*14/ ;
alias (j, q)

SET f(q) dynamic set;

SET i input /1*5/;

SET r output /1*4/;


PARAMETER x(i,j) livello di input i per la DMU j;
$call "GDXXRW Dati.xlsx par=x rng=Foglio1!a1:o6 cdim=1 rdim=1" ;
$gdxin Dati.gdx
$load  x
display x;

PARAMETER y(r,j) livello di output r per la DMU j;
$call "GDXXRW Dati.xlsx par=y rng=Foglio2!a1:o5 cdim=1 rdim=1";
$gdxin Dati.gdx
$load  y
display y;

PARAMETER alfa(i) preferenze decisori per input i;
$call "GDXXRW Dati.xlsx par=alfa rng=Foglio3!a1:e2 cdim=1" ;
$gdxin Dati.gdx
$load  alfa
display alfa;

PARAMETER beta(i) preferenze decisori per input i;
$call "GDXXRW Dati.xlsx par=beta rng=Foglio4!a1:e2 cdim=1" ;
$gdxin Dati.gdx
$load  beta
display beta;

PARAMETER teta(r) preferenze decisori per output r;
$call "GDXXRW Dati.xlsx par=teta rng=Foglio5!a1:d2 cdim=1 " ;
$gdxin Dati.gdx
$load  teta
display teta;

PARAMETER lambda(r) preferenze decisori per output r;
$call "GDXXRW Dati.xlsx par=lambda rng=Foglio6!a1:d2 cdim=1 " ;
$gdxin Dati.gdx
$load  lambda
display lambda;

SCALAR M bigM;
M=100;

POSITIVE VARIABLES
h
s(j)
;

BINARY VARIABLES
K(j)
;

VARIABLES
u(r) peso associato all'ouput r
v(i) peso associato all'input i
EF efficienza della DMU j
z funzione obiettivo
;

EQUATIONS
UpperBoundDef(j) upper bound per l'efficienza della DMU
LowerBoundDef(j) lower bound per l'efficienza della DMU
UnicaDMUDef Vincolo che impone una sola DMU efficiente
Logico1Def(j) Vincolo per z
Logico2Def(j) Vincolo per z
Logico3Def(j) Vincolo per h
WeightedRestrictionIn_upper(i) vincoli per la preferenza del decisore
WeightedRestrictionIn_lower(i) vincoli per la preferenza del decisore
WeightedRestrictionOut_upper(r) vincoli per la preferenza del decisore
WeightedRestrictionOut_lower(r) vincoli per la preferenza del decisore
ObjDef vincolo funzione obiettivo
additionalrestriction vincolo aggiuntivo
;


UpperBoundDef(j) .. sum(r, u(r)*y(r,j)) - sum(i, v(i)*x(i,j)) =l= M*K(j) - h + s(j);

LowerBoundDef(j) .. sum(r, u(r)*y(r,j)) - sum(i, v(i)*x(i,j)) =g= s(j) - (M * (1-K(j)));

UnicaDMUDef .. sum(j,K(j))=e=1;

Logico1Def(j) .. s(j) =l= M*K(j);

Logico2Def(j) .. s(j) =l= h;

Logico3Def(j) .. h =l= s(j) + (M*(1-K(j)));

WeightedRestrictionIn_upper(i)$(ord (i) gt 1 ) .. v(i) =l= beta(i)*v(i-1);

WeightedRestrictionIn_lower(i)$(ord (i) gt 1 ) .. v(i) =g= alfa(i)*v(i-1);

WeightedRestrictionOut_upper(r)$(ord (r) gt 1 ) .. u(r) =l= lambda(r)*u(r-1);

WeightedRestrictionOut_lower(r)$(ord (r) gt 1 ) .. u(r) =g= teta(r)*u(r-1);

additionalrestriction.. sum(f, K(f))=e=0;

ObjDef .. z =e= h;


Model Model9 /all/;
loop (q,
solve Model9 MAXIMIZING z using MIP;
if (z.l gt 0,f(q)$K.l(q)=yes; );
);




Display h.l, u.l, v.l, K.l;

Renger wrote: 3 years ago Hi

If you define f over q as a dynamic set (and not an alias), I think you can do it as follows:

Code: Select all

set f(q)  dynamic set;

...

additionalrestriction.. sum(f, K(f))=e=0;
...
loop (q,

solve Model9 MAXIMIZING z using MIP;
	if (z.l gt 0,
    		f(q)$k.l(q) = yes;
	);
);

...
Hope this helps
Renger
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop issue

Post by Renger »

What is not working? Send all the files, so I can at least run your model to see what is happening.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
matteoc5
User
User
Posts: 7
Joined: 3 years ago

Re: Loop issue

Post by matteoc5 »

Thank you so much! I really don't know how can define the loop.
Renger wrote: 3 years ago What is not working? Send all the files, so I can at least run your model to see what is happening.
Cheers
Renger
Last edited by matteoc5 3 years ago, edited 1 time in total.
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop issue

Post by Renger »

Hi Mateo

I think you don't need the additional constraint. If you find a solution for a DMU, you can fix the K-Variable to zero.
I attached my solution.

CHeers
Renger
Model9 - Loop - Copia.gms
(2.79 KiB) Downloaded 408 times
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
matteoc5
User
User
Posts: 7
Joined: 3 years ago

Re: Loop issue

Post by matteoc5 »

Dear Renger,
first of all, I want to thank you for kindness and patience! But the model still doesn't complete the loop. The model should find the first DMU during the first loop (the DMU 7), then it should add the restriction (K('7')=0), then it should find the second DMU (the DMU 3), then it should add the restriction (K('7')+K('3')=0), then it should find the third DMU (DMU 9) and it should add the restriction (K('7')+K('3')+K('9')=0). It should stop because the function object should be 0. This is why I added the additional restriction with the sum and I should find the SET E, which contains all the efficient DMU (as in the previous flowchart).

Thank you!
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Loop issue

Post by Renger »

Hi
Sorry, I overlooked something. Just put in the version I sent you the following loop (no need for the additional constraint).

Code: Select all

loop (q,
solve Model9 MAXIMIZING z using MIP;
if (z.l gt 0,
    counter = counter + 1;
    display "solved",counter, K.L;
    Eff(j)$(K.L(j) = 1) = YES;
    K.FX(j)$(K.L(j) = 1) = 0;
);
);
display eff;
This will give you:

Code: Select all

----    126 PARAMETER eff  

3 1.000,    7 1.000,    9 1.000
CHeers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Post Reply