Bilevel problem in GAMSPy

Dear experts,

Is it possible to build a bi-level optimisation problem within GAMSPy? I mean : 1 upper problem (minimization) and 1 lower problem (maximization).

Thanks for your help!

Since gamspy_base does not include JAMS solver at the moment, you cannot do it with regular gamspy installation but what you can do is that you can use a GAMS installation as the system directory as follows:

import sys

from gamspy import (
    Container,
    Equation,
    Model,
    Problem,
    Sense,
    Variable,
)

m = Container(
    working_directory=".",
    system_directory="/opt/gams/gams47.3_linux_x64_64_sfx",
)

x = Variable(m, "x", type="positive")
y = Variable(m, "y", type="positive")
objout = Variable(m, "objout")
objin = Variable(m, "objin")

defout = Equation(m, "defout")
defin = Equation(m, "defin")
e1 = Equation(m, "e1")
e2 = Equation(m, "e2")
e3 = Equation(m, "e3")
e4 = Equation(m, "e4")

defout[...] = objout == x - 4 * y
defin[...] = objin == y

e1[...] = -x - y <= -3
e2[...] = -2 * x + y <= 0
e3[...] = 2 * x + y <= 12
e4[...] = 3 * x - 2 * y <= 4

bard = Model(
    m,
    "bard",
    equations=m.getEquations(),
    problem=Problem.EMP,
    sense=Sense.MIN,
    objective=objout,
)

with open("empinfo.dat", "w") as file:
    file.write("bilevel x min objin y defin e1 e2 e3 e4")

bard.solve(
    solver="JAMS",
    output=sys.stdout,
    solver_options={"empinfofile": "empinfo.dat"},
)

If you want to check the equivalent model in GAMS, see this: https://gams.com/latest/emplib_ml/libhtml/emplib_bard511.html