def construct_nvm(): from msppy.msp import MSLP import numpy as np nvm = MSLP(T=3, sense=-1, bound=500) def sample_path_generator(random_state, size): a = np.zeros([size, 3, 1]) for t in range(1, 3): a[:, t, :] = (0.5 * a[:, t - 1, :] + random_state.lognormal(2.5, 1, size=[size, 1])) return a nvm.add_Markovian_uncertainty(sample_path_generator) for t in range(3): m = nvm[t] buy_now, buy_past = m.addStateVar(name='bought', obj=-1.0) if t != 0: sold = m.addVar(name='sold', obj=2) unsatisfied = m.addVar(name='unsatisfied') recycled = m.addVar(name='recycled', obj=0.5) m.addConstr(sold + unsatisfied == 0, uncertainty_dependent={'rhs': 0}) m.addConstr(sold + recycled == buy_past) return nvm
def g(random_state, size): a = numpy.empty([size, T, 2]) a[:, 0, :] = [[1.2, 1.06]] for t in range(1, T): noise = random_state.multivariate_normal( mean=[1.0, 1.0], cov=[[0.1, 0], [0, 0.01]], size=size, ) a[:, t, :] = 0.1 * a[:, t - 1, :] + noise return a AssetMgt = MSLP(T=T, sense=-1, bound=1e5, outputFlag=0) AssetMgt.add_Markovian_uncertainty(g) for t in range(T): m = AssetMgt[t] now, past = m.addStateVars(2, lb=0, obj=0) if t == 0: m.addConstr(now[0] + now[1] == 55) if t in [1, 2]: m.addConstr(past[0] + past[1] == now[0] + now[1], uncertainty_dependent={ past[0]: 0, past[1]: 1 }) if t == 3: y = m.addVar(obj=1) w = m.addVar(obj=-4) m.addConstr(past[0] + past[1] - y + w == 80,
} pandas.DataFrame( [pandas.Series(result_simulated),pandas.Series(result)], index=['Simulated','True'] ).to_csv("./result/statistics_MCA.csv") # evaluation table evaluation_tab = [] evaluationTrue_tab = [] for lambda_ in [0.0,0.25,0.5,0.75]: AssetMgt = MSLP( T=T, sense=-1, bound=200 ) N = 100 K = 5 AssetMgt.add_Markovian_uncertainty(generator_augmented) for t in range(T): m = AssetMgt[t] now, past = m.addStateVars(N+1, lb=0, obj=0, name='asset') if t == 0: buy = m.addVars(N, name='buy') sell = m.addVars(N, name='sell') m.addConstrs(now[j] == buy[j] - sell[j] for j in range(N)) m.addConstr( now[N] == 100 - (1+fee) * gurobipy.quicksum(buy[j] for j in range(N)) + (1-fee) * gurobipy.quicksum(sell[j] for j in range(N)) ) elif t != T-1: sell = m.addVars(N, name='sell') buy = m.addVars(N, name='buy')
def generator(random_state, size): inflow = numpy.empty([size, T, 4]) inflow[:, 0, :] = inflow_initial[numpy.newaxis:, ] for t in range(T - 1): noise = numpy.exp( random_state.multivariate_normal(mean=[0] * 4, cov=sigma[t % 12], size=size)) inflow[:, t + 1, :] = noise * ((1 - gamma[t % 12]) * exp_mu[t % 12] + gamma[t % 12] * exp_mu[t % 12] / exp_mu[ (t - 1) % 12] * inflow[:, t, :]) return inflow HydroThermal = MSLP(T=T, bound=0, discount=0.9906) HydroThermal.add_Markovian_uncertainty(generator) for t in range(T): m = HydroThermal[t] stored_now, stored_past = m.addStateVars(4, ub=hydro_['UB'][:4], name="stored") spill = m.addVars(4, obj=0.001, name="spill") hydro = m.addVars(4, ub=hydro_['UB'][-4:], name="hydro") deficit = m.addVars( [(i, j) for i in range(4) for j in range(4)], ub=[ demand.iloc[t % 12][i] * deficit_['DEPTH'][j] for i in range(4) for j in range(4) ], obj=[deficit_['OBJ'][j] for i in range(4) for j in range(4)], name="deficit")