Exemple #1
0
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)
        obstacles = geom_to_linear_obstacles(self.field.obstacles)

        # Add new spawns to the field for the leaders
        self.field.spawns.extend([
            rectangle(25, 45, 10, 10),
            rectangle(80, 65, 10, 10),
            rectangle(75, 35, 10, 10),
            rectangle(35, 5, 10, 10),
        ])

        for i in range(self.size_leaders):
            group_leader = AgentGroup(
                agent_type=self.agent_type,
                size=1,
                attributes=self.attributes(target=i, is_follower=False))

            agents.add_non_overlapping_group(
                group_leader,
                position_gen=self.field.sample_spawn(i + 1),
                obstacles=obstacles)

        group_herding = AgentGroup(
            agent_type=self.agent_type,
            size=self.size_herding,
            attributes=self.attributes(target=NO_TARGET, is_follower=True))

        agents.add_non_overlapping_group(
            group_herding,
            position_gen=self.field.sample_spawn(0),
            obstacles=obstacles)

        return agents
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        group1 = AgentGroup(size=self.size // 2,
                            agent_type=self.agent_type,
                            attributes=self.attributes1)
        group2 = AgentGroup(size=self.size // 2,
                            agent_type=self.agent_type,
                            attributes=self.attributes2)

        agents.add_non_overlapping_group(
            group=group1, position_gen=self.field.sample_spawn(0))
        agents.add_non_overlapping_group(
            group=group2, position_gen=self.field.sample_spawn(1))

        return agents
def agents_three_circle(size=10):
    agents = Agents(agent_type=ThreeCircle)
    group = AgentGroup(agent_type=ThreeCircle,
                       size=size,
                       attributes=attributes)
    agents.add_non_overlapping_group(
        group, position_gen=lambda: np.random.uniform(-10.0, 10.0, 2))
    return agents
Exemple #4
0
def test_agent_group(agent_type, attributes):
    group = AgentGroup(size=SIZE, agent_type=agent_type, attributes=attributes)

    assert isinstance(group.size, int)
    assert issubclass(group.agent_type, AgentType)
    assert isinstance(group.attributes, (Collection, Generator, Callable))

    assert isinstance(group.members, list)
    assert len(group.members) == SIZE
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        group_leader = AgentGroup(agent_type=self.agent_type,
                                  size=self.size_leaders,
                                  attributes=self.attributes(is_leader=True))

        agents.add_non_overlapping_group(
            group_leader, position_gen=self.field.sample_spawn(0))

        group_herding = AgentGroup(agent_type=self.agent_type,
                                   size=self.size_herding,
                                   attributes=self.attributes(is_leader=False))

        agents.add_non_overlapping_group(
            group_herding, position_gen=self.field.sample_spawn(0))

        return agents
Exemple #6
0
def test_agent_agent_block_list(benchmark, size, agent_type, algorithm):
    # Grow the area with size. Keeps agent density constant.
    area_size = np.sqrt(2 * size)
    agents = Agents(agent_type=agent_type)
    group = AgentGroup(agent_type=agent_type, size=size, attributes=attributes)
    agents.add_non_overlapping_group(
        group,
        position_gen=lambda: np.random.uniform(-area_size, area_size, 2))
    benchmark(agent_agent_block_list, agents.array)
    assert True
Exemple #7
0
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        group_active = AgentGroup(
            agent_type=self.agent_type,
            size=self.size_leaders,
            attributes=self.attributes(has_target=True, is_follower=False))

        group_herding = AgentGroup(
            agent_type=self.agent_type,
            size=self.size_herding,
            attributes=self.attributes(has_target=False, is_follower=True))

        for group in (group_active, group_herding):
            agents.add_non_overlapping_group(
                group,
                position_gen=self.field.sample_spawn(0),
                obstacles=geom_to_linear_obstacles(self.field.obstacles))

        return agents
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        group_leader = AgentGroup(agent_type=self.agent_type,
                                  size=self.size_leaders,
                                  attributes=self.attributes(is_leader=True))

        agents.add_non_overlapping_group(
            group_leader,
            position_gen=lambda: np.array((self.horizontal_ratio * self.width,
                                           self.vertical_ratio * self.height)))

        group_herding = AgentGroup(agent_type=self.agent_type,
                                   size=self.size_herding,
                                   attributes=self.attributes(is_leader=False))

        agents.add_non_overlapping_group(
            group_herding, position_gen=self.field.sample_spawn(0))

        return agents
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        group = AgentGroup(agent_type=self.agent_type,
                           size=self.size,
                           attributes=self.attributes)

        agents.add_non_overlapping_group(
            group, position_gen=self.field.sample_spawn(0))

        return agents
    def _observe_agents(self, change):
        if self.agent_type is not None:
            agents = Agents(agent_type=self.agent_type)

            group = AgentGroup(agent_type=self.agent_type,
                               size=1,
                               attributes=partial(self.attributes,
                                                  orientation=0.0))

            agents.add_non_overlapping_group(
                group, position_gen=lambda: self.start_pos1)

            group = AgentGroup(agent_type=self.agent_type,
                               size=1,
                               attributes=partial(self.attributes,
                                                  orientation=np.pi))

            agents.add_non_overlapping_group(
                group, position_gen=lambda: self.start_pos2)

            self.agents = agents
    def _observe_agents(self, change):
        if self.agent_type is not None:
            agents = Agents(agent_type=self.agent_type)

            group = AgentGroup(agent_type=self.agent_type,
                               size=self.size,
                               attributes=self.attributes)

            agents.add_non_overlapping_group(group,
                                             position_gen=lambda: np.array(
                                                 (0, 0)))

            self.agents = agents
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        for spawn, name in enumerate(
            ['finlandiahall', 'foyer', 'helsinkihall']):
            group = AgentGroup(agent_type=self.agent_type,
                               size=getattr(self, f'size_{name}'),
                               attributes=self.attributes(has_target=True,
                                                          is_follower=False))

            agents.add_non_overlapping_group(
                group,
                position_gen=self.field.sample_spawn(spawn),
                obstacles=geom_to_linear_obstacles(self.field.obstacles))

        return agents
Exemple #13
0
def test_agents(agent_type, attributes):
    agents = Agents(agent_type=agent_type)
    group = AgentGroup(size=SIZE, agent_type=agent_type, attributes=attributes)
    agents.add_non_overlapping_group(
        group=group, position_gen=lambda: np.random.uniform(XMIN, XMAX, 2))
    assert True
Exemple #14
0
    def _default_agents(self):
        agents = Agents(agent_type=self.agent_type)

        # Generate iterators for group of leaders.
        #target_exits = [0,1,0]
        #cells = [913,695,652]
        target_exits = []
        cells = []
        n_guides = len(target_exits)

        speed_left = "fast"
        speed_lower = "fast"
        speed_right = "fast"
        speed_upper = "fast"

        # Exiting agents in left spawn
        group_follower_spawn_left = AgentGroup(
            agent_type=self.agent_type,
            size=getattr(self, 'size_spawn_left'),
            attributes=self.attributes(familiar=2, has_target=True, is_follower=True))

        agents.add_non_overlapping_group(
            speed_left,
            "spawn_left",
            group_follower_spawn_left,
            position_gen=False,
            position_iter=iter([]),
            spawn=0,
            obstacles=geom_to_linear_obstacles(self.field.obstacles))

        # Exiting agents in lower spawn
        group_follower_spawn_lower = AgentGroup(
            agent_type=self.agent_type,
            size=getattr(self, 'size_spawn_lower'),
            attributes=self.attributes(familiar=3, has_target=True, is_follower=True))

        agents.add_non_overlapping_group(
            speed_lower,
            "spawn_lower",
            group_follower_spawn_lower,
            position_gen=False,
            position_iter=iter([]),
            spawn=0,
            obstacles=geom_to_linear_obstacles(self.field.obstacles))

        # Exiting agents in right spawn
        group_follower_spawn_right = AgentGroup(
            agent_type=self.agent_type,
            size=getattr(self, 'size_spawn_right'),
            attributes=self.attributes(familiar=0, has_target=True, is_follower=True))

        agents.add_non_overlapping_group(
            speed_right,
            "spawn_right",
            group_follower_spawn_right,
            position_gen=False,
            position_iter=iter([]),
            spawn=2,
            obstacles=geom_to_linear_obstacles(self.field.obstacles))

        # Exiting agents in upper spawn
        group_follower_spawn_upper = AgentGroup(
            agent_type=self.agent_type,
            size=getattr(self, 'size_spawn_upper'),
            attributes=self.attributes(familiar=1, has_target=True, is_follower=True))

        agents.add_non_overlapping_group(
            speed_upper,
            "spawn_upper",
            group_follower_spawn_upper,
            position_gen=False,
            position_iter=iter([]),
            spawn=3,
            obstacles=geom_to_linear_obstacles(self.field.obstacles))

        if n_guides != 0:
            init_pos = self.generate_leader_pos(cells, n_guides)
            print(init_pos)
            # init_pos = [[8, 6]]
            target_exits = iter(target_exits)
            init_pos = iter(init_pos)

            # Guides in Variance Dilemma
            group_leader = AgentGroup(
                agent_type=self.agent_type,
                size=n_guides,
                attributes=self.attributes_leader(target_iter=target_exits, has_target=True, is_follower=False))

            agents.add_non_overlapping_group(
                "group_leader",
                group_leader,
                position_gen=True,
                position_iter=init_pos,
                spawn=0,
                obstacles=geom_to_linear_obstacles(self.field.obstacles))

        return agents
Exemple #15
0
def run(individual, n_leaders, scenario):

    # Import simulation
    simulation = ComplexFloor()

    # Import Hallway floor field
    field = simulation.field

    # Generate iterators for group of guides.
    target_exits = []  # Target exit for the guide
    cells = []

    if n_leaders > 0:
        for i in range(n_leaders):
            target_exits.append(individual[i][0])
            cells.append(individual[i][1])

    # Save the target_exits, in another list for later use
    exits = target_exits

    # Number of followers
    size_spawn_left = 50
    size_spawn_lower = 50
    size_spawn_right = 50
    size_spawn_upper = 50

    # Set the familiar exit for the followers based on the scenario.
    if scenario == 0:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    # Absolute worst
    elif scenario == 1:
        familiar_left = 2
        familiar_lower = 3
        familiar_right = 0
        familiar_upper = 1
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    # Quite bad
    elif scenario == 2:
        familiar_left = 2
        familiar_lower = 3
        familiar_right = 0
        familiar_upper = 1
        speed_left = "fast"
        speed_lower = "fast"
        speed_right = "fast"
        speed_upper = "fast"
    # Not so bad
    elif scenario == 3:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "fast"
        speed_lower = "fast"
        speed_right = "fast"
        speed_upper = "fast"
    elif scenario == 4:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 5:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 6:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 7:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 8:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 9:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 10:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 11:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 12:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 13:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    elif scenario == 14:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"
    else:
        familiar_left = 0
        familiar_lower = 1
        familiar_right = 2
        familiar_upper = 3
        speed_left = "slow"
        speed_lower = "slow"
        speed_right = "slow"
        speed_upper = "slow"

    # Exiting agents in left spawn
    group_follower_spawn_left = AgentGroup(agent_type=Circular,
                                           size=size_spawn_left,
                                           attributes=attributes(
                                               simulation,
                                               familiar=familiar_left,
                                               has_target=True,
                                               is_follower=True))

    simulation.agents.add_non_overlapping_group(
        speed_left,
        "spawn_left",
        group_follower_spawn_left,
        position_gen=False,
        position_iter=iter([]),
        spawn=0,
        obstacles=geom_to_linear_obstacles(field.obstacles))

    # Exiting agents in lower spawn
    group_follower_spawn_lower = AgentGroup(agent_type=Circular,
                                            size=size_spawn_lower,
                                            attributes=attributes(
                                                simulation,
                                                familiar=familiar_lower,
                                                has_target=True,
                                                is_follower=True))

    simulation.agents.add_non_overlapping_group(
        speed_lower,
        "spawn_lower",
        group_follower_spawn_lower,
        position_gen=False,
        position_iter=iter([]),
        spawn=1,
        obstacles=geom_to_linear_obstacles(field.obstacles))

    # Exiting agents in right spawn
    group_follower_spawn_right = AgentGroup(agent_type=Circular,
                                            size=size_spawn_right,
                                            attributes=attributes(
                                                simulation,
                                                familiar=familiar_right,
                                                has_target=True,
                                                is_follower=True))

    simulation.agents.add_non_overlapping_group(
        speed_right,
        "spawn_right",
        group_follower_spawn_right,
        position_gen=False,
        position_iter=iter([]),
        spawn=2,
        obstacles=geom_to_linear_obstacles(field.obstacles))

    # Exiting agents in upper spawn
    group_follower_spawn_upper = AgentGroup(agent_type=Circular,
                                            size=size_spawn_upper,
                                            attributes=attributes(
                                                simulation,
                                                familiar=familiar_upper,
                                                has_target=True,
                                                is_follower=True))

    simulation.agents.add_non_overlapping_group(
        speed_upper,
        "spawn_upper",
        group_follower_spawn_upper,
        position_gen=False,
        position_iter=iter([]),
        spawn=3,
        obstacles=geom_to_linear_obstacles(field.obstacles))

    if n_leaders == 0:
        # Check if the solution has already been evaluated, if it has print the penalty and total evacuation time and return
        bank_evactime = solutionbank(cells, target_exits, n_leaders, scenario)
        if bank_evactime != 0:
            print(bank_evactime)
            return

    if n_leaders > 0:
        # generate_leader_pos() should check that guides are not spawned in unfeasible positions
        init_pos = generate_leader_pos(simulation, cells, n_leaders)
        print(init_pos)
        target_exits = iter(target_exits)
        init_pos = iter(init_pos)

        # Check if the solution has already been evaluated, if it has print the penalty and total evacuation time and return
        bank_evactime = solutionbank(cells, target_exits, n_leaders, scenario)
        if bank_evactime != 0:
            print(bank_evactime)
            return

        # Add guides.
        # NB! If there are multiple guides, the function that is set to create the guides should check that the guides do
        # not overlap each other.
        group_leader = AgentGroup(agent_type=Circular,
                                  size=n_leaders,
                                  attributes=attributes_leader(
                                      simulation,
                                      target_iter=target_exits,
                                      has_target=True,
                                      is_follower=False))

        # If it is not taken care before hand that leaders can't overlap, the function will terminate here.
        simulation.agents.add_non_overlapping_group(
            "dummy_speed",
            "group_leader",
            group_leader,
            position_gen=True,
            position_iter=init_pos,
            spawn=0,
            obstacles=geom_to_linear_obstacles(field.obstacles))

    # We do not need to set the seed number, since we use the deterministic social force model
    #np.random.seed(seed)
    simulation.update()
    simulation.run()

    print(simulation.data['time_tot'])

    # Write the solution to the solution bank
    if n_leaders == 0:
        banksolution = "{}{}{}{}".format(simulation.data['time_tot'], ' ',
                                         scenario, '\n')
    if n_leaders == 1:
        banksolution = "{}{}{}{}{}{}{}{}".format(simulation.data['time_tot'],
                                                 ' ', scenario, ' ', cells[0],
                                                 ' ', exits[0], '\n')
    if n_leaders == 2:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], '\n')
    if n_leaders == 3:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], '\n')
    if n_leaders == 4:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], '\n')
    if n_leaders == 5:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], '\n')
    if n_leaders == 6:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], ' ', cells[5], ' ', exits[5], '\n')
    if n_leaders == 7:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], ' ', cells[5], ' ', exits[5], ' ', cells[6], ' ',
            exits[6], '\n')
    if n_leaders == 8:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], ' ', cells[5], ' ', exits[5], ' ', cells[6], ' ',
            exits[6], ' ', cells[7], ' ', exits[7], '\n')
    if n_leaders == 9:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], ' ', cells[5], ' ', exits[5], ' ', cells[6], ' ',
            exits[6], ' ', cells[7], ' ', exits[7], ' ', cells[8], ' ',
            exits[8], '\n')
    if n_leaders == 10:
        banksolution = "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(
            simulation.data['time_tot'], ' ', scenario, ' ', cells[0], ' ',
            exits[0], ' ', cells[1], ' ', exits[1], ' ', cells[2], ' ',
            exits[2], ' ', cells[3], ' ', exits[3], ' ', cells[4], ' ',
            exits[4], ' ', cells[5], ' ', exits[5], ' ', cells[6], ' ',
            exits[6], ' ', cells[7], ' ', exits[7], ' ', cells[8], ' ',
            exits[8], ' ', cells[9], ' ', exits[9], '\n')

    if os.path.isfile("complex/bank_complex.out"):
        bankfile = open("complex/bank_complex.out", "a")
        bankfile.write(banksolution)
        bankfile.close()
    else:
        bankfile = open("complex/bank_complex.out", "w")
        bankfile.write(banksolution)
        bankfile.close()

    return