example: fix linter in chemical_balance
This commit is contained in:
80
examples/python/chemical_balance_lp.py
Normal file → Executable file
80
examples/python/chemical_balance_lp.py
Normal file → Executable file
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright 2010-2022 Google LLC
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -11,59 +12,68 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# We are trying to group items in equal sized groups.
|
||||
# Each item has a color and a value. We want the sum of values of each group to
|
||||
# be as close to the average as possible.
|
||||
# Furthermore, if one color is an a group, at least k items with this color must
|
||||
# be in that group.
|
||||
|
||||
'''
|
||||
We are trying to group items in equal sized groups.
|
||||
Each item has a color and a value. We want the sum of values of each group to
|
||||
be as close to the average as possible.
|
||||
Furthermore, if one color is an a group, at least k items with this color must
|
||||
be in that group.
|
||||
'''
|
||||
|
||||
from ortools.linear_solver import pywraplp
|
||||
|
||||
import math
|
||||
|
||||
# Data
|
||||
|
||||
max_quantities = [["N_Total", 1944], ["P2O5", 1166.4], ["K2O", 1822.5],
|
||||
["CaO", 1458], ["MgO", 486], ["Fe", 9.7], ["B", 2.4]]
|
||||
max_quantities = [
|
||||
["N_Total", 1944],
|
||||
["P2O5", 1166.4],
|
||||
["K2O", 1822.5],
|
||||
["CaO", 1458],
|
||||
["MgO", 486],
|
||||
["Fe", 9.7],
|
||||
["B", 2.4],
|
||||
]
|
||||
|
||||
chemical_set = [["A", 0, 0, 510, 540, 0, 0, 0], ["B", 110, 0, 0, 0, 160, 0, 0],
|
||||
["C", 61, 149, 384, 0, 30, 1,
|
||||
0.2], ["D", 148, 70, 245, 0, 15, 1,
|
||||
0.2], ["E", 160, 158, 161, 0, 10, 1, 0.2]]
|
||||
chemical_set = [
|
||||
["A", 0, 0, 510, 540, 0, 0, 0],
|
||||
["B", 110, 0, 0, 0, 160, 0, 0],
|
||||
["C", 61, 149, 384, 0, 30, 1, 0.2],
|
||||
["D", 148, 70, 245, 0, 15, 1, 0.2],
|
||||
["E", 160, 158, 161, 0, 10, 1, 0.2],
|
||||
]
|
||||
|
||||
num_products = len(max_quantities)
|
||||
all_products = range(num_products)
|
||||
NUM_PRODUCTS = len(max_quantities)
|
||||
ALL_PRODUCTS = range(NUM_PRODUCTS)
|
||||
|
||||
num_sets = len(chemical_set)
|
||||
all_sets = range(num_sets)
|
||||
NUM_SETS = len(chemical_set)
|
||||
ALL_SETS = range(NUM_SETS)
|
||||
|
||||
# Model
|
||||
|
||||
max_set = [
|
||||
min(max_quantities[q][1] / chemical_set[s][q + 1] for q in all_products
|
||||
if chemical_set[s][q + 1] != 0.0) for s in all_sets
|
||||
min(max_quantities[q][1] / chemical_set[s][q + 1] for q in ALL_PRODUCTS
|
||||
if chemical_set[s][q + 1] != 0.0) for s in ALL_SETS
|
||||
]
|
||||
|
||||
solver = pywraplp.Solver("chemical_set_lp",
|
||||
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
|
||||
|
||||
set_vars = [solver.NumVar(0, max_set[s], "set_%i" % s) for s in all_sets]
|
||||
set_vars = [solver.NumVar(0, max_set[s], f"set_{s}") for s in ALL_SETS]
|
||||
|
||||
epsilon = solver.NumVar(0, 1000, "epsilon")
|
||||
|
||||
for p in all_products:
|
||||
for p in ALL_PRODUCTS:
|
||||
solver.Add(
|
||||
sum(chemical_set[s][p + 1] * set_vars[s]
|
||||
for s in all_sets) <= max_quantities[p][1])
|
||||
for s in ALL_SETS) <= max_quantities[p][1])
|
||||
solver.Add(
|
||||
sum(chemical_set[s][p + 1] * set_vars[s]
|
||||
for s in all_sets) >= max_quantities[p][1] - epsilon)
|
||||
for s in ALL_SETS) >= max_quantities[p][1] - epsilon)
|
||||
|
||||
solver.Minimize(epsilon)
|
||||
|
||||
print(("Number of variables = %d" % solver.NumVariables()))
|
||||
print(("Number of constraints = %d" % solver.NumConstraints()))
|
||||
print(f"Number of variables = {solver.NumVariables()}")
|
||||
print(f"Number of constraints = {solver.NumConstraints()}")
|
||||
|
||||
result_status = solver.Solve()
|
||||
|
||||
@@ -72,19 +82,17 @@ assert result_status == pywraplp.Solver.OPTIMAL
|
||||
|
||||
assert solver.VerifySolution(1e-7, True)
|
||||
|
||||
print(("Problem solved in %f milliseconds" % solver.wall_time()))
|
||||
print(f"Problem solved in {solver.wall_time()} milliseconds")
|
||||
|
||||
# The objective value of the solution.
|
||||
print(("Optimal objective value = %f" % solver.Objective().Value()))
|
||||
print(f"Optimal objective value = {solver.Objective().Value()}")
|
||||
|
||||
for s in all_sets:
|
||||
print(
|
||||
" %s = %f" % (chemical_set[s][0], set_vars[s].solution_value()),
|
||||
end=" ")
|
||||
for s in ALL_SETS:
|
||||
print(f" {chemical_set[s][0]} = {set_vars[s].solution_value()}", end=" ")
|
||||
print()
|
||||
for p in all_products:
|
||||
for p in ALL_PRODUCTS:
|
||||
name = max_quantities[p][0]
|
||||
max_quantity = max_quantities[p][1]
|
||||
quantity = sum(
|
||||
set_vars[s].solution_value() * chemical_set[s][p + 1] for s in all_sets)
|
||||
print("%s: %f out of %f" % (name, quantity, max_quantity))
|
||||
quantity = sum(set_vars[s].solution_value() * chemical_set[s][p + 1]
|
||||
for s in ALL_SETS)
|
||||
print(f"{name}: {quantity} out of {max_quantity}")
|
||||
|
||||
74
examples/python/chemical_balance_sat.py
Normal file → Executable file
74
examples/python/chemical_balance_sat.py
Normal file → Executable file
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright 2010-2022 Google LLC
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -11,31 +12,42 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# We are trying to group items in equal sized groups.
|
||||
# Each item has a color and a value. We want the sum of values of each group to
|
||||
# be as close to the average as possible.
|
||||
# Furthermore, if one color is an a group, at least k items with this color must
|
||||
# be in that group.
|
||||
'''
|
||||
We are trying to group items in equal sized groups.
|
||||
Each item has a color and a value. We want the sum of values of each group to
|
||||
be as close to the average as possible.
|
||||
Furthermore, if one color is an a group, at least k items with this color must
|
||||
be in that group.
|
||||
'''
|
||||
|
||||
|
||||
from ortools.sat.python import cp_model
|
||||
import math
|
||||
from ortools.sat.python import cp_model
|
||||
|
||||
# Data
|
||||
|
||||
max_quantities = [["N_Total", 1944], ["P2O5", 1166.4], ["K2O", 1822.5],
|
||||
["CaO", 1458], ["MgO", 486], ["Fe", 9.7], ["B", 2.4]]
|
||||
max_quantities = [
|
||||
["N_Total", 1944],
|
||||
["P2O5", 1166.4],
|
||||
["K2O", 1822.5],
|
||||
["CaO", 1458],
|
||||
["MgO", 486],
|
||||
["Fe", 9.7],
|
||||
["B", 2.4],
|
||||
]
|
||||
|
||||
chemical_set = [["A", 0, 0, 510, 540, 0, 0, 0], ["B", 110, 0, 0, 0, 160, 0, 0],
|
||||
["C", 61, 149, 384, 0, 30, 1,
|
||||
0.2], ["D", 148, 70, 245, 0, 15, 1,
|
||||
0.2], ["E", 160, 158, 161, 0, 10, 1, 0.2]]
|
||||
chemical_set = [
|
||||
["A", 0, 0, 510, 540, 0, 0, 0],
|
||||
["B", 110, 0, 0, 0, 160, 0, 0],
|
||||
["C", 61, 149, 384, 0, 30, 1, 0.2],
|
||||
["D", 148, 70, 245, 0, 15, 1, 0.2],
|
||||
["E", 160, 158, 161, 0, 10, 1, 0.2],
|
||||
]
|
||||
|
||||
num_products = len(max_quantities)
|
||||
all_products = range(num_products)
|
||||
NUM_PRODUCTS = len(max_quantities)
|
||||
ALL_PRODUCTS = range(NUM_PRODUCTS)
|
||||
|
||||
num_sets = len(chemical_set)
|
||||
all_sets = range(num_sets)
|
||||
NUM_SETS = len(chemical_set)
|
||||
ALL_SETS = range(NUM_SETS)
|
||||
|
||||
# Model
|
||||
|
||||
@@ -46,40 +58,38 @@ max_set = [
|
||||
int(
|
||||
math.ceil(
|
||||
min(max_quantities[q][1] * 1000 / chemical_set[s][q + 1]
|
||||
for q in all_products if chemical_set[s][q + 1] != 0)))
|
||||
for s in all_sets
|
||||
for q in ALL_PRODUCTS if chemical_set[s][q + 1] != 0)))
|
||||
for s in ALL_SETS
|
||||
]
|
||||
|
||||
set_vars = [model.NewIntVar(0, max_set[s], "set_%i" % s) for s in all_sets]
|
||||
set_vars = [model.NewIntVar(0, max_set[s], f"set_{s}") for s in ALL_SETS]
|
||||
|
||||
epsilon = model.NewIntVar(0, 10000000, "epsilon")
|
||||
|
||||
for p in all_products:
|
||||
for p in ALL_PRODUCTS:
|
||||
model.Add(
|
||||
sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
|
||||
for s in all_sets) <= int(max_quantities[p][1] * 10000))
|
||||
for s in ALL_SETS) <= int(max_quantities[p][1] * 10000))
|
||||
model.Add(
|
||||
sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
|
||||
for s in all_sets) >= int(max_quantities[p][1] * 10000) - epsilon)
|
||||
for s in ALL_SETS) >= int(max_quantities[p][1] * 10000) - epsilon)
|
||||
|
||||
model.Minimize(epsilon)
|
||||
|
||||
# Creates a solver and solves.
|
||||
solver = cp_model.CpSolver()
|
||||
status = solver.Solve(model)
|
||||
print("Status = %s" % solver.StatusName(status))
|
||||
print(f"Status = {solver.StatusName(status)}")
|
||||
# The objective value of the solution.
|
||||
print("Optimal objective value = %f" % (solver.ObjectiveValue() / 10000.0))
|
||||
print(f"Optimal objective value = {solver.ObjectiveValue() / 10000.0}")
|
||||
|
||||
for s in all_sets:
|
||||
print(
|
||||
" %s = %f" % (chemical_set[s][0], solver.Value(set_vars[s]) / 1000.0),
|
||||
end=" ")
|
||||
for s in ALL_SETS:
|
||||
print(f" {chemical_set[s][0]} = {solver.Value(set_vars[s]) / 1000.0}", end=" ")
|
||||
print()
|
||||
for p in all_products:
|
||||
for p in ALL_PRODUCTS:
|
||||
name = max_quantities[p][0]
|
||||
max_quantity = max_quantities[p][1]
|
||||
quantity = sum(
|
||||
solver.Value(set_vars[s]) / 1000.0 * chemical_set[s][p + 1]
|
||||
for s in all_sets)
|
||||
print("%s: %f out of %f" % (name, quantity, max_quantity))
|
||||
for s in ALL_SETS)
|
||||
print(f"{name}: {quantity} out of {max_quantity}")
|
||||
|
||||
Reference in New Issue
Block a user