def test_instanciate(self): from lms2 import AbsLModel from lms2 import ScalablePowerSource import pandas as pd from pyomo.dae import ContinuousSet from pyomo.environ import TransformationFactory m = AbsLModel() m.time = ContinuousSet() m.u = ScalablePowerSource(curtailable=False) df = pd.Series({0: 0, 1: 1, 2: 2, 3: 1, 4: 0}) data_u = { 'time': {None: [0, 4]}, 'profile_index': {None: df.index}, 'profile_value': df.to_dict()} data = \ {None: { 'time': {None: [0, 4]}, 'u': data_u } } inst = m.create_instance(data) TransformationFactory('dae.finite_difference').apply_to(inst, nfe=4) self.assertFalse(hasattr(inst.u, 'p_curt')) self.assertTrue(hasattr(inst.u, 'p')) self.assertTrue(hasattr(inst.u, 'p_scaled')) self.assertTrue(hasattr(inst.u, 'scale_fact'))
def test_battery_v0(self): from lms2 import BatteryV0, PowerLoad, FixedPowerLoad, AbsLModel from pyomo.environ import TransformationFactory, SolverFactory from pyomo.dae import ContinuousSet from pyomo.network import Arc m = AbsLModel() m.time = ContinuousSet() m.b = BatteryV0() m.pl = FixedPowerLoad() m.ps = PowerLoad() m.arc1 = Arc(source=m.b.outlet, dest=m.pl.inlet) m.arc2 = Arc(source=m.b.outlet, dest=m.ps.inlet) data_batt = dict( time={None: [0, 10]}, dpcmax={None: 100000}, dpdmax={None: 100000}, emin={None: 0}, emax={None: 500}, pcmax={None: 80}, pdmax={None: 80}, e0={None: 50}, ef={None: None}, etac={None: 1.0}, etad={None: 1.0}) data_pl = { 'time': {None: [0, 10]}, 'profile_index': {None: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}, 'profile_value': dict(zip([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 0, -10, -90, -20, 20, 30, 40, 40, 10])) } data_ps = { 'time': {None: (0, 10)} } data = \ {None: { 'time': {None: [0, 10]}, 'b': data_batt, 'pl': data_pl, 'ps': data_ps } } inst = m.create_instance(data) from lms2.economic.cost import def_absolute_cost from pyomo.environ import Objective from pyomo.dae import Integral inst.ps.instant_cost = def_absolute_cost(inst.ps, var_name='p') inst.new_int = Integral(inst.time, wrt=inst.time, rule=lambda b, t: b.ps.instant_cost[t]) TransformationFactory('dae.finite_difference').apply_to(inst, nfe=5) TransformationFactory("network.expand_arcs").apply_to(inst) inst.obj = Objective(expr=inst.new_int) opt = SolverFactory("glpk") from time import time t1 = time() results = opt.solve(inst, tee=False) print(f'Solve time : {time() - t1:0.4f} s') from pyomo.opt import SolverStatus, TerminationCondition self.assertTrue(results.solver.status == SolverStatus.ok) self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal)
if m.pmax.value is None: return Constraint.Skip return m.p_out[t] <= m.pmax @self.Constraint(self.time) def efficiency(m, t): return m.p_out[t] == m.eta * m.p_in[t] self.inlet = Port(initialize={'f': (self.p_in, Port.Conservative)}) self.outlet = Port(initialize={'f': (self.p_out, Port.Conservative)}) if __name__ == "__main__": from lms2 import AbsLModel from pyomo.dae import ContinuousSet m = AbsLModel(name='test') m.time = ContinuousSet m.b = SimpleConverter() data_conv = { 'time': {None: (0, 1)}, 'pmax': {None: -10}, 'eta': {None: 1}} data = {None: dict(time={None: [0, 1]}, b=data_conv)} inst = m.create_instance(data) inst.pprint()
from pyomo.environ import Var, Param, Objective, Constraint from pyomo.environ import NonNegativeIntegers from lms2 import AbsLModel m = AbsLModel(name='Model') m.v = Var(doc='a viariable', within=NonNegativeIntegers) m.p = Param(default=10, doc='a parameter') m.c = Param(default=1, doc='cost associated to variable "v"') m.cst = Constraint(expr=10 <= m.v * m.p <= 15) m.obj = Objective(expr=m.c * m.v, sense=1) inst = m.create_instance() inst.pprint() from pyomo.environ import SolverFactory opt = SolverFactory("glpk") results = opt.solve(inst, tee=False) print(inst.v()) data = {None: {'p': {None: 5}, 'c': {None: 2}}} inst2 = m.create_instance(data) results = opt.solve(inst2, tee=False) print(inst.v())
def test_battery_v3(self): from lms2 import BatteryV3, FixedPowerLoad, AbsLModel, PVPanels, DebugSource, MainGridV1 from lms2.economic.cost import def_absolute_cost from pyomo.environ import TransformationFactory, SolverFactory from pyomo.dae import ContinuousSet from pyomo.network import Arc import numpy as np import pandas as pd m = AbsLModel() m.time = ContinuousSet(initialize=(0, 10)) m.b = BatteryV3(method='piecewise') m.pl = FixedPowerLoad() m.debug = DebugSource() m.mg = MainGridV1() m.ps = PVPanels(curtailable=True) m.arc1 = Arc(source=m.b.outlet, dest=m.pl.inlet) m.arc2 = Arc(source=m.ps.outlet, dest=m.pl.inlet) m.arc3 = Arc(source=m.debug.outlet, dest=m.pl.inlet) m.arc4 = Arc(source=m.mg.outlet, dest=m.pl.inlet) m.b.inst_cost = def_absolute_cost(m.b, var_name='dp') t = pd.timedelta_range(start=0, end='2 days', freq='30Min').total_seconds() ps = [(-np.cos(2 * np.pi * i / (86400)) + 1)**6 / 2**6 * (0.2 * np.sin(2 * np.pi * i / (86400 * 7)) + 0.4) * 10 for i in t] pl = np.array([5] * len(t)) time = (t[0], 86400 * 2) nfe = 24 * 2 * 60 / 30 data_batt = dict( time={None: time}, dpcmax={None: 100}, dpdmax={None: 100}, socmin={None: 40}, socmax={None: 100}, soc0={None: 50}, socf={None: 50}, # final soc socabs={None: 85}, # absorption soc emin={None: 40}, emax={None: 100}, pcmax={None: 20}, pdmax={None: 20}, etac={None: 0.90}, etad={None: 0.90}, pw_i={None: [1, 2, 3]}, pw_j={None: [1, 2]}, pw_soc={ 1: 40, 2: 85, 3: 100 }, pw_pcmax={ 1: 20, 2: 20, 3: 1 }, pfloat={None: 0.125}, max_cycles={None: 10}, cycle_passed={None: 8}, dp_cost={None: 0}) data_mg = { 'time': { None: time }, 'cost_out': { None: 0.15 }, 'cost_in': { None: 0 }, 'pmax': { None: 30 }, 'pmin': { None: 0 } } data_pl = { 'time': { None: time }, 'profile_index': { None: t }, 'profile_value': dict(zip(t, pl)) } data_ps = { 'time': { None: time }, 'profile_index': { None: t }, 'profile_value': dict(zip(t, ps)) } data_debug = {'time': {None: time}, 'p_cost': {None: 10}} data = { None: dict(time={None: time}, b=data_batt, mg=data_mg, ps=data_ps, debug=data_debug, pl=data_pl) } inst = m.create_instance(data) inst.ps.surf.fix(4) from lms2.economic.cost import def_absolute_cost from pyomo.environ import Objective from pyomo.dae import Integral from pyomo.opt import SolverStatus, TerminationCondition TransformationFactory('dae.finite_difference').apply_to(inst, nfe=nfe) TransformationFactory("network.expand_arcs").apply_to(inst) inst.ps.instant_cost = def_absolute_cost(inst.ps, var_name='p') inst.new_int = Integral(inst.time, wrt=inst.time, rule=lambda b, t: b.debug.inst_cost[t] + b.b. inst_cost[t] + b.mg.instant_cost[t]) inst.b._nbr_charge.reconstruct() inst.obj = Objective(expr=inst.new_int) opt = SolverFactory("gurobi", solver_io="direct") results = opt.solve(inst, tee=False) self.assertTrue(results.solver.status == SolverStatus.ok) self.assertTrue(results.solver.termination_condition == TerminationCondition.optimal) self.assertAlmostEqual(7.8386091, inst.obj(), places=5)