ampl api python , the proper way to index the element of parameter in the data file when it contains more than one dimension

76 views
Skip to first unread message

rupu...@gmail.com

unread,
Jan 15, 2018, 4:11:10 AM1/15/18
to AMPL Modeling Language
Hello,
    I have met a problem about how to modify the data file when the parameter is shaped with two dimensions, since the example provided is only one dimension.
    The parameter in data file is presented as following:

param XH :=
H1 C1 0.1
H1 C2 0.1
H2 C1 0.1
E4 C2 0.1
E5 C1 0.1 ;
    I want to change the value of the first one element to 0.2, so I put it in python this way
Bypass = ampl.getParameter("XH")
Bypass.setValues({['H1','C1']:0.2})
   The error message is presented as:
Bypass.setValues({['H1','C1']:0.2})
TypeError: unhashable type: 'list'
I also tried to use ''Bypass.setValues({[0]:0.2})'' to modify the first element, it lead to the same error information.
Can someone tell me the right way to index the element when there is more than one dimension for the parameter ?
Thank you very much for the help !

Filipe Brandão

unread,
Jan 15, 2018, 6:47:13 AM1/15/18
to AMPL Modeling Language
The proper way to change a single instance of an indexed parameter is the following:

Bypass = ampl.getParameter("XH")
Bypass['H1', 'C1'] = 0.2
setValues is intended to set multiple instances at one. It receives a python dictionary which cannot have lists as keys (python lists are unhashable), but you can use tuples as keys instead:
Bypass.setValues({('H1','C1'): 0.2})

Best regards,
Filipe

rupu...@gmail.com

unread,
Jan 15, 2018, 9:11:46 AM1/15/18
to AMPL Modeling Language
      Thank you very much Filipe, but I have another problem. I want to do some basic calculation with the results of variable T defined at ampl. As the following codes present:
x = ampl.getVariable('T')
xo = x.getValues()
x1 = x['C1',7]

Bypass = ampl.getParameter("XH")
Bypass['H1','C1']=0.5
t = ampl.getVariable('T')
to = t.getValues()
t1 = t['C1',7]
print (x1-t1)

The error message is that "unsupported operand type(s) for -: 'Variable' and 'Variable'". How should I extract the pure value from
the variable in the ampl, so I can proceed with math calculations?
Best wishes,
Rupu

在 2018年1月15日星期一 UTC+1下午12:47:13,Filipe Brandão写道:

Filipe Brandão

unread,
Jan 15, 2018, 5:19:04 PM1/15/18
to AMPL Modeling Language
x1 and t1 are two objects of the class Variable. You can see all the methods available for objects of this type at http://amplpy.readthedocs.io/en/latest/classes/variable.html. In order to compute the difference between the values of the two variables you should use x1.value()-t1.value().

Best regards,
Filipe

rupu...@gmail.com

unread,
Jan 16, 2018, 3:51:23 AM1/16/18
to AMPL Modeling Language
Thanks a lot Filipe, you are so kind. I will repeat the example files for ampl api to get better understanding of the amplpy, since it was the begining for me to work with programming language, I kept proposing silly questions.
Best wishes,
Rupu

在 2018年1月15日星期一 UTC+1下午11:19:04,Filipe Brandão写道:
Reply all
Reply to author
Forward
0 new messages