Files

61 lines
1.4 KiB
Python
Raw Permalink Normal View History

2021-12-07 09:13:09 +01:00
#!/usr/bin/env python3
2014-02-19 18:53:31 +00:00
from google.protobuf import text_format
from ortools.linear_solver import pywraplp
from ortools.linear_solver import linear_solver_pb2
2014-02-19 18:53:31 +00:00
import sys
import types
2014-05-22 20:13:16 +00:00
def Sum(arg):
2024-04-23 17:43:37 +02:00
if type(arg) is types.GeneratorType:
arg = [x for x in arg]
sum = 0
for i in arg:
sum += i
print("sum(%s) = %d" % (str(arg), sum))
2014-05-22 20:13:16 +00:00
def test_sum_no_brackets():
2024-04-23 17:43:37 +02:00
Sum(x for x in range(10) if x % 2 == 0)
Sum([x for x in range(10) if x % 2 == 0])
2014-02-19 18:53:31 +00:00
text_model = """
2022-11-12 12:04:50 +01:00
solver_type:GLOP_LINEAR_PROGRAMMING
2014-02-19 18:53:31 +00:00
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
2014-06-13 07:27:24 +00:00
var_index:0
var_index:1
coefficient:1
coefficient:2
2014-02-19 18:53:31 +00:00
>
>
"""
2014-05-22 20:13:16 +00:00
2014-02-19 18:53:31 +00:00
def test_proto():
2024-04-23 17:43:37 +02:00
input_proto = linear_solver_pb2.MPModelRequest()
text_format.Merge(text_model, input_proto)
solver = pywraplp.Solver.CreateSolver("glop")
print(input_proto)
# For now, create the model from the proto by parsing the proto
solver.LoadModelFromProto(input_proto.model)
solver.EnableOutput()
solver.Solve()
# Fill solution
solution = linear_solver_pb2.MPSolutionResponse()
solver.FillSolutionResponseProto(solution)
print(solution)
2014-02-19 18:53:31 +00:00
def main():
2024-04-23 17:43:37 +02:00
test_sum_no_brackets()
test_proto()
2024-04-23 17:43:37 +02:00
if __name__ == "__main__":
main()