Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
class TestInterface(TestCase):
    def setUp(self):
        self.simulator = create_autospec(Simulator)
        self.network = ChargingNetwork()
        self.simulator.network = self.network
        self.interface = Interface(self.simulator)
        evse1 = EVSE('PS-001')
        self.network.register_evse(evse1, 120, -30)
        evse2 = EVSE('PS-002')
        evse3 = EVSE('PS-003')
        self.network.register_evse(evse3, 360, 150)
        self.network.register_evse(evse2, 240, 90)

    def test_init(self):
        self.assertIs(self.interface._simulator, self.simulator)

    def test_active_evs(self):
        _ = self.interface.active_evs()
        self.simulator.get_active_evs.assert_called_once()

    def test_last_applied_pilot_signals_low_iteration(self):
        self.simulator.iteration = 1
        self.assertEqual(self.interface.last_applied_pilot_signals, {})

    def test_evse_voltage(self):
        self.assertEqual(self.interface.evse_voltage('PS-002'), 240)

    def test_is_feasible_empty_schedule(self):
        self.assertTrue(self.interface.is_feasible({}))

    def test_is_feasible_unequal_schedules(self):
        with self.assertRaises(InvalidScheduleError):
            self.interface.is_feasible({
                'PS-001': [1, 2],
                'PS-002': [3, 4, 5],
                'PS-003': [4, 5]
            })

    def test_is_feasible(self):
        # Mock network's is_feasible function to check its call signature later
        self.network.is_feasible = create_autospec(self.network.is_feasible)
        _ = self.interface.is_feasible({'PS-001': [1, 2], 'PS-002': [4, 5]})
        network_is_feasible_args = self.network.is_feasible.call_args
        # Check that the call to the network's is_feasible method has the correct arguments
        np.testing.assert_allclose(network_is_feasible_args[0][0],
                                   np.array([[1, 2], [0, 0], [4, 5]]))
        # Network's is_feasible method has its second argument (linear) defaulting to False. Check this is the case.
        self.assertEqual(network_is_feasible_args[0][1], False)
Exemplo n.º 4
0
class TestInterface(TestCase):
    def setUp(self) -> None:
        """ Run this setup function once before each test. """
        self.simulator = create_autospec(Simulator)
        self.network = ChargingNetwork()
        self.simulator.network = self.network
        self.interface = Interface(self.simulator)
        evse1 = EVSE("PS-001")
        self.network.register_evse(evse1, 120, -30)
        evse2 = EVSE("PS-002")
        evse3 = DeadbandEVSE("PS-003")
        self.network.register_evse(evse3, 360, 150)
        self.network.register_evse(evse2, 240, 90)
        # Include a FiniteRatesEVSE for more thorough testing.
        self.allowable_rates: List[int] = [0, 8, 16, 24, 32]
        evse4: FiniteRatesEVSE = FiniteRatesEVSE("PS-004",
                                                 self.allowable_rates)
        self.network.register_evse(evse4, 120, -30)
        for i, station_id in enumerate(self.network.station_ids):
            self.network.add_constraint(Current(station_id), 1, f"C{i+1}")

    def test_init(self) -> None:
        self.assertIs(self.interface._simulator, self.simulator)

    def test_violation_tolerance(self) -> None:
        self.assertEqual(
            self.interface._violation_tolerance,
            self.interface._simulator.network.violation_tolerance,
        )

    def test_relative_tolerance(self) -> None:
        self.assertEqual(
            self.interface._relative_tolerance,
            self.interface._simulator.network.relative_tolerance,
        )

    def test_active_evs(self) -> None:
        with self.assertWarns(UserWarning):
            _ = self.interface.active_evs
        self.simulator.get_active_evs.assert_called_once()

    def test_last_applied_pilot_signals_low_iteration(self) -> None:
        self.simulator.iteration = 1
        self.assertEqual(self.interface.last_applied_pilot_signals, {})

    def test_allowable_pilot_signals(self) -> None:
        self.assertEqual(self.interface.allowable_pilot_signals("PS-001"),
                         (True, [0, float("inf")]))

    def test_allowable_pilot_signals_deadband(self) -> None:
        self.assertEqual(self.interface.allowable_pilot_signals("PS-003"),
                         (True, [6, float("inf")]))

    def test_allowable_pilot_signals_finite_rates(self) -> None:
        self.assertEqual(
            self.interface.allowable_pilot_signals("PS-004"),
            (False, self.allowable_rates),
        )

    def test_max_pilot_signal(self) -> None:
        self.assertEqual(self.interface.max_pilot_signal("PS-001"),
                         float("inf"))

    def test_max_pilot_signal_deadband(self) -> None:
        self.assertEqual(self.interface.max_pilot_signal("PS-003"),
                         float("inf"))

    def test_max_pilot_signal_finite_rates(self) -> None:
        self.assertEqual(self.interface.max_pilot_signal("PS-004"), 32)

    def test_min_pilot_signal(self) -> None:
        self.assertEqual(self.interface.min_pilot_signal("PS-001"), 0)

    def test_min_pilot_signal_deadband(self) -> None:
        self.assertEqual(self.interface.min_pilot_signal("PS-003"), 0)

    def test_min_pilot_signal_finite_rates(self) -> None:
        self.assertEqual(self.interface.min_pilot_signal("PS-004"), 8)

    def test_evse_voltage(self) -> None:
        self.assertEqual(self.interface.evse_voltage("PS-002"), 240)

    def test_evse_phase(self) -> None:
        self.assertEqual(self.interface.evse_phase("PS-002"), 90)

    def test_get_constraints(self) -> None:
        constraint_info: Constraint = self.interface.get_constraints()
        nptest.assert_equal(constraint_info.constraint_matrix,
                            self.network.constraint_matrix)
        nptest.assert_equal(constraint_info.magnitudes,
                            self.network.magnitudes)
        self.assertEqual(constraint_info.constraint_index,
                         self.network.constraint_index)
        self.assertEqual(constraint_info.evse_index, self.network.station_ids)

    def test_is_feasible_empty_schedule(self) -> None:
        self.assertTrue(self.interface.is_feasible({}))

    def test_is_feasible_unequal_schedules(self) -> None:
        with self.assertRaises(InvalidScheduleError):
            self.interface.is_feasible({
                "PS-001": [1, 2],
                "PS-002": [3, 4, 5],
                "PS-003": [4, 5]
            })

    def test_is_feasible(self) -> None:
        # Mock network's is_feasible function to check its call signature later
        self.network.is_feasible = create_autospec(self.network.is_feasible)
        self.interface.is_feasible({"PS-001": [1, 2], "PS-002": [4, 5]})
        network_is_feasible_args = self.network.is_feasible.call_args
        # Check that the call to the network's is_feasible method has the correct
        # arguments.
        np.testing.assert_allclose(network_is_feasible_args[0][0],
                                   np.array([[1, 2], [0, 0], [4, 5], [0, 0]]))
        # Network's is_feasible method has its second argument (linear) defaulting to
        # False. Check this is the case.
        self.assertEqual(network_is_feasible_args[0][1], False)
        # Network's is_feasible method has its third argument (violation_tolerance)
        # defaulting to None. Check this is the case.
        self.assertEqual(network_is_feasible_args[0][2], 1e-5)
        self.assertEqual(network_is_feasible_args[0][3], 1e-7)

    def test_is_feasible_with_options(self) -> None:
        # Mock network's is_feasible function to check its call signature later
        self.network.is_feasible = create_autospec(self.network.is_feasible)
        self.interface.is_feasible(
            {
                "PS-001": [1, 2],
                "PS-002": [4, 5]
            },
            linear=True,
            violation_tolerance=1e-3,
            relative_tolerance=1e-5,
        )
        network_is_feasible_args = self.network.is_feasible.call_args
        # Check that the call to the network's is_feasible method has the correct
        # arguments.
        np.testing.assert_allclose(network_is_feasible_args[0][0],
                                   np.array([[1, 2], [0, 0], [4, 5], [0, 0]]))
        self.assertEqual(network_is_feasible_args[0][1], True)
        self.assertEqual(network_is_feasible_args[0][2], 1e-3)
        self.assertEqual(network_is_feasible_args[0][3], 1e-5)
Exemplo n.º 5
0
class TestInterface(TestCase):
    def setUp(self):
        self.simulator = create_autospec(Simulator)
        self.network = ChargingNetwork()
        self.simulator.network = self.network
        self.interface = Interface(self.simulator)
        evse1 = EVSE("PS-001")
        self.network.register_evse(evse1, 120, -30)
        evse2 = EVSE("PS-002")
        evse3 = EVSE("PS-003")
        self.network.register_evse(evse3, 360, 150)
        self.network.register_evse(evse2, 240, 90)

    def test_init(self):
        self.assertIs(self.interface._simulator, self.simulator)

    def test_active_evs(self):
        _ = self.interface.active_evs
        self.simulator.get_active_evs.assert_called_once()

    def test_last_applied_pilot_signals_low_iteration(self):
        self.simulator.iteration = 1
        self.assertEqual(self.interface.last_applied_pilot_signals, {})

    def test_allowable_pilot_signals(self):
        self.assertEqual(self.interface.allowable_pilot_signals("PS-001"),
                         (True, [0, float("inf")]))

    def test_evse_voltage(self):
        self.assertEqual(self.interface.evse_voltage("PS-002"), 240)

    def test_is_feasible_empty_schedule(self):
        self.assertTrue(self.interface.is_feasible({}))

    def test_is_feasible_unequal_schedules(self):
        with self.assertRaises(InvalidScheduleError):
            self.interface.is_feasible({
                "PS-001": [1, 2],
                "PS-002": [3, 4, 5],
                "PS-003": [4, 5]
            })

    def test_is_feasible(self):
        # Mock network's is_feasible function to check its call signature later
        self.network.is_feasible = create_autospec(self.network.is_feasible)
        self.interface.is_feasible({"PS-001": [1, 2], "PS-002": [4, 5]})
        network_is_feasible_args = self.network.is_feasible.call_args
        # Check that the call to the network's is_feasible method has the correct arguments
        np.testing.assert_allclose(network_is_feasible_args[0][0],
                                   np.array([[1, 2], [0, 0], [4, 5]]))
        # Network's is_feasible method has its second argument (linear) defaulting to False. Check this is the case.
        self.assertEqual(network_is_feasible_args[0][1], False)
        # Network's is_feasible method has its third argument (violation_tolerance) defaulting to None. Check this is the case.
        self.assertIsNone(network_is_feasible_args[0][2])
        self.assertIsNone(network_is_feasible_args[0][3])

    def test_is_feasible_with_options(self):
        # Mock network's is_feasible function to check its call signature later
        self.network.is_feasible = create_autospec(self.network.is_feasible)
        self.interface.is_feasible(
            {
                "PS-001": [1, 2],
                "PS-002": [4, 5]
            },
            linear=True,
            violation_tolerance=1e-3,
            relative_tolerance=1e-5,
        )
        network_is_feasible_args = self.network.is_feasible.call_args
        # Check that the call to the network's is_feasible method has the correct arguments
        np.testing.assert_allclose(network_is_feasible_args[0][0],
                                   np.array([[1, 2], [0, 0], [4, 5]]))
        self.assertEqual(network_is_feasible_args[0][1], True)
        self.assertEqual(network_is_feasible_args[0][2], 1e-3)
        self.assertEqual(network_is_feasible_args[0][3], 1e-5)