14.2.19 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 objectModel.dualObjValue
– Get the dual objective value in the current solution.Model.flushParameters
– Flush all parameters to the underlying task.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.getParameter
– Get the parameter matching the given name.Model.getPrimalSolutionStatus
– Return the status of the primal solution.Model.getProblemStatus
– Return the status of the problem.Model.getSolverDoubleInfo
– Fetch a solution information item from the solverModel.getSolverIntInfo
– Fetch a solution information item from the solverModel.getSolverLIntInfo
– Fetch a solution information item from the solverModel.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.hasParameter
– Check whether the model contains a parameter with a given name.Model.hasVariable
– Check whether the model contains a variable with a given name.Model.objective
– Replace the objective expression.Model.optserverHost
– Specify an OptServer for remote calls.Model.parameter
– Create a new parameter in the model.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 parameterModel.solve
– Attempt to optimize the model.Model.updateObjective
– Update part of the objective.Model.variable
– Create a new variable in the model.Model.writeTask
– Dump the current solver task to a file.Static members: Model.getVersion
– Return MOSEK version.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
¶ 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.Optimal
. It is necessary to change the accepted status to access sub-optimal solutions and infeasibility certificates.The methods
Model.getPrimalSolutionStatus
andModel.getDualSolutionStatus
can be used to get the actual status of the solutions.Parameters:
(what
AccSolutionStatus
) – The new accepted solution status.
-
Model.breakSolver
¶ 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.constraint
¶ constraint(str name, Expression expr, PSDDomain psddom) -> Constraint constraint(Expression expr, PSDDomain psddom) -> Constraint constraint(str name, Expression expr, LinPSDDomain lpsddom) -> Constraint constraint(Expression expr, LinPSDDomain lpsddom) -> Constraint constraint(str name, Expression expr, LinearDomain ldom) -> Constraint constraint(Expression expr, LinearDomain ldom) -> Constraint constraint(str name, Expression expr, RangeDomain rdom) -> RangedConstraint constraint(Expression expr, RangeDomain rdom) -> RangedConstraint constraint(str name, Expression expr, ConeDomain qdom) -> Constraint constraint(Expression expr, ConeDomain qdom) -> Constraint
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:
Domain.lessThan
,Domain.greaterThan
,Domain.inRange
,Domain.equalsTo
— puts linear bounds \(E\leq u\), \(l\leq E\), \(l\leq E\leq u\) or \(E=c\) on an expresion \(E\).Domain.inQCone
,Domain.inRotatedQCone
— constrains a vector or matrix expression \(E\) to a second-order cone.Domain.inPExpCone
,Domain.inPPowerCone
— constrains a vector or matrix expression \(E\) to an exponential or power cone.Domain.inPSDCone
— constrains a square matrix expression \(E\) to be positive semidefinite.
See
Domain
for a full list of domains.Parameters:
(name
str
) – 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.
(ldom
LinearDomain
) – A linear domain.
(rdom
RangeDomain
) – A ranged domain.
(qdom
ConeDomain
) – A domain in a cone.
Return:
-
Model.dispose
¶ 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
¶ dualObjValue() -> float
Get the dual objective value in the current solution.
Return: ( float
)
-
Model.flushParameters
¶ flushParameters()
Flush all parameters to the underlying task.
-
Model.flushSolutions
¶ flushSolutions()
If any solution values have been provided, flush those values to the underlying task.
-
Model.getAcceptedSolutionStatus
¶ getAcceptedSolutionStatus() -> AccSolutionStatus
Get the accepted solution status.
Return: ( AccSolutionStatus
)
-
Model.getConstraint
¶ getConstraint(str name) -> Constraint getConstraint(int index) -> Constraint
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
str
) – The constraint’s name.
(index
int
) – The constraint’s linear index.
Return:
-
Model.getDualSolutionStatus
¶ getDualSolutionStatus(SolutionType which) -> SolutionStatus getDualSolutionStatus() -> SolutionStatus
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
¶ getName() -> str
Return the model name, or an empty string if it has not been set.
Return: ( str
)
-
Model.getParameter
¶ getParameter(str name) -> Parameter
Get the parameter matching the given name.
Parameters:
(name
str
) – The parameter’s name.Return: ( Parameter
)
-
Model.getPrimalSolutionStatus
¶ getPrimalSolutionStatus(SolutionType which) -> SolutionStatus getPrimalSolutionStatus() -> SolutionStatus
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
¶ getProblemStatus(SolutionType which) -> ProblemStatus getProblemStatus() -> ProblemStatus
Return the status of the problem. 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.Return: ( ProblemStatus
)
-
Model.getSolverDoubleInfo
¶ getSolverDoubleInfo(str name) -> float
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
str
) – A string name of the information item.Return: ( float
)
-
Model.getSolverIntInfo
¶ getSolverIntInfo(str name) -> int
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
str
) – A string name of the information item.Return: ( int
)
-
Model.getSolverLIntInfo
¶ getSolverLIntInfo(str name) -> int
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
str
) – A string name of the information item.Return: ( int
)
-
Model.getTask
¶ getTask() -> mosek.Task
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
¶ getVariable(str name) -> Variable getVariable(int index) -> Variable
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
str
) – The variable’s name.
(index
int
) – The variable’s linear index.
Return: (
Variable
)
-
Model.getVersion
¶ Model.getVersion() -> str
Returns the MOSEK version as a string, for example “9.2.38”.
Return: ( str
)
-
Model.hasConstraint
¶ hasConstraint(str name) -> bool
Check whether the model contains a constraint with a given name.
Parameters:
(name
str
) – The constraint name.Return: ( bool
)
-
Model.hasParameter
¶ hasParameter(str name) -> bool
Check whether the model contains a parameter with a given name.
Parameters:
(name
str
) – The parameter’s name.Return: ( bool
)
-
Model.hasVariable
¶ hasVariable(str name) -> bool
Check whether the model contains a variable with a given name.
Parameters:
(name
str
) – The variable name.Return: ( bool
)
-
Model.objective
¶ objective(str name, ObjectiveSense sense, Expression expr) objective(str name, ObjectiveSense sense, float c) objective(str name, float c) objective(ObjectiveSense sense, Expression expr) objective(ObjectiveSense sense, float c) objective(float c)
Replace the objective expression. This method must be called at least once before the first
Model.solve
.Parameters:
(name
str
) – 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.
(c
float
) – A constant scalar.
-
Model.optserverHost
¶ optserverHost(str addr)
Specify an OptServer URL for remote calls. The URL should contain protocol, host and port in the form
http://server:port
orhttps://server:port
. If the URL is set using this function, all subsequent calls toModel.solve
will be sent to the specified OptServer instead of being executed locally. Passing NULL deactivates this redirection.Parameters:
(addr
str
) – Address of the OptServer. It should be a valid URL, for examplehttp://server:port
orhttps://server:port
.
-
Model.parameter
¶ parameter(int[] shape, int[][] sparsity) -> Parameter parameter(int[] shape, int[] sp) -> Parameter parameter(int[] shape) -> Parameter parameter(int d1) -> Parameter parameter(int d1, int d2) -> Parameter parameter(int d1, int d2, int d3) -> Parameter parameter() -> Parameter parameter(str name, int[] shape, int[][] sparsity) -> Parameter parameter(str name, int[] shape, int[] sp) -> Parameter parameter(str name, int[] shape) -> Parameter parameter(str name, int d1) -> Parameter parameter(str name, int d1, int d2) -> Parameter parameter(str name, int d1, int d2, int d3) -> Parameter parameter(str name) -> Parameter
Create a new parameter in the model. A parameter is a placeholder for a constant (scalar or array) value that can be assigned and reset after the model is built and between optimizations.
If the shape is not provided, the parameter is a scalar parameter. The default value of a newly created parameter is 0. To set the value use
Parameter.setValue
.Parameters:
(shape
int
[]) – Shape of the parameter.
(sparsity
int
[][]) – Non-zero sparsity pattern, if the parameter is sparse.
(sp
int
[]) – Non-zero sparsity pattern as a list of linear indexes, if the parameter is sparse.
(d1
int
) – First dimension of a parameter.
(d2
int
) – Second dimension of a parameter.
(d3
int
) – Third dimension of a parameter.
(name
str
) – Name of the parameter.
Return:
-
Model.primalObjValue
¶ primalObjValue() -> float
Get the primal objective value in the current solution.
Return: ( float
)
-
Model.putlicensecode
¶ Model.putlicensecode(int[] code)
Set the license code in the global environment.
Parameters:
(code
int
[])
-
Model.putlicensepath
¶ Model.putlicensepath(str licfile)
Set the license path in the global environment.
Parameters:
(licfile
str
)
-
Model.putlicensewait
¶ 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
¶ 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
) – The solution type to select as default.
-
Model.setCallbackHandler
¶ 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
(mosek.callback) -> int
.Parameters:
(h
CallbackHandler
) – The callback handler or NULL.
-
Model.setDataCallbackHandler
¶ 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
(mosek.callback, double[], int[], long[]) -> int
.Parameters:
(h
DataCallbackHandler
) – The callback handler or NULL.
-
Model.setLogHandler
¶ 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 must have a
write
method accepting a string, for exampleM.setLogHandler(sys.stdout)
Parameters:
(h
StreamWriter
) – The log handler object or NULL.
-
Model.setSolverParam
¶ setSolverParam(str name, str strval) setSolverParam(str name, int intval) setSolverParam(str name, float 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
str
) – Name of the parameter to set
(strval
str
) – A string value to assign to the parameter.
(intval
int
) – An integer value to assign to the parameter.
(floatval
float
) – A floating point value to assign to the parameter.
-
Model.solve
¶ solve() solve(str server, str port)
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
andModel.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
andConstraint
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
andVariable.level
and their dual analogues.By default, trying to fetch a non-optimal solution using
Variable.level
orVariable.dual
will cause an exception. To fetch infeasibility certificates or other less optimal solutions it is necessary to change the accepted solution flag withModel.acceptedSolutionStatus
.Parameters:
(server
str
) – Name or IP address of the solver server, if optimizing remotely with OptServer.
(port
str
) – Network port of the solver server, if optimizing remotely with OptServer.
- If the model is primal-dual feasible, or nearly so, and the solver found a solution, the solution
values can be accessed through the
-
Model.updateObjective
¶ updateObjective(Expression expr, Variable x)
Update the columns in the objective expression defined by
x
.Parameters:
(expr
Expression
) – The expression to update with. This must have size 1.
(x
Variable
) – The columns to replace.
-
Model.variable
¶ variable(str name) -> Variable variable(str name, int size) -> Variable variable(str name, int size, LinearDomain ldom) -> Variable variable(str name, int size, RangeDomain rdom) -> RangedVariable variable(str name, int size, ConeDomain qdom) -> Variable variable(str name, int[] shp) -> Variable variable(str name, int[] shp, LinearDomain ldom) -> Variable variable(str name, int[] shp, RangeDomain rdom) -> RangedVariable variable(str name, int[] shp, ConeDomain qdom) -> Variable variable(str name, LinearDomain ldom) -> Variable variable(str name, RangeDomain rdom) -> RangedVariable variable(str name, ConeDomain qdom) -> Variable variable() -> Variable variable(int size) -> Variable variable(int size, LinearDomain ldom) -> Variable variable(int size, RangeDomain rdom) -> RangedVariable variable(int size, ConeDomain qdom) -> Variable variable(int[] shp) -> Variable variable(int[] shp, LinearDomain ldom) -> Variable variable(int[] shp, RangeDomain rdom) -> RangedVariable variable(int[] shp, ConeDomain qdom) -> Variable variable(LinearDomain ldom) -> Variable variable(RangeDomain rdom) -> RangedVariable variable(ConeDomain qdom) -> Variable variable(str name, int[] shp, PSDDomain psddom) -> Variable variable(str name, int n, PSDDomain psddom) -> Variable variable(str name, int n, int m, PSDDomain psddom) -> Variable variable(str name, PSDDomain psddom) -> Variable variable(int n, PSDDomain psddom) -> Variable variable(int n, int m, PSDDomain psddom) -> Variable variable(PSDDomain psddom) -> Variable variable(str name, int[] shp, LinPSDDomain lpsddom) -> Variable variable(str name, int n, LinPSDDomain lpsddom) -> Variable variable(str name, int n, int m, LinPSDDomain lpsddom) -> Variable variable(str name, LinPSDDomain lpsddom) -> Variable variable(int n, LinPSDDomain lpsddom) -> Variable variable(int n, int m, LinPSDDomain lpsddom) -> Variable variable(LinPSDDomain lpsddom) -> Variable
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:
Domain.lessThan
,Domain.greaterThan
,Domain.inRange
— creates a variable \(x\) with bounds \(x\leq u\), \(l\leq x\) or \(l\leq x\leq u\).Domain.inPSDCone
— creates a symmetric positive definite variable of dimension \(n\).
Parameters:
(name
str
) – 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.
(ldom
LinearDomain
) – A linear domain for the variable.
(rdom
RangeDomain
) – A ranged domain for the variable.
(qdom
ConeDomain
) – A conic domain for the variable.
(shp
int
[]) – Defines the shape of the variable.
(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
¶ writeTask(str 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
str
) – Name of the output file.