AMPLPY using list as a parameter value


I am using amplpy and having an issue when I try to use a list of strings as a parameter value, since param only takes scalar values (per my understanding).

I essentially have the following:

set S = {‘Mon’, ‘Tue’, ‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’, ‘Sun’} ;

param O {S};

I would like O to be: { ‘Mon’:[‘Sat’, ‘Sun’], ‘Tue’:[‘Sun’, ‘Mon’], … ‘Sun’:[‘Fri’, ‘Sat’] }

The intent is to use O to exclude certain values when using sum, so that I can sum for 5 days of the week instead of all 7.

AMPL gives me an error message when I set O to be a dictionary like the above.

The workaround constraints I have used are shown in the following:

ampl.eval (‘’’


set S;
param demand {S} ;

var x {S} >= 0 integer;

minimize workers_objective : sum {j in S} x[j] ;

subject to

mon_constraint : sum {i in S} x[i] - x[‘Tue’] - x[‘Wed’] >= demand[‘Mon’] ;
tue_constraint : sum {i in S} x[i] - x[‘Wed’] - x[‘Thu’] >= demand[‘Tue’] ;
wed_constraint : sum {i in S} x[i] - x[‘Thu’] - x[‘Fri’] >= demand[‘Wed’] ;
thu_constraint : sum {i in S} x[i] - x[‘Fri’] - x[‘Sat’] >= demand[‘Thu’] ;
fri_constraint : sum {i in S} x[i] - x[‘Sat’] - x[‘Sun’] >= demand[‘Fri’] ;
sat_constraint : sum {i in S} x[i] - x[‘Sun’] - x[‘Mon’] >= demand[‘Sat’] ;
sun_constraint : sum {i in S} x[i] - x[‘Mon’] - x[‘Tue’] >= demand[‘Sun’] ;


S = [‘Mon’, ‘Tue’, ‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’, ‘Sun’]
demand = {‘Mon’: 17, ‘Tue’: 13, ‘Wed’: 15, ‘Thu’: 19, ‘Fri’: 14, ‘Sat’: 16, ‘Sun’: 11}


My preference would be to have a single constraint which utilizes the values of param O to subtract appropriately for each day of the week. Hope the above is clear.

In the pyomo version, I’m able to achieve this with the following python code:

for i in S:
sum(model.x[j] for j in S if i not in O[j]) >= demand[i])

Would appreciate any guidance. I’m happy to provide more information as needed.



Note: This question was answered on Google Groups, and I am just copying the answer here:

For the specific situation you are describing, the following definitions would be sufficient:

set D circular = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'} ;
param demand {D};

var x {D} >= 0;

subject to day_constraint {d in D}:
   sum {dd in D diff {next(d),next(d,2)}} x[dd] >= demand[d];

Then you would just need to provide AMPL with data for param demand. However, if you need something more general then you can use an indexed collection of AMPL sets:

set D circular = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'} ;
set O {D};
param demand {D};

var x {D} >= 0;

subject to day_constraint {d in D}:
   sum {dd in D diff O[d]} x[dd] >= demand[d];

Then you also need to provide AMPL with the members of the sets O[d] for each d in D. Write back if you need help with that.