def assets_to_factors_optim(self, gamma=0.5, A=None, activate_cons=True): if A is None: A = self.asset_to_factor_exp() if activate_cons: lb = np.zeros(self.n_factors) ub = np.ones(self.n_factors) bnds = tuple([(i, j) for i, j in zip(lb, ub)]) cons = lc(np.ones((1, len(lb))), np.ones(1), np.ones(1)) else: bnds = None cons = () w_ini = np.ones(self.n_factors) / self.n_factors sol = minimize(self.assets_to_factors_obj, w_ini, args=(self.w_asset.values, gamma), bounds=bnds, constraints=cons, method='SLSQP') w_f = pd.Series(np.round(sol.x, 5), index=self.w_factor.index) return w_f
def main(): # Create feedstock instances manure = Feedstock("dairy", 200, 16.31, 0.65, 1500, 0.5) silage = Feedstock("mais", 120, 81.10, 0.41, 130, 0.5) silage.fshow_properties() manure.fshow_properties() # create mixed feedstock instances with averaged data cn_mix = ad_util.fco_cn([manure.x, silage.x], [manure.cn, silage.cn]) bd_mix = ad_util.fco_bd([manure.x, silage.x], [manure.bd, silage.bd]) d_mix = ad_util.fco_d([manure.x, silage.x], [manure.d, silage.d]) bmp_mix = -ad_util.fblending([manure.x, silage.x], [manure.bmp, silage.bmp], [manure.cn, silage.cn], [manure.bd, silage.bd]) mix_fs = Feedstock("mix", bmp_mix, cn_mix, bd_mix, d_mix, 1) mix_fs.fshow_properties() # solve ode system with initial substrate composition tspan = np.linspace(0, 35, 1000) init_manure = manure.fsubstrate() init_silage = silage.fsubstrate() S0 = init_manure[0] + init_silage[0] X0 = init_manure[1] + init_silage[1] x_mix = mix_fs.fcodigestion(35, [km, Ks, kd]) S_mix = x_mix[0] X_mix = x_mix[1] M_mix = x_mix[2] # obtain the optimized blending stat print("\nFeedstock blending optimization has started...") x_0 = [manure.x, silage.x] bounds = bd([0, 0], [1, 1]) constr = lc([[manure.cn, silage.cn], [manure.bd, silage.bd], [1, 1]], [20, 0.6, 1], [40, 1, 1]) blend = minimize(ad_util.fblending, x_0, args=([manure.bmp, silage.bmp],\ [manure.cn, silage.cn],[manure.bd, silage.bd]), \ method='trust-constr', bounds=bounds, constraints=constr, tol=1e-9, \ options={'verbose': 1, 'disp': True}) print("\n") manure.x = blend.x[0] silage.x = blend.x[1] manure.fshow_properties() silage.fshow_properties() # generate the new blended mixture instance cn_mix = ad_util.fco_cn([manure.x, silage.x], [manure.cn, silage.cn]) bd_mix = ad_util.fco_bd([manure.x, silage.x], [manure.bd, silage.bd]) d_mix = ad_util.fco_d([manure.x, silage.x], [manure.d, silage.d]) bmp_mix = -ad_util.fblending([manure.x, silage.x], [manure.bmp, silage.bmp], [manure.cn, silage.cn], [manure.bd, silage.bd]) mix_new = Feedstock("mix", bmp_mix, cn_mix, bd_mix, d_mix, 1) mix_new.fshow_properties() # solve ode system with new substrate composition x_blend = mix_new.fcodigestion(35, [km, Ks, kd]) S_blend = x_blend[0] X_blend = x_blend[1] M_blend = x_blend[2] # visualization plt.figure(1) plt.title('Simulation at original blending stats (i=m=50%)') plt.plot(tspan, S_mix, label='S') plt.plot(tspan, X_mix, label='X') plt.plot(tspan, M_mix, label='M') plt.xlabel('time [d]') plt.ylabel('concentration [kg/m3]') plt.legend(loc = 'best') plt.ylim([0, 1500]) plt.grid(True) plt.figure(2) newtitle = 'Simulation at new blending stats (i='+'{:.2f}'.format(blend.x[1])+' m='+'{:.2f}'.format(blend.x[0])+')' plt.title(newtitle) plt.plot(tspan, S_blend, label='S') plt.plot(tspan, X_blend, label='X') plt.plot(tspan, M_blend, label='M') plt.xlabel('time [d]') plt.ylabel('concentration [kg/m3]') plt.legend(loc = 'best') plt.ylim([0, 1500]) plt.grid(True) plt.show() return 0
def main(): # retrive solving information tspan = np.linspace(0, 35, 1000) init = feffluent(x_M, d_M, d_I) S0 = init[0] X0 = init[1] # solve ode system with initial substrate composition x_blend_0 = odeint(fsimulate, [S0, X0, 0], tspan, args=([km, Ks, kd], )) S_blend_0 = x_blend_0[:, 0] X_blend_0 = x_blend_0[:, 1] M_blend_0 = x_blend_0[:, 2] # obtain the optimized blending stat x_0 = [x_M, x_I] bounds = bd([0, 0], [1, 1]) constr = lc([[CN_M, CN_I], [BD_M, BD_I], [1, 1]], [20, 0.6, 1], [30, 1, 1]) blend = minimize(fblending, x_0, args=([BMPe_M, BMPe_I],[CN_M, CN_I],[BD_M, BD_I]), \ method='trust-constr', bounds=bounds, constraints=constr, tol=1e-9, \ options={'verbose': 1, 'disp': True}) # solve ode system with new substrate composition init_n = feffluent(blend.x[0], d_M, d_I) S0_n = init_n[0] X0_n = init_n[1] x_blend_1 = odeint(fsimulate, [S0_n, X0_n, 0], tspan, args=([km, Ks, kd], )) S_blend_1 = x_blend_1[:, 0] X_blend_1 = x_blend_1[:, 1] M_blend_1 = x_blend_1[:, 2] print(blend) # visualization plt.figure(1) plt.title('Simulation at original blending stats (i=m=50%)') plt.plot(tspan, S_blend_0, label='S') plt.plot(tspan, X_blend_0, label='X') plt.plot(tspan, M_blend_0, label='M') plt.xlabel('time [d]') plt.ylabel('concentration [kg/m3]') plt.legend(loc='best') plt.ylim([0, 1500]) plt.grid(True) plt.figure(2) newtitle = 'Simulation at new blending stats (i=' + '{:.2f}'.format( blend.x[1]) + ' m=' + '{:.2f}'.format(blend.x[0]) + ')' plt.title(newtitle) plt.plot(tspan, S_blend_1, label='S') plt.plot(tspan, X_blend_1, label='X') plt.plot(tspan, M_blend_1, label='M') plt.xlabel('time [d]') plt.ylabel('concentration [kg/m3]') plt.legend(loc='best') plt.ylim([0, 1500]) plt.grid(True) plt.show() return 0