def test_grid_remove(grid_size): from adjsim import core, analysis, decision class TestAgent(core.SpatialAgent): def __init__(self, pos): super().__init__(pos=pos) test_sim = core.Simulation() test_sim.agents.add(TestAgent(np.array([0, 0]) * grid_size)) test_sim.agents.add(TestAgent(np.array([0, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([1, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([2, 0]) * grid_size)) agent_to_remove = TestAgent(np.array([-1, 0]) * grid_size) test_sim.agents.add(agent_to_remove) test_sim.indices.grid.initialize(grid_size) test_sim.agents.remove(agent_to_remove) assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 2 common.step_simulate_interpolation(test_sim) assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 2
def test_neighbours_trivial_multiple_inhabitants(grid_size): from adjsim import core, analysis, decision class TestAgent(core.SpatialAgent): def __init__(self, pos): super().__init__(pos=pos) test_sim = core.Simulation() test_sim.agents.add(TestAgent(np.array([0, 0]) * grid_size)) test_sim.agents.add(TestAgent(np.array([0, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([0, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([0, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([1, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([1, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([2, 0]) * grid_size)) test_sim.indices.grid.initialize(grid_size) assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 5 common.step_simulate_interpolation(test_sim) assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 5
def test_invalid_type(): from adjsim import core, utility test_sim = core.Simulation() with pytest.raises(utility.InvalidAgentException): test_sim.agents.add({"I'm not valid!"})
def test_agent_move(): from adjsim import core, utility, decision def move_callback(agent): move_callback.count += 1 move_callback.count = 0 def move(env, source): source.x += 10 source.y += 10 class TestAgent(core.VisualAgent): def __init__(self, x, y): super().__init__(pos=np.array([x, y])) self.decision = decision.RandomSingleCastDecision() self.actions["move"] = move test_sim = core.Simulation() test_sim.callbacks.agent_moved.register(move_callback) test_sim.agents.add(TestAgent(0, 0)) test_sim.agents.add(TestAgent(0, -10)) common.step_simulate_interpolation(test_sim) assert move_callback.count == 4*common.INTERPOLATION_NUM_TIMESTEP # 2 agents * 2 updates per move # test core ended
def test_invalid_type(): from adjsim import core, analysis, utility test_sim = core.Simulation() with pytest.raises(utility.TrackerException): test_sim.trackers["count"] = lambda x: 0
def test_exponential_agent_count(): from adjsim import core, analysis, decision from matplotlib import pyplot def increment_agents(env, source): env.agents.add(TestAgent()) class TestAgent(core.Agent): def __init__(self): super().__init__() self.actions["increment"] = increment_agents self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.agents.add(TestAgent()) test_sim.trackers["count"] = analysis.AgentCountTracker() common.step_simulate_interpolation(test_sim) assert test_sim.trackers["count"].data == [ 2**i for i in range(common.INTERPOLATION_NUM_TIMESTEP + 1) ] test_sim.trackers["count"].plot(block=False) pyplot.close()
def test_inhabitants_single(grid_size): from adjsim import core, analysis, decision class TestAgent(core.SpatialAgent): def __init__(self, pos): super().__init__(pos=pos) test_sim = core.Simulation() agent_00 = TestAgent(np.array([0, 0]) * grid_size) test_sim.agents.add(agent_00) agent_01 = TestAgent(np.array([0, 1]) * grid_size) test_sim.agents.add(agent_01) test_sim.indices.grid.initialize(grid_size) assert test_sim.indices.grid.get_inhabitants(np.array([0, 0]) * grid_size) == [agent_00] assert test_sim.indices.grid.get_inhabitants(np.array([0, 1]) * grid_size) == [agent_01] assert test_sim.indices.grid.get_inhabitants(np.array([1, 1]) * grid_size) == [] common.step_simulate_interpolation(test_sim) assert test_sim.indices.grid.get_inhabitants(np.array([0, 0]) * grid_size) == [agent_00] assert test_sim.indices.grid.get_inhabitants(np.array([0, 1]) * grid_size) == [agent_01] assert test_sim.indices.grid.get_inhabitants(np.array([1, 1]) * grid_size) == []
def test_invalid_end_condition(): from adjsim import core, utility test_sim = core.Simulation() test_sim.end_condition = True with pytest.raises(utility.EndConditionException): common.step_simulate_interpolation(test_sim)
def test_trivial(): from adjsim import core test_sim = core.Simulation() common.step_simulate_interpolation(test_sim) assert test_sim.time == common.INTERPOLATION_NUM_TIMESTEP
def test_valid_end_condition(): from adjsim import core test_sim = core.Simulation() test_sim.end_condition = lambda env: env.time == common.INTERPOLATION_NUM_TIMESTEP - 2 common.step_simulate_interpolation(test_sim) assert test_sim.time == common.INTERPOLATION_NUM_TIMESTEP - 1
def test_grid_move(grid_size): from adjsim import core, analysis, decision def move(simulation, source): source.y += grid_size class TestAgent(core.SpatialAgent): def __init__(self, pos): super().__init__(pos=pos) self.decision = decision.RandomSingleCastDecision() self.actions["move"] = move test_sim = core.Simulation() test_sim.agents.add(TestAgent(np.array([0, 0]) * grid_size)) test_sim.agents.add(TestAgent(np.array([1, 0]) * grid_size)) test_sim.agents.add(TestAgent(np.array([1, 1]) * grid_size)) test_sim.agents.add(TestAgent(np.array([2, 0]) * grid_size)) test_sim.indices.grid.initialize(grid_size) test_sim.start() assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 2 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 1]) * grid_size)) == 3 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 2]) * grid_size)) == 1 test_sim.step() assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 2 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 1]) * grid_size)) == 2 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 2]) * grid_size)) == 3 test_sim.step() assert len( test_sim.indices.grid.get_neighbours(np.array([0, 0]) * grid_size)) == 0 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 1]) * grid_size)) == 2 assert len( test_sim.indices.grid.get_neighbours(np.array([0, 2]) * grid_size)) == 2 test_sim.end()
def test_valid(): from adjsim import core, analysis, utility class ValidTracker(analysis.Tracker): def __call__(self, value): pass test_sim = core.Simulation() test_sim.trackers["count"] = ValidTracker() common.step_simulate_interpolation(test_sim)
def test_trivial_agent_count(): from adjsim import core, analysis test_sim = core.Simulation() test_sim.trackers["count"] = analysis.AgentCountTracker() common.step_simulate_interpolation(test_sim) assert test_sim.trackers["count"].data == [ 0 for i in range(common.INTERPOLATION_NUM_TIMESTEP + 1) ]
def test_qlearning_perturbative(mutable_min, mutable_max): from adjsim import core, utility, decision def move(env, source): move.count += 1 source.step_complete = True move.count = 0 def perception(env, source): perception.count += 1 return 1 perception.count = 0 def loss(env, source): loss.count += 1 return 1 loss.count = 0 class TestAgent(core.VisualAgent): def __init__(self, x, y, sim): super().__init__(pos=np.array([x, y])) self.decision = decision.PerturbativeQLearningDecision(perception=perception, loss=loss, simulation=sim, output_file_name=None, input_file_name=None) self.actions["move"] = move self.mutable = decision.DecisionMutableFloat(mutable_min, mutable_max) test_sim = core.Simulation() agent = TestAgent(0, 0, test_sim) test_sim.agents.add(agent) test_sim.simulate(1) # Check proper call counts. assert move.count == 1 assert perception.count == 1 assert loss.count == 2 # Check expected q_table entries. assert agent.decision.q_table[1].loss == 1 assert len(agent.decision.q_table[1].action_premise.iterations) == 1 assert len(agent.decision.q_table[1].action_premise.iterations[0].decision_mutables) == 1 decision_mutable_premise = agent.decision.q_table[1].action_premise.iterations[0].decision_mutables[0] assert decision_mutable_premise.name == "mutable" assert decision_mutable_premise.value < mutable_max and decision_mutable_premise.value > mutable_min assert agent.decision.q_table[1].action_premise.iterations[0].action_name == "move"
def test_invalid_too_few_arguments(): from adjsim import core, analysis, utility class InvalidTracker(analysis.Tracker): def __call__(self): pass test_sim = core.Simulation() test_sim.trackers["count"] = InvalidTracker() with pytest.raises(utility.TrackerException): common.step_simulate_interpolation(test_sim)
def test_grid_uninitialized(): from adjsim import core, analysis, decision, utility test_sim = core.Simulation() with pytest.raises(utility.IndexInitializationException): test_sim.indices.grid.get_inhabitants(np.array([0, 0])) with pytest.raises(utility.IndexInitializationException): test_sim.indices.grid.get_neighbour_coordinates(np.array([0, 0])) with pytest.raises(utility.IndexInitializationException): test_sim.indices.grid.get_neighbours(np.array([0, 0]))
def test_trivial(): from adjsim import core, analysis, utility, decision action = lambda env, source: 0 agent = core.Agent() agent.decision = decision.RandomSingleCastDecision() agent.actions["trivial"] = action test_sim = core.Simulation() test_sim.agents.add(agent) common.step_simulate_interpolation(test_sim)
def test_invalid_decision(): from adjsim import core, analysis, utility action = lambda env, source: 0 agent = core.Agent() agent.decision = None agent.actions["trivial"] = action test_sim = core.Simulation() test_sim.agents.add(agent) with pytest.raises(utility.DecisionException): common.step_simulate_interpolation(test_sim)
def test_invalid_too_many_arguments(): from adjsim import core, analysis, utility, decision action = lambda env, source, kek: 0 agent = core.Agent() agent.decision = decision.RandomSingleCastDecision() agent.actions["trivial"] = action test_sim = core.Simulation() test_sim.agents.add(agent) with pytest.raises(utility.DecisionException): common.step_simulate_interpolation(test_sim)
def test_grid_invalid_type(): from adjsim import core, analysis, decision, utility test_sim = core.Simulation() test_sim.indices.grid.initialize(1) invalid_types = [(0, 0), 5, object, [0, 0], {0: 0}] for invalid_type in invalid_types: with pytest.raises(TypeError): test_sim.indices.grid.get_inhabitants(invalid_type) with pytest.raises(TypeError): test_sim.indices.grid.get_neighbour_coordinates(invalid_type) with pytest.raises(TypeError): test_sim.indices.grid.get_neighbours(invalid_type)
def test_agent_add(): from adjsim import core, analysis, decision def increment_agents(env, source): env.agents.add(TestAgent()) class TestAgent(core.Agent): def __init__(self): super().__init__() self.actions["increment"] = increment_agents self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.agents.add(TestAgent()) common.step_simulate_interpolation(test_sim) assert len(test_sim.agents) == 2**common.INTERPOLATION_NUM_TIMESTEP
def test_agent_remove(): from adjsim import core, analysis, decision def decrement_agents(env, source): env.agents.remove(source) class TestAgent(core.Agent): def __init__(self): super().__init__() self.actions["decrement"] = decrement_agents self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() for _ in range(20): test_sim.agents.add(TestAgent()) common.step_simulate_interpolation(test_sim) assert len(test_sim.agents) == 0
def test_timestep_count(): from adjsim import core, analysis, decision def increment_action(env, source): source.count += 1 class TestAgent(core.Agent): def __init__(self): super().__init__() self.actions["increment"] = increment_action self.count = 0 agent = TestAgent() agent.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.agents.add(agent) common.step_simulate_interpolation(test_sim) assert agent.count == common.INTERPOLATION_NUM_TIMESTEP
def test_visual_order(): from adjsim import core, utility, decision order_log = [] orders = [i for i in range(5)] def log(env, source): order_log.append(source.order) source.order = orders[source.index] def shuffle(env, source): random.shuffle(orders) class TestAgent(core.Agent): def __init__(self, order): super().__init__() self.actions["log"] = log self.order = order self.index = order self.decision = decision.RandomSingleCastDecision() class Shuffler(core.Agent): def __init__(self): super().__init__() self.actions["shuffle"] = shuffle self.order = 10 self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.agents.add(Shuffler()) for i in orders: test_sim.agents.add(TestAgent(i)) common.step_simulate_interpolation(test_sim) assert order_log == [i for i in range(5)] * common.INTERPOLATION_NUM_TIMESTEP
def test_agent_add(): from adjsim import core, analysis, decision def addition_callback(agent): addition_callback.count += 1 addition_callback.count = 0 def increment_agents(env, source): env.agents.add(TestAgent()) class TestAgent(core.Agent): def __init__(self): super().__init__() self.actions["increment"] = increment_agents self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.callbacks.agent_added.register(addition_callback) test_sim.agents.add(TestAgent()) test_sim.trackers["count"] = analysis.AgentTypeCountTracker() common.step_simulate_interpolation(test_sim) assert addition_callback.count == 2**common.INTERPOLATION_NUM_TIMESTEP
def test_trivial(): from adjsim import core, utility, decision class TrivialAgent(core.Agent): def __init__(self): super().__init__() self.decision = decision.RandomSingleCastDecision() class TrivialSpatialAgent(core.SpatialAgent): def __init__(self): super().__init__() self.decision = decision.RandomSingleCastDecision() class TrivialVisualAgent(core.VisualAgent): def __init__(self): super().__init__() self.decision = decision.RandomSingleCastDecision() test_sim = core.Simulation() test_sim.agents.add(TrivialAgent()) test_sim.agents.add(TrivialSpatialAgent()) test_sim.agents.add(TrivialVisualAgent()) common.step_simulate_interpolation(test_sim)