def setUp(self): start = Mock(datetime) network = ChargingNetwork() evse1 = EVSE("PS-001", max_rate=32) network.register_evse(evse1, 240, 0) evse2 = EVSE("PS-002", max_rate=32) network.register_evse(evse2, 240, 0) evse3 = EVSE("PS-003", max_rate=32) network.register_evse(evse3, 240, 0) self.scheduler = BaseAlgorithm() self.scheduler.max_recompute = None events = EventQueue(events=[Event(1), Event(2)]) self.simulator = Simulator(network, self.scheduler, events, start)
def setUpClass(cls): # Make instance of each class in registry. # Battery cls.battery1 = acnsim.Battery(100, 50, 20) cls.battery1._current_charging_power = 10 # Linear2StageBattery cls.battery2 = acnsim.Linear2StageBattery(100, 50, 20) cls.battery2._current_charging_power = 10 cls.battery2._noise_level = 0.1 cls.battery2._transition_soc = 0.85 # EVs staying_time = 10 ev1_arrival = 10 cls.ev1 = acnsim.EV( ev1_arrival, ev1_arrival + staying_time, 30, "PS-001", "EV-001", deepcopy(cls.battery1), estimated_departure=35, ) cls.ev1._energy_delivered = 0.05 cls.ev1._current_charging_rate = 10 ev2_arrival = 40 cls.ev2 = acnsim.EV( ev2_arrival, ev2_arrival + staying_time, 30, "PS-002", "EV-002", deepcopy(cls.battery2), estimated_departure=65, ) cls.ev2._energy_delivered = 0.05 cls.ev2._current_charging_rate = 10 ev3_arrival = 50 cls.ev3 = acnsim.EV( ev3_arrival, ev3_arrival + staying_time, 30, "PS-003", "EV-003", deepcopy(cls.battery2), estimated_departure=75, ) cls.ev3._energy_delivered = 0.05 cls.ev3._current_charging_rate = 10 # EVSEs cls.evse0 = acnsim.EVSE("PS-000", max_rate=32) cls.evse1 = acnsim.EVSE("PS-001", max_rate=32) cls.evse1.plugin(cls.ev1) cls.evse1.set_pilot(30, 220, 1) cls.evse2 = acnsim.DeadbandEVSE("PS-002", max_rate=32, deadband_end=4) cls.evse2.plugin(cls.ev2) cls.evse2.set_pilot(30, 220, 1) cls.evse3 = acnsim.FiniteRatesEVSE("PS-003", allowable_rates=[0, 8, 16, 24, 32]) cls.evse3.plugin(cls.ev3) cls.evse3.set_pilot(24, 220, 1) # Events cls.event = acnsim.Event(0) cls.plugin_event1 = acnsim.PluginEvent(10, cls.ev1) cls.unplug_event = acnsim.UnplugEvent(20, cls.ev1) cls.recompute_event1 = acnsim.RecomputeEvent(30) cls.plugin_event2 = acnsim.PluginEvent(40, cls.ev2) cls.plugin_event3 = acnsim.PluginEvent(50, cls.ev3) # Modify a default attribute to check if it's loaded correctly. cls.recompute_event2 = acnsim.RecomputeEvent(10) cls.recompute_event2.event_type = "Recompute Modified" # EventQueue cls.event_queue = acnsim.EventQueue() cls.event_queue.add_events([ cls.event, cls.plugin_event1, cls.recompute_event1, cls.plugin_event2, cls.plugin_event3, ]) # Network cls.network = acnsim.ChargingNetwork(violation_tolerance=1e-3, relative_tolerance=1e-5) cls.network.register_evse(cls.evse1, 220, 30) cls.network.register_evse(cls.evse2, 220, 150) cls.network.register_evse(cls.evse3, 220, -90) cls.network.constraint_matrix = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) cls.network.magnitudes = np.array([32, 32, 32]) cls.network.constraint_index = ["C1", "C2", "C3"] _ = cls.network._update_info_store() cls.empty_network = acnsim.ChargingNetwork() # Simulator cls.simulator = acnsim.Simulator( cls.network, UncontrolledCharging(), cls.event_queue, datetime(2019, 1, 1), verbose=False, store_schedule_history=True, ) # Make a copy of the simulator to run cls.simulator_run = deepcopy(cls.simulator) # Do necessary unplugs. for evse in cls.simulator_run.network._EVSEs.values(): if evse.ev is not None: evse.unplug() # Run simulation cls.simulator_run.run() # Make a copy of the simulator with signals cls.simulator_signal = deepcopy(cls.simulator) cls.simulator_signal.signals = {"a": [0, 1, 2], "b": [3, 4]} cls.simulator_hard_signal = deepcopy(cls.simulator) cls.simulator_hard_signal.signals = {"a": BaseAlgorithm()} cls.simulator_no_sch_hist = deepcopy(cls.simulator) cls.simulator_no_sch_hist.schedule_history = None # Class data used for testing. cls.simple_attributes = { "Battery": [ "_max_power", "_current_charging_power", "_current_charge", "_capacity", "_init_charge", ], "Linear2StageBattery": [ "_max_power", "_current_charging_power", "_current_charge", "_capacity", "_init_charge", "_noise_level", "_transition_soc", ], "EV": [ "_arrival", "_departure", "_requested_energy", "_estimated_departure", "_session_id", "_station_id", "_energy_delivered", "_current_charging_rate", ], "EVSE": [ "_station_id", "_max_rate", "_min_rate", "_current_pilot", "is_continuous", ], "DeadbandEVSE": [ "_station_id", "_max_rate", "_current_pilot", "_deadband_end", "is_continuous", ], "FiniteRatesEVSE": [ "_station_id", "_current_pilot", "is_continuous", "allowable_rates", ], "Event": ["timestamp", "event_type", "precedence"], "PluginEvent": ["timestamp", "event_type", "precedence"], "UnplugEvent": ["timestamp", "event_type", "precedence"], "RecomputeEvent": ["timestamp", "event_type", "precedence"], "EventQueue": ["_timestep"], "ChargingNetwork": [ "constraint_index", "violation_tolerance", "relative_tolerance", "_station_ids_dict", ], "Simulator": [ "period", "max_recompute", "verbose", "peak", "_iteration", "_resolve", "_last_schedule_update", "schedule_history", ], }
def _from_dict(cls, attribute_dict, context_dict, loaded_dict=None): """ Implements BaseSimObj._from_dict. Certain simulator attributes are not loaded completely as they are not ACN-Sim objects (signals and scheduler exist in their own modules). If the Python version used is less than 3.7, the start attribute is stored in ISO format instead of datetime, and a warning is thrown. The signals attribute is only loaded if it was natively JSON Serializable, in the original object, otherwise None is set as the signals attribute. The Simulator's signals can be set after the Simulator is loaded. The scheduler attribute is only accurate if the scheduler's constructor takes no arguments, otherwise BaseAlgorithm is stored. The Simulator provides a method to set the scheduler after the Simulator is loaded. """ # noinspection PyProtectedMember network, loaded_dict = BaseSimObj._build_from_id( attribute_dict["network"], context_dict, loaded_dict=loaded_dict ) # noinspection PyProtectedMember events, loaded_dict = BaseSimObj._build_from_id( attribute_dict["event_queue"], context_dict, loaded_dict=loaded_dict ) scheduler_cls = locate(attribute_dict["scheduler"]) try: scheduler = scheduler_cls() except TypeError: warnings.warn( f"Scheduler {attribute_dict['scheduler']} " f"requires constructor inputs. Setting " f"scheduler to BaseAlgorithm instead." ) scheduler = BaseAlgorithm() start = datetime.strptime(attribute_dict["start"], "%H:%M:%S.%f %d%m%Y") out_obj = cls( network, scheduler, events, start, period=attribute_dict["period"], signals=attribute_dict["signals"], verbose=attribute_dict["verbose"], ) scheduler.register_interface(Interface(out_obj)) attr_lst = [ "max_recompute", "peak", "_iteration", "_resolve", "_last_schedule_update", ] for attr in attr_lst: setattr(out_obj, attr, attribute_dict[attr]) if attribute_dict["schedule_history"] is not None: out_obj.schedule_history = { int(key): value for key, value in attribute_dict["schedule_history"].items() } else: out_obj.schedule_history = None out_obj.pilot_signals = np.array(attribute_dict["pilot_signals"]) out_obj.charging_rates = np.array(attribute_dict["charging_rates"]) ev_history = {} for session_id, ev in attribute_dict["ev_history"].items(): # noinspection PyProtectedMember ev_elt, loaded_dict = BaseSimObj._build_from_id( ev, context_dict, loaded_dict=loaded_dict ) ev_history[session_id] = ev_elt out_obj.ev_history = ev_history event_history = [] for past_event in attribute_dict["event_history"]: # noinspection PyProtectedMember loaded_event, loaded_dict = BaseSimObj._build_from_id( past_event, context_dict, loaded_dict=loaded_dict ) event_history.append(loaded_event) out_obj.event_history = event_history return out_obj, loaded_dict
def setUpClass(cls): # Make instance of each class in registry. # Battery cls.battery1 = acnsim.Battery(100, 50, 20) cls.battery1._current_charging_power = 10 # Linear2StageBattery cls.battery2 = acnsim.Linear2StageBattery(100, 50, 20) cls.battery2._current_charging_power = 10 cls.battery2._noise_level = 0.1 cls.battery2._transition_soc = 0.85 # EVs staying_time = 10 ev1_arrival = 10 cls.ev1 = acnsim.EV(ev1_arrival, ev1_arrival + staying_time, 30, 'PS-001', 'EV-001', deepcopy(cls.battery1), estimated_departure=25) cls.ev1._energy_delivered = 0.05 cls.ev1._current_charging_rate = 10 ev2_arrival = 40 cls.ev2 = acnsim.EV(ev2_arrival, ev2_arrival + staying_time, 30, 'PS-002', 'EV-002', deepcopy(cls.battery2), estimated_departure=25) cls.ev2._energy_delivered = 0.05 cls.ev2._current_charging_rate = 10 ev3_arrival = 50 cls.ev3 = acnsim.EV(ev3_arrival, ev3_arrival + staying_time, 30, 'PS-003', 'EV-003', deepcopy(cls.battery2), estimated_departure=25) cls.ev3._energy_delivered = 0.05 cls.ev3._current_charging_rate = 10 # EVSEs cls.evse0 = acnsim.EVSE('PS-000', max_rate=32) cls.evse1 = acnsim.EVSE('PS-001', max_rate=32) cls.evse1.plugin(cls.ev1) cls.evse1.set_pilot(30, 220, 1) cls.evse2 = acnsim.DeadbandEVSE('PS-002', max_rate=32, deadband_end=4) cls.evse2.plugin(cls.ev2) cls.evse2.set_pilot(30, 220, 1) cls.evse3 = acnsim.FiniteRatesEVSE('PS-003', allowable_rates=[0, 8, 16, 24, 32]) cls.evse3.plugin(cls.ev3) cls.evse3.set_pilot(24, 220, 1) # Events cls.event = acnsim.Event(0) cls.plugin_event1 = acnsim.PluginEvent(10, cls.ev1) cls.unplug_event = acnsim.UnplugEvent(20, 'PS-001', 'EV-001') cls.recompute_event1 = acnsim.RecomputeEvent(30) cls.plugin_event2 = acnsim.PluginEvent(40, cls.ev2) cls.plugin_event3 = acnsim.PluginEvent(50, cls.ev3) # Modify a default attribute to check if it's loaded correctly. cls.recompute_event2 = acnsim.RecomputeEvent(10) cls.recompute_event2.event_type = 'Recompute Modified' # EventQueue cls.event_queue = acnsim.EventQueue() cls.event_queue.add_events([ cls.event, cls.plugin_event1, cls.recompute_event1, cls.plugin_event2, cls.plugin_event3 ]) # Network cls.network = acnsim.ChargingNetwork(violation_tolerance=1e-3, relative_tolerance=1e-5) cls.network.register_evse(cls.evse1, 220, 30) cls.network.register_evse(cls.evse2, 220, 150) cls.network.register_evse(cls.evse3, 220, -90) cls.network.constraint_matrix = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) cls.network.magnitudes = np.array([32, 32, 32]) cls.network.constraint_index = ['C1', 'C2', 'C3'] cls.empty_network = acnsim.ChargingNetwork() # Simulator cls.simulator = acnsim.Simulator(cls.network, UncontrolledCharging(), cls.event_queue, datetime(2019, 1, 1), verbose=False, store_schedule_history=True) # Make a copy of the simulator to run cls.simulator_run = deepcopy(cls.simulator) # Do necessary unplugs. for evse in cls.simulator_run.network._EVSEs.values(): if evse.ev is not None: evse.unplug() # Run simulation cls.simulator_run.run() # Make a copy of the simulator with signals cls.simulator_signal = deepcopy(cls.simulator) cls.simulator_signal.signals = {'a': [0, 1, 2], 'b': [3, 4]} cls.simulator_hard_signal = deepcopy(cls.simulator) cls.simulator_hard_signal.signals = {'a': BaseAlgorithm()} cls.simulator_no_sch_hist = deepcopy(cls.simulator) cls.simulator_no_sch_hist.schedule_history = None # Class data used for testing. cls.simple_attributes = { 'Battery': [ '_max_power', '_current_charging_power', '_current_charge', '_capacity', '_init_charge' ], 'Linear2StageBattery': [ '_max_power', '_current_charging_power', '_current_charge', '_capacity', '_init_charge', '_noise_level', '_transition_soc' ], 'EV': [ '_arrival', '_departure', '_requested_energy', '_estimated_departure', '_session_id', '_station_id', '_energy_delivered', '_current_charging_rate' ], 'EVSE': [ '_station_id', '_max_rate', '_min_rate', '_current_pilot', 'is_continuous' ], 'DeadbandEVSE': [ '_station_id', '_max_rate', '_current_pilot', '_deadband_end', 'is_continuous' ], 'FiniteRatesEVSE': [ '_station_id', '_current_pilot', 'is_continuous', 'allowable_rates' ], 'Event': ['timestamp', 'event_type', 'precedence'], 'PluginEvent': ['timestamp', 'event_type', 'precedence'], 'UnplugEvent': [ 'timestamp', 'event_type', 'precedence', 'station_id', 'session_id' ], 'RecomputeEvent': ['timestamp', 'event_type', 'precedence'], 'EventQueue': ['_timestep'], 'ChargingNetwork': ['constraint_index', 'violation_tolerance', 'relative_tolerance'], 'Simulator': [ 'period', 'max_recompute', 'verbose', 'peak', '_iteration', '_resolve', '_last_schedule_update', 'schedule_history' ] }