How to use the let command on parameters indexed over multiple sets?

430 views
Skip to first unread message

Cathy Chamberlin

unread,
Feb 26, 2021, 11:56:55 AM2/26/21
to am...@googlegroups.com

Hello!

 

Apologies if there is another topic that addresses this, I was unable to find one that helped me fix this problem. I'm relatively new to AMPL and am trying to do a sensitivity analysis on an AMPL problem.

 

To find the sensitivity of a parameter, I am using the let function in a for loop to first compute all the values I want to try as a fraction of the original value of the parameter. I then use another for loop with the let function to use those values in the solver. This works well for parameters that are single values, however I am having difficulty with writing a script that will do this for parameters that are indexed over multiple sets.

 

In the simplified problem I have attached to this message, I would like to test values of the cost of one index value of a set (here it is 'Contour_Farming' in Set ag_bmp) over all of the indexes of another set (c in comid). To do this I am trying this:

 

set Sens;

let Sens := {0.01, 0.1, 0.5, 1, 5, 10, 100};

param SENSVALS {Sens, comid};

param frac;

for {s in Sens} {

  let frac := s;

  for {c in comid} {

    let SENSVALS[s,c] := frac * ag_costs['Contour_Farming', c];

    }

}

;

 

for {s in Sens} {

let frac := s;

  for {c in comid} {

    let ag_costs['Contour_Farming', c] :=  SENSVALS[s,c];

    }

;

solve;

}

 

CPLEX gives an error that there is an invalid subscript ag_costs['Contour_Farming', '4594081'], and I don't know why this is the case. It is defined in the model.

 

How can I format this problem correctly? I think maybe I don't understand how CPLEX is calling and assigning parameters, or maybe there is a glaringly obvious syntax error that I am missing?

 

Thanks!

Cathy

STcommand_sens_simple.amp
STdata_simple.dat
STmodel_simple.mod

Chamberlin, Catherine

unread,
Feb 26, 2021, 11:58:12 AM2/26/21
to am...@googlegroups.com
STcommand_sens_simple.amp
STdata_simple.dat
STmodel_simple.mod

Mikhail

unread,
Feb 26, 2021, 10:22:24 PM2/26/21
to AMPL Modeling Language
Hello!.
Because when you declaring:
param ag_costs {comid, ag_bmp};
param ag_effic {comid, ag_bmp}; 
the first index is comid. However, in what follows, you use:
...
     let SENSVALS [s, c]: = frac * ag_costs ['Contour_Farming', c];
...
     let ag_costs ['Contour_Farming', c]: = SENSVALS [s, c];
in place of 1 index ag_bmp
It should be written like this:
let SENSVALS [s, c]: = frac * ag_costs [c, 'Contour_Farming'];
let ag_costs [c, 'Contour_Farming']: = ...

пятница, 26 февраля 2021 г. в 19:58:12 UTC+3, Chamberlin, Catherine:

Cathy Chamberlin

unread,
Mar 1, 2021, 11:47:13 AM3/1/21
to AMPL Modeling Language
Ah, I see thank you. I did not realize that the order mattered.
Reply all
Reply to author
Forward
0 new messages