-
Notifications
You must be signed in to change notification settings - Fork 0
/
HRH_AG_RS_main.py
66 lines (54 loc) · 2.57 KB
/
HRH_AG_RS_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
from SimulatedAnnealing import SA
from HRH_AG_RS import HRH_RS
from item import Item
from random import shuffle
from datetime import datetime
import json
def log(message, end=None):
print(message, flush=True, end=end)
if __name__ == '__main__':
datasets = [
{"name": "HARD9.txt", "solution":56, "results": {}},
{"name": "HARD0.txt", "solution":56, "results": {}},
{"name": "HARD6.txt", "solution":57, "results": {}},
{"name": "HARD7.txt", "solution":55, "results": {}},
{"name": "N1C1W4_E.txt", "solution":35, "results": {}},
{"name": "N1C2W2_H.txt", "solution":23, "results": {}},
{"name": "N1C3W1_C.txt", "solution":17, "results": {}},
{"name": "N2C1W1_G.txt", "solution":60, "results": {}},
{"name": "N2C2W1_F.txt", "solution":49, "results": {}},
{"name": "N2C3W1_Q.txt", "solution":34, "results": {}},
{"name": "N3C1W1_K.txt", "solution":102, "results": {}},
{"name": "N1W4B2R3.txt", "solution":6, "results": {}},
{"name": "N2W1B3R8.txt", "solution":34, "results": {}},
{"name": "N3W3B3R1.txt", "solution":27, "results": {}},
{"name": "N4W4B3R9.txt", "solution":56, "results": {}},
]
# Loop through each data set.
for dataset in datasets:
# Read the data into memory
with open('datasets/{}'.format(dataset["name"]), 'r') as file:
data = file.read().splitlines()
num_items, capacity, items = int(data[0]), int(data[1]), data[2:]
log("\n\nDATASET {}: num_items {} capacity {} items_read {}".format(dataset["name"], num_items, capacity, len(items)))
items = [Item(size=int(i)) for i in items]
log(" Iteration", end=" ")
# Perform 30 independent iterations.
for iteration in range(1):
log(iteration+1, end=" ")
# Randomize the order of the items in the item list.
shuffle(items)
thing = HRH_RS(capacity, items)
start_time = datetime.now()
sa = thing.run()
execution_time = datetime.now() - start_time
# Record the relevant data for analysis
summary = {
"execution_time": str(execution_time),
"num_bins": len(sa.bins),
}
dataset["results"].setdefault("HRH_RS", []).append(summary)
#dataset["results"].setdefault("SA", []).append(summary)
# Write the captured data to disk.
with open("./resultats/results_HRH_AG-RS.json", "w") as file:
file.write(json.dumps(datasets, indent=2))