2022-12-19 15:51:57 +01:00
|
|
|
#!/usr/bin/env python3
|
2022-06-17 08:40:20 +02:00
|
|
|
# Copyright 2010-2022 Google LLC
|
2018-08-20 09:09:08 +02:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# 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.
|
2022-12-21 09:54:01 +01:00
|
|
|
"""We are trying to group items in equal sized groups.
|
2022-12-19 15:51:57 +01:00
|
|
|
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
|
2022-12-21 09:54:01 +01:00
|
|
|
be in that group."""
|
2018-08-20 09:09:08 +02:00
|
|
|
|
|
|
|
|
import math
|
2022-12-19 15:51:57 +01:00
|
|
|
from ortools.sat.python import cp_model
|
2018-08-20 09:09:08 +02:00
|
|
|
|
|
|
|
|
# Data
|
|
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
max_quantities = [
|
|
|
|
|
["N_Total", 1944],
|
|
|
|
|
["P2O5", 1166.4],
|
|
|
|
|
["K2O", 1822.5],
|
|
|
|
|
["CaO", 1458],
|
|
|
|
|
["MgO", 486],
|
|
|
|
|
["Fe", 9.7],
|
|
|
|
|
["B", 2.4],
|
|
|
|
|
]
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
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],
|
|
|
|
|
]
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
NUM_PRODUCTS = len(max_quantities)
|
|
|
|
|
ALL_PRODUCTS = range(NUM_PRODUCTS)
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
NUM_SETS = len(chemical_set)
|
|
|
|
|
ALL_SETS = range(NUM_SETS)
|
2018-08-20 09:09:08 +02:00
|
|
|
|
|
|
|
|
# Model
|
|
|
|
|
|
|
|
|
|
model = cp_model.CpModel()
|
|
|
|
|
|
|
|
|
|
# Scale quantities by 100.
|
2018-09-06 15:09:32 +02:00
|
|
|
max_set = [
|
|
|
|
|
int(
|
|
|
|
|
math.ceil(
|
|
|
|
|
min(max_quantities[q][1] * 1000 / chemical_set[s][q + 1]
|
2022-12-19 15:51:57 +01:00
|
|
|
for q in ALL_PRODUCTS if chemical_set[s][q + 1] != 0)))
|
|
|
|
|
for s in ALL_SETS
|
2018-09-06 15:09:32 +02:00
|
|
|
]
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
set_vars = [model.NewIntVar(0, max_set[s], f"set_{s}") for s in ALL_SETS]
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2018-09-06 15:09:32 +02:00
|
|
|
epsilon = model.NewIntVar(0, 10000000, "epsilon")
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
for p in ALL_PRODUCTS:
|
2018-11-11 09:39:59 +01:00
|
|
|
model.Add(
|
|
|
|
|
sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
|
2022-12-19 15:51:57 +01:00
|
|
|
for s in ALL_SETS) <= int(max_quantities[p][1] * 10000))
|
2018-11-11 09:39:59 +01:00
|
|
|
model.Add(
|
|
|
|
|
sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
|
2022-12-19 15:51:57 +01:00
|
|
|
for s in ALL_SETS) >= int(max_quantities[p][1] * 10000) - epsilon)
|
2018-08-20 09:09:08 +02:00
|
|
|
|
|
|
|
|
model.Minimize(epsilon)
|
|
|
|
|
|
|
|
|
|
# Creates a solver and solves.
|
|
|
|
|
solver = cp_model.CpSolver()
|
|
|
|
|
status = solver.Solve(model)
|
2022-12-19 15:51:57 +01:00
|
|
|
print(f"Status = {solver.StatusName(status)}")
|
2018-08-20 09:09:08 +02:00
|
|
|
# The objective value of the solution.
|
2022-12-19 15:51:57 +01:00
|
|
|
print(f"Optimal objective value = {solver.ObjectiveValue() / 10000.0}")
|
2018-08-20 09:09:08 +02:00
|
|
|
|
2022-12-19 15:51:57 +01:00
|
|
|
for s in ALL_SETS:
|
|
|
|
|
print(f" {chemical_set[s][0]} = {solver.Value(set_vars[s]) / 1000.0}", end=" ")
|
2018-11-11 09:39:59 +01:00
|
|
|
print()
|
2022-12-19 15:51:57 +01:00
|
|
|
for p in ALL_PRODUCTS:
|
2018-11-11 09:39:59 +01:00
|
|
|
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]
|
2022-12-19 15:51:57 +01:00
|
|
|
for s in ALL_SETS)
|
|
|
|
|
print(f"{name}: {quantity} out of {max_quantity}")
|