polish lp code
This commit is contained in:
@@ -15,89 +15,90 @@
|
||||
# [START import]
|
||||
from __future__ import print_function
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
# [END import]
|
||||
|
||||
|
||||
# [START data_model]
|
||||
def create_data_model():
|
||||
"""Stores the data for the problem."""
|
||||
data = {}
|
||||
# Locations in block units
|
||||
data['constraint_coeffs'] = [
|
||||
[5, 7, 9, 2, 1],
|
||||
[18, 4, -9, 10, 12],
|
||||
[4, 7, 3, 8, 5],
|
||||
[5, 13, 16, 3, -7],
|
||||
]
|
||||
data['bounds'] = [250, 285, 211, 315]
|
||||
data['obj_coeffs'] = [7, 8, 2, 9, 6]
|
||||
data['num_vars'] = 5
|
||||
data['num_constraints'] = 4
|
||||
return data
|
||||
"""Stores the data for the problem."""
|
||||
data = {}
|
||||
# Locations in block units
|
||||
data['constraint_coeffs'] = [
|
||||
[5, 7, 9, 2, 1],
|
||||
[18, 4, -9, 10, 12],
|
||||
[4, 7, 3, 8, 5],
|
||||
[5, 13, 16, 3, -7],
|
||||
]
|
||||
data['bounds'] = [250, 285, 211, 315]
|
||||
data['obj_coeffs'] = [7, 8, 2, 9, 6]
|
||||
data['num_vars'] = 5
|
||||
data['num_constraints'] = 4
|
||||
return data
|
||||
|
||||
|
||||
# [END data_model]
|
||||
|
||||
|
||||
def main():
|
||||
# [START data]
|
||||
data = create_data_model()
|
||||
# [END data]
|
||||
# [START data]
|
||||
data = create_data_model()
|
||||
# [END data]
|
||||
|
||||
# [START solver]
|
||||
# MOE:begin_strip
|
||||
# Create the mip solver with the CBC backend.
|
||||
solver = pywraplp.Solver(
|
||||
'simple_mip_program',
|
||||
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
|
||||
# [END solver]
|
||||
# [START variables]
|
||||
infinity = solver.infinity()
|
||||
x = {}
|
||||
for j in range(data['num_vars']):
|
||||
x[j] = solver.IntVar(0, infinity, 'x[%i]' % j)
|
||||
print('Number of variables =', solver.NumVariables())
|
||||
# [END variables]
|
||||
|
||||
# [START constraints]
|
||||
for i in range(data['num_constraints']):
|
||||
constraint = solver.RowConstraint(0, data['bounds'][i], '')
|
||||
# [START solver]
|
||||
# Create the mip solver with the CBC backend.
|
||||
solver = pywraplp.Solver('simple_mip_program',
|
||||
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
|
||||
# [END solver]
|
||||
# [START variables]
|
||||
infinity = solver.infinity()
|
||||
x = {}
|
||||
for j in range(data['num_vars']):
|
||||
constraint.SetCoefficient(x[j], data['constraint_coeffs'][i][j])
|
||||
print('Number of constraints =', solver.NumConstraints())
|
||||
# In Python, you can also set the constraints as follows.
|
||||
# for i in range(data['num_constraints']):
|
||||
# constraint_expr = \
|
||||
# [data['constraint_coeffs'][i][j] * x[j] for j in range(data['num_vars'])]
|
||||
# solver.Add(sum(constraint_expr) <= data['bounds'][i])
|
||||
# [END constraints]
|
||||
x[j] = solver.IntVar(0, infinity, 'x[%i]' % j)
|
||||
print('Number of variables =', solver.NumVariables())
|
||||
# [END variables]
|
||||
|
||||
# [START objective]
|
||||
objective = solver.Objective()
|
||||
for j in range(data['num_vars']):
|
||||
objective.SetCoefficient(x[j], data['obj_coeffs'][j])
|
||||
objective.SetMaximization()
|
||||
# In Python, you can also set the objective as follows.
|
||||
# obj_expr = [data['obj_coeffs'][j] * x[j] for j in range(data['num_vars'])]
|
||||
# solver.Maximize(solver.Sum(obj_expr))
|
||||
# [END objective]
|
||||
# [START constraints]
|
||||
for i in range(data['num_constraints']):
|
||||
constraint = solver.RowConstraint(0, data['bounds'][i], '')
|
||||
for j in range(data['num_vars']):
|
||||
constraint.SetCoefficient(x[j], data['constraint_coeffs'][i][j])
|
||||
print('Number of constraints =', solver.NumConstraints())
|
||||
# In Python, you can also set the constraints as follows.
|
||||
# for i in range(data['num_constraints']):
|
||||
# constraint_expr = \
|
||||
# [data['constraint_coeffs'][i][j] * x[j] for j in range(data['num_vars'])]
|
||||
# solver.Add(sum(constraint_expr) <= data['bounds'][i])
|
||||
# [END constraints]
|
||||
|
||||
# [START solve]
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
# [START print_solution]
|
||||
if status == pywraplp.Solver.OPTIMAL:
|
||||
print('Objective value =', solver.Objective().Value())
|
||||
# [START objective]
|
||||
objective = solver.Objective()
|
||||
for j in range(data['num_vars']):
|
||||
print(x[j].name(), ' = ', x[j].solution_value())
|
||||
print()
|
||||
print('Problem solved in %f milliseconds' % solver.wall_time())
|
||||
print('Problem solved in %d iterations' % solver.iterations())
|
||||
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
|
||||
else:
|
||||
print('The problem does not have an optimal solution.')
|
||||
# [END print_solution]
|
||||
objective.SetCoefficient(x[j], data['obj_coeffs'][j])
|
||||
objective.SetMaximization()
|
||||
# In Python, you can also set the objective as follows.
|
||||
# obj_expr = [data['obj_coeffs'][j] * x[j] for j in range(data['num_vars'])]
|
||||
# solver.Maximize(solver.Sum(obj_expr))
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
# [START print_solution]
|
||||
if status == pywraplp.Solver.OPTIMAL:
|
||||
print('Objective value =', solver.Objective().Value())
|
||||
for j in range(data['num_vars']):
|
||||
print(x[j].name(), ' = ', x[j].solution_value())
|
||||
print()
|
||||
print('Problem solved in %f milliseconds' % solver.wall_time())
|
||||
print('Problem solved in %d iterations' % solver.iterations())
|
||||
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
|
||||
else:
|
||||
print('The problem does not have an optimal solution.')
|
||||
# [END print_solution]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
# [END program]
|
||||
|
||||
Reference in New Issue
Block a user