MLE/03_euler_gen_alg.py

55 lines
1.5 KiB
Python

import numpy as np
import random
import struct
# import matplotlib.pyplot as plt
def generate_random_individuals():
g = format(random.getrandbits(32), '32b')
# val = int(b, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
return val
def grey_to_bin(gray):
"""Convert Gray code to binary, operating on the integer value directly"""
num = int(gray, 2) # Convert string to integer
mask = num
while mask != 0:
mask >>= 1
num ^= mask
return format(num, f'0{len(gray)}b') # Convert back to binary string with same length
def bin_to_grey(binary):
"""Convert binary to Gray code using XOR with right shift"""
num = int(binary, 2) # Convert string to integer
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 quadratic_error(original_fn, approx_fn, n):
error = 0.0
for i in range(n):
error += (original_fn(i) - approx_fn(i))**2
return error
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():
e_func = lambda x: np.e**x
fixed_approx = lambda x: e_fn_approx(1.0, 0.1, 0.2, 1.0, x)
while quadratic_error(e_func, fixed_approx, 6) > 0.01:
pass
# berechne fitness
# selection
# crossover
# mutation
# neue population
return 0
b = format(random.getrandbits(32), '32b')
print(b)
# print(quadratic_error(e_func, fixed_approx, 6)) # hopefully works