I'm working on an optimization code using differential evolution to solve. However, it is taking a long time to get the solution. See that I have a variable called number_of_iterations that must equal 1000000. When I consider this decision variable equal to a smaller value (eg number_of_iterations=100), the result is pretty quick. But when I use a million, the result takes a long time. Does anyone have any suggestions on how to speed up this procedure?
Here's the code I'm using:
import numpy as np import random from scipy.optimize import differential_evolution,NonlinearConstraint import math V = 220 FP = 0.85 FS = 1.15 cn = 13.0 cm = cn*FS alpha = 1 beta = 1000 Cp = 50 Cc = 250 h = 24 Ch = 0.4 n = 273.487851 B = 2.929689 pi = 4210.61 def otm(y): T=y cn_m=y Custo_Eqp = Ciclo = Custo_acumulado = Ciclo_acumulado = 0 number_of_iterations=1000000 for i in range(1, number_of_iterations): cn = 13.0 cr = cn stop = 1 W = math.floor(random.weibullvariate(n,B)) j = 1 while stop == 1: j = j+1 x = np.random.gamma(alpha,beta) cr = cr+x pf = (3**0.5)*V*cr*FP p = ((pi+pf)*1)/2 Ce = ((p*(j*h))/1000)*Ch if T < W: if j <= T and cr > cm: Custo_Eqp = Ce+Cc Ciclo = j stop = 0 elif j < T and cn_m <= cr <= cm: Custo_Eqp = Ce+Cp Ciclo = j stop = 0 elif j == T and cr <= cm: Custo_Eqp = Ce+Cp Ciclo = T stop = 0 else: if j <= W and cr > cm: Custo_Eqp = Ce+Cc Ciclo = j stop = 0 elif j < W and cn_m <= cr < cm: Custo_Eqp = Ce+Cp Ciclo = j stop = 0 elif j == W and cr <= cm: Custo_Eqp = Ce+Cc Ciclo = W stop = 0 Custo_acumulado = Custo_acumulado + Custo_Eqp Ciclo_acumulado = Ciclo_acumulado + Ciclo return Custo_acumulado/Ciclo_acumulado y0=(round(n-(0.2*n)),round(n+(0.2*n))) y1=(cn,cm) bounds=(y0,y1) nlc = NonlinearConstraint(otm, 0, +np.inf) res=differential_evolution(otm, bounds=bounds, constraints=(nlc),disp = True,maxiter=100) print(res)
I tried running it on colab pro, but little advance in speed I had. Is there any other place that might even pay to get a lot of speed?