14.2.21 Class Model

mosek::fusion::Model

The object containing all data related to a single optimization model.

Implements:
 

BaseModel

Members:
 

Model.acceptedSolutionStatus – Set the accepted solution status.

Model.breakSolver – Request that the solver terminates as soon as possible.

Model.clone – Return a copy of the model.

Model.constraint – Create a new constraint in the model.

Model.dispose – Destroy the Model object

Model.dualObjValue – Get the dual objective value in the current solution.

Model.flushSolutions – If any solution values have been provided, flush those values to the underlying task.

Model.getAcceptedSolutionStatus – Get the accepted solution status.

Model.getConstraint – Get the constraint matching the given name or linear index.

Model.getDualSolutionStatus – Return the status of the dual solution.

Model.getName – Return the model name, or an empty string if it has not been set.

Model.getPrimalSolutionStatus – Return the status of the primal solution.

Model.getProblemStatus – Return the problem status.

Model.getSolverDoubleInfo – Fetch a solution information item from the solver

Model.getSolverIntInfo – Fetch a solution information item from the solver

Model.getSolverLIntInfo – Fetch a solution information item from the solver

Model.getTask – Return the underlying MOSEK task object.

Model.getVariable – Get the variable matching the given name or linear index.

Model.hasConstraint – Check whether the model contains a constraint with a given name.

Model.hasVariable – Check whether the model contains a variable with a given name.

Model.numConstraints – Return the number of constraints.

Model.numVariables – Return the number of variables.

Model.objective – Replace the objective expression.

Model.primalObjValue – Get the primal objective value in the current solution.

Model.selectedSolution – Chooses a solution.

Model.setCallbackHandler – Attach a progress callback handler.

Model.setDataCallbackHandler – Attach a data callback handler.

Model.setLogHandler – Attach a log handler.

Model.setSolverParam – Set a solver parameter

Model.solve – Attempt to optimize the model.

Model.variable – Create a new variable in the model.

Model.writeTask – Dump the current solver task to a file.

Static members:
 

Model.putlicensecode – Set the license code in the global environment.

Model.putlicensepath – Set the license path in the global environment.

Model.putlicensewait – Set the license wait flag in the global environment.

Model.acceptedSolutionStatus
void acceptedSolutionStatus(AccSolutionStatus what)

Set the accepted solution status. This defines which solution status values are considered as acceptable when fetching a solution. Requesting a solution value for a variable or constraint when the status does not match at least the accepted value will cause an error.

By default the accepted solution status is AccSolutionStatus.NearOptimal. It is necessary to change the accepted status to access sub-optimal solutions and infeasibility certificates.

The methods Model.getPrimalSolutionStatus and Model.getDualSolutionStatus can be used to get the actual status of the solutions.

Parameters:
 what (AccSolutionStatus) – The new accepted solution status.
Model.breakSolver
void breakSolver()

Request that the solver terminates as soon as possible. This must be called from another thread than the one in which solve() was called, or from a callback function.

The method does not stop the solver directly, rather it sets a flag that the solver checks occasionally, indicating it should terminate.

Model.clone
Model::t clone()

Return a copy of the model.

Return:
  (Model)
Model.constraint
Constraint::t constraint(string name, Expression::t expr, PSDDomain::t psddom)
Constraint::t constraint(Expression::t expr, PSDDomain::t psddom)
Constraint::t constraint(string name, Expression::t expr, LinPSDDomain::t lpsddom)
Constraint::t constraint(Expression::t expr, LinPSDDomain::t lpsddom)
Constraint::t constraint(string name, Set::t shape, Expression::t expr, LinearDomain::t ldom)
Constraint::t constraint(Set::t shape, Expression::t expr, LinearDomain::t ldom)
Constraint::t constraint(string name, Expression::t expr, LinearDomain::t ldom)
Constraint::t constraint(Expression::t expr, LinearDomain::t ldom)
Constraint::t constraint(string name, Set::t shape, Expression::t expr, RangeDomain::t rdom)
Constraint::t constraint(Set::t shape, Expression::t expr, RangeDomain::t rdom)
Constraint::t constraint(string name, Expression::t expr, RangeDomain::t rdom)
Constraint::t constraint(Expression::t expr, RangeDomain::t rdom)
Constraint::t constraint(string name, Set::t shape, Expression::t expr, QConeDomain::t qdom)
Constraint::t constraint(Set::t shape, Expression::t expr, QConeDomain::t qdom)
Constraint::t constraint(string name, Expression::t expr, QConeDomain::t qdom)
Constraint::t constraint(Expression::t expr, QConeDomain::t qdom)
Constraint::t constraint(string name, Variable::t v, PSDDomain::t psddom)
Constraint::t constraint(Variable::t v, PSDDomain::t psddom)
Constraint::t constraint(string name, Variable::t v, LinPSDDomain::t lpsddom)
Constraint::t constraint(Variable::t v, LinPSDDomain::t lpsddom)
Constraint::t constraint(string name, Set::t shape, Variable::t v, LinearDomain::t ldom)
Constraint::t constraint(Set::t shape, Variable::t v, LinearDomain::t ldom)
Constraint::t constraint(string name, Variable::t v, LinearDomain::t ldom)
Constraint::t constraint(Variable::t v, LinearDomain::t ldom)
Constraint::t constraint(string name, Set::t shape, Variable::t v, RangeDomain::t rdom)
Constraint::t constraint(Set::t shape, Variable::t v, RangeDomain::t rdom)
Constraint::t constraint(string name, Variable::t v, RangeDomain::t rdom)
Constraint::t constraint(Variable::t v, RangeDomain::t rdom)
Constraint::t constraint(string name, Set::t shape, Variable::t v, QConeDomain::t qdom)
Constraint::t constraint(Set::t shape, Variable::t v, QConeDomain::t qdom)
Constraint::t constraint(string name, Variable::t v, QConeDomain::t qdom)
Constraint::t constraint(Variable::t v, QConeDomain::t qdom)

Adds a new constraint to the model. A constraint is always a statement that an expression or variable belongs to a domain. Constraints can have optional names.

Typical domains used for defining constraints include:

Parameters:
 
  • name (string) – Name of the constraint. This must be unique among all constraints in the model. The value NULL is allowed instead of a unique name.
  • expr (Expression) – An expression.
  • psddom (PSDDomain) – A positive semidefinte domain.
  • lpsddom (LinPSDDomain) – A linear positive semidefinte domain.
  • shape (Set) – Defines the shape of the constraint. If this is NULL, the shape will be derived from the shape of the expression or variable.
  • ldom (LinearDomain) – A linear domain.
  • rdom (RangeDomain) – A ranged domain.
  • qdom (QConeDomain) – A domain in a second order cone.
  • v (Variable) – A variable used as an expression.
Return:
 

(Constraint)

Model.dispose
void dispose()

Destroy the Model object. This removes all references to other objects from the Model.

This helps garbage collection by removing cyclic references, and in some cases it is necessary to ensure that the garbage collector can collect the Model object and assosiated objects.

Model.dualObjValue
double dualObjValue()

Get the dual objective value in the current solution.

Return:
  (double)
Model.flushSolutions
void flushSolutions()

If any solution values have been provided, flush those values to the underlying task.

Model.getAcceptedSolutionStatus
AccSolutionStatus getAcceptedSolutionStatus()

Get the accepted solution status.

Return:
  (AccSolutionStatus)
Model.getConstraint
Constraint::t getConstraint(string name)
Constraint::t getConstraint(int index)

Get the constraint matching the given name or linear index. Constraints are assigned indices in the order they are added to the model.

Parameters:
 
  • name (string) – The constraint’s name.
  • index (int) – The constraint’s linear index.
Return:
 

(Constraint)

Model.getDualSolutionStatus
SolutionStatus getDualSolutionStatus(SolutionType which)
SolutionStatus getDualSolutionStatus()

Return the status of the dual solution. If no solution type is given the solution set with Model.selectedSolution is checked. It is recommended to check the problem and solution status before accessing the solution values.

Parameters:
 which (SolutionType) – The type of the solution for which status is requested.
Return:
  (SolutionStatus)
Model.getName
string getName()

Return the model name, or an empty string if it has not been set.

Return:
  (string)
Model.getPrimalSolutionStatus
SolutionStatus getPrimalSolutionStatus(SolutionType which)
SolutionStatus getPrimalSolutionStatus()

Return the status of the primal solution. If no solution type is given the solution set with Model.selectedSolution is checked. It is recommended to check the problem and solution status before accessing the solution values.

Parameters:
 which (SolutionType) – The type of the solution for which status is requested.
Return:
  (SolutionStatus)
Model.getProblemStatus
ProblemStatus getProblemStatus(SolutionType which)

Return the problem status. It is recommended to check the problem and solution status before accessing the solution values.

Parameters:
 which (SolutionType) – The type of the solution.
Return:
  (ProblemStatus)
Model.getSolverDoubleInfo
double getSolverDoubleInfo(string name)

This method returns the value for the specified double solver information item. The information items become available during and after the solver execution. A runtime exception is thrown if a non-existing information item is requested. The double information items are listed in Section Double information items.

Parameters:
 name (string) – A string name of the information item.
Return:
  (double)
Model.getSolverIntInfo
int getSolverIntInfo(string name)

This method returns the value for the specified integer solver information item. The information items become available during and after the solver execution. A runtime exception is thrown if a non-existing information item is requested. The integer information items are listed in Section Integer information items..

Parameters:
 name (string) – A string name of the information item.
Return:
  (int)
Model.getSolverLIntInfo
long long getSolverLIntInfo(string name)

This method returns the value for the specified long solver information item. The information items become available during and after the solver execution. A runtime exception is thrown if a non-existing information item is requested. The long integer information items are listed in Section Long integer information items..

Parameters:
 name (string) – A string name of the information item.
Return:
  (long long)
Model.getTask
mosek.Task getTask()

Returns the underlying MOSEK Task object. Note that the returned object is the actual underlying object, not a copy. This means if the returned object is modified by the user, the Model object may become invalid. Accessing the task object should never be necessary, except maybe for advanced debugging. For details on the Task object see the Optimizer API documentation.

Return:
  (Task)
Model.getVariable
Variable::t getVariable(string name)
Variable::t getVariable(int index)

Get the variable matching the given name or linear index. Variables are assigned indices in the order they are added to the model.

Parameters:
 
  • name (string) – The variable’s name.
  • index (int) – The variable’s linear index.
Return:
 

(Variable)

Model.hasConstraint
bool hasConstraint(string name)

Check whether the model contains a constraint with a given name.

Parameters:
 name (string) – The constraint name.
Return:
  (bool)
Model.hasVariable
bool hasVariable(string name)

Check whether the model contains a variable with a given name.

Parameters:
 name (string) – The variable name.
Return:
  (bool)
Model.numConstraints
long long numConstraints()

Return the number of constraints.

Return:
  (long long)
Model.numVariables
long long numVariables()

Return the number of variables.

Return:
  (long long)
Model.objective
void objective(string name, ObjectiveSense sense, Expression::t expr)
void objective(string name, ObjectiveSense sense, Variable::t v)
void objective(string name, ObjectiveSense sense, double c)
void objective(string name, double c)
void objective(ObjectiveSense sense, Expression::t expr)
void objective(ObjectiveSense sense, Variable::t v)
void objective(ObjectiveSense sense, double c)
void objective(double c)

Replace the objective expression. This method must be called at least once before the first Model.solve.

Parameters:
 
  • name (string) – Name of the objective. This may be any string, and it has no function except when writing the problem to an external file format.
  • sense (ObjectiveSense) – The objective sense. Defines whether the objective must be minimized or maximized.
  • expr (Expression) – The objective expression. This must be an expression that evaluates to a scalar.
  • v (Variable) – The objective variable. This must be a scalar variable.
  • c (double) – A constant scalar.
Model.primalObjValue
double primalObjValue()

Get the primal objective value in the current solution.

Return:
  (double)
Model.putlicensecode
void Model::putlicensecode(shared_ptr<ndarray<int,1>> code)

Set the license code in the global environment.

Parameters:
 code (int[])
Model.putlicensepath
void Model::putlicensepath(string licfile)

Set the license path in the global environment.

Parameters:
 licfile (string)
Model.putlicensewait
void Model::putlicensewait(bool wait)

Set the license wait flag in the global environment. If set, MOSEK will wait until a license becomes available.

Parameters:
 wait (bool)
Model.selectedSolution
void selectedSolution(SolutionType soltype)

Chooses a solution. The values of variables and constraints will be read from the chosen solution. The default is to consider all solution types in the order of SolutionType.Default.

Parameters:
 soltype (SolutionType)
Model.setCallbackHandler
void setCallbackHandler(System.CallbackHandler h)

Attach a progress callback handler. During optimization this handler will be called, providing a code with the current state of the solver. Passing NULL detaches the current handler. See Section Progress and data callback for details and examples and the Optimizer API for information about callback codes.

The progress callback handler is a function of type std::function<int(MSKcallbackcodee)>.

Parameters:
 h (CallbackHandler) – The callback handler or NULL.
Model.setDataCallbackHandler
void setDataCallbackHandler(System.DataCallbackHandler h)

Attach a data callback handler. During optimization this handler will be called, providing various information about the current state of the solution and solver. Passing NULL detaches the current handler. See Section Progress and data callback for details and examples and the Optimizer API for information about callback codes.

The data callback handler is a function of type std::function<bool(MSKcallbackcodee, const double *, const int32_t *, const int64_t *)>.

Parameters:
 h (DataCallbackHandler) – The callback handler or NULL.
Model.setLogHandler
void setLogHandler(System.StreamWriter h)

Attach a log handler. The solver log information will be sent to the stream handler. Passing NULL detaches the current handler.

The log handler is a function of type std::function<void(const std::string &)>, for example

M->setLogHandler([](const std::string & msg) { std::cout << msg << std::flush; } );
Parameters:
 h (StreamWriter) – The log handler object or NULL.
Model.setSolverParam
void setSolverParam(string name, string strval)
void setSolverParam(string name, int intval)
void setSolverParam(string name, double floatval)

Set a solver parameter. Solver parameter values can be either symbolic values, integers or doubles, depending on the parameter. The value is automatically converted to a suitable type whenever possible. If this fails, an exception will be thrown. For example, if the parameter accepts a double value and is given a string, the string will be parsed to produce a double.

See Section Parameters (alphabetical list sorted by type) for a listing of all parameter settings.

Parameters:
 
  • name (string) – Name of the parameter to set
  • strval (string) – A string value to assign to the parameter.
  • intval (int) – An integer value to assign to the parameter.
  • floatval (double) – A floating point value to assign to the parameter.
Model.solve
void solve()

This calls the MOSEK solver to solve the problem defined in the model.

If no error occurs, on exit a solution status will be defined for the primal and the dual solutions. These can be obtained with Model.getPrimalSolutionStatus and Model.getDualSolutionStatus. Depending on the solution status, various values may be defined:

  • If the model is primal-dual feasible, or nearly so, and the solver found a solution, the solution values can be accessed through the Variable and Constraint objects in the model. For integer problems only the primal solution is defined, while for continuous problems both primal and dual solutions are available.
  • If the model is primal or dual infeasible, only the primal or the dual solution is defined, depending on the solution status. The available solution contains a certificate of infeasibility.
  • If the status is unknown the solver ran into problems and did not find anything useful. In this case the solution values may be garbage.

The solution can be obtained with Model.primalObjValue and Variable.level and their dual analogues.

By default, trying to fetch a non-optimal solution using Variable.level or Variable.dual will cause an exception. To fetch infeasibility certificates or other less optimal solutions it is necessary to change the accepted solution flag with Model.acceptedSolutionStatus.

Model.variable
Variable::t variable(string name)
Variable::t variable(string name, int size)
Variable::t variable(string name, shared_ptr<ndarray<int,1>> sizea)
Variable::t variable(string name, int size, LinearDomain::t ldom)
Variable::t variable(string name, int size, RangeDomain::t rdom)
Variable::t variable(string name, int size, QConeDomain::t qdom)
Variable::t variable(string name, Set::t shp, LinearDomain::t ldom)
Variable::t variable(string name, Set::t shp, RangeDomain::t rdom)
Variable::t variable(string name, Set::t shp, QConeDomain::t qdom)
Variable::t variable(string name, shared_ptr<ndarray<int,1>> sizea, LinearDomain::t ldom)
Variable::t variable(string name, shared_ptr<ndarray<int,1>> sizea, RangeDomain::t rdom)
Variable::t variable(string name, LinearDomain::t ldom)
Variable::t variable(string name, RangeDomain::t rdom)
Variable::t variable(string name, QConeDomain::t qdom)
Variable::t variable()
Variable::t variable(int size)
Variable::t variable(shared_ptr<ndarray<int,1>> sizea)
Variable::t variable(int size, LinearDomain::t ldom)
Variable::t variable(int size, RangeDomain::t rdom)
Variable::t variable(int size, QConeDomain::t qdom)
Variable::t variable(Set::t shp, LinearDomain::t ldom)
Variable::t variable(Set::t shp, RangeDomain::t rdom)
Variable::t variable(Set::t shp, QConeDomain::t qdom)
Variable::t variable(shared_ptr<ndarray<int,1>> sizea, LinearDomain::t ldom)
Variable::t variable(shared_ptr<ndarray<int,1>> sizea, RangeDomain::t rdom)
Variable::t variable(LinearDomain::t ldom)
Variable::t variable(RangeDomain::t rdom)
Variable::t variable(QConeDomain::t qdom)
SymmetricVariable::t variable(string name, int size, SymmetricLinearDomain::t symdom)
SymmetricVariable::t variable(int size, SymmetricLinearDomain::t symdom)
Variable::t variable(string name, shared_ptr<ndarray<int,1>> sizea, PSDDomain::t psddom)
Variable::t variable(string name, Set::t shp, PSDDomain::t psddom)
Variable::t variable(string name, int n, PSDDomain::t psddom)
Variable::t variable(string name, int n, int m, PSDDomain::t psddom)
Variable::t variable(string name, PSDDomain::t psddom)
Variable::t variable(int n, PSDDomain::t psddom)
Variable::t variable(int n, int m, PSDDomain::t psddom)
Variable::t variable(PSDDomain::t psddom)
Variable::t variable(string name, shared_ptr<ndarray<int,1>> sizea, LinPSDDomain::t lpsddom)
Variable::t variable(string name, Set::t shp, LinPSDDomain::t lpsddom)
Variable::t variable(string name, int n, LinPSDDomain::t lpsddom)
Variable::t variable(string name, int n, int m, LinPSDDomain::t lpsddom)
Variable::t variable(string name, LinPSDDomain::t lpsddom)
Variable::t variable(int n, LinPSDDomain::t lpsddom)
Variable::t variable(int n, int m, LinPSDDomain::t lpsddom)
Variable::t variable(LinPSDDomain::t lpsddom)

Create a new variable in the model. All variables must be created using this method. The many versions of the method accept a name (optional), the shape of the variable and its domain. The domain must be suitable for the given variable shape. If the domain is not provided, it is assumed that the variable is unbounded. If the dimension is not provided the variable is a single scalar variable.

Typical domains used for creating variables include:

Parameters:
 
  • name (string) – Name of the variable. This must be unique among all variables in the model. The value NULL is allowed instead of a unique name.
  • size (int) – Size of the variable. The variable becomes a one-dimensional vector of the given size.
  • sizea (int[]) – Size of the variable. The variable becomes a multi-dimensional vector of the given size.
  • ldom (LinearDomain) – A linear domain for the variable.
  • rdom (RangeDomain) – A ranged domain for the variable.
  • qdom (QConeDomain) – A quadratic domain for the variable.
  • shp (Set) – Defines the shape of the variable.
  • symdom (SymmetricLinearDomain)
  • psddom (PSDDomain) – A semidefinte domain for the variable.
  • n (int) – Dimension of the semidefinite variable.
  • m (int) – Number of semidefinite variables.
  • lpsddom (LinPSDDomain) – A linear semidefinte domain for the variable.
Return:
 
Model.writeTask
void writeTask(string filename)

Dump the current solver task to a file. The file extension determines the file format, see Section Supported File Formats for details. The file can be read with the command line MOSEK or with the Optimizer API for debugging purposes.

Parameters:
 filename (string) – Name of the output file.