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.linear_solver import pywraplp
22
23import math
24
25# Data
26
27max_quantities = [["N_Total", 1944], ["P2O5", 1166.4], ["K2O", 1822.5],
28                  ["CaO", 1458], ["MgO", 486], ["Fe", 9.7], ["B", 2.4]]
29
30chemical_set = [["A", 0, 0, 510, 540, 0, 0, 0], ["B", 110, 0, 0, 0, 160, 0, 0],
31                ["C", 61, 149, 384, 0, 30, 1,
32                 0.2], ["D", 148, 70, 245, 0, 15, 1,
33                        0.2], ["E", 160, 158, 161, 0, 10, 1, 0.2]]
34
35num_products = len(max_quantities)
36all_products = range(num_products)
37
38num_sets = len(chemical_set)
39all_sets = range(num_sets)
40
41# Model
42
43max_set = [
44    min(max_quantities[q][1] / chemical_set[s][q + 1] for q in all_products
45        if chemical_set[s][q + 1] != 0.0) for s in all_sets
46]
47
48solver = pywraplp.Solver("chemical_set_lp",
49                         pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
50
51set_vars = [solver.NumVar(0, max_set[s], "set_%i" % s) for s in all_sets]
52
53epsilon = solver.NumVar(0, 1000, "epsilon")
54
55for p in all_products:
56    solver.Add(
57        sum(chemical_set[s][p + 1] * set_vars[s]
58            for s in all_sets) <= max_quantities[p][1])
59    solver.Add(
60        sum(chemical_set[s][p + 1] * set_vars[s]
61            for s in all_sets) >= max_quantities[p][1] - epsilon)
62
63solver.Minimize(epsilon)
64
65print(("Number of variables = %d" % solver.NumVariables()))
66print(("Number of constraints = %d" % solver.NumConstraints()))
67
68result_status = solver.Solve()
69
70# The problem has an optimal solution.
71assert result_status == pywraplp.Solver.OPTIMAL
72
73assert solver.VerifySolution(1e-7, True)
74
75print(("Problem solved in %f milliseconds" % solver.wall_time()))
76
77# The objective value of the solution.
78print(("Optimal objective value = %f" % solver.Objective().Value()))
79
80for s in all_sets:
81    print(
82        "  %s = %f" % (chemical_set[s][0], set_vars[s].solution_value()),
83        end=" ")
84    print()
85for p in all_products:
86    name = max_quantities[p][0]
87    max_quantity = max_quantities[p][1]
88    quantity = sum(
89        set_vars[s].solution_value() * chemical_set[s][p + 1] for s in all_sets)
90    print("%s: %f out of %f" % (name, quantity, max_quantity))
91