Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import uniform
- L = [-0.5, 0.5]
- U = [1.5, 2.5]
- def f(x,y):
- return (1-x)**2 + 100*(y-x**2)**2
- def randomSearch(L, U, N=1000):
- Y, P = [], []
- for k in range(N): # uwaga, zdefiniować N
- R = [uniform(0,1) for k in range(2)]
- #X = [l+r*(u-l) for l,u,r in zip(L,U,R)]
- X = list(map(lambda l,r,u: l+r*(u-l), L,U,R))
- Y.append(f(X[0], X[1]))
- P.append(X)
- fMin = Y[0]
- fMinId = 0
- for k in range(len(Y)):
- if fMin > Y[k]:
- fMin = Y[k]
- fMinId = k
- pMin = P[fMinId]
- return fMin, pMin
- val = randomSearch(L,U,N=100)
- print("Wynik dla N = 100")
- print(val)
- val = randomSearch(L,U,N=1000)
- print("Wynik dla N = 1000")
- print(val)
- val = randomSearch(L,U,N=10000)
- print("Wynik dla N = 10000")
- print(val)
- val = randomSearch(L,U,N=100000)
- print("Wynik dla N = 100000")
- print(val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement