Hi, I am moving my AMPL code to the module amplpy and I got stuck when modifying my model data, probably this is a simple question because I don’t have much expertise in python. So, what I need is to pass this assignment to amplpy:
let Vq_inc[t,d,h,j]:=1;
In https://amplpy.readthedocs.io/ I found examples when the parameter is not indexed (e.g. doing index.set_values([1]) and when it has one index (e.g., index.set_values({i:0})); thus, for my problem I tried:
Vq_inc.set_values({(t,d,h,j):1 for t in T for d in D for h in H for j in No})
but it doesn’t work. Please, help me with that issue.
Thanks in advance
1 Like
Hi @Jonathan,
Vq_inc.set_values({(t,d,h,j):1 for t in T for d in D for h in H for j in No})
seems correct. Was it giving some error?
The following snippet assigns a dictionary to a parameter with 4 indices:
ampl = AMPL()
ampl.eval('param p{1..10, 1..10, 1..10, 1..10};')
ampl.param['p'].set_values({(i, j, k, l): 1 for i in range(1, 10+1) for j in range(1, 10+1) for k in range(1, 10+1) for l in range(1, 10+1)})
ampl.display('p')
Note: For this case in particular, ampl.eval('let {t in ..., d in ..., h in ..., j in ...} Vq_inc[t,d,h,j] := 1;')
(replacing ...
by the corresponding sets if they exist in AMPL) would be the fastest way to perform this operation as it would avoid data transfer from Python completely.
Hi Filipe,
Thanks for your response. The error that I obtained with Vq_inc.set_values({(t,d,h,j):1 for t in T for d in D for h in H for j in No}) is TypeError: Failed to cast value to int/float/double/string. The suggested routine works well, but I would like to replace the ranges by their corresponding sets, and probably that is the cause of the error, since T, D, H and No are sets. Finally, the last method indicated in the note works very well, so I will use it, thanks for that.