mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 17:43:12 +02:00
fix: calculating time for generation and not for dislpaying plot
This commit is contained in:
parent
2156caa787
commit
d13e681cb9
36
lab3/main.py
36
lab3/main.py
@ -52,10 +52,13 @@ def evolution_strategy(
|
||||
mutation_strength=0.1,
|
||||
number_of_generations=123,
|
||||
min_max=(-5.12, 5.12),
|
||||
number_of_outputs = 10
|
||||
number_of_outputs = 10,
|
||||
no_display = False
|
||||
):
|
||||
""" Define the Evolutionary Strategy (μ, λ) algorithm """
|
||||
# Initialize the population
|
||||
total_time = 0
|
||||
start_time = time.perf_counter()
|
||||
population = np.random.uniform(
|
||||
low=min_max[0], high=min_max[1], size=(
|
||||
size_of_population, 2))
|
||||
@ -76,18 +79,24 @@ def evolution_strategy(
|
||||
if number_of_generations % number_of_outputs == 0 \
|
||||
else number_of_generations//(number_of_outputs-1)
|
||||
offset = number_of_generations % step
|
||||
if (generation_number - offset) % step == 0:
|
||||
end_time = time.perf_counter()
|
||||
total_time += end_time - start_time
|
||||
if (generation_number - offset) % step == 0 and not no_display:
|
||||
output(population, generation_number, f"{generation_number}:nop_{number_of_parents}:sop_{size_of_population}:ms_{mutation_strength}:nog_{number_of_generations}:min_max_{min_max}:noo_{number_of_outputs}")
|
||||
summary.append(population)
|
||||
|
||||
print_summary(summary, f"{generation_number}:nop-{number_of_parents}:sop-{size_of_population}:ms-{mutation_strength}:nog-{number_of_generations}:min-max-{min_max}:noo-{number_of_outputs}")
|
||||
if not no_display:
|
||||
print_summary(summary, f"{generation_number}:nop-{number_of_parents}:sop-{size_of_population}:ms-{mutation_strength}:nog-{number_of_generations}:min-max-{min_max}:noo-{number_of_outputs}")
|
||||
start_time = time.perf_counter()
|
||||
# Evaluate the fitness of the final population
|
||||
fitness = np.array([rastrigin(x_point_value, y_point_value)
|
||||
for x_point_value, y_point_value in population])
|
||||
|
||||
# Return the best individual found
|
||||
best_idx = np.argmin(fitness)
|
||||
return population[best_idx], fitness[best_idx], population
|
||||
end_time = time.perf_counter()
|
||||
total_time += end_time - start_time
|
||||
return population[best_idx], fitness[best_idx], population, total_time
|
||||
|
||||
|
||||
def print_help():
|
||||
@ -115,6 +124,8 @@ def print_help():
|
||||
-max --max_value [number]
|
||||
-noo, --number_of_outputs [number]
|
||||
Those arguments can be given in any order and any argument which was not entered will be replaced with default value,
|
||||
Additional flags:
|
||||
-nd, --no-display (does not show the plots)
|
||||
exemplary use:
|
||||
python main.py -nop 5 -sop 20 -ms 0.1 -i 100 -min -5.12 -max 5.12 -noo 100
|
||||
""")
|
||||
@ -251,7 +262,8 @@ def user_input():
|
||||
"number_of_generations": 100,
|
||||
"min": -5.12,
|
||||
"max": 5.12,
|
||||
"number_of_outputs": 10}
|
||||
"number_of_outputs": 10,
|
||||
"no_display": False}
|
||||
for index, argument in enumerate(sys.argv):
|
||||
if argument in ('-h', '--help'):
|
||||
print_help()
|
||||
@ -270,6 +282,8 @@ def user_input():
|
||||
arguments["max"] = float(sys.argv[index+1])
|
||||
if argument in ('-noo', '--number_of_outputs'):
|
||||
arguments["number_of_outputs"] = int(sys.argv[index + 1])
|
||||
if argument in ('-nd', '--no_display'):
|
||||
arguments["no_display"] = True
|
||||
|
||||
return arguments
|
||||
|
||||
@ -278,20 +292,18 @@ def user_input():
|
||||
if __name__ == "__main__":
|
||||
# Run the Evolutionary Strategy algorithm
|
||||
ARGUMENTS = user_input()
|
||||
start_time = time.perf_counter()
|
||||
best_individual, best_fitness, output_population = evolution_strategy(
|
||||
best_individual, best_fitness, output_population, generation_time = evolution_strategy(
|
||||
ARGUMENTS["number_of_parents"],
|
||||
ARGUMENTS["size_of_population"],
|
||||
ARGUMENTS["mutation_strength"],
|
||||
ARGUMENTS["number_of_generations"],
|
||||
(ARGUMENTS["min"], ARGUMENTS["max"]),
|
||||
ARGUMENTS["number_of_outputs"])
|
||||
end_time = time.perf_counter()
|
||||
total_generation_time = end_time - start_time
|
||||
time_per_generation = total_generation_time / \
|
||||
ARGUMENTS["number_of_outputs"],
|
||||
ARGUMENTS["no_display"])
|
||||
time_per_generation = generation_time / \
|
||||
ARGUMENTS["number_of_generations"]
|
||||
|
||||
print("Best individual found:", best_individual)
|
||||
print("Best fitness found:", best_fitness)
|
||||
print("total_generation_time: ", total_generation_time)
|
||||
print("total_generation_time: ", generation_time)
|
||||
print("time_per_generation: ", time_per_generation)
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
\HyPL@Entry{0<</S/D>>}
|
||||
\@writefile{toc}{\contentsline {section}{\numberline {1}Exercise Variant 2 - "Rastrigin function"}{1}{section.1}\protected@file@percent }
|
||||
\@writefile{toc}{\contentsline {section}{\numberline {2}Implementation}{1}{section.2}\protected@file@percent }
|
||||
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Exemplary plot halfway through generation with parameters \\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) }}{2}{figure.1}\protected@file@percent }
|
||||
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Exemplary plot halfway through generation with parameters \\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) noo 10 }}{2}{figure.1}\protected@file@percent }
|
||||
\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Exemplary summary plot with parameters\\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) noo 10 }}{3}{figure.2}\protected@file@percent }
|
||||
\@writefile{toc}{\contentsline {section}{\numberline {3}Results}{3}{section.3}\protected@file@percent }
|
||||
\gdef \@abspage@last{3}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
# Fdb version 4
|
||||
["xdvipdfmx"] 1681756657 "EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" "EARIN_LAB_3_RUDNICKI_KLISZKO.pdf" "EARIN_LAB_3_RUDNICKI_KLISZKO" 1681756658 0
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" 1681756657 27840 dd68a4ffd83ce7a718d16a45334a4966 "xelatex"
|
||||
["xdvipdfmx"] 1681756905 "EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" "EARIN_LAB_3_RUDNICKI_KLISZKO.pdf" "EARIN_LAB_3_RUDNICKI_KLISZKO" 1681756905 0
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" 1681756905 30192 bd831cc7fba44cf2e46d93256fd6cbfe "xelatex"
|
||||
(generated)
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.pdf"
|
||||
(rewritten before read)
|
||||
["xelatex"] 1681756657 "/home/kuchy/earin/EARIN/lab3/report/EARIN_LAB_3_RUDNICKI_KLISZKO.tex" "EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" "EARIN_LAB_3_RUDNICKI_KLISZKO" 1681756658 0
|
||||
"/home/kuchy/earin/EARIN/lab3/report/EARIN_LAB_3_RUDNICKI_KLISZKO.tex" 1681756657 2911 e46de053342f55766dd21ffa226b1f81 ""
|
||||
["xelatex"] 1681756905 "/home/kuchy/earin/EARIN/lab3/report/EARIN_LAB_3_RUDNICKI_KLISZKO.tex" "EARIN_LAB_3_RUDNICKI_KLISZKO.xdv" "EARIN_LAB_3_RUDNICKI_KLISZKO" 1681756905 0
|
||||
"/home/kuchy/earin/EARIN/lab3/report/EARIN_LAB_3_RUDNICKI_KLISZKO.tex" 1681756904 3239 1c19d3382eb02eca12f28f266179f0f0 ""
|
||||
"/usr/share/texmf-dist/fonts/map/fontname/texfonts.map" 1679564905 3524 cb3e574dea2d1052e39280babc910dc8 ""
|
||||
"/usr/share/texmf-dist/fonts/tfm/adobe/zapfding/pzdr.tfm" 1679564905 1528 f853c4d1b4e0550255e02831fdc8496f ""
|
||||
"/usr/share/texmf-dist/fonts/tfm/public/cm/cmmi12.tfm" 1679564905 1524 4414a8315f39513458b80dfc63bff03a ""
|
||||
@ -66,10 +66,11 @@
|
||||
"/usr/share/texmf-dist/tex/latex/url/url.sty" 1679564905 12796 8edb7d69a20b857904dd0ea757c14ec9 ""
|
||||
"/usr/share/texmf-dist/web2c/texmf.cnf" 1679564905 39911 2da6c67557ec033436fe5418a70a8a61 ""
|
||||
"/var/lib/texmf/web2c/xetex/xelatex.fmt" 1680438638 11046058 c421129080a65de742470a82b56f326d ""
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.aux" 1681756657 1099 5eff8fa639af3de13b18290c7e4b7d95 "xelatex"
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.out" 1681756657 454 50b668959753031c85a7636eb970de0c "xelatex"
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.tex" 1681756657 2911 e46de053342f55766dd21ffa226b1f81 ""
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.aux" 1681756905 1319 29ad114a76f2d8d01aa31fa0dc1b211c "xelatex"
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.out" 1681756905 454 50b668959753031c85a7636eb970de0c "xelatex"
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.tex" 1681756904 3239 1c19d3382eb02eca12f28f266179f0f0 ""
|
||||
"example_halfway.jpg" 1681756460 43787 f0809821c60091352d61146053f795f2 ""
|
||||
"example_summary.jpg" 1681756506 17901 286cc778e6a6b564c8027d51b98a968f ""
|
||||
(generated)
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.aux"
|
||||
"EARIN_LAB_3_RUDNICKI_KLISZKO.log"
|
||||
|
||||
@ -498,6 +498,9 @@ OUTPUT EARIN_LAB_3_RUDNICKI_KLISZKO.xdv
|
||||
INPUT ./example_halfway.jpg
|
||||
INPUT ./example_halfway.jpg
|
||||
INPUT example_halfway.jpg
|
||||
INPUT ./example_summary.jpg
|
||||
INPUT ./example_summary.jpg
|
||||
INPUT example_summary.jpg
|
||||
INPUT EARIN_LAB_3_RUDNICKI_KLISZKO.aux
|
||||
INPUT ./EARIN_LAB_3_RUDNICKI_KLISZKO.out
|
||||
INPUT ./EARIN_LAB_3_RUDNICKI_KLISZKO.out
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -56,13 +56,19 @@ To print help info about program user can issue help flag:
|
||||
\end{lstlisting}
|
||||
Results will be displayed on 2D scatter plot. There will be as many outputs as user wanted with incrementation of generation so that the final plot will be on final generation \\
|
||||
\begin{figure}[H]
|
||||
\caption{Exemplary plot halfway through generation with parameters \\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) }
|
||||
\caption{Exemplary plot halfway through generation with parameters \\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) noo 10 }
|
||||
\includegraphics[width=8cm]{example_halfway.jpg}
|
||||
\centering
|
||||
\end{figure}
|
||||
At the end summary of results on plot will display with red gradient showing results from the earliest (white) to latest (bright red) \\
|
||||
\begin{figure}[H]
|
||||
\caption{Exemplary summary plot with parameters\\ nop 250 sop 1000 ms 0.1 nog 500 min max (-5.12, 5.12) noo 10 }
|
||||
\includegraphics[width=8cm]{example_summary.jpg}
|
||||
\centering
|
||||
\end{figure}
|
||||
Results will be displayed and saved in the same folder as code directory for further inspection, with file name containing information about input parameters
|
||||
\section{Results}
|
||||
We have successfully implemented ES($\mu$, $\lambda$) to optimize Rastrigin function
|
||||
We have successfully implemented ES($\mu$, $\lambda$) to optimize Rastrigin function \\
|
||||
Rastrigin function is used to test optimization algorithms as it contains a lot of local minima
|
||||
|
||||
\end{document}
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user