-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (56 loc) · 2.13 KB
/
main.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
import os
import sys
import argparse
from Parser import Parser
from QLearningAgent import QLearningAgent
from Krpsim import Krpsim
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 main():
argparser = argparse.ArgumentParser(
description="krpsim")
argparser.add_argument("input_filename", nargs="?",
help="Path to the input file")
argparser.add_argument("delay", nargs="?",
help="the waiting time the program will not have to exceed")
argparser.add_argument("-g", "--graph", action="store_true",
help="Visualize the graph")
argparser.add_argument("-v", "--verbose", action="store_true",
help="Visualize the graph")
argparser.add_argument("-r", "--random", action="store_true",
help="Optimize randomly")
args = argparser.parse_args()
if args.input_filename is None or args.delay is None:
argparser.print_help()
sys.exit(1)
try:
delay_seconds = int(args.delay)
except ValueError:
print(f"Error: '{args.delay}' is not a valid integer for delay")
sys.exit(1)
if args.input_filename:
input_file = load_file(args.input_filename)
agent = QLearningAgent()
parser = Parser(agent)
if parser.parse(input_file) == False:
sys.exit(1)
agent.initial_stock = agent.stock
if args.verbose:
agent.print_initial_stocks()
agent.init_agent(args.verbose)
print(
f"Nice file! {len(agent.process)} processes, {len(agent.initial_stock)} stocks, {len(agent.optimize)} to optimize")
if args.verbose:
print("\nPrint Base info:\n")
print(agent)
krpsim = Krpsim(agent, delay_seconds, args.verbose, args.random)
krpsim.run()
if args.graph:
agent.create_graph()
agent.visualize_graph()
if __name__ == "__main__":
main()