parameters: ()
docstring: Example using Gaussian Process optimization for noisy data and multiple mins
docstring_summary: Example using Gaussian Process optimization for noisy data and multiple mins
docstring_tokens: Example using Gaussian Process optimization for noisy data and multiple mins
function: def gp_opt_example():
      """
      Example using Gaussian Process optimization for noisy data and multiple mins
  
      """
      plt.close('all')
  
      ## Required to define is hypercube  ##
  
  
      d = 3                                       # Dimension of problem
      opt_fun = 5                                 # Define type of optimization function
  
      d = 2
      opt_fun = 4
  
      ## Parameters Used for Optimization Function Defining Range of Errors in Energy##
      sigma_a = 1e-4;
      sigma_b = 5e-2;
  
  
      ## Set Hypercube for each type of optimization function and check for proper dim ##
      GP_info = GParmFlags()
  
      if opt_fun == 1:     
          ## Do LJ Potential ##
          GP_info.hyper_cube = 2*np.ones([d,2])       # Upper and lower bound for each dimension
          GP_info.hyper_cube[:,0] = .2*GP_info.hyper_cube[:,0]
          if d>1:
              GP_info.hyper_cube[1:d,0] = 0
              GP_info.n_its = 5
  
              if d == 3:
                  print 'True min: f(x) = -3, x = [1 .5 sqrt(3)/2]'
          else:
              print 'True min: f(x) = -1, x = 1'
  
      elif opt_fun == 2: 
          d = 2
          GP_info.hyper_cube = np.ones([d,2])       # Upper and lower bound for each dimension
          GP_info.hyper_cube[0,0] = 2
          GP_info.hyper_cube[1,0] = -.5
          GP_info.hyper_cube[0,1] = 4
          GP_info.hyper_cube[1,1] = 1
  
          print 'Only defined for d=2'
          print '2D function, global minimum at (3,.5)'   
  
      elif opt_fun == 3: 
  
          if d>7:
              d = 2
          elif d<3:
              d = 3
  
  
          GP_info.Nc0 = 2*(d+2)*(d+1)    
          GP_info.hyper_cube = 1.5*np.ones([d,2])       # Upper and lower bound for each dimension
          GP_info.hyper_cube[:,0] = 0.5     
  
          print 'Only defined for d=3-7'
          print 'Global minimum at (1,1,...,1)'
  
      elif opt_fun == 4: 
          d = 2
          GP_info.hyper_cube = np.ones([d,2])       # Upper and lower bound for each dimension
          GP_info.hyper_cube[0,0] = -1
          GP_info.hyper_cube[1,0] = -2
          GP_info.hyper_cube[0,1] = 1
          GP_info.hyper_cube[1,1] = 0
  
          print 'Only defined for d=2'
          print '2D function, global minimum at (0,-1)'  
  
      else:
          GP_info.hyper_cube = np.ones([d,2])/2       # Upper and lower bound for each dimension
          GP_info.hyper_cube[:,0] = -GP_info.hyper_cube[:,0]
  
          GP_info.n_its = 4
  
          print 'True min: f(x) = -1, x = 0'
  
      ## Setup Gausian Process Structure ##
      try:
          GP_info.hyper_cube
          E,dE,mol_pos,best_pos,best_E,GP_info = gp_opt_setup(GP_info)
      except:
          print 'Please Define GP_info.hyper_cube'
          exit
  
      ## Do Gausian Process Iteration ##
  
      for jits in range(GP_info.n_its):
          GP_info.jits = jits
          new_points,best_pos,best_E,best_dE,GP_info,neig_data = gp_opt(E,dE,mol_pos,GP_info) 
          if jits < GP_info.n_its-1:
              ## Simulate random uncertainty in optimization function
              new_dE = (sigma_b-sigma_a)*np.random.rand(GP_info.Nc0,1)+sigma_a
              new_E = opt_surface_f(new_points,opt_fun)+new_dE*np.random.randn(GP_info.Nc0, 1)
              if jits == 0:
                  mol_pos = np.copy(new_points)
                  E = np.copy(new_E)
                  dE = np.copy(new_dE)
              else:
                  mol_pos = np.append(mol_pos,new_points,axis=0)
                  E = np.append(E,new_E,axis=0)
                  dE = np.append(dE,new_dE,axis=0)
  
  
      print 'Best Guess in position: '
      print best_pos
      print 'Best Guess in energy: '
      print best_E
function_tokens: def gp_opt_example ( ) : plt . close ( 'all' ) ## Required to define is hypercube  ## d = 3 # Dimension of problem opt_fun = 5 # Define type of optimization function d = 2 opt_fun = 4 ## Parameters Used for Optimization Function Defining Range of Errors in Energy## sigma_a = 1e-4 sigma_b = 5e-2 ## Set Hypercube for each type of optimization function and check for proper dim ## GP_info = GParmFlags ( ) if opt_fun == 1 : ## Do LJ Potential ## GP_info . hyper_cube = 2 * np . ones ( [ d , 2 ] ) # Upper and lower bound for each dimension GP_info . hyper_cube [ : , 0 ] = .2 * GP_info . hyper_cube [ : , 0 ] if d > 1 : GP_info . hyper_cube [ 1 : d , 0 ] = 0 GP_info . n_its = 5 if d == 3 : print 'True min: f(x) = -3, x = [1 .5 sqrt(3)/2]' else : print 'True min: f(x) = -1, x = 1' elif opt_fun == 2 : d = 2 GP_info . hyper_cube = np . ones ( [ d , 2 ] ) # Upper and lower bound for each dimension GP_info . hyper_cube [ 0 , 0 ] = 2 GP_info . hyper_cube [ 1 , 0 ] = - .5 GP_info . hyper_cube [ 0 , 1 ] = 4 GP_info . hyper_cube [ 1 , 1 ] = 1 print 'Only defined for d=2' print '2D function, global minimum at (3,.5)' elif opt_fun == 3 : if d > 7 : d = 2 elif d < 3 : d = 3 GP_info . Nc0 = 2 * ( d + 2 ) * ( d + 1 ) GP_info . hyper_cube = 1.5 * np . ones ( [ d , 2 ] ) # Upper and lower bound for each dimension GP_info . hyper_cube [ : , 0 ] = 0.5 print 'Only defined for d=3-7' print 'Global minimum at (1,1,...,1)' elif opt_fun == 4 : d = 2 GP_info . hyper_cube = np . ones ( [ d , 2 ] ) # Upper and lower bound for each dimension GP_info . hyper_cube [ 0 , 0 ] = - 1 GP_info . hyper_cube [ 1 , 0 ] = - 2 GP_info . hyper_cube [ 0 , 1 ] = 1 GP_info . hyper_cube [ 1 , 1 ] = 0 print 'Only defined for d=2' print '2D function, global minimum at (0,-1)' else : GP_info . hyper_cube = np . ones ( [ d , 2 ] ) / 2 # Upper and lower bound for each dimension GP_info . hyper_cube [ : , 0 ] = - GP_info . hyper_cube [ : , 0 ] GP_info . n_its = 4 print 'True min: f(x) = -1, x = 0' ## Setup Gausian Process Structure ## try : GP_info . hyper_cube E , dE , mol_pos , best_pos , best_E , GP_info = gp_opt_setup ( GP_info ) except : print 'Please Define GP_info.hyper_cube' exit ## Do Gausian Process Iteration ## for jits in range ( GP_info . n_its ) : GP_info . jits = jits new_points , best_pos , best_E , best_dE , GP_info , neig_data = gp_opt ( E , dE , mol_pos , GP_info ) if jits < GP_info . n_its - 1 : ## Simulate random uncertainty in optimization function new_dE = ( sigma_b - sigma_a ) * np . random . rand ( GP_info . Nc0 , 1 ) + sigma_a new_E = opt_surface_f ( new_points , opt_fun ) + new_dE * np . random . randn ( GP_info . Nc0 , 1 ) if jits == 0 : mol_pos = np . copy ( new_points ) E = np . copy ( new_E ) dE = np . copy ( new_dE ) else : mol_pos = np . append ( mol_pos , new_points , axis = 0 ) E = np . append ( E , new_E , axis = 0 ) dE = np . append ( dE , new_dE , axis = 0 ) print 'Best Guess in position: ' print best_pos print 'Best Guess in energy: ' print best_E