Expressions

An expression generically refers to any mathematical expression involving variables or constraints. In idol, expressions are represented by the QuadExpr, AffExpr and LinExpr classes. These classes are used to represent quadratic, affine and linear expressions, respectively.

An expression can be created by adding, subtracting or multiplying variables together. For instance, the following code creates the mathematical expression \(1 + 3 x_0 + x_1 + x_0 + 2 x_0 x_1\).

const AffExpr expr = 1 + 3 * x[0] + x[1] + x[0] + 2 * x[0] * x[1];

std::cout << expr << std::endl;

LinExpr: Linear Expressions

The LinExpr class is used to represent linear expressions. A linear expression is a mathematical expression that is linear in the variables. For instance, the following code creates the mathematical expression \(1 + 3 x_0 + x_1\).

const LinExpr expr = 1 + 3 * x[0] + x[1];

std::cout << expr << std::endl; // "1 + 3 * x[0] + 1 * x[1]"

Linear in What?

Actually, LinExpr is a template class with one parameter, which is the type of the “variables” in the linear expression. For instance, it is possible de create a linear expression of Ctr objects, which are constraints. The following code creates the mathematical expression \(1 + 3 c_0 + c_1\).

const LinExpr<Ctr> expr = 1 + 3 * c[0] + c[1];

std::cout << expr << std::endl; // "1 + 3 * c[0] + 1 * c[1]"

It is possible to iterate over the terms in the expression as follows.

for (const auto& [var, constant] : expr) {
    std::cout << constant << " multiplied by " << var << std::endl;
}

AffExpr: Affine Expressions

The AffExpr class is used to represent affine expressions. An affine expression is a mathematical expression that is linear in the variables and has a constant term.

The linear part of the expression is accessed by the AffExpr::linear() method, and the constant term is accessed by the AffExpr::constant() method.

QuadExpr: Quadratic Expressions

The QuadExpr class is used to represent quadratic expressions. A quadratic expression is a mathematical expression which contains an affine part and a quadratic part. The affine part can be accessed by the QuadExpr::affine() method.

It is possible to iterate over the terms in the quadratic part of the expression as follows.

for (const auto& [pair, constant] : expr) {
    std::cout << constant << " multiplied by " << pair.first << " and " << pair.second << std::endl;
}

for (const auto& [var, constant] : expr.affine().linear()) {
    std::cout << constant << " multiplied by " << var << std::endl;
}