• API
• FAQ
• Tools
• Archive
daily pastebin goal
32%
SHARE
TWEET

# Untitled

a guest Mar 25th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import matplotlib.pyplot as plt
3.
4. from GeneticAlgorithm import GeneticAlgorithm
6. from TTP import TTP
7.
8. def run_tests(tests_array):
9.     for test in tests_array:
10.         save_path = ("results/%s/gen=%d_pop=%d_px=%.2f_pm=%.2f_tour=%d_testCount=%d_%s_%s"
11.             % (test['test_name'], test['generations'], test['pop_size'], test['px'],
12.                test['pm'], test['tour'], test['testCount'], test['selection_type'], test['mutation_type']))
13.
14.         print("Starting test")
15.         print(test)
16.         dims, capacity, min_speed, max_speed, cities = load_data("data/" + test['test_name'] + ".ttp")
17.         ttp = TTP(dims, capacity, min_speed, max_speed, cities)
18.         ga = GeneticAlgorithm(test['pop_size'], test['generations'], test['px'], test['pm'], test['tour'],
19.         ttp, test['test_name'], selection_type=test['selection_type'], mutation_type=test['mutation_type'])
20.
21.         time = [i for i in range(test['generations']+1)]
22.
23.         bests, avgs, worsts = run_algorithm(ga, test['testCount'])
24.         save_plot(time, bests, avgs, worsts, save_path, test['testCount'])
25.         save_text_data(bests, avgs, worsts, save_path)
26.
27.
28. def run_algorithm(ga, testCount):
29.     bests, avgs, worsts = np.array([]), np.array([]), np.array([])
30.     for i in range(testCount):
31.         print("Test - %d" % (i))
32.         b, a, w = ga.run()
33.
34.         if i == 0:
35.             bests = np.asarray(b)
36.             avgs = np.asarray(a)
37.             worsts = np.asarray(w)
38.         else:
39.             bests = np.column_stack((bests, np.asarray(b)))
40.             avgs = np.column_stack((avgs, np.asarray(a)))
41.             worsts = np.column_stack((worsts, np.asarray(w)))
42.
43.     return bests, avgs, worsts
44.
45.
46. def save_plot(time, bests, avgs, worsts, path, testCount):
47.     plt.plot(time, np.mean(bests, axis=1), label="Najlepszy")
48.     plt.plot(time, np.mean(avgs, axis=1), label="Średni")
49.     plt.plot(time, np.mean(worsts, axis=1), label="Najgorszy")
50.     plt.xlabel("GENERACJA")
51.     plt.ylabel("FITNESS")
52.     plt.title("Średni fitness w generacji dla %d prób" % (testCount))
53.     plt.legend(loc='lower right')
54.     plt.savefig(path + ".png")
55.     plt.close()
56.
57.
58. def save_text_data(bests, avgs, worsts, path):
59.     with open(path + ".txt", 'w') as file:
60.         file.write('Średnia fitnessu najlepszych: %f\n' % (np.mean(bests)))
61.         file.write('Odchylenie standardowe dla najlepszych: %f\n' % (np.std(bests)))
62.         file.write('Średnia fitnessu w populacji: %f\n' % (np.mean(avgs)))
63.         file.write('Odchylenie standardowe dla najlepszych: %f\n' % (np.std(avgs)))
64.         file.write('Sreðnia fitnessu najgorszych: %f\n' % (np.mean(worsts)))
65.         file.write('Odchylenie standardowe dla najlepszych: %f\n' % (np.std(worsts)))
66.
67.         file.close()
68.
69. tests = [
70.     {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
71.      "tour": 5, "test_name": "trivial_0", "selection_type": "tournament",
72.      "mutation_type": "swap", "testCount": 10},
73.     {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
74.      "tour": 5, "test_name": "trivial_1", "selection_type": "tournament",
75.      "mutation_type": "swap", "testCount": 10},
76.     #end of trivial_tests
77.     {"pop_size": 100, "generations": 200, "px": 0.75, "pm": 0.01,
78.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
79.      "mutation_type": "swap", "testCount": 10},
80.     {"pop_size": 100, "generations": 200, "px": 0.75, "pm": 0.01,
81.      "tour": 5, "test_name": "medium_3", "selection_type": "roulette",
82.      "mutation_type": "swap", "testCount": 10},
83.     #end of selection method tests
84.     {"pop_size": 20, "generations": 100, "px": 0.75, "pm": 0.01,
85.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
86.      "mutation_type": "swap", "testCount": 10},
87.     {"pop_size": 50, "generations": 100, "px": 0.75, "pm": 0.01,
88.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
89.      "mutation_type": "swap", "testCount": 10},
90.     {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
91.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
92.      "mutation_type": "swap", "testCount": 10},
93.     {"pop_size": 500, "generations": 100, "px": 0.75, "pm": 0.01,
94.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
95.      "mutation_type": "swap", "testCount": 10},
96.     {"pop_size": 1000, "generations": 100, "px": 0.75, "pm": 0.01,
97.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
98.      "mutation_type": "swap", "testCount": 10},
99.     # end of population size tests
100.     {"pop_size": 100, "generations": 20, "px": 0.75, "pm": 0.01,
101.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
102.      "mutation_type": "swap", "testCount": 10},
103.     {"pop_size": 100, "generations": 50, "px": 0.75, "pm": 0.01,
104.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
105.      "mutation_type": "swap", "testCount": 10},
106.     {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
107.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
108.      "mutation_type": "swap", "testCount": 10},
109.     {"pop_size": 100, "generations": 500, "px": 0.75, "pm": 0.01,
110.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
111.      "mutation_type": "swap", "testCount": 10},
112.     {"pop_size": 100, "generations": 1000, "px": 0.75, "pm": 0.01,
113.      "tour": 5, "test_name": "medium_3", "selection_type": "tournament",
114.      "mutation_type": "swap", "testCount": 10},
115.     # end of generation count tests
116.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
117.     #  "tour": 5, "test_name": "hard_0", "selection_type": "tournament",
118.     #  "mutation_type": "swap", "testCount": 10},
119.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
120.     #  "tour": 5, "test_name": "hard_1", "selection_type": "tournament",
121.     #  "mutation_type": "swap", "testCount": 10},
122.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
123.     #  "tour": 5, "test_name": "hard_2", "selection_type": "tournament",
124.     #  "mutation_type": "swap", "testCount": 10},
125.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
126.     #  "tour": 5, "test_name": "hard_3", "selection_type": "tournament",
127.     #  "mutation_type": "swap", "testCount": 10},
128.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
129.     #  "tour": 5, "test_name": "hard_4", "selection_type": "tournament",
130.     #  "mutation_type": "swap", "testCount": 10},
131.     #  #end of hard tests
132.     # {"pop_size": 100, "generations": 100, "px": 0.75, "pm": 0.01,
133.     #  "tour": 5, "test_name": "hard_0", "selection_type": "tournament",
134.     #  "mutation_type": "swap", "testCount": 10}
135. ]
136.
137. if __name__ == '__main__':
138.     run_tests(tests)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top