Example #1
0
 def take_emergency_action(self):
     if self.reference_agent.distance_to_xy(center_pixel()) >= 250:
         self.grow_shrink('shrink')
         return True
     elif self.reference_agent.distance_to_xy(center_pixel()) <= 50:
         self.grow_shrink('grow')
         return True
     return False
Example #2
0
    def __init__(self,
                 center_pixel=None,
                 color=None,
                 scale=1.4,
                 shape_name='netlogo_figure'):
        # Can't make this a default value because pairs.CENTER_PIXEL()
        # isn't defined when the default values are compiled
        if center_pixel is None:
            center_pixel = pairs.center_pixel()

        if color is None:
            # Select a color at random from the color_palette
            color = choice(Agent.color_palette)[1]

        super().__init__(center_pixel, color)

        self.scale = scale

        self.shape_name = shape_name
        self.base_image = self.create_base_image()

        self.id = Agent.id
        Agent.id += 1

        World.agents.add(self)
        self.current_patch().add_agent(self)

        self.animation_target = None

        # Agents are created with a random heading and a velocity of 0.
        # In NetLogo, agents do not have a speed or velocity attribute. They have a heading attribute.
        # They move by a given amount (forward(amount)) in the heading direction.
        # After each forward() action, the agent is no longer moving. (But it retains its heading.)
        self.heading = randint(0, 359)
        self.velocity = Velocity.velocity_00
    def build_ring_star_or_wheel_graph(graph_type, ring_node_list):
        # Create a center_node variable if a STAR and WHEEL graph.
        center_node = None
        if graph_type in [STAR, WHEEL]:
            center_node = Graph_Node()
            center_node.move_to_xy(center_pixel())

        # If ring_node_list is empty, do nothing more.
        if not ring_node_list:
            return

        # If ring_node_list contains exactly one node and the graph type is STAR or WHEEL,
        # link the center node to the one ring node and return.
        if len(ring_node_list) == 1:
            if graph_type in [STAR, WHEEL]:
                Link(center_node, ring_node_list[0])
            return

        # Otherwise (2 or more ring nodes), create the outer ring and spokes as appropriate.
        for (node_a, node_b) in zip(ring_node_list,
                                    ring_node_list[1:] + [ring_node_list[0]]):
            if graph_type in [RING, WHEEL]:
                Link(node_a, node_b)

            if graph_type in [STAR, WHEEL]:
                Link(center_node, node_a)

        # Complete the ring if a ring or wheel graph
        if graph_type in [RING, WHEEL]:
            Link(ring_node_list[-1], ring_node_list[0])
Example #4
0
    def __init__(self,
                 center_pixel=None,
                 color=None,
                 scale=1.4,
                 shape=SHAPES['netlogo_figure']):
        # Can't make this a default value because pairs.CENTER_PIXEL() isn't defined
        # when the default values are compiled
        if center_pixel is None:
            center_pixel = pairs.center_pixel()

        if color is None:
            # Select a color at random from the color_palette
            color = choice(Agent.color_palette)[1]

        super().__init__(center_pixel, color)

        self.scale = scale
        self.shape = shape
        self.base_image = self.create_base_image()

        self.id = Agent.id
        Agent.id += 1
        self.label = None
        World.agents.add(self)
        self.current_patch().add_agent(self)
        self.heading = randint(0, 359)
        self.speed = 1
        # To keep PyCharm happy.
        self.velocity = Velocity.velocity_00
Example #5
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 = gui_get('figure')
     self.do_a_step()
Example #6
0
 def create_random_agent(self, color=None, shape_name='netlogo_figure', scale=1.4):
     """
     Create an Agent placed randomly on the screen.
     Set it to face the screen's center pixel.
     """
     agent = self.agent_class(color=color, shape_name=shape_name, scale=scale)
     agent.move_to_xy(Pixel_xy.random_pixel())
     agent.face_xy(center_pixel())
     return agent
Example #7
0
 def do_a_step(self):
     if self.current_figure in ['clockwise', 'counter-clockwise']:
         r = self.reference_agent.distance_to_xy(center_pixel())
         self.go_in_circle(r)
     elif self.current_figure == 'breathe':
         self.breathe()
     elif self.current_figure == 'twitchy':
         self.go_twitchily()
     else:
         print(f'Error. No current figure: ({self.current_figure})')
 def create_random_agents(self,
                          n,
                          shape_name='netlogo_figure',
                          color=None,
                          scale=1.4):
     """
     Create n Agents placed randomly on the screen. They are all facing the screen's center pixel.
     """
     for _ in range(n):
         agent = self.agent_class(color=color,
                                  shape_name=shape_name,
                                  scale=scale)
         agent.move_to_xy(Pixel_xy.random_pixel())
         agent.face_xy(center_pixel())
    def link_nodes_for_graph(graph_type, nbr_nodes, ring_node_list):
        """
        Link the nodes to create the requested graph.

        Args:
            graph_type: The name of the graph type.
            nbr_nodes: The total number of nodes the user requested.
                       (Will be > 0 or this method won't be called.)
            ring_node_list: The nodes that have been arranged in a ring.
                            Will contain either:
                            nbr_nodes - 1 if graph type is STAR or WHEEL
                            or nbr_nodes otherwise

        Returns: None

        Overrides this function from graph_framework.
        """
        print("\n\nlink_nodes_for_graph: Your code goes here.")

        # ! central node is needed if graph type is wheel or star
        center_node = None
        if graph_type in ['wheel', 'star']:
            center_node = Graph_Node()
            center_node.move_to_xy(center_pixel())

            # ! The ring_list is empty
        if not ring_node_list:
            return

        # ! if the graph type is wheel or star and there is only one node, link the lone node to itself
        if len(ring_node_list) == 1:
            if graph_type in ['wheel', 'star']:
                Link(center_node, ring_node_list[0])
            return
        # ! there is more than one node
        for (node_a, node_b) in zip(ring_node_list,
                                    ring_node_list[1:] + [ring_node_list[0]]):

            # ! if user sets 4 nodes
            # ! [1, 2, 3, 4] --> (1,2), (2,3), (3, 1) --> node 4 is the center node

            if graph_type in ['wheel', 'ring']:
                Link(node_a, node_b)

            # ! the center node gets linked to each other node
            # ! node 4 is linked to nodes 1, 2, 3 based on the example above
            if graph_type in ['wheel', 'star']:
                Link(center_node, node_a)

        # ! complete the ring
        # if graph_type in ['wheel', 'ring']:
        #     Link(ring_node_list[-1], ring_node_list[0])

        if graph_type in ['random']:
            link_probability = gui_get(LINK_PROB)
            links = (Link(ring_node_list[i], ring_node_list[j])
                     for i in range(nbr_nodes - 1)
                     for j in range(i + 1, nbr_nodes)
                     if randint(1, 100) <= link_probability)
            for _ in links:
                pass
            return

        # Preferential Attachment
        if graph_type in ['pref attachment']:
            Link(ring_node_list[0], ring_node_list[1])

            most_links = 1
            node_links = []

            for x in range(nbr_nodes):
                node_links.append(0)
            node_links[0] = 1
            node_links[1] = 1

            for i in range(2, nbr_nodes):
                for j in range(nbr_nodes):
                    if node_links[j] > node_links[most_links]:
                        most_links = j
                degree = node_links[most_links] / (i + 2)

                if most_links == 1:
                    first_link = randint(0, 1)
                    Link(ring_node_list[i], ring_node_list[first_link])
                    node_links[first_link] = 2
                    node_links[i] = 1
                    most_links = first_link

                else:

                    random_Number = randint(1, 10) / 10
                    if random_Number >= degree:
                        random_Node = randint(1, nbr_nodes - 1)
                        if random_Node != i:
                            Link(ring_node_list[i],
                                 ring_node_list[random_Node])
                            node_links[random_Node] += 1
                            node_links[i] += 1
                        else:
                            Link(ring_node_list[i],
                                 ring_node_list[(i + 1) % nbr_nodes])
                            node_links[i] += 1

                    else:
                        Link(ring_node_list[i], ring_node_list[most_links])
                        node_links[i] += 1
                        node_links[most_links] += 1
            return

        # Small World
        if graph_type in ['small world']:

            link_probability = gui_get(LINK_PROB)
            for i in range(nbr_nodes):
                if randint(1, 100) <= link_probability:
                    #Link to 2 random nodes if we hit the link_probability
                    for i in range(0, 3):
                        random_Node = randint(1, nbr_nodes - 1)
                        if random_Node != i:
                            Link(ring_node_list[i],
                                 ring_node_list[random_Node])

                else:
                    #Otherwise link to the next 2 nodes in line
                    Link(ring_node_list[i],
                         ring_node_list[(i + 1) % nbr_nodes])
                    Link(ring_node_list[i],
                         ring_node_list[(i + 2) % nbr_nodes])

            return
    def link_nodes_for_graph(graph_type, nbr_nodes, ring_node_list):
        """
        Link the nodes to create the requested graph.

        Args:
            graph_type: The name of the graph type.
            nbr_nodes: The total number of nodes the user requested.
                       (Will be > 0 or this method won't be called.)
            ring_node_list: The nodes that have been arranged in a ring.
                            Will contain either:
                            nbr_nodes - 1 if graph type is STAR or WHEEL
                            or nbr_nodes otherwise

        Returns: None

        Overrides this function from graph_framework.
        """
        print("\n\nlink_nodes_for_graph: Your code goes here.")

        # ! central node is needed if graph type is wheel or star
        center_node = None
        if graph_type in ['wheel', 'star']:
            center_node = Graph_Node()
            center_node.move_to_xy(center_pixel())

            # ! The ring_list is empty
        if not ring_node_list:
            return

        # ! if the graph type is wheel or star and there is only one node, link the lone node to itself
        if len(ring_node_list) == 1:
            if graph_type in ['wheel', 'star']:
                Link(center_node, ring_node_list[0])
            return
        # ! there is more than one node
        for (node_a, node_b) in zip(ring_node_list,
                                    ring_node_list[1:] + [ring_node_list[0]]):

            # ! if user sets 4 nodes
            # ! [1, 2, 3, 4] --> (1,2), (2,3), (3, 1) --> node 4 is the center node

            if graph_type in ['wheel', 'ring']:
                Link(node_a, node_b)

            # ! the center node gets linked to each other node
            # ! node 4 is linked to nodes 1, 2, 3 based on the example above
            if graph_type in ['wheel', 'star']:
                Link(center_node, node_a)

        # ! complete the ring
        # if graph_type in ['wheel', 'ring']:
        #     Link(ring_node_list[-1], ring_node_list[0])

        if graph_type in ['random']:
            link_probability = gui_get(LINK_PROB)
            links = (Link(ring_node_list[i], ring_node_list[j])
                     for i in range(nbr_nodes - 1)
                     for j in range(i + 1, nbr_nodes)
                     if randint(1, 100) <= link_probability)
            for _ in links:
                pass
            return