Follow us on Twitter and LinkedIn to get the latest updates from the dev team!
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()
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'}
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}
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.
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.
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)
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}
Get the number of variables.
Get the solution status.
Start the optimization process.
Write the solution (in AMPL-compatible .sol format) to the specified file.
Get the current objective value.
Get the error message corresponding to the code.
Enable adding lazy constraints via callbacks (to be called only once)
Utility function: prints all variables to screen.
Get the name of the NL file from which the model has been loaded from
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)
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)
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)
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 via AMPLModel.setCallback() before starting the solution process via AMPLModel.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.
GenericCallback.run()
AMPLModel.setCallback()
AMPLModel.optimize()
Function to override, called periodically by the optimizer.
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().
AMPLModel.getSolutionVector()
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().
AMPLModel.getSolutionDict()
Get the current objective value. Note that this method can not be called for all stages of the solution process.
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
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 or amplpy_cplex.CPLEXCallback depending on which solver is being used)
amplpy_gurobi.GurobiCallback
amplpy_cplex.CPLEXCallback
Get a textual representation of when in the solution process the callback has been called.
Get the message that was being printed (valid is if GenericCallback.getAMPLWhere() == ampls.Where.MSG)
GenericCallback.getAMPLWhere()
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.
Get the map AMPLEntityName -> SolverVarIndex. See AMPLModel.getVarMap().
AMPLModel.getVarMap()
Get the map SolverVarIndex -> AMPLEntityName. See AMPLModel.getVarMapInverse().
AMPLModel.getVarMapInverse()
Get the map AMPLEntityName -> SolverVarIndex for the AMPL variables which start with the specified string. See AMPLModel.getVarMapInverse().
Add a user cut using AMPL variables names.
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
Add a lazy constraint using AMPL variables names.
Add a user cut using solver indices.
nvars – Number of variables in the cut (length of vars)
vars (list) – Vector of variable indices (in the solvers representation)
Add a lazy constraint using solver indices.
Represent the direction of a constraint.
Less or equal
Greater or equal
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.
When the solver wants to print a message, obtain it via GenericCallback::getMessage
Presolve phase
Executing simplex
Exploring a MIP node
Found a new MIP solution
Executing MIP algorithm
Not mapped, refer to the specific user documentation
These (generic) values map the most important solver statuses to a generic enumeration. Possible values are:
Solution status unknown
Optimal solution returned
Unfeasible problem
Unbounded problem
Hit an iterations limit
Hit a nodes limit
Hit a time limit
Hit a number of solutions limit
Interrupted by the user
Solution status not mapped in terms of generic ampls.Status enumeration. Use the solver specific API to obtain more information.
previous
Reference
next
AMPL