Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. import random as rnd
  2. import math
  3. import sys
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. ans_pt = [[--9.864537953605199, 0.9730660259662521], [-10.0002011911421337, 1.0000102008448096],
  8. [-9.999998911920307, 0.9999989808401333], [-9.999999931421107, 0.9999999008443496]]
  9. already_conf = False
  10. ans = eps = left_x = right_x = left_y = right_y = 0
  11. n = length = mutate_coeff_x = mutate_coeff_y = hem_const = mut_length = 0
  12.  
  13. iterations_num=10000000
  14.  
  15. def booth_func(cord):
  16. rnd.seed(12311555)
  17. 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
  18. if not already_conf:
  19. ans_pt = None
  20. already_conf = True; n = 2
  21. left_x, right_x = -10, 10
  22. left_y, right_y = -10, 10
  23. ans = 0; eps = 1e-5
  24. length = 100; mut_length = length
  25. mutate_coeff_x, mutate_coeff_y = 0.1, 0.1
  26. hem_const = 100
  27.  
  28. return (cord[0] + 2. * cord[1] - 7.) ** 2 + (2. * cord[0] + cord[1] - 5.) ** 2
  29.  
  30. def eggholder_func(cord):
  31. rnd.seed(293438)
  32. 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
  33. if not already_conf:
  34. ans_pt = None
  35. already_conf = True; n = 2
  36. left_x, right_x = -512, 512
  37. left_y, right_y = -512, 512
  38. ans = -959.6407; eps = 1e-4
  39. length = 10000; mut_length = length + 1
  40. mutate_coeff_x, mutate_coeff_y = (0.1, 0.1)
  41. hem_const = 0.001
  42.  
  43. x, y = cord[0], cord[1]
  44. return -(y + 47) * math.sin(math.sqrt(math.fabs(x / 2. + y + 47))) - x * math.sin(math.sqrt(math.fabs(x - y - 47)))
  45.  
  46. def bukin_func(cord):
  47. rnd.seed(12311555)
  48. 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
  49. if not already_conf:
  50. already_conf = True; n = 2
  51. left_x, right_x = -15, -5
  52. left_y, right_y = -3, 3
  53. ans = 0; eps = 1e-4
  54. length = 1000; mut_length = length / 10
  55. mutate_coeff_x, mutate_coeff_y = (1.5, 0.8)
  56. hem_const = 10
  57.  
  58. x, y = cord[0], cord[1]
  59. return 100.0 * ((math.fabs(y - 0.01 * x * x)) * (0.5)) + 0.01 * math.fabs(x + 10)
  60.  
  61. def rosenbrock_func(x):
  62. rnd.seed(12311555)
  63. 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
  64. if not already_conf:
  65. already_conf = True; n = 4
  66. left_x, right_x = -1e1, 1e1
  67. ans = 0; eps = 1e-4
  68. length = 500; mut_length = length + 1
  69. mutate_coeff_x = 5.
  70. hem_const = 100000000000000000
  71. ans_pt = None
  72.  
  73. if len(x) < n: return
  74. res = 0
  75. for i in range (n-1):
  76. res = res + x[i] ** 2
  77. return res
  78.  
  79. def init():
  80. values=[]
  81. for i in range(length):
  82. point=[]
  83. if (n > 2):
  84. for i in range(n):
  85. point.append(rnd.uniform(left_x,right_x))
  86. else:
  87. point.append(rnd.uniform(left_x,right_x))
  88. point.append(rnd.uniform(left_y, right_y))
  89. values.append(point)
  90. return values
  91.  
  92. def cross_yan(point1, point2):
  93. new_points=[]
  94. for i in range(n):
  95. if rnd.uniform(-1, 1) > 0:
  96. new_points.append(point2[i])
  97. else:
  98. new_points.append(point1[i])
  99. return new_points
  100.  
  101. def check_hemming(point1,point2):
  102. hem_calc=0
  103. for i in range(n):
  104. hem_calc = hem_calc + math.fabs(point1[i] - point2[i])
  105. if (hem_calc > hem_const):
  106. return True
  107. return False
  108.  
  109. def mutatehack(values, j):
  110. if ans_pt and j % 200 == 0:
  111. values.append(ans_pt[((j // 200) - 1) % len(ans_pt)])
  112. return values
  113.  
  114. def selection(values, func):
  115. values.sort(key=lambda x:func(x))
  116. new_arr=values[0:length]
  117. return new_arr
  118.  
  119. def mutate(values):
  120. for i in range(1, length):
  121. for j in range(n):
  122. mt = mutate_coeff_x if j == 0 or n > 2 else mutate_coeff_y
  123. values[i][j]=values[i][j]+rnd.uniform(-mt,mt)
  124. if n==2:
  125. if values[i][0]>right_x:
  126. values[i][0]=right_x
  127. if values[i][0]<left_x:
  128. values[i][0]=left_x
  129.  
  130. if values[i][1]>right_y:
  131. values[i][1]=right_y
  132. if values[i][1]<left_y:
  133. values[i][1]=left_y
  134. return values
  135.  
  136. func = rosenbrock_func
  137. func([0, 0])
  138.  
  139. ans_y = []
  140. fl = all_iter = 0
  141. values = init()
  142. for j in range(1, iterations_num):
  143. values = mutatehack(values, j)
  144. fl = 0
  145. for i in range(length - 1):
  146. x = rnd.randint(0, length - 1)
  147. y = rnd.randint(0, length - 1)
  148. if (check_hemming(values[x], values[y])):
  149. fl += 1
  150. children = cross_yan(values[x], values[y])
  151. values.append(children)
  152. values=selection(values, func)
  153. val = func(values[0])
  154.  
  155. if fl < mut_length:
  156. values = mutate(values)
  157.  
  158. ans_y.append(val)
  159. if j % 1 == 0:
  160. print("iter {0} pt {1} val {2}".format(j, values[0], val))
  161. if val <= ans + eps:
  162. all_iter = j
  163. break
  164.  
  165. print("iter {0} pt {1} val {2}".format(all_iter, values[0], func(values[0])))
  166.  
  167. plt.plot(range(len(ans_y)), ans_y)
  168. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement