AMPLModel¶
- class ampls.AMPLModel¶
Store an in-memory representation of an AMPL model, which can be constructed by loading it from an NL file. It also contains two-way mappings between solver column and row numbers and AMPL entity names. In the following documentation, a very simple AMPL model will be used as a reference to show various function output values:
set A{1..5}; var x[A]; var y <=0.5; maximize z: sum{a in A} x[a] + y; c{a in A}: x[a] <= a;
This model is supposed to be loaded in the ampls interface via SolverDriver.loadModel() or via amplpy.AMPL.exportModel()
- getVarMapInverse() dict ¶
Get the map from variable index in the solver interface to AMPL variable instance name. On the example above:
model.getVarMapInverse(); {0: 'x[1]', 1: 'x[2]', 2: 'x[3]', 3: 'x[4]', 4: 'x[5]', 5: 'y'}
- getVarMap() dict ¶
Get the map from variable name to index in the solver interface. On the example above:
model.getVarMap() {'x[1]': 0, 'x[2]': 1, 'x[3]': 2, 'x[4]': 3, 'x[5]': 4, 'y': 5}
- getVarMapFiltered(prefix: str) dict ¶
Get the variable map filtered by the variable name, to avoid getting the whole (possibly large) map. The map is constructed only by the variable instances beginning with the specified string.
- setCallback(callback: GenericCallback)¶
Set a callback to be called during optimization. The callback must derive from the class GenericCallback and implement the method run, that will be called at various steps during the optimization.
- getSolutionVector()¶
Get a vector with all the variables values for the current problem:
model.getSolutionVector() (1.0, 2.0, 3.0, 4.0, 5.0, 0.5)
- getSolutionDict() dict ¶
Get a dictionary { AMPLVariableName -> value}. For the example problem:
model.getSolutionDict() {'x[1]': 1.0, 'x[2]': 2.0, 'x[3]': 3.0, 'x[4]': 4.0, 'x[5]': 5.0, 'y': 0.5}
- getNumVars()¶
Get the number of variables.
- getStatus() ampls.Status ¶
Get the solution status.
- optimize()¶
Start the optimization process.
- writeSol(solFileName: string)¶
Write the solution (in AMPL-compatible .sol format) to the specified file.
- getObj()¶
Get the current objective value.
- error(code: int)¶
Get the error message corresponding to the code.
- enableLazyConstraints()¶
Enable adding lazy constraints via callbacks (to be called only once)
- printModelVars(onlyNonZero: bool)¶
Utility function: prints all variables to screen.
- getFileName() string ¶
Get the name of the NL file from which the model has been loaded from
- setAMPLParameter(whichParameter: ampls.SolverParams, value)¶
Set a generic solver parameter to the specified value. The paramter currently mapped are only the ones accessible via the ampls.SolverParams enumeration. To set other solver controls refer to the solver specific API. Example:
model.setAMPLParameter(ampls.SolverParams.INT_LP_Algorithm, ampls.LPAlgorithms.Barrier)
- getAMPLIntParameter(whichParameter: ampls.SolverParams)¶
Get the current value of an (integer) solver control. The type of the solver control is obvious by the enumeration name (e.g. ampls.SolverParams.INT_SolutionLimit)
- getAMPLDoubleParameter(whichParameter: ampls.SolverParams)¶
Get the current value of a (float) solver control. The type of the solver control is obvious by the enumeration name (e.g. ampls.SolverParams.DBL_MIPGap)
GenericCallback¶
- class ampls.GenericCallback¶
Base abstract class for generic callbacks, inherit from this to declare a generic callback. Provides all mapping between solver-specific and generic values. To implement a callback, you should implement the
GenericCallback.run()
method and set it viaAMPLModel.setCallback()
before starting the solution process viaAMPLModel.optimize()
. Depending on where the callback is called from, you can obtain various information about the progress of the optimization and can modify the behaviour of the solver.- run()¶
Function to override, called periodically by the optimizer.
- getSolutionVector()¶
Get the current solution as a vector (see
AMPLModel.getSolutionVector()
). Note that this method can not be called for all stages of the solution process, namely it can not be called for all values of py:meth:GenericCallback.getAMPLWhere().
- getSolutionDict() dict ¶
Get the current solution as a dictionary (see
AMPLModel.getSolutionDict()
). Note that this method can not be called for all stages of the solution process, namely it can not be called for all values of py:meth:GenericCallback.getAMPLWhere().
- getObj()¶
Get the current objective value. Note that this method can not be called for all stages of the solution process.
- getWhere()¶
Get an iteger representing where in the solution process the callback has been called. NOTE: this is expressed using the solver’s own (not mapped) values
- getAMPLWhere() ampls.Where ¶
Get where in the solution process the callback has been called (mapped). Not all possible values are mapped; in case more advanced functionality is needed, please refer to the solver-specific documentation (inherit directly from
amplpy_gurobi.GurobiCallback
oramplpy_cplex.CPLEXCallback
depending on which solver is being used)
- getWhereString()¶
Get a textual representation of when in the solution process the callback has been called.
- getMessage()¶
Get the message that was being printed (valid is if
GenericCallback.getAMPLWhere()
== ampls.Where.MSG)
- getValue(Value.CBValue v)¶
Get a (mapped) value from the solver. Not all possible values are accessible via this interface, for more advanced functionality refer to the solver specific documentation.
- getVarMap()¶
Get the map AMPLEntityName -> SolverVarIndex. See
AMPLModel.getVarMap()
.
- getVarMapInverse()¶
Get the map SolverVarIndex -> AMPLEntityName. See
AMPLModel.getVarMapInverse()
.
- getVarMapInverse(name: string)¶
Get the map AMPLEntityName -> SolverVarIndex for the AMPL variables which start with the specified string. See
AMPLModel.getVarMapInverse()
.
- addCut(vars, coeffs, direction, rhs)¶
Add a user cut using AMPL variables names.
- Parameters
vars (list) – List of AMPL variable names
coeffs (list) – Vector of cut coefficients
direction (CutDirection) – Direction of the constraint
rhs (dbl) – Right hand side value
- addLazy(vars, coeffs, direction, rhs)¶
Add a lazy constraint using AMPL variables names.
- Parameters
vars (list) – List of AMPL variable names
coeffs (list) – Vector of cut coefficients
direction (CutDirection) – Direction of the constraint
rhs (dbl) – Right hand side value
- addCutsIndices(nvars, vars, coeffs, direction, rhs)¶
Add a user cut using solver indices.
- Parameters
nvars – Number of variables in the cut (length of vars)
vars (list) – Vector of variable indices (in the solvers representation)
coeffs (list) – Vector of cut coefficients
direction (CutDirection) – Direction of the constraint
rhs (dbl) – Right hand side value
- addLazyIndices(nvars, vars, coeffs, direction, rhs)¶
Add a lazy constraint using solver indices.
- Parameters
nvars – Number of variables in the cut (length of vars)
vars (list) – Vector of variable indices (in the solvers representation)
coeffs (list) – Vector of cut coefficients
direction (CutDirection) – Direction of the constraint
rhs (dbl) – Right hand side value
CutDirection¶
- class ampls.CutDirection¶
Represent the direction of a constraint.
- LE¶
Less or equal
- GE¶
Greater or equal
CBWhere¶
- class ampls.CBWhere¶
These values (generic) identify where in the solution process a callback has been called; to get this generic value call GenericCallback::getAMPLType(). Not all solvers “where” are mapped to these values; in case the callback is called with a not-mapped “where” parameter, refer to the solver-specific functionality.
- MSG¶
When the solver wants to print a message, obtain it via GenericCallback::getMessage
Status¶
- class ampls.Status¶
These (generic) values map the most important solver statuses to a generic enumeration. Possible values are:
- UNKNOWN¶
Solution status unknown
- OPTIMAL¶
Optimal solution returned
- INFEASIBLE,
Unfeasible problem
- UNBOUNDED¶
Unbounded problem
- LIMIT_ITERATION¶
Hit an iterations limit
- LIMIT_NODE¶
Hit a nodes limit
- LIMIT_TIME¶
Hit a time limit
- LIMIT_SOLUTION¶
Hit a number of solutions limit
- INTERRUPTED¶
Interrupted by the user
- NOTMAPPED¶
Solution status not mapped in terms of generic ampls.Status enumeration. Use the solver specific API to obtain more information.