How to create a random subset with/without replacement and run model multiple times Topic is solved

Problems with modeling
Post Reply
teo21r
User
User
Posts: 10
Joined: 4 years ago

How to create a random subset with/without replacement and run model multiple times

Post by teo21r »

Hello,

I have a model like the one shown below. In its current form, the model yields the same solution in each iteration. How can I modify the loop statement to solve the model n times (n=10 in my example) where in each iteration set p1 contains 50 observations but instead of being the first 50 ones (as is the case in my model) they are drawn randomly from the larger set i with or without replacement?

Thank you.

Code: Select all

SETS    i /1*100/
p1(i)  /1*50/
j  /y, x1,x2/
  outp(j)  /y/
  inp(j)   /x1, x2/
ALIAS (i,ii) ;
Parameter
data(i,j);
data(i,j) = uniform(0,1);
display data;
PARAMETERS
x(inp,i)
y(outp,i)
;
Positive variables
f(i,p1)
b(i)
;
Variables
z1
;
loop(i,
x(inp,i)=data(i,inp);
y(outp,i)=data(i,outp);
);
Equations
objective1
output1(i,outp)
vinput1(i,inp)
;
objective1..             z1=E=sum(i$p1(i),b(i));
output1(i,outp)..        sum(p1,f(i,p1)*y(outp,p1))=G=y(outp,i);
vinput1(i,inp)..         sum(p1,f(i,p1)*x(inp,p1))=L=x(inp,i)*b(i);
model m /all/;

set k /1*10/
loop(k,
solve m using nlp minimizing z1;
);
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: How to create a random subset with/without replacement and run model multiple times

Post by bussieck »

Here is only the loop with the random selection of p1:

Code: Select all

scalar adr;
loop(k,
  option clear=p1;
  while(card(p1)<>50, adr = UniformInt(0,99); loop(i$(ord(i)=1), p1(i+adr) = yes));
  solve m using nlp minimizing z1;
);
-Michael
teo21r
User
User
Posts: 10
Joined: 4 years ago

Re: How to create a random subset with/without replacement and run model multiple times

Post by teo21r »

Hi Michael,

Thank you for your prompt reply. The code you proposed, and more specifically the "option clear=p1" and "p1(i+adr) = yes" lines, results in the following errors, respectively:

Error 579...Cannot clear a set used as domain or used in lag/ord operations

Error 188... Assigning to this set is NOT allowed. The set may have been
**** used in a domain definition or is a predefined/readonly set.

Any idea how these errors can be corrected?

Thanks,
Teo
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: How to create a random subset with/without replacement and run model multiple times

Post by bussieck »

Correct. I did not see the declaration "Positive variables f(i,p1)". Change this to "Positive variables f(i,i)". Since you use the variable f only over p1 there is no difference.

-Michael
teo21r
User
User
Posts: 10
Joined: 4 years ago

Re: How to create a random subset with/without replacement and run model multiple times

Post by teo21r »

Thank you, Michael. After making this change, the code runs fine.

Teo
Post Reply