AMPLPY: Increase the size of a set
muv1023 opened this issue · comments
Hello,
I have a set R defined in the mod file as set RM:={4, 8, 12}; RM serves as an index to a parameter.
Now I need to add new elements to this list. The size of set RM will be NM, where NM is greater than 3. I tried the following
RM = ampl.getSet('RM')
ampl.eval('reset data RM;')
RMUpdate = np.full(NM,0)
# RMUpdate will be updated with new elements
RM.setValues(RMUpdate)
When I run this code I get an error
RuntimeError: file -
line 1 offset 25
RM was defined in the model
Any guidance is really appreciated.
Thanks
Hi, this is not really related to AMPLPY, it has more to do with how AMPL deals with data.
If you define the data while declaring the corresponding entity: set RM := {4, 8, 12};
AMPL will assume your data is fixed (from here the error "RM was defined in the model").
To allow subsequent modification of the data, you should separate declaration of the entity and of its data:
set RM;
data;
set RM := 4 8 12;
model;
Note that:
- Instead of being in-line, the data definition is usually placed in a separate file (with extension .dat), in which case you don't need the directive
data
- When using this way of defining data (called "data mode"), you don't need the brackets and commas to define the set members.
You can find further information in the AMPL book.
Hope this helps
The following works without any issues on the latest version:
from amplpy import AMPL
import numpy as np
ampl = AMPL()
ampl.eval(r"set RM;")
ampl.set["RM"] = np.arange(0, 10)
ampl.display("RM")
ampl.set["RM"] = np.arange(0, 20)
ampl.display("RM")
and produces the output:
set RM := 0 1 2 3 4 5 6 7 8 9;
set RM := 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19;