Page 1 of 1

Running a gams model N times for a random subset of the initial set

Posted: Thu May 07, 2020 10:44 pm
by teo21r
Hello everyone,

How can I create N random subsets from my initial set of observations and run my Gams model for each of these subsets? To better explain what I want to do I provide the following example:

SETS i /1*100/
j 'outputs and inputs' /y, x1, x2/
outp(j) /y/
inp(j) /x1,x2/
$CALL GDXXRW.EXE firms.xlsx par=data rng=sheet1!A1:C101
Parameter data(i,j);
$GDXIN firms.gdx
$LOAD data
$GDXIN
display data;
PARAMETERS
.......;
EQUATIONS
.....;
MODEL SP1 /.../;
SOLVE SP1 USING NLP MAXIMIZING Z;


Using the code above I have loaded a data set in GAMS that contains 100 observations and 3 variables. I know how to specify parameters and variables, assign the data to specific variables, and generate equations to be used in the solve statement. My question is how can I make GAMS generate, for example, 10 random subsets of let's say 50 observations each and run the model separately for each of these subsets.

Thank you in advance for your help.

Theo

Re: Running a gams model N times for a random subset of the initial set

Posted: Fri May 08, 2020 8:53 am
by bussieck
The following code should help you:

Code: Select all

SETS i /1*100/
j 'outputs and inputs' /y, x1, x2/
outp(j) /y/
inp(j) /x1,x2/;
Parameter data(i,j);
data(i,j) = uniform(0,1);
variable obj,x1,x2,dp(i),dn(i);
positive variable dp,dn;
equation e(i), defobj;
set ii(i);
e(ii).. data(ii,'y') =e= x1*data(ii,'x1') + x2*data(ii,'x1') +dp(ii) -dn(ii);
defobj.. obj =e= sum(ii, sqr(dp(ii))+sqr(dn(ii)));

model m /all/;
set k /k1*k10/; scalar ordi;
parameter rep;
loop(k,
* sample 50 observations
  option clear=ii;
  while(card(ii)<50,
     ordi = uniformInt(1,card(i));
     ii(i)$(ord(i)=ordi) = yes;
  );
  solve m us qcp min obj;
  rep(k,'x1') = x1.l;
  rep(k,'x2') = x2.l;
);
display rep;
-Michael

Re: Running a gams model N times for a random subset of the initial set

Posted: Sat May 09, 2020 5:26 pm
by teo21r
Michael,

Thank you, I really appreciate your help.

Theo