ampl / amplpy

Python API for AMPL

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reducing the range of index of a multi index parameter in loop

muv1023 opened this issue · comments

I have to reduce the range of the parameter of a two index parameter, S_{n,m}, in a loop. I am able to increase the range of index 'n' in a while loop while satisfying certain convergence condition. I start the loop with n=10. The data for S is updated each time from a random distribution and the model is solved until the convergence point. To achieve the convergence point, the value on 'n' needs to be increased. 'n' becomes 25 when the convergence point is reached at the first iteration. I need to repeat the whole process with a different value of another parameter. This time n is set to 10 from n =25 (from the previous iteration) by the following code

PathN=10
ampl.param['n'] = pathN.
path = np.arange(1,PathN+1)
timep = np.arange(1,13)
DemS = pd.DataFrame(Dem,index = path, columns=timep).stack(). 
S.setValues(DemS). 

I get the error invalid subscripts S[11,1], S[11,2],... However, if I set PathN=25 (the value of n when the previous loop ended), the problem is solved. Thus, I am not able to decrease the size of the index in a loop, but I can increase.

Any help to get this problem resolved is highly appreciated.
Thank you

Could you please provide a complete script so that we can reproduce this issue? You might be able to solve it just by adding ampl.eval('reset data S;') right after updating ampl.param['n']; you may need to do the same for any other set or parameter that will then be holding invalid data.

It's often possible to handle this situation though a change in the model. The idea is to index the data over a set that does not change. As an example,

param nMax integer > 0;
param mMax integer > 0;
param S {1..nMax,1..mMax};

param n <= nMax;
param m <= mMax;

The values of nMax and mMax should be set once and not changed. But the definitions of variables, objectives, and constraints should continue to use n and m. Then there will be no "invalid subscript" errors when n is reduced to 10 after being set to 25.

Thank you all ampl.eval('reset data S;') statement worked in my case. Setting a Max value is a great idea. However, it doesn't fit into the logical flow of my program.