-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsweep.py
102 lines (76 loc) · 3.72 KB
/
sweep.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
import subprocess
import csv
import re
import numpy as np
def format_args(args):
return ["-D{0!s}={1!s}".format(name, value) for name, value in args.items()]
if __name__ == "__main__":
runs = 50
n_jobs = 4
# Function to run.
function_name = "SchaffersEvaluation"
base_population_count = 690
base_offspring_count = 3298
migration_fraction = 0.2
# Change this to actual counts
island_counts = [1, 2, 5, 10, 20, 30, 40, 50, 75, 100, 200, 500]
migration_intervals = [25, 50, 75, 100]
results_file = open("results-{0!s}.csv".format(function_name), "w")
results_writer = csv.DictWriter(results_file, fieldnames=["islandCount", "migrationInterval", "run", "generation", "diversity",])
results_writer.writeheader()
for island_count in island_counts:
for migration_interval in migration_intervals:
parameter_settings = {
"islandCount": island_count,
"populationCount": int(np.maximum(1, base_population_count // island_count)),
"offspringCount": int(np.maximum(1, base_offspring_count // island_count)),
"migrationCount": int(np.maximum(1, (base_population_count // island_count) * migration_fraction)),
"migrationInterval": migration_interval
}
for i in range(0, runs, n_jobs):
results = []
for _ in range(0, min(runs - i, n_jobs)):
processes = []
for j in range(np.minimum(n_jobs, runs - i)):
args = [
"docker", "run", "--rm", "-e", "LD_LIBRARY_PATH=/code", "ec",
"java",
] + format_args(parameter_settings) + [
"-jar", "testrun.jar", "-submission=player66",
"-evaluation={0!s}".format(function_name),
"-seed={0!s}".format(np.random.randint(2**31)),
]
processes.append(
subprocess.Popen(
args,
stdout=subprocess.PIPE
)
)
for process in processes:
process.wait()
output = process.stdout.readlines()
print(output)
if len(output) < 2:
continue
r = []
# Parse the output to get the required results.
for line in output:
m = re.match(r"Diversity for generation (?P<generation>\d+) is (?P<diversity>.*)", line.decode())
if m:
generation = int(m.group("generation"))
diversity = float(m.group("diversity"))
r.append({
"generation": generation,
"diversity": diversity
})
results.append(r)
for i, result in enumerate(results):
# Here you write out the results.
for r in result:
results_writer.writerow({
"islandCount": island_count,
"migrationInterval": migration_interval,
"run": i,
**r
})
results_file.flush()