Model#

class gamspy.Model(container: Container, name: str | None = None, problem: Problem | str = Problem.LP, equations: Sequence[Equation] = [], sense: Sense | str | None = None, objective: Variable | Expression | None = None, matches: dict[Equation, Variable] | None = None, limited_variables: Iterable[Variable] | None = None)[source]#

Bases: object

Represents a list of equations to be solved.

Parameters:
containerContainer

Container of the model.

namestr, optional

Name of the model. Name is autogenerated by default.

equationsSequence[Equation]

Sequence of Equation objects.

problemProblem or str, optional

‘LP’, ‘NLP’, ‘QCP’, ‘DNLP’, ‘MIP’, ‘RMIP’, ‘MINLP’, ‘RMINLP’, ‘MIQCP’, ‘RMIQCP’, ‘MCP’, ‘CNS’, ‘MPEC’, ‘RMPEC’, ‘EMP’, or ‘MPSGE’, by default Problem.LP.

senseSense, optional

“MIN”, “MAX”, or “FEASIBILITY”.

objectiveVariable | Expression, optional

Objective variable to minimize or maximize or objective itself.

matchesdict[Equation, Variable]

Equation - Variable matches for MCP models.

limited_variablesIterable, optional

Allows limiting the domain of variables used in a model.

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> v = gp.Variable(m, "v")
>>> e = gp.Equation(m, "e", definition= v == 5)
>>> my_model = gp.Model(m, "my_model", "LP", [e])
Attributes:
infeasibility_tolerance

This option sets the tolerance for marking an equation infeasible in the equation listing.

Methods

compute_infeasibilities()

Computes infeasabilities for all equations of the model

freeze(modifiables[, options])

Freezes all symbols except modifiable symbols.

getDeclaration()

Declaration of the Model in GAMS

interrupt()

Sends interrupt signal to the running job.

solve([solver, options, solver_options, ...])

Solves the model with given options.

toGams(path)

Generates GAMS model under path/<model_name>.gms

unfreeze()

Unfreezes the model

compute_infeasibilities() dict[str, pd.DataFrame][source]#

Computes infeasabilities for all equations of the model

Returns:
dict[str, pd.DataFrame]

Dictionary of infeasibilities where equation names are keys and infeasibilities are values

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, name="i", records=["i1", "i2"])
>>> j = gp.Set(m, name="j", records=["j1", "j2", "j3"])
>>> a = gp.Parameter(m, name="a", domain=i, records=[("i1", 350), ("i2", 600)])
>>> b = gp.Parameter(m, name="b", domain=j, records=[("j1", 400), ("j2", 450), ("j3", 420)])
>>> x = gp.Variable(m, name="x", domain=[i,j], type="Positive")
>>> s = gp.Equation(m, name="s", domain=i)
>>> d = gp.Equation(m, name="d", domain=j)
>>> s[i] = gp.Sum(j, x[i, j]) <= a[i]
>>> d[j] = gp.Sum(i, x[i, j]) >= b[j]
>>> my_model = gp.Model(m, name="my_model", equations=m.getEquations(), problem="LP", sense="min", objective=gp.Sum((i, j), x[i, j]))
>>> summary = my_model.solve()
>>> infeasibilities = my_model.compute_infeasibilities()
>>> infeasibilities["s"].infeasibility.item()
320.0
property infeasibility_tolerance: float | None#

This option sets the tolerance for marking an equation infeasible in the equation listing. By default, 1.0e-13.

Returns:
float | None
interrupt() None[source]#

Sends interrupt signal to the running job.

Raises:
ValidationError

If the job is not initialized

freeze(modifiables: list[Parameter | ImplicitParameter], options: Options | None = None) None[source]#

Freezes all symbols except modifiable symbols.

Parameters:
modifiablesList[Parameter | ImplicitParameter]
freeze_optionsdict, optional

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> a = gp.Parameter(m, name="a", records=10)
>>> x = gp.Variable(m, name="x")
>>> e = gp.Equation(m, name="e", definition= x <= a)
>>> my_model = gp.Model(m, name="my_model", equations=m.getEquations(), problem="LP", sense="max", objective=x)
>>> solved = my_model.solve()
>>> float(x.toValue())
10.0
>>> my_model.freeze(modifiables=[a])
>>> a.setRecords(35)
>>> solved = my_model.solve()
>>> float(x.toValue())
35.0
unfreeze() None[source]#

Unfreezes the model

solve(solver: str | None = None, options: Options | None = None, solver_options: dict | None = None, model_instance_options: ModelInstanceOptions | dict | None = None, output: io.TextIOWrapper | None = None, backend: Literal['local', 'engine', 'neos'] = 'local', client: EngineClient | NeosClient | None = None) pd.DataFrame | None[source]#

Solves the model with given options.

Parameters:
solverstr, optional

Solver name

optionsOptions, optional

GAMS options

solver_optionsdict, optional

Solver options

model_instance_optionsoptional

Model instance options

outputTextIOWrapper, optional

Output redirection target

backendstr, optional

Backend to run on

clientEngineClient, NeosClient, optional

EngineClient to communicate with GAMS Engine or NEOS Client to communicate with NEOS Server

Returns:
DataFrame, optional

Summary of the solve

Raises:
ValidationError

In case engine_config is not provided for engine backend or neos_client is not provided for neos backend.

ValueError

In case problem is not in possible problem types

ValueError

In case sense is different than “MIN” or “MAX”

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> v = gp.Variable(m, "v")
>>> e = gp.Equation(m, "e", definition= v == 5)
>>> my_model = gp.Model(m, "my_model", "LP", [e], "max", v)
>>> solved = my_model.solve()
getDeclaration() str[source]#

Declaration of the Model in GAMS

Returns:
str

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> v = gp.Variable(m, "v")
>>> e = gp.Equation(m, "e", definition= v == 5)
>>> my_model = gp.Model(m, "my_model", "LP", [e])
>>> my_model.getDeclaration()
'Model my_model / e /;'
toGams(path: str) None[source]#

Generates GAMS model under path/<model_name>.gms

Parameters:
pathstr

Path to the directory which will contain the GAMS model.

class gamspy.ModelStatus(value)[source]#

Bases: Enum

An enumeration for model status types

OptimalGlobal = 1#
OptimalLocal = 2#
Unbounded = 3#
InfeasibleGlobal = 4#
InfeasibleLocal = 5#
InfeasibleIntermed = 6#
Feasible = 7#
Integer = 8#
NonIntegerIntermed = 9#
IntegerInfeasible = 10#
LicenseError = 11#
ErrorUnknown = 12#
ErrorNoSolution = 13#
NoSolutionReturned = 14#
SolvedUnique = 15#
Solved = 16#
SolvedSingular = 17#
UnboundedNoSolution = 18#
InfeasibleNoSolution = 19#
class gamspy.SolveStatus(value)[source]#

Bases: Enum

An enumeration for solve status types

NormalCompletion = 1#
IterationInterrupt = 2#
ResourceInterrupt = 3#
TerminatedBySolver = 4#
EvaluationInterrupt = 5#
CapabilityError = 6#
LicenseError = 7#
UserInterrupt = 8#
SetupError = 9#
SolverError = 10#
InternalError = 11#
Skipped = 12#
SystemError = 13#