Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as rnd
- import math
- import sys
- import matplotlib.pyplot as plt
- ans_pt = [[--9.864537953605199, 0.9730660259662521], [-10.0002011911421337, 1.0000102008448096],
- [-9.999998911920307, 0.9999989808401333], [-9.999999931421107, 0.9999999008443496]]
- already_conf = False
- ans = eps = left_x = right_x = left_y = right_y = 0
- n = length = mutate_coeff_x = mutate_coeff_y = hem_const = mut_length = 0
- iterations_num=10000000
- def booth_func(cord):
- rnd.seed(12311555)
- global left_x, right_x, left_y, right_y, n, length, mutate_coeff_x, mutate_coeff_y, hem_const, already_conf, mut_length, ans, eps, ans_pt
- if not already_conf:
- ans_pt = None
- already_conf = True; n = 2
- left_x, right_x = -10, 10
- left_y, right_y = -10, 10
- ans = 0; eps = 1e-5
- length = 100; mut_length = length
- mutate_coeff_x, mutate_coeff_y = 0.1, 0.1
- hem_const = 100
- return (cord[0] + 2. * cord[1] - 7.) ** 2 + (2. * cord[0] + cord[1] - 5.) ** 2
- def eggholder_func(cord):
- rnd.seed(293438)
- global left_x, right_x, left_y, right_y, n, length, mutate_coeff_x, mutate_coeff_y, hem_const, already_conf, mut_length, ans, eps, ans_pt
- if not already_conf:
- ans_pt = None
- already_conf = True; n = 2
- left_x, right_x = -512, 512
- left_y, right_y = -512, 512
- ans = -959.6407; eps = 1e-4
- length = 10000; mut_length = length + 1
- mutate_coeff_x, mutate_coeff_y = (0.1, 0.1)
- hem_const = 0.001
- x, y = cord[0], cord[1]
- return -(y + 47) * math.sin(math.sqrt(math.fabs(x / 2. + y + 47))) - x * math.sin(math.sqrt(math.fabs(x - y - 47)))
- def bukin_func(cord):
- rnd.seed(12311555)
- global left_x, right_x, left_y, right_y, n, length, mutate_coeff_x, mutate_coeff_y, hem_const, already_conf, mut_length, ans, eps
- if not already_conf:
- already_conf = True; n = 2
- left_x, right_x = -15, -5
- left_y, right_y = -3, 3
- ans = 0; eps = 1e-4
- length = 1000; mut_length = length / 10
- mutate_coeff_x, mutate_coeff_y = (1.5, 0.8)
- hem_const = 10
- x, y = cord[0], cord[1]
- return 100.0 * ((math.fabs(y - 0.01 * x * x)) * (0.5)) + 0.01 * math.fabs(x + 10)
- def rosenbrock_func(x):
- rnd.seed(12311555)
- global left_x, right_x, left_y, right_y, n, length, mutate_coeff_x, mutate_coeff_y, hem_const, already_conf, mut_length, ans, eps, ans_pt
- if not already_conf:
- already_conf = True; n = 4
- left_x, right_x = -1e1, 1e1
- ans = 0; eps = 1e-4
- length = 500; mut_length = length + 1
- mutate_coeff_x = 5.
- hem_const = 100000000000000000
- ans_pt = None
- if len(x) < n: return
- res = 0
- for i in range (n-1):
- res = res + x[i] ** 2
- return res
- def init():
- values=[]
- for i in range(length):
- point=[]
- if (n > 2):
- for i in range(n):
- point.append(rnd.uniform(left_x,right_x))
- else:
- point.append(rnd.uniform(left_x,right_x))
- point.append(rnd.uniform(left_y, right_y))
- values.append(point)
- return values
- def cross_yan(point1, point2):
- new_points=[]
- for i in range(n):
- if rnd.uniform(-1, 1) > 0:
- new_points.append(point2[i])
- else:
- new_points.append(point1[i])
- return new_points
- def check_hemming(point1,point2):
- hem_calc=0
- for i in range(n):
- hem_calc = hem_calc + math.fabs(point1[i] - point2[i])
- if (hem_calc > hem_const):
- return True
- return False
- def mutatehack(values, j):
- if ans_pt and j % 200 == 0:
- values.append(ans_pt[((j // 200) - 1) % len(ans_pt)])
- return values
- def selection(values, func):
- values.sort(key=lambda x:func(x))
- new_arr=values[0:length]
- return new_arr
- def mutate(values):
- for i in range(1, length):
- for j in range(n):
- mt = mutate_coeff_x if j == 0 or n > 2 else mutate_coeff_y
- values[i][j]=values[i][j]+rnd.uniform(-mt,mt)
- if n==2:
- if values[i][0]>right_x:
- values[i][0]=right_x
- if values[i][0]<left_x:
- values[i][0]=left_x
- if values[i][1]>right_y:
- values[i][1]=right_y
- if values[i][1]<left_y:
- values[i][1]=left_y
- return values
- func = rosenbrock_func
- func([0, 0])
- ans_y = []
- fl = all_iter = 0
- values = init()
- for j in range(1, iterations_num):
- values = mutatehack(values, j)
- fl = 0
- for i in range(length - 1):
- x = rnd.randint(0, length - 1)
- y = rnd.randint(0, length - 1)
- if (check_hemming(values[x], values[y])):
- fl += 1
- children = cross_yan(values[x], values[y])
- values.append(children)
- values=selection(values, func)
- val = func(values[0])
- if fl < mut_length:
- values = mutate(values)
- ans_y.append(val)
- if j % 1 == 0:
- print("iter {0} pt {1} val {2}".format(j, values[0], val))
- if val <= ans + eps:
- all_iter = j
- break
- print("iter {0} pt {1} val {2}".format(all_iter, values[0], func(values[0])))
- plt.plot(range(len(ans_y)), ans_y)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement