ampl / amplpy

Python API for AMPL

Home Page:https://amplpy.ampl.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set values indexed sets

MartinPJorge opened this issue · comments

Hi,

In the docs for class amplpy.Set(_impl) it is mentioned the following:

In case of indexed sets, you can gain access to the instances using the operator [].

How can I define an indexed set like the one below?

# .MOD
set FLOOR;
set family {FLOOR};

# .DAT
set FLOOR := 'first' 'second';
set family['first'] := 'Gutierrez';
set family['second'] := 'Montoro';

I'd like to do something like:

ampl.set['family']['first'] = 'Gutierrez'

Hi Martin,

You can do that as follows:

from amplpy import AMPL

ampl = AMPL()
ampl.eval('''
set FLOOR;
set family {FLOOR};
''')

ampl.set['FLOOR'] = ['first', 'third']
ampl.set['family']['first'] = ['Gutierrez']
ampl.set['family']['third'] = ['Montoro']
ampl.display('family') 

This will display the following:

set family[first] := Gutierrez;
set family[third] := Montoro;

Instead of ampl.set['family']['first'] = 'Gutierrez' you just needed to be assigned a list (considering that you are setting values for a set) instead of a string (i.e., ['Gutierrez'] instead of 'Gutierrez').

Fantastic!, thanks for the explanation.

I think it would be great if you include such example in the docs, just to illustrate how it is working with indexed sets. Maybe it is already there and I have not seen the example.

I agree with @MartinPJorge, it would be great if this example is added to the docs. I'll be happy to help