Constant

class Constant

Constant term modeling-old object.

This class is used to build constant terms which will appear in models.

Typically, they represent a real number. However, they can also be an affine combination of Param. This can be useful to define specific decomposition schemes such as Dantzig-Wolfe reformulations where decision variables from a model A are seen as constant terms in a model B.

It is composed of a numerical term and a set of { Param, numerical } pairs representing the following mathematical expression.

\( \text{numerical}_0 + \sum_{j=1}^n \text{numerical}_j \times \text{Param}_j \)

Constant terms can be created by calling the appropriate constructor, or by using overloaded operators such as + and *. For instance, 2 * xi where xi is a Param will yield a Constant.

Public Functions

Constant() = default

Create a new constant term equal to zero.

Constant(const Param &t_param, double t_value = 1.)

Constructor.

Creates a new constant term representing t_value * t_param.

Parameters:
  • t_param – The parameter.

  • t_value – The coefficient for the parameter.

Constant(const Param &t_param_1, const Param &t_param_2, double t_value = 1.)

Constructor.

Creates a new constant term representing t_value * t_param_1 * t_param_2.

Parameters:
  • t_param_1 – The first parameter.

  • t_param_2 – The second parameter.

  • t_value – The coefficient for the parameters.

Constant(double t_constant)

Constructor.

Creates a new constant term equal to the constant given as argument.

Parameters:

t_constant – The constant.

Constant(const Constant &t_src)

Copy constructor.

Constant(Constant&&) noexcept = default

Move constructor.

Constant &operator=(const Constant &t_rhs)

Copy-assignment operator.

Returns:

*this

Constant &operator=(Constant&&) noexcept = default

Move-assignment operator.

Returns:

*this

void set(const Param &t_param, double t_value)

Sets the coefficient for a given parameter in the constant term.

If t_coefficient is zero, the entry for t_param is removed (i.e., not stored). The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:
  • t_param – The parameter.

  • t_value – The coefficient associated to the parameter.

void set(const Param &t_param_1, const Param &t_param_2, double t_value)

Sets the coefficient for a given pair of parameters in the constant term.

If t_coefficient is zero, the entry for t_param is removed (i.e., not stored). The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:
  • t_param_1 – The first parameter.

  • t_param_2 – The second parameter.

  • t_value – The coefficient associated to the pair of parameters.

double get(const Param &t_param) const

Returns the coefficient of the parameter given as argument.

If no coefficient is found, zero is returned.

Parameters:

t_param – The queried parameter.

double get(const Param &t_param_1, const Param &t_param_2) const

Returns the coefficient of the pair of parameters given as arguments.

Parameters:
  • t_param_1 – The first parameter.

  • t_param_2 – The second parameter.

Returns:

The queried parameters.

inline void set_numerical(double t_constant)

Sets the numerical term equal to the constant given as parameter.

Parameters:

t_constant – The numerical term.

inline double numerical() const

Returns the numerical term of the constant.

double as_numerical() const

Returns the numerical term of the constant, and throws an exception if the Constant also contains non-numerical terms, i.e., the Constant is a parametrized constant.

Returns:

The numerical term of the constant

bool is_zero() const

Returns true if the constant is equal to zero (i.e., if its numerical term is zero and no { Param, numerical } pair can be found). The tolerance Tolerance::Sparsity is used to compare with zero.

inline unsigned int size() const

Returns the number of { Param, numerical } pairs stored inside the Constant.

bool is_numerical() const

Returns true if the Constant is a pure numerical, i.e., the Constant is not a parametrized constant, false otherwise.

inline auto linear()

Returns the linear part of the parametrized expression.

Returns:

The linear part of the parametrized expression.

inline auto linear() const

Returns the linear part of the parametrized expression.

Returns:

The linear part of the parametrized expression.

inline auto quadratic()

Returns the quadratic part of the parametrized expression.

Returns:

The quadratic part of the parametrized expression.

inline auto quadratic() const

Returns the quadratic part of the parametrized expression.

Returns:

The quadratic part of the parametrized expression.

Constant &operator*=(double t_coefficient)

Multiplies the Constant by t_coefficient (i.e., every { Param, numerical } pairs and the numerical term are multiplied).

Parameters:

t_coefficient – The coefficient for multiplication.

Constant &operator/=(double t_coefficient)
Constant &operator+=(double t_term)

Adds up a numerical term to the current numerical term.

Parameters:

t_term – The numerical term to add.

Constant &operator+=(const Param &t_term)

Adds a parameter to the constant (equivalent to adding Constant(t_param, 1)).

Resulting zero entries are removed. The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:

t_term – The parameter to add.

Constant &operator+=(const Constant &t_term)

Adds up another constant term (i.e., both the numerical terms and every { Param, numerical } pairs are added up).

Resulting zero-entries are removed. The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:

t_term

Constant &operator-=(const Constant &t_term)

Subtracts another constant term (i.e., both the numerical terms and every { Param, numerical } pairs are substracted).

Resulting zero-entries are removed. The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:

t_term – The constant to subtract.

Constant &operator-=(double t_term)

Subtracts a numerical term to the current numerical term.

Parameters:

t_term – The numerical term to subtract.

Constant &operator-=(Param t_term)

Subtracts parameter to the constant (equivalent to subtracting Constant(t_param, 1)).

Resulting zero entries are removed. The tolerance Tolerance::Sparsity is used to compare with zero.

Parameters:

t_term – The parameter to subtract.

double fix(const Solution::Primal &t_primals) const

Computes the resulting numerical by replacing each Param by their corresponding values in t_primals.

Parameters:

t_primals – Primal values associated to the stored Param

Returns:

the resulting numerical

double fix(const Solution::Dual &t_duals) const

Computes the resulting numerical by replacing each Param by their corresponding values in t_duals.

Parameters:

t_primals – Dual values associated to the stored Param

Returns:

the resulting numerical

void round()
Constant &multiply_with_precision(double t_factor, unsigned int t_n_digits)
Constant &multiply_with_precision_by_power_of_10(unsigned int t_exponent, unsigned int t_n_digits)

Public Static Attributes

static Constant Zero

Represents an empty constant.