def train(self): self.death = self.load_dead(self.country) self.recovered = self.load_recovered(self.country) if self.cleanRecovered: zeroRecDeaths = 0 else: zeroRecDeaths = 1 self.data = self.load_confirmed( self.country) - zeroRecDeaths * (self.recovered + self.death) bounds = [(1e-12, .2), (1e-12, .2), (1 / 60, 0.4), (1 / 60, .4), (1 / 60, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4)] maxiterations = 1500 f=create_lossOdeint(self.data, self.recovered, \ self.death, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.startNCases, \ self.weigthCases, self.weigthRecov) de = DE(f, bounds, maxiters=maxiterations) i = 0 with tqdm(total=maxiterations * 750) as pbar: for step in de.geniterator(): idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) pbar.update(i) i += 1 p = best_params[0] #parameter list for optimization #beta, beta2, sigma, sigma2, sigma3, gamma, b, mu beta, beta2, sigma, sigma2, sigma3, gamma, b, mu = p new_index, extended_actual, extended_recovered, extended_death, y0, y1, y2, y3, y4, y5 \ = self.predict(beta, beta2, sigma, sigma2, sigma3, gamma, b, mu, \ self.data, self.recovered, self.death, self.country, self.s_0, \ self.e_0, self.a_0, self.i_0, self.r_0, self.d_0) df = pd.DataFrame( { 'Susceptible': y0, 'Exposed': y1, 'Asymptomatic': y2, 'Infected data': extended_actual, 'Infected': y3, 'Recovered': extended_recovered, 'Predicted Recovered': y4, 'Death data': extended_death, 'Predicted Deaths': y5 }, index=new_index) df.to_pickle('./data/SEAIRD_sigmaOpt_' + self.country + '.pkl') print(f"country={self.country}, beta={beta:.8f}, beta2={beta2:.8f}, 1/sigma={1/sigma:.8f},"+\ f" 1/sigma2={1/sigma2:.8f},1/sigma3={1/sigma3:.8f}, gamma={gamma:.8f}, b={b:.8f},"+\ f" mu={mu:.8f}, r_0:{(beta/gamma):.8f}") print(self.country + " is done!")
def benchmark(problem, method, popsize, stop_after, maxiters, mutation, recombination): times = [] fitness = [] start = time() if method == 'scipy_de': def callback(xk, convergence): curr = time() - start fitness.append(problem(xk)) times.append(curr) if curr > stop_after: return True return False multiplier = int(round(popsize / len(problem.bounds))) if multiplier * len(problem.bounds) != popsize: raise ValueError( 'Invalid value for popsize for the number of parameters.') result = SDE(problem, problem.bounds, strategy='rand1bin', popsize=multiplier, maxiter=maxiters, mutation=mutation, recombination=recombination, polish=False, tol=-1, init='random', callback=callback) else: if method == 'yabox_de': it = DE(problem, problem.bounds, popsize=popsize, maxiters=maxiters, mutation=mutation, crossover=recombination, self_adaptive=False).geniterator() elif method == 'yabox_pde': it = PDE(problem, problem.bounds, popsize=popsize, maxiters=maxiters, mutation=mutation, crossover=recombination, self_adaptive=False).geniterator() i = 0 for status in it: curr = time() - start times.append(curr) fitness.append(status.best_fitness) i += 1 if i > maxiters or curr > stop_after: break return times, fitness
def opt(country, e0, a0, r0, d0, date, version): bounds = [(1e6,9e6),(0,15),(0,600),\ (0.1,0.7),(0.1,0.4)] maxiterations = 600 f = create_fun(country, e0, a0, r0, d0, date, version) de = DE(f, bounds, maxiters=maxiterations) i = 0 with tqdm(total=maxiterations * 500) as pbar: for step in de.geniterator(): try: idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) except: print("error in function evaluation") pbar.update(i) i += 1 p = best_params[0] return p
def opt(country, e0, a0, date, version, startNCases): if country == "China": dayInf = 0 else: dayInf = -10 bounds=[(0.5e6,20e6),(dayInf,10),(0,1500),\ (0.01,0.7),(0.01,0.12),(-50e3,50e3),(0,1500)] maxiterations = 1000 f = create_fun(country, e0, a0, date, version, startNCases) de = DE(f, bounds, maxiters=maxiterations) for step in de.geniterator(): try: idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) except: print("error in function evaluation") p = best_params[0] return p
def train(self): self.death = self.load_dead() self.recovered = self.load_confirmed() * self.ratio self.data = self.load_confirmed() - self.recovered - self.death size = len(self.data) + 1 bounds = [(1e-16, .9), (1e-16, .9), (1e-16, .9), (5, int(size * self.sigmoidTime + 0.5) - 5), (int(size * self.sigmoidTime + 0.5) + 5, size - 5), (1e-16, .9), (1e-16, 10), (1e-16, 10), (0, 10), (1e-16, 10), (1e-16, 10)] maxiterations = 4500 f = self.create_lossOdeint() de = DE(f, bounds, maxiters=maxiterations) #,popsize=100) i = 0 with tqdm(total=maxiterations * 1750 * maxiterations / 3500) as pbar: for step in de.geniterator(): idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) pbar.update(i) i += 1 p = best_params[0] if self.under: f = self.create_lossSub(p) bnds = ((.1, 4), (.1, 4)) x0 = [1, 1] minimizer_kwargs = {"method": "L-BFGS-B", "bounds": bnds} optimal = basinhopping(f, x0, minimizer_kwargs=minimizer_kwargs, disp=True, niter=30) p2 = optimal.x print("districtRegion {}".format(self.districtRegion)) print("under notifications cases {:.2f}".format(p2[0])) print("under notifications deaths {:.2f}".format(p2[1])) else: p2 = [1, 1] point = np.concatenate((p, p2)) today = datetime.today() endDate = today + timedelta(days=-2) self.end_date = datetime.strftime(endDate, '%Y-%m-%d') self.death = self.load_dead() self.recovered = self.load_confirmed() * self.ratio self.data = self.load_confirmed() - self.recovered - self.death new_index, y0, y1, y2, y3, y4, y5 = self.predict(point) #prepare dataframe to export dataFr = [y0, y1, y2, y3, y4, y5] dataFr2 = np.array(dataFr).T df = pd.DataFrame(data=dataFr2) df.columns = [ 'Susceptible', 'Exposed', 'Asymptomatic', 'Infected', 'Recovered', 'Deaths' ] df.index = pd.date_range(start=new_index[0], end=new_index[-1]) df.index.name = 'date' if self.savedata: dR = self.strip_accents(self.districtRegion) #save simulation results for comparison and use in another codes/routines df.to_pickle('./data/SEAIRD_sigmaOpt_' + dR + self.version + '.pkl') df.to_csv('./results/data/SEAIRD_sigmaOpt_' + dR + self.version + '.csv', sep=",") del dataFr, dataFr2, idx, norm_vector, best_params, df,\ new_index, y0, y1, y2, y3, y4, y5 return p
def train(self): self.death = self.load_dead(self.country) self.recovered = self.load_recovered(self.country) if self.cleanRecovered: zeroRecDeaths=0 else: zeroRecDeaths=1 self.data = self.load_confirmed(self.country)-zeroRecDeaths*(self.recovered+self.death) #optmizer solver setup and run # bounds=[(1e-12, .4), (1e-12, .4), (1/300,0.2), (1/300,0.2), (1/300,0.2),\ # (1e-12, 0.4), (1e-12, 0.2), (1e-12, 0.2)] # minimizer_kwargs = dict(method="L-BFGS-B", bounds=bounds, args=(self.data, self.recovered, \ # self.death, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.startNCases, \ # self.weigthCases, self.weigthRecov)) # x0=[0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001, 0.0001] # optimal = basinhopping(lossOdeint,x0,minimizer_kwargs=minimizer_kwargs, # niter=200,disp=True) out=[self.data, self.recovered, \ self.death, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.startNCases, \ self.weigthCases, self.weigthRecov] with open('./data/data.pkl','wb') as f: pickle.dump(out,f) bounds=[(1e-12, .2),(1e-12, .2),(1/300 ,0.4),(1/300, .4), (1/300, .4),(1e-12, .4),(1e-12, .4),(1e-12, .4)] p, f = DE(lossOdeint, bounds, popsize=256, self_adaptive=False, maxiters=35000).solve(show_progress=True) print(p[-1]) print(p[7]) #parameter list for optimization #beta, beta2, sigma, sigma2, sigma3, gamma, b, mu beta, beta2, sigma, sigma2, sigma3, gamma, b, mu = p[-1] new_index, extended_actual, extended_recovered, extended_death, y0, y1, y2, y3, y4, y5 \ = self.predict(beta, beta2, sigma, sigma2, sigma3, gamma, b, mu, \ self.data, self.recovered, self.death, self.country, self.s_0, \ self.e_0, self.a_0, self.i_0, self.r_0, self.d_0) df = pd.DataFrame({ 'Susceptible': y0, 'Exposed': y1, 'Asymptomatic': y2, 'Infected data': extended_actual, 'Infected': y3, 'Recovered': extended_recovered, 'Predicted Recovered': y4, 'Death data': extended_death, 'Predicted Deaths': y5}, index=new_index) plt.rc('font', size=14) fig, ax = plt.subplots(figsize=(15, 10)) ax.set_title("Global Opt - SEAIR-D Model for "+self.country) ax.set_ylim((0, max(y0)*1.1)) df.plot(ax=ax) #,style=['-','-','-','o','-','x','-','s','-']) print(f"country={self.country}, beta={beta:.8f}, beta2={beta2:.8f}, 1/sigma={1/sigma:.8f},"+\ f" 1/sigma2={1/sigma2:.8f},1/sigma3={1/sigma3:.8f}, gamma={gamma:.8f}, b={b:.8f},"+\ f" mu={mu:.8f}, r_0:{(beta/gamma):.8f}") plt.annotate('Dr. Guilherme Araujo Lima da Silva, www.ats4i.com', fontsize=10, xy=(1.04, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='right',rotation=90) plt.annotate('Source: https://www.lewuathe.com/covid-19-dynamics-with-sir-model.html', fontsize=10, xy=(1.06,0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left',rotation=90) plt.annotate('Original SEIR-D with delay model, São Paulo, Brazil', fontsize=10, xy=(1.045,0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left',rotation=90) df.to_pickle('./data/SEAIRD_sigmaOpt_'+self.country+'.pkl') country=self.country strFile ="./results/modelSEAIRDOptGlobalOptimum"+country+"Yabox.png" savePlot(strFile) plt.show() plt.close() plotX=new_index[range(0,self.predict_range)] plotXt=new_index[range(0,len(extended_actual))] fig, ax = plt.subplots(figsize=(15, 10)) ax.set_title("Zoom SEAIR-D Model for "+self.country) plt.xticks(np.arange(0, self.predict_range, self.predict_range/8)) ax.set_ylim(0,max(y3)*1.1) ax.plot(plotX,y3,'y-',label="Infected") ax.plot(plotX,y4,'c-',label="Recovered") ax.plot(plotX,y5,'m-',label="Deaths") ax.plot(plotXt,extended_actual,'o',label="Infected data") ax.plot(plotXt,extended_death,'x',label="Death data") ax.plot(plotXt,extended_recovered,'s',label="Recovered data") ax.legend() plt.annotate('Dr. Guilherme A. L. da Silva, www.ats4i.com', fontsize=10, xy=(1.04, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='right',rotation=90) plt.annotate('Original SEAIR-D with delay model, São Paulo, Brazil', fontsize=10, xy=(1.045,0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left',rotation=90) strFile ="./results/ZoomModelSEAIRDOpt"+country+"Yabox.png" savePlot(strFile) plt.show() plt.close() print(self.country+" is done!")
x0, args=(), method='BFGS', jac=None, hess=None, hessp=None, bounds=bounds, constraints=(), tol=None, callback=None, options=None) elif method == 'DE': ea = DE(simulator.objfunction, bounds, mutation=(0.5, 1.0), maxiters=num) p, f = ea.solve(show_progress=True) elif method == 'GA': ea = Evolutionary(simulator.objfunction, lower, upper, popsize=15, max_iter=num, random_state=3) xopt, gfit = ea.optimize(solver="cpso") # the simulator object stores all the data itself df_X = pd.DataFrame(
def train(self): self.death = self.load_dead(self.country) self.recovered = self.load_recovered(self.country) if self.cleanRecovered: zeroRecDeaths = 0 else: zeroRecDeaths = 1 self.data = self.load_confirmed( self.country) - zeroRecDeaths * (self.recovered + self.death) out=[self.data, self.recovered, \ self.death, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.startNCases, \ self.weigthCases, self.weigthRecov] with open('./data/data.pkl', 'wb') as f: pickle.dump(out, f) bounds = [(1e-12, .2), (1e-12, .2), (1 / 600, 0.4), (1 / 600, .4), (1 / 600, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4)] maxiterations = 1500 de = DE(lossOdeint, bounds, maxiters=maxiterations) i = 0 with tqdm(total=maxiterations * 1000) as pbar: for step in de.geniterator(): idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) pbar.update(i) i += 1 p = best_params[0] #parameter list for optimization #beta, beta2, sigma, sigma2, sigma3, gamma, b, mu beta, beta2, sigma, sigma2, sigma3, gamma, b, mu = p new_index, extended_actual, extended_recovered, extended_death, y0, y1, y2, y3, y4, y5 \ = self.predict(beta, beta2, sigma, sigma2, sigma3, gamma, b, mu, \ self.data, self.recovered, self.death, self.country, self.s_0, \ self.e_0, self.a_0, self.i_0, self.r_0, self.d_0) df = pd.DataFrame( { 'Susceptible': y0, 'Exposed': y1, 'Asymptomatic': y2, 'Infected data': extended_actual, 'Infected': y3, 'Recovered': extended_recovered, 'Predicted Recovered': y4, 'Death data': extended_death, 'Predicted Deaths': y5 }, index=new_index) plt.rc('font', size=14) fig, ax = plt.subplots(figsize=(15, 10)) ax.set_title("Global Opt - SEAIR-D Model for " + self.country) ax.set_ylim((0, max(y0) * 1.1)) df.plot(ax=ax) #,style=['-','-','-','o','-','x','-','s','-']) print(f"country={self.country}, beta={beta:.8f}, beta2={beta2:.8f}, 1/sigma={1/sigma:.8f},"+\ f" 1/sigma2={1/sigma2:.8f},1/sigma3={1/sigma3:.8f}, gamma={gamma:.8f}, b={b:.8f},"+\ f" mu={mu:.8f}, r_0:{(beta/gamma):.8f}") plt.annotate('Dr. Guilherme Araujo Lima da Silva, www.ats4i.com', fontsize=10, xy=(1.04, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='right', rotation=90) plt.annotate( 'Source: https://www.lewuathe.com/covid-19-dynamics-with-sir-model.html', fontsize=10, xy=(1.06, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left', rotation=90) plt.annotate('Original SEAIR-D with delay model, São Paulo, Brazil', fontsize=10, xy=(1.045, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left', rotation=90) df.to_pickle('./data/SEAIRD_sigmaOpt_' + self.country + '.pkl') country = self.country strFile = "./results/modelSEAIRDOptGlobalOptimum" + country + "Yabox.png" savePlot(strFile) plt.show() plt.close() plotX = new_index[range(0, self.predict_range)] plotXt = new_index[range(0, len(extended_actual))] fig, ax = plt.subplots(figsize=(15, 10)) ax.set_title("Zoom SEAIR-D Model for " + self.country) plt.xticks(np.arange(0, self.predict_range, self.predict_range / 8)) ax.set_ylim(0, max(y3) * 1.1) ax.plot(plotX, y3, 'y-', label="Infected") ax.plot(plotX, y4, 'c-', label="Recovered") ax.plot(plotX, y5, 'm-', label="Deaths") ax.plot(plotXt, extended_actual, 'o', label="Infected data") ax.plot(plotXt, extended_death, 'x', label="Death data") ax.plot(plotXt, extended_recovered, 's', label="Recovered data") ax.legend() plt.annotate('Dr. Guilherme A. L. da Silva, www.ats4i.com', fontsize=10, xy=(1.04, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='right', rotation=90) plt.annotate('Original SEAIR-D with delay model, São Paulo, Brazil', fontsize=10, xy=(1.045, 0.1), xycoords='axes fraction', xytext=(0, 0), textcoords='offset points', ha='left', rotation=90) strFile = "./results/ZoomModelSEAIRDOpt" + country + "Yabox.png" savePlot(strFile) plt.show() plt.close() print(self.country + " is done!")
def test_de_ackley_fun(): problem = Ackley() x, f = DE(problem, problem.bounds).solve() assert f < 1e-8
def test_de_adaptive(): x, f = DE(lambda x: sum(x**2), [(-10, 10)], self_adaptive=True).solve() # Check variance and average values of c and f pass
def test_de_simple_fun_2d(): x, f = DE(lambda x: sum(x**2), [(-10, 10), (-10, 10)]).solve() assert f < 1e-2
def train(self): self.death = self.load_dead(self.country) self.recovered = self.load_recovered(self.country) self.data = self.load_confirmed( self.country) - self.recovered - self.death size = len(self.data) bounds = [(1e-12, .2), (1e-12, .2), (5, size - 5), (1e-12, .2), (1 / 160, 0.4), (1 / 160, .4), (1 / 160, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4)] maxiterations = 5500 f=self.create_lossOdeint(self.data, \ self.death, self.recovered, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.startNCases, \ self.weigthCases, self.weigthRecov) de = DE(f, bounds, maxiters=maxiterations) #,popsize=100) i = 0 with tqdm(total=maxiterations * 1750 * maxiterations / 3500) as pbar: for step in de.geniterator(): idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) pbar.update(i) i += 1 p = best_params[0] #parameter list for optimization beta0, beta01, startT, beta2, sigma, sigma2, sigma3, gamma, b, gamma2, d, mu = p new_index, extended_actual, extended_death, extended_recovered, y0, y1, y2, y3, y4, y5 \ = self.predict(beta0, beta01, startT, beta2, sigma, sigma2, sigma3, gamma, b, gamma2, d, mu,\ self.data, self.death, self.recovered, self.country, self.s_0, \ self.e_0, self.a_0, self.i_0, self.r_0, self.d_0, self.predict_range) #prepare dataframe to export df = pd.DataFrame( { 'Susceptible': y0, 'Exposed': y1, 'Asymptomatic': y2, 'Infected data': extended_actual, 'Infected': y3, 'Recovered': extended_recovered, 'Predicted Recovered': y4, 'Death data': extended_death, 'Predicted Deaths': y5 }, index=new_index) if self.savedata: #save simulation results for comparison and use in another codes/routines df.to_pickle('./data/SEAIRDv5_Yabox_' + self.country + '.pkl') df.to_csv('./results/data/SEAIRDv5_Yabox_' + self.country + '.csv', sep=",") del idx, norm_vector, best_params, df,\ new_index, extended_actual, extended_death, extended_recovered, y0, y1, y2, y3, y4, y5 return p
def train(self): self.death = self.load_dead(self.country) self.recovered = self.load_recovered(self.country) if self.cleanRecovered: zeroRecDeaths = 0 else: zeroRecDeaths = 1 self.data = self.load_confirmed( self.country) - zeroRecDeaths * (self.recovered + self.death) size = len(self.data) halfSize = int(round(size / 2)) bounds = [(1e-12, .2), (1e-12, .2), (5, halfSize), (1e-12, .2), (halfSize + 1, size - 5), (1e-12, .2), (1 / 120, 0.4), (1 / 120, .4), (1 / 120, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4), (1e-12, .4), (0.05, 0.35)] maxiterations = 2500 f=create_lossOdeint(self.data, self.recovered, \ self.death, self.s_0, self.e_0, self.a_0, self.i_0, self.r_0, \ self.d_0, self.startNCases, \ self.weigthCases, self.weigthRecov) de = DE(f, bounds, maxiters=maxiterations) i = 0 ndims = len(bounds) with tqdm(total=maxiterations * ndims * 125) as pbar: for step in de.geniterator(): idx = step.best_idx norm_vector = step.population[idx] best_params = de.denormalize([norm_vector]) pbar.update(i) i += 1 p = best_params[0] #parameter list for optimization beta0, beta01, startT, beta02, startT2, beta2, sigma, sigma2, sigma3, gamma, b, mu, gamma2, d, p = p new_index, extended_actual, extended_recovered, extended_death, y0, y1, y2, y3, y4, y5 \ = self.predict(beta0, beta01, startT, beta02, startT2, beta2, sigma, sigma2, sigma3, gamma, b, mu, gamma2, d, p, \ self.data, self.recovered, self.death, self.country, self.s_0, \ self.e_0, self.a_0, self.i_0, self.r_0, self.d_0) df = pd.DataFrame( { 'Susceptible': y0, 'Exposed': y1, 'Asymptomatic': y2, 'Infected data': extended_actual, 'Infected': y3, 'Recovered': extended_recovered, 'Predicted Recovered': y4, 'Death data': extended_death, 'Predicted Deaths': y5 }, index=new_index) df.to_pickle('./data/SEAIRDv7_Yabox_' + self.country + '.pkl') print(f"country={self.country}, beta0={beta0:.8f}, beta01={beta01:.8f}, startT={startT:.8f}, beta02={beta02:.8f}, startT2={startT2:.8f}, beta2={beta2:.8f}, 1/sigma={1/sigma:.8f},"+\ f" 1/sigma2={1/sigma2:.8f},1/sigma3={1/sigma3:.8f}, gamma={gamma:.8f}, b={b:.8f},"+\ f" gamma2={gamma2:.8f}, d={d:.8f}, mu={mu:.8f}, r_0:{((beta0+beta01)/gamma):.8f}") print(self.country + " is done!")