Problem loading and solving old gams model

My target is basically load old Gams models to GamsPy and solve it. I started with one of the examples from official GamsPy documentation (Blend Problem).

First I tried to save it as gdx with GamsPy. I did it because I was not have a traditional GAMS model right now that I can work, also for a basic start. I am just adding last small part of the code.

b1 = Model(
    container=m,
    name="b1",
    equations=[pc],
    problem="LP",
    sense=Sense.MIN,
    objective= Sum(alloy, price[alloy] * v[alloy])
)
b1.toGams(path="gams/b1")

report = Parameter(container=m, name="report", domain=[alloy, "*"])

b1.solve()
report[alloy, "blend-1"] = v.l[alloy]
m.write("b1_test.gdx")

After getting gdx outputs, I tried to load this problem in a different repo to see if I success to work with external non python models. When I try to run this code I got an error about equations.

from gamspy import Container, Model

b1 = Container(load_from="gams/b1/b1_data.gdx")
# b1.loadRecordsFromGdx("b1_test.gdx")

model = Model(b1, "b1_model", equations=b1.getEquations(), problem="LP", sense="MIN", objective=??)
model.solve()

Error Message:

gamspy.exceptions.ValidationError: `pc` has been declared as an equation but no equation definition was found.

I also tried it on a different example again from the official documentation. I tried the watch the changes and data in model. I can see the data and definition as Equation like the other model but somehow it does not works for me. Probably I am missing something.

Thanks

GDX file does not contain the definitions of equations. The purpose of the container.write method is to save the records of the symbols. Hence, you cannot do what you want to do with simply writing a gdx file and reloading it. Instead you can serialize your container and deserialize later. Serialization saves all records and equations definitions in a zip file. For example, you can serialize the blend model as follows:

import gamspy as gp
import numpy as np

m = gp.Container()
alloy = gp.Set(
    m,
    name="alloy",
    records=["a", "b", "c", "d", "e", "f", "g", "h", "i"],
    description="products on the market",
)
elem = gp.Set(
    m,
    name="elem",
    records=["lead", "zinc", "tin"],
    description="required elements",
)

compdat = gp.Parameter(
    m,
    name="compdat",
    domain=[elem, alloy],
    records=np.array(
        [
            [10, 10, 40, 60, 30, 30, 30, 50, 20],
            [10, 30, 50, 30, 30, 40, 20, 40, 30],
            [80, 60, 10, 10, 40, 30, 50, 10, 50],
        ]
    ),
    description="composition data (pct)",
)
price = gp.Parameter(
    m,
    name="price",
    domain=alloy,
    records=np.array([4.1, 4.3, 5.8, 6.0, 7.6, 7.5, 7.3, 6.9, 7.3]),
    description="composition data (price)",
)
rb = gp.Parameter(
    m,
    name="rb",
    domain=elem,
    records=np.array([30, 30, 40]),
    description="required blend",
)

v = gp.Variable(
    m,
    name="v",
    domain=alloy,
    type="Positive",
    description="purchase of alloy (pounds)",
)

objective = gp.Sum(alloy, price[alloy] * v[alloy])
pc = gp.Equation(m, name="pc", domain=elem, description="purchase constraint")
mb = gp.Equation(m, name="mb", description="material balance")

pc[elem] = gp.Sum(alloy, compdat[elem, alloy] * v[alloy]) == rb[elem]
mb[...] = gp.Sum(alloy, v[alloy]) == 1

b1 = gp.Model(
    m,
    name="b1",
    equations=[pc],
    problem="LP",
    sense=gp.Sense.MIN,
    objective=objective,
)
gp.serialize(m, "path_to_the_zip_file.zip")

then, you can later deserialize it and solve as follows:

import gamspy as gp
m = gp.deserialize("path_to_the_zip_file.zip")
b1 = m.models["b1"]
b1.solve()