return Water_Draws, Mfct_goods, Energy_Ds def partial_objective(Mfct_goods): total = 0 for county in range(N): total += Mfct_goods[county] * p_mn return total def sole_objective(params): """Partial objective, plus the cost of fossil fuels.""" lambda_Cs = params[0:N] sigma_Cs = params[N:2 * N] Water_Draws, Mfct_goods, Energy_Ds = simulate_all(lambda_Cs, sigma_Cs) return partial_objective(Mfct_goods) - sum(Energy_Ds) * lib.p_E bounds = [(0, W_surf[county] / ((1 - alpha[county]) * Area[county])) for county in range(N)] + [(0, None)] * N if __name__ == '__main__': result = lib.maximize(sole_objective, generate, bounds) print result print bounds
return -overdraws def constraint_energy(params): Crop_yields, Food_Ss, Food_Ds, Biofuel_Stocks, Biofuel_Es, \ Water_Draws, Energy_Ss, Energy_Ds, Mfct_goods = simulate_all(params) return sum(Energy_Ss) - sum(Energy_Ds) bounds = biofuel.bounds + fossil.bounds + agriculture.bounds + industry.bounds constraints = [{'type': 'ineq', 'fun': constraint_water}, {'type': 'ineq', 'fun': constraint_energy}] def safe_generate(): """Generates a starting point that satisfies both constraints.""" # Initial conditions invalid = True attempts = 0 while invalid: params = generate() ineq1 = constraint_water(params) ineq2 = constraint_energy(params) invalid = ineq1 < 0 or ineq2 < 0 attempts += 1 return params if __name__ == '__main__': result = lib.maximize(objective, safe_generate, bounds, iterations=100, constraints=constraints) print result
'type': 'ineq', 'fun': constraint_water }, { 'type': 'ineq', 'fun': constraint_energy }] def safe_generate(): """Generates a starting point that satisfies both constraints.""" # Initial conditions invalid = True attempts = 0 while invalid: params = generate() ineq1 = constraint_water(params) ineq2 = constraint_energy(params) invalid = ineq1 < 0 or ineq2 < 0 attempts += 1 return params if __name__ == '__main__': result = lib.maximize(objective, safe_generate, bounds, iterations=100, constraints=constraints) print result
return 0 # Assumes that energy supply comes from nowhere else def sole_objective(betas, Crop_yields, Energy_Ds): Biofuel_Stocks, Biofuel_Es, Water_Draws = simulate_all(betas, Crop_yields) total = 0 for county in range(N): if Biofuel_Es[county] > Energy_Ds[county]: total += (Biofuel_Es[county] - Energy_Ds[county]) * lib.p_E / lib.markup else: total += (Biofuel_Es[county] - Energy_Ds[county]) * lib.p_E return total bounds = [(0, 1.0)] * N if __name__ == '__main__': # External parameters Crop_yields = [500000] * 5 Energy_Ds = [100] * 5 print lib.maximize(sole_objective, generate, bounds, args=(Crop_yields, Energy_Ds))
import csv import numpy as np import merged, agriculture, lib with open('prices.csv', 'w') as fp: writer = csv.writer(fp) writer.writerow(['p_E', 'p_F', 'objective'] + ['beta' + str(ii) for ii in range(1, 6)] + \ ['phi' + str(ii) for ii in range(1, 6)] + \ ['lambda_A' + str(ii) for ii in range(1, 6)] + \ ['sigma_A' + str(ii) for ii in range(1, 6)] + \ ['lambda_C' + str(ii) for ii in range(1, 6)] + \ ['sigma_C' + str(ii) for ii in range(1, 6)]) for p_E in np.exp(np.linspace(np.log(.1), np.log(10), 60)): #[.1, .2, .5, 1.0, 2.0, 5.0, 10.0]: for p_F in np.exp( np.linspace(np.log(.1), np.log(10), 60)): #[.1, .2, .5, 1.0, 2.0, 5.0, 10.0]: print p_E, p_F lib.p_E = p_E agriculture.p_F = p_F result = lib.maximize(merged.objective, merged.safe_generate, merged.bounds, iterations=20, constraints=merged.constraints) writer.writerow([p_E, p_F] + [result['fun']] + result['x'])
else: total += (Food_Ss[county] - Food_Ds[county]) * p_F return total def sole_objective(params, betas): """Partial objective, plus cost of energy.""" lambda_As = params[0:N] sigma_As = params[N:2 * N] Crop_yields, Food_Ss, Food_Ds, Water_Draws, Energy_Ds = simulate_all( lambda_As, sigma_As, betas) return partial_objective(Food_Ss, Food_Ds) - sum(Energy_Ds) * lib.p_E # Cannot use more water than available, but may use any amount of nergy bounds = [(0, W_surf[county] / (alpha[county] * Area[county])) for county in range(N)] + \ [(0, None)] * N if __name__ == '__main__': betas = [0.5] * 5 result = lib.maximize(sole_objective, generate, bounds, args=(betas, )) print result Crop_yields, Food_Ss, Food_Ds, Water_Draws, Energy_Ds = simulate_all( result['x'][0:N], result['x'][N:2 * N], betas) print np.mean(Crop_yields)
return Biofuel_Stocks, Biofuel_Es, Water_Draws # Just sells of any remaining def partial_objective(Biofuel_Es, Energy_Ds): if sum(Biofuel_Es) > sum(Energy_Ds): return sum(Biofuel_Es) - sum(Energy_Ds)*lib.p_E / lib.markup return 0 # Assumes that energy supply comes from nowhere else def sole_objective(betas, Crop_yields, Energy_Ds): Biofuel_Stocks, Biofuel_Es, Water_Draws = simulate_all(betas, Crop_yields) total = 0 for county in range(N): if Biofuel_Es[county] > Energy_Ds[county]: total += (Biofuel_Es[county] - Energy_Ds[county])*lib.p_E / lib.markup else: total += (Biofuel_Es[county] - Energy_Ds[county])*lib.p_E return total bounds = [(0, 1.0)] * N if __name__ == '__main__': # External parameters Crop_yields = [500000] * 5 Energy_Ds = [100] * 5 print lib.maximize(sole_objective, generate, bounds, args=(Crop_yields, Energy_Ds))
Water_Draw = lambda_f * Fossil_E return Fossil_E, Water_Draw def simulate_all(phis): Fossil_Es = [] Water_Draws = [] for county in range(N): Fossil_E, Water_Draw = simulate_county(phis[county]) Fossil_Es.append(Fossil_E) Water_Draws.append(Water_Draw) return Fossil_Es, Water_Draws def objective(phis): total = 0 for county in range(N): total -= phis[county] * lib.p_E # this all costs us return total bounds = [(0, 1e9)] * N if __name__ == '__main__': result = lib.maximize(objective, generate, bounds) print result print bounds
if Food_Ss[county] > Food_Ds[county]: total += (Food_Ss[county] - Food_Ds[county])*p_F / lib.markup else: total += (Food_Ss[county] - Food_Ds[county])*p_F return total def sole_objective(params, betas): """Partial objective, plus cost of energy.""" lambda_As = params[0:N] sigma_As = params[N:2*N] Crop_yields, Food_Ss, Food_Ds, Water_Draws, Energy_Ds = simulate_all(lambda_As, sigma_As, betas) return partial_objective(Food_Ss, Food_Ds) - sum(Energy_Ds)*lib.p_E # Cannot use more water than available, but may use any amount of nergy bounds = [(0, W_surf[county] / (alpha[county] * Area[county])) for county in range(N)] + \ [(0, None)] * N if __name__ == '__main__': betas = [0.5] * 5 result = lib.maximize(sole_objective, generate, bounds, args=(betas,)) print result Crop_yields, Food_Ss, Food_Ds, Water_Draws, Energy_Ds = simulate_all(result['x'][0:N], result['x'][N:2*N], betas) print np.mean(Crop_yields)