forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpywraplp_test.py
115 lines (97 loc) · 3.42 KB
/
pywraplp_test.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
# Copyright 2010-2018 Google LLC
# 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.
"""Simple unit tests for python/linear_solver.swig. Not exhaustive."""
import types
from ortools.linear_solver import linear_solver_pb2
from ortools.linear_solver import pywraplp
from google.protobuf import text_format
def compute_sum(arg):
if type(arg) is types.GeneratorType:
arg = [x for x in arg]
s = 0
for i in arg:
s += i
print 'sum(%s) = %d' % (str(arg), s)
def test_sum_no_brackets():
compute_sum(x for x in range(10) if x % 2 == 0)
compute_sum([x for x in range(10) if x % 2 == 0])
text_model = """
solver_type:CBC_MIXED_INTEGER_PROGRAMMING
model <
maximize:true
variable < lower_bound:1 upper_bound:10 objective_coefficient:2 >
variable < lower_bound:1 upper_bound:10 objective_coefficient:1 >
constraint < lower_bound:-10000 upper_bound:4
var_index:0
var_index:1
coefficient:1
coefficient:2
>
>
"""
def test_proto():
input_proto = linear_solver_pb2.MPModelRequest()
text_format.Merge(text_model, input_proto)
solver = pywraplp.Solver('solveFromProto',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
print input_proto
# For now, create the model from the proto by parsing the proto
errors = solver.LoadModelFromProto(input_proto.model)
assert not errors
solver.EnableOutput()
solver.Solve()
# Fill solution
solution = linear_solver_pb2.MPSolutionResponse()
solver.FillSolutionResponseProto(solution)
print solution
def test_external_api():
solver = pywraplp.Solver('TestExternalAPI',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
infinity = solver.Infinity()
infinity2 = solver.infinity()
assert infinity == infinity2
# x1, x2 and x3 are continuous non-negative variables.
x1 = solver.NumVar(0.0, infinity, 'x1')
x2 = solver.NumVar(0.0, infinity, 'x2')
x3 = solver.NumVar(0.0, infinity, 'x3')
assert x1.Lb() == 0
assert x1.Ub() == infinity
assert not x1.Integer()
solver.Maximize(10 * x1 + 6 * x2 + 4 * x3 + 5)
assert solver.Objective().Offset() == 5
c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, 'ConstraintName0')
c1 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300)
sum_of_vars = sum([x1, x2, x3])
solver.Add(sum_of_vars <= 100.0, 'OtherConstraintName')
assert c1.Lb() == -infinity
assert c1.Ub() == 300
c1.SetLb(-100000)
assert c1.Lb() == -100000
c1.SetUb(301)
assert c1.Ub() == 301
solver.SetTimeLimit(10000)
result_status = solver.Solve()
# The problem has an optimal solution.
assert result_status == pywraplp.Solver.OPTIMAL
print 'Problem solved in %f milliseconds' % solver.WallTime()
print 'Problem solved in %d iterations' % solver.Iterations()
print x1.ReducedCost()
print c0.DualValue()
def main():
test_sum_no_brackets()
# TODO(user): Support the proto API in or-tools. When this happens, re-enable
# the test below:
# test_proto()
test_external_api()
if __name__ == '__main__':
main()