-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathord-dev.py
239 lines (149 loc) · 5.56 KB
/
ord-dev.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
UNIT_CONVERTER = 24.5
data = pd.read_excel('media/data/order2024.xlsx', engine="openpyxl")
# Rename columns
data = data.rename(columns={
"กำหนดส่ง": "due_date",
"แผ่นหน้า": "front_sheet",
"ลอน C": "c_wave",
"แผ่นกลาง": "middle_sheet",
"ลอน B": "b_wave",
"แผ่นหลัง": "back_sheet",
"จน.ชั้น": "level",
"กว้างผลิต": "width",
"ยาวผลิต": "length",
"ทับเส้นซ้าย": "left_edge_cut",
"ทับเส้นกลาง": "middle_edge_cut",
"ทับเส้นขวา": "right_edge_cut",
"เลขที่ใบสั่งขาย": "order_number",
"ชนิดส่วนประกอบ": "component_type",
"จำนวนสั่งขาย": "quantity",
"จำนวนสั่งผลิต": "production_quantity",
"ประเภททับเส้น": "edge_type",
"สถานะใบสั่ง": "order_status",
"% ที่เกิน": "excess_percentage"
})
# In[2]:
import uuid
for index, row in data.iterrows():
data.at[index, 'width'] = round(row["width"] / UNIT_CONVERTER, 4)
data.at[index, 'length'] = round(row["length"] / UNIT_CONVERTER, 4)
data.at[index, 'id'] = uuid.uuid4()
# In[ ]:
# In[3]:
start_date = pd.to_datetime('2024-08-1')
stop_date = pd.to_datetime('2024-08-7')
# In[4]:
# In[6]:
from icecream import ic
import numpy as np
from dataclasses import dataclass
from typing import Any, Dict
from numpy import roll
import pandas as pd
# In[7]:
# from modules.ordplan import ORD
# legacy_data = ORD(filtered_data, deadline_range = 30, size=temp_size).get()
# In[8]:
from modules.lp import LP
# In[9]:
from new_ga import GA
# In[10]:
from hd import HD
# In[11]:
from ordplan_project.settings import MAX_TRIM, MIN_TRIM
from typing import Dict, Any
def is_trim_fit(trim: float):
"""
Check if trim exceed min/max tirm.
"""
return trim <= MAX_TRIM and trim >= MIN_TRIM
def handle_switcher(used) -> Dict[str,Any]:
if is_trim_fit(used):
return
switcher = LP({'fitness': used}).run().get()
# ic(switcher,used)
return switcher
def update_acc_list(acc_list,ga_instance,hd_instance,elapsed_times):
temp_acc_str = ""
temp_acc_str+='Parameters -> '
parameters = (
"parent:" +ga_instance.parent_selection_type + " | " +
"co proba:" +str(ga_instance.crossover_probability) + " | " +
"crossover:" +ga_instance.crossover_type + " | " +
str(ga_instance.mutation_probability) + " | " +
str(ga_instance.mutation_percent_genes) + " | " +
"heu type:" +hd_instance.h_type +"\n"
)
temp_acc_str+=parameters
accuracy = (passed_trim / count) * 100.00 if count != 0 else 0.0
temp_acc_str += "Accuracy -> {:.2f}%".format(accuracy)
total_seconds = sum(elapsed_times)
minutes, seconds = divmod(total_seconds, 60)
time_spent_str = f"{int(minutes)}:{int(seconds):02d}"
temp_acc_str += f"\nTime Spent -> {time_spent_str}"
temp_acc_str += "\n"
acc_list.append(temp_acc_str)
# In[14]:
from tqdm import tqdm
import itertools
import time
import enlighten
crossover_types = ["two_points", "uniform"]
mutation_proba = [None,[0.25, 0.05]]
crossover_proba = [None, 0.65]
acc_list = []
x = 1
count = 2
hd_instances = []
h_types = ["ff", "ffa"]
for h_type in h_types:
hd_instance = HD(orders=data,h_type=h_type, x=x, start_date=start_date, stop_date=stop_date)
hd_instances.append(hd_instance)
temp_size = hd_instance.temp_size
manager = enlighten.get_manager()
types = manager.counter(total=len(list((itertools.product(crossover_types, mutation_proba, crossover_proba, hd_instances)))), desc='Types', unit='it')
counts = manager.counter(total=count*len(list((itertools.product(crossover_types, mutation_proba, crossover_proba, hd_instances)))), desc='Count', unit='it')
for crossover_prob, crossover, mutation, hd_instance in itertools.product(crossover_proba, crossover_types, mutation_proba,hd_instances):
heuristic_data = hd_instance.get()
elapsed_times = []
passed_trim = 0
types.update()
for i in range(count):
counts.update()
start_time = time.time()
ga_instance = GA(
heuristic_data,
size=temp_size,
num_generations=50,
out_range=5,
seed=i,
parent_selection_type="tournament",
crossover_type=crossover,
mutation_probability=mutation,
crossover_probability=crossover_prob
)
ga_instance.run()
size = ga_instance.PAPER_SIZE
trim = abs(ga_instance.fitness_values)
used = ga_instance.PAPER_SIZE+ga_instance.fitness_values
switcher = handle_switcher(used)
if switcher is not None:
size = switcher["new_roll"]
trim = switcher["new_trim"]
# print(size)
# print(used)
# print(trim)
if trim < 3:
passed_trim+=1
end_time = time.time()
elapsed_time = end_time - start_time
elapsed_times.append(elapsed_time)
update_acc_list(acc_list,ga_instance,hd_instance,elapsed_times)
# In[15]:
for i in acc_list:
print(i)
# In[ ]: