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 34
/
demo.py
78 lines (66 loc) · 2.83 KB
/
demo.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
# Copyright 2019 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 __future__ import print_function
from dwave.system.composites import EmbeddingComposite
from dwave.system.samplers import DWaveSampler
from job_shop_scheduler import get_jss_bqm, is_auxiliary_variable
# Construct a BQM for the jobs
jobs = {"cupcakes": [("mixer", 2), ("oven", 1)],
"smoothie": [("mixer", 1)],
"lasagna": [("oven", 2)]}
max_time = 4 # Upperbound on how long the schedule can be; 4 is arbitrary
bqm = get_jss_bqm(jobs, max_time)
# Submit BQM
# Note: may need to tweak the chain strength and the number of reads
sampler = EmbeddingComposite(DWaveSampler())
sampleset = sampler.sample(bqm,
chain_strength=2,
num_reads=1000,
label='Example - Job Shop Scheduling')
# Grab solution
solution = sampleset.first.sample
# Visualize solution
# Note0: we are making the solution simpler to interpret by restructuring it
# into the following format:
# task_times = {"job": [start_time_for_task0, start_time_for_task1, ..],
# "other_job": [start_time_for_task0, ..]
# ..}
#
# Note1: each node in our BQM is labelled as "<job>_<task_index>,<time>".
# For example, the node "cupcakes_1,2" refers to job 'cupcakes', its 1st task
# (where we are using zero-indexing, so task '("oven", 1)'), starting at time
# 2.
#
# Hence, we are grabbing the nodes selected by our solver (i.e. nodes flagged
# with 1s) that will make a good schedule.
# (see next line of code, 'selected_nodes')
#
# Note2: if a start_time_for_task == -1, it means that the solution is invalid
# Grab selected nodes
selected_nodes = [k for k, v in solution.items() if v == 1]
# Parse node information
task_times = {k: [-1]*len(v) for k, v in jobs.items()}
for node in selected_nodes:
if is_auxiliary_variable(node):
continue
job_name, task_time = node.rsplit("_", 1)
task_index, start_time = map(int, task_time.split(","))
task_times[job_name][task_index] = start_time
# Print problem and restructured solution
print("Jobs and their machine-specific tasks:")
for job, task_list in jobs.items():
print("{0:9}: {1}".format(job, task_list))
print("\nJobs and the start times of each task:")
for job, times in task_times.items():
print("{0:9}: {1}".format(job, times))