Solution::Primal

class Primal : public idol::AbstractSolution<Var, Primal>

Primal solution class.

This class is used to store a primal solution to an optimization problem.

Typically, it is obtained by calling the save_primal function, or by creating a new solution from scratch.

Example 1:

model.optimize();
if (model.get_status() == Optimal) {
     Solution::Primal solution = save_primal(model);
     std::cout << solution << std::endl;
}

Example 2:

Solution::Primal solution;

solution.set_status(Feasible);
solution.set_objective_value(13.);
solution.set(x, 1.);

Public Types

using const_iterator = typename Map<Var, double>::const_iterator

Public Functions

inline void set_status(SolutionStatus t_status)

Sets the solution status.

Parameters:

t_status – The desired solution status.

inline SolutionStatus status() const

Returns the stored solution status.

inline void set_reason(SolutionReason t_reason)

Sets the reason for the solution status.

Example:

solution.set_status(Feasible);
solution.set_reason(IterLimit); // We only found a feasible solution because of iteration limit reached

Parameters:

t_reason – The desired reason.

inline SolutionReason reason() const

Returns the reason for the solution status.

Returns:

The reason for the solution status.

inline void set_objective_value(double t_value)

Sets the objective value of the solution.

Parameters:

t_value – The desired objective value.

inline double objective_value() const

Returns the objective value of the solution.

inline bool has_objective_value() const

Returns true if the solution has an objective value, false otherwise.

Returns:

True if the solution has an objective value, false otherwise.

inline void reset_objective_value()

Resets the stored objective value.

Trying to access the objective value after calling this method will throw an exception.

void set(const Var &t_key, double t_value)

Sets the value associated to the object t_key given as argument.

Parameters:
  • t_key – The key for which the entry should be set.

  • t_value – The desired value associated to the key.

double get(const Var &t_key) const

Returns the value associated to the object t_key given as argument.

If no value is stored, zero is returned.

Parameters:

t_key – The queried key.

inline unsigned int size() const

Returns the number of non-zero entries in the solution.

Returns:

The number of non-zero entries in the solution

inline const_iterator begin() const

Returns an iterator on the values stored in the solution.

Returns:

An iterator on the values stored in the solution.

inline const_iterator end() const

Returns an iterator on the values stored in the solution.

Returns:

An iterator on the values stored in the solution.

inline const_iterator cbegin() const

Returns an iterator on the values stored in the solution.

Returns:

An iterator on the values stored in the solution.

inline const_iterator cend() const

Returns an iterator on the values stored in the solution.

Returns:

An iterator on the values stored in the solution.

double norm(double t_p = 2.) const

Returns the \( l_p \)-norm of the solution.

Note that Inf is a possible value for t_p, in which case, the infinity norm is computed.

Parameters:

t_p – The \( p \) parameter in the \( l_p \)-norm.

Primal &merge_without_conflict(Primal t_rhs)

Merges the solution with another solution, explicitly requiring that no conflict arises (i.e., that no entry from the solution t_rhs is already stored in the solution). If a conflict is detected, an exception is thrown.

Parameters:

t_rhs – The solution to merge.

Returns:

*this

Primal &normalize(double t_p = 2.)

Normalizes the solution (i.e., divides every entry by the norm of the solution) with respect to a given \( l_p \)-norm.

Parameters:

t_p – The parameter \( p \) for the norm.

Returns:

*this

Primal &round(unsigned int t_n_digits = 0)

Rounds all the entries of the solution to the closest double with t_n_digits.

Using t_n_digits = 0 leads to the usual rounding, i.e., closest integer.

Parameters:

t_n_digits – The number of digits for the closest double.

Returns:

*this

Primal &operator+=(const Primal &t_rhs)

Adds the solution to another solution t_rhs.

Parameters:

t_rhs – The solution to add.

Returns:

*this

Primal &operator*=(double t_factor)

Multiplies every entries in the solution by t_factor.

Parameters:

t_factor – The factor for multiplication.

Returns:

*this