Loop Function and Parameters Based on a Distribution Topic is solved

Problems with modeling
Post Reply
diabolik
User
User
Posts: 14
Joined: 1 year ago

Loop Function and Parameters Based on a Distribution

Post by diabolik »

Hello everyone,

I am trying to solve a single model multiple times with different parameters. My parameters are c(j) and d(j). Their distributions are UNI(50,200) and UNI(20,200). In each iteration, I want to generate a value between those given values for each parameter based on uniform distribution. After defining the problem I created a loop to solve the problem iteratively. However, the problem keeps solving the same problem based on exactly the same parameters, the parameters do not change. I thought it is because I define the parameters before solving the next iteration and tried defining parameters inside the loop, but GAMS gives an error saying that it is not possible to make definitions inside a loop. What do you think I am doing wrong?

Edit: In addition, I want d(j) to take a value according to UNI(20,200) with 0.2 probability and 0 with 0.8 probability. Do you think it is also possible?

Code: Select all

Sets
j   facility points /1*200/
n   iteration number/1*200/;

Parameters
k/100/
gamma_parameter /0/;

parameter c(j)    numbers from distributions;
c(j)=uniform(50,200);

parameter d(j)    numbers from distributions;
d(j)=uniform(20,200);

Variables
obj
nom_cost;

Positive Variables
z
p(j);

Binary Variables
x(j);

Equations
objective
equation1
equation2
nominal_cost;

objective..obj=e=sum(j,(c(j)*x(j)))+gamma_parameter*z+sum(j,p(j));
equation1(j)..z+p(j)=g=d(j)*x(j);
equation2..sum(j,x(j))=e=k;
nominal_cost..nom_cost=e=sum(j,c(j)*x(j));

Model model1 / all /;

loop(n,
         Option mip=CPLEX, limrow=105, reslim=1000000, iterlim=1000000, optcr=0, optca=0;
         solve model1 using mip minimizing obj;
         display c, d, x.l, z.l, p.l, obj.l, nom_cost.l;
);
diabolik
User
User
Posts: 14
Joined: 1 year ago

Re: Loop Function and Parameters Based on a Distribution

Post by diabolik »

Update on the question. I was able to solve regeneration of variables in each iteration by adding "execseed = 1+gmillisec(jnow);" to my code.

Final version looks as follows:

Code: Select all

execseed = 1+gmillisec(jnow);
Sets
j   facility points /1*200/
n   iteration number/1*10/;

parameter c(j) ’Samples from a uniform dist’;
parameter d(j) ’Samples from a uniform dist’;

Parameters
k/100/
gamma_parameter /10/;

Variables
obj
nom_cost;

Positive Variables
z
p(j);

Binary Variables
x(j);

Equations
objective
equation1
equation2
nominal_cost;

objective..obj=e=sum(j,(c(j)*x(j)))+gamma_parameter*z+sum(j,p(j));
equation1(j)..z+p(j)=g=d(j)*x(j);
equation2..sum(j,x(j))=e=k;
nominal_cost..nom_cost=e=sum(j,c(j)*x(j));

Model model1 / all /;
model1.solprint=2;
model1.solvelink=2;

loop(n,
         c(j)=uniform(50,200);
         d(j)=uniform(20,200);
         Option mip=CPLEX, limrow=105, reslim=1000000, iterlim=1000000, optcr=0, optca=0;
         solve model1 using mip minimizing obj;
         display obj.l, nom_cost.l;
);
However, still stuck at adding a discrete probability. Tried EMP but it does not seem to work.
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Loop Function and Parameters Based on a Distribution

Post by dirkse »

Hello,

Perhaps this is what you are looking for.

-Steve

Code: Select all

execseed = 86358;

set j / 1 * 20 /;
scalar offProb / 0.8 /;
parameters d(j), onoff(j);
onoff(j) = uniform(0,1);
onoff(j) = 1$[onoff(j) >= offProb];
d(j)$onoff(j) = uniform(20,200);
display onoff, d;
diabolik
User
User
Posts: 14
Joined: 1 year ago

Re: Loop Function and Parameters Based on a Distribution

Post by diabolik »

Thank you so much! It works.

However, in each iteration GAMS finds the solution slower. Do you think it is normal? Is there anything I can do about it?
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Loop Function and Parameters Based on a Distribution

Post by bussieck »

Perhaps, you can post the model where you experience this so we can talk about the same thing rather than speculating.

-Michael
diabolik
User
User
Posts: 14
Joined: 1 year ago

Re: Loop Function and Parameters Based on a Distribution

Post by diabolik »

Hey, this is the model. The issue I am facing is finding the optimal solution gets increasingly slower in each iteration. While single iteration takes like 1 minute, 100 iteration takes 9-10 hours.

Code: Select all

execseed = 86358;
Sets
j   facility points /1*200/
n   iteration number/1*100/;

parameter c(j) ’Samples from a uniform dist’;

scalar offProb / 0.8 /;
parameters d(j), onoff(j);

Parameters
k/100/
gamma_parameter /30/;

Variables
obj
nom_cost;

Variables
q(j);

Positive Variables
z
p(j);

Binary Variables
x(j);

Equations
objective
equation1
equation2
nominal_cost;

objective..obj=e=sum(j,(c(j)*x(j)))+gamma_parameter*z+sum(j,p(j));
equation1(j)..z+p(j)=g=d(j)*x(j);
equation2..sum(j,x(j))=e=k;
nominal_cost..nom_cost=e=sum(j,c(j)*x(j));

Model model1 / all /;
model1.solprint=2;
model1.solvelink=2;

loop(n,
         c(j)=uniform(50,200);
         onoff(j) = uniform(0,1);
         onoff(j) = 1$[onoff(j) >= offProb];
         d(j)$onoff(j) = uniform(20,200);
         Option mip=CPLEX, limrow=105, reslim=1000000, iterlim=1000000, optcr=0, optca=0;
         solve model1 using mip minimizing obj;
         display nom_cost.l;
);
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Loop Function and Parameters Based on a Distribution

Post by dirkse »

Diabolik,

Sometimes it is useful to simply put your eyes on the data. In this case, to display and look at the c(j) and d(j) values in each pass of the loop. My guess is that you'll see the d(j) filling up or becoming less sparse.

The assignment

Code: Select all

d(j)$onoff(j) = uniform(20,200);
does the expected thing if d is zero, but when used in a loop, it does not. It assigns nonzero values to around 20% of d, but it doesn't zero the other 80%. Since you do this in a loop, it gets fuller and fuller because nonzero values from previous loops are never set back to zero. You should probably use this one:

Code: Select all

d(j) = uniform(20,200)$onoff(j);
which assigns every value of d: either to 0 or to uniform(20,200), as dictated by onoff(j).

-Steve
Post Reply