done did it again
parent
839f023ee8
commit
bc1ffb957a
|
|
@ -1,14 +1,23 @@
|
||||||
|
"""
|
||||||
|
Schreibe einen genetischen Algorithmus, der die Parameter
|
||||||
|
(a,b,c,d) der Funktion f (x ) = ax 3 + bx 2 + cx + d so optimiert,
|
||||||
|
dass damit die Funktion g(x ) = e x im Bereich [-1..1] möglichst
|
||||||
|
gut angenähert wird. Nutze dazu den quadratischen Fehler (oder
|
||||||
|
alternativ die Fläche zwischen der e-Funktion und dem Polynom).
|
||||||
|
Zeichne die Lösung und vergleiche die Koeffizienten mit denen der
|
||||||
|
Taylor-Reihe um 0.
|
||||||
|
"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
import struct
|
import struct
|
||||||
|
import time
|
||||||
# import matplotlib.pyplot as plt
|
# import matplotlib.pyplot as plt
|
||||||
|
|
||||||
def generate_random_individuals():
|
def generate_random_population():
|
||||||
pop_grey = format(random.getrandbits(32), '32b')
|
pop_grey = [format(random.getrandbits(32), '32b') for i in range(10)]
|
||||||
pop_bin = grey_to_bin(pop_grey)
|
pop_bin = grey_to_bin(pop_grey)
|
||||||
a, b, c, d = pop_bin[0:7], pop_bin[8:15], pop_bin[16:23], pop_bin[24:31]
|
a, b, c, d = pop_bin[0:7], pop_bin[8:15], pop_bin[16:23], pop_bin[24:31]
|
||||||
# val = int(b, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
|
||||||
|
|
||||||
|
|
||||||
return [a, b, c, d]
|
return [a, b, c, d]
|
||||||
|
|
||||||
|
|
@ -29,17 +38,17 @@ def bin_to_grey(binary):
|
||||||
|
|
||||||
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
||||||
"""Convert binary string to float parameter in range [q_min, q_max]"""
|
"""Convert binary string to float parameter in range [q_min, q_max]"""
|
||||||
# Convert binary string to integer
|
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||||
val = int(binary, 2)
|
|
||||||
# Scale to range [q_min, q_max]
|
# Scale to range [q_min, q_max]
|
||||||
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
def quadratic_error(original_fn, approx_fn, n):
|
def quadratic_error(original_fn, approx_fn, n):
|
||||||
error = 0.0
|
error = 0.0
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(-(n // 2), (n // 2) + 1):
|
||||||
error += (original_fn(i) - approx_fn(i))**2
|
error += (original_fn(i) - approx_fn(i))**2
|
||||||
|
|
||||||
return error
|
return error
|
||||||
|
|
@ -48,18 +57,20 @@ def e_fn_approx(a, b, c, d, x = 1):
|
||||||
return a*x**3 + b*x**2 + c*x + d
|
return a*x**3 + b*x**2 + c*x + d
|
||||||
|
|
||||||
def fuck_that_shit_up():
|
def fuck_that_shit_up():
|
||||||
bin_values = generate_random_individuals()
|
bin_values = generate_random_population()
|
||||||
# Convert all binary strings to parameters in range 0.0-10.0
|
# Convert binary string to parameters for bin_values
|
||||||
float_values = [bin_to_param(bin) for bin in bin_values]
|
a, b, c, d = [bin_to_param(bin) for bin in bin_values]
|
||||||
a, b, c, d = float_values
|
|
||||||
|
|
||||||
e_func = lambda x: np.e**x
|
e_func = lambda x: np.e**x
|
||||||
fixed_approx = lambda x: e_fn_approx(a, b, c, d, x)
|
fixed_approx = lambda x: e_fn_approx(a, b, c, d, x)
|
||||||
|
fitness = quadratic_error(e_func, fixed_approx, 6)
|
||||||
|
|
||||||
while quadratic_error(e_func, fixed_approx, 6) > 0.01:
|
while fitness > 0.01:
|
||||||
|
# calc fitness
|
||||||
|
fitness = quadratic_error(e_func, fixed_approx, 6)
|
||||||
|
print(fitness)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
pass
|
|
||||||
# berechne fitness
|
|
||||||
# selection
|
# selection
|
||||||
# crossover
|
# crossover
|
||||||
# mutation
|
# mutation
|
||||||
|
|
@ -67,6 +78,6 @@ def fuck_that_shit_up():
|
||||||
# neue population
|
# neue population
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
b = format(random.getrandbits(32), '32b')
|
fuck_that_shit_up()
|
||||||
print(b)
|
# b = format(random.getrandbits(32), '32b')
|
||||||
# print(quadratic_error(e_func, fixed_approx, 6)) # hopefully works
|
# print(quadratic_error(e_func, fixed_approx, 6)) # hopefully works
|
||||||
Loading…
Reference in New Issue