Beispiel #1
0
 def pixel_to_row_col(self: Pixel_xy):
     """
     Get the patch RowCol for this pixel
    """
     row = self.y // gui.BLOCK_SPACING()
     col = self.x // gui.BLOCK_SPACING()
     return RowCol((int(row), int(col)))
Beispiel #2
0
 def patch_to_center_pixel(self) -> Pixel_xy:
     """
     Get the center_pixel position for this RowCol.
     Leave a border of 1 pixel at the top and left of the patches
     """
     pv = Pixel_xy((1 + gui.BLOCK_SPACING() * self.col + gui.HALF_PATCH_SIZE(),
                    1 + gui.BLOCK_SPACING() * self.row + gui.HALF_PATCH_SIZE()))
     return pv
Beispiel #3
0
    def __init__(self, gui_left_upper, gui_right_upper=None, caption="Basic Model",
                 patch_size=15, board_rows_cols=(51, 51), clear=None, bounce=None, fps=None):

        gui.PATCH_SIZE = patch_size if patch_size % 2 == 1 else patch_size + 1
        gui.PATCH_ROWS = board_rows_cols[0] if board_rows_cols[0] % 2 == 1 else board_rows_cols[0] + 1
        gui.PATCH_COLS = board_rows_cols[1] if board_rows_cols[1] % 2 == 1 else board_rows_cols[1] + 1

        self.EXIT = 'Exit'
        self.GRAPH = '-GRAPH-'
        self.SETUP = 'setup'
        self.STOP = 'Stop'

        self.clock = pg.time.Clock()

        self.caption = caption

        self.screen_shape_width_height = (SCREEN_PIXEL_WIDTH(), SCREEN_PIXEL_HEIGHT())

        # All these gui.<variable> elements are globals in this file.
        gui.WINDOW = self.make_window(caption, gui_left_upper, gui_right_upper=gui_right_upper,
                                      clear=clear, bounce=bounce, fps=fps)
        pg.init()
        gui.FONT = SysFont(None, int(1.5 * gui.BLOCK_SPACING()))

        # All graphics are drawn to gui.SCREEN, which is a global variable.
        gui.SCREEN = pg.display.set_mode(self.screen_shape_width_height)
Beispiel #4
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()
Beispiel #5
0
 def __init__(self, center_pixel: Pixel_xy, color=Color('black')):
     super().__init__()
     self.center_pixel = center_pixel
     self.rect = Rect((0, 0), (gui.PATCH_SIZE, gui.PATCH_SIZE))
     # noinspection PyTypeChecker
     sum_pixel: Pixel_xy = center_pixel + Pixel_xy((1, 1))
     assert isinstance(sum_pixel, Pixel_xy)
     self.rect.center = sum_pixel  # .as_tuple()
     self.image = Surface((self.rect.w, self.rect.h))
     self.color = self.base_color = color
     self.label = None
     self.font = Font(None, int(1.5 * gui.BLOCK_SPACING()))
     self.agent_text_offset = int(1.5 * gui.PATCH_SIZE)
     self.patch_text_offset = -int(1.0 * gui.PATCH_SIZE)
    def move(self, initial_speed, move_by_delay):
        if move_by_delay:
            self.forward(gui.BLOCK_SPACING())
            self.ticks_here = 1

        else:
            if self.in_middle:
                self.forward(initial_speed * 10)
            else:
                if self.current_patch().color == Color("green"):
                    speed = initial_speed
                else:
                    speed = (self.current_patch().color.b / 127)
                    if speed <= 1:
                        speed = 1
                self.forward(speed)
Beispiel #7
0
    def __init__(self,
                 gui_left_upper,
                 gui_right_upper=None,
                 caption="Basic Model",
                 patch_size=15,
                 board_rows_cols=(51, 51),
                 bounce=None,
                 fps=None):

        gui.PATCH_SIZE = patch_size if patch_size % 2 == 1 else patch_size + 1
        gui.PATCH_ROWS = board_rows_cols[
            0] if board_rows_cols[0] % 2 == 1 else board_rows_cols[0] + 1
        gui.PATCH_COLS = board_rows_cols[
            1] if board_rows_cols[1] % 2 == 1 else board_rows_cols[1] + 1

        self.EXIT = 'Exit'
        self.GO = 'go'
        self.GO_ONCE = 'go once'
        self.GOSTOP = 'GoStop'
        self.GRAPH = '-GRAPH-'
        self.SETUP = 'setup'
        self.STOP = 'Stop'

        self.clock = pg.time.Clock()
        self.fps = 60
        self.idle_fps = 10

        self.screen_color = pg.Color(sg.RGB(50, 60, 60))

        self.caption = caption

        self.screen_shape_width_height = (SCREEN_PIXEL_WIDTH(),
                                          SCREEN_PIXEL_HEIGHT())
        gui.WINDOW = self.make_window(caption,
                                      gui_left_upper,
                                      gui_right_upper=gui_right_upper,
                                      bounce=bounce,
                                      fps=fps)

        pg.init()
        gui.FONT = pg.font.SysFont(None,
                                   int(1.5 * gui.BLOCK_SPACING()))  # None

        # All graphics are drawn to gui.SCREEN, which is a global variable.
        gui.SCREEN = pg.display.set_mode(self.screen_shape_width_height)
Beispiel #8
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()