Utils

namespace Utils

Functions

arma::mat resizePatch(const arma::mat &mat, const unsigned int nR, const unsigned int nC)

Armadillo arma::sp_mat::resize() is not robust as it initializes garbage values to new columns. This fixes the problem by creating new columns with guaranteed zero values. For arma::sp_mat.

Parameters
  • mat – Input matrix

  • nR – Number of rows for the output

  • nC – Number of columns for the output

Returns

The resized mat

arma::vec resizePatch(const arma::vec &mat, const unsigned int nR)

Armadillo arma::mat::resize() is not robust as it initializes garbage values to new columns. This fixes the problem by creating new columns with guaranteed zero values. For arma::sp_mat.

Parameters
  • mat – Input vector (row, or column)

  • nR – Number of rows for the output

Returns

The resized mat

bool containsRow(const arma::sp_mat &A, const arma::vec &row, const double tol = 1e-6)

Check if A contains a row row.

Parameters
  • A – The input matrix

  • row – The input row

  • tol – A numerical tolerance

Returns

True if the row was found

bool containsElement(const arma::vec &b, const double &element, const double tol = 1e-6)

Check if the vector b contains element.

Parameters
  • b – The vector containing the elements to be checked

  • element – The input element

  • tol – A numerical tolerance

Returns

True if the element was found

bool containsConstraint(const arma::sp_mat &A, const arma::vec &b, const arma::vec &lhs, const double &rhs, const double tol = 1e-6)

Given a matrix A and a vector of LHSs b, checks if both lhs is in A, and rhs is in b up to a tolerance of tol.

Parameters
  • A – The matrix containing the constraints

  • b – The vector containing the RHSs

  • lhs – The input cut RHS

  • rhs – The input cut LHS

  • tol – A numerical tolerance

Returns

True if the constraint was found

bool containsConstraint(const arma::sp_mat &A, const arma::vec &b, const arma::sp_mat &lhs, const double &rhs, const double tol = 1e-6)

Given a matrix A and a vector of LHSs b, checks if both lhs is in A, and rhs is in b up to a tolerance of tol.

Parameters
  • A – The matrix containing the constraints

  • b – The vector containing the RHSs

  • lhs – The input cut RHS (in a matrix form)

  • rhs – The input cut LHS

  • tol – A numerical tolerance

Returns

True if the constraint was found

void appendSave(const arma::vec &matrix, const std::string &out, const std::string &header = "", bool erase = false)

Utility to append an arma::vec to a data file.

Parameters
  • matrix – The arma::vec to be saved

  • out – The output file

  • header – An optional header

  • erase – Erase the file?

long int appendRead(arma::vec &matrix, const std::string &in, long int pos, const std::string &header = "")

Utility to read an arma::vec from a long file.

Parameters
  • matrix – The output matrix

  • in – The input file

  • pos – The position of matrix in the file

  • header – An optional header

Returns

The end position from which the next data object can be read.

void appendSave(const arma::sp_mat &matrix, const std::string &out, const std::string &header = "", bool erase = false)

Utility to append an arma::sp_mat to a data file.

Parameters
  • matrix – The arma::sp_mat to be saved

  • out – File name of the output file

  • header – A header that might be used to check the filetype

  • erase – Should the matrix be appended to the file or not?

long int appendRead(arma::sp_mat &matrix, const std::string &in, long int pos, const std::string &header = "")

Utility to read an arma::sp_mat from a long file.

Parameters
  • matrix – Read and store the solution in this matrix.

  • in – File to read from (could be file very many matrices are appended one below another)

  • pos – Position in the long file where reading should start

  • header – Any header to check data sanctity

Returns

The end position from which the next data object can be read.

void appendSave(const std::vector<double> v, const std::string out, const std::string header = "", bool erase = false)
long int appendRead(std::vector<double> &v, const std::string in, long int pos, const std::string header = "")
void appendSave(const std::string &v, const std::string &out, bool erase = false)

Utility to write a string to a file.

Parameters
  • v – The string to be saved

  • out – The output file

  • erase – Should the file be erased?

long int appendRead(std::string &v, const std::string &in, long int pos)

Utility to read a std::string from a long file.

Parameters
  • v – The output string

  • in – The input file

  • pos – The position of v

Returns

The end position from which the next data object can be read.

void appendSave(const long int v, const std::string &out, const std::string &header = "", bool erase = false)

Utility to save a long int to file.

Parameters
  • v – The int

  • out – The output file

  • header – An optional header

  • erase – Overwrite the file?

long int appendRead(long int &v, const std::string &in, long int pos, const std::string &header = "")

Utility to read a long int from a file.

Parameters
  • v – The output number

  • in – The input file

  • pos – The position of v in the file

  • header – An optional header check

Returns

The end position from which the next data object can be read.

void appendSave(const unsigned int v, const std::string &out, const std::string &header = "", bool erase = false)

Utility to save an unsigned int to file.

Parameters
  • v – The long int to be saved

  • out – The output file

  • header – An optional header

  • erase – Should the file be erased?

long int appendRead(unsigned int &v, const std::string &in, long int pos, const std::string &header = "")

An utility to read an unsigned int from a file.

Parameters
  • v – The output number

  • in – The input file

  • pos – The position of the data in the file

  • header – An optional header

Returns

The end position from which the next data object can be read.

bool isZero(const arma::mat &M, double tol = 1e-6) noexcept

Checks if a matrix is a zero matrix.

Parameters
  • M – The input matrix

  • tol – A numerical tolerance

Returns

True if all elements are zero

bool isZero(const arma::sp_mat &M, double tol = 1e-6) noexcept

Checks if a matrix is a zero matrix. Optimized for sparse matrices.

Parameters
  • M – The input matrix

  • tol – A numerical tolerance

Returns

True if all elements are zero

void sortByKey(perps &P)

Sort the perps by the first element (key)

Parameters

set – The input perps

VariableBounds intersectBounds(const VariableBounds &bA, const VariableBounds &bB)

Given two VariableBounds, it returns the strictest intersection of them.

Parameters
  • bA – First input bound vector

  • bB – Second input bound vector

Returns

The intersection

CoinPackedMatrix armaToCoinSparse(const arma::sp_mat &A)

Given an arma::sp_mat A, returns a CoinPackedMatrix. A The armadillo sparse matrix.

Returns

A CoinPackedMatrix from A

std::vector<CoinPackedVector> armaToCoinPackedVector(const arma::sp_mat &A)

Given an arma::sp_mat A, returns an array of CoinPackedVector(s). A The armadillo sparse matrix.

Returns

A CoinPackedVector arary from A

std::string printBounds(const VariableBounds &bounds)

Given bounds, provides a string with the readable output.

Parameters

bounds – The input bounds

Returns

A readable output for the bounds

arma::vec normalizeVec(const arma::vec &v)

Normalizes a vector according to the “equilibrium normalization”. Namely, we divide for the largest absolute value among the elements of the vector. v is the input vector

Returns

The normalized vector

void normalizeIneq(arma::vec &lhs, double &rhs, bool force)

Normalizes an inequality according to the “equilibrium normalization”. Namely, we divide for the largest absolute value among the elements of the lhs and the rhs. If the ration between the largest non-zero and the smallest non-zero (in abs) is greater than 1e2, we normalize with the former. rhs is the input and output RHS value lhs is the input and output LHS

double round_nplaces(const double &value, const int &numDecimals = 5)

Round the input value to a decimal up to numDecimals.

Parameters
  • value – The input number

  • numDecimals – Decimal precision tolerance

Returns

The rounded value

void addSparseConstraints(const arma::sp_mat &A, const arma::vec &b, GRBVar *x, const std::string &basename, GRBModel *model, int sense, GRBVar *z)

Create constraints for a given model given the matrix A, the RHS vector b, the variables x, and an additional RHS of variables z. The resulting constraints read: \(Ax \quad (sense) \quad b+z\)

Parameters
  • A – The input sparse matrix of LHS

  • b – The input vector of RHS

  • x – The input variables

  • basename – The basename of these constraints

  • model – A pointer to the model

  • sense – As in Gurobi.

  • z – Additional RHS of variables

arma::sp_mat resizePatch(const arma::sp_mat &mat, const unsigned int nR, const unsigned int nC)

Armadillo arma::sp_mat::resize() is not robust as it initializes garbage values to new columns. This fixes the problem by creating new columns with guaranteed zero values. For arma::sp_mat.

Parameters
  • mat – Input matrix

  • nR – Number of rows for the output

  • nC – Number of columns for the output

Returns

The resized mat

int vecToBin(const arma::vec &x)

Convert a binary vector (0-1 entries) into an unique int.

Parameters

x – The input vector

Returns

The int encoding

int nonzeroDecimals(const double num, const int decimalBound = 6)

Given a number num and a bound decimalBound, counts the number of non-zero decimals up to decimalBound.

Parameters
  • num – The input number

  • decimalBound – The maximal bound on decimals to be counted

Returns

The number of non-zero decimals

bool isEqualAbs(const double a, const double b, const double tol = 1e-6)

Checks if two numbers are equal in absolute tolerance.

Parameters
  • a – The first number

  • b – The second number

  • tol – An absolute tolerance

Returns

True if the two numbers are equal up to the given tolerance

bool isEqualRel(const double a, const double b, const double percent = (1 - 1e-4))

Checks if two numbers are equal in relative tolerance.

Parameters
  • a – The first number

  • b – The second number

  • percent – Percent of similarity (e.g., 1 and 0.99 are similar at least at 0.99)

Returns

True if the two numbers are equal up to the given tolerance

bool isEqual(const double a, const double b, const double tol = 1e-6, const double percent = (1 - 1e-4))

Checks if two numbers are equal in either absolute or relative tolerance.

Parameters
  • a – The first number

  • b – The second number

  • tol – An absolute tolerance

  • percent – Percent of similarity (e.g., 1 and 0.99 are similar at least at 0.99)

Returns

arma::sp_mat clearMatrix(const arma::sp_mat &A, double tol = 1e-6, double percent = 1 - 1e-6)

Given an arma::sp_mat A, returns the same matrix where the entries within a relative and absolute tolerance tol percent are set to zero A The armadillo sparse matrix tol The absolute tolerance percent The relative tolerance.

Returns

The new refactored matrix

arma::vec clearVector(const arma::vec &b, double tol = 1e-6, double percent = 1 - 1e-6)

Given an arma::vec A, returns the same vector where the entries within a relative and absolute tolerance tol percent are set to zero b The armadillo vector tol The absolute tolerance percent The relative tolerance.

Returns

The new refactored matrix

int getSign(double val)

Gets the sign.

Parameters

val – Input data

Returns

Either -1 or 1