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 # Function to flip a bit # represented as character. def flip(c): return '1' if c == '0' else '0' # accepts string binary array def grey_to_bin(gray): binary = "" # MSB of binary code is same as gray code binary += gray[0] # Compute remaining bits for i in range(1, len(gray)): # If current bit is 0, concatenate # previous bit if gray[i] == '0': binary += binary[i - 1] # Else, concatenate invert of # previous bit else: binary += flip(binary[i - 1]) return binary # accepts string binary array def bin_to_grey(binary): gray = "" # MSB of gray code is same as binary code gray += binary[0] # Compute remaining bits, next bit is computed by # doing XOR of previous and current in Binary for i in range(1, len(binary)): # Concatenate XOR of previous bit # with current bit gray += xorChar(binary[i - 1], binary[i]) return gray 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