Ejemplo n.º 1
0
 def add_barrier(self, last):
     if self.screen_width - last.x > 100 and self.add_barriers == True:
         self.barrier_types = [barrier.Barrier(self.screen_width, self.screen_height, last.last, self.openings),
                               closing_barrier.ClosingBarrier(self.screen_width, self.screen_height, last.last, self.openings),
                               moving_barrier.MovingBarrier(self.screen_width, self.screen_height, last.last, self.openings)]
         if self.num_barriers % 5 == 0:
             self.openings -= 10
         self.barriers.append(random.choice(self.barrier_types))
         self.num_barriers += 1
Ejemplo n.º 2
0
 def level1(self):
     barriers = []
     barriers.append(barrier.Barrier(0, 70, 600, 10)) 
     barriers.append(barrier.Barrier(0, 145, 420, 10)) 
     barriers.append(barrier.Barrier(520, 145, 550, 10)) 
     barriers.append(barrier.Barrier(0, 225, 550, 10)) 
     barriers.append(barrier.Barrier(0, 305, 250, 10))  
     barriers.append(barrier.Barrier(350, 305, 250, 10)) 
     barriers.append(barrier.Barrier(475, 315, 10, 70)) 
     barriers.append(barrier.Barrier(100, 385, 600, 10)) 
     return barriers
Ejemplo n.º 3
0
 def __init__(self, height, width):
     self.screen_width = width
     self.screen_height = height
     self.barrier_types = [barrier.Barrier(width, height),
                           closing_barrier.ClosingBarrier(width, height),
                           moving_barrier.MovingBarrier(width, height)]
     self.barriers = [random.choice(self.barrier_types)]
     self.num_barriers = len(self.barriers)
     self.openings = 200
     self.add_barriers = True
Ejemplo n.º 4
0
 def __init__(self, host, port):
     print "Starting poker server..."
     self.host = host
     self.port = port
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.bind((self.host, self.port))
     self.players = []
     self.currentPlayer = 0
     self.betToMatch = 0
     self.pot = 0
     self.deck = dealer.getDeck()
     self.barrier = barrier.Barrier(numPlayers)
Ejemplo n.º 5
0
 def level4(self):
     barriers = []
     barriers.append(barrier.Barrier(0, 70, 155, 10)) 
     barriers.append(barrier.Barrier(250, 0, 10, 80)) 
     barriers.append(barrier.Barrier(260, 70, 140, 10)) 
     barriers.append(barrier.Barrier(390, 80, 10, 110)) 
     barriers.append(barrier.Barrier(490, 70, 210, 10)) 
     barriers.append(barrier.Barrier(0, 145, 250, 10)) 
     barriers.append(barrier.Barrier(85, 225, 220, 10)) 
     barriers.append(barrier.Barrier(85, 235, 10, 150)) 
     barriers.append(barrier.Barrier(95, 300, 300, 10)) 
     barriers.append(barrier.Barrier(95, 375, 500, 10)) 
     barriers.append(barrier.Barrier(485, 265, 10, 110)) 
     barriers.append(barrier.Barrier(485, 185, 110, 10)) 
     barriers.append(barrier.Barrier(585, 265, 10, 110)) 
     return barriers
Ejemplo n.º 6
0
 def level3(self):
     barriers = []
     barriers.append(barrier.Barrier(90, 0, 10, 233)) 
     barriers.append(barrier.Barrier(180, 70, 10, 233)) 
     barriers.append(barrier.Barrier(90, 303, 100, 10)) 
     barriers.append(barrier.Barrier(90, 383, 190, 10)) 
     barriers.append(barrier.Barrier(270, 70, 10, 90)) 
     barriers.append(barrier.Barrier(360, 70, 210, 10)) 
     barriers.append(barrier.Barrier(280, 150, 280, 10)) 
     barriers.append(barrier.Barrier(560, 150, 10, 80)) 
     barriers.append(barrier.Barrier(560, 190, 140, 10)) 
     barriers.append(barrier.Barrier(280, 230, 290, 10)) 
     barriers.append(barrier.Barrier(270, 230, 10, 80)) 
     barriers.append(barrier.Barrier(360, 310, 10, 80)) 
     barriers.append(barrier.Barrier(370, 310, 330, 10)) 
     return barriers
Ejemplo n.º 7
0
 def level2(self):
     barriers = []
     barriers.append(barrier.Barrier(105, 110, 305, 10)) 
     barriers.append(barrier.Barrier(410, 70, 200, 10)) 
     barriers.append(barrier.Barrier(410, 80, 10, 110)) 
     barriers.append(barrier.Barrier(600, 80, 10, 200)) 
     barriers.append(barrier.Barrier(105, 190, 315, 10)) 
     barriers.append(barrier.Barrier(105, 200, 10, 200)) 
     barriers.append(barrier.Barrier(205, 280, 10, 120)) 
     barriers.append(barrier.Barrier(205, 270, 215, 10)) 
     barriers.append(barrier.Barrier(410, 280, 10, 90)) 
     barriers.append(barrier.Barrier(420, 360, 90, 10)) 
     barriers.append(barrier.Barrier(460, 360, 10, 110)) 
     barriers.append(barrier.Barrier(510, 200, 10, 170)) 
     barriers.append(barrier.Barrier(520, 280, 90, 10)) 
     barriers.append(barrier.Barrier(610, 360, 90, 10)) 
     return barriers
Ejemplo n.º 8
0
class Game:
    """Main class controlling the game"""
    WINDOW = pygame.display.set_mode(
        (constants.GAMEMAP_WIDTH_PX, constants.GAMEMAP_HEIGHT_PX))
    SPRITE_SHEET = pygame.image.load(constants.SPRITE_SHEET).convert()
    MAP = gamemap.Map(constants.GAMEMAP_FILE)
    barrier = barrier.Barrier(list(MAP.get_barriers()))

    def __init__(self):
        self.tick = 0
        self.level = 0
        self.score = 0
        self.player = None
        self.pellets = list(self.MAP.get_pellets())
        self.fruit = 0
        self.lives = 4
        self.combo = 1
        self.wait = 0
        self.ghosts = {}
        self.previous_ghosts_state = constants.SCATTER

    def initialize_level(self, next_level):
        """Initializing level after player death or to advance to new level"""
        player_x, player_y = self.MAP.get_coordinates('s')
        blinky_x, blinky_y = self.MAP.get_coordinates('b')
        pinky_x, pinky_y = self.MAP.get_coordinates('p')
        inky_x, inky_y = self.MAP.get_coordinates('i')
        clyde_x, clyde_y = self.MAP.get_coordinates('c')
        self.player = characters.Player(player_x, player_y)
        self.ghosts = {
            "blinky": characters.Blinky(blinky_x, blinky_y),
            "pinky": characters.Pinky(pinky_x, pinky_y),
            "inky": characters.Inky(inky_x, inky_y),
            "clyde": characters.Clyde(clyde_x, clyde_y),
        }
        self.combo = 1
        self.fruit = 0
        self.update_caption()
        self.wait = 1
        if next_level:
            self.pellets = list(self.MAP.get_pellets())
            self.draw_walls()
            self.draw_pellets()
            self.level += 1
            self.tick = 0
        else:
            for ghost in self.ghosts.values():
                ghost.state = self.previous_ghosts_state
            self.lives -= 1
            if self.lives == 0:
                drawhelper.draw_text("GAME OVER!")
                pygame.display.update()
                while True:
                    events = pygame.event.get()
                    for event in events:
                        if event.type == pygame.KEYDOWN:
                            return

    def update_caption(self):
        """Updates caption shown on Application bar"""
        pygame.display.set_caption(f"Pacman level: {self.level} "
                                   f"score: {self.score} "
                                   f"lives: {self.lives}")

    def remove_pellet(self, tile_x, tile_y):
        """Removes the pellet from given location and returns the value of it"""
        try:
            tile = next(t for t in self.pellets
                        if t.x == tile_x and t.y == tile_y)
            if tile.cell in [constants.PELLET, constants.PELLET2]:
                del self.pellets[self.pellets.index(tile)]
                return 10
            if tile.cell == constants.POWER_PELLET:
                del self.pellets[self.pellets.index(tile)]
                return 50
            return False
        except StopIteration:
            return False

    def step(self):
        """Step method - executed once every frame"""
        start_time = time.time()
        if not self.wait:
            if self.player.eat(
                    self,
                    self.remove_pellet(self.player.get_tile_x(),
                                       self.player.get_tile_y())):

                self.spawn_fruit()
                if self.next_level():
                    return (time.time() - start_time) * 1000
            else:
                self.player.move(self.level)
            self.change_ghost_states()
            if self.check_collisions():
                return (time.time() - start_time) * 1000
            for ghost in self.ghosts.values():
                ghost.move(self.player, self.ghosts, len(self.pellets),
                           self.previous_ghosts_state, self.level)
            self.draw_fruit()
            self.draw_pellets()
            self.draw_characters()
            pygame.display.update()  # room for improvement
            self.tick += 1
            self.clear_characters()
        else:
            self.clear_fruit()
            self.draw_pellets()
            self.draw_characters()
            drawhelper.draw_text("R E A D Y !")
            pygame.display.update()

            events = pygame.event.get()
            keys = [pygame.K_RIGHT, 0, pygame.K_LEFT, 0]
            for event in events:
                if event.type == pygame.KEYDOWN:
                    if event.key in keys:
                        self.player.direction = keys.index(event.key)
                        self.player.next_direction = keys.index(event.key)
                    self.wait = 0
                    self.clear_characters()
                    drawhelper.clear_text()
        return (time.time() - start_time) * 1000

    def check_collisions(self):
        """Check for collisions of player with any of the ghosts"""
        for ghost in self.ghosts.values():
            if not ghost.dead:
                if ghost.get_tile_x() == self.player.get_tile_x():
                    if ghost.get_tile_y() == self.player.get_tile_y():
                        if ghost.state == constants.FRIGHTENED:
                            self.score += 200 * self.combo
                            self.combo *= 2
                            self.update_caption()
                            ghost.dead = True
                            ghost.update_target(self.player, self.ghosts)
                        else:
                            self.initialize_level(False)
                            return True
        return False

    def next_level(self):
        """Checks if no pellets are left and moves to next level"""
        if not self.pellets:
            self.initialize_level(True)
            return True
        return False

    def change_ghost_states(self):
        """Operates the ghost state cycle rotation"""
        if self.player.fright > 0:
            self.player.fright -= 1
        else:
            if any(g for g in self.ghosts.values()
                   if g.state == constants.FRIGHTENED):
                for ghost in self.ghosts.values():
                    ghost.change_state(self.previous_ghosts_state)
            cycle_times = constants.get_level_based_constant(
                self.level, constants.GHOST_MODE_CYCLE)
            second = self.tick / constants.TICKRATE - \
                     self.player.power_pellets * \
                     constants.get_level_based_constant(self.level,
                                                        constants.FRIGHT_TIME)
            if second in cycle_times:
                cycle = cycle_times.index(second)
                new_state = constants.SCATTER if cycle % 2 else constants.CHASE
                self.previous_ghosts_state = new_state
                for ghost in self.ghosts.values():
                    ghost.change_state(new_state)

    def draw_walls(self):
        """Draws all the map walls depending on wall types"""
        for wall_x, wall_y, wall_type in self.MAP.get_walls():
            {
                0: lambda x, y: drawhelper.draw_arc(x + .5, y + .5, 1 / 2, 1),
                1: lambda x, y: drawhelper.draw_arc(x - .5, y + .5, 0, 1 / 2),
                2: lambda x, y: drawhelper.draw_arc(x - .5, y - .5, 3 / 2, 0),
                3: lambda x, y: drawhelper.draw_arc(x + .5, y - .5, 1, 3 / 2),
                4: lambda x, y: drawhelper.draw_line(x + .5, y, x + .5, y + 1),
                5: lambda x, y: drawhelper.draw_line(x, y + .5, x + 1, y + .5),
            }[wall_type](wall_x, wall_y)
        pygame.display.update()

    def draw_characters(self):
        """Draws barrier, player and all the ghosts"""
        self.barrier.draw()
        self.player.draw(self.tick)
        for ghost in self.ghosts.values():
            ghost.draw(self.tick, self.player.fright)

    def clear_characters(self):
        """Clears barrier, player and all the ghosts"""
        self.barrier.clear()
        self.player.clear()
        for ghost in self.ghosts.values():
            ghost.clear()

    def spawn_fruit(self):
        """Spawn fruits when enough pellets are eaten"""
        pellets = len(self.pellets)
        if self.MAP.total_pellets - pellets in constants.FRUIT_SPAWN:
            self.fruit = random.randint(9 * constants.TICKRATE,
                                        10 * constants.TICKRATE)

    def draw_fruit(self):
        """Draws fruit on game window"""
        if self.fruit > 0:
            fruit_x, fruit_y = self.MAP.get_coordinates('f')
            fruit_image_col = constants.get_level_based_constant(
                self.level, constants.FRUITS)[0]
            offset = constants.TILE_SIZE / 2 - constants.SPRITE_SIZE / 2
            self.WINDOW.blit(
                drawhelper.get_image_at(fruit_image_col,
                                        constants.FRUIT_IMAGE_ROW),
                ((fruit_x + 0.5) * constants.TILE_SIZE + offset,
                 fruit_y * constants.TILE_SIZE + offset))
            self.fruit -= 1
            if self.fruit == 0:
                self.clear_fruit()

    def clear_fruit(self):
        """Clears fruit"""
        fruit_x, fruit_y = self.MAP.get_coordinates('f')
        offset = (constants.TILE_SIZE - constants.SPRITE_SIZE) / 2
        if self.fruit == 0:
            drawhelper.draw_rect(fruit_x + 0.5,
                                 fruit_y,
                                 constants.SPRITE_SIZE,
                                 offset=offset)

    def draw_pellets(self):
        """Draws all pellets"""
        tile_size = constants.TILE_SIZE
        size = tile_size / 8
        offset = tile_size / 2 - size / 2
        for pellet in self.pellets:
            pellet_x, pellet_y, pellet_type = dataclasses.astuple(pellet)
            if pellet_type in [constants.PELLET, constants.PELLET2]:
                drawhelper.draw_rect(pellet_x,
                                     pellet_y,
                                     size,
                                     offset=offset,
                                     color=constants.PELLET_COLOR)
            elif pellet_type == constants.POWER_PELLET:
                pygame.draw.circle(self.WINDOW, constants.PELLET_COLOR,
                                   (int((pellet_x + 0.5) * tile_size),
                                    int((pellet_y + 0.5) * tile_size)),
                                   int(size * 2))
Ejemplo n.º 9
0
 def level7(self):
     barriers = []
     barriers.append(barrier.Barrier(80, 70, 10, 250)) 
     barriers.append(barrier.Barrier(90, 240, 100, 10)) 
     barriers.append(barrier.Barrier(190, 70, 10, 320)) 
     barriers.append(barrier.Barrier(80, 390, 540, 10)) 
     barriers.append(barrier.Barrier(285, 235, 10, 70)) 
     barriers.append(barrier.Barrier(285, 305, 420, 10)) 
     barriers.append(barrier.Barrier(285, 70, 10, 85)) 
     barriers.append(barrier.Barrier(285, 155, 155, 10)) 
     barriers.append(barrier.Barrier(440, 155, 10, 70)) 
     barriers.append(barrier.Barrier(375, 70, 195, 10)) 
     barriers.append(barrier.Barrier(570, 70, 10, 155)) 
     return barriers
Ejemplo n.º 10
0
 def level6(self):
     barriers = []
     barriers.append(barrier.Barrier(80, 70, 195, 10)) 
     barriers.append(barrier.Barrier(80, 80, 10, 70)) 
     barriers.append(barrier.Barrier(80, 230, 10, 240)) 
     barriers.append(barrier.Barrier(170, 80, 10, 310)) 
     barriers.append(barrier.Barrier(170, 390, 155, 10)) 
     barriers.append(barrier.Barrier(405, 0, 10, 80)) 
     barriers.append(barrier.Barrier(300, 150, 310, 10)) 
     barriers.append(barrier.Barrier(600, 70, 10, 80)) 
     barriers.append(barrier.Barrier(290, 150, 10, 90)) 
     barriers.append(barrier.Barrier(290, 240, 220, 10)) 
     barriers.append(barrier.Barrier(290, 315, 310, 10)) 
     barriers.append(barrier.Barrier(600, 240, 10, 230)) 
     barriers.append(barrier.Barrier(450, 390, 150, 10)) 
     return barriers
def init_barriers(height, width):
    height = convertEv(height)
    return np.array([barrier.Barrier(height, width) for i in range(0, 4)])
Ejemplo n.º 12
0
def main(processes, messages, runscript, broadcastType, logsDir, testConfig):
    # Set tc for loopback
    #tc = TC(testConfig['TC'])
    #print(tc)

    sleepSends = [25, 50]
    sleepDelivers = [150, 200, 250, 300, 350]
    timeoutAcks = [10, 15]
    tries = [2]
    numOutstandings = [1400, 1500, 1600]
    sleepOutStandings = [50, 100, 150]

    best_finish = float('inf')
    best_del = 0
    best_finish_dict = defaultdict(int)
    best_del_dict = defaultdict(int)

    for sleepSend in sleepSends:
        for sleepDeliver in sleepDelivers:
            for timeoutAck in timeoutAcks:
                for tri in tries:
                    for numOutstanding in numOutstandings:
                        for sleepOutStanding in sleepOutStandings:
                            print("sleepSend:", sleepSend, flush=True)
                            print("sleepDeliver:", sleepDeliver, flush=True)
                            print("timeoutAck:", timeoutAck, flush=True)
                            print("tries:", tri, flush=True)
                            print("numOutstanding:", numOutstanding, flush=True)
                            print("sleepOutStanding:", sleepOutStanding, flush=True)
                            tot_finishes = []
                            tot_dels = []
                            for _ in range(3):
                                # Start the barrier
                                initBarrier = barrier.Barrier(BARRIER_IP, BARRIER_PORT, processes)
                                initBarrier.listen()
                                startTimesFuture = initBarrier.startTimesFuture()

                                initBarrierThread = threading.Thread(target=initBarrier.wait)
                                initBarrierThread.start()

                                # Start the finish signal
                                finishSignal = finishedSignal.FinishedSignal(SIGNAL_IP, SIGNAL_PORT, processes)
                                finishSignal.listen()
                                finishSignalThread = threading.Thread(target=finishSignal.wait)
                                finishSignalThread.start()

                                if broadcastType == "fifo":
                                    validation = FifoBroadcastValidation(processes, messages, logsDir)
                                else:
                                    validation = LCausalBroadcastValidation(processes, messages, logsDir, None)

                                hostsFile, configFile = validation.generateConfig()

                                try:
                                    # Start the processes and get their PIDs
                                    procs = startProcesses(processes, runscript, hostsFile.name, configFile.name, logsDir, 
                                                            sleepSend, sleepDeliver, timeoutAck, tri, numOutstanding, sleepOutStanding)

                                    # Create the stress test
                                    st = StressTest(procs,
                                                    testConfig['ST']['concurrency'],
                                                    testConfig['ST']['attempts'],
                                                    testConfig['ST']['attemptsDistribution'])

                                    for (logicalPID, procHandle) in procs:
                                        print("Process with logicalPID {} has PID {}".format(logicalPID, procHandle.pid), flush=True)


                                    initBarrierThread.join()
                                    print("All processes have been initialized.", flush=True)

                                    # st.run()
                                    # print("StressTest is complete.")


                                    # print("Resuming stopped processes.")
                                    # st.continueStoppedProcesses()

                                    print("Waiting until all running processes have finished broadcasting.", flush=True)
                                    finishSignalThread.join(1)

                                    if(not finishSignalThread.is_alive()):
                                        finishes = []
                                        for pid, startTs in OrderedDict(sorted(startTimesFuture.items())).items():
                                            print("Process {} finished broadcasting {} messages in {} ms".format(pid, messages, finishSignal.endTimestamps()[pid] - startTs), flush=True)
                                            finishes.append(finishSignal.endTimestamps()[pid] - startTs)
                                        avg_time = np.mean(np.array(finishes))
                                        print("Average time to finished broadcast: {} ms".format(avg_time), flush=True)
                                        tot_finishes.append(avg_time)
                                    else:
                                        tot_finishes.append(1000)
                                        print("Average time to finished broadcast: > 1 seconds", flush=True)


                                    numberDel = []
                                    time.sleep(10)
                                    st.terminateAllProcesses()
                                    time.sleep(3)
                                    for pid in range(1, processes+1):
                                        filePath = os.path.join(logsDir, 'proc{:02d}.stdout'.format(pid))
                                        with open(filePath) as f:
                                            for line in f:
                                                if "Total message delivered: " in line:
                                                    tot_del = line.split("Total message delivered: ")[-1].rstrip("\n").rstrip()
                                                    print("Process {} delivered {} messages".format(pid, tot_del), flush=True)
                                                    numberDel.append(int(tot_del))
                                                    break
                                    avg_del = np.mean(np.array(numberDel))
                                    print("Average number of delivered messages: {} ".format(avg_del), flush=True)
                                    tot_dels.append(avg_del)

                                    mutex = threading.Lock()

                                    def waitForProcess(logicalPID, procHandle, mutex):
                                        procHandle.wait()
                                        with mutex:
                                            print("Process {} exited with {}".format(logicalPID, procHandle.returncode), flush=True)

                                    # Monitor which processes have exited
                                    monitors = [threading.Thread(target=waitForProcess, args=(logicalPID, procHandle, mutex)) for (logicalPID, procHandle) in procs]
                                    [p.start() for p in monitors]
                                    [p.join() for p in monitors]
                                    if procs is not None:
                                        for _, p in procs:
                                            p.kill()

                                    # input('Hit `Enter` to validate the output')
                                    # print("Result of validation: {}".format(validation.checkAll()))
                                finally:
                                    if procs is not None:
                                        for _, p in procs:
                                            p.kill()
                            full_avg_del = np.mean(np.array(tot_dels))
                            if (full_avg_del > best_del):
                                best_del_dict["sleepSend"] = sleepSend
                                best_del_dict["sleepDeliver"] = sleepDeliver
                                best_del_dict["timeoutAck"] = timeoutAck
                                best_del_dict["tries"] = tri
                                best_del_dict["numOutstanding"] = numOutstanding
                                best_del_dict["sleepOutStanding"] = sleepOutStanding
                                print("Best del dict:", best_del_dict, flush=True)
                                print("Best del:", full_avg_del, flush=True)
                                best_del = full_avg_del
                            full_avg_finishes = np.mean(np.array(tot_finishes))
                            if (full_avg_finishes < best_finish):
                                best_finish_dict["sleepSend"] = sleepSend
                                best_finish_dict["sleepDeliver"] = sleepDeliver
                                best_finish_dict["timeoutAck"] = timeoutAck
                                best_finish_dict["tries"] = tri
                                best_finish_dict["numOutstanding"] = numOutstanding
                                best_finish_dict["sleepOutStanding"] = sleepOutStanding
                                best_finish = full_avg_finishes
                                print("Best finish dict:", best_finish_dict, flush=True)
                                print("Best finish:", full_avg_finishes, flush=True)

    print("Best del num: ", best_del, flush=True)
    print("Best del dict:", best_del_dict, flush=True)
    print("Best finish time: ", best_finish, flush=True)
    print("Best finish dict:", best_finish_dict, flush=True)
Ejemplo n.º 13
0
    def __init__(self, filename):
        # Class variables
        self.immobileList = pygame.sprite.RenderPlain()
        self.doorList = pygame.sprite.RenderPlain()
        orig = (TILE_SIZE, TILE_SIZE)

        self.startList = [orig for i in range(MAX_PLAYERS + 1)]
        self.monsterList = pygame.sprite.RenderPlain()

        # Attempt to load the map file
        try:
            fullName = filename
            mapFile = open(fullName)
        except:
            raise TypeError, "Map file %s could not be loaded." % filename

        # Read the lines of the map file
        mapLines = mapFile.readlines()
        mapStarted = False
        lineNum = 1
        x = 0
        y = 0

        # Parse each line separately
        for line in mapLines:
            # Check if we have started reading the map tiles
            if mapStarted:
                #try:
                x = 0
                for tile in line.split():
                    if tile[0] == '*':
                        newWall = barrier.Barrier((x, y))
                        self.immobileList.add(newWall)
                    elif tile == '.':
                        pass
                    elif tile == 'D':
                        newDoor = barrier.Door((x, y))
                        self.doorList.add(newDoor)
                        self.immobileList.add(newDoor)
                    elif tile[0] == 'P':
                        #set the starting point
                        if tile[1] == '#':
                            self.startList[0] = [x, y]
                            for startPos in range(len(self.startList)):
                                if self.startList[startPos] == orig:
                                    self.startList[startPos] = [x, y]
                        else:
                            self.startList[int(tile[1])] = [x, y]
                    elif tile[0] == '!':
                        # Powerup
                        newItem = item.Item([x, y], int(tile[1]))
                        self.immobileList.add(newItem)

                    elif tile[0] == 'M':
                        # Monster
                        global nextid
                        nextid += 1
                        self.monsterList.add \
                        (character.Character([x,y],MONSTER_CLASS + int(tile[1]),nextid))

                    elif tile[0] == 'G':
                        # Generator
                        global nextid
                        nextid += 1
                        genLevel = 3
                        newGenerator = monstergenerator.MonsterGenerator\
                                       ([x,y],GENERATOR_CLASS + int(tile[1]), \
                                        genLevel, nextid)
                        self.monsterList.add(newGenerator)

                    elif tile[0] == 'X':
                        # Exit or teleporter
                        if tile[1] == '0':
                            #teleporter
                            pass
                        else:
                            #exit
                            newExit = exit.Exit([x, y], int(tile[1]))
                            self.immobileList.add(newExit)
                        pass
                    else:
                        #default to clear for now
                        pass
                    x += TILE_SIZE
                y += TILE_SIZE
                #except:
                pass

            elif line.startswith('author'):
                self.author = line[7:-2]
            else:
                # Try to get the map size
                try:
                    lineList = line.split()
                    self.width = int(lineList[0])
                    self.height = int(lineList[1])
                    mapStarted = True
                except ValueError:
                    mapStarted = False
            # Keep track of the line number for error messages
            lineNum += 1
Ejemplo n.º 14
0
 def reset(self):
     self.barriers = [barrier.Barrier(self.screen_width, self.screen_height)]
     self.opening = 150
Ejemplo n.º 15
0
 def level5(self):
     barriers = []
     barriers.append(barrier.Barrier(0, 70, 300, 10)) 
     barriers.append(barrier.Barrier(400, 0, 10, 120)) 
     barriers.append(barrier.Barrier(500, 110, 110, 10)) 
     barriers.append(barrier.Barrier(600, 120, 10, 65)) 
     barriers.append(barrier.Barrier(90, 185, 520, 10)) 
     barriers.append(barrier.Barrier(435, 195, 10, 80)) 
     barriers.append(barrier.Barrier(90, 265, 250, 10)) 
     barriers.append(barrier.Barrier(150, 275, 10, 100)) 
     barriers.append(barrier.Barrier(90, 375, 310, 10)) 
     barriers.append(barrier.Barrier(190, 385, 10, 80)) 
     barriers.append(barrier.Barrier(535, 265, 65, 10)) 
     barriers.append(barrier.Barrier(600, 265, 10, 110)) 
     barriers.append(barrier.Barrier(500, 375, 110, 10)) 
     return barriers
Ejemplo n.º 16
0
 def level8(self):
     barriers = []
     barriers.append(barrier.Barrier(90, 160, 10, 225)) 
     barriers.append(barrier.Barrier(100, 240, 155, 10)) 
     barriers.append(barrier.Barrier(255, 80, 10, 80)) 
     barriers.append(barrier.Barrier(255, 160, 100, 10)) 
     barriers.append(barrier.Barrier(345, 90, 10, 70)) 
     barriers.append(barrier.Barrier(345, 80, 160, 10)) 
     barriers.append(barrier.Barrier(495, 90, 10, 70)) 
     barriers.append(barrier.Barrier(495, 160, 90, 10)) 
     barriers.append(barrier.Barrier(585, 160, 10, 170)) 
     barriers.append(barrier.Barrier(345, 240, 10, 230)) 
     barriers.append(barrier.Barrier(585, 410, 10, 60))
     barriers.append(barrier.Barrier(590, 80, 110, 10)) 
     return barriers    
if get_mac() == 154505288144005:
	root_dir = "/tmp/"
else:
	root_dir = "/root/trading_data/"

trade_logger = setup_logger('first_logger', root_dir + "prediction_pairs_output.log")

x1 = breakout_process.Breakout() # bad
x2 = volatility_process.VolatilityProcess()# bad
x3 = delta_process.DeltaProcess()#-2.17
x4 = jump_process.JumpProcess()#okay
x5 = create_regimes.CreateRegimes() # good
x6 = gradient.Gradient()# good
x7 = gradient.Gradient()#
x8 = barrier.Barrier()# excellent
x9 = barrier.Barrier()#
x10 = grid_delta.GridDelta()# okay
x11 = markov_process.MarkovProcess()

x_sets = [x5, x2, x4, x11, x10, x6]
lags = [0, 0, 0, 0, 0, 96]


white_list = pickle.load(open(root_dir + "scalper_model_whitelist", 'rb'))
model_prediction_set = []

prediction_key_map = {}

for item in white_list:
Ejemplo n.º 18
0
 def level9(self):
     barriers = []
     barriers.append(barrier.Barrier(90, 80, 10, 300)) 
     barriers.append(barrier.Barrier(190, 120, 10, 250)) 
     barriers.append(barrier.Barrier(190, 110, 400, 10)) 
     barriers.append(barrier.Barrier(590, 110, 10, 260)) 
     barriers.append(barrier.Barrier(590, 450, 10, 50)) 
     barriers.append(barrier.Barrier(390, 370, 10, 150)) 
     barriers.append(barrier.Barrier(290, 280, 100, 10)) 
     barriers.append(barrier.Barrier(290, 200, 10, 80)) 
     barriers.append(barrier.Barrier(300, 200, 200, 10)) 
     barriers.append(barrier.Barrier(490, 210, 10, 160)) 
     barriers.append(barrier.Barrier(300, 360, 190, 10)) 
     return barriers    
def train_and_back_test_stat_arb(trade_logger, model_key, pair,
                                 is_use_residual, martingale_type,
                                 global_currency_pairs, avg_spreads,
                                 avg_prices, entry_bias, trade_dir,
                                 is_train_model, root_dir):

    print model_key, root_dir

    trade_logger.info('Model Key: ' + model_key)

    norm_prices = pickle.load(
        open(root_dir + "stat_arb_series_" + model_key, 'rb'))
    times = {model_key: [1] * len(norm_prices[model_key])}
    portfolio_wts = pickle.load(
        open(root_dir + "portfolio_wts_" + model_key, 'rb'))

    if "_basket" in model_key:
        portfolio_pairs = [pair]
        actual_prices = pickle.load(
            open(root_dir + "actual_price_series_" + model_key, 'rb'))
        prices1, y1 = get_basket_series(norm_prices[model_key],
                                        actual_prices[model_key])

        if pair[4:7] == 'JPY':
            pip_size = 0.01
        else:
            pip_size = 0.0001

        commission = avg_spreads[pair] * pip_size

    else:
        portfolio_pairs = portfolio_wts[model_key]['currency_pairs']
        prices1, y1 = get_portfolio_series(norm_prices[model_key])

        commission = 0
        for currency_pair in portfolio_wts[model_key]['wt']:

            if currency_pair[4:7] == 'JPY':
                pip_size = 0.01
            else:
                pip_size = 0.0001

            commission += abs(
                portfolio_wts[model_key]['wt'][currency_pair]) * (
                    avg_spreads[currency_pair] * pip_size)

    x1 = breakout_process.Breakout()  # bad
    x2 = volatility_process.VolatilityProcess()  # bad
    x3 = delta_process.DeltaProcess()  #-2.17
    x4 = jump_process.JumpProcess()  #okay
    x5 = create_regimes.CreateRegimes()  # good
    x6 = gradient.Gradient()  # good
    x7 = gradient.Gradient()  #
    x8 = barrier.Barrier()  # excellent
    x9 = barrier.Barrier()  #
    x10 = grid_delta.GridDelta()  # okay
    x11 = markov_process.MarkovProcess()
    x_sets = [x5, x2, x4, x11, x10, x6]
    lags = [0, 0, 0, 0, 0, 96]

    whitelist = []
    for model, lag in zip(x_sets, lags):

        model_key_base = model_key + "_" + str(
            model.__class__.__name__) + "_" + str(lag)

        if lag == 0:
            predictions, test_prices = model.init(root_dir, model_key, None,
                                                  prices1, y1, None,
                                                  is_use_residual,
                                                  is_train_model)
        else:
            predictions, test_prices = model.init(root_dir, model_key, None,
                                                  prices1, y1, None, lag,
                                                  is_use_residual,
                                                  is_train_model)

        if "_basket" in model_key:
            test_prices = find_market_prices(actual_prices[model_key], y1)

        if predictions == None:
            print "not found", model_key_base
            continue

        equity_curve, returns, rating, draw_down, avg_profit, closed_profits, replay_trades, closed_times = back_test_strategy(
            times[model_key], martingale_type, entry_bias, trade_dir,
            predictions, test_prices, 1, commission, model_key)

        if len(closed_profits) > 1:
            sharpe_all = (float(np.mean(closed_profits)) /
                          np.std(closed_profits))
        else:
            sharpe_all = 0

        from sklearn.linear_model import LinearRegression
        reg = LinearRegression().fit([[v] for v in range(len(equity_curve))],
                                     equity_curve)
        y_pred = reg.predict([[v] for v in range(len(equity_curve))])

        r2 = r2_score(equity_curve, y_pred)

        trade_logger.info(
            str(model_key_base) + " " + str(sharpe_all) + " " + str(r2) + " " +
            str(portfolio_wts))

        if sharpe_all < 0.0 and False:
            equity_curve = None
        else:
            print model_key_base, sharpe_all, r2, np.median(
                closed_profits), np.mean(closed_profits)

        whitelist.append({
            "is_use_residual": is_use_residual,
            "closed_times": closed_times,
            "closed_profits": closed_profits,
            "replay_trades": replay_trades,
            "prediction_key": model_key,
            "model_key": model_key_base,
            "wts": portfolio_wts[model_key]['wt'],
            "currency_pairs": portfolio_pairs,
            "num_trades": len(closed_profits),
            "r2_score": r2,
            "martingale_type": martingale_type,
            "draw_down": draw_down,
            "avg_profit": avg_profit,
            "equity_curve": equity_curve,
            "sharpe": sharpe_all,
            "entry_bias": entry_bias,
            "trade_dir": trade_dir
        })

    return whitelist
Ejemplo n.º 20
0
 def level10(self):
     barriers = []
     barriers.append(barrier.Barrier(80, 70, 540, 10)) 
     barriers.append(barrier.Barrier(610, 80, 10, 315)) 
     barriers.append(barrier.Barrier(80, 150, 450, 10)) 
     barriers.append(barrier.Barrier(80, 160, 10, 80)) 
     barriers.append(barrier.Barrier(90, 230, 40, 10)) 
     barriers.append(barrier.Barrier(520, 160, 10, 80)) 
     barriers.append(barrier.Barrier(230, 230, 180, 10)) 
     barriers.append(barrier.Barrier(410, 230, 10, 90)) 
     barriers.append(barrier.Barrier(80, 310, 220, 10)) 
     barriers.append(barrier.Barrier(290, 320, 10, 80)) 
     barriers.append(barrier.Barrier(80, 400, 10, 70)) 
     barriers.append(barrier.Barrier(80, 390, 100, 10)) 
     barriers.append(barrier.Barrier(300, 390, 110, 10)) 
     barriers.append(barrier.Barrier(400, 400, 10, 70)) 
     barriers.append(barrier.Barrier(420, 310, 100, 10)) 
     barriers.append(barrier.Barrier(520, 310, 10, 80)) 
     return barriers        
Ejemplo n.º 21
0
def main(processes, messages, runscript, broadcastType, logsDir, testConfig):
    # Set tc for loopback
    tc = TC(testConfig['TC'])
    print(tc)

    # Start the barrier
    initBarrier = barrier.Barrier(BARRIER_IP, BARRIER_PORT, processes)
    initBarrier.listen()
    startTimesFuture = initBarrier.startTimesFuture()

    initBarrierThread = threading.Thread(target=initBarrier.wait)
    initBarrierThread.start()

    # Start the finish signal
    finishSignal = finishedSignal.FinishedSignal(
        SIGNAL_IP, SIGNAL_PORT, processes)
    finishSignal.listen()
    finishSignalThread = threading.Thread(target=finishSignal.wait)
    finishSignalThread.start()

    if broadcastType == "fifo":
        validation = FifoBroadcastValidation(processes, messages, logsDir)
    else:
        validation = LCausalBroadcastValidation(
            processes, messages, logsDir, None)

    hostsFile, configFile = validation.generateConfig()

    try:
        # Start the processes and get their PIDs
        procs = startProcesses(processes, runscript,
                               hostsFile.name, configFile.name, logsDir)

        # Create the stress test
        st = StressTest(procs,
                        testConfig['ST']['concurrency'],
                        testConfig['ST']['attempts'],
                        testConfig['ST']['attemptsDistribution'])

        for (logicalPID, procHandle) in procs:
            print("Process with logicalPID {} has PID {}".format(
                logicalPID, procHandle.pid))

        initBarrierThread.join()
        print("All processes have been initialized.")

        st.run()
        print("StressTest is complete.")

        print("Resuming stopped processes.")
        st.continueStoppedProcesses()

        print("Waiting until all running processes have finished broadcasting.")
        finishSignalThread.join()

        for pid, startTs in OrderedDict(sorted(startTimesFuture.items())).items():
            print("Process {} finished broadcasting {} messages in {} ms".format(
                pid, messages, finishSignal.endTimestamps()[pid] - startTs))

        unterminated = st.remainingUnterminatedProcesses()
        if unterminated is not None:
            input('Hit `Enter` to terminate the remaining processes with logicalPIDs {}.'.format(
                unterminated))
            st.terminateAllProcesses()

        mutex = threading.Lock()

        def waitForProcess(logicalPID, procHandle, mutex):
            procHandle.wait()

            with mutex:
                print("Process {} exited with {}".format(
                    logicalPID, procHandle.returncode))

        # Monitor which processes have exited
        monitors = [threading.Thread(target=waitForProcess, args=(
            logicalPID, procHandle, mutex)) for (logicalPID, procHandle) in procs]
        [p.start() for p in monitors]
        [p.join() for p in monitors]

        input('Hit `Enter` to validate the output')
        print("Result of validation: {}".format(validation.checkAll()))

    finally:
        if procs is not None:
            for _, p in procs:
                p.kill()
Ejemplo n.º 22
0
def main(processes, messages_avoided, runscript, broadcastType, logsDir, testConfig):
    # Set tc for loopback
    # tc = TC(testConfig['TC'])
    # print(tc)

    # windowSize = [100, 500, 1000, 2500, 5000, 10000, 20000]
    # initThresh = [100, 500, 1000, 5000, 10000, 50000]
    messages_list = [100, 500, 1000, 3000, 5000, 10000, 15000, 30000, 100000, 1000000]
    waiting_time = [60, 60, 300, 600, 600, 600, 1200, 1200, 300, 600]

    for i, messages in enumerate(messages_list):
        tot_finishes = []
        tot_dels = []
        print("tentative:", i)
        print("Messages:", messages)
        print("Will wait:", waiting_time[i])
        # Start the barrier
        initBarrier = barrier.Barrier(BARRIER_IP, BARRIER_PORT, processes)
        initBarrier.listen()
        startTimesFuture = initBarrier.startTimesFuture()

        initBarrierThread = threading.Thread(target=initBarrier.wait)
        initBarrierThread.start()

        # Start the finish signal
        finishSignal = finishedSignal.FinishedSignal(SIGNAL_IP, SIGNAL_PORT, processes)
        finishSignal.listen()
        finishSignalThread = threading.Thread(target=finishSignal.wait)
        finishSignalThread.start()

        if broadcastType == "fifo":
            validation = FifoBroadcastValidation(processes, messages, logsDir)
        else:
            validation = LCausalBroadcastValidation(processes, messages, logsDir, None)

        hostsFile, configFile = validation.generateConfig()

        try:
            # Start the processes and get their PIDs
            procs = startProcesses(processes, runscript, hostsFile.name, configFile.name, logsDir, i)

            # Create the stress test
            st = StressTest(procs,
                            testConfig['ST']['concurrency'],
                            testConfig['ST']['attempts'],
                            testConfig['ST']['attemptsDistribution'])

            for (logicalPID, procHandle) in procs:
                print("Process with logicalPID {} has PID {}".format(logicalPID, procHandle.pid), flush=True)


            initBarrierThread.join()
            print("All processes have been initialized.", flush=True)

            #st.run()
            print("StressTest is complete.")


            print("Resuming stopped processes.")
            st.continueStoppedProcesses()

            print("Waiting until all running processes have finished broadcasting.", flush=True)
            finishSignalThread.join(1)

            if(not finishSignalThread.is_alive()):
                finishes = []
                for pid, startTs in OrderedDict(sorted(startTimesFuture.items())).items():
                    print("Process {} finished broadcasting {} messages in {} ms".format(pid, messages, finishSignal.endTimestamps()[pid] - startTs), flush=True)
                    finishes.append(finishSignal.endTimestamps()[pid] - startTs)
                avg_time = np.mean(np.array(finishes))
                print("Average time to finished broadcast: {} ms".format(avg_time), flush=True)
                tot_finishes.append(avg_time)
            else:
                tot_finishes.append(1000)
                print("Average time to finished broadcast: > 1 seconds", flush=True)


            numberDel = []
            time.sleep(waiting_time[i])
            st.terminateAllProcesses()
            time.sleep(3)
            for pid in range(1, processes+1):
                filePath = os.path.join(logsDir, str(i), 'proc{:02d}.stdout'.format(pid))
                with open(filePath) as f:
                    for line in f:
                        if "Total message delivered: " in line:
                            tot_del = line.split("Total message delivered: ")[-1].rstrip("\n").rstrip()
                            print("Process {} delivered {} messages".format(pid, tot_del), flush=True)
                            numberDel.append(int(tot_del))
                            break
            avg_del = np.mean(np.array(numberDel))
            print("Average number of delivered messages: {} ".format(avg_del), flush=True)
            tot_dels.append(avg_del)

            mutex = threading.Lock()

            def waitForProcess(logicalPID, procHandle, mutex):
                procHandle.wait()
                with mutex:
                    print("Process {} exited with {}".format(logicalPID, procHandle.returncode), flush=True)

            # Monitor which processes have exited
            monitors = [threading.Thread(target=waitForProcess, args=(logicalPID, procHandle, mutex)) for (logicalPID, procHandle) in procs]
            [p.start() for p in monitors]
            [p.join() for p in monitors]
            if procs is not None:
                for _, p in procs:
                    p.kill()

            # input('Hit `Enter` to validate the output')
            # print("Result of validation: {}".format(validation.checkAll()))
        finally:
            if procs is not None:
                for _, p in procs:
                    p.kill()
        full_avg_del = np.mean(np.array(tot_dels))
        print(full_avg_del, flush=True)
        full_avg_finishes = np.mean(np.array(tot_finishes))
        print(full_avg_finishes, flush=True)