-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (118 loc) · 6.68 KB
/
main.py
File metadata and controls
135 lines (118 loc) · 6.68 KB
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
import pathlib
import sys
import time
from pathlib import Path
from setup_problem import batch_generate
from knapsack import knapsack
if __name__ == "__main__":
if len(sys.argv) < 1:
print(f"Usage: {sys.argv[0]} [parameter_file] [exclude_baseline]")
sys.exit(1)
input_path = Path(sys.argv[1]) if len(sys.argv) >= 2 else Path.cwd() / "parameters"
json_files = []
if input_path.is_file():
json_files = [input_path]
elif input_path.is_dir():
json_files = sorted(list(input_path.glob("*.json")))
else:
input_path.mkdir(parents=True, exist_ok=True)
with open("results.txt", "a+", encoding="utf-8") as f:
f.seek(0)
content = f.read()
existing_recursive_results_count = content.count("| R |")
existing_recursive_td_results_count = content.count("| RTD |")
existing_iterative_bu_results_count = content.count("| IBU |")
existing_iterative_bu_opt_results_count = content.count("| IBUOPT |")
existing_bnb_results_count = content.count("| BNB |")
if len(sys.argv) >= 3:
skip_recursive = True
skip_recursive_td = True
skip_iterative_bu = True
print("Skipping recursive baseline for more complex knapsack problems resolution")
if not json_files:
print(f"Directory {input_path} is empty. Generating instances...")
input_path.mkdir(parents=True, exist_ok=True)
batch_generate(
n_instances=15,
parameters_seed=42,
max_capacity=100000,
max_profit=100,
max_weight=100,
nb_items=200,
output_dir=input_path)
json_files = sorted(list(input_path.glob("*.json")))
else:
skip_recursive = (existing_recursive_results_count >= len(json_files))
skip_recursive_td = (existing_recursive_td_results_count >= len(json_files))
skip_iterative_bu = (existing_iterative_bu_results_count >= len(json_files))
if not json_files:
print(f"Directory {input_path} is empty. Generating instances for problem with baseline computation...")
input_path.mkdir(parents=True, exist_ok=True)
batch_generate(n_instances=15, parameters_seed=42, output_dir=input_path)
json_files = sorted(list(input_path.glob("*.json")))
skip_iterative_bu_opt = (existing_iterative_bu_opt_results_count >= len(json_files))
skip_bnb = (existing_bnb_results_count >= len(json_files))
if json_files is not None:
for json_file in json_files:
time.sleep(0.1)
if not skip_recursive:
print(f"Treating knapsack problem recursively for parameters in {json_file}")
solution, capacity_left, metrics = knapsack(json_file, "recursive")
f.write(
f"Parameters file: {json_file.name} | R | Solution: {solution} of profit, {capacity_left} of "
f"capacity left | {metrics}")
f.flush() # Ensure data is written to disk immediately
time.sleep(0.1) # Prevent thermal noise
time.sleep(0.1)
if not skip_recursive_td:
print(f"Treating knapsack problem recursively with Top-Down design for parameters in {json_file}")
solution, capacity_left, metrics = knapsack(json_file, "recursive_td")
f.write(
f"Parameters file: {json_file.name} | RTD | Solution: {solution} of profit, {capacity_left} of "
f"capacity left | {metrics}")
f.flush() # Ensure data is written to disk immediately
time.sleep(0.1) # Prevent thermal noise
time.sleep(0.1)
if not skip_iterative_bu:
print(f"Treating knapsack problem iteratively with Bottom-Up design for parameters in {json_file}")
solution, capacity_left, metrics = knapsack(json_file, "iterative_bu")
f.write(
f"Parameters file: {json_file.name} | IBU | Solution: {solution} of profit, {capacity_left} of "
f"capacity left | {metrics}")
f.flush() # Ensure data is written to disk immediately
time.sleep(0.1) # Prevent thermal noise
time.sleep(0.1)
if not skip_iterative_bu_opt:
print(f"Treating knapsack problem iteratively with Bottom-Up design space optimized, "
f"for parameters in {json_file}")
solution, capacity_left, metrics = knapsack(json_file, "iterative_bu_opt")
f.write(
f"Parameters file: {json_file.name} | IBUOPT | Solution: {solution} of profit, {capacity_left} "
f"of capacity left | {metrics}")
f.flush() # Ensure data is written to disk immediately
time.sleep(0.1) # Prevent thermal noise
time.sleep(0.1)
if not skip_bnb:
print(f"Treating knapsack problem with branch and bound logic, "
f"for parameters in {json_file}")
solution, capacity_left, metrics = knapsack(json_file, "bnb")
f.write(
f"Parameters file: {json_file.name} | BNB | Solution: {solution} of profit, {capacity_left} "
f"of capacity left | {metrics}")
f.flush() # Ensure data is written to disk immediately
time.sleep(0.1) # Prevent thermal noise
if skip_recursive:
print(f"Skipped recursive : {existing_recursive_results_count} results already found in "
f"results.txt.")
if skip_recursive_td:
print(f"Skipped recursive Top-Down : {existing_recursive_td_results_count} results already found in "
f"results.txt.")
if skip_iterative_bu:
print(f"Skipped iterative Bottom-Up : {existing_recursive_td_results_count} results already found in "
f"results.txt.")
if skip_iterative_bu_opt:
print(f"Skipped iterative Bottom-Up space optimized : {existing_recursive_td_results_count} results "
f"already found in results.txt.")
if skip_bnb:
print(f"Skipped Branch and bound : {existing_bnb_results_count} results already found in "
f"results.txt.")