def test_simulation_arg_to_param_map(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, arg_to_param_map={0: "txpower"}, ) pta.add_transition("IDLE", "TX", "send", energy=3, duration=10) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10]] expected_energy = 510000 expected_duration = 50120 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})
def test_simulation(self): pta = PTA() pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", duration=50000) pta.add_transition("IDLE", "TX", "send", energy=3, duration=10) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [ ["init"], [None, 10000000], ["send", "foo", 3], [None, 5000000], ["send", "foo", 3], ] expected_energy = 5.0 * 10000000 + 3 + 100 * 2000 + 5 * 5000000 + 3 + 100 * 2000 expected_duration = 50000 + 10000000 + 10 + 2000 + 5000000 + 10 + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {})
def test_simulation_set_param(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition( "UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000, set_param={"txpower": 10}, ) trace = [["init"]] expected_energy = 500000 expected_duration = 50000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})
def test_simulation_param_function(self): pta = PTA(parameters=["length", "txpower"]) pta.add_state("IDLE", power=5) pta.add_state( "TX", power=AnalyticFunction( 100, "regression_arg(0) + regression_arg(1) * parameter(txpower)", ["length", "txpower"], regression_args=[1000, 2], ), ) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["length", "txpower"], regression_args=[3, 5], num_args=2, ), duration=10, param_update_function=lambda param, arg: { **param, "length": arg[1] }, ) pta.add_transition( "TX", "IDLE", "txComplete", is_interrupt=True, timeout=AnalyticFunction( 2000, "regression_arg(0) + regression_arg(1) * parameter(length)", ["length", "txpower"], regression_args=[500, 16], ), ) trace = [["init"], ["setTxPower", 10], ["send", "foo", 3]] expected_energy = (500000 + 10000 + (3 + 5 * 3) + (1000 + 2 * 10) * (500 + 16 * 3)) expected_duration = 50000 + 120 + 10 + (500 + 16 * 3) result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": 3})
def test_simulation_arg_function(self): pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[3.0, 5], num_args=2, ), duration=AnalyticFunction( 10, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[48, 8], num_args=2, ), ) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10], ["send", "foo", 3]] expected_energy = 500000 + 10000 + (3 + 5 * 3) + (2000 * 100) expected_duration = 50000 + 120 + (48 + 8 * 3) + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None}) pta = PTA(parameters=["txpower", "length"]) pta.add_state("IDLE", power=5) pta.add_state("TX", power=100) pta.add_transition("UNINITIALIZED", "IDLE", "init", energy=500000, duration=50000) pta.add_transition( "IDLE", "IDLE", "setTxPower", energy=10000, duration=120, param_update_function=lambda param, arg: { **param, "txpower": arg[0] }, ) pta.add_transition( "IDLE", "TX", "send", energy=AnalyticFunction( 3, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[3, 5], num_args=2, ), duration=AnalyticFunction( 10, "regression_arg(0) + regression_arg(1) * function_arg(1)", ["txpower", "length"], regression_args=[48, 8], num_args=2, ), ) pta.add_transition("TX", "IDLE", "txComplete", timeout=2000, is_interrupt=True) trace = [["init"], ["setTxPower", 10], ["send", "foobar", 6]] expected_energy = 500000 + 10000 + (3 + 5 * 6) + (2000 * 100) expected_duration = 50000 + 120 + (48 + 8 * 6) + 2000 result = pta.simulate(trace) self.assertAlmostEqual(result.energy, expected_energy * 1e-12, places=12) self.assertAlmostEqual(result.duration, expected_duration * 1e-6, places=6) self.assertEqual(result.end_state.name, "IDLE") self.assertEqual(result.parameters, {"txpower": 10, "length": None})