Ejemplo n.º 1
0
    def animate(self, player):
        # simple movement script for now
        room = player.getRoom()
        playerPos = room.getPlayerPos(player)

        if self.dict['state'] == 'chase':
            self.dict['prevpos'] = (self.dict['x'], self.dict['y'])
            # TODO -- vertical upwards movement is slow for some reason
            vector = utils.direction((self.dict['x'], self.dict['y']), playerPos)
            self.dict['x'] += round(vector[0] * self.dict['stats']['speed'])
            self.dict['y'] += round(vector[1] * self.dict['stats']['speed'])

            if self.dict['x'] < 0:
                self.dict['x'] = 0
            elif self.dict['x'] > room.dict['width'] - 1:
                self.dict['x'] = room.dict['width'] - 1

            if self.dict['y'] < 0:
                self.dict['y'] = 0
            elif self.dict['y'] > room.dict['height'] - 1:
                self.dict['y'] = room.dict['height'] - 1
        elif self.dict['state'] == 'idle':
            if utils.distance(playerPos, (self.dict['x'], self.dict['y'])) <= self.dict['stats']['sensory-radius']:
                self.dict['state'] = 'chase'

        self.applyState()
Ejemplo n.º 2
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search
    #the second test prevents the player from going to coins which have been taken by the opponent
    if currentcoin == player_location or opponentLocation in coins_to_search:
        dists_matrix, routes_matrix = u.update_dists_from_each(dist_matrix, route_matrix, player_location, mazeMap, coins)

        coins_to_search = get_n_shortest(3, coins, player_location, dist_matrix)
        ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        may_be_lost_coins = []
        # Remove from coins_to_search the first coin which is closer to the opponent than to the player
        for c in coins_to_search:
            if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]:
                may_be_lost_coins.append(c)
        if len(may_be_lost_coins) != 0:
            coins_to_search.remove(may_be_lost_coins[0]) 
   
        best_weight = float("inf")
        best_path = []
        exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        meta_route = [player_location]+best_path
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[1]

    return u.direction(player_location, route.pop(0))
Ejemplo n.º 3
0
 def checkOptions(self, player, otherPos, rotSeq):
     currentPos = (self.dict['x'], self.dict['y'])
     if otherPos == currentPos:
         options = self.generateCollisionOptions(player)
         bestDirection = utils.direction((self.dict['x'], self.dict['y']), self.dict['prevpos'])
         bdpos = utils.round_vector(bestDirection)
         if utils.magnitude(bdpos) == 0:
             bdpos = rand.choice(optKeys)
         if options[bdpos] and player.getRoom().validPosition(utils.add_vector(currentPos, bdpos)):
             self.dict['x'] += bdpos[0]
             self.dict['y'] += bdpos[1]
             return True
         else:
             for rot in rotSeq:
                 testvec = utils.rotate(bdpos, rot)
                 testvec = utils.round_vector(testvec)
                 if options[testvec]:
                     self.dict['x'] += testvec[0]
                     self.dict['y'] += testvec[1]
                     return True
             # if nothing worked -- this could be expanded to do proper step-by-step
             # checking until invalid
             self.dict['x'] = self.dict['prevpos'][0]
             self.dict['y'] = self.dict['prevpos'][1]
             return True
     return False
Ejemplo n.º 4
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, currentcoin, old_coins, meta_route, meta_route_len, next_coin
    # api.debug("Update routes...")
    t = time()
    t0 = t
    # update for our location
    update_dists_from_each(dists_matrix, routes_matrix, playerLocation, mazeMap, coins)
    # update the oponent location
    try:
        update_dists_from_each(dists_matrix, routes_matrix, opponentLocation, mazeMap, coins)
    except:
        pass
    # api.debug("Time : " + str(time() - t))
    meta_route = [c for c in meta_route if c in coins]
    # api.debug("Calc init route...")
    t = time()
    if playerLocation in old_coins:
        next_coin = meta_route.pop(0)
        route = location_list_to_route([playerLocation, next_coin], routes_matrix)
    # api.debug("Time : " + str(time() - t))  

    next_move = route.pop(0)  # Discard the first element to avoid back and forth

    for _ in range(3000):
        if len(meta_route) > 2:
            meta_route_len, meta_route_len = opt_algorithm(meta_route, meta_route_len, dists_matrix)

    t_tot = time() - t0
    # api.debug("TOTAL TIME : " + str(t_tot))
    if t_tot > .1:
        api.debug("/!\OVER_SHOT : +" + str(t_tot - .1))
    old_coins = coins
    return u.direction(playerLocation, next_move)
Ejemplo n.º 5
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search, index
    if opponentLocation in coins_to_search:
        coins_to_search, meta_route, route = change_way(coins, opponentLocation, player_location)[:3]
        index = 0
    elif currentcoin == player_location: 
        if len(route) != 0:
            old_dist = algo.dijkstra(mazeMap, player_location)[1][meta_route[index+1]]
        coins_to_search2, meta_route2, route2, new_dist = change_way(coins, opponentLocation, player_location)

        #dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins)
        #coins_to_search = get_n_shortest(3, coins, player_location, dists_matrix)
    	
        #ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        #for c in coins_to_search:
            #if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]:
               # coins_to_search.remove(c)
                #break
        		
        #best_weight = float("inf")
        #best_path = []
        #exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        #meta_route2 = [player_location] + best_path
        #route2 = u.location_list_to_route(meta_route2, route_matrix)
        #new_dist = dist_matrix[player_location][meta_route2[1]]
		
        if len(route) == 0 or old_dist - new_dist > 3:
            route = route2
            meta_route = meta_route2    
            index = 0
        index += 1
        currentcoin = meta_route[index]
    #api.debug(route)
    return u.direction(player_location, route.pop(0))
Ejemplo n.º 6
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search
    #the second test prevents the player from going to coins which have been taken by the opponent
    if currentcoin == player_location or opponentLocation in coins_to_search:
        dists_matrix, routes_matrix = u.update_dists_from_each(
            dist_matrix, route_matrix, player_location, mazeMap, coins)

        coins_to_search = get_n_shortest(3, coins, player_location,
                                         dist_matrix)
        ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        may_be_lost_coins = []
        # Remove from coins_to_search the first coin which is closer to the opponent than to the player
        for c in coins_to_search:
            if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[
                    player_location][c]:
                may_be_lost_coins.append(c)
        if len(may_be_lost_coins) != 0:
            coins_to_search.remove(may_be_lost_coins[0])

        best_weight = float("inf")
        best_path = []
        exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        meta_route = [player_location] + best_path
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[1]

    return u.direction(player_location, route.pop(0))
Ejemplo n.º 7
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, old_coins, playerScore, enemyScore
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation, mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation, mazeMap, coins + [playerLocation])

    # Calculate our score and en score :
    if playerLocation in old_coins and playerLocation not in coins:
        playerScore += 1
    if playerLocation in old_coins and playerLocation not in coins:
        enemyScore += 1

    if len(route) == 0 or route[-1] not in coins:
        winning_value, next_coin, en_best_path, pl_best_path = minmax(coins, playerScore, 0, playerLocation, enemyScore, 0, opponentLocation, 0)
        interface.debug(pl_best_path)
        interface.debug(en_best_path)
        interface.debug("going to : " + str(next_coin) + " with a probable score of : " + str(winning_value))
        route = route_matrix[playerLocation][next_coin]

    coins_which_are_close = coins_close(playerLocation, coins, dists_matrix, limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        pass
        # route = route_matrix[playerLocation][closest_coin]
        # interface.debug("NEAR : " + str(closest_coin))

    old_coins = coins
    next_pos = route.pop(0)
    # input()
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 8
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, coins_to_get
    # First we update our dists and routes matrix :
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation, mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation, mazeMap, coins)

    if len(route) == 0 or route[-1] not in coins:
        coins_to_get = [c for c in coins_to_get if c in coins]
        sorted(coins_to_get, key=lambda x: dists_matrix[x][playerLocation], reverse=False)
        # probabilities = closer_probability(playerLocation, opponentLocation, coins_to_get, dists_matrix)
        # next_coin = which_coin_is_next(coins_to_get, playerLocation, opponentLocation)

        # Il y a des pièces que l'on pourrait récuperer maintenant ?
        # Une sorte de système de packets
        coins_which_are_close = coins_close(playerLocation, coins, dists_matrix)
        closest_coin = None
        for coin in coins_which_are_close:
            # If we don't already go there, and the enemy is not going there
            if coin not in coins_to_get and dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, we should check that with the enemy
                if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                    closest_coin = coin
        if closest_coin is not None:
            coins_to_get.insert(0, closest_coin)
            interface.debug("Finally go to : " + str(closest_coin))

        try:
            next_coin = coins_to_get.pop(0)
        except:
            interface.debug("PHASE III")
            # Si on a plus rien, on fait un voyageur de commerce sur les pieces qui restent
            sorted(coins, key=lambda coin: dists_matrix[playerLocation][coin])
            coins_to_get = coins[:7]
            coins_to_get = voyageur_commerce(playerLocation, coins_to_get, dists_matrix)
            next_coin = coins_to_get.pop(0)

        interface.debug(coins_to_get)
        # coins_to_get.remove(next_coin)

        route = route_matrix[playerLocation][next_coin]

    # In case we pass not far from a coin :
    # Il y a des pièces que l'on pourrait récuperer maintenant ?
    # Une sorte de système de packets
    coins_which_are_close = coins_close(playerLocation, coins, dists_matrix, limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if coin not in coins_to_get and dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        coins_to_get.insert(0, route[-1])
        route = route_matrix[playerLocation][closest_coin]
        interface.debug("Finally go to : " + str(closest_coin))

    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 9
0
def determineNextMove(playerLocation, opponentLocation, coins):
    """Function called at each turn, must return the next move of the player"""
    global packages, route_table, best_path, best_weight, route
    if len(best_path) == 0:
        current_package = packages.pop(0)
        exhaustive(current_package, playerLocation, [], 0,
                   (route_table, dists))
        api.debug(best_path)
    return u.direction(playerLocation, best_path.pop(0))
Ejemplo n.º 10
0
def move():
    data = bottle.request.json

    game_id, grid, food, charlie, enemies = init(data)

    # TODO - thhe print grid function does not work on rectangles
    # print_grid(grid, game_id)

    # # Only check eat kills if length greater than 3
    if charlie['length'] >= 3 and charlie['health'] >= 20:
        path = eat_snake(charlie, enemies, grid)
        if path:
            # log('eat snake path ', path)
            return move_response(direction(path[0], path[1]))

    # # Only check wall kills if length greater than 3
    if charlie['length'] >= 3 and charlie['health'] >= 20:
        path = wall_kill(charlie, enemies, grid)
        if path:
            # log('wall kill path ', path)
            return move_response(direction(path[0], path[1]))

    # Eat food when length or health below threshhold
    if len(enemies) >= 2 or charlie['length'] <= 30 or charlie['health'] <= 60:
        path = eat_food(charlie, grid, food)
        if path:
            # log('eat path ', path)
            return move_response(direction(path[0], path[1]))

    # # if our length is greater than threshold and no other path was available
    if charlie['length'] >= 3:
        path = find_my_tail(charlie, grid)
        if path:
            # log('find my tail path ', path)
            return move_response(direction(path[0], path[1]))

    # # if no path available to tail check if there is an enemies available
    if not path:
        path = find_enemy_tail(charlie, enemies, grid)
        if path:
            # log('find enemy tail path ', path)
            return move_response(direction(path[0], path[1]))

    # # if our length is greater than threshold and no other path was available
    if charlie['length'] >= 3:
        path = find_my_tail_emergency(charlie, grid)
        if path:
            # log('find my tail emergency path ', path)
            return move_response(direction(path[0], path[1]))

    # Choose a random free space if no available enemy tail
    if not path:
        path = trouble(charlie, grid)
        if path:
            # log('trouble path ', path)
            return move_response(direction(path[0], path[1]))
Ejemplo n.º 11
0
    def play_to_init_buffer(self):
        for i_episode in range(self.model.batch_size):
            #reset env
            state = env.reset(obs_as_dict=False)

            #noise decay
            self.noise = self.noise * self.noise_decay

            #epsilon decay
            self.epsilon = self.epsilon * self.epsilon_decay

            noise = np.zeros([1, self.model.env.action_space.shape[0]])

            action_with_noise = np.zeros(
                [1, self.model.env.action_space.shape[0]])

            for i_step in itertools.count():
                state = [state]
                action_original = self.model.Actor.actor_model.predict(
                    np.asarray(state))

                #action for training with noise
                action_with_noise[0] = np.zeros(
                    [1, self.model.env.action_space.shape[0]])

                #We use epsilon decay to decide wether we add noise to action or not
                s = np.random.binomial(1, self.epsilon)
                if s == 1:
                    #create noise by Gaussian distribution
                    noise[0] = np.random.randn(
                        self.model.env.action_space.shape[0]) * self.noise
                action_with_noise = action_original + noise

                #execute action action_with_noise and observe reward r_t and s_t+1
                next_state, reward, done, info = self.model.env.step(
                    action_with_noise[0], obs_as_dict=False)
                reward = -reward
                if done:
                    if utils.direction(next_state) == self.direction:
                        reward += 10
                    else:
                        reward -= 10

                self.model.memory_buffer.memorize(
                    [state, action_with_noise, reward, next_state, done])

                if done:
                    break
                else:
                    state = next_state
Ejemplo n.º 12
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, old_coins, playerScore, enemyScore
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation,
                             mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation,
                             mazeMap, coins + [playerLocation])

    # Calculate our score and en score :
    if playerLocation in old_coins and playerLocation not in coins:
        playerScore += 1
    if playerLocation in old_coins and playerLocation not in coins:
        enemyScore += 1

    if len(route) == 0 or route[-1] not in coins:
        winning_value, next_coin, en_best_path, pl_best_path = minmax(
            coins, playerScore, 0, playerLocation, enemyScore, 0,
            opponentLocation, 0)
        interface.debug(pl_best_path)
        interface.debug(en_best_path)
        interface.debug("going to : " + str(next_coin) +
                        " with a probable score of : " + str(winning_value))
        route = route_matrix[playerLocation][next_coin]

    coins_which_are_close = coins_close(playerLocation,
                                        coins,
                                        dists_matrix,
                                        limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if dists_matrix[playerLocation][coin] < dists_matrix[opponentLocation][
                coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][
                    coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        pass
        # route = route_matrix[playerLocation][closest_coin]
        # interface.debug("NEAR : " + str(closest_coin))

    old_coins = coins
    next_pos = route.pop(0)
    # input()
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 13
0
def determineNextMove(playerLocation, opponentLocation, coins):
    
    global route, currentcoin, acc
    u.update_dists_from_each(dist_matrix, route_matrix, playerLocation, mazeMap, coins)
    if currentcoin == playerLocation:
        best_weight = float("inf")
        best_path = []
        coins_to_search = get_n_shortest(7, coins, playerLocation, dist_matrix)
        if playerLocation in coin_to_search:
            api.debug(coin_to_search)
            api.debug(playerLocation)
        meta_route = exhaustive(coins_to_search, playerLocation, [] ,0 ,dist_matrix)
        route = u.location_list_to_route(meta_route, route_matrix)
        currentcoin = meta_route[0]
        #if currentcoin == playerLocation:
            #api.debug(meta_route)
            #api.debug(route)
            #acc+=1
            #api.debug(acc)
    return u.direction(playerLocation, route.pop(0))
Ejemplo n.º 14
0
def determineNextMove(player_location, opponentLocation, coins):
    """Return the next direction"""
    global route, currentcoin, meta_route, best_weight, best_path, coins_to_search, index
    if opponentLocation in coins_to_search:
        coins_to_search, meta_route, route = change_way(
            coins, opponentLocation, player_location)[:3]
        index = 0
    elif currentcoin == player_location:
        if len(route) != 0:
            old_dist = algo.dijkstra(mazeMap,
                                     player_location)[1][meta_route[index + 1]]
        coins_to_search2, meta_route2, route2, new_dist = change_way(
            coins, opponentLocation, player_location)

        #dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins)
        #coins_to_search = get_n_shortest(3, coins, player_location, dists_matrix)

        #ennemy_dists = algo.dijkstra(mazeMap, opponentLocation)
        #for c in coins_to_search:
        #if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]:
        # coins_to_search.remove(c)
        #break

        #best_weight = float("inf")
        #best_path = []
        #exhaustive(coins_to_search, player_location, [], 0, dist_matrix)
        #meta_route2 = [player_location] + best_path
        #route2 = u.location_list_to_route(meta_route2, route_matrix)
        #new_dist = dist_matrix[player_location][meta_route2[1]]

        if len(route) == 0 or old_dist - new_dist > 3:
            route = route2
            meta_route = meta_route2
            index = 0
        index += 1
        currentcoin = meta_route[index]
    #api.debug(route)
    return u.direction(player_location, route.pop(0))
Ejemplo n.º 15
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, currentcoin, old_coins, meta_route, meta_route_len, next_coin
    # api.debug("Update routes...")
    t = time()
    t0 = t
    # update for our location
    update_dists_from_each(dists_matrix, routes_matrix, playerLocation,
                           mazeMap, coins)
    # update the oponent location
    try:
        update_dists_from_each(dists_matrix, routes_matrix, opponentLocation,
                               mazeMap, coins)
    except:
        pass
    # api.debug("Time : " + str(time() - t))
    meta_route = [c for c in meta_route if c in coins]
    # api.debug("Calc init route...")
    t = time()
    if playerLocation in old_coins:
        next_coin = meta_route.pop(0)
        route = location_list_to_route([playerLocation, next_coin],
                                       routes_matrix)
    # api.debug("Time : " + str(time() - t))

    next_move = route.pop(
        0)  # Discard the first element to avoid back and forth

    for _ in range(3000):
        if len(meta_route) > 2:
            meta_route_len, meta_route_len = opt_algorithm(
                meta_route, meta_route_len, dists_matrix)

    t_tot = time() - t0
    # api.debug("TOTAL TIME : " + str(t_tot))
    if t_tot > .1:
        api.debug("/!\OVER_SHOT : +" + str(t_tot - .1))
    old_coins = coins
    return u.direction(playerLocation, next_move)
Ejemplo n.º 16
0
def determineNextMove(mazeWidth, mazeHeight, mazeMap, timeAllowed,
                      playerLocation, opponentLocation, coins):
    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 17
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route
    if len(route) == 0:
        pick_good_coin(playerLocation, coins, coins_dists)
    return u.direction(playerLocation, route.pop(0))
Ejemplo n.º 18
0
        else:
            points.append(None)

    # Draw Skeleton
    for pair in POSE_PAIRS:
        partA = pair[0]
        partB = pair[1]

        if points[partA] and points[partB]:

            #calculate diretction of bodypart
            p1 = points[partA]
            p2 = points[partB]
            vector = points_to_vector(p1, p2)
            if vector is not None:
                theta = abs(direction(vector))

            if (pair == [1, 8] or pair == [1, 11]) and theta < 45:
                cv2.line(frame,
                         points[partA],
                         points[partB], (0, 0, 255),
                         2,
                         lineType=cv2.LINE_AA)
                cv2.putText(frame,
                            "PERSONEN ER FALDET", (50, 50),
                            cv2.FONT_HERSHEY_COMPLEX,
                            1, (0, 0, 255),
                            2,
                            lineType=cv2.LINE_AA)
            else:
                cv2.line(frame,
Ejemplo n.º 19
0
                                          cThr=[150, 175],
                                          showCanny=False,
                                          filter=4,
                                          draw=False)
    if len(conts) != 0:
        biggest = conts[0][2]
        imgWarp = utils.warpImg(img, biggest, 350,
                                350)  #warping the Image to Region of Interest

        imgCont2, cont2, imgThre = utils.getContours(imgWarp,
                                                     cThr=[50, 50],
                                                     showCanny=False,
                                                     filter=7,
                                                     draw=False)
        if len(cont2) != 0:
            c = utils.direction(imgWarp)
            cv2.imshow("cut", imgThre)
            if c == 1:
                print("Right")
                pub2.publish('Right')
                cv2.putText(img, "RIGHT", (40, 40), cv2.FONT_HERSHEY_SIMPLEX,
                            1, (0, 255, 0), 3)
            elif c == 2:
                print("left")
                pub2.publish('left')
                cv2.putText(img, "LEFT", (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1,
                            (0, 255, 0), 3)
            elif c == 3:
                print("up")
                pub2.publish('up')
                cv2.putText(img, "UP", (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1,
Ejemplo n.º 20
0
    def DDPG(self, model_name_prefix):
        scores = []
        for i_episode in range(self.episode_start, self.num_episodes):
            one_episode_score = 0
            #write log of training
            name = "./log/training.txt"
            with open(name, 'a') as f:
                f.write("Episode {}/{} \n".format(i_episode + 1,
                                                  self.num_episodes))
            f.close()

            if (i_episode + 1) % 100 == 0:
                avg = np.mean(np.asarray(scores))
                if (i_episode + 1) % 1000 == 0:
                    prefixe = "./checkpoints/"
                    self.model.Actor.save(prefixe=prefixe +
                                          "checkpoint_avgScore_{}".format(avg))
                    self.model.Critic.save(
                        prefixe=prefixe + "checkpoint_avgScore_{}".format(avg))
                print(
                    "Episode {}/{} : Average score in 100 latest episodes : {}"
                    .format(i_episode + 1, self.num_episodes, avg))
                scores.clear()

            #reset env
            state = env.reset(obs_as_dict=False)

            #noise decay
            self.noise *= self.noise_decay

            #epsilon decay
            self.epsilon *= self.epsilon_decay

            noise = np.zeros([1, self.model.env.action_space.shape[0]])

            action_with_noise = np.zeros(
                [1, self.model.env.action_space.shape[0]])

            for i_step in itertools.count():
                state = [state]

                action_original = self.model.Actor.actor_model.predict(
                    np.asarray(state))

                #action for training with noise
                action_with_noise[0] = np.zeros(
                    [1, self.model.env.action_space.shape[0]])

                #We use epsilon decay to decide wether we add noise to action or not
                s = np.random.binomial(1, self.epsilon)
                if s == 1:
                    #create noise by Gaussian distribution
                    noise[0] = np.random.randn(
                        self.model.env.action_space.shape[0]) * self.noise
                action_with_noise = action_original + noise

                #execute action action_with_noise and observe reward r_t and s_t+1
                next_state, reward, done, info = self.model.env.step(
                    action_with_noise[0], obs_as_dict=False)
                reward = -reward
                if done:
                    if utils.direction(next_state) == self.direction:
                        reward += 10
                    else:
                        reward -= 10
                one_episode_score += reward
                self.model.memory_buffer.memorize(
                    [state, action_with_noise, reward, next_state, done])
                self.experience_replay()

                if done:
                    print("Episode {} ->>> Scores : {}".format(
                        i_episode, one_episode_score))
                    scores.append(one_episode_score)
                    # write reward for tensorboard
                    summary = self.model.sess.run(
                        self.tf_reward_summary,
                        feed_dict={self.tf_reward: one_episode_score})
                    # add summary to writer
                    self.writer.add_summary(summary, i_episode)
                    break
                else:
                    state = next_state
            name = "./log/training.txt"
            with open(name, 'a') as f:
                f.write("Total score : {} \n".format(one_episode_score))
            f.close()

        #save information to log
        name = "./log/data.txt"
        with open(name, 'a') as f:
            f.write(" ".join((self.num_episodes, model_name_prefix, self.noise,
                              self.epsilon)))
        f.close()
        print("Log saved successfully! \n")
        #save memory buffer
        self.model.memory_buffer.save()
        print("Memory buffer saved successfully \n")
        #save model
        model.save("./models/{}_{}".format(args.direction, args.episodes))
        print("Models saved successfully ! \n")
Ejemplo n.º 21
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, coins_to_get
    # First we update our dists and routes matrix :
    u.update_dists_from_each(dists_matrix, route_matrix, playerLocation,
                             mazeMap, coins)
    u.update_dists_from_each(dists_matrix, route_matrix, opponentLocation,
                             mazeMap, coins)

    if len(route) == 0 or route[-1] not in coins:
        coins_to_get = [c for c in coins_to_get if c in coins]
        sorted(coins_to_get,
               key=lambda x: dists_matrix[x][playerLocation],
               reverse=False)
        # probabilities = closer_probability(playerLocation, opponentLocation, coins_to_get, dists_matrix)
        # next_coin = which_coin_is_next(coins_to_get, playerLocation, opponentLocation)

        # Il y a des pièces que l'on pourrait récuperer maintenant ?
        # Une sorte de système de packets
        coins_which_are_close = coins_close(playerLocation, coins,
                                            dists_matrix)
        closest_coin = None
        for coin in coins_which_are_close:
            # If we don't already go there, and the enemy is not going there
            if coin not in coins_to_get and dists_matrix[playerLocation][
                    coin] < dists_matrix[opponentLocation][
                        coin]:  # TODO : magic number, we should check that with the enemy
                if closest_coin is None or dists_matrix[playerLocation][
                        coin] < dists_matrix[playerLocation][closest_coin]:
                    closest_coin = coin
        if closest_coin is not None:
            coins_to_get.insert(0, closest_coin)
            interface.debug("Finally go to : " + str(closest_coin))

        try:
            next_coin = coins_to_get.pop(0)
        except:
            interface.debug("PHASE III")
            # Si on a plus rien, on fait un voyageur de commerce sur les pieces qui restent
            sorted(coins, key=lambda coin: dists_matrix[playerLocation][coin])
            coins_to_get = coins[:7]
            coins_to_get = voyageur_commerce(playerLocation, coins_to_get,
                                             dists_matrix)
            next_coin = coins_to_get.pop(0)

        interface.debug(coins_to_get)
        # coins_to_get.remove(next_coin)

        route = route_matrix[playerLocation][next_coin]

    # In case we pass not far from a coin :
    # Il y a des pièces que l'on pourrait récuperer maintenant ?
    # Une sorte de système de packets
    coins_which_are_close = coins_close(playerLocation,
                                        coins,
                                        dists_matrix,
                                        limit=2)
    closest_coin = None
    for coin in coins_which_are_close:
        # If we don't already go there, and the enemy is not going there
        if coin not in coins_to_get and dists_matrix[playerLocation][
                coin] < dists_matrix[opponentLocation][
                    coin]:  # TODO : magic number, same : with the enemy
            if closest_coin is None or dists_matrix[playerLocation][
                    coin] < dists_matrix[playerLocation][closest_coin]:
                closest_coin = coin
    if closest_coin is not None:
        coins_to_get.insert(0, route[-1])
        route = route_matrix[playerLocation][closest_coin]
        interface.debug("Finally go to : " + str(closest_coin))

    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 22
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, currentcoin
    if coinsNumber != len(coins):
        routingTable = algo.dijkstra(mazeMap, playerLocation)
        route = u.way_width(routingTable, playerLocation, coins[0])
    return u.direction(playerLocation, route.pop(0))
Ejemplo n.º 23
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route
    if len(route) == 0:
        pick_good_coin(playerLocation, coins, coins_dists)
    return u.direction(playerLocation, route.pop(0))
Ejemplo n.º 24
0
def determineNextMove(mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) :
    next_pos = route.pop(0)
    return u.direction(playerLocation, next_pos)
Ejemplo n.º 25
0
        points.append((int(x), int(y)))
    else:
        points.append(None)

# Draw Skeleton
for pair in POSE_PAIRS:
    partA = pair[0]
    partB = pair[1]
    print(partA, partB)
    if points[partA] and points[partB]:

        #calculate diretction of bodypart
        p1 = points[partA]
        p2 = points[partB]
        vector = points_to_vector(p1, p2)
        theta = int(abs(direction(vector)))
        print(theta)

        cv2.circle(frame,
                   points[partA],
                   8, (0, 255, 255),
                   thickness=-1,
                   lineType=cv2.FILLED)
        if (pair == [1, 8] or pair == [1, 11]) and theta < 45:
            cv2.line(frame, points[partA], points[partB], (0, 0, 255), 2)
        else:
            cv2.line(frame, points[partA], points[partB], (0, 255, 0), 2)

cv2.imshow('Output-Keypoints', frameCopy)
cv2.imshow('Output-Skeleton', frame)
Ejemplo n.º 26
0
def determineNextMove(playerLocation, opponentLocation, coins):
    global route, currentcoin
    if coinsNumber != len(coins):
        routingTable = algo.dijkstra(mazeMap, playerLocation)
        route = u.way_width(routingTable, playerLocation, coins[0])
    return u.direction(playerLocation, route.pop(0))