Debian Codegraph @

docs/source/basis/solver_internals.rst

raw
1 .. _basics-internals:
2
3 Solver internals and tips
4 =========================
5
6 .. include:: ../substitutions.sub
7
8
9 Kiwi is not a mere rewriting of Cassowary, and due to this, it does not always
10 perfectly reflect the original implementation. The following sections point out
11 these discrepancies and provide tips on how to work effectively with Kiwi.
12
13
14 Inspecting the solver state
15 ---------------------------
16
17 The state of the solver can be inspected by dumping a text representation of
18 its state either to stdout using the ``dump`` method of the solver, or to a
19 string using the ``dumps`` method. Typically, at least a basic understanding of
20 the Cassowary algorithm is necessary to analyse the output.
21
22 A typical output is reproduced below:
23
24
25 .. code::
26
27 Objective
28 ---------
29 -2 + 2 * e2 + 1 * s8 + -2 * s10
30
31 Tableau
32 -------
33 v1 | 1 + 1 * s10
34 e3 | -1 + 1 * e2 + -1 * s10
35 v4 | -1 + -1 * d5 + -1 * s10
36 s6 | -2 + -1 * s10
37 e9 | -1 + 1 * s8 + -1 * s10
38
39 Infeasible
40 ----------
41 e3
42 e9
43
44 Variables
45 ---------
46 bar = v1
47 foo = v4
48
49 Edit Variables
50 --------------
51 bar
52
53 Constraints
54 -----------
55 1 * bar + -0 >= 0 | strength = 1
56 1 * bar + 1 <= 0 | strength = 1.001e+09
57 1 * foo + 1 * bar + 0 == 0 | strength = 1.001e+09
58 1 * bar + 0 == 0 | strength = 1
59
60 In the dump, the letters have the following meaning:
61
62 - v: external variable, corresponds to the variable created by you, the user
63 - s: slack symbol, used to represent inequalities
64 - e: error symbol, used to represent non-required constraints
65 - d: dummy variable, always zero, used to keep track of the impact of an
66 external variable in the tableau.
67 - i: invalid symbol, returned when no valid symbol can be found.
68
69
70 Stay constraints emulation
71 --------------------------
72
73 One feature of Cassowary that Kiwi abandoned is the notion of stay
74 constraints. Stay constraints are typically used in under-constrained
75 situations (drag and drop) to allow the solver to find a solution by keeping
76 non-modified variable close to their original position. A typical example is
77 a rectangle whose one corner is being dragged in a drawing application.
78
79 Kiwi does not have stay constraints mostly because in the context of widget
80 placement, the system is typically well constrained, rendering stay constraints
81 unnecessary.
82
83 If your application requires them, several workarounds can be considered:
84
85 - adding/removing non-required equality constraints to mimic stay constraints
86 - using edit-variables to mimic stay constraints
87
88 The first method will require to remove the old constraints as soon as they
89 stop making sense, while the second will require to update the suggested value.
90
91
92 Creating strengths and their internal representation
93 ----------------------------------------------------
94
95 Kiwi provides three strengths in addition to the required strength by default:
96 "weak", "medium", and "strong". Contrary to Cassowary, which uses lexicographic
97 ordering to ensure that strength are always respected, Kiwi strives for speed
98 and uses simple floating point numbers.
99
100 .. note::
101
102 Using simple floating point, means that in some rare corner cases, a large
103 number of weak constraints may outweigh a medium constraint. However, in
104 practice, this rarely happens.
105
106 Kiwi allows to create custom strengths in the following manner:
107
108 .. tabs::
109
110 .. code-tab:: python
111
112 from kiwisolver import strength
113
114 my_strength = strength.create(1, 1, 1)
115 my_strength2 = strength.create(1, 1, 1, 2)
116
117 .. code-tab:: c++
118
119 double my_strength = strength::create(1, 1, 1);
120 double my_strength = strength::create(1, 1, 1, 2);
121
122 The first argument is multiplied by 1 000 000, the second argument by 1 000,
123 and the third by 1. No strength can be create larger than the required
124 strength. The default strengths in Kiwi correspond to:
125
126 .. code:: python
127
128 weak = strength.create(0, 0, 1)
129 medium = strength.create(0, 1, 0)
130 strong = strength.create(1, 0, 0)
131 required = strength.create(1000, 1000, 1000)
132
133 While Cassowary differentiates between strength and weight, those two concepts
134 are fused in Kiwi: when creating a strength, one can apply a weight (the fourth
135 argument) that will multiply it.
136
137 .. note::
138
139 Because strengths are implemented as floating point numbers, in order to be
140 effective, strengths must be different enough from one another. The
141 following is unlikely to produce any really useful result.
142
143 .. code:: python
144
145 weak1 = strength.create(0, 0, 1)
146 weak2 = strength.create(0, 0, 2)
147 weak3 = strength.create(0, 0, 3)
148
149
150 Managing memory
151 ---------------
152
153 When removing a constraint, Kiwi does not check whether or not the variables
154 used in the constraint are still in use in other constraints. This is mostly
155 because such checks could be quite expensive. However, this means the map of
156 variables can grow over time.
157
158 To avoid this possibly causing large memory leaks, it is recommended to reset
159 the solver state (using the method of the same name) and to add back the
160 constraints, that are still valid at this point.
161
162
163 Representation of constraints
164 -----------------------------
165
166 If you browse through the API documentation you may notice a number of classes
167 that do not appear anywhere in this documentation: Term and Expression.
168
169 Those classes are used internally in constraints and are created automatically
170 by the library. A |Term| represents a variable/symbol and the coefficient that
171 multiplies it, |Expression| represents a sum of terms and a constant value and
172 is used as the left hand side of a constraint.
173
174
175 Performance implementation tricks
176 ---------------------------------
177
178 Map type
179 ^^^^^^^^
180
181 Kiwi uses maps to represent the state of the solver and to manipulate it. As a
182 consequence, the map type should be fast, with a particular emphasis on
183 iteration. The C++ standard library provides unordered_map and map that could
184 be used in kiwi, but none of those are very friendly to the CPU cache. For
185 this reason, Kiwi uses the AssocVector class implemented in Loki (slightly
186 updated to respect c++11 standards). The use of this class provides a 2x
187 speedup over std::map.
188
189
190 Symbol representation
191 ^^^^^^^^^^^^^^^^^^^^^
192
193 Symbols are used in Kiwi to represent the state of the solver. Since solving the
194 system requires a large number of manipulations of the symbols, the operations
195 have to compile down to an efficient representation. In Kiwi, symbols compile
196 down to long long meaning that a vector of them fits in a CPU cache line.