-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
475 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/tutorials/mixed-integer-programming/modeling/environment.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 46 additions & 47 deletions
93
docs/tutorials/mixed-integer-programming/modeling/expressions.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,79 @@ | ||
Expressions | ||
----------- | ||
Expressions with the :code:`QuadExpr`, :code:`AffExpr` and :code:`LinExpr` Classes | ||
================================================================================== | ||
|
||
An expression generically refers to any mathematical expression involving variables or constraints. In idol, expressions | ||
are represented by the :ref:`AffExpr <api_Expr>` class. | ||
are represented by the :code:`QuadExpr`, :code:`AffExpr` and :code:`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 with constants or other expressions. For instance, | ||
An expression can be created by adding, subtracting or multiplying variables together. For instance, | ||
the following code creates the mathematical expression :math:`1 + 3 x_0 + x_1 + x_0 + 2 x_0 x_1`. | ||
|
||
.. code:: cpp | ||
const AffExpr expr = 1 + 3 * x[0] + x[1] + x[0] + 2 * x[0] * x[1]; | ||
std::cout << expr << std::endl; // "1 + 4 * x[0] + 1 * x[1] + 2 * x[0] * x[1]" | ||
std::cout << expr << std::endl; | ||
Expressions are composed of three parts: | ||
* A constant (or offset) which is an instance of the :ref:`Constant <api_Constant>` class; | ||
* A linear part which is an instance of the :ref:`LinExpr <api_LinExpr>` class; | ||
* A quadratic part which is an instance of the :ref:`QuadExpr <api_QuadExpr>` class. | ||
.. contents:: Table of Contents | ||
:local: | ||
:depth: 2 | ||
|
||
Each of these parts can be accessed using the methods :cpp:`AffExpr::constant`, :cpp:`AffExpr::linear` and :cpp:`AffExpr::quadratic`, respectively. | ||
For instance, consider the following code. | ||
:code:`LinExpr`: Linear Expressions | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
.. code-block:: cpp | ||
The :code:`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 :math:`1 + 3 x_0 + x_1`. | ||
|
||
for (const auto& [var, coefficient] : expr.linear()) { | ||
std::cout << var << " is multiplied by " << coefficient << std::endl; | ||
} | ||
.. code:: cpp | ||
This code iterates over the linear part of the expression and prints the variables and their coefficients. | ||
const LinExpr expr = 1 + 3 * x[0] + x[1]; | ||
std::cout << expr << std::endl; // "1 + 3 * x[0] + 1 * x[1]" | ||
.. admonition:: About constants in expressions | ||
.. admonition:: Linear in What? | ||
|
||
Without going into too much detail, we should here precise that each constant multiplying a variable in an :cpp:`AffExpr`, | ||
as well as the offset constant, can actually be composite. For instance, this is the case when a variable is multiplied by | ||
a parameter which is considered fixed in the current model but cannot be evaluated at the time the expression is created. | ||
Actually, :code:`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 :code:`Ctr` objects, which are constraints. The following code | ||
creates the mathematical expression :math:`1 + 3 c_0 + c_1`. | ||
|
||
Creating such "parameters" can be done by prepending a variable with the ``!`` symbol. This will automatically | ||
create an instance of the :ref:`Param <api_Param>` class. | ||
For instance, the following code creates | ||
a constant which involves the variables :math:`\xi_0` and :math:`\xi_1`, viewed as parameters. | ||
.. code:: cpp | ||
.. code-block:: | ||
const LinExpr<Ctr> expr = 1 + 3 * c[0] + c[1]; | ||
Env env; | ||
const Model model(env); | ||
const auto x = model.add_vars(Dim<1>(2), 0., Inf, Continuous, "x"); | ||
std::cout << expr << std::endl; // "1 + 3 * c[0] + 1 * c[1]" | ||
const Model model2(env); | ||
const auto xi = model2.add_vars(Dim<1>(2), 0., 1., Continuous, "xi"); | ||
It is possible to iterate over the terms in the expression as follows. | ||
|
||
const AffExpr expr = (1 + 2 * !xi[0]) * x[0] + 3 * !xi[1] * x[1]; | ||
.. code:: | ||
Here, ``1 + 2 * !xi_0`` is an instance of the ``Constant`` object and can be used as follows. | ||
for (const auto& [var, constant] : expr) { | ||
std::cout << constant << " multiplied by " << var << std::endl; | ||
} | ||
.. code-block:: | ||
:code:`AffExpr`: Affine Expressions | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Constant constant = 1 + 2 * !xi_0; | ||
The :code:`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. | ||
|
||
std::cout << constant.numerical() << std::endl; // output: 1 | ||
The linear part of the expression is accessed by the :code:`AffExpr::linear()` method, and the constant term is accessed | ||
by the :code:`AffExpr::constant()` method. | ||
|
||
for (const auto& [param, coeff] : constant) { | ||
std::cout << coeff << " * " << param << std::endl; // output: 2 * !xi_0 | ||
} | ||
:code:`QuadExpr`: Quadratic Expressions | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Note that a parameter can be turned back into a variable by calling the :cpp:`Param::as<T>` template method. | ||
See, for instance. | ||
The :code:`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 :code:`QuadExpr::affine()` method. | ||
|
||
.. code-block:: cpp | ||
It is possible to iterate over the terms in the quadratic part of the expression as follows. | ||
|
||
const auto param = !xi[0]; | ||
.. code:: | ||
if (param.is<Var>()) { | ||
const auto var = param.as<Var>(); | ||
// do somthing with the variable | ||
} | ||
for (const auto& [pair, constant] : expr) { | ||
std::cout << constant << " multiplied by " << pair.first << " and " << pair.second << std::endl; | ||
} | ||
Parameters can be variables or constraints. | ||
for (const auto& [var, constant] : expr.affine().linear()) { | ||
std::cout << constant << " multiplied by " << var << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.