0

I wanna optimize the following class using the Scipy and got this error: RecursionError: maximum recursion depth exceeded

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?

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 optimization():
    
    def __init__(self,wt,l,k,site,windturbine,wfModel,deflectionModel,turbulencemodel):
        
        self.wt=wt
        self.l=l
        self.k=k
        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) :

        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
    
        if (all(x0 <= 0.5)==True):
                power=0
        else:
    
            operating = np.ones((self.wt)) 
            operating[x0 <= 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=self.l, # Wind direction (defaults to site.default_wd (0,1,...,360 if not overriden))
                ws=self.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):
        x0 = np.full((wt),0.5)
        bounds=np.full((wt,2),(0,1)).reshape(-1, 2)  
        res= minimize(self.objective, x0=x0, bounds=bounds)
        return(res)
    
# =============================================================================
# Run the Optimization
# =============================================================================

wt =9
wd=np.arange(0,10,5)
ws=np.arange(4,10)  
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))


    
for l in range(len(wd)):
    for k in range(len(ws)):

        opt=optimization(wt,wd[l],ws[k],site,windTurbines,wf_model,deflection_model,turbulence_model)
        res=opt.optimization()
        C_result[l][k]=res.x
        power[l][k]=-res.fun
        status[l][k]=res.success
sadra
  • 21
  • 1
  • 7
  • 1
    That's too much code for a SO question. At the very least show the `traceback`. Assuming it occurs in the `minimize` call, double check its arguments. Do they match the `minimize` docs? what does the `self.objective` method produce? A scalar? – hpaulj Feb 10 '22 at 19:51
  • @hpaulj, the fact is that as I mentioned in the question I can get the right answer when wd or wd are small values. I am telling it was true since I have also written the same code without using classes and I got the same result. `self.objective` is the output of the function that i wanna maximize. indeed it is a code for a wind farm that i am going to maximize its power output. – sadra Feb 10 '22 at 20:27

0 Answers0