Debian Codegraph @

doc/source/guides/how_to_mip_start.rst

raw
1 How to warm-start a solver
2 ======================================
3
4 Many solvers permit the possibility of giving a valid (or parcially valid in some cases) solution so the solver can start from that solution. This can lead to performance gains.
5
6
7 Supported solver APIs
8 -----------------------
9
10 The present solver APIs that work with PuLP warm-start are the following: ``CPLEX_CMD``, ``GUROBI_CMD``, ``PULP_CBC_CMD``, ``CBC_CMD``, ``CPLEX_PY``, ``GUROBI``, ``XPRESS``, ``XPRESS_PY``.
11
12 Example problem
13 ----------------
14
15 We will use as example the model in :ref:`set-partitioning-problem`. At the end is the complete modified code.
16
17
18 Filling a variable with a value
19 --------------------------------
20
21 If a model has been previously solved, each variable has already a value. To check the value of a variable we can do it via the ``value`` method of the variable.
22
23 In our example, if we solve the problem, we could just do the following afterwards:
24
25 .. code-block:: python
26
27 x[('O', 'P', 'Q', 'R')].value() # 1.0
28 x[('K', 'N', 'O', 'R')].value() # 0.0
29
30 If we have not yet solved the model, we can use the ``setInitialValue`` method to assign a value to the variable.
31
32 In our example, if we want to get those two same values, we would do the following:
33
34 .. code-block:: python
35
36 x[('O', 'P', 'Q', 'R')].setInitialValue(1)
37 x[('K', 'N', 'O', 'R')].setInitialValue(0)
38
39
40 Activating MIP start
41 ---------------------
42
43 Once we have assigned values to all variables and we want to run a model while reusing those values, we just need to pass the ``warmStart=True`` argument to the solver when initiating it.
44
45 For example, using the default PuLP solver we would do:
46
47 .. code-block:: python
48
49 seating_model.solve(pulp.PULP_CBC_CMD(msg=True, warmStart=True))
50
51 I usually turn ``msg=True`` so I can see the messages from the solver confirming it loaded the solution correctly.
52
53 Fixing a variable
54 -------------------
55
56 Assigning values to variables also permits fixing those variables to that value. In order to do that, we use the ``fixValue`` method of the variable.
57
58 For our example, if we know some variable needs to be 1, we can do:
59
60
61 .. code-block:: python
62
63 _variable = x[('O', 'P', 'Q', 'R')]
64 _variable.setInitialValue(1)
65 _variable.fixValue()
66
67 This implies setting the lower bound and the upperbound to the value of the variable.
68
69
70 Whole Example
71 -------------
72
73 If you want to see the complete code of the warm start version of the example, :download:`click here <../../../examples/wedding_initial.py>` or see below.
74
75 .. literalinclude:: ../../../examples/wedding_initial.py