GowriHari commited on
Commit
b65b002
·
1 Parent(s): a586e21

Delete OR_tools.py

Browse files
Files changed (1) hide show
  1. OR_tools.py +0 -148
OR_tools.py DELETED
@@ -1,148 +0,0 @@
1
- # importing required modules
2
- import pandas as pd
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- import pandas as pd
6
- import chart_studio.plotly as py
7
- import plotly.figure_factory as ff
8
- import datetime
9
- from datetime import timedelta
10
- import time
11
- import copy
12
-
13
- #importing OR-tools solver
14
- import collections
15
- from ortools.sat.python import cp_model
16
-
17
- def gantt_for_OR(df,method):
18
- title_chart = 'Job shop Schedule - using '+ method
19
- fig = ff.create_gantt(df, index_col='Resource', show_colorbar=True, group_tasks=True, showgrid_x=True, title=title_chart)
20
- return fig
21
-
22
- def or_fn(input_file):
23
- xls = pd.ExcelFile(input_file)
24
- df1 = pd.read_excel(xls, 'Machines Sequence',index_col = 0)
25
- df2 = pd.read_excel(xls, 'Processing Time',index_col = 0)
26
-
27
- jobs_data =[]
28
-
29
- for ind in df1.index:
30
- lists = []
31
- for col in df1:
32
- lists.append((df1[col][ind],df2[col][ind]))
33
- jobs_data.append(lists)
34
-
35
- model = cp_model.CpModel()
36
- machines_count = 1 + max(task[0] for job in jobs_data for task in job)
37
- all_machines = range(machines_count)
38
- # Computes horizon dynamically as the sum of all durations.
39
- horizon = sum(task[1] for job in jobs_data for task in job)
40
-
41
- # Named tuple to store information about created variables.
42
- task_type = collections.namedtuple('task_type', 'start end interval')
43
- # Named tuple to manipulate solution information.
44
- assigned_task_type = collections.namedtuple('assigned_task_type',
45
- 'start job index duration')
46
-
47
- # Creates job intervals and add to the corresponding machine lists.
48
- all_tasks = {}
49
- machine_to_intervals = collections.defaultdict(list)
50
-
51
- for job_id, job in enumerate(jobs_data):
52
- for task_id, task in enumerate(job):
53
- machine = task[0]
54
- duration = task[1]
55
- suffix = '_%i_%i' % (job_id, task_id)
56
- start_var = model.NewIntVar(0, horizon, 'start' + suffix)
57
- end_var = model.NewIntVar(0, horizon, 'end' + suffix)
58
- interval_var = model.NewIntervalVar(start_var, duration, end_var,
59
- 'interval' + suffix)
60
- all_tasks[job_id, task_id] = task_type(start=start_var,
61
- end=end_var,
62
- interval=interval_var)
63
- machine_to_intervals[machine].append(interval_var)
64
-
65
- # Create and add disjunctive constraints.
66
- for machine in all_machines:
67
- model.AddNoOverlap(machine_to_intervals[machine])
68
-
69
- # Precedences inside a job.
70
- for job_id, job in enumerate(jobs_data):
71
- for task_id in range(len(job) - 1):
72
- model.Add(all_tasks[job_id, task_id +
73
- 1].start >= all_tasks[job_id, task_id].end)
74
-
75
- # Makespan objective.
76
- obj_var = model.NewIntVar(0, horizon, 'makespan')
77
- model.AddMaxEquality(obj_var, [
78
- all_tasks[job_id, len(job) - 1].end
79
- for job_id, job in enumerate(jobs_data)
80
- ])
81
- model.Minimize(obj_var)
82
-
83
- start_time = time.time()
84
-
85
- solver = cp_model.CpSolver()
86
- status = solver.Solve(model)
87
-
88
- print("--- %s seconds ---" % (time.time() - start_time))
89
-
90
- if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
91
- print('Solution:')
92
- # Create one list of assigned tasks per machine.
93
- assigned_jobs = collections.defaultdict(list)
94
- for job_id, job in enumerate(jobs_data):
95
- for task_id, task in enumerate(job):
96
- machine = task[0]
97
- assigned_jobs[machine].append(
98
- assigned_task_type(start=solver.Value(
99
- all_tasks[job_id, task_id].start),
100
- job=job_id,
101
- index=task_id,
102
- duration=task[1]))
103
-
104
- # Create per machine output lines.
105
- output = ''
106
- for machine in all_machines:
107
- # Sort by starting time.
108
- assigned_jobs[machine].sort()
109
- sol_line_tasks = 'Machine ' + str(machine) + ': '
110
- sol_line = ' '
111
-
112
- for assigned_task in assigned_jobs[machine]:
113
- name = 'job_%i_task_%i' % (assigned_task.job,
114
- assigned_task.index)
115
- # Add spaces to output to align columns.
116
- sol_line_tasks += '%-15s' % name
117
-
118
- start = assigned_task.start
119
- duration = assigned_task.duration
120
- sol_tmp = '[%i,%i]' % (start, start + duration)
121
- # Add spaces to output to align columns.
122
- sol_line += '%-15s' % sol_tmp
123
-
124
- sol_line += '\n'
125
- sol_line_tasks += '\n'
126
- output += sol_line_tasks
127
- output += sol_line
128
-
129
- # Finally print the solution found.
130
- print(f'Optimal Schedule Length: {solver.ObjectiveValue()}')
131
- op = "Optimal Schedule Length : "+ str(solver.ObjectiveValue())
132
- #print(output)
133
- else:
134
- print('No solution found.')
135
- op = "No solution found."
136
- #Solution df:
137
- df = []
138
- for machine in all_machines:
139
- for task in assigned_jobs[machine]:
140
- df.append(dict(Task='Machine %s'%(machine), Start='2020-02-01 %s'%(str(timedelta(seconds=int(task[0])))), \
141
- Finish='2020-02-01 %s'%(str(timedelta(seconds=int(task[0]+task[3])))),Resource='Job %s'%(task[1])))
142
-
143
- method = 'OR Tools'
144
- fig = gantt_for_OR(df,method)
145
- fig.show()
146
- df_data = pd.DataFrame(df)
147
-
148
- return op,gantt_for_OR(df,method),df_data