This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrop_rotation.py
447 lines (356 loc) · 16.1 KB
/
crop_rotation.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# Copyright 2021 D-Wave Systems Inc.
#
# 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.
from collections import defaultdict
from itertools import combinations
from os.path import dirname, join
import random
import sys
import tempfile
import click
from dwave.system import LeapHybridDQMSampler
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb
from matplotlib.ticker import MultipleLocator
import yaml
from caselabeldqm import CaseLabelDQM
from validate import validate_problem, InvalidProblem
DEFAULT_PATH = join(dirname(__file__), 'data', 'problem1.yaml')
def load_problem_file(path):
"""Load a problem file.
Args:
path (string): Path to input file.
Returns:
Tuple of time_units (int), plot_adjacency (dict), and crops (dict).
"""
try:
data = list(yaml.safe_load_all(path))[0]
except IndexError:
raise InvalidProblem('empty file')
validate_problem(data, path)
time_units = data['time_units']
plot_adjacency = data['plot_adjacency']
crops = data['crops']
return time_units, plot_adjacency, crops
def crop_colors(crop_families):
"""Generate colors for crops to be used in visualization of solution.
All crops should have a unique color and crops in the same family should
have similar colors.
Args:
crop_families (dict):
Dictionary of crop family names mapped to crops.
Returns:
Dictionary of crop names mapped to colors.
"""
h_scale = 2 / (3 * len(crop_families))
colors = {}
for k, crops in enumerate(crop_families.values()):
h_fam = k / len(crop_families)
for crop in crops:
h = h_fam + (random.random() - 0.5) * h_scale
colors[crop] = (min(1, max(0, h)),
0.7 + (random.random() - 0.5) * 0.3,
0.7 + (random.random() - 0.5) * 0.3)
return {crop: hsv_to_rgb(color) for crop, color in colors.items()}
class CropRotation:
"""Solve a crop rotation problem.
"""
def __init__(self, time_units, plot_adjacency, crops, verbose):
self.time_units = time_units
self.plot_adjacency = plot_adjacency
self.crops = crops
self.verbose = verbose
self.dqm = None
self.grow_time = {crop: dict_['grow_time']
for crop, dict_ in crops.items()}
self.gamma = 1 + max(self.grow_time.values())
self.period_crops = {1 + period: [] for period in range(self.time_units)}
self.crop_families = defaultdict(list)
for crop, dict_ in self.crops.items():
for period in range(dict_['planting'][0], dict_['planting'][1] + 1):
self.period_crops[period].append(crop)
self.crop_families[dict_['family']].append(crop)
self._crop_r = []
for crop in self.crops.keys():
range_ = range(self.grow_time[crop])
self._crop_r.extend([(crop, r) for r in range_])
self._neighbor_pairs = []
for plot, neighbors in plot_adjacency.items():
self._neighbor_pairs.extend([(plot, v) for v in neighbors])
self.crop_colors = crop_colors(self.crop_families)
def rollover_period(self, period):
"""Ensure that `period` is between 1 and `self.time_units` by adding or
subtracting multiples of `self.time_units`.
"""
while period > self.time_units:
period -= self.time_units
while period < 1:
period += self.time_units
return period
def crop_r_combinations(self, plot, period, crop_r):
"""Generate combinations of variables and cases from an input sequence.
Args:
plot: The plot to generate combinations for.
period: The period to generate combinations for.
crop_r: Sequence of crops paired with r-values. R-values are
integers used to identify earlier or later growing periods on
a plot relative to `period`.
Returns:
Generator yielding four-tuples: var1, crop1, var2, crop2.
"""
for (crop1, r1), (crop2, r2) in combinations(crop_r, 2):
if r1 == r2:
# DQM enforces one-hot constraint for all variables so we can
# ignore this case.
continue
r1_period = self.rollover_period(period - r1)
if crop1 not in self.period_crops[r1_period]:
continue
r2_period = self.rollover_period(period - r2)
if crop2 not in self.period_crops[r2_period]:
continue
var1 = f'{plot},{r1_period}'
var2 = f'{plot},{r2_period}'
yield var1, crop1, var2, crop2
def plot_crop_r_combinations(self, period, plot_crop_r):
"""Generate combinations of variables and cases from an input sequence.
Args:
period: The period to generate combinations for.
plot_crop_r: Sequence of (plot, crop, r-value) triples. R-values
are integers used to identify earlier or later growing periods
on a plot relative to `period`.
Returns:
Generator yielding four-tuples: var1, crop1, var2, crop2.
"""
for (u, crop1, r1), (v, crop2, r2) in combinations(plot_crop_r, 2):
if (u, r1) == (v, r2):
# DQM enforces one-hot constraint for all variables so we can
# ignore this case.
continue
r1_period = self.rollover_period(period - r1)
if crop1 not in self.period_crops[r1_period]:
continue
r2_period = self.rollover_period(period - r2)
if crop2 not in self.period_crops[r2_period]:
continue
var1 = f'{u},{r1_period}'
var2 = f'{v},{r2_period}'
yield var1, crop1, var2, crop2
def build_dqm(self):
"""Build a discrete quadratic model that encodes the problem.
"""
self.dqm = dqm = CaseLabelDQM()
# add variables and set linear biases.
for period in range(1, self.time_units + 1):
for plot in self.plot_adjacency:
var = f'{plot},{period}'
dqm.add_variable(
[None] + self.period_crops[period], label=var)
for crop in self.period_crops[period]:
dqm.set_linear_case(var, crop, -self.grow_time[crop])
# set quadratic biases for first constraint set.
for period in range(1, self.time_units + 1):
for plot in self.plot_adjacency:
for args in self.crop_r_combinations(plot, period, self._crop_r):
dqm.set_quadratic_case(*args, self.gamma)
# set quadratic biases for second constraint set.
for period in range(1, self.time_units + 1):
for plot in self.plot_adjacency:
for F_p in self.crop_families.values():
crop_r = []
for crop in F_p:
range_ = range(self.grow_time[crop] + 1)
crop_r.extend([(crop, r) for r in range_])
for args in self.crop_r_combinations(plot, period, crop_r):
dqm.set_quadratic_case(*args, self.gamma)
# set quadratic biases for third constraint set.
for period in range(1, self.time_units + 1):
for u, v in self._neighbor_pairs:
for F_p in self.crop_families.values():
plot_crop_r = []
for crop in F_p:
range_ = range(self.grow_time[crop])
plot_crop_r.extend([(u, crop, r) for r in range_])
plot_crop_r.extend([(v, crop, r) for r in range_])
for args in self.plot_crop_r_combinations(period, plot_crop_r):
dqm.set_quadratic_case(*args, self.gamma)
if self.verbose:
n_v = dqm.num_variables()
n_v_i = dqm.num_variable_interactions()
max_n_v_i = n_v * (n_v - 1) // 2
n_c = dqm.num_cases()
n_c_i = dqm.num_case_interactions()
n_plots = len(self.plot_adjacency)
# this is the number of case interactions that are disallowed by
# DQM (cases of a variable interacting with itself).
illegal_c_i = sum((1 + len(x)) * len(x) // 2
for x in self.period_crops.values()) * n_plots
max_n_c_i = (n_c * (n_c - 1) // 2) - illegal_c_i
print(f'DQM num. variables: {n_v}')
print(f'DQM num. variable interactions: {n_v_i} '
f'({n_v_i * 100 / max_n_v_i:.1f} % of max)')
print(f'DQM num. cases: {n_c}')
print(f'DQM num. case interactions: {n_c_i} '
f'({n_c_i * 100 / max_n_c_i:.1f} % of max)')
def solve(self):
"""Solve the problem using LeapHybridDQMSampler.
"""
sampler = LeapHybridDQMSampler()
self.sampleset = sampler.sample_dqm(self.dqm,
label='Example - Crop Rotation')
def validate(self, sample):
"""Check that the constraints of the problem are satisfied.
"""
errors = []
# check first constraint set.
for period in range(1, self.time_units + 1):
for plot in self.plot_adjacency:
var = f'{plot},{period}'
crop = sample[var]
if crop:
for r in range(1, self.grow_time[crop]):
r_period = self.rollover_period(period + r)
r_var = f'{plot},{r_period}'
r_crop = sample[r_var]
if r_crop:
errors += [f'Constraint 1 violated: {var} {crop} '
f'{r_var} {r_crop} {self.grow_time[crop]}']
# check second constraint set.
for period in range(1, self.time_units + 1):
for plot in self.plot_adjacency:
var = f'{plot},{period}'
crop = sample[var]
if crop:
family = self.crops[crop]['family']
related_crops = set(self.crop_families[family]) - {crop}
for r in range(1, self.grow_time[crop] + 1):
r_period = self.rollover_period(period + r)
r_var = f'{plot},{r_period}'
r_crop = sample[r_var]
if r_crop in related_crops:
errors += [f'Constraint 2 violated: {var} {crop} '
f'{r_var} {r_crop}']
# check third constraint set.
for period in range(1, self.time_units + 1):
for plot, neighbors in self.plot_adjacency.items():
var = f'{plot},{period}'
crop = sample[var]
if crop:
family = self.crops[crop]['family']
related_crops = set(self.crop_families[family])
for r in range(0, self.grow_time[crop]):
r_period = self.rollover_period(period + r)
for neighbor in neighbors:
var2 = f'{neighbor},{r_period}'
crop2 = sample[var2]
if crop2 in related_crops:
errors += [f'Constraint 3 violated: {var} {crop} '
f'{var2} {crop2}']
return errors
@property
def solution(self):
return self.dqm.map_sample(self.sampleset.first.sample)
def render_solution(self, path):
"""Generate a visual representation of the solution.
"""
sample = self.solution
labels = set() # keep track of labels so legend won't have duplicates.
width = 1
max_x = self.time_units
fig, ax = plt.subplots()
for k, plot in enumerate(self.plot_adjacency):
for period in range(1, self.time_units + 1):
crop = sample[f'{plot},{period}']
if crop:
xs = list(range(period, period + self.grow_time[crop]))
max_x = max(max_x, max(xs))
ys = [1] * len(xs)
color = self.crop_colors[crop]
bottom = [k] * len(xs)
label = crop if crop not in labels else None
labels.add(crop)
ax.bar(xs, ys, width, bottom=bottom, color=color,
label=label, align='edge')
# indicate wrap-around periods by repeating the 1..time_units labels.
period_labels = [1 + (x % self.time_units) for x in range(max_x + 1)]
plt.title('Crop Rotation Demo')
ax.set_xlabel('Period')
ax.set_ylabel('Plot')
ax.set_xticks(list(range(1, max_x + 2)))
ax.set_xticklabels(period_labels)
ax.set_yticks(list(range(1, len(self.plot_adjacency) + 1)))
ax.set_yticklabels(list(self.plot_adjacency.keys()))
period_divisor = max_x // 4
if period_divisor:
# hide a fraction of period labels or they will be hard to read.
ax.xaxis.set_major_locator(MultipleLocator(period_divisor))
ax.xaxis.set_minor_locator(MultipleLocator(1))
# place legend to right of chart.
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.3, 1), shadow=True)
# show grid lines.
if period_divisor:
plt.grid(axis='x', which='both')
else:
plt.grid(axis='x')
plt.grid(axis='y')
fig.savefig(path)
print(f'Saved illustration of solution to {path}')
@property
def utilization(self):
"""Return the fraction of the maximum possible plot utilization for the
found solution.
"""
utilization = 0
for k, plot in enumerate(self.plot_adjacency):
for period in range(1, self.time_units + 1):
crop = self.solution[f'{plot},{period}']
if crop:
utilization += self.grow_time[crop]
return utilization / (len(self.plot_adjacency) * self.time_units)
def evaluate(self):
"""Evaluate the solution.
"""
if self.verbose:
print(self.sampleset)
sample = self.solution
print(f'Solution: {dict(((k, v) for k, v in sample.items() if v))}')
print(f'Solution energy: {self.sampleset.first.energy}')
print(f'Plot utilization: {100 * self.utilization:.1f} %')
for error in self.validate(sample):
print(f'Solution is invalid: {error}')
@click.command(help='Solve an instance of the Crop Rotation problem using '
'LeapHybridDQMSampler.')
@click.option('--path', type=click.File(), default=DEFAULT_PATH,
help=f'Path to problem file. Default is {DEFAULT_PATH!r}')
@click.option('--output-tempfile', is_flag=True,
help='Output solution illustration to a unique, named temporary '
'file.')
@click.option('--verbose', is_flag=True)
def main(path, output_tempfile, verbose):
try:
rotation = CropRotation(*load_problem_file(path), verbose)
except InvalidProblem as e:
sys.exit(f'E: {e.args[0]}')
rotation.build_dqm()
rotation.solve()
rotation.evaluate()
if output_tempfile:
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as f:
output_path = f.name
else:
output_path = 'output.png'
rotation.render_solution(output_path)
if __name__ == '__main__':
main()