Skip to content

Commit af7bdbb

Browse files
authored
Merge pull request #4 from Wwarrior1/parameters_tester
Parameters tester
2 parents 5efe928 + 33b1db7 commit af7bdbb

File tree

5 files changed

+137
-35
lines changed

5 files changed

+137
-35
lines changed

algorithm/algorithm.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44

55
class Algorithm:
6-
76
@staticmethod
8-
def execute(data):
7+
def execute(data, n, m, e, nep, nsp, ngh, iterations):
98

109
"""
1110
Pomaga myslenie o rozwiazaniach jako o kwiatkach. Kazda pszczola siedzi w danym momencie na jednym konkretnym
@@ -21,20 +20,10 @@ def execute(data):
2120
max_iter - liczba iteracji
2221
"""
2322

24-
n = 25
25-
m = 9
26-
e = 3
27-
nep = 9
28-
nsp = 3
29-
ngh = 1
30-
max_iter = 1000
31-
3223
# inicjalizujemy pule losowymi rozwiazaniami
3324
pool = [AlgorithmUtils.random_solution(data) for _ in range(n)]
3425

35-
for i in range(max_iter):
36-
print("Iter ", i)
37-
26+
for i in range(iterations):
3827
# dla kazdego rozwiazania obliczamy wynik - ukladamy je od najlepszych do najgorszych
3928
solutions_ranking = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
4029
# najlepsze e rozwiazan to elitarne...
@@ -54,11 +43,11 @@ def execute(data):
5443
for gs in good_solutions:
5544
pool.append(AlgorithmUtils.best_in_neighbourhood(data, gs, ngh, nsp))
5645
# dla pszczol ktore byly slabe wybieramy dla nich nowe, calkowicie losowe rozwiazania (leca gdzie indziej)
57-
for _ in range(n-m):
46+
for _ in range(n - m):
5847
pool.append(AlgorithmUtils.random_solution(data))
5948

60-
# podsumowujac - jesli np. n=5, m=3, e=1, to zawsze na koniec iteracji 1 pszczola pochodzi z obszaru
61-
# elitarnego, 2 z dobrych, a 2 sa losowane od nowa.
49+
# podsumowujac - jesli np. n=5, m=3, e=1, to zawsze na koniec iteracji 1 pszczola pochodzi z obszaru
50+
# elitarnego, 2 z dobrych, a 2 sa losowane od nowa.
6251

6352
pool = sorted(pool, key=lambda s: calculate_score(data, s), reverse=True)
6453
return pool[0]

main.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1+
from argparse import ArgumentParser
2+
from os.path import split
3+
from time import time
4+
15
from algorithm.algorithm import Algorithm
26
from parsers.input_parser import parse_input
3-
from utils.solution_checker import check_solution
4-
from parsers.output_builder import save_solution
5-
import time
7+
from parsers.output_builder import save_solution, save_execution_parameters
8+
from utils.argument_parser_util import add_parser_arguments, str2bool
9+
from utils.solution_checker import check_solution, calculate_score
610

711

812
def main():
9-
input_file = "files/videos_worth_spreading.in"
10-
# input_file = "files/kittens.in"
11-
# input_file = "files/example1.in"
12-
input_file = "files/me_at_the_zoo.in"
13-
# input_file = "files/trending_today.in"
14-
15-
# solution_file = "files/example1.out"
16-
# solution = check_solution(input_file, solution_file)
17-
# print(solution)
13+
input_file, output_file = args.i, args.o
14+
n, m, e = args.n, args.m, args.e
15+
nep, nsp, ngh = args.nep, args.nsp, args.ngh
16+
iterations = args.max
17+
should_save = str2bool(args.s)
1818

19-
before = time.time()
20-
best_solution = Algorithm.execute(parse_input(input_file))
21-
save_solution(best_solution, "files/out1.out")
22-
score = check_solution(input_file, "files/out1.out")
23-
after = time.time()
19+
before = time()
20+
data = parse_input(input_file)
21+
best_solution = Algorithm.execute(data, n, m, e, nep, nsp, ngh, iterations)
22+
if should_save:
23+
save_solution(best_solution, output_file)
24+
score = check_solution(input_file, output_file)
25+
else:
26+
score = calculate_score(data, best_solution)
27+
filename = split(input_file)[1]
28+
save_execution_parameters(";", filename, n, m, e, nep, nsp, ngh, iterations, score, output_file)
29+
after = time()
2430

2531
print(best_solution)
2632
print("-----------------------------")
27-
print("Algorithm took: " + str(round(after-before, 2)) + " s")
33+
print("Algorithm took: " + str(round(after - before, 2)) + " s")
2834
print("-----------------------------")
2935
print("Saved time: " + str(round(score / 1000, 2)) + " s (score: " + str(score) + ")")
3036

31-
if __name__ == '__main__':
37+
38+
if __name__ == "__main__":
39+
parser = ArgumentParser(description="Distributed cache problem solver.")
40+
add_parser_arguments(parser)
41+
args = parser.parse_args()
3242
main()

parsers/output_builder.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ def save_solution(solution, name):
33
f.write(str(len(solution)) + "\n")
44
for s in solution:
55
f.write(str(s) + " " + " ".join([str(v) for v in solution[s]]) + "\n")
6+
7+
8+
def save_execution_parameters(separator, file, n, m, e, nep, nsp, ngh, iterations, score, output_file):
9+
with open(output_file, 'a') as f:
10+
f.write(separator.join(str(x) for x in (file, n, m, e, nep, nsp, ngh, iterations, score)) + "\n")
11+
12+
13+
def clear_results_file(separator, output_file):
14+
with open(output_file, 'w') as out:
15+
out.write(separator.join(("file", "n", "m", "e", "nep", "nsp", "ngp", "iterations", "score")) + "\n")

utils/argument_parser_util.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from argparse import ArgumentTypeError
2+
from os.path import join
3+
4+
from os import getcwd
5+
6+
7+
def add_parser_arguments(parser):
8+
default_input_filename = "me_at_the_zoo.in"
9+
parser.add_argument("-i", "-input_file", help="Path to input file.", type=str,
10+
default=join(getcwd(), "files", default_input_filename))
11+
12+
default_output_filename = "solution.out"
13+
parser.add_argument("-o", "-output_file", help="Path to output file.", type=str,
14+
default=join(getcwd(), "files", default_output_filename))
15+
16+
default_n = 25
17+
parser.add_argument("-n", "-bees", help="Amount of bees.", type=int,
18+
default=default_n)
19+
20+
default_m = 9
21+
parser.add_argument("-m", "-good_solutions", help="Amount of good solutions.", type=int,
22+
default=default_m)
23+
24+
default_e = 3
25+
parser.add_argument("-e", "-elite_solutions", help="Amount of elite solutions.", type=int,
26+
default=default_e)
27+
28+
default_nep = 9
29+
parser.add_argument("-nep", help="???", type=int,
30+
default=default_nep)
31+
32+
default_nsp = 3
33+
parser.add_argument("-nsp", help="???", type=int,
34+
default=default_nsp)
35+
36+
default_ngh = 1
37+
parser.add_argument("-ngh", help="Neighborhood radius.", type=int,
38+
default=default_ngh)
39+
40+
default_max_iter = 100
41+
parser.add_argument("-max", "-iterations", help="Amount of iterations.", type=int,
42+
default=default_max_iter)
43+
44+
default_s = "True"
45+
parser.add_argument("-s", "-save_solution", help="Should solution be saved?", type=str,
46+
default=default_s)
47+
48+
49+
def str2bool(v):
50+
if v.lower() in ('yes', 'true', 't', 'y', '1'):
51+
return True
52+
if v.lower() in ('no', 'false', 'f', 'n', '0'):
53+
return False
54+
else:
55+
raise ArgumentTypeError('Boolean value expected.')

utils/parameters_tester.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from os.path import join, split
2+
from subprocess import call
3+
from time import time
4+
5+
from concurrent.futures import ProcessPoolExecutor
6+
from os import getcwd
7+
8+
from parsers.output_builder import clear_results_file
9+
10+
11+
def main():
12+
main_path = __get_path()
13+
input_dir = join(split(getcwd())[0], "files")
14+
input_file = join(input_dir, "me_at_the_zoo.in")
15+
output_dir = join(split(getcwd())[0], "files")
16+
output_file = join(output_dir, "results.csv")
17+
clear_results_file(';', output_file)
18+
before = time()
19+
with ProcessPoolExecutor(None) as executor:
20+
# todo make ranges and steps as parameters? maybe add more parameters to manipulate
21+
for bees in range(10, 11, 10):
22+
for iterations in range(100, 150, 50):
23+
for tries_with_same_parameters in range(0, 10):
24+
command = "python {0} -i {1} -o {2} -n {3} -max {4} -s False" \
25+
.format(main_path, input_file, output_file, bees, iterations)
26+
executor.submit(call, command)
27+
after = time()
28+
print("Execution took: " + str(round(after - before, 2)) + " s")
29+
30+
31+
def __get_path():
32+
directory = split(getcwd())[0]
33+
filename = "main.py"
34+
return join(directory, filename)
35+
36+
37+
if __name__ == '__main__':
38+
main()

0 commit comments

Comments
 (0)