Home > Back-end >  Accessing entries of multidimensional variables using the gams-c api
Accessing entries of multidimensional variables using the gams-c api

Time:03-18

I am generating the following gams program with my c program

variable x(*) /1.lo = -1,1.up = 1,2.lo = -1,2.up = 1/;
variable obj; equation eqobj; eqobj.. obj =e= x['1'] x['2'];
parameter ms, ss, lbd, ubd, cpu;
model mod /all/;
option decimals = 8;
solve mod minimizing obj using minlp;
lbd=mod.objest; ubd=obj.l;
ms=mod.modelstat; ss=mod.solvestat; cpu=mod.resusd;

and using the gams-c api to let gams solve it. Afterwards, I want to obtain the results in c using this method:

auto value = m_job.outDB().getVariable(var).findRecord().level();

were job is my GAMSJob, which was used to solve the program above and var is a string containing the variable name, whose value I want to obtain. This methods works perfectly with one dimensional variables, e.g. when my variable looks as follows

variable x;
x.up = 1;
x.lo = 0;

and I am passing "x" as var in the code above.

I have tried to access the entries of the multidimensional variables now with a string like "x['1']", but then always 0 is returned. What is the correct way to obtain the values I want, i.e. the entries of the multidimensional variable?

CodePudding user response:

I guess you want to iterate over all records of x? There is actually an example in the tutorial for a two dimension variable doing this:

for (GAMSVariableRecord rec : m_job.outDB().getVariable("x"))
    cout << "x(" << rec.key(0) << "," << rec.key(1) << "):" << " level=" << rec.level() << " marginal=" << rec.marginal() << endl;
  • Related