mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 17:43:12 +02:00
chore: initialize lab3 folder, code and report
This commit is contained in:
parent
a76b04b8f9
commit
eff54a6b61
14
lab3/EARIN_LAB_3_RUDNICKI_KLISZKO.tex
Normal file
14
lab3/EARIN_LAB_3_RUDNICKI_KLISZKO.tex
Normal file
@ -0,0 +1,14 @@
|
||||
\documentclass{article}
|
||||
\usepackage{graphicx} % Required for inserting images
|
||||
|
||||
\title{EARIN_LAB_3_RUDNICKI_KLISZKO.tex}
|
||||
\author{321krzychu }
|
||||
\date{April 2023}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\maketitle
|
||||
|
||||
\section{Introduction}
|
||||
|
||||
\end{document}
|
||||
BIN
lab3/EARIN_Lab_3.pdf
Normal file
BIN
lab3/EARIN_Lab_3.pdf
Normal file
Binary file not shown.
57
lab3/main.py
Normal file
57
lab3/main.py
Normal file
@ -0,0 +1,57 @@
|
||||
"""
|
||||
Program that optimizes Rastrigin function: f (x, y) =
|
||||
20 + (x^2 - 10cos(2πx)) + (y^2 - 10 cos(2πy)).
|
||||
Using Evolutionary Strategy (μ, λ).
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
|
||||
def rastrigin(x_argument, y_argument):
|
||||
""" Define the Rastrigin function """
|
||||
return 20 + x_argument**2 - 10 * np.cos(2 * np.pi * x_argument) + \
|
||||
y_argument**2 - 10 * np.cos(2 * np.pi * y_argument)
|
||||
|
||||
|
||||
def evolution_strategy(top_individuals, lambda_, sigma, iterations):
|
||||
""" Define the Evolutionary Strategy (μ, λ) algorithm """
|
||||
# Initialize the population
|
||||
population = np.random.uniform(low=-5.12, high=5.12, size=(lambda_, 2))
|
||||
|
||||
# Iterate for a fixed number of iterations
|
||||
for i in range(iterations):
|
||||
# Evaluate the fitness of each individual
|
||||
fitness = np.array([rastrigin(x, y) for x, y in population])
|
||||
|
||||
# Select the top top_individuals individuals
|
||||
parents = population[np.argsort(fitness)[:top_individuals]]
|
||||
|
||||
# Generate the next generation of lambda individuals by recombination
|
||||
children = np.concatenate(
|
||||
[np.random.permutation(parents) for i in range(lambda_)])
|
||||
|
||||
# Add mutation to the children
|
||||
mutation = np.random.normal(loc=0, scale=sigma, size=(lambda_, 2))
|
||||
population = children + mutation
|
||||
|
||||
# Evaluate the fitness of the final population
|
||||
fitness = np.array([rastrigin(x, y) for x, y in population])
|
||||
|
||||
# Return the best individual found
|
||||
best_idx = np.argmin(fitness)
|
||||
return population[best_idx], fitness[best_idx]
|
||||
|
||||
|
||||
# Ran first in the code
|
||||
if __name__ == "__main__":
|
||||
# Set the parameters
|
||||
MU = 5
|
||||
LAMBDA = 20
|
||||
SIGMA = 0.1
|
||||
ITERATIONS = 100
|
||||
|
||||
# Run the Evolutionary Strategy algorithm
|
||||
best_individual, best_fitness = evolution_strategy(
|
||||
MU, LAMBDA, SIGMA, ITERATIONS)
|
||||
|
||||
print("Best individual found:", best_individual)
|
||||
print("Best fitness found:", best_fitness)
|
||||
Loading…
Reference in New Issue
Block a user