コード例 #1
0
    def setup(self):
        Agent.id = 0
        Minority_Game_World.steps_to_win = SimEngine.get_gui_value(
            STEPS_TO_WIN)
        # Adjust how far one step is based on number of steps needed to win
        Minority_Game_World.one_step = (
            gui.PATCH_COLS -
            2) * gui.BLOCK_SPACING() / Minority_Game_World.steps_to_win
        # For longer/shorter races, speed up/slow down frames/second
        gui.set_fps(round(6 * Minority_Game_World.steps_to_win / 50))

        # self.done will be True if this a repeat game with the same agents.
        if self.done:
            self.reset_agents()
            return

        # This is the normal setup.
        Minority_Game_World.nbr_agents = SimEngine.get_gui_value(NBR_AGENTS)
        if Minority_Game_World.nbr_agents % 2 == 0:
            Minority_Game_World.nbr_agents += (
                1 if Minority_Game_World.nbr_agents <
                gui.WINDOW[NBR_AGENTS].Range[1] else (-1))
            gui.WINDOW[NBR_AGENTS].update(value=Minority_Game_World.nbr_agents)
        Minority_Game_World.random_agent_ids = {
            0, Minority_Game_World.nbr_agents - 1
        }

        # Generate a random initial history
        self.history_length = SimEngine.get_gui_value(HISTORY_LENGTH)
        self.history = [choice([0, 1]) for _ in range(self.history_length)]

        self.generate_the_agents()
コード例 #2
0
 def cohere(self, flockmates):
     max_cohere_turn = SimEngine.get_gui_value('max-cohere-turn')
     avg_heading_toward_flockmates = self.average_heading_toward_flockmates(
         flockmates)
     amount_to_turn = utils.turn_toward_amount(
         self.heading, avg_heading_toward_flockmates, max_cohere_turn)
     self.turn_right(amount_to_turn)
コード例 #3
0
 def align(self, flockmates):
     max_align_turn = SimEngine.get_gui_value('max-align-turn')
     average_flockmate_heading = self.average_flockmate_heading(flockmates)
     amount_to_turn = utils.turn_toward_amount(self.heading,
                                               average_flockmate_heading,
                                               max_align_turn)
     self.turn_right(amount_to_turn)
コード例 #4
0
 def setup(self):
     nbr_agents = SimEngine.get_gui_value('nbr_agents')
     self.create_ordered_agents(nbr_agents)
     self.reference_agent = list(World.agents)[0]
     twitchy_turn = randint(0, 360)
     for agent in World.agents:
         agent.speed = 1
         agent.forward(100)
         self.current_figure = SimEngine.get_gui_value('figure')
         self.breathing_phase = 'inhale'
         if self.current_figure in ['clockwise', 'counter-clockwise']:
             agent.turn_right(90 if self.current_figure ==
                              'clockwise' else -90)
         elif self.current_figure == 'twitchy':
             agent.turn_right(twitchy_turn)
         agent.cached_heading = agent.heading
コード例 #5
0
ファイル: on_off.py プロジェクト: SamSunshine/PyLogo
 def get_color_and_update_button(button, default_color_string):
     key = button.get_text()
     color_string = SimEngine.get_gui_value(key)
     if color_string in {'None', '', None}:
         color_string = default_color_string
     button.update(button_color=(color_string, color_string))
     color = Color(color_string)
     return color
コード例 #6
0
    def flock(self):
        # NetLogo allows one to specify the units within the Gui widget.
        # Here we do it explicitly by multiplying by BLOCK_SPACING().
        vision_limit_in_pixels = SimEngine.get_gui_value(
            'vision') * BLOCK_SPACING()
        flockmates = self.agents_in_radius(vision_limit_in_pixels)
        if len(flockmates) > 0:
            nearest_neighbor = min(
                flockmates, key=lambda flockmate: self.distance_to(flockmate))

            min_separation = SimEngine.get_gui_value(
                'minimum separation') * BLOCK_SPACING()
            if self.distance_to(nearest_neighbor) < min_separation:
                self.separate(nearest_neighbor)
            else:
                self.align(flockmates)
                self.cohere(flockmates)
コード例 #7
0
    def setup(self):
        nbr_agents = int(SimEngine.get_gui_value('nbr_agents'))
        for i in range(nbr_agents):
            # Adds itself to self.agents and to its patch's list of Agents.
            agent = self.agent_class(color=Color('red'))
            agent.set_velocity(Velocity((uniform(-2, 2), uniform(-2, 2))))

        for patch in self.patches:
            patch.update_collision_color(World.agents)
コード例 #8
0
 def step(self):
     # World.agents is the set of agents kept by the world
     for agent in World.agents:
         # agent.flock() resets agent's heading. Agent doesn't move.
         agent.flock()
         # Here's where the agent actually moves.
         # The move depends on the speed and the heading.
         speed = SimEngine.get_gui_value('speed')
         agent.forward(speed)
コード例 #9
0
 def step(self):
     # For simplicity, start each step by having all agents face the center.
     for agent in World.agents:
         agent.face_xy(center_pixel())
         # Emergency action is going beyond the inner and outer limits.
     if self.take_emergency_action():
         return
     self.current_figure = SimEngine.get_gui_value('figure')
     self.do_a_step()
コード例 #10
0
ファイル: starburst.py プロジェクト: SamSunshine/PyLogo
    def setup(self):
        nbr_agents = SimEngine.get_gui_value('nbr_agents')
        for _ in range(nbr_agents):
            # When created, a agent adds itself to self.agents and to its patch's list of Agents.
            # self.agent_class(scale=1)
            Agent(scale=1)

        initial_velocities = cycle([Velocity((-1, -1)), Velocity((-1, 1)),
                                    Velocity((0, 0)),
                                    Velocity((1, -1)), Velocity((1, 1))])
        for (agent, vel) in zip(World.agents, initial_velocities):
            agent.set_velocity(vel)
コード例 #11
0
    def setup(self):
        density = SimEngine.get_gui_value('density')
        pct_similar_wanted = SimEngine.get_gui_value('% similar wanted')
        self.color_items = self.select_the_colors()
        (color_a, color_b) = [color_item[1] for color_item in self.color_items]
        print(f'\n\t The colors: {self.colors_string()}')
        self.empty_patches = set()
        # print('About to create agents')
        for patch in self.patches:   # .flat:.flat:
            patch.set_color(self.patch_color)
            patch.neighbors_8()  # Calling neighbors_8 stores it as a cached value

            # Create the Agents. The density is approximate.
            if randint(0, 100) <= density:
                agent = SegregationAgent(color=choice([color_a, color_b]))
                agent.pct_similar_wanted = pct_similar_wanted
                agent.move_to_patch(patch)
            else:
                self.empty_patches.add(patch)
        # print('Finished creating agents')
        self.update_all()
コード例 #12
0
 def generate_all_strategies(self):
     # Generate enough strategies for all agents.
     strategy_length = 2**self.history_length
     strategies_per_agent = SimEngine.get_gui_value(STRATEGIES_PER_AGENT)
     strategies = set()
     # Why a while loop rather than a for loop?
     # Why is strategies a set rather than a list?
     # Note strategies is made into a list after the loop.
     while len(strategies
               ) < Minority_Game_World.nbr_agents * strategies_per_agent:
         strategies.add(self.generate_a_strategy(strategy_length))
     strategies = list(strategies)
     return (strategies, strategies_per_agent)
コード例 #13
0
    def setup(self):
        Agent.id = 0
        Minority_Game_World.steps_to_win = SimEngine.get_gui_value(
            STEPS_TO_WIN)
        # Adjust how far one step is based on number of steps needed to win
        Minority_Game_World.one_step = (
            gui.PATCH_COLS -
            2) * gui.BLOCK_SPACING() / Minority_Game_World.steps_to_win
        # For longer/shorter races, speed up/slow down frames/second
        gui.set_fps(round(6 * Minority_Game_World.steps_to_win / 50))

        # self.done will be True if this a repeat game with the same agents.
        if self.done:
            self.reset_agents()
            return

        # This is the normal setup.
        Minority_Game_World.nbr_agents = SimEngine.get_gui_value(NBR_AGENTS)
        Minority_Game_World.nbr_last_strat_agents = SimEngine.get_gui_value(
            PREV_STRAT_AGENTS)
        Minority_Game_World.nbr_spying_strat_agents = SimEngine.get_gui_value(
            SPY_STRAT_AGENTS)

        if Minority_Game_World.nbr_agents % 2 == 0:
            Minority_Game_World.nbr_agents += (
                1 if Minority_Game_World.nbr_agents <
                gui.WINDOW[NBR_AGENTS].Range[1] else (-1))
            gui.WINDOW[NBR_AGENTS].update(value=Minority_Game_World.nbr_agents)

        ##agent creation stuff

        #make the list of indexes of the various strategies
        index = 1

        #makes index from 1 - number of agents
        if Minority_Game_World.last_strat_ids is None:
            Minority_Game_World.last_strat_ids = []

        if Minority_Game_World.spying_strat_ids is None:
            Minority_Game_World.spying_strat_ids = []

        # Minority_Game_World.last_strat_ids = [] if Minority_Game_World.last_strat_ids is None else None
        # Minority_Game_World.spying_strat_ids = [] if Minority_Game_World.spying_strat_ids is None else None

        while len(Minority_Game_World.last_strat_ids
                  ) < Minority_Game_World.nbr_last_strat_agents:
            Minority_Game_World.last_strat_ids.append(index)
            index += 1

        #makes list of indexes from the last one in last strat to total number in spying strat
        while len(Minority_Game_World.spying_strat_ids
                  ) < Minority_Game_World.nbr_spying_strat_agents:
            Minority_Game_World.spying_strat_ids.append(index)
            index += 1

        # Minority_Game_World.last_strat_ids = range(1, Minority_Game_World.nbr_last_strat_agents + 1)

        # get number of agents to be using the Spying method and add the ID to this array
        # Minority_Game_World.spying_strat_ids = {Minority_Game_World.nbr_last_strat_agents,
        #                                         Minority_Game_World.nbr_spying_strat_agents - 1}
        # Make first and last Agents into random agents
        Minority_Game_World.random_agent_ids = {
            0, Minority_Game_World.nbr_agents - 1
        }
        # [prev strat, spying strat, rest]

        # Generate a random initial history
        self.history_length = SimEngine.get_gui_value(HISTORY_LENGTH)
        self.history = [choice([0, 1]) for _ in range(self.history_length)]

        self.generate_the_agents()
コード例 #14
0
 def setup(self):
     super().setup()
     density = SimEngine.get_gui_value('density')
     for patch in self.patches:
         is_alive = randint(0, 100) < density
         patch.set_alive_or_dead(is_alive)
コード例 #15
0
 def setup(self):
     nbr_agents = SimEngine.get_gui_value('population')
     self.create_agents(nbr_agents)
コード例 #16
0
 def separate(self, nearest_neighbor):
     max_separate_turn = SimEngine.get_gui_value('max-sep-turn')
     amount_to_turn = utils.turn_away_amount(self.heading,
                                             nearest_neighbor.heading,
                                             max_separate_turn)
     self.turn_right(amount_to_turn)