forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_jugs_mip.py
173 lines (141 loc) · 4.3 KB
/
3_jugs_mip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright 2011 Hakan Kjellerstrand [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
3 jugs problem using MIP in Google or-tools.
A.k.a. water jugs problem.
Problem from Taha 'Introduction to Operations Research',
page 245f .
Compare with the CP model:
http://www.hakank.org/google_or_tools/3_jugs_regular
This model was created by Hakan Kjellerstrand ([email protected])
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
"""
from __future__ import print_function
import sys
from ortools.linear_solver import pywraplp
def main(sol='CBC'):
# Create the solver.
print('Solver: ', sol)
# using GLPK
if sol == 'GLPK':
solver = pywraplp.Solver('CoinsGridGLPK',
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
else:
# Using CBC
solver = pywraplp.Solver('CoinsGridCBC',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
#
# data
#
n = 15
start = 0 # start node
end = 14 # end node
M = 999 # a large number
nodes = [
'8,0,0', # start
'5,0,3',
'5,3,0',
'2,3,3',
'2,5,1',
'7,0,1',
'7,1,0',
'4,1,3',
'3,5,0',
'3,2,3',
'6,2,0',
'6,0,2',
'1,5,2',
'1,4,3',
'4,4,0' # goal!
]
# distance
d = [[M, 1, M, M, M, M, M, M, 1, M, M, M, M, M, M],
[M, M, 1, M, M, M, M, M, M, M, M, M, M, M, M],
[M, M, M, 1, M, M, M, M, 1, M, M, M, M, M, M],
[M, M, M, M, 1, M, M, M, M, M, M, M, M, M, M],
[M, M, M, M, M, 1, M, M, 1, M, M, M, M, M, M],
[M, M, M, M, M, M, 1, M, M, M, M, M, M, M, M],
[M, M, M, M, M, M, M, 1, 1, M, M, M, M, M, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, 1],
[M, M, M, M, M, M, M, M, M, 1, M, M, M, M, M],
[M, 1, M, M, M, M, M, M, M, M, 1, M, M, M, M],
[M, M, M, M, M, M, M, M, M, M, M, 1, M, M, M],
[M, 1, M, M, M, M, M, M, M, M, M, M, 1, M, M],
[M, M, M, M, M, M, M, M, M, M, M, M, M, 1, M],
[M, 1, M, M, M, M, M, M, M, M, M, M, M, M, 1],
[M, M, M, M, M, M, M, M, M, M, M, M, M, M, M]]
#
# variables
#
# requirements (right hand statement)
rhs = [solver.IntVar(-1, 1, 'rhs[%i]' % i) for i in range(n)]
x = {}
for i in range(n):
for j in range(n):
x[i, j] = solver.IntVar(0, 1, 'x[%i,%i]' % (i, j))
out_flow = [solver.IntVar(0, 1, 'out_flow[%i]' % i) for i in range(n)]
in_flow = [solver.IntVar(0, 1, 'in_flow[%i]' % i) for i in range(n)]
# length of path, to be minimized
z = solver.Sum(
[d[i][j] * x[i, j] for i in range(n) for j in range(n) if d[i][j] < M])
#
# constraints
#
for i in range(n):
if i == start:
solver.Add(rhs[i] == 1)
elif i == end:
solver.Add(rhs[i] == -1)
else:
solver.Add(rhs[i] == 0)
# outflow constraint
for i in range(n):
solver.Add(
out_flow[i] == solver.Sum([x[i, j] for j in range(n) if d[i][j] < M]))
# inflow constraint
for j in range(n):
solver.Add(
in_flow[j] == solver.Sum([x[i, j] for i in range(n) if d[i][j] < M]))
# inflow = outflow
for i in range(n):
solver.Add(out_flow[i] - in_flow[i] == rhs[i])
# objective
objective = solver.Minimize(z)
#
# solution and search
#
solver.Solve()
print()
print('z: ', int(solver.Objective().Value()))
t = start
while t != end:
print(nodes[t], '->', end=' ')
for j in range(n):
if x[t, j].SolutionValue() == 1:
print(nodes[j])
t = j
break
print()
print('walltime :', solver.WallTime(), 'ms')
if sol == 'CBC':
print('iterations:', solver.Iterations())
if __name__ == '__main__':
sol = 'CBC'
if len(sys.argv) > 1:
sol = sys.argv[1]
if sol != 'GLPK' and sol != 'CBC':
print('Solver must be either GLPK or CBC')
sys.exit(1)
main(sol)