Example #1
0
    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
Example #2
0
    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
Example #3
0
    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))
        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)