creating params
parent
e67b8fd702
commit
839f023ee8
|
|
@ -4,11 +4,13 @@ import struct
|
|||
# import matplotlib.pyplot as plt
|
||||
|
||||
def generate_random_individuals():
|
||||
g = format(random.getrandbits(32), '32b')
|
||||
pop_grey = format(random.getrandbits(32), '32b')
|
||||
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]
|
||||
# val = int(b, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||
|
||||
|
||||
return val
|
||||
return [a, b, c, d]
|
||||
|
||||
def grey_to_bin(gray):
|
||||
"""Convert Gray code to binary, operating on the integer value directly"""
|
||||
|
|
@ -25,6 +27,15 @@ def bin_to_grey(binary):
|
|||
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
||||
return format(gray, f'0{len(binary)}b') # Convert back to binary string with same length
|
||||
|
||||
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 integer
|
||||
val = int(binary, 2)
|
||||
# Scale to range [q_min, q_max]
|
||||
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
||||
return q
|
||||
|
||||
|
||||
def quadratic_error(original_fn, approx_fn, n):
|
||||
error = 0.0
|
||||
|
||||
|
|
@ -37,15 +48,21 @@ def e_fn_approx(a, b, c, d, x = 1):
|
|||
return a*x**3 + b*x**2 + c*x + d
|
||||
|
||||
def fuck_that_shit_up():
|
||||
bin_values = generate_random_individuals()
|
||||
# Convert all binary strings to parameters in range 0.0-10.0
|
||||
float_values = [bin_to_param(bin) for bin in bin_values]
|
||||
a, b, c, d = float_values
|
||||
|
||||
e_func = lambda x: np.e**x
|
||||
fixed_approx = lambda x: e_fn_approx(1.0, 0.1, 0.2, 1.0, x)
|
||||
fixed_approx = lambda x: e_fn_approx(a, b, c, d, x)
|
||||
|
||||
while quadratic_error(e_func, fixed_approx, 6) > 0.01:
|
||||
|
||||
pass
|
||||
# berechne fitness
|
||||
# selection
|
||||
# crossover
|
||||
# mutation
|
||||
# berechne fitness
|
||||
# selection
|
||||
# crossover
|
||||
# mutation
|
||||
|
||||
# neue population
|
||||
return 0
|
||||
|
|
|
|||
Loading…
Reference in New Issue