Advertisement
cybereq

rosenbrock function

Dec 1st, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | Source Code | 0 0
  1. from random import uniform
  2. L = [-0.5, 0.5]
  3. U = [1.5, 2.5]
  4.  
  5.  
  6. def f(x,y):
  7.         return (1-x)**2 + 100*(y-x**2)**2
  8.  
  9. def randomSearch(L, U, N=1000):
  10.         Y, P = [], []
  11.         for k in range(N): # uwaga, zdefiniować N
  12.                 R = [uniform(0,1) for k in range(2)]
  13.                 #X = [l+r*(u-l) for l,u,r in zip(L,U,R)]
  14.                 X = list(map(lambda l,r,u: l+r*(u-l), L,U,R))
  15.                 Y.append(f(X[0], X[1]))
  16.                 P.append(X)
  17.         fMin = Y[0]
  18.         fMinId = 0
  19.         for k in range(len(Y)):
  20.                 if fMin > Y[k]:
  21.                         fMin = Y[k]
  22.                         fMinId = k
  23.         pMin = P[fMinId]
  24.  
  25.         return fMin, pMin
  26.  
  27. val = randomSearch(L,U,N=100)
  28. print("Wynik dla N = 100")
  29. print(val)
  30. val = randomSearch(L,U,N=1000)
  31. print("Wynik dla N = 1000")
  32. print(val)
  33. val = randomSearch(L,U,N=10000)
  34. print("Wynik dla N = 10000")
  35. print(val)
  36. val = randomSearch(L,U,N=100000)
  37. print("Wynik dla N = 100000")
  38. print(val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement