| 1 |
.. _basis-basic-systems: |
| 2 |
|
| 3 |
Constraints definition and system solving |
| 4 |
========================================= |
| 5 |
|
| 6 |
.. include:: ../substitutions.sub |
| 7 |
|
| 8 |
A system within Kiwi is defined by a set of constraints, which may |
| 9 |
be either equalities or inequalities (limited to >= and <=, as strict inequalities |
| 10 |
are not supported). Each constraint can be assigned a specific 'strength', |
| 11 |
indicating its relative importance in the problem-solving process. The subsequent |
| 12 |
sections will delve into the methods of defining these constraints and extracting |
| 13 |
results from the solver. |
| 14 |
|
| 15 |
Defining variables and constraints |
| 16 |
---------------------------------- |
| 17 |
|
| 18 |
The initial step involves defining variables, which represent |
| 19 |
the values that the solver aims to determine. These variables are |
| 20 |
encapsulated by |Variable| objects. The creation of these objects |
| 21 |
can be accomplished as follows: |
| 22 |
|
| 23 |
.. tabs:: |
| 24 |
|
| 25 |
.. code-tab:: python |
| 26 |
|
| 27 |
from kiwisolver import Variable |
| 28 |
|
| 29 |
x1 = Variable('x1') |
| 30 |
x2 = Variable('x2') |
| 31 |
xm = Variable('xm') |
| 32 |
|
| 33 |
.. code-tab:: c++ |
| 34 |
|
| 35 |
#include <kiwi/kiwi.h> |
| 36 |
|
| 37 |
using namespace kiwi |
| 38 |
|
| 39 |
Variable x1("x1"); |
| 40 |
Variable x2("x2"); |
| 41 |
Variable xm("xm"); |
| 42 |
|
| 43 |
.. note:: |
| 44 |
|
| 45 |
Naming your variables is not mandatory but it is recommended since it will |
| 46 |
help the solver in providing more meaningful error messages. |
| 47 |
|
| 48 |
Now that we have some variables we can define our constraints. |
| 49 |
|
| 50 |
.. tabs:: |
| 51 |
|
| 52 |
.. code-tab:: python |
| 53 |
|
| 54 |
constraints = [x1 >= 0, x2 <= 100, x2 >= x1 + 10, xm == (x1 + x2) / 2] |
| 55 |
|
| 56 |
.. code-tab:: c++ |
| 57 |
|
| 58 |
Constraint constraints[] = { Constraint {x1 >= 0}, |
| 59 |
Constraint {x2 <= 100}, |
| 60 |
Constraint {x2 >= x1 + 20}, |
| 61 |
Constraint {xm == (x1 + x2) / 2} |
| 62 |
}; |
| 63 |
|
| 64 |
Next, add these variables to the solver, an instance of |Solver|: |
| 65 |
|
| 66 |
.. tabs:: |
| 67 |
|
| 68 |
.. code-tab:: python |
| 69 |
|
| 70 |
from kiwisolver import Solver |
| 71 |
|
| 72 |
solver = Solver() |
| 73 |
|
| 74 |
for cn in constraints: |
| 75 |
solver.addConstraint(cn) |
| 76 |
|
| 77 |
.. code-tab:: c++ |
| 78 |
|
| 79 |
Solver solver; |
| 80 |
|
| 81 |
for(auto& constraint : constraints) |
| 82 |
{ |
| 83 |
solver.addConstraint(constraint); |
| 84 |
} |
| 85 |
|
| 86 |
.. note:: |
| 87 |
|
| 88 |
You can start adding constraints to the solver without creating all your |
| 89 |
variables first. |
| 90 |
|
| 91 |
|
| 92 |
So far, we have defined a system representing three points on the segment |
| 93 |
[0, 100], with one of them being the middle of the others, which cannot get |
| 94 |
closer than 10. All those constraints have to be satisfied; in the context |
| 95 |
of Cassowary, they are required constraints. |
| 96 |
|
| 97 |
.. note:: |
| 98 |
|
| 99 |
Cassowary (and Kiwi) allows for redundant constraints, which means |
| 100 |
even with two constraints (x == 10, x + y == 30) being equivalent to a |
| 101 |
third one (y == 20), all three can be added to the solver without issues. |
| 102 |
|
| 103 |
However, it is advisable not to add the same constraint multiple times |
| 104 |
in the same form to the solver. |
| 105 |
|
| 106 |
|
| 107 |
Managing constraints strength |
| 108 |
----------------------------- |
| 109 |
|
| 110 |
Cassowary also supports constraints that are not required. Those are only |
| 111 |
respected on a best effort basis. To express that a constraint is not required |
| 112 |
we need to assign it a *strength*. Kiwi specifies three standard strengths |
| 113 |
besides the "required" strength: strong, medium, weak. A strong constraint |
| 114 |
will always win over a medium constraint, which in turn will always override |
| 115 |
a weak constraint [#f1]_ . |
| 116 |
|
| 117 |
In our example, let's assume x1 would like to be at 40, without this being a |
| 118 |
requirement. This is translated as follows: |
| 119 |
|
| 120 |
.. tabs:: |
| 121 |
|
| 122 |
.. code-tab:: python |
| 123 |
|
| 124 |
solver.addConstraint((x1 == 40) | "weak") |
| 125 |
|
| 126 |
.. code-tab:: c++ |
| 127 |
|
| 128 |
solver.addConstraint(x1 == 40 | strength::weak); |
| 129 |
|
| 130 |
|
| 131 |
Adding edit variables |
| 132 |
--------------------- |
| 133 |
|
| 134 |
So far our system is pretty static; we have no way of trying to find solutions |
| 135 |
for a particular value of `xm`, let's say. This is a problem. In a real |
| 136 |
application (e.g. a GUI layout), we would like to find the size of the widgets |
| 137 |
based on the top window but also react to the window resizing, so actually |
| 138 |
adding and removing constraints all the time wouldn't be optimal. And there is |
| 139 |
a better way: edit variables. |
| 140 |
|
| 141 |
Edit variables are variables for which you can suggest values. Edit variables |
| 142 |
have a strength which can be at most strong (the value of a edit variable can |
| 143 |
never be required). |
| 144 |
|
| 145 |
For the sake of our example, we will make "xm" editable: |
| 146 |
|
| 147 |
.. tabs:: |
| 148 |
|
| 149 |
.. code-tab:: python |
| 150 |
|
| 151 |
solver.addEditVariable(xm, 'strong') |
| 152 |
|
| 153 |
.. code-tab:: c++ |
| 154 |
|
| 155 |
solver.addEditVariable(xm, strength::strong); |
| 156 |
|
| 157 |
Once a variable has been added as an edit variable, you can suggest a value for |
| 158 |
it and the solver will try to solve the system with it. |
| 159 |
|
| 160 |
.. tabs:: |
| 161 |
|
| 162 |
.. code-tab:: python |
| 163 |
|
| 164 |
solver.suggestValue(xm, 60) |
| 165 |
|
| 166 |
.. code-tab:: c++ |
| 167 |
|
| 168 |
solver.suggestValue(xm, 60); |
| 169 |
|
| 170 |
This would give the following solution: ``xm == 60, x1 == 40, x2 == 80``. |
| 171 |
|
| 172 |
|
| 173 |
Solving and updating variables |
| 174 |
------------------------------ |
| 175 |
|
| 176 |
Kiwi solves the system each time a constraint is added or removed, or a new |
| 177 |
value is suggested for an edit variable. Solving the system each time makes for |
| 178 |
faster updates and allows to keep the solver in a consinstent state. However, |
| 179 |
the variable values are not updated automatically, and you need to ask |
| 180 |
the solver to perform this operation before reading the values, as illustrated |
| 181 |
below: |
| 182 |
|
| 183 |
.. tabs:: |
| 184 |
|
| 185 |
.. code-tab:: python |
| 186 |
|
| 187 |
solver.suggestValue(xm, 90) |
| 188 |
solver.updateVariables() |
| 189 |
print(xm.value(), x1.value(), x2.value()) |
| 190 |
|
| 191 |
.. code-tab:: c++ |
| 192 |
|
| 193 |
solver.suggestValue(xm, 90); |
| 194 |
solver.updateVariables(); |
| 195 |
std::cout << xm.value() << ", " << x1.value() << ", " << x2.value(); |
| 196 |
|
| 197 |
This last update creates an infeasible situation by pushing x2 further than |
| 198 |
100, if we keep x1 where it would like to be. As a consequence, we get the |
| 199 |
following solution: ``xm == 90, x1 == 80, x2 == 100`` |
| 200 |
|
| 201 |
|
| 202 |
.. note:: |
| 203 |
|
| 204 |
To determine if a non-required constraint was violated when solving the system, |
| 205 |
you can use the constraint's ``violated`` method. |
| 206 |
|
| 207 |
.. versionadded:: 1.4 |
| 208 |
|
| 209 |
Footnotes |
| 210 |
--------- |
| 211 |
|
| 212 |
.. [#f1] Actually, there are some corner cases in which this can be violated. |
| 213 |
See :ref:`basics-internals` |