Model#
- class gamspy.Model(container: Container, name: str | None = None, problem: Problem | str = Problem.LP, equations: Iterable[Equation] = [], sense: Sense | str | None = None, objective: Variable | Expression | None = None, matches: dict[Equation, Variable] | None = None, limited_variables: Iterable[Variable] | None = None, external_module: str | None = None)[source]#
Bases:
objectRepresents a list of equations to be solved.
- Parameters:
- containerContainer
Container of the model.
- namestr, optional
Name of the model. Name is autogenerated by default.
- equationsIterable[Equation]
Iterable 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.
- external_module: str, optional
The name of the external module in which the external equations are implemented
- Attributes:
external_moduleName of the external module in which the external equations are implemented.
Methods
Computes infeasabilities for all equations of the model
freeze(modifiables[, options])Freezes all symbols except modifiable symbols.
Declaration of the Model in GAMS
getEquationListing([n, infeasibility_threshold])Returns the generated equations.
getVariableListing([n])Returns the variable listing.
Sends interrupt signal to the running job.
solve([solver, options, solver_options, ...])Solves the model with given options.
toGams(path[, options])Generates GAMS model under path/<model_name>.gms
toLatex(path[, generate_pdf])Generates a latex file that contains the model definition under path/<model_name>.gms
unfreeze()Unfreezes the 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])
- property external_module: str | None#
Name of the external module in which the external equations are implemented. By default, this parameter is set to None. When provided, it triggers the opening of the specified file using a File statement and incorporates the file into the model by adding it as an external module.
This feature requires a solid understanding of programming, compilation, and linking processes. For more information, please refer to the https://www.gams.com/latest/docs/UG_ExternalEquations.html .
- Returns:
- str | None
- computeInfeasibilities() 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.computeInfeasibilities() >>> infeasibilities["s"].infeasibility.item() 320.0
- getEquationListing(n: int | None = None, infeasibility_threshold: float | None = None) list[str][source]#
Returns the generated equations.
- Parameters:
- nint | None, optional
Number of equations to be returned.
- infeasibility_threshold: float, optional
Filters out equations with infeasibilities that are above this value.
- Returns:
- list[str]
- getVariableListing(n: int | None = None) list[str][source]#
Returns the variable listing.
- Parameters:
- nint | None, optional
Number of variables to be returned.
- Returns:
- list[str]
- 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
- 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, load_symbols: list[Symbol] | 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 /;'
- class gamspy.ModelStatus(value)[source]#
Bases:
EnumAn 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:
EnumAn 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#