1# Copyright 2010-2021 Google LLC
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14# We are trying to group items in equal sized groups.
15# Each item has a color and a value. We want the sum of values of each group to
16# be as close to the average as possible.
17# Furthermore, if one color is an a group, at least k items with this color must
18# be in that group.
19
20
21from ortools.sat.python import cp_model
22import math
23
24# Data
25
26max_quantities = [["N_Total", 1944], ["P2O5", 1166.4], ["K2O", 1822.5],
27                  ["CaO", 1458], ["MgO", 486], ["Fe", 9.7], ["B", 2.4]]
28
29chemical_set = [["A", 0, 0, 510, 540, 0, 0, 0], ["B", 110, 0, 0, 0, 160, 0, 0],
30                ["C", 61, 149, 384, 0, 30, 1,
31                 0.2], ["D", 148, 70, 245, 0, 15, 1,
32                        0.2], ["E", 160, 158, 161, 0, 10, 1, 0.2]]
33
34num_products = len(max_quantities)
35all_products = range(num_products)
36
37num_sets = len(chemical_set)
38all_sets = range(num_sets)
39
40# Model
41
42model = cp_model.CpModel()
43
44# Scale quantities by 100.
45max_set = [
46    int(
47        math.ceil(
48            min(max_quantities[q][1] * 1000 / chemical_set[s][q + 1]
49                for q in all_products if chemical_set[s][q + 1] != 0)))
50    for s in all_sets
51]
52
53set_vars = [model.NewIntVar(0, max_set[s], "set_%i" % s) for s in all_sets]
54
55epsilon = model.NewIntVar(0, 10000000, "epsilon")
56
57for p in all_products:
58    model.Add(
59        sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
60            for s in all_sets) <= int(max_quantities[p][1] * 10000))
61    model.Add(
62        sum(int(chemical_set[s][p + 1] * 10) * set_vars[s]
63            for s in all_sets) >= int(max_quantities[p][1] * 10000) - epsilon)
64
65model.Minimize(epsilon)
66
67# Creates a solver and solves.
68solver = cp_model.CpSolver()
69status = solver.Solve(model)
70print("Status = %s" % solver.StatusName(status))
71# The objective value of the solution.
72print("Optimal objective value = %f" % (solver.ObjectiveValue() / 10000.0))
73
74for s in all_sets:
75    print(
76        "  %s = %f" % (chemical_set[s][0], solver.Value(set_vars[s]) / 1000.0),
77        end=" ")
78    print()
79for p in all_products:
80    name = max_quantities[p][0]
81    max_quantity = max_quantities[p][1]
82    quantity = sum(
83        solver.Value(set_vars[s]) / 1000.0 * chemical_set[s][p + 1]
84        for s in all_sets)
85    print("%s: %f out of %f" % (name, quantity, max_quantity))
86