use black on examples/python
This commit is contained in:
@@ -11,14 +11,16 @@
|
||||
# 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.
|
||||
|
||||
"""Linear programming examples that show how to use the APIs."""
|
||||
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
|
||||
def Announce(solver, api_type):
|
||||
print('---- Linear programming example with ' + solver + ' (' + api_type +
|
||||
') -----')
|
||||
print(
|
||||
"---- Linear programming example with " + solver + " (" + api_type + ") -----"
|
||||
)
|
||||
|
||||
|
||||
def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):
|
||||
@@ -28,23 +30,25 @@ def RunLinearExampleNaturalLanguageAPI(optimization_problem_type):
|
||||
if not solver:
|
||||
return
|
||||
|
||||
Announce(optimization_problem_type, 'natural language API')
|
||||
Announce(optimization_problem_type, "natural language API")
|
||||
|
||||
infinity = solver.infinity()
|
||||
# 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')
|
||||
x1 = solver.NumVar(0.0, infinity, "x1")
|
||||
x2 = solver.NumVar(0.0, infinity, "x2")
|
||||
x3 = solver.NumVar(0.0, infinity, "x3")
|
||||
|
||||
solver.Maximize(10 * x1 + 6 * x2 + 4 * x3)
|
||||
c0 = solver.Add(10 * x1 + 4 * x2 + 5 * x3 <= 600, 'ConstraintName0')
|
||||
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])
|
||||
c2 = solver.Add(sum_of_vars <= 100.0, 'OtherConstraintName')
|
||||
c2 = solver.Add(sum_of_vars <= 100.0, "OtherConstraintName")
|
||||
|
||||
SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != 'PDLP')
|
||||
SolveAndPrint(
|
||||
solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != "PDLP"
|
||||
)
|
||||
# Print a linear expression's solution value.
|
||||
print('Sum of vars: %s = %s' % (sum_of_vars, sum_of_vars.solution_value()))
|
||||
print("Sum of vars: %s = %s" % (sum_of_vars, sum_of_vars.solution_value()))
|
||||
|
||||
|
||||
def RunLinearExampleCppStyleAPI(optimization_problem_type):
|
||||
@@ -53,13 +57,13 @@ def RunLinearExampleCppStyleAPI(optimization_problem_type):
|
||||
if not solver:
|
||||
return
|
||||
|
||||
Announce(optimization_problem_type, 'C++ style API')
|
||||
Announce(optimization_problem_type, "C++ style API")
|
||||
|
||||
infinity = solver.infinity()
|
||||
# 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')
|
||||
x1 = solver.NumVar(0.0, infinity, "x1")
|
||||
x2 = solver.NumVar(0.0, infinity, "x2")
|
||||
x3 = solver.NumVar(0.0, infinity, "x3")
|
||||
|
||||
# Maximize 10 * x1 + 6 * x2 + 4 * x3.
|
||||
objective = solver.Objective()
|
||||
@@ -69,31 +73,32 @@ def RunLinearExampleCppStyleAPI(optimization_problem_type):
|
||||
objective.SetMaximization()
|
||||
|
||||
# x1 + x2 + x3 <= 100.
|
||||
c0 = solver.Constraint(-infinity, 100.0, 'c0')
|
||||
c0 = solver.Constraint(-infinity, 100.0, "c0")
|
||||
c0.SetCoefficient(x1, 1)
|
||||
c0.SetCoefficient(x2, 1)
|
||||
c0.SetCoefficient(x3, 1)
|
||||
|
||||
# 10 * x1 + 4 * x2 + 5 * x3 <= 600.
|
||||
c1 = solver.Constraint(-infinity, 600.0, 'c1')
|
||||
c1 = solver.Constraint(-infinity, 600.0, "c1")
|
||||
c1.SetCoefficient(x1, 10)
|
||||
c1.SetCoefficient(x2, 4)
|
||||
c1.SetCoefficient(x3, 5)
|
||||
|
||||
# 2 * x1 + 2 * x2 + 6 * x3 <= 300.
|
||||
c2 = solver.Constraint(-infinity, 300.0, 'c2')
|
||||
c2 = solver.Constraint(-infinity, 300.0, "c2")
|
||||
c2.SetCoefficient(x1, 2)
|
||||
c2.SetCoefficient(x2, 2)
|
||||
c2.SetCoefficient(x3, 6)
|
||||
|
||||
SolveAndPrint(solver, [x1, x2, x3], [c0, c1, c2],
|
||||
optimization_problem_type != 'PDLP')
|
||||
SolveAndPrint(
|
||||
solver, [x1, x2, x3], [c0, c1, c2], optimization_problem_type != "PDLP"
|
||||
)
|
||||
|
||||
|
||||
def SolveAndPrint(solver, variable_list, constraint_list, is_precise):
|
||||
"""Solve the problem and print the solution."""
|
||||
print('Number of variables = %d' % solver.NumVariables())
|
||||
print('Number of constraints = %d' % solver.NumConstraints())
|
||||
print("Number of variables = %d" % solver.NumVariables())
|
||||
print("Number of constraints = %d" % solver.NumConstraints())
|
||||
|
||||
result_status = solver.Solve()
|
||||
|
||||
@@ -105,38 +110,40 @@ def SolveAndPrint(solver, variable_list, constraint_list, is_precise):
|
||||
if is_precise:
|
||||
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())
|
||||
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()))
|
||||
print("%s = %f" % (variable.name(), variable.solution_value()))
|
||||
|
||||
print('Advanced usage:')
|
||||
print('Problem solved in %d iterations' % solver.iterations())
|
||||
print("Advanced usage:")
|
||||
print("Problem solved in %d iterations" % solver.iterations())
|
||||
for variable in variable_list:
|
||||
print('%s: reduced cost = %f' %
|
||||
(variable.name(), variable.reduced_cost()))
|
||||
print("%s: reduced cost = %f" % (variable.name(), variable.reduced_cost()))
|
||||
activities = solver.ComputeConstraintActivities()
|
||||
for i, constraint in enumerate(constraint_list):
|
||||
print(('constraint %d: dual value = %f\n'
|
||||
' activity = %f' %
|
||||
(i, constraint.dual_value(), activities[constraint.index()])))
|
||||
print(
|
||||
(
|
||||
"constraint %d: dual value = %f\n activity = %f"
|
||||
% (i, constraint.dual_value(), activities[constraint.index()])
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
RunLinearExampleNaturalLanguageAPI('GLOP')
|
||||
RunLinearExampleNaturalLanguageAPI('GLPK_LP')
|
||||
RunLinearExampleNaturalLanguageAPI('CLP')
|
||||
RunLinearExampleNaturalLanguageAPI('PDLP')
|
||||
RunLinearExampleNaturalLanguageAPI("GLOP")
|
||||
RunLinearExampleNaturalLanguageAPI("GLPK_LP")
|
||||
RunLinearExampleNaturalLanguageAPI("CLP")
|
||||
RunLinearExampleNaturalLanguageAPI("PDLP")
|
||||
|
||||
RunLinearExampleCppStyleAPI('GLOP')
|
||||
RunLinearExampleCppStyleAPI('GLPK_LP')
|
||||
RunLinearExampleCppStyleAPI('CLP')
|
||||
RunLinearExampleCppStyleAPI('PDLP')
|
||||
RunLinearExampleCppStyleAPI("GLOP")
|
||||
RunLinearExampleCppStyleAPI("GLPK_LP")
|
||||
RunLinearExampleCppStyleAPI("CLP")
|
||||
RunLinearExampleCppStyleAPI("PDLP")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user