def test_update_schedules_infeasible_schedule(self): network = ChargingNetwork() network.register_evse(EVSE("PS-001"), 240, 0) network.register_evse(EVSE("PS-004"), 240, 0) network.register_evse(EVSE("PS-003"), 240, 0) network.register_evse(EVSE("PS-002"), 240, 0) network.register_evse(EVSE("PS-006"), 240, 0) curr_dict1 = {"PS-001": 0.25, "PS-002": 0.50, "PS-003": -0.25} current1 = Current(curr_dict1) curr_dict2 = {"PS-006": 0.30, "PS-004": -0.60, "PS-002": 0.50} current2 = Current(curr_dict2) network.add_constraint(current1, 50, name="first_constraint") network.add_constraint(current2, 10) start = Mock(datetime) scheduler = create_autospec(BaseAlgorithm) scheduler.max_recompute = None events = EventQueue(events=[Event(1), Event(2)]) simulator = Simulator(network, scheduler, events, start) bad_schedule = { "PS-001": [200, 0, 160, 0], "PS-004": [0, 0, 0, 0], "PS-003": [0, 0, 0, 0], "PS-002": [0, 0, 26, 0], "PS-006": [0, 0, 0, 40], } with self.assertWarnsRegex( UserWarning, r"Invalid schedule provided at iteration 0. " r"Max violation is 2.9999\d+? A on _const_1 at time index 2.", ): simulator._update_schedules(bad_schedule)
def generate_events( self, sessions_per_day: List[int], period: float, voltage: float, max_battery_power: float, max_len: int = None, battery_params: Optional[BatteryParams] = None, force_feasible: bool = False, ) -> EventQueue: """ Return EventQueue from random generated samples. Args: sessions_per_day (List[int]): Number of sessions to sample for each day of the simulation. period (int): Length of each time interval. (minutes) voltage (float): Voltage of the network. max_battery_power (float): Default maximum charging power for batteries. max_len (int): Maximum length of a session. (periods) Default None. battery_params (Dict[str, object]): Dictionary containing parameters for the EV's battery. Three keys are supported. If none, Battery type is used with default configuration. Default None. - 'type' maps to a Battery-like class. (required) - 'capacity_fn' maps to a function which takes in the the energy delivered to the car, the length of the session, the period of the simulation, and the voltage of the system. It should return a tuple with the capacity of the battery and the initial charge of the battery both in A*periods. - 'kwargs' maps to a dictionary of keyword arguments which will be based to the Battery constructor. force_feasible (bool): If True, the requested_energy of each session will be reduced if it exceeds the amount of energy which could be delivered at maximum rate during the duration of the charging session. Default False. Returns: EventQueue: Queue of plugin events for the samples charging sessions. """ daily_sessions: List[np.ndarray] = [] for d, num_sessions in enumerate(sessions_per_day): if num_sessions > 0: daily_arrivals = self.sample(num_sessions) daily_arrivals[:, 0] += 24 * d daily_sessions.append(daily_arrivals) ev_matrix = np.vstack([day for day in daily_sessions if day is not None]) evs = self._convert_ev_matrix( ev_matrix, period, voltage, max_battery_power, max_len, battery_params, force_feasible, ) events = [PluginEvent(sess.arrival, sess) for sess in evs] return EventQueue(events)
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 setUp(self): self.events = EventQueue()
class TestEventQueue(TestCase): def setUp(self): self.events = EventQueue() def test_empty_on_init(self): self.assertTrue(self.events.empty()) def test_add_event(self): self.events.add_event(Event(5)) self.assertFalse(self.events.empty()) self.assertEqual(len(self.events._queue), 1) def test_add_events(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertFalse(self.events.empty()) self.assertEqual(5, len(self.events._queue)) def test_len(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertEqual(5, len(self.events)) def test_get_event(self): self.events.add_event(Event(5)) self.assertEqual(len(self.events._queue), 1) e = self.events.get_event() self.assertTrue(self.events.empty()) self.assertEqual(5, e.timestamp) def test_get_current_events(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) curr_events = self.events.get_current_events(3) self.assertEqual(len(curr_events), 3) def test_get_last_timestamp(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertEqual(5, self.events.get_last_timestamp()) self.events.add_event(Event(8)) self.assertEqual(8, self.events.get_last_timestamp()) curr_events = self.events.get_current_events(3) self.assertEqual(8, self.events.get_last_timestamp()) def test_get_last_timestamp_no_events(self): events = [] self.events.add_events(events) self.assertIsNone(self.events.get_last_timestamp())
class TestEventQueue(TestCase): def setUp(self): self.events = EventQueue() def test_empty_on_init(self): self.assertTrue(self.events.empty()) def test_add_event(self): self.events.add_event(Event(5)) self.assertFalse(self.events.empty()) self.assertEqual(len(self.events._queue), 1) def test_add_events(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertFalse(self.events.empty()) self.assertEqual(5, len(self.events._queue)) def test_len(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertEqual(5, len(self.events)) def test_get_event(self): self.events.add_event(Event(5)) self.assertEqual(len(self.events._queue), 1) e = self.events.get_event() self.assertTrue(self.events.empty()) self.assertEqual(5, e.timestamp) def test_get_current_events_empty(self): events = [] self.events.add_events(events) curr_events = self.events.get_current_events(3) self.assertEqual(curr_events, []) def test_get_current_events(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) curr_events = self.events.get_current_events(3) self.assertEqual(len(curr_events), 3) for event, timestamp in zip(curr_events, [1, 2, 3]): self.assertEqual(event.timestamp, timestamp) def test_get_last_timestamp(self): events = [Event(i) for i in range(1, 6)] self.events.add_events(events) self.assertEqual(5, self.events.get_last_timestamp()) self.events.add_event(Event(8)) self.assertEqual(8, self.events.get_last_timestamp()) # Check that the last timestamp is unaltered from a call to # get_current_events at an earlier timestamp. _ = self.events.get_current_events(3) self.assertEqual(8, self.events.get_last_timestamp()) def test_get_last_timestamp_no_events(self): events = [] self.events.add_events(events) self.assertIsNone(self.events.get_last_timestamp())