grey to bin and bin to grey progress
parent
79800b9907
commit
4a8b78bc80
|
|
@ -1,23 +1,61 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
import scipy.integrate
|
import struct
|
||||||
|
|
||||||
# import matplotlib.pyplot as plt
|
# import matplotlib.pyplot as plt
|
||||||
|
|
||||||
def generate_random_individuals():
|
def generate_random_individuals():
|
||||||
a = random.randrange(10)
|
g = format(random.getrandbits(32), '32b')
|
||||||
b = random.randrange(10)
|
# val = int(b, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||||
c = random.randrange(10)
|
|
||||||
d = random.randrange(10)
|
|
||||||
|
|
||||||
return [a, b, c, d]
|
|
||||||
|
|
||||||
def integrate_individual(function):
|
return val
|
||||||
result = scipy.integrate.quad(function, -3, 3)
|
|
||||||
return result
|
# 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):
|
def quadratic_error(original_fn, approx_fn, n):
|
||||||
error = 0
|
error = 0.0
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
error += (original_fn(i) - approx_fn(i))**2
|
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):
|
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():
|
||||||
e_func = lambda x: np.e**x
|
e_func = lambda x: np.e**x
|
||||||
fixed_approx = 1 # TODO
|
fixed_approx = lambda x: e_fn_approx(1.0, 0.1, 0.2, 1.0, x)
|
||||||
|
|
||||||
while quadratic_error(e_func, fixed_approx, n) > 0.01:
|
while quadratic_error(e_func, fixed_approx, 6) > 0.01:
|
||||||
|
pass
|
||||||
# berechne fitness
|
# berechne fitness
|
||||||
# selection
|
# selection
|
||||||
# crossover
|
# crossover
|
||||||
# mutation
|
# mutation
|
||||||
|
|
||||||
# neue population
|
# 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
|
||||||
Loading…
Reference in New Issue