Advertisement
evgeniya_polyntseva

nelder-mead

May 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. class Vector(object):
  4.     def __init__(self, x, y):
  5.         """ создаём вектор """
  6.         self.x = x
  7.         self.y = y
  8.  
  9.     def __repr__(self):
  10.         return "({0}, {1})".format(self.x, self.y)
  11.  
  12.     def __add__(self, other):
  13.         x = self.x + other.x
  14.         y = self.y + other.y
  15.         return Vector(x, y)
  16.  
  17.     def __sub__(self, other):
  18.         x = self.x - other.x
  19.         y = self.y - other.y
  20.         return Vector(x, y)
  21.  
  22.     def __rmul__(self, other):
  23.         x = self.x * other
  24.         y = self.y * other
  25.         return Vector(x, y)
  26.  
  27.     def __truediv__(self, other):
  28.         x = self.x / other
  29.         y = self.y / other
  30.         return Vector(x, y)
  31.  
  32.     def c(self):
  33.         return (self.x, self.y)
  34.  
  35.  
  36. def f(z):
  37.     x, y = z
  38.     # функция по варианту
  39.     # return  -(2 / (1 + ((x - 1) / 2) ** 2 + (y - 1) ** 2)) - (3 / (1 + ((x - 2) / 3) ** 2 + ((y - 3) / 2) ** 2))
  40.     # функция из методы min(1,1)
  41.     # return 100*(y - x) ** 2 + (1 - x) ** 2
  42.     # функция Розенброка min(1,1)
  43.     return 100 * (y - x ** 2) ** 2 + (1 - x) ** 2
  44.  
  45.  
  46. def nelder_mead(alpha=1, beta=0.5, gamma=2, i=10):
  47.     v1 = Vector(0, 1)
  48.     v2 = Vector(1, 0)
  49.     v3 = Vector(1, 1)
  50.  
  51.     for i in range(i):
  52.         adict = {v1: f(v1.c()), v2: f(v2.c()), v3: f(v3.c())}
  53.         points = sorted(adict.items(), key=lambda p: p[1])
  54.         b = points[0][0]
  55.         g = points[1][0]
  56.         w = points[2][0]
  57.         mid = (g + b) / 2
  58.  
  59.         # отражение
  60.         xr = mid + alpha * (mid - w)
  61.         if f(xr.c()) < f(g.c()):
  62.             w = xr
  63.         else:
  64.             if f(xr.c()) < f(w.c()):
  65.                 w = xr
  66.             c = (w + mid) / 2
  67.             if f(c.c()) < f(w.c()):
  68.                 w = c
  69.         if f(xr.c()) < f(b.c()):
  70.  
  71.             # растяжение
  72.             xe = mid + gamma * (xr - mid)
  73.             if f(xe.c()) < f(xr.c()):
  74.                 w = xe
  75.             else:
  76.                 w = xr
  77.         if f(xr.c()) > f(g.c()):
  78.  
  79.             # сжатие
  80.             xc = mid + beta * (w - mid)
  81.             if f(xc.c()) < f(w.c()):
  82.                 w = xc
  83.  
  84.         # переобозначим точки: b = best, g = good, w = worst — соответственно.
  85.         v1 = w
  86.         v2 = g
  87.         v3 = b
  88.     return b
  89.  
  90.  
  91. res = nelder_mead()
  92. print("Лучшее решение: %s" % (res))
  93. print("Значение функции в точке экстремума: %s" % f(res.c()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement