forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reallocate_sat.py
136 lines (108 loc) · 3.8 KB
/
reallocate_sat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright 2010-2021 Google LLC
# 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.
"""Reallocate production to smooth it over years."""
import collections
from ortools.sat.python import cp_model
def main():
# Data
data_0 = [
[107, 107, 107, 0, 0], # pr1
[0, 47, 47, 47, 0], # pr2
[10, 10, 10, 0, 0], # pr3
[0, 55, 55, 55, 55], # pr4
]
data_1 = [
[119444030, 0, 0, 0],
[34585586, 38358559, 31860661, 0],
[19654655, 21798799, 18106106, 0],
[298836792, 0, 0, 0],
[3713428, 4118530, 4107277, 3072018],
[6477273, 7183884, 5358471, 0],
[1485371, 1647412, 1642911, 1228807]
]
data_2 = [
[1194440, 0, 0, 0],
[345855, 383585, 318606, 0],
[196546, 217987, 181061, 0],
[2988367, 0, 0, 0],
[37134, 41185, 41072, 30720],
[64772, 71838, 53584, 0],
[14853, 16474, 16429, 12288]
]
pr = data_0
num_pr = len(pr)
num_years = len(pr[1])
total = sum(pr[p][y] for p in range(num_pr) for y in range(num_years))
avg = total // num_years
# Model
model = cp_model.CpModel()
# Variables
delta = model.NewIntVar(0, total, 'delta')
contributions_per_years = collections.defaultdict(list)
contributions_per_prs = collections.defaultdict(list)
all_contribs = {}
for p, inner_l in enumerate(pr):
for y, item in enumerate(inner_l):
if item != 0:
contrib = model.NewIntVar(0, total, 'r%d c%d' % (p, y))
contributions_per_years[y].append(contrib)
contributions_per_prs[p].append(contrib)
all_contribs[p, y] = contrib
year_var = [
model.NewIntVar(0, total, 'y[%i]' % i) for i in range(num_years)
]
# Constraints
# Maintain year_var.
for y in range(num_years):
model.Add(year_var[y] == sum(contributions_per_years[y]))
# Fixed contributions per pr.
for p in range(num_pr):
model.Add(sum(pr[p]) == sum(contributions_per_prs[p]))
# Link delta with variables.
for y in range(num_years):
model.Add(year_var[y] >= avg - delta)
for y in range(num_years):
model.Add(year_var[y] <= avg + delta)
# Solve and output
model.Minimize(delta)
# Solve model.
solver = cp_model.CpSolver()
status = solver.Solve(model)
# Output solution.
if status == cp_model.OPTIMAL:
print('Data')
print(' - total = ', total)
print(' - year_average = ', avg)
print(' - number of projects = ', num_pr)
print(' - number of years = ', num_years)
print(' - input production')
for p in range(num_pr):
for y in range(num_years):
if pr[p][y] == 0:
print(' ', end='')
else:
print('%10i' % pr[p][y], end='')
print()
print('Solution')
for p in range(num_pr):
for y in range(num_years):
if pr[p][y] == 0:
print(' ', end='')
else:
print('%10i' % solver.Value(all_contribs[p, y]), end='')
print()
for y in range(num_years):
print('%10i' % solver.Value(year_var[y]), end='')
print()
if __name__ == '__main__':
main()