title: optimization in a class in python using Scipy minimize
body: <p>in the following code, I want to optimize the defined class suing Scipy but I got this error:</p>
  <pre><code>RecursionError: maximum recursion depth exceeded
  </code></pre>
  <p>the fact is that if I reduce wd and ws I can get the results while for higher numbers it is not possible. Indeed I need to work with larger numbers so this problem must be solved. regarding the code, I want to optimize the class for different wd and ws by using for loop; in a better way for each value of [l][k], the class output will be maximized (which is optimization function in class). As I found the problem is related to recursion but I cannot understand where it happened?</p>
  <pre><code>import numpy as np
  import pandas as pd
  from py_wake.examples.data.hornsrev1 import V80 
  from py_wake.examples.data.hornsrev1 import Hornsrev1Site # We work with the Horns Rev 1 site, which comes already set up with PyWake.
  from py_wake import BastankhahGaussian
  from py_wake.turbulence_models import GCLTurbulence
  from py_wake.deflection_models.jimenez import JimenezWakeDeflection
  from scipy.optimize import minimize
  from py_wake.wind_turbines.power_ct_functions import PowerCtFunctionList, PowerCtTabular
  
  #-----------------------------------------
  
  class optimizating():
      
      def __init__(self,wt,wd,ws,site,windturbine,wfModel,deflectionModel,turbulencemodel):
          
          self.wt=wt
          self.wd=wd
          self.ws=ws
          self.site=site
          self.windTurbines=windturbine
          self.wfModel=wfModel
          self.deflectionModel=deflectionModel
          self.turbulenceModel=turbulencemodel
         
          
  #--------------------------------------------------  
       
      def newSite(self):
          
          x, y = self.site.initial_position.T
          xNew=np.array([x[0]+560*i for i in range(3)])
          yNew=np.array([y[0]+560*i for i in range(3)])
          x_newsite=np.array([xNew[0],xNew[0],xNew[0],xNew[1],xNew[1],xNew[1],xNew[2],xNew[2],xNew[2]])
          y_newsite=np.array([yNew[0],yNew[1],yNew[2],yNew[0],yNew[1],yNew[2],yNew[0],yNew[1],yNew[2]])
          return(x_newsite[:(self.wt)],y_newsite[:(self.wt)])
  #-----------------------------------------------------
  
      def objective(self,x0,l,k) :
  
          x_newsite, y_newsite=self.newSite()
                
          self.windTurbines.powerCtFunction = PowerCtFunctionList(
          key='operating',
          powerCtFunction_lst=[PowerCtTabular(ws=[0, 100], power=[0, 0], power_unit='w', ct=[0, 0]), # 0=No power and ct
          self.windTurbines.powerCtFunction],default_value=1)   # 1=Normal operation
        
          power=0
      
          operating = np.ones((self.wt)) 
          operating[x0 &lt;= 0.5]=0
              
          wf_model = self.wfModel(self.site, self.windTurbines, deflectionModel= self.deflectionModel,turbulenceModel= self.turbulenceModel)
          
              # run wind farm simulation
          sim_res = wf_model(
                  x_newsite, y_newsite, # wind turbine positions
                  h=None, # wind turbine heights (defaults to the heights defined in windTurbines)
                  wd=l, # Wind direction (defaults to site.default_wd (0,1,...,360 if not overriden))
                  ws=k, # Wind speed (defaults to site.default_ws (3,4,...,25m/s if not overriden))
                  operating=operating)
              
          
      
          power=np.sum(sim_res.Power)
          
          print(power)
          return (float(-power))
      
  #----------------------------------------------------------------
      
  def optimization(self):
      
      C_result=np.zeros((len(wd),len(ws),wt))
      power=np.zeros((len(wd),len(ws),1))
      status=np.zeros((len(wd),len(ws),1))
      for l in range(len(wd)):
          for k in range(len(ws)):
              x0 = np.full((wt),0.5)
              bounds=np.full((wt,2),(0,1)).reshape(-1, 2)  
              res= minimize(self.objective, x0=x0, bounds=bounds,args=(wd[l],ws[k]))
              C_result[l][k]=res.x
              power[l][k]=-res.fun
              status[l][k]=res.success                
      return(status,C_result,power)
      
  # =============================================================================
  # Run the Optimization
  # =============================================================================
  
  wt =9
  wd=np.arange(5,16,5)
  ws=np.arange(4,12)  
  site=Hornsrev1Site()
  windTurbines=V80()
  wf_model=BastankhahGaussian
  deflection_model=JimenezWakeDeflection()
  turbulence_model=GCLTurbulence()
  C_result=np.zeros((len(wd),len(ws),wt))
  power=np.zeros((len(wd),len(ws),1))
  status=np.zeros((len(wd),len(ws),1))
  
  
  
  opt=optimizating(wt,wd,ws,site,windTurbines,wf_model,deflection_model,turbulence_model)
  res=opt.optimization() 
  </code></pre>
  <p>I think the problem should be in the last part where I want to introduce a sample(opt) but I have tried different way to solve it but I could not.</p>