-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKrpsim.py
152 lines (133 loc) · 5.15 KB
/
Krpsim.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
import copy
import time
class Krpsim:
def __init__(self, agent, delay, verbose, random=False):
self.inventory = []
self.agent = agent.copy()
self.delay = delay
self.stock = (agent.stock)
self.verbose = verbose
self.random = random
@property
def stock(self):
return self._stock
@stock.setter
def stock(self, stock):
if isinstance(stock, dict):
self._stock = dict(stock)
else:
raise ValueError('Stock must be a dictionary')
def copy(self):
return copy.copy(self)
def generate_inventory(self):
if len(self.agent.get_available_process_lst()) == 0:
return self
i = 0
while True and i < 1000:
if self.random == False:
walk = self.agent.generate_inventory(self.inventory, self.delay)
else:
walk = self.agent.generate_walk(self.inventory, self.delay)
if walk == None or len(walk) == 0:
self.stock = self.agent.stock
self.agent.stock = self.agent.initial_stock
i += 1
continue
self.inventory.extend(walk)
if self.verbose:
print('agent stock')
self.agent.print_stocks(self.agent.stock)
self.stock = self.agent.stock
self.agent.stock = self.agent.initial_stock
if self.inventory != None and len(self.inventory) != 0:
break
if i >= 1000:
self.inventory.clear()
self.inventory = list(self.agent.walk)
return self
def run(self) -> None:
if self.verbose:
print('start')
print('inventory.stock:', self.stock)
else:
print("Evaluating ", end='')
stock = self.optimize()
print(" done.")
if self.agent.finite and 'time' in self.agent.optimize:
stock = self.optimize_time(stock)
self.print_trace(stock)
def optimize(self) -> dict:
prev_indi = self.agent.copy()
prev_indi.init_stocks()
indi = self.copy()
# for time measurement
dot_interval = 0.5
start_time = time.time()
next_dot_time = start_time + dot_interval
# for finite checking:
self.agent.finite = False
while indi.stock != prev_indi.stock:
prev_indi = indi
new_indi = indi.copy()
new_indi.agent.stock = dict(indi.stock)
new_indi.agent.initial_stock = dict(indi.stock)
new_indi.generate_inventory()
if self.verbose:
print('new stock:', new_indi.stock)
indi = new_indi.copy()
current_time = time.time()
if current_time >= next_dot_time:
print('.', end='', flush=True)
next_dot_time = current_time + dot_interval
if current_time - start_time >= self.delay:
break
elif indi.stock == prev_indi.stock or len(indi.agent.get_available_process_lst()) == 0:
self.agent.finite = True
break
self.inventory = list(new_indi.inventory)
return dict(new_indi.stock)
def optimize_time(self, stock_dict: dict) -> dict:
min_total_cycle = float('inf')
inventory = []
time_stock = []
for i in range(80):
if len(self.inventory) != 0:
total_cycle = self.inventory[-1][1]
total_cycle += int(
self.agent.process[self.inventory[len(self.inventory) - 1][0]].nb_cycle)
else:
total_cycle = float('inf')
if min_total_cycle > total_cycle:
min_total_cycle = total_cycle
inventory = list(self.inventory)
time_stock = dict(stock_dict)
elif min_total_cycle == total_cycle:
for optimize in self.agent.optimize:
if optimize != 'time' and len(time_stock) != 0 and time_stock[optimize] < stock[optimize]:
inventory = list(self.inventory)
time_stock = dict(stock_dict)
self.agent.cycle = 0
self.inventory.clear()
stock = self.optimize()
self.inventory.clear()
self.inventory = list(inventory)
return time_stock
def print_trace(self, stock_dict: dict) -> None:
print("Main walk")
if len(self.inventory) != 0:
total_cycle = self.inventory[-1][1]
else:
total_cycle = 0
biggest_cycle = 0
for item in self.inventory:
if (int(self.agent.process[item[0]].nb_cycle) > biggest_cycle):
biggest_cycle = int(self.agent.process[item[0]].nb_cycle)
print(f"{item[1]}:{item[0]}")
if len(self.inventory) != 0:
total_cycle += int(
self.agent.process[self.inventory[len(self.inventory) - 1][0]].nb_cycle)
if self.agent.finite is True:
print(f"no more process doable at time {total_cycle + 1}")
print("Stock :")
for key, value in stock_dict.items():
print(f" {key} => {value}")