GAMS-dev / gamspy

Python-based algebraic modeling interface to GAMS
Other
41 stars 2 forks source link

`toGams()` fails with `first` property of a set #6

Closed grecht closed 3 months ago

grecht commented 3 months ago

Here's an example:

from gamspy import Container, Sum, Sense

ct = Container()
s = ct.addSet("s", records=[1, 2])
x = ct.addVariable("x", type="positive", domain=s)
eq = ct.addEquation("eq", domain=s)
eq[s].where[~s.first] = x[s] >= 1

m = ct.addModel(
    problem="LP",
    name="test",
    sense=Sense.MIN,
    equations=[eq],
    objective=Sum(s, x[s]))

res = m.solve()
print(res)

m.toGams(".")

This results in the following output:

Solver Status   Model Status Objective Num of Equations Num of Variables Model Type Solver Solver Time
0        Normal  OptimalGlobal         1                2                3         LP  CPLEX           0
Traceback (most recent call last):
  File "C:\ProgramData\mambaforge\envs\simplesom\lib\site-packages\gams\transfer\containers\_container.py", line 147, in __getitem__       
    return self.data[sym]
  File "C:\ProgramData\mambaforge\envs\simplesom\lib\site-packages\gams\transfer\_internals\casepreservingdict.py", line 62, in __getitem__
    return self.data[self._casefolded_key_map[key.casefold()]]
KeyError: 's.first'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\rech_ge\Documents\workspace\simplesom\simplesom\test\to_gams_example.py", line 19, in <module>
    m.toGams(".")
  File "C:\ProgramData\mambaforge\envs\simplesom\lib\site-packages\gamspy\_model.py", line 796, in toGams
    all_needed_symbols = sorted(all_symbols, key=sort_names)
  File "C:\ProgramData\mambaforge\envs\simplesom\lib\site-packages\gamspy\_model.py", line 784, in sort_names
    return PRECEDENCE[type(self.container[name])]
  File "C:\ProgramData\mambaforge\envs\simplesom\lib\site-packages\gams\transfer\containers\_container.py", line 149, in __getitem__
    raise KeyError(
KeyError: 'Attempted retrieval of symbol `s.first`, but `s.first` does not exist in the Container'

The model is fine, but toGams() fails. So GAMSPy must be using a different toGams() function under the hood?

mabdullahsoyturk commented 3 months ago

Thanks for reporting. This issue is fixed in v0.13.5. You can just upgrade your gamspy version with "pip install gamspy --upgrade".