LinExpr
This class is used to represent linear expressions in the modeling interface of idol.
It consists in a set of linear terms which represent products of keys (typically, variables) and scalars.
Example
In this example, we create a linear expression representing the following linear expression.
auto x = model.add_vars(Dim<1>(2), 0, Inf, Continuous, "x");
LinExpr lin_expr = 3 * x[0] + 2 * x[1];
-
template<class Key = idol::Var>
class LinExpr : public idol::AbstractExpr<idol::Var, LinTerm<idol::Var>> - Template Parameters:
Key – the class representing keys
Public Functions
-
LinExpr() = default
-
void set(const Key &t_key, const Constant &t_coefficient)
Sets the value of a key’s coefficient
The constant is copied.
Example:
LinExpr lin_expr; lin_expr.set(x[0], -1);
- Parameters:
t_key – the key
t_coefficient – the key’s coefficient
-
virtual void set(const Key &t_key, Constant &&t_coefficient) override
Sets the value of a key’s coefficient
The constant is moved.
Example:
LinExpr lin_expr; lin_expr.set(x[0], -1);
- Parameters:
t_key – the key
t_coefficient – the key’s coefficient
-
virtual const Constant &get(const Key &t_key) const override
Returns a key’s coefficient
Example:
LinExpr lin_expr = 2 * x[0] + 3 * x[1]; std::cout << lin_expr.get(x[0]) << std::endl; // output: 2
- Parameters:
t_key – the key
- Returns:
the key’s coefficient
-
virtual void remove(const Key &t_key) override
Sets the coefficient of a key to zero
This is equivalent to calling
LinExpr<Key>::set
with a zero coefficient.- Parameters:
t_key – the key
-
AbstractExpr<Key, IteratorOutputT, Hash, EqualTo> &operator+=(const AbstractExpr<Key, IteratorOutputT, Hash, EqualTo> &t_rhs)
Adds each term of
t_rhs
to the expression.Zero entries are removed from the resulting expression.
- Parameters:
t_rhs – the expression whose terms are to be added up.
- Returns:
The object itself.
-
AbstractExpr<Key, IteratorOutputT, Hash, EqualTo> &operator-=(const AbstractExpr<Key, IteratorOutputT, Hash, EqualTo> &t_rhs)
Subtracts each term of
t_rhs
to the expression.Zero entries are removed from the resulting expression.
- Parameters:
t_rhs – The object whose terms are to be subtracted.
- Returns:
The object itself.
-
AbstractExpr<Key, IteratorOutputT, Hash, EqualTo> &operator*=(double t_factor)
Multiplies every term of the expression by
t_factor
.If
t_factor
equals zero with toleranceTolerance::Sparsity
, then the expression is emptied.- Parameters:
t_factor – The multiplying factor.
- Returns:
The object itself.
-
inline unsigned int size() const
Returns the size of the expression (i.e., number of non-zero entries which are currently stored).
- Returns:
the size of the expression
-
inline bool empty() const
Returns true if and only if the size is zero.
- Returns:
true if and only if the size is zero
-
inline const_iterator begin() const
Returns an iterator pointing to the first term of the expression.
- Returns:
an iterator pointing to the first term of the expression
-
inline const_iterator end() const
Returns an iterator pointing to the end of the expression.
- Returns:
an iterator pointing to the end the expression
-
inline void clear()
Removes all entries from the expression.
-
void round()
-
AbstractExpr multiply_with_precision(double t_factor, unsigned int t_n_digits)
-
AbstractExpr multiply_with_precision_by_power_of_10(unsigned int t_exponent, unsigned int t_n_digits)
-
long gcd() const