linear_solver: Cleanup python samples
This commit is contained in:
@@ -158,6 +158,7 @@ def main():
|
||||
|
||||
# Solve
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ def main():
|
||||
|
||||
# Solve
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -88,6 +88,7 @@ def main():
|
||||
|
||||
# Solve
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -92,6 +92,7 @@ def main():
|
||||
|
||||
# Solve
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ def main():
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ def main():
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ def IntegerProgrammingExample():
|
||||
x = solver.IntVar(0.0, solver.infinity(), "x")
|
||||
y = solver.IntVar(0.0, solver.infinity(), "y")
|
||||
z = solver.IntVar(0.0, solver.infinity(), "z")
|
||||
|
||||
print("Number of variables =", solver.NumVariables())
|
||||
# [END variables]
|
||||
|
||||
# [START constraints]
|
||||
@@ -53,6 +55,8 @@ def IntegerProgrammingExample():
|
||||
constraint2.SetCoefficient(x, 5)
|
||||
constraint2.SetCoefficient(y, 2)
|
||||
constraint2.SetCoefficient(z, -6)
|
||||
|
||||
print("Number of constraints =", solver.NumConstraints())
|
||||
# [END constraints]
|
||||
|
||||
# [START objective]
|
||||
@@ -64,17 +68,30 @@ def IntegerProgrammingExample():
|
||||
objective.SetMaximization()
|
||||
# [END objective]
|
||||
|
||||
# Solve the problem and print the solution.
|
||||
# Solve the problem.
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
# Print the solution.
|
||||
# [START print_solution]
|
||||
solver.Solve()
|
||||
# Print the objective value of the solution.
|
||||
print("Maximum objective function value = %d" % solver.Objective().Value())
|
||||
print()
|
||||
# Print the value of each variable in the solution.
|
||||
for variable in [x, y, z]:
|
||||
print("%s = %d" % (variable.name(), variable.solution_value()))
|
||||
if status == pywraplp.Solver.OPTIMAL:
|
||||
print("Solution:")
|
||||
print(f"Objective value = {solver.Objective().Value()}")
|
||||
# Print the value of each variable in the solution.
|
||||
for variable in [x, y, z]:
|
||||
print(f"{variable.name()} = {variable.solution_value()}")
|
||||
else:
|
||||
print("The problem does not have an optimal solution.")
|
||||
# [END print_solution]
|
||||
|
||||
# [START advanced]
|
||||
print("\nAdvanced usage:")
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
# [END advanced]
|
||||
|
||||
|
||||
IntegerProgrammingExample()
|
||||
# [END program]
|
||||
|
||||
@@ -56,23 +56,24 @@ def LinearProgrammingExample():
|
||||
|
||||
# Solve the system.
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
# [START print_solution]
|
||||
if status == pywraplp.Solver.OPTIMAL:
|
||||
print("Solution:")
|
||||
print("Objective value =", solver.Objective().Value())
|
||||
print("x =", x.solution_value())
|
||||
print("y =", y.solution_value())
|
||||
print(f"Objective value = {solver.Objective().Value():0.1f}")
|
||||
print(f"x = {x.solution_value():0.1f}")
|
||||
print(f"y = {y.solution_value():0.1f}")
|
||||
else:
|
||||
print("The problem does not have an optimal solution.")
|
||||
# [END print_solution]
|
||||
|
||||
# [START advanced]
|
||||
print("\nAdvanced usage:")
|
||||
print("Problem solved in %f milliseconds" % solver.wall_time())
|
||||
print("Problem solved in %d iterations" % solver.iterations())
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
# [END advanced]
|
||||
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@ def main():
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
@@ -93,9 +94,9 @@ def main():
|
||||
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())
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
print(f"Problem solved in {solver.nodes():d} branch-and-bound nodes")
|
||||
else:
|
||||
print("The problem does not have an optimal solution.")
|
||||
# [END print_solution]
|
||||
|
||||
@@ -75,6 +75,7 @@ def main():
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
|
||||
@@ -68,8 +68,8 @@ def main():
|
||||
|
||||
# [START advanced]
|
||||
print("\nAdvanced usage:")
|
||||
print("Problem solved in %f milliseconds" % solver.wall_time())
|
||||
print("Problem solved in %d iterations" % solver.iterations())
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
# [END advanced]
|
||||
|
||||
|
||||
|
||||
@@ -68,9 +68,9 @@ def main():
|
||||
|
||||
# [START advanced]
|
||||
print("\nAdvanced usage:")
|
||||
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())
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
print(f"Problem solved in {solver.nodes():d} branch-and-bound nodes")
|
||||
# [END advanced]
|
||||
|
||||
|
||||
|
||||
@@ -160,6 +160,7 @@ def main():
|
||||
# [END objective]
|
||||
|
||||
# [START solve]
|
||||
print(f"Solving with {solver.SolverVersion()}")
|
||||
status = solver.Solve()
|
||||
# [END solve]
|
||||
|
||||
@@ -192,8 +193,8 @@ def main():
|
||||
|
||||
# [START advanced]
|
||||
print("\nAdvanced usage:")
|
||||
print("Problem solved in ", solver.wall_time(), " milliseconds")
|
||||
print("Problem solved in ", solver.iterations(), " iterations")
|
||||
print(f"Problem solved in {solver.wall_time():d} milliseconds")
|
||||
print(f"Problem solved in {solver.iterations():d} iterations")
|
||||
# [END advanced]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user