8.2.7. MdoModel¶
-
class mindopt::MdoModel¶
Object to define an optimization model.
This object implements the data structure to hold an optimization model.
Typical steps to to input a column into the optimization model are:
Create a set of empty variable objects by calling
MdoModel::addVar()
.Specify the variable’s lower bound, upper bound, and objective coefficients.
Create an empty linear expression object by calling
MdoExprLinear::MdoExprLinear()
.Input a constraint by calling
MdoModel::addCons()
.Optimize the problem by calling
MdoModel::solveProb()
.
Examples
/* Create an empty model. */ MdoModel model; MdoI32 itmp = 0; MdoReal dtmp = 0.0; std::string ctmp; model.setDebug(MDO_YES); btmp = model.isDebug(); /* Change to minimization problem. */ model.setIntAttr("MinSense", MDO_YES); /* Add empty constraints. */ std::vector<std::reference_wrapper<MdoCons> > cons; cons.push_back(model.addCons(1.0, 1.0, "c0")); cons.push_back(model.addCons(1.0, MDO_INFINITY, "c1")); /* Input columns. */ std::vector<MdoCol> col(4); col[0].addTerm(cons[0], 1.0); col[0].addTerm(cons[1], 1.0); col[1].addTerm(cons[0], 1.0); col[2].addTerm(cons[0], 2.0); col[2].addTerm(cons[1], -1.0); col[3].addTerm(cons[0], 3.0); col[3].addTerm(cons[1], 6.0); /* Add variables. */ std::vector<std::reference_wrapper<MdoVar> > x; x.push_back(model.addVar(0.0, 10.0, 1.0, col[0], "x0", MDO_NO)); x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, col[1], "x1", MDO_NO)); x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, col[2], "x2", MDO_NO)); x.push_back(model.addVar(0.0, MDO_INFINITY, 1.0, col[3], "x3", MDO_NO)); /* Set obj offset. */ model.setObjOffset(10.0); /* Solve the problem and check the result. */ model.solveProb(); model.displayResults(); /* Write problem and solution */ model.writeProb(MY_FOLDER "model.lp"); model.writeSoln(MY_FOLDER "model.bas"); model.writeSoln(MY_FOLDER "model.sol"); /* Check Solution */ status = model.getStatus(); std::string status_reason = model.explainStatus(status); dtmp = model.getRealAttr("PrimalObjVal"); dtmp = model.getRealAttr("DualObjVal"); itmp = model.getIntAttr("HasSolution"); itmp = model.getIntAttr("HasPrimalRay"); dtmp = model.getRealAttrIndex("PrimalSoln", 3); dtmp = model.getRealAttr("SolutionTime"); std::vector<double> primal_soln_array = model.getRealAttrArray("PrimalSoln", 0, 4); dtmp = x[3].get().getRealAttr("PrimalSoln"); dtmp = model.getRealAttrIndex("DualSoln", 0); std::vector<double> dual_soln_array = model.getRealAttrArray("DualSoln", 0, 2); dtmp = cons[0].get().getRealAttr("DualSoln"); itmp = model.getIntAttr("SPX/NumIters"); itmp = model.getIntAttr("IPM/NumIters"); dtmp = model.getRealAttrIndex("Activity", 0); std::vector<double> activity_array = model.getRealAttrArray("Activity", 0, 2); dtmp = cons[1].get().getRealAttr("Activity"); dtmp = model.getRealAttrIndex("ReducedCost", 0); std::vector<double> reduced_cost_array = model.getRealAttrArray("ReducedCost", 0, 4); dtmp = x[2].get().getRealAttr("ReducedCost"); std::vector<double> reduced_cost_array_2 = model.getRealAttrVars("ReducedCost", x); /* Basis */ std::vector<int> col_basis; std::vector<int> row_basis; for (auto col : x) { col_basis.push_back(col.get().getIntAttr("ColBasis")); } for (auto row : cons) { row_basis.push_back(row.get().getIntAttr("RowBasis")); } std::vector<int> col_basis_array = model.getIntAttrArray("ColBasis", 0, 4); std::vector<int> row_basis_array = model.getIntAttrArray("RowBasis", 0, 2); std::vector<std::reference_wrapper<MdoVar> > col_vec; std::vector<std::reference_wrapper<MdoCons> > row_vec; col_vec.push_back(model.getVar(0)); col_vec.push_back(model.getVar(1)); row_vec.push_back(model.getCons(0)); row_vec.push_back(model.getCons(1)); const int col_basis_chk_2[] = {1, 1}; const int row_basis_chk_2[] = {1, 2}; model.setIntAttrVars("ColBasis", col_vec, col_basis_chk_2); std::vector<int> col_basis_vec = model.getIntAttrVars("ColBasis", col_vec); model.setIntAttrConss("RowBasis", row_vec, row_basis_chk_2); std::vector<int> row_basis_vec = model.getIntAttrConss("RowBasis", row_vec); /* Change the model */ std::vector<double> objs = model.getObjs(x); model.setObjs(x, objs); model.setMaxObjSense(); btmp = model.isMaxObjSense(); model.setMinObjSense(); btmp = model.isMinObjSense(); model.setObjOffset(10.0); dtmp = model.getObjOffset(); const double col_ub_chk_2[] = {1.0, 2.0}; const double row_rhs_chk_2[] = {3.0, 4.0}; model.setRealAttrVars("UB", col_vec, col_ub_chk_2); std::vector<double> col_ub_vec = model.getRealAttrVars("UB", col_vec); model.setRealAttrConss("RHS", row_vec, row_rhs_chk_2); std::vector<double> row_rhs_vec = model.getRealAttrConss("RHS", row_vec); /* Model Attributes */ model.setIntAttr("MinSense", 1); itmp = model.getIntAttr("MinSense"); model.setRealAttr("ObjConst", 1.0); dtmp = model.getRealAttr("ObjConst"); itmp = model.getIntAttr("NumVars"); itmp = model.getIntAttr("NumConss"); itmp = model.getIntAttr("NumEnts"); model.setRealAttrIndex("LB", 0, -10.0); dtmp = model.getRealAttrIndex("LB", 0); double lb_array_chk[4] = {-1, -2, -3, -4}; model.setRealAttrArray("LB", 0, 4, lb_array_chk); std::vector<double> lb_array = model.getRealAttrArray("LB", 0, 4); x[3].get().setRealAttr("LB", -10.0); dtmp = x[3].get().getRealAttr("LB"); model.setRealAttrIndex("UB", 0, +10.0); dtmp = model.getRealAttrIndex("UB", 0); double ub_array_chk[4] = {1, 2, 3, 4}; model.setRealAttrArray("UB", 0, 4, ub_array_chk); std::vector<double> ub_array = model.getRealAttrArray("UB", 0, 4); x[3].get().setRealAttr("UB", +10.0); dtmp = x[3].get().getRealAttr("UB"); model.setRealAttrIndex("Obj", 0, +100.0); dtmp = model.getRealAttrIndex("Obj", 0); double obj_array_chk[4] = {100, 200, 300, 400}; model.setRealAttrArray("Obj", 0, 4, obj_array_chk); std::vector<double> obj_array = model.getRealAttrArray("Obj", 0, 4); x[2].get().setRealAttr("Obj", +100); dtmp = x[2].get().getRealAttr("Obj"); model.setRealAttrIndex("LHS", 0, -10.0); dtmp = model.getRealAttrIndex("LHS", 0); double lhs_array_chk[2] = {-1, -2}; model.setRealAttrArray("LHS", 0, 2, lhs_array_chk); std::vector<double> lhs_array = model.getRealAttrArray("LHS", 0, 2); cons[1].get().setRealAttr("LHS", -5.0); dtmp = cons[1].get().getRealAttr("LHS"); model.setRealAttrIndex("RHS", 0, +10.0); dtmp = model.getRealAttrIndex("RHS", 0); double rhs_array_chk[2] = {1, 2}; model.setRealAttrArray("RHS", 0, 2, rhs_array_chk); std::vector<double> rhs_array = model.getRealAttrArray("RHS", 0, 2); cons[1].get().setRealAttr("RHS", 5.0); dtmp = cons[1].get().getRealAttr("RHS"); std::string col_name = "col_name"; model.setStrAttrIndex("ColName", 0, col_name); ctmp = model.getStrAttrIndex("ColName", 0); col_name = "col_name2"; x[1].get().setStrAttr("ColName", col_name); ctmp = x[1].get().getStrAttr("ColName"); std::string row_name = "row_name"; model.setStrAttrIndex("RowName", 0, row_name); ctmp = model.getStrAttrIndex("RowName", 0); row_name = "row_name2"; cons[1].get().setStrAttr("RowName", row_name); ctmp = cons[1].get().getStrAttr("RowName"); model.setIntAttrIndex("IsInteger", 0, 1); itmp = model.getIntAttrIndex("IsInteger", 0); int is_integer_chk[4] = {1, 1, 1, 1}; model.setIntAttrArray("IsInteger", 0, 4, is_integer_chk); std::vector<int> is_integer_array = model.getIntAttrArray("IsInteger", 0, 4); x[1].get().setIntAttr("IsInteger", 0); itmp = x[1].get().getIntAttr("IsInteger"); /* Change elements */ std::vector<std::reference_wrapper<MdoCons> > row_elements; std::vector<std::reference_wrapper<MdoVar> > col_elements; std::vector<double> value_elements; int row_indices[] = { 0, 0, 0, 0, 1, 1, 1 }; int col_indices[] = { 0, 1, 2, 3, 0, 2, 3 }; const double elements_value[] = { 1.0, 2.0, 3.0, 4.0, -1.0, -2.0, -3.0 }; for (int i = 0 ; i < 7; i++ ) { row_elements.push_back(model.getCons(row_indices[i])); col_elements.push_back(model.getVar(col_indices[i])); value_elements.push_back(elements_value[i]); } model.setElements(row_elements, col_elements, value_elements); std::vector<double> elements_chk = model.getElements(row_elements, col_elements); /* Model deletions */ itmp = model.getIntAttr("NumEnts"); std::cout << "NumEnts:" << itmp << std::endl; std::vector<std::reference_wrapper<MdoCons> > cons_del; std::vector<std::reference_wrapper<MdoVar> > vars_del; cons_del.push_back(model.getCons(0)); vars_del.push_back(x[0].get()); model.deleteElements(cons_del, vars_del); itmp = model.getIntAttr("NumEnts"); std::cout << "NumEnts:" << itmp << std::endl; model.deleteConss(cons); itmp = model.getIntAttr("NumConss"); std::cout << "NumConss:" << itmp << std::endl; model.deleteVars(x); itmp = model.getIntAttr("NumVars"); std::cout << "NumVars:" << itmp << std::endl;
Constructor / destructor
-
MdoModel(void)¶
Default constructor.
Constructor with argument.
-
~MdoModel(void)¶
Destructor.
Basic helpers
-
void freeData(int type = 0)¶
Clean up all internal data.
- Parameters
type – [i] 0: default (both); 1: row only; 2: column only.
-
void loadData(int type = 0)¶
Load to the internal data.
- Parameters
type – [i] 0: default (both); 1: row only; 2: column only.
-
void syncData(int type = 0)¶
Sync the internal data.
- Parameters
type – [i] 0: default (both); 1: row only; 2: column only.
-
MdoModel &operator=(const MdoModel &rhs)¶
Assignment operator.
- Parameters
rhs – Object to copy.
- Return
Updated object.
Methods to create/delete variable(s) or constraint(s).
-
MdoVar &addVar(MdoReal lb, MdoReal ub, MdoReal obj, std::string name = "", bool is_integer = false)¶
This function adds a new variable (column) to the model.
- Parameters
lb – Lower bounded value.
ub – Upper bounded value.
obj – Objective coefficient.
name – Variable name, can be empty.
is_integer – Flag that specifies if this is an integer variable.
- Return
A new variable object.
-
MdoVar &addVar(MdoReal lb, MdoReal ub, MdoReal obj, const MdoCol &col, std::string name = "", bool is_integer = false)¶
This function adds a new variable (column) to the model.
- Parameters
lb – Lower bounded value.
ub – Upper bounded value.
obj – Objective coefficient.
col – Column object that holds the nonzero elements.
name – Variable name, can be empty.
is_integer – Flag that specifies if this is an integer variable.
- Return
A new variable object.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const bool *are_integers, const std::string *names)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.names – An array of variable names. If
NULL
, then default names will be used.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const bool *are_integers, std::string name_prefix)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.name_prefix – Prefix part for the variable names. If empty, then default names will be used.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, MdoReallb, MdoReal ub, MdoReal obj, const std::string *names, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lb – Value for the lower bound of all new variables.
ub – Value for the upper bound of all new variables.
obj – Value for the objective coefficient of all new variables.
names – An array of variable names. If
NULL
, then default names will be used.are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, MdoReal lb, MdoReal ub, MdoReal obj, std::string name_prefix, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lb – Value for the lower bound of all new variables.
ub – Value for the upper bound of all new variables.
obj – Value for the objective coefficient of all new variables.
name_prefix – Prefix part for the variable names. If empty, then default names will be used.
are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const MdoReal *lbs, const MdoReal *ubs, const MdoReal *objs, const std::string *names, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lbs – An array of lower bounds for the new variables. If
NULL
, then a0.0
value will be used for all lower bounds.ubs – An array of upper bounds. If
NULL
, then an infinity value will be used for all upper bounds.objs – An array of objective coefficients. If
NULL
, then a0.0
value will be used for all objective coefficients.names – An array of variable names. If
NULL
, then default names will be used.are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const MdoReal *lbs, const MdoReal *ubs, const MdoReal *objs, std::string name_prefix, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lbs – An array of lower bounds for the new variables. If
NULL
, then a0.0
value will be used for all lower bounds.ubs – An array of upper bounds. If
NULL
, then an infinity value will be used for all upper bounds.objs – An array of objective coefficients. If
NULL
, then a0.0
value will be used for all objective coefficients.name_prefix – Prefix part for the variable names. If empty, then default names will be used.
are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const MdoReal *lbs, const MdoReal *ubs, const MdoReal *objs, const MdoCol *cols, const std::string *names, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lbs – An array of lower bounds for the new variables. If
NULL
, then a0.0
value will be used for all lower bounds.ubs – An array of upper bounds. If
NULL
, then an infinity value will be used for all upper bounds.objs – An array of objective coefficients. If
NULL
, then a0.0
value will be used for all objective coefficients.cols – An array of column objects that holds the nonzero elements. If
NULL
, then all added columns will be treated as empty.names – An array of variable names. If
NULL
, then default names will be used.are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
std::vector<std::reference_wrapper<MdoVar>> addVars(MdoI32 num_vars, const MdoReal *lbs, const MdoReal *ubs, const MdoReal *objs, const MdoCol *cols, std::string name_prefix, const bool *are_integers)¶
This function adds a set of new variables (columns) to the model.
- Parameters
num_vars – Number of variables to add.
lbs – An array of lower bounds for the new variables. If
NULL
, then a0.0
value will be used for all lower bounds.ubs – An array of upper bounds. If
NULL
, then an infinity value will be used for all upper bounds.objs – An array of objective coefficients. If
NULL
, then a 0.0 value will be used for all objective coefficients.cols – An array of column objects that holds the nonzero elements. If
NULL
, then all added columns will be treated as empty.name_prefix – Prefix part for the variable names. If empty, then default names will be used.
are_integers – An array of flags that specifies the variable types. If
NULL
, then all variables will be treated as continuous variables.
- Return
A vector object that will hold the references to the variable objects.
-
MdoCons &addCons(MdoReal lhs, MdoReal rhs, std::string name = "")¶
This function adds a new linear constraint to the model.
- Parameters
lhs – Left-hand-side value.
rhs – Right-hand-side value.
name – Variable name, can be empty.
- Return
A new constraint object.
-
MdoCons &addCons(MdoReal lhs, MdoReal rhs, const MdoExprLinear &expr, std::string name = "")¶
This function adds a new linear constraint to the model.
- Parameters
lhs – Left-hand-side value.
rhs – Right-hand-side value.
expr – Expression object that holds the new linear constraint.
name – Variable name, can be empty.
- Return
A new constraint object.
-
MdoCons &addCons(const MdoTempLinear &temp, std::string name = "")¶
This function adds a new linear constraint to the model.
- Parameters
temp – A temporary object that holds the new linear constraint.
name – Variable name, can be empty.
- Return
A new constraint object.
-
std::vector<std::reference_wrapper<MdoCons>> addConss(MdoI32 num_conss, const MdoReal *lhss, const MdoReal *rhss, const std::string *names)¶
This function adds a set of new linear constraints to the model.
- Parameters
num_conss – Number of constraints to add.
lhss – An array of left-hand-side values.
rhss – An array of right-hand-side values.
names – An array of constraint names. If
NULL
, then default names will be used.
- Return
A vector object that will hold the references to the constraint objects.
-
std::vector<std::reference_wrapper<MdoCons>> addConss(MdoI32 num_conss, const MdoReal *lhss, const MdoReal *rhss, std::string name_prefix)¶
This function adds a set of new linear constraints to the model.
- Parameters
num_conss – Number of constraints to add.
lhss – An array of left-hand-side values.
rhss – An array of right-hand-side values.
name_prefix – Prefix part for the constraint names. If empty, then default names will be used.
- Return
A vector object that will hold the references to the constraint objects.
-
std::vector<std::reference_wrapper<MdoCons>> addConss(MdoI32 num_conss, const MdoReal *lhss, const MdoReal *rhss, const MdoExprLinear *exprs, const std::string *names)¶
This function adds a set of new linear constraints to the model.
- Parameters
num_conss – Number of constraints to add.
lhss – An array of left-hand-side values.
rhss – An array of right-hand-side values.
exprs – An array of expression objects that holds the new linear constraint. If
NULL
, then all added constraints will be treated as empty.names – An array of constraint names. If
NULL
, then default names will be used.
- Return
A vector object that will hold the references to the constraint objects.
-
std::vector<std::reference_wrapper<MdoCons>> addConss(MdoI32 num_conss, const MdoReal *lhss, const MdoReal *rhss, const MdoExprLinear *exprs, std::string name_prefix)¶
This function adds a set of new linear constraints to the model.
- Parameters
num_conss – Number of constraints to add.
lhss – An array of left-hand-side values.
rhss – An array of right-hand-side values.
exprs – An array of expression objects that holds the new linear constraint. If
NULL
, then all added constraints will be treated as empty.name_prefix – Prefix part for the constraint names. If empty, then default names will be used.
- Return
A vector object that will hold the references to the constraint objects.
-
void deleteVars(std::vector<std::reference_wrapper<MdoVar>> &vars)¶
This function removes a set of columns from the model.
- Parameters
vars – A vector object that holds the references to the variables.
-
void deleteConss(std::vector<std::reference_wrapper<MdoCons>> &conss)¶
This function removes a set of rows from the model.
- Parameters
conss – A vector object that holds the references to the constraints.
- Return
A response code that specifies the status of the function.
Methods related to SDP.
Add a block variable (symmetric positive semidefinite matrix) to the model.
- Param dim_mat
Dimension of the added block variable.
- Param mat_name
Name of the added block variable. Can be
NULL
.
-
void addSymMats(MdoI32 num_mats, const MdoI32 *dim_mats, const std::string *mat_names = NULL)¶
Add multiple block variables to the model.
- Param num_mats
Number of block variables to be added.
- Param dim_mats
An integer array that holds the dimension of the block variables.
- Param mat_names
An array to hold the matrix variable names. If
NULL
,then default names will be used.
-
void replaceSymMatObjs(const MdoI32 mat_index, MdoI32 size, const MdoI32 *mat_row_indices, const MdoI32 *mat_col_indices, const MdoReal *mat_values)¶
Replace the values of the specified block variable in the objective function.
- Param mat_index
Block variable index.
- Param size
Number of elements of the block variable to access.
- Param mat_row_indices
An array that holds the row indices of the block variable.
- Param mat_col_indices
An array that holds the column indices of the block variable.
- Param mat_values
An array that holds the objective coefficients associated with the block variable.
Note
If mat_row_indices[e] is not equal to mat_col_indices[e], element (mat_row_indices[e],mat_col_indices[e]) and element (mat_col_indices[e],mat_row_indices[e]) are changed to values[e] to ensure the symmetricity. As such, user should input only the lower triangular part or upper triangular part of the block variable.
-
void replaceSymMatElements(MdoI32 row_index, MdoI32 mat_index, MdoI32 size, const MdoI32 *mat_row_indices, const MdoI32 *mat_col_indices, const MdoReal *mat_values)¶
Replace the values of the specified block variable in the constraints.
- Param row_index
Row index.
- Param mat_index
Block variable index.
- Param size
Number of elements of the block variable to access.
- Param mat_row_indices
An array that holds the row indices of the block variable.
- Param mat_col_indices
An array that holds the column indices of the block variable.
- Param mat_values
An array that holds the objective coefficients associated with the block variable.
Note
If mat_row_indices[e] is not equal to mat_col_indices[e], element (mat_row_indices[e],mat_col_indices[e]) and element (mat_col_indices[e],mat_row_indices[e]) are changed to values[e] to ensure the symmetricity. As such, user should input only the lower triangular part or upper triangular part of the block variable.
-
std::vector<std::reference_wrapper<MdoReal>> getRealAttrSymMat(std::string att, MdoI32 mat_index, MdoI32 size, const MdoI32 *mat_row_indices, const MdoI32 *mat_col_indices)¶
Retrieve the values associated with the specified block variable of the attribute.
- Param att
A real-valued symmetric variable attribute to access.
- Param mat_index
Block variable index.
- Param size
Number of elements of the block variable to access.
- Param mat_row_indices
An array that holds the row indices of the block variable.
- Param mat_col_indices
An array that holds the column indices of the block variable.
- Retrun
A vector object that will hold the values associated with the specified block variable of the attribute.
Note
If size is greater than or equal to the number of elements in the specified block matrix, then all elements in the specified block matrix will be accessed. As a result, mat_row_indices and mat_col_indices will not be accessed.
Methods related to objective function.
-
MdoBool isMinObjSense(void) const¶
This function checks if the objective function has a minimization sense.
- Return
A boolean flag that specifies if the objective function has a minimization sense.
-
MdoBool isMaxObjSense(void) const¶
This function checks if the objective function has a maximization sense.
- Return
A boolean flag that specifies if the objective function has a maximization sense.
-
void setMinObjSense(void)¶
This function changes the objective function of the loaded problem to minimization sense.
-
void setMaxObjSense(void)¶
This function changes the objective function of the loaded problem to maximization sense.
-
MdoReal getObjOffset(void) const¶
This function retrieves the objective offset (constant term).
- Return
The value of the objective offset (constant term).
-
void setObjOffset(MdoReal obj_fix)¶
This function retrieves the objective offset (constant term).
- Parameters
obj_fix – New objective offset (fixed cost).
-
std::vector<MdoReal> getObjs(std::vector<std::reference_wrapper<MdoVar>> &vars)¶
This function retrieves the values of the specified objective coefficients.
- Parameters
vars – A vector object that holds the references to the variables to access.
- Return
A STL vector that holds the values of the specified objective coefficients.
-
void setObjs(std::vector<std::reference_wrapper<MdoVar>> &vars, const MdoReal *vals)¶
This function changes the values of the objective coefficients.
- Parameters
vars – A vector object that holds the references to the variables to access.
vals – The new values for the specified objective coefficients.
Methods for row/column query.
-
MdoCol getCol(const MdoVar &var) const¶
This function retrieves a column object that will hold a set of constraint and coefficient pairs.
- Parameters
var – A reference to the variable object.
- Return
A column object that will hold a set of constraint and coefficient pairs.
-
MdoExprLinear getExprLinear(const MdoCons &cons) const¶
This function retrieves a linear expression object that will hold a set of variable and coefficient pairs.
- Parameters
cons – A refernce to the constraint object.
- Return
A linear expression object that will hold a set of variable and coefficient pairs.
Methods related to nonzero elements in the constraint matrix.
-
std::vector<MdoReal> getElements(const std::vector<std::reference_wrapper<MdoCons>> &conss, const std::vector<std::reference_wrapper<MdoVar>> &vars) const¶
This function returns a vector object that will hold the nonzero values of all specified elements in the constraint matrix.
- Parameters
conss – A vector object that holds the references to the constraints.
vars – A vector object that holds the references to the variables.
- Return
A vector object that will hold the nonzero values of all specified elements in the constraint matrix.
-
void setElements(const std::vector<std::reference_wrapper<MdoCons>> &conss, const std::vector<std::reference_wrapper<MdoVar>> &vars, const std::vector<MdoReal> &values)¶
This function modifies a set of values of all specified elements in the constraint matrix.
- Parameters
conss – A vector object that holds the references to the constraints.
vars – A vector object that holds the references to the variables.
values – An array that holds the new nonzero values for each specified element in the constraint matrix.
-
void setQuadraticElements(const std::vector<std::reference_wrapper<MdoVar>> &vars1, const std::vector<std::reference_wrapper<MdoVar>> &vars2, const std::vector<MdoReal> &values)¶
This function modifies a set of values of all specified elements in the quadratic matrix of a quadratic program.
- Parameters
vars1 – A vector object that holds the references to the first variables.
vars2 – A vector object that holds the references to the second variables.
values – An array that holds the new nonzero values.
-
void deleteElements(const std::vector<std::reference_wrapper<MdoCons>> &conss, const std::vector<std::reference_wrapper<MdoVar>> &vars)¶
This function deletes a set of elements from the constraint matrix.
- Parameters
conss – A vector object that holds the references to the constraints.
vars – A vector object that holds the references to the variables.
-
void deleteAllElements(void)¶
This function deletes all elements from the constraint matrix.
-
void deleteAllQuadraticElements(void)¶
This function deletes all elements in the quadratic matrix of a quadratic program.
-
void relaxIntegrality(void)¶
This function removes all integrality requirements in the model and transforms it into a continuous relaxation problem.
Basic IO.
-
void readProb(std::string filename)¶
This function reads an optimization problem from a file.
- Parameters
filename – A string that specifies the filename to be read.
Note
Note that the type of the inputted model is determined by the file suffix. Valid suffixes are
.mps
(.bz2/.gz
) or.lp
(.bz2/.gz
) or.dat-s
.Once the function is called, the previously loaded problem will be abandoned.
Users may
getConss()
(resp.getVars()
) to retrieve all created constraint objects (resp. variable objects).
-
void writeProb(std::string filename) const¶
This function writes an optimization problem to a file.
- Parameters
filename – A string that specifies the filename for the output.
Note
Note that the type of the output model is determined by the file suffix. Valid suffixes are
.mps
(.bz2/.gz
) or.lp
(.bz2/.gz
).
-
void writeSoln(std::string filename) const¶
This function writes an optimization solution to a file.
- Parameters
filename – A string that specifies the filename for the output.
Note
Note that the type of the optimization solution is determined by the file suffix. Valid suffixes are
.sol
or.bas
.
-
void readTask(std::string filename, bool read_model = true, bool read_param = true, bool read_soln = true)¶
This function reads an optimization model task from a file.
- Parameters
filename – A string that specifies the filename for the output.
read_model – A boolean flag that specifies if the model shall be loaded. Default is true.
read_param – A boolean flag that specifies if the parameters shall be loaded. Default is true.
read_soln – A boolean flag that specifies if the solution shall be loaded. Default is true.
- See
Note
Once the function is called, the previously loaded problem will be abandoned.
Users may getConss (resp. getVars) to retrieve all created constraint objects (resp. variable objects).
A model task file includes the problem data, parameter settings, and solutions in a binary format.
-
void writeTask(std::string filename, bool write_model = true, bool write_param = true, bool write_soln = true)¶
This function writes an optimization task to a file.
- Parameters
filename – A string that specifies the filename.
write_model – A boolean flag that specifies if the model shall be outputted. Default is true.
write_param – A boolean flag that specifies if the parameters shall be outputted. Default is true.
write_soln – A boolean flag that specifies if the solution shall be outputted. Default is true.
- See
Note
A model task file includes the problem data, parameter settings, and solutions in a binary format.
-
std::string submitTask(void)¶
This function submits an optimization model task to a remote server for optimization.
- Return
A STL string that specifies the ID of the submitted job. Users may use this job ID to query the optimization result.
- See
Note
A model task file includes the problem data, parameter settings, and solutions in a binary format.
The token ID and the address of the remote server must be specified before calling this API function.
-
std::string retrieveTask(std::string job_id, MdoStatus &code, MdoResult &result, MdoBool &has_soln)¶
Check the status of the submitted task, and then retrieve the corresponding optimization result if available. All possible status values are: “Submitted”, “Solving”, “Canceled”, Finished”, and “Failed”.
- Parameters
job_id – Job ID.
code – A status code that specifies the model status.
result – A response code that specifies the optimization result, that is, the response code while calling Mdo_solveProb on a remote server.
has_soln – A flag that specifies the availability of the solution. If true, user may then use Mdo_readTask to read the solution file that was saved to the designated location.
- Return
A STL string object that will hold the status of the submitted task.
- See
Note
Note that the optimization result will be returned only if the submitted task is in a “Finished” status.
The token ID and the address of the remote server must be specified before calling this API function.
Methods for accessing the internal data using attribute.
-
void setStrAttrIndex(std::string att, MdoI32 idx, std::string val)¶
This function changes the value of a string-valued row/column attribute.
- Parameters
att – A string-valued model row/column to access.
idx – An index of the array to access.
val – A new value.
-
std::string getStrAttrIndex(std::string att, MdoI32 idx) const¶
This function retrieves the value of a string-valued row/column attribute.
- Parameters
att – A string-valued row/column attribute to access.
idx – An index of the array to access.
- Return
The value of a string-valued row/column attribute.
-
void setIntAttr(std::string att, MdoI32 val)¶
This function changes the value of a 32-bit integer-valued model attribute.
- Parameters
att – An integer-valued model attribute to access.
val – A new value.
-
MdoI32 getIntAttr(std::string att) const¶
This function retrieves the value of a 32-bit integer-valued model attribute.
- Parameters
att – An integer-valued model attribute to access.
- Return
The value of a 32-bit integer-valued model attribute.
-
void setIntAttrIndex(std::string att, MdoI32 idx, MdoI32 val)¶
This function changes the value of a 32-bit integer-valued row/column attribute.
- Parameters
att – An integer-valued row/column attribute to access.
idx – An index of the array to access.
val – A new value.
-
MdoI32 getIntAttrIndex(std::string att, MdoI32 idx) const¶
This function retrieves the value of a 32-bit integer-valued row/column attribute.
- Parameters
att – An integer-valued row/column attribute to access.
idx – An index of the array to access.
- Return
The value of a 32-bit integer-valued row/column attribute.
-
void setIntAttrArray(std::string att, MdoI32 bgn, MdoI32 len, const MdoI32 *val)¶
This function changes the values of the specified array of a 32-bit integer-valued row/column attribute.
- Parameters
att – An integer-valued row/column attribute to access.
bgn – Index of the first element to access.
len – Number of elements to access.
val – The new values for the specified array of the attribute.
-
std::vector<MdoI32> getIntAttrArray(std::string att, MdoI32 bgn, MdoI32 len) const¶
This function retrieves the values of the specified array of a 32-bit integer-valued row/column attribute.
- Parameters
att – An integer-valued row/column attribute to access.
bgn – Index of the first element to access.
len – Number of elements to access.
- Return
A STL vector that holds the values of the specified array of the attribute.
-
void setIntAttrVars(std::string att, std::vector<std::reference_wrapper<MdoVar>> &vars, const MdoI32 *vals)¶
This function changes the values of the specified array of a 32-bit integer-valued column attribute.
- Parameters
att – An integer-valued array attribute to access.d row/column attribute to access.
vars – A vector object that holds the references to the variables to access.
vals – The new values for the specified array of the attribute.
-
std::vector<MdoI32> getIntAttrVars(std::string att, std::vector<std::reference_wrapper<MdoVar>> &vars)¶
This function retrieves the values of the specified array of a 32-bit integer-valued column attribute.
- Parameters
att – An integer-valued array attribute to access.d row/column attribute to access.
vars – A vector object that holds the references to the variables to access.
- Return
A STL vector that holds the values of the specified sub-array of the attribute.
-
void setIntAttrConss(std::string att, std::vector<std::reference_wrapper<MdoCons>> &conss, const MdoI32 *vals)¶
This function changes the values of the specified array of a 32-bit integer-valued row attribute.
- Parameters
att – An integer-valued array attribute to access.d row/column attribute to access.
conss – A vector object that holds the references to the constraints to access.
vals – The new values for the specified array of the attribute.
-
std::vector<MdoI32> getIntAttrConss(std::string att, std::vector<std::reference_wrapper<MdoCons>> &conss)¶
This function retrieves the values of the specified array of a 32-bit integer-valued row attribute.
- Parameters
att – An integer-valued array attribute to access.d row/column attribute to access.
conss – A vector object that holds the references to the constraints to access.
- Return
A STL vector that holds the values of the specified sub-array of the attribute.
-
void setRealAttr(std::string att, MdoReal val)¶
This function changes the value of a real-valued model attribute.
- Parameters
att – A real-valued model attribute to access.
val – A new value.
-
MdoReal getRealAttr(std::string att) const¶
This function retrieves the value of a real-valued model attribute.
- Parameters
att – A real-valued model attribute to access.
- Return
The value of a real-valued model attribute.
-
void setRealAttrIndex(std::string att, MdoI32 idx, MdoReal val)¶
This function changes the value of a real-valued row/column attribute.
- Parameters
att – A real-valued row/column attribute to access.
idx – An index of the array to access.
val – A new value.
-
MdoReal getRealAttrIndex(std::string att, MdoI32 idx) const¶
This function retrieves the value of a real-valued row/column attribute.
- Parameters
att – A real-valued row/column attribute to access.
idx – An index of the array to access.
- Return
The current value of a real-valued row/column attribute.
-
void setRealAttrArray(std::string att, MdoI32 bgn, MdoI32 len, const MdoReal *vals)¶
This function changes the values of the specified array of a real-valued row/column attribute.
- Parameters
att – A real-valued row/column attribute to access.
bgn – Index of the first element to access.
len – Number of elements to access.
vals – The new values for the specified array of the attribute.
-
std::vector<MdoReal> getRealAttrArray(std::string att, MdoI32 bgn, MdoI32 len) const¶
This function retrieves the values of the specified array of a real-valued row/column attribute.
- Parameters
att – A real-valued row/column attribute to access.
bgn – Index of the first element to access.
len – Number of elements to access.
- Return
A STL vector that holds the values of the specified array of the attribute.
-
void setRealAttrVars(std::string att, std::vector<std::reference_wrapper<MdoVar>> &vars, const MdoReal *vals)¶
This function changes the values of the specified array of a real-valued column attribute.
- Parameters
att – A real-valued colunm attribute to access.
vars – A vector object that holds the references to the variables to access.
vals – The new values for the specified array of the attribute.
-
std::vector<MdoReal> getRealAttrVars(std::string att, std::vector<std::reference_wrapper<MdoVar>> &vars)¶
This function retrieves the values of the specified array of a real-valued column attribute.
- Parameters
att – A real-valued colunm attribute to access.
vars – A vector object that holds the references to the variables to access.
- Return
A STL vector that holds the values of the specified array of the attribute.
-
void setRealAttrConss(std::string att, std::vector<std::reference_wrapper<MdoCons>> &conss, const MdoReal *vals)¶
This function changes the values of the specified array of a real-valued row attribute.
- Parameters
att – A real-valued row attribute to access.
conss – A vector object that holds the references to the constraints to access.
vals – The new values for the specified array of the attribute.
-
std::vector<MdoReal> getRealAttrConss(std::string att, std::vector<std::reference_wrapper<MdoCons>> &conss)¶
This function retrieves the values of the specified array of a real-valued row attribute.
- Parameters
att – A real-valued row attribute to access.
conss – A vector object that holds the references to the constraints to access.
- Return
A STL vector that holds the values of the specified array of the attribute.
Methods to access the parameter settings.
-
void setStrParam(std::string par, std::string val)¶
This function changes the value of a string-valued parameter.
- Parameters
par – A string-valued parameter to access.
val – A new value for the string-valued parameter.
-
std::string getStrParam(std::string par) const¶
This function retrieves the value of a string-valued parameter.
- Parameters
par – A string parameter to access.
- Return
The value of a string-valued parameter.
-
void setIntParam(std::string par, MdoI32 val)¶
This function changes the value of a 32-bit integer-valued parameter.
- Parameters
par – An integer-valued parameter to access.
val – A new value for the 32-bit integer-valued parameter.
-
MdoI32 getIntParam(std::string par) const¶
This function retrieves the value of a 32-bit integer-valued parameter.
- Parameters
par – An integer-valued parameter to access.
- Return
The value of a 32-bit integer-valued parameter.
-
void setRealParam(std::string par, MdoReal val)¶
This function changes the value of a real-valued parameter.
- Parameters
par – A real-valued parameter to access.
val – A new value for the real-valued parameter.
-
MdoReal getRealParam(std::string par) const¶
This function retrieves the value of a real-valued parameter.
- Parameters
par – A real-valued parameter to access.
- Return
The value of a 32-bit integer-valued parameter.
Methods to optimize the model and retrieve the result.
-
void solveProb(void)¶
This function solves the loaded optimization problem.
-
void displayResults(void) const¶
This function displays current solver results, including
primal and dual objective values;
variable bound and constraint violations.
-
MdoStatus getStatus(void) const¶
This function returns the problem status code.
- Return
Problem status code.
-
std::string explainStatus(MdoStatus status) const¶
This function explains the details of a solver status code. Specifically, if MindOpt optimize the loaded problem to successfully, users may use this function to populate the solution status.
- Parameters
status – Solver status code to be explained.
- Return
A string that holds the explanation of the solver status code.
-
std::string explainResult(MdoResult result) const¶
This function explains the details of a solver result code. Specifically, if MindOpt fails to optimize the loaded problem to optimality, users may use this function to populate the details of the result code.
- Parameters
result – Solver result code to be explained.
- Return
A string that holds the explanation of the optimization status code.
-
void computeIIS(void)¶
This function computes a subsystem that contains at least an IIS (irreduciable infeasible system). The cardinality of the subsystem is supposed to be small. Note that the problem is supposed to be infeasible.
Methods the internal data access.
-
MdoCons &getCons(MdoI32 i)¶
This function returns reference to a constraint object by index.
- Parameters
i – Row index.
- Return
A reference to a constraint object.
-
MdoCons &getCons(std::string name)¶
This function returns a reference to constraint object by name.
- Parameters
name – Constraint name.
- Return
A reference to a constraint object
-
std::vector<std::reference_wrapper<MdoCons>> getConss(void)¶
This function returns a vector that will hold references to the constraint objects.
- Return
a vector that will hold references to the constraint objects.
-
MdoVar &getVar(MdoI32 j)¶
This function returns a reference to variable object by index.
- Parameters
j – Column index.
- Return
A reference to a variable object.
-
MdoVar &getVar(std::string name)¶
This function returns a reference to variable object by given name.
- Parameters
name – Variable name.
- Return
A reference to a variable object.
-
std::vector<std::reference_wrapper<MdoVar>> getVars(void) const¶
This function returns a vector that will hold references to the variable objects.
- Return
a vector that will hold references to the variable objects.
Debuggers.