toweber

rosenbrock_optimization

Sep 12th, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import os
  2. import ipdb;
  3. import numpy as np
  4. from my_optimization import *
  5.  
  6. # ROSENBROCK TESTBENCH
  7. def tb_rosenbrock(x):
  8.     #traditional bounds on x_i values are between
  9.     #-5.12 and +5.12 inclusive
  10.     temp = 0
  11.     n = len(x)
  12.     f = 0
  13.     find = []
  14.     for i in range(0,n-1):
  15.         find.append(abs((1-x[i])**2 + 100*(x[i+1]-x[i]**2)**2))
  16.         f = f + find[i]
  17.  
  18.     f = abs(f)
  19.     find = np.abs(find)
  20.  
  21.     return f
  22.  
  23.  
  24.  
  25. def tb_simple(x):
  26.     f = np.sum(np.abs(x[1:]))+x[0]
  27.     return f
  28.  
  29.  
  30.  
  31. # OPTIMIZATION
  32.  
  33. size_x = 3
  34. bound_i = (-5.12,5.12)
  35. bounds = []
  36. for i in range (0, size_x):
  37.       bounds.append(bound_i)
  38.  
  39. x0 = 5*np.ones(size_x)
  40.  
  41. maxiter = 2000
  42. step = 0.1
  43. args = []
  44.  
  45. res = my_hillclimbing(tb_rosenbrock, x0, bounds, maxiter, step, args)
  46. print("Final x: [",)
  47. print("%f" % res.x[0],)
  48. for i in range(1,len(res.x)):
  49.     print(", %f" % res.x[i],)
  50. print("]\n",)
  51. print("Final cost: %f \n" % res.cost)
  52.  
Advertisement
Add Comment
Please, Sign In to add comment