-
Notifications
You must be signed in to change notification settings - Fork 0
/
krpsim_verif.py
124 lines (106 loc) · 4.35 KB
/
krpsim_verif.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
import os
import sys
import argparse
from Parser import Parser
from QLearningAgent import QLearningAgent
def parse_result(input_result: object):
inventory_begin = False
inventory = []
for line_number, line in enumerate(input_result, start=1):
line = line.strip()
if inventory_begin and (line.startswith("no more process") or line == "Stock :"):
break
if inventory_begin == True:
inventory.append(line.split(":"))
if line == "Main walk":
inventory_begin = True
result_stock = {}
for line_number, line in enumerate(input_result):
parts = line.strip().split(" => ")
# Check if there are two parts (key and value)
if len(parts) == 2:
key, value = parts
# Convert the value to an integer
try:
value = int(value)
except ValueError:
# Handle the case where the value is not an integer
pass
# Add the key-value pair to the dictionary
result_stock[key] = value
return inventory, result_stock
def load_file(input_filename: str) -> object:
if not os.path.exists(input_filename):
print(f"Error: File '{input_filename}' does not exist.")
exit()
input_file = open(input_filename, "r")
return input_file
def check_process_name(inventory, process):
for item in inventory:
if item[1] not in process:
return False
return True
def print_nice_stock(stock, mark):
print(f"Stock ({mark}) :")
for key, value in stock.items():
print(f" {key} => {value}")
def main():
argparser = argparse.ArgumentParser(
description="krpsim")
argparser.add_argument("file", nargs="?",
help="Path to the input file")
argparser.add_argument("result_to_test", nargs="?",
help="Text file containing the krpsim trace")
args = argparser.parse_args()
if args.file:
input_file = load_file(args.file)
agent = QLearningAgent()
parser = Parser(agent)
parser.parse(input_file)
agent.initial_stock = agent.stock
agent.print_initial_stocks()
if args.result_to_test:
input_result = load_file(args.result_to_test)
inventory, result_stock = parse_result(input_result)
if check_process_name(inventory, agent.process) is False:
print("Error: process name mismatch.")
sys.exit(1)
stock_copy = dict(agent.initial_stock)
begin_range = 0
prev_cycle = 0
process_todo = []
for i in range(len(inventory)):
print("Process", i, ":", inventory[i][1], "/ cycle:", inventory[i][0])
if prev_cycle != int(inventory[i][0]):
if prev_cycle > int(inventory[i][0]):
print("Error: Cycle number wrong")
sys.exit(1)
for j in range(begin_range, i):
process_todo.append(
[agent.process[inventory[j][1]].nb_cycle, inventory[j][1]])
to_remove = []
for k in range(len(process_todo)):
if (agent.process[process_todo[k][1]].nb_cycle + prev_cycle) <= int(inventory[i][0]) and not any(value < 0 for value in {
key: stock_copy[key] - agent.process[process_todo[k][1]].need.get(key, 0) for key in stock_copy}.values()):
stock_copy = {
key: stock_copy[key] - agent.process[process_todo[k][1]].need.get(key, 0) for key in stock_copy}
stock_copy = {
key: stock_copy[key] + agent.process[process_todo[k][1]].result.get(key, 0) for key in stock_copy}
to_remove.append(k)
flag = False
for k in reversed(to_remove):
if (agent.process[process_todo[k][1]].nb_cycle + prev_cycle) == int(inventory[i][0]):
flag = True
process_todo.pop(k)
if flag == False:
print("Error: incorrect number of cycle")
sys.exit(1)
begin_range = i
prev_cycle = int(inventory[i][0])
if any(value < 0 for value in stock_copy.values()):
print(
f"Error: Excueted process not valid.")
print_nice_stock(stock_copy, "")
sys.exit(1)
if __name__ == "__main__":
main()