14.2.22 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.DataReport – Print out a report about problem data.
Model.Disjunction – Create a new disjunctive constraint in the model.
Model.Dispose – Destroy the Model object
Model.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 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.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 parameter
Model.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.
Model.WriteTaskStream – Write the current solver task to a stream.
- 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.SolveBatch – Attempt to optimize a collection of models in parallel.
- 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.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¶
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.Constraint¶
Constraint Constraint(string name, Expression expr, PSDDomain psddom) Constraint Constraint(Expression expr, PSDDomain psddom) Constraint Constraint(string name, Expression expr, LinearDomain ldom) Constraint Constraint(Expression expr, LinearDomain ldom) Constraint Constraint(string name, Expression expr, ConeDomain qdom) Constraint Constraint(Expression expr, ConeDomain qdom) RangedConstraint Constraint(string name, Expression expr, RangeDomain rdom) RangedConstraint Constraint(Expression expr, RangeDomain rdom)
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
(string
) – Name of the constraint. This must be unique among all constraints in the model. The valuenull
is allowed instead of a unique name.expr
(Expression
) – An expression.psddom
(PSDDomain
) – A positive semidefinte domain.ldom
(LinearDomain
) – A linear domain.qdom
(ConeDomain
) – A domain in a cone.rdom
(RangeDomain
) – A ranged domain.
- Return
- Model.DataReport¶
void DataReport(int detail) void DataReport()
Prints out a text report about the dimensions and numerical properties of the problem data. The report is printed to the currently attached log stream. The report is generated for the internal low-level optimizer task as a rough guidance for the user; some care may be needed to relate it correctly to the Fusion model.
- Parameters
detail
(int
) – The amount of detail to include, where 0 means nothing and 10 is full. If not provided then a full report is printed.
- Model.Disjunction¶
Disjunction Disjunction(string name, Term t1) Disjunction Disjunction(string name, Term t1, Term t2) Disjunction Disjunction(string name, Term t1, Term t2, Term t3) Disjunction Disjunction(Term t1) Disjunction Disjunction(Term t1, Term t2) Disjunction Disjunction(Term t1, Term t2, Term t3) Disjunction Disjunction(Term[] terms) Disjunction Disjunction(string name, Term[] terms)
Adds a new disjunctive constraint to the model. A disjunctive constraint with terms \(T_1,\ldots,T_n\) is:
\[T_1 \ \mathrm{OR}\ \cdots\ \mathrm{OR}\ T_n.\]Each term \(T_i\) of a disjunctive constraint is a
Term
, which can be created withDJC.Term
or withDJC.AND
.- Parameters
- Return
- 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.FlushParameters¶
void FlushParameters()
Flush all parameters to the underlying task.
- 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
- Model.GetConstraint¶
Constraint GetConstraint(string name) Constraint 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
- 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
- Model.GetName¶
string GetName()
Return the model name, or an empty string if it has not been set.
- Return
(
string
)
- Model.GetParameter¶
Parameter GetParameter(string name)
Get the parameter matching the given name.
- Parameters
name
(string
) – The parameter’s name.- Return
- 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
- Model.GetProblemStatus¶
ProblemStatus GetProblemStatus(SolutionType which) ProblemStatus GetProblemStatus()
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
- 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 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
)
- 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 GetVariable(string name) Variable 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.GetVersion¶
string Model.GetVersion()
Returns the MOSEK version as a string, for example “10.2.12”.
- Return
(
string
)
- 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.HasParameter¶
bool HasParameter(string name)
Check whether the model contains a parameter with a given name.
- Parameters
name
(string
) – The parameter’s 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.Objective¶
void Objective(string name, ObjectiveSense sense, Expression expr) void Objective(string name, ObjectiveSense sense, double c) void Objective(string name, double c) void Objective(ObjectiveSense sense, Expression expr) 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 affine expression that evaluates to a scalar.c
(double
) – A constant scalar.
- Model.OptserverHost¶
void OptserverHost(string 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
(string
) – Address of the OptServer. It should be a valid URL, for examplehttp://server:port
orhttps://server:port
.
- Model.Parameter¶
Parameter Parameter(int[] shape, int[,] sparsity) Parameter Parameter(int[] shape, long[] 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(string name, int[] shape, int[,] sparsity) Parameter Parameter(string name, int[] shape, long[] sp) Parameter Parameter(string name, int[] shape) Parameter Parameter(string name, int d1) Parameter Parameter(string name, int d1, int d2) Parameter Parameter(string name, int d1, int d2, int d3) Parameter Parameter(string name)
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
(long
[]) – 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
(string
) – Name of the parameter.
- Return
- Model.PrimalObjValue¶
double PrimalObjValue()
Get the primal objective value in the current solution.
- Return
(
double
)
- Model.Putlicensecode¶
void Model.Putlicensecode(int[] code)
Set the license code in the global environment. This function has an effect only before the first optimization in the current process.
- Parameters
code
(int
[])
- Model.Putlicensepath¶
void Model.Putlicensepath(string licfile)
Set the license path in the global environment. This function has an effect only before the first optimization in the current process.
- 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
) – The solution type to select as default.
- 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 an object of type
mosek.ProgressCallback
.- Parameters
h
(CallbackHandler
) – The callback handler ornull
.
- 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 an object of type
mosek.DataCallback
.- Parameters
h
(DataCallbackHandler
) – The callback handler ornull
.
- 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 an object of type
System.IO.TextWriter
, for exampleM.SetLogHandler(Console.Out)
- Parameters
h
(StreamWriter
) – The log handler object ornull
.
- 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 setstrval
(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() void Solve(string addr, string accesstoken)
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
addr
(string
) – Address of the OptServer if optimizing remotely. It should be a valid URL, for examplehttp://server:port
orhttps://server:port
.accesstoken
(string
) – Access token if optimizing remotely with authentication.
- Model.SolveBatch¶
SolverStatus[] Model.SolveBatch(bool israce, double maxtime, int numthreads, Model[] models)
Calls the MOSEK solver to solve all the provided models in parallel. All callbacks and log output streams are disabled.
Assuming that each model takes about same time and there many more models than threads then a linear speedup can be achieved, also known as strong scaling. A typical application of this method is to solve many small models of similar type; in this case it is recommended that each of them is allocated a single thread by setting
numThreads
to \(1\).If the parameters
israce
ormaxtime
are used, then the result may not be deterministic, in the sense that the models which complete first may vary between runs.The remaining behavior is the same as if each model was solved separately with
Model.Solve
. The return array describes if each model solved, failed or wasn’t attempted because another one finished first (ifisrace
is used). Debugging a failed model can be done by solving it individually.This method parallelizes the call to the numerical solver; additional operations such as postprocessing the optimizer solution into the solution of the Fusion model are still performed sequentially.
- Parameters
israce
(bool
) – If true, then the function is terminated after the first model completed.maxtime
(double
) – Time limit: if nonnegative, then the function is terminated after this time (seconds).numthreads
(int
) – The number of threads for the whole pool available to all models. If set to 0 the number of threads used will be equal to the number of cores detected on the machine.models
(Model
[]) – An array of models to be solved.
- Return
(
SolverStatus
[])
- Model.UpdateObjective¶
void 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 Variable(string name) Variable Variable(string name, int size) Variable Variable(string name, int size, LinearDomain ldom) RangedVariable Variable(string name, int size, RangeDomain rdom) Variable Variable(string name, int size, ConeDomain qdom) Variable Variable(string name, int[] shp) Variable Variable(string name, int[] shp, LinearDomain ldom) RangedVariable Variable(string name, int[] shp, RangeDomain rdom) Variable Variable(string name, int[] shp, ConeDomain qdom) Variable Variable(string name, LinearDomain ldom) RangedVariable Variable(string name, RangeDomain rdom) Variable Variable(string name, ConeDomain qdom) Variable Variable() Variable Variable(int size) Variable Variable(int size, LinearDomain ldom) RangedVariable Variable(int size, RangeDomain rdom) Variable Variable(int size, ConeDomain qdom) Variable Variable(int[] shp) Variable Variable(int[] shp, LinearDomain ldom) RangedVariable Variable(int[] shp, RangeDomain rdom) Variable Variable(int[] shp, ConeDomain qdom) Variable Variable(LinearDomain ldom) RangedVariable Variable(RangeDomain rdom) Variable Variable(ConeDomain qdom) Variable Variable(string name, int[] shp, PSDDomain psddom) Variable Variable(string name, int n, PSDDomain psddom) Variable Variable(string name, int n, int m, PSDDomain psddom) Variable Variable(string name, PSDDomain psddom) Variable Variable(int n, PSDDomain psddom) Variable Variable(int n, int m, PSDDomain psddom) Variable Variable(PSDDomain psddom)
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
(string
) – Name of the variable. This must be unique among all variables in the model. The valuenull
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.
- 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.
- Model.WriteTaskStream¶
void WriteTaskStream(string ext, System.DataStream stream)
Write the current solver task to a stream object. The extension determines the file format, see Section Supported File Formats, and it should be a string such as
"ptf"
,"task.gz"
, etc. that would be used for file extension inModel.WriteTask
.stream
is an object of typeSystem.IO.BinaryWriter
, for exampleM.WriteTaskStream("jtask", new BinaryWriter(Console.OpenStandardOutput())); using (var outfile = File.Open("dump.ptf", FileMode.Create)) using (var stream = new BinaryWriter(outfile)) M.WriteTaskStream("ptf", stream);
- Parameters
ext
(string
) – Extension, which determines the file format.stream
(DataStream
) – The output stream.