grey to bin and bin to grey progress

master
Ruben-FreddyLoafers 2025-10-13 13:27:05 +02:00
parent 79800b9907
commit 4a8b78bc80
1 changed files with 60 additions and 16 deletions

View File

@ -1,23 +1,61 @@
import numpy as np
import random
import scipy.integrate
import struct
# import matplotlib.pyplot as plt
def generate_random_individuals():
a = random.randrange(10)
b = random.randrange(10)
c = random.randrange(10)
d = random.randrange(10)
g = format(random.getrandbits(32), '32b')
# val = int(b, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
return [a, b, c, d]
def integrate_individual(function):
result = scipy.integrate.quad(function, -3, 3)
return result
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
error = 0.0
for i in range(n):
error += (original_fn(i) - approx_fn(i))**2
@ -27,14 +65,20 @@ def quadratic_error(original_fn, approx_fn, n):
def e_fn_approx(a, b, c, d, x = 1):
return a*x**3 + b*x**2 + c*x + d
e_func = lambda x: np.e**x
fixed_approx = 1 # TODO
while quadratic_error(e_func, fixed_approx, n) > 0.01:
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
print("Hello World")
return 0
b = format(random.getrandbits(32), '32b')
print(b)
# print(quadratic_error(e_func, fixed_approx, 6)) # hopefully works