reformat python files as tab length = 4 now

This commit is contained in:
Laurent Perron
2018-11-11 09:39:59 +01:00
parent 987174f8a4
commit d99cf97acb
67 changed files with 6149 additions and 5709 deletions

View File

@@ -16,121 +16,124 @@ from ortools.linear_solver import pywraplp
def RunIntegerExampleNaturalLanguageAPI(optimization_problem_type):
"""Example of simple integer program with natural language API."""
solver = pywraplp.Solver('RunIntegerExampleNaturalLanguageAPI',
optimization_problem_type)
infinity = solver.infinity()
# x1 and x2 are integer non-negative variables.
x1 = solver.IntVar(0.0, infinity, 'x1')
x2 = solver.IntVar(0.0, infinity, 'x2')
"""Example of simple integer program with natural language API."""
solver = pywraplp.Solver('RunIntegerExampleNaturalLanguageAPI',
optimization_problem_type)
infinity = solver.infinity()
# x1 and x2 are integer non-negative variables.
x1 = solver.IntVar(0.0, infinity, 'x1')
x2 = solver.IntVar(0.0, infinity, 'x2')
solver.Minimize(x1 + 2 * x2)
solver.Add(3 * x1 + 2 * x2 >= 17)
solver.Minimize(x1 + 2 * x2)
solver.Add(3 * x1 + 2 * x2 >= 17)
SolveAndPrint(solver, [x1, x2])
SolveAndPrint(solver, [x1, x2])
def RunIntegerExampleCppStyleAPI(optimization_problem_type):
"""Example of simple integer program with the C++ style API."""
solver = pywraplp.Solver('RunIntegerExampleCppStyleAPI',
optimization_problem_type)
infinity = solver.infinity()
# x1 and x2 are integer non-negative variables.
x1 = solver.IntVar(0.0, infinity, 'x1')
x2 = solver.IntVar(0.0, infinity, 'x2')
"""Example of simple integer program with the C++ style API."""
solver = pywraplp.Solver('RunIntegerExampleCppStyleAPI',
optimization_problem_type)
infinity = solver.infinity()
# x1 and x2 are integer non-negative variables.
x1 = solver.IntVar(0.0, infinity, 'x1')
x2 = solver.IntVar(0.0, infinity, 'x2')
# Minimize x1 + 2 * x2.
objective = solver.Objective()
objective.SetCoefficient(x1, 1)
objective.SetCoefficient(x2, 2)
# Minimize x1 + 2 * x2.
objective = solver.Objective()
objective.SetCoefficient(x1, 1)
objective.SetCoefficient(x2, 2)
# 2 * x2 + 3 * x1 >= 17.
ct = solver.Constraint(17, infinity)
ct.SetCoefficient(x1, 3)
ct.SetCoefficient(x2, 2)
# 2 * x2 + 3 * x1 >= 17.
ct = solver.Constraint(17, infinity)
ct.SetCoefficient(x1, 3)
ct.SetCoefficient(x2, 2)
SolveAndPrint(solver, [x1, x2])
SolveAndPrint(solver, [x1, x2])
def SolveAndPrint(solver, variable_list):
"""Solve the problem and print the solution."""
print(('Number of variables = %d' % solver.NumVariables()))
print(('Number of constraints = %d' % solver.NumConstraints()))
"""Solve the problem and print the solution."""
print(('Number of variables = %d' % solver.NumVariables()))
print(('Number of constraints = %d' % solver.NumConstraints()))
result_status = solver.Solve()
result_status = solver.Solve()
# The problem has an optimal solution.
assert result_status == pywraplp.Solver.OPTIMAL
# The problem has an optimal solution.
assert result_status == pywraplp.Solver.OPTIMAL
# The solution looks legit (when using solvers others than
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
assert solver.VerifySolution(1e-7, True)
# The solution looks legit (when using solvers others than
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
assert solver.VerifySolution(1e-7, True)
print(('Problem solved in %f milliseconds' % solver.wall_time()))
print(('Problem solved in %f milliseconds' % solver.wall_time()))
# The objective value of the solution.
print(('Optimal objective value = %f' % solver.Objective().Value()))
# The objective value of the solution.
print(('Optimal objective value = %f' % solver.Objective().Value()))
# The value of each variable in the solution.
for variable in variable_list:
print(('%s = %f' % (variable.name(), variable.solution_value())))
# The value of each variable in the solution.
for variable in variable_list:
print(('%s = %f' % (variable.name(), variable.solution_value())))
print('Advanced usage:')
print(('Problem solved in %d branch-and-bound nodes' % solver.nodes()))
print('Advanced usage:')
print(('Problem solved in %d branch-and-bound nodes' % solver.nodes()))
def Announce(solver, api_type):
print(('---- Integer programming example with ' + solver + ' (' + api_type +
') -----'))
print(('---- Integer programming example with ' + solver + ' (' + api_type
+ ') -----'))
def RunAllIntegerExampleNaturalLanguageAPI():
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
def RunAllIntegerExampleCppStyleAPI():
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'C++ style API')
RunIntegerExampleCppStyleAPI(pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'C++ style API')
RunIntegerExampleCppStyleAPI(pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'C++ style API')
RunIntegerExampleCppStyleAPI(pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
def main():
RunAllIntegerExampleNaturalLanguageAPI()
RunAllIntegerExampleCppStyleAPI()
RunAllIntegerExampleNaturalLanguageAPI()
RunAllIntegerExampleCppStyleAPI()
if __name__ == '__main__':
main()
main()