-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_function_optimization.py
More file actions
83 lines (68 loc) · 2.96 KB
/
Copy pathgenetic_function_optimization.py
File metadata and controls
83 lines (68 loc) · 2.96 KB
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
import numpy as np
import random
# Funkcja do optymalizacji: ax^3 + bx^2 + cx + d
def func(x, a, b, c, d):
return a * x**3 + b * x**2 + c * x + d
# Generowanie populacji chromosomów
def generate_population(size, lower_bound, upper_bound):
return [random.uniform(lower_bound, upper_bound) for _ in range(size)]
# Funkcja przystosowania (wartość funkcji dla danego x)
def fitness(x, a, b, c, d):
return func(x, a, b, c, d)
return x
# Selekcja metodą ruletki
def roulette_selection(population, fitness_values):
total_fitness = sum(fitness_values)
probabilities = [f / total_fitness for f in fitness_values]
return np.random.choice(population, p=probabilities)
# Krzyżowanie jednopunktowe
def crossover(parent1, parent2, crossover_rate):
if random.random() < crossover_rate:
alpha = random.uniform(0, 1)
child1 = alpha * parent1 + (1 - alpha) * parent2
child2 = alpha * parent2 + (1 - alpha) * parent1
return child1, child2
return parent1, parent2
# Mutacja losowa
def mutate(x, mutation_rate, lower_bound, upper_bound):
if random.random() < mutation_rate:
return random.uniform(lower_bound, upper_bound)
return x
# Algorytm genetyczny
def genetic_algorithm(a, b, c, d, lower_bound=0, upper_bound=31, population_size=10, generations=50, crossover_rate=0.8, mutation_rate=0.2, stagnation_limit=10):
population = generate_population(population_size, lower_bound, upper_bound)
best_x = None
best_fitness = float('-inf')
stagnation_count = 0
for generation in range(generations):
fitness_values = [fitness(x, a, b, c, d) for x in population]
new_population = []
for _ in range(population_size // 2):
parent1 = roulette_selection(population, fitness_values)
parent2 = roulette_selection(population, fitness_values)
child1, child2 = crossover(parent1, parent2, crossover_rate)
child1 = mutate(child1, mutation_rate, lower_bound, upper_bound)
child2 = mutate(child2, mutation_rate, lower_bound, upper_bound)
new_population.extend([child1, child2])
population = new_population
max_fitness = max(fitness_values)
max_x = population[np.argmax(fitness_values)]
if max_fitness > best_fitness:
best_fitness = max_fitness
best_x = max_x
stagnation_count = 0
else:
stagnation_count += 1
if stagnation_count >= stagnation_limit:
break
print("Najlepsza znaleziona wartosc funkcji:", best_fitness)
print("Liczba iteracji:", generation + 1)
return best_x
# Wprowadzanie współczynników przez użytkownika
a = float(input("Podaj wspolczynnik a: "))
b = float(input("Podaj wspolczynnik b: "))
c = float(input("Podaj wspolczynnik c: "))
d = float(input("Podaj wspolczynnik d: "))
# Uruchomienie algorytmu
best_x = genetic_algorithm(a, b, c, d)
print("Maksymalne x:", best_x)