# AMPLPY using list as a parameter value

**URL:** <https://discuss.ampl.com/t/amplpy-using-list-as-a-parameter-value/274>\
**Category:** New to AMPL\
**Created:** [December 16, 2022, 10:06pm UTC](https://discuss.ampl.com/t/amplpy-using-list-as-a-parameter-value/274 "2022-12-16T22:06:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atma\_Iyer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/atma_iyer/32/107_2.png) [@Atma\_Iyer](https://discuss.ampl.com/u/Atma_Iyer)\
**Post date:** [December 16, 2022, 10:06pm UTC](https://discuss.ampl.com/t/amplpy-using-list-as-a-parameter-value/274/1 "2022-12-16T22:06:18Z")

</div>

Hi,

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 (‘’’

reset;

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}

ampl.getSet(‘S’).setValues(S)  
ampl.getParameter(‘demand’).setValues(demand)

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:  
model.demand\_constraint.add(  
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.

Thanks

Atma

---

<div class="post-metadata">

**Author:** ![fdabrandao](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/fdabrandao/32/63_2.png) [@fdabrandao](https://discuss.ampl.com/u/fdabrandao)\
**Post date:** [January 3, 2023, 11:36am UTC](https://discuss.ampl.com/t/amplpy-using-list-as-a-parameter-value/274/2 "2023-01-03T11:36:44Z")

</div>

Note: This question was answered on [Google Groups](https://discuss.ampl.com/t/re-ampl-24419-amplpy-using-list-as-a-parameter-value/275), and I am just copying the answer here:

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

```auto
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:

```auto
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.
