13.2 Linear/Simplex Toolbox API¶
Reference for moseklinmodel
, the API for defining and solving linear problems. Most linear problems can be solved using the main entry point mosekmodel
. This part of the API is intended primarily for users who need to use the simplex algorithm and its associated features: basic solution and warm-start.
For tutorials with explanations and full code examples see Sec. 7 (Optimization Tutorials), and especially Sec. 7.7 (The linear/simplex interface tutorial).
13.2.1 Construction¶
- model = moseklinmodel(name, numvar, objsense, objective, objfixterm, A, b, blx, bux, solution_x, solution_y, solution_slx, solution_sux, solution_skc, solution_skx, varnames, connames)¶
Creates a model, which represents a linear problem. Most of the problem’s data can be set directly in this constructor; problem data can also be input and altered using dedicated methods later. The model object is the user’s access point to the problem and solution.
- Return:
model
(moseklinmodel) – The created linear model object.- Parameters:
name
(string) – (optional) Name of the model.numvar
(uint32) – (optional) Number of scalar variables initially in the model.objsense
(string) – (optional) Objective sense:"min"
,"minimize"
,"max"
,"maximize"
.objective
(double(:,1)) – (optional) Objective vector of length matchingnumvar
, if provided.objfixterm
(double) – (optional) Fixed term in the objective.A
(double(:,:)) – (optional) The linear constraint matrix.b
(double(:,1)) – (optional) The linear constraint right-hand side.blx
(double(:,1)) – (optional) Variable lower bounds.bux
(double(:,1)) – (optional) Variable upper bounds.solution_x
(double(:,1)) – (optional) Initialx
solution, primal solution values.solution_y
(double(:,1)) – (optional) Initialy
solution, dual values of linear constraints.solution_slx
(double(:,1)) – (optional) Initialslx
solution, dual values of variable lower bounds.solution_sux
(double(:,1)) – (optional) Initialsux
solution, dual values of variable upper bounds.solution_skc
(string(:,1)) – (optional) Initialskc
solution, status keys of constraints.solution_skx
(string(:,1)) – (optional) Initialskx
solution, status keys of variables.varnames
(string(:,1)) – (optional) Names of variables.connames
(string(:,1)) – (optional) Names of constraints.
Example:
model = moseklinmodel(... name = "lo1", ... numvar = 6, ... objsense = "maximize", ... objective = [ 3 1 5 1 0 0 ]', ... A = [ 3 1 2 0 0 0 ; ... 2 1 3 1 -1 0 ; ... 0 2 0 3 0 -1 ], ... b = [ 30 15 25 ]', ... blx = [ 0.0 0.0 0.0 0.0 0.0 -inf ]', ... bux = [ inf 10.0 inf inf inf 0.0 ]')
13.2.2 Adding data to the model¶
- moseklinmodel.appendcons(A, b, x, names)¶
Append a new constraint block of the form \(Ax=b\).
- Parameters:
A
(double(:,:)) – (required) Matrix block of the appended constraints.b
(double(:,1)) – (required) Right-hand side of the constraint block.size(b,1)==size(A,1)
must hold.x
(uint32(:,1)) – (optional) Indexes of variables. If provided, then it is assumed that the constraint blocks only involves the columns with indices inx
, andsize(A,2)==size(x,1)
must hold.names
(string) – (optional) Names of the constraint rows,size(names, 1)==size(A,1)
must hold.
Example:
model.appendcons([ 1 1 1 1 1 1 ], ... [10]', ... names = ["sum==10"]) model.appendcons(speye(6), ... ones(6, 1) * 3, ... names = "x_" + (1:6) + "_eq_3")
- moseklinmodel.objective(objsense, objective, objfixterm)¶
Adds or replaces the objective in the model.
- Parameters:
objsense
(string) – (required) Objective sense:"min"
,"minimize"
,"max"
,"maximize"
.objective
(double(:,1)) – (required) The objective vector. It’s length must match the number of variables in the model.objfixterm
(double(:,1)) – (optional) Fixed term in the objective;
Example:
model.objective("minimize", [4 2 0 1 1 0]') %model.objective("maximize", sparse([1], [2], [1.5], 1, 6), objfixterm = 10.0)
- moseklinmodel.setsolution(part, values)¶
Set an initial solution.
- Parameters:
part
(string) – (required) Indicates which part of the solution is requested:"x"
for the primal variable values,"y"
for dual constraint values,"slx"
,"sux"
for duals of variable bounds,"skc"
,"skx"
for constraint and variable status keys taken fromstakey
.values
(double(:,1)/string(,:1)) – (required) The vector of the initial solution, a double array for numerical values or a string array for status keys taken fromstakey
.
Example:
model.setsolution("x", [1.0 1.0 2.0 3.0 zeros(1,2)]') model.setsolution("skx", ["low" "bas" "bas"]')
- varindexes = moseklinmodel.appendvars(num, bl, bu, c, A, names)¶
Append
num
new variables.- Return:
varindexes
(uint32(:,1)) – Indexes of the newly added variables.- Parameters:
num
(double) – (required) The number of new variables.bl
(double(:,1)) – (optional) Lower bounds for the new variables.bu
(double(:,1)) – (optional) Upper bounds for the new variables.c
(double(:,1)) – (optional) The objective coefficients for the new variables.A
(double(:,:)) – (optional) Constraint matrix nonzeros for the new columns, appended horizontally to the current constraint matrix.names
(string(1,:)) – (optional) Names of the new variables.
Example:
model.appendvars(3, bl = [0, 0, 0], bu = [1, 1, 1], ... names = "tmp_" + (1:3)) model.appendvars(1, bl = [-1], bu = [1], ... A = sparse([2 zeros(1,9)]'), c = [1]', ... names = ["slack"])
13.2.3 Solving and obtaining the solution¶
- moseklinmodel.solve(write_to_file, logfile, nosolve, quiet, optserver, analyze, licfile, liccode, param)¶
Invokes the optimizer to solve the problem.
- Parameters:
write_to_file
(string) – (optional) Write the problem to this file before solving. The extension specifies the file format.logfile
(string) – (optional) Write log to this file.nosolve
(logical) – (optional) Format and load problem, but do not solve.quiet
(logical) – (optional) Do not print log.optserver
(string) – (optional) Host name for remote optimization.licfile
(string) – (optional) License file path. The same value must be used in all calls in the current process, otherwise the outcome is undefined.liccode
(char(1,:)) – (optional) License key as string. The same value must be used in all calls in the current process, otherwise the outcome is undefined.analyze
(logical) – (optional) Whether to run the problem analyzer after optimization.param
(string(1,:)) – (optional) Array of parameters in the form[name, value, name, value,...]
.
Example:
model.solve() model.solve(quiet = true) model.solve(quiet = true, param = ["MSK_DPAR_OPTIMIZER_MAX_TIME", "10.0"])
- exists, prosta, solsta = moseklinmodel.hassolution()¶
Check if the basic solution exists and return its statuses.
- Return:
exists
(logical) – Indicates if the basic solution is available.prosta
(string) – (optional) A string indicating the problem status for the basic solution, one ofprosta
. Only returned ifexists
is true.solsta
(string) – (optional) A string indicating the solution status for the basic solution, one ofsolsta
. Only returned ifexists
is true.
Example:
[exists, prosta, solsta] = model.hassolution()
- value, prosta, solsta = moseklinmodel.getsolution(part)¶
Return the requested solution values for the basic solution. Throws an error if the basic solution is not available; use
moseklinmodel.hassolution
to check solution availability.- Return:
value
(double(:,1)/string(:,1)) – The solution values requested. Returns a double array for numerical values and string array for status keys.prosta
(string) – A string indicating the problem status for the basic solution, one ofprosta
.solsta
(string) – A string indicating the solution status for the basic solution, one ofsolsta
.
- Parameters:
part
(string) – (required) Indicates which part of the solution is requested:"x"
for the primal variable values,"y"
for dual constraint values,"slx"
,"sux"
for duals of variable bounds,"skc"
,"skx"
for constraint and variable status keys taken fromstakey
.
Example:
x = model.getsolution("x") y = model.getsolution("y") skc = model.getsolution("skc")
13.2.4 Modifying the model¶
- moseklinmodel.setb(b, first)¶
Update the constraint right-hand side vector
b
or its slice.- Parameters:
b
(double(:,1)) – (required) New sequence of values.first
(int32) – (optional) The first index of the slice to be updated. Default equals to 1.
Example:
model.setb([3, 4, 5]', first = 2)
- moseklinmodel.setvarbounds(bl, bu, first)¶
Update the variable bounds for all variables or their slice.
- Parameters:
bl
(double(:,1)) – (optional) New sequence of lower bounds.bu
(double(:,1)) – (optional) New sequence of upper bounds.first
(int32) – (optional) The first index of the slice to be updated. Default equals to 1.
Example:
model.setvarbounds(bl = [5], bu = [6], first = 5)
- moseklinmodel.setrows(A, first)¶
Update a slice of rows in the constraint matrix
A
.- Parameters:
A
(double(:,:)) – (required) A matrix of new row values. The second dimensionsize(A,2)
must equal the number of variables in the problem.first
(int32) – (optional) The first row index of the slice to be updated. Default equals to 1.
Example:
model.setrows(zeros(2, model.getnumvar())) model.setrows(ones(1, model.getnumvar()), first = 3)
- moseklinmodel.setcolumns(A, first)¶
Update a slice of columns in the constraint matrix
A
.- Parameters:
A
(double(:,:)) – (required) A matrix of new column values. The first dimensionsize(A,1)
must equal the number of constraints in the problem.first
(int32) – (optional) The first column index of the slice to be updated. Default equals to 1.
Example:
model.setcolumns(zeros(model.getnumcon(), 3)) model.setcolumns(ones(model.getnumcon(), 1), first = 3)
- moseklinmodel.setc(c, first)¶
Update the objective vector or its slice.
- Parameters:
c
(double(:,1)) – (required) New sequence of values.first
(int32) – (optional) The first index of the slice to be updated. Default equals to 1.
Example:
model.setc(zeros(6, 1), first = 3)
13.2.5 Auxiliary functions¶
- moseklinmodel.write(filename, param)¶
Writes the current problem to a file.
- Parameters:
filename
(string) – (required) File name to write the problem to. The format is determined by the extension.param
(string(1,:)) – (optional) Array of parameters in the form[name, value, name, value,...]
.
Example:
model.write("dump.task.gz") model.write("save.ptf", param = ["MSK_IPAR_PTF_WRITE_SOLUTIONS", "MSK_ON"])
13.2.6 Names¶
- moseklinmodel.varname(indexes, names)¶
Adds variable names to the model.
- Parameters:
indexes
(uint32(:,1)) – (required) Indexes of the variables to name.names
(string(:,1)) – (required) Names to assign.
Example:
model.varname(2:4, ["b", "tmp1", "tmp2"])
- moseklinmodel.conname(indexes, names)¶
Adds constraint names to the model.
- Parameters:
indexes
(uint32(:,1)) – (required) Indexes of the constraints to name.names
(string(:,1)) – (required) Names to assign.
Example:
model.conname(1:2, ["cons1", "cons2"])