8.2.6. MdoExprLinear¶
-
class mindopt::MdoExprLinear : public mindopt::MdoExpr¶
A linear expression object.
This object implements the data structure to hold a linear constraint expression object, which consists of a set of coefficient-variable pairs.
Typical steps to to input a linear constraint expression are:
Create a set of empty variable objects by calling
MdoModel::addVar()
.Create an empty linear expression object by calling
MdoExprLinear::MdoExprLinear()
.Input linear expression using overloaded operators such as
+=
or+
, or member functionMdoExprLinear::addTerms()
.
For example,
MdoVar & x1 = model.addVar(); MdoVar & x2 = model.addVar(); MdoExprLinear expr1; expr1 = 1 * x1; expr1 += x2; MdoVar & x3 = model.addVar(); MdoExprLinear expr2; expr2 = expr1 + x3;
A nonzero element can be queried by calling
MdoExprLinear::getVar()
andMdoCol::getCoeff()
to access the associated variable and coefficient.Note
This is a temporary object with a short lifespan in general.
In general,
expr += x
(orexpr -= x
) is much more efficient thanexpr = expr + x
, and thus the later one shall be avoided in a loop. Note that the most efficient way to build an expression object is to callMdoExprLinear::addTerms()
.
Examples
/* Create an empty model. */ MdoModel model; MdoI32 itmp = 0; MdoReal dtmp = 0.0; /* 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)); MdoExprLinear c0_expr; c0_expr = model.getExprLinear(c0); std::cout << "c0_expr: " << std::endl << c0_expr << std::endl; /* Check linear expression object */ MdoVar x_tmp = c0_expr.getVar(0); itmp = x_tmp.sameAs(x0); dtmp = c0_expr.getCoeff(0); itmp = c0_expr.getSize(); /* Set/get constant */ dtmp = c0_expr.getConstant(); c0_expr.addConstant(10.0); dtmp = c0_expr.getConstant(); c0_expr.setConstant(-10.0); dtmp = c0_expr.getConstant(); /* Remove item from linear expression */ c0_expr.removeTerm(1); itmp = c0_expr.getSize(); c0_expr.removeTerm(x_tmp); itmp = c0_expr.getSize(); c0_expr.clear(); itmp = c0_expr.getSize(); /* Adds a set of nonzero terms. */ MdoVar x_array[] = {x[0].get(), x[1].get(), x[2].get(), x[3].get()}; double row_coeff_array[] = {1, 2, 3}; c0_expr.addTerms(x_array, row_coeff_array, 3); double *row_coeff_chk = new double[3]; int i; for (i=0; i < 3; i++) { row_coeff_chk[i] = c0_expr.getCoeff(i); } /* Adds a nonzero term. */ c0_expr.addTerm(x_array[3], 4.0); dtmp = c0_expr.getCoeff(3);
Constructor / destructor.
-
MdoExprLinear(void)¶
Default constructor.
-
virtual ~MdoExprLinear(void)¶
Destructor.
-
MdoExprLinear(const MdoExprLinear &rhs)¶
Copy constructor.
- Parameters
rhs – Object to copy.
Overloaded operators.
-
MdoExprLinear &operator=(const MdoExprLinear &rhs)¶
Set the current expression equal to another one.
- Parameters
rhs – Another expression.
- Return
Updated expression object.
-
MdoExprLinear operator+(const MdoExprLinear &rhs) const¶
Add another expression to the current one, and result in a new linear expression.
- Parameters
rhs – Another expression.
- Return
A new expression object that equals to the sum of the invoking expression and the argument expression.
-
MdoExprLinear operator-(const MdoExprLinear &rhs) const¶
Subtract the current expression by another one, and result in a new linear expression.
- Parameters
rhs – Another expression.
- Return
A new expression object that equals to the invoking expression extracted by the argument expression.
-
MdoExprLinear &operator+=(const MdoExprLinear &rhs)¶
Add another expression to the current one.
- Parameters
rhs – Another expression.
- Return
An updated expression object that equals to the sum of the invoking expression and the argument expression.
-
MdoExprLinear &operator-=(const MdoExprLinear &rhs)¶
Subtract the current expression by another one.
- Parameters
rhs – Another expression.
- Return
An updated expression object that equals to the invoking expression extracted by the argument expression.
-
MdoExprLinear &operator+=(const MdoVar &var)¶
Add a variable to the current one.
- Parameters
var – A variable object.
- Return
An updated expression object that equals to the sum of the invoking expression and a variable object.
-
MdoExprLinear &operator-=(constMdoVar &var)¶
Subtract the current expression by a variable.
- Parameters
var – A variable object.
- Return
An updated expression object that equals to the invoking expression extracted by a variable object.
-
MdoExprLinear &operator+=(const MdoReal constant)¶
Add a constant term to the current one.
- Parameters
constant – A constant term.
- Return
An updated expression object that equals to the sum of the invoking expression and a constant term.
-
MdoExprLinear &operator-=(const MdoReal constant)¶
Subtract the current expression by a constant term.
- Parameters
constant – A constant term.
- Return
An updated expression object that equals to the invoking expression extracted by a constant term.
-
MdoExprLinear &operator*=(MdoReal scal)¶
Multiple the current expression by a constant.
- Parameters
scal – Muliplier.
- Return
An updated expression object.
-
MdoExprLinear &operator/=(MdoReal scal)¶
Divide the current expression by a constant.
- Parameters
scal – Divisor.
- Return
An updated expression object.
Functions to access the internal data.
-
void addTerm(const MdoVar &var, MdoReal coeff)¶
This function adds a nonzero term. A nonzero variable object is a pair of variable and coefficient.
- Parameters
var – A variable object associated with the nonzero term.
coeff – A coefficient of the nonzero term.
-
void addTerms(const MdoVar *vars, const MdoReal *coeffs, MdoI32 size)¶
This function adds a set of nonzero terms. A nonzero term is a pair of variable and coefficient.
- Parameters
vars – An array that holds the variable objects.
coeffs – An array that holds the nonzero coefficients.
size – Number of terms to be added.
-
const MdoVar &getVar(MdoI32 pos) const¶
This function retrieves the the variable object from the expression object.
- Parameters
pos – The term that is currently stored at position
pos
.- Return
The nonzero coefficient.
-
MdoVar &getVar(MdoI32 pos)¶
This function retrieves the the variable object from the expression object.
- Parameters
pos – The term that is currently stored at position pos.
- Return
The nonzero coefficient.
-
MdoReal getCoeff(MdoI32 pos) const¶
This function retrieves the value of a nonzero element from the expression object.
- Parameters
pos – The term that is currently stored at position
pos
.- Return
The value of the nonzero element.
-
void removeTerm(MdoI32 pos)¶
This function removes a nonzero element from the expression object, given a variable index.
- Parameters
pos – The term that is currently stored at position
pos
.
-
MdoBool removeTerm(const MdoVar &var)¶
This function removes a nonzero element from the expression object, given a variable object.
- Parameters
var – A variable object whose coefficient will be removed.
- Return
Return true if the specified coefficient is in the expression and has been removed.
-
MdoReal getConstant(void) const¶
This function returns the constant term in the expression object.
- Return
The constant term.
-
void setConstant(MdoReal constant)¶
This function changes the constant term in the expression object.
- Parameters
constant – A new constant term.
-
void addConstant(MdoReal constant)¶
This function adds a constant term to the expression object.
- Parameters
constant – A constant term that will be added.
-
void clear(void)¶
This function removes all nonzero elements from the expression object, and resets the constant term to
0.0
.