def test_add_bite(self):
     table_name = "volleyball_single"
     table_col = 0
     data = {'table': table_name, 'subject_col_id': table_col, 'slice': 0, 'total': 1, 'technique': 'found-majority'}
     Bite.delete().execute()  # delete all Bites
     Apple.delete().execute()  # delete all Apples
     result = self.app.post('/add', data=data, content_type='multipart/form-data')
     self.assertIn('apple', result.json)
     apple_id = result.json['apple']
     self.assertEqual(result.status_code, 200, msg=result.data)
     database.connect(reuse_if_open=True)
     self.assertEqual(len(Bite.select()), 1)
     sleep(sleep_time)
     result = self.app.get('/status')
     self.assertEqual(result.status_code, 200, msg=result.data)
     self.assertTrue(result.is_json)
     j = {
         "apples": [
             {
                 "apple": table_name,
                 "status": STATUS_COMPLETE,
                 "complete": True,
                 "agreement": 1.0,
                 "elected": 0,
             }
         ]
     }
     self.assertEqual(elect(apple_id, 'majority'), None)  # just for the coverage
     self.assertEqual(elect(apple_id, 'found-majority'), None)  # just for the coverage
示例#2
0
def test(times = 10):
    model = load_model('my_model.h5')
    s = []
    for i in range(times):
        print(i)
        pygame.event.pump()
        failed = False
        lead_x = 70
        lead_y = 70
        snake = NeuralNetwork_Snake(showScreen, screenx, screeny, snakeImg, lead_x, lead_y)
        apple = Apple(showScreen, screenx, screeny, b_size, appleImg, snake.snake_list)
        while not failed:
            apple_x, apple_y = apple.apple_pos()
            snake.update_snake_list(apple_x, apple_y)
            if snake.is_alive() is False:
                failed = True
                s.append(snake.snake_length)
            showScreen.fill(white)
            if snake.eaten is True:
                apple.update_apple_pos(snake.snake_list)
            apple_x, apple_y = apple.apple_pos()
            allPredictions = {}
            for act in moves:
               state = snake.state([apple_x, apple_y], act)
               allPredictions[act] = model.predict(np.array(state).reshape(-1, 5))[0][0]
            act = max(allPredictions, key=allPredictions.get)
            snake.set_direction(act)

            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            time.tick(FPS)
    print("Avg is: {}".format(sum(s)/len(s)))
 def test_add_multiple_bite(self):
     table_name = "volleyball_double"
     table_col = 0
     data = {'table': table_name, 'subject_col_id': table_col, 'slice': 0, 'total': 2, 'technique': 'majority'}
     Bite.delete().execute()  # delete all Bites
     Apple.delete().execute()  # delete all Apples
     result = self.app.post('/add', data=data, content_type='multipart/form-data')
     self.assertEqual(result.status_code, 200, msg=result.data)
     database.connect(reuse_if_open=True)
     self.assertEqual(len(Bite.select()), 1)
     sleep(sleep_time)
     result = self.app.get('/status')
     self.assertEqual(result.status_code, 200, msg=result.data)
     self.assertTrue(result.is_json)
     j = {
         "apples": [
             {
                 "apple": table_name,
                 "status": STATUS_NEW,
                 "complete": False,
                 "elected": None,
                 "agreement": None,
             }
         ]
     }
     self.assertDictEqual(result.get_json(), j)
     data = {'table': table_name, 'subject_col_id': table_col, 'slice': 1, 'total': 2, 'technique': 'majority'}
     result = self.app.post('/add', data=data, content_type='multipart/form-data')
     self.assertEqual(result.status_code, 200, msg=result.data)
     database.connect(reuse_if_open=True)
     self.assertEqual(len(Bite.select()), 2)
     sleep(sleep_time)
     result = self.app.get('/status')
     self.assertEqual(result.status_code, 200, msg=result.data)
     self.assertTrue(result.is_json)
     j = {
         "apples": [
             {
                 "apple": table_name,
                 "status": STATUS_COMPLETE,
                 "complete": True,
                 "agreement": 1.0,
                 "elected": table_col,
             }
         ]
     }
示例#4
0
def add_bite():
    table_name = request.values.get('table').strip()
    column = int(request.values.get('subject_col_id'))
    # if 'subject_col_id' in request.values:
    #     column = int(request.values.get('subject_col_id'))
    # else:
    #     column = int(request.values.get('col_id'))
    slice = int(request.values.get('slice'))
    tot = int(request.values.get('total'))  # total number of slices

    agg_technique = request.values.get('technique')  # aggregation technique
    if agg_technique not in AGGREGATION_TECHNIQUES:
        return jsonify(error="Invalid aggregation technique"), 400

    apples = Apple.select().where(Apple.table==table_name)
    if len(apples) == 0:
        logger.log(LOG_LVL, "\nNew apple: table=%s, columns=%d, slice=%d ,total=%d" % (table_name, column, slice, tot))
        try:
            apple = Apple(table=table_name, total=tot)
            apple.save()
        except DatabaseError:
            logger.log(LOG_LVL, "\nCatch existing: table=%s, columns=%d, slice=%d ,total=%d" % (table_name, column, slice, tot))
            apple = apples[0]
    else:
        apple = apples[0]
        logger.log(LOG_LVL, "\nExisting apple: table=%s, columns=%d, slice=%d ,total=%d" % (table_name, column, slice, tot))

    if apple.complete:
        return jsonify(error="The apple is already complete, your request will not be processed"), 400

    b = Bite(apple=apple, slice=slice, col_id=column)
    b.save()

    slices = []
    for bite in apple.bites:
        slices.append(bite.slice)
    if sorted(slices) == range(apple.total):
        apple.complete = True
        apple.save()

    if apple.complete:
        # if app.testing:
        #     combine_graphs(apple.id)
        # else:
        #     g.db.close()
        #     p = Process(target=combine_graphs, args=(apple.id,))
        #     p.start()
        g.db.close()
        p = Process(target=elect, args=(apple.id, agg_technique))
        p.start()
    return jsonify({"apple": apple.id, "bite": b.id})
示例#5
0
def get_graph():
    apple_id = request.values.get('id')
    #apple_id = int(apple_id)
    apples = Apple.select().where(Apple.id==apple_id)
    print("apples: ")
    print(apples)
    print("len: ")
    print(len(apples))
    if len(apples) == 1:
        apple = apples[0]
        fname = apple.fname
        if fname != "":
            return send_from_directory(UPLOAD_DIR, fname, as_attachment=True)
    abort(404)
示例#6
0
def trainSnake(times = 40000):
    train_data = []
    for i in range(times):
        print(i)
        pygame.event.pump()
        failed = False
        lead_x = 70
        lead_y = 70
        snake = NeuralNetwork_Snake(showScreen, screenx, screeny, snakeImg, lead_x, lead_y)
        apple = Apple(showScreen, screenx, screeny, b_size, appleImg, snake.snake_list)
        apple_x, apple_y = apple.apple_pos()
        act = "up"
        state = snake.state([apple_x, apple_y], act)
        former_distance = snake.distance([apple_x, apple_y])
        while not failed:
            apple_x, apple_y = apple.apple_pos()
            snake.update_snake_list(apple_x, apple_y)
            distance = snake.distance([apple_x, apple_y])
            score = 0
            if snake.is_alive() is False:
                failed = True
                score = -1
            showScreen.fill(white)
            if snake.eaten is True:  apple.update_apple_pos(snake.snake_list)
            if snake.eaten is True or  distance < former_distance: score = 1
            train_data.append([np.array(state), score])
            act = random.choice(moves)
            apple_x, apple_y = apple.apple_pos()
            former_distance = snake.distance([apple_x, apple_y])
            state = snake.state([apple_x, apple_y], act)
            snake.set_direction(act)
            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            time.tick(FPS)

    print(len(train_data))
    f = open("train_data.txt", "wb")
    pickle.dump(train_data, f)
    f.close()
示例#7
0
def add_bite():
    if request.method == 'GET':
        return render_template('combine.html')
    table_name = request.values.get('table').strip()
    column = int(request.values.get('column'))
    slice = int(request.values.get('slice'))
    m = int(request.values.get('m'))
    tot = int(request.values.get('total'))  # total number of slices

    uploaded_file = request.files['graph']

    apples = Apple.select().where(Apple.table==table_name, Apple.column==column)
    if len(apples) == 0:
        logger.log(LOG_LVL, "\nNew apple: table=%s, columns=%d, slice=%d ,total=%d" % (table_name, column, slice, tot))
        apple = Apple(table=table_name, column=column, total=tot)
        apple.save()
    else:
        apple = apples[0]
        logger.log(LOG_LVL, "\nExisting apple: table=%s, columns=%d, slice=%d ,total=%d" % (table_name, column, slice, tot))

    if apple.complete:
        return jsonify(error="The apple is already complete, your request will not be processed"), 400

    b = Bite(apple=apple, slice=slice, m=m)
    b.save()
    fname = "%d-%s-%d-%d.json" % (b.id, b.apple.table.replace(" ", "_"), b.apple.column, b.slice)
    uploaded_file.save(os.path.join(UPLOAD_DIR, fname))
    b.fname = fname
    b.save()

    slices = []
    for bite in apple.bites:
        slices.append(bite.slice)
    if sorted(slices) == range(apple.total):
        apple.complete = True
        apple.save()

    if apple.complete:
        if app.testing:
            combine_graphs(apple.id)
        else:
            g.db.close()
            p = Process(target=combine_graphs, args=(apple.id,))
            p.start()
    return jsonify({"apple": apple.id, "bite": b.id})
示例#8
0
def one_game():
    pygame.event.pump()
    game_over = False
    lead_x = 150
    lead_y = 150
    snake = Snake(showScreen, screenx, screeny, snakeImg, lead_x, lead_y)
    apple = Apple(showScreen, screenx, screeny, b_size, appleImg, snake.snake_list)

    while not game_over:
        x, y = apple.get_apple_pos()
        snake.update_snake_list(x, y)

        if snake.is_alive() is False: game_over = True
        showScreen.fill(white)
        if snake.eaten is True: apple.update_apple_pos(snake.snake_list)

        apple.display()
        snake.eaten = False
        snake.display()
        snake.display_score()
        pygame.display.update()

        a_x, a_y = apple.get_apple_pos()
        s_x, s_y = snake.get_snake_head()

        visited = snake.snake_list.copy()
        visited.remove([s_x, s_y])
        result = BreathFirstSearch(screenx, screeny, b_size, visited, [a_x, a_y], [s_x, s_y])

        next_cell = result[1]

        x_diff = next_cell[0] - s_x
        y_diff = next_cell[1] - s_y

        if x_diff > 0:  snake.direction = "right"
        elif x_diff < 0:  snake.direction = "left"
        elif y_diff > 0:  snake.direction = "down"
        elif y_diff < 0:  snake.direction = "up"

        time.tick(FPS)
示例#9
0
def elect(apple_id, technique):
    """
    :param apple_id:
    :return: most_frequent, agreement (how many agreed on this).
    """
    database = get_database()
    database.connect(reuse_if_open=True)
    apples = Apple.select().where(Apple.id==apple_id)
    if len(apples) == 0:
        logger.error("Apple with id: %s does not exists" % str(apple_id))
        database.close()
        return
        # return None, None
    apple = apples[0]
    apple.status = STATUS_PROCESSING
    apple.save()
    try:
        col_ids = [b.col_id for b in apple.bites]
        # c = Counter(col_ids)
        # sorted_cols_counts = sorted(c.items(), key=lambda item: item[1])
        # most_frequent_col_id = sorted_cols_counts[-1][0]
        # agreement = sorted_cols_counts[-1][1]*1.0/len(col_ids)
        if technique =="majority":
            elected_col_id, agreement = majority_aggregation(col_ids)
        elif technique == "found-majority":
            elected_col_id, agreement = found_majority_aggregation(col_ids)
        else:
            logger.error("unvalid technique: <%s>" % technique)
            apple.status = STATUS_STOPPED
            apple.save()
            return
        apple.elected = elected_col_id
        apple.agreement = agreement
        apple.status = STATUS_COMPLETE
        apple.save()
    except Exception as e:
        logger.error(str(e))
        apple.status = STATUS_STOPPED
        apple.save()
    database.close()
示例#10
0
def combine_graphs(apple_id):
    database = get_database()
    database.connect(reuse_if_open=True)
    apple = Apple.select().where(Apple.id==apple_id)[0]
    apple.status = STATUS_PROCESSING
    apple.save()
    graphs = []
    for bite in apple.bites:
        print("bite: %d" % bite.id)
        f = open(os.path.join(UPLOAD_DIR, bite.fname))
        j = json.load(f)
        graphs.append(j)
        f.close()
    graph = merge_graphs(graphs=graphs)
    fname = "%d-%s-%d-merged.json" % (apple.id, apple.table.replace(" ", "_"), apple.column)
    f = open(os.path.join(UPLOAD_DIR, fname), "w")
    f.write(json.dumps(graph))
    f.close()
    apple.fname = fname
    apple.status = STATUS_COMPLETE
    apple.save()
    database.close()
示例#11
0
def one_game():
    pygame.event.pump()
    game_over = False

    # snake will start in the middle of the game window
    lead_x = 70
    lead_y = 70

    # snake default direction is right
    snake = Snake(gameDisplay, display_width, display_height, img, lead_x,
                  lead_y)
    apple = Apple(gameDisplay, display_width, display_height, block_size, img2,
                  snake.snake_list)

    while not game_over:

        # based on the direction, we can work out the x, y changes to update the snake
        x, y = apple.get_apple_pos()
        snake.update_snake_list(x, y)

        # check if snake dies
        if snake.is_alive() is False:
            game_over = True

        gameDisplay.fill(white)

        # if snake eats the apple, make a random new apple
        if snake.eaten is True:
            apple.update_apple_pos(snake.snake_list)

        apple.display()
        snake.eaten = False
        snake.display()
        snake.display_score()
        pygame.display.update()

        # this part is using the snake position and apple
        # position to use the A* method to get the path
        a_x, a_y = apple.get_apple_pos()
        s_x, s_y = snake.get_snake_head()
        visited = snake.snake_list.copy()
        visited.remove([s_x, s_y])
        result = A_star(display_width, display_height, block_size, visited,
                        (a_x, a_y), (s_x, s_y))

        # since the path starts from snake position, the second element will
        # be next move
        next_cell = result[1]

        # update the snake position based on the next move position
        x_diff = next_cell[0] - s_x
        y_diff = next_cell[1] - s_y
        if x_diff > 0:
            snake.direction = "right"
        elif x_diff < 0:
            snake.direction = "left"
        elif y_diff > 0:
            snake.direction = "down"
        elif y_diff < 0:
            snake.direction = "up"

        clock.tick(FPS)
示例#12
0
def testing_game(times=10):
    # load the trained NN model
    model = load_model('my_model.h5')

    # s list will store the score of each game
    s = []
    for i in range(times):
        # print out the game number
        print(i)

        pygame.event.pump()
        game_over = False

        # Will be the leader of the #1 block of the snake
        lead_x = 70
        lead_y = 70

        # snake default direction is right
        snake = NN_Snake(gameDisplay, display_width, display_height, img,
                         lead_x, lead_y)
        apple = Apple(gameDisplay, display_width, display_height, block_size,
                      img2, snake.snake_list)

        a_x, a_y = apple.get_apple_pos()

        # get the initial state, and action will be "up" starting
        action = "up"
        state = snake.get_state([a_x, a_y], action)
        old_distance = snake.get_distance([a_x, a_y])

        while not game_over:

            # based on the direction, we can work out the x, y changes to update the snake
            a_x, a_y = apple.get_apple_pos()
            snake.update_snake_list(a_x, a_y)

            # check if snake dies
            if snake.is_alive() is False:
                game_over = True
                s.append(snake.snake_length)

            gameDisplay.fill(white)

            # if snake eats the apple, make a random new apple
            if snake.eaten is True:
                apple.update_apple_pos(snake.snake_list)

            #############################################
            # get the position of the apple
            a_x, a_y = apple.get_apple_pos()

            # use NN model to get the action with max Q
            precictions = {}
            for action in actions:
                state = snake.get_state([a_x, a_y], action)
                precictions[action] = model.predict(
                    np.array(state).reshape(-1, 5))[0][0]
            action = max(precictions, key=precictions.get)

            # set the direction of snake using the chosen action
            snake.set_direction_by_action(action)
            #############################################

            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            clock.tick(FPS)
    # after traning is done, print out the average score
    print("Average score is: {}".format(sum(s) / len(s)))
示例#13
0
def training_game(times=40000):
    # we need to store the training data first
    # then use the data to train the NN
    train_data = []
    # f = open("train_data.txt", "rb")
    # train_data = pickle.load(f)
    # f.close()

    for i in range(times):
        # print out the game number
        print(i)

        pygame.event.pump()
        game_over = False

        # Will be the leader of the #1 block of the snake
        lead_x = 70
        lead_y = 70

        # snake default direction is right
        snake = NN_Snake(gameDisplay, display_width, display_height, img,
                         lead_x, lead_y)
        apple = Apple(gameDisplay, display_width, display_height, block_size,
                      img2, snake.snake_list)

        a_x, a_y = apple.get_apple_pos()

        # get the initial state, and action will be "up" starting
        action = "up"
        state = snake.get_state([a_x, a_y], action)
        old_distance = snake.get_distance([a_x, a_y])

        while not game_over:

            # based on the direction, we can work out the x, y changes to update the snake
            a_x, a_y = apple.get_apple_pos()
            snake.update_snake_list(a_x, a_y)

            # after snake moves, get the new distance
            distance = snake.get_distance([a_x, a_y])

            # default reward is 0
            reward = 0
            # check if snake dies
            if snake.is_alive() is False:
                game_over = True
                # if snake dies, award is -1
                reward = -1

            gameDisplay.fill(white)

            # if snake eats the apple, make a random new apple
            if snake.eaten is True:
                apple.update_apple_pos(snake.snake_list)

            # if snake eats the apple, or moved closer to apple, reward is 1
            if snake.eaten is True or distance < old_distance:
                reward = 1

            #############################################
            # collect the training data for NN
            train_data.append([np.array(state), reward])

            # this part is using random method to move the snake
            action = random.choice(actions)
            a_x, a_y = apple.get_apple_pos()
            old_distance = snake.get_distance([a_x, a_y])
            state = snake.get_state([a_x, a_y], action)
            snake.set_direction_by_action(action)
            #############################################

            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            clock.tick(FPS)
    # store the training data to txt
    print(len(train_data))
    f = open("train_data.txt", "wb")
    pickle.dump(train_data, f)
    f.close()
示例#14
0
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800, 800))
screen.fill(Colors.GREY)
game_area = pygame.Rect((0, 0, 800, 800))

score = 0
font = pygame.font.Font('arial.ttf', 32)
text = font.render(str(score), True, Colors.WHITE)
textrect = text.get_rect()
textrect.right = 780
screen.blit(text, textrect)

draw_grid()
snake = Snake(screen)
apple = Apple(screen, snake)

eat_apple = pygame.USEREVENT + 1
game_over = False

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if not game_over:
                if event.key == pygame.K_w:
                    snake.turn('-y')
                if event.key == pygame.K_a:
示例#15
0
def training_game(times=10):
    # s list will store the score of each game
    s = []
    for i in range(times):
        # print out the game number
        print(i)

        pygame.event.pump()
        game_over = False

        # Will be the leader of the #1 block of the snake
        lead_x = 70
        lead_y = 70

        # snake default direction is right
        snake = RL_Snake(gameDisplay, display_width, display_height, img,
                         lead_x, lead_y)
        apple = Apple(gameDisplay, display_width, display_height, block_size,
                      img2, snake.snake_list)

        a_x, a_y = apple.get_apple_pos()

        # get the initial state, and action will be "up" starting
        old_state = snake.get_state([a_x, a_y])
        old_action = "up"

        while not game_over:

            # based on the direction, we can work out the x, y changes to update the snake
            a_x, a_y = apple.get_apple_pos()
            snake.update_snake_list(a_x, a_y)

            # snake not die or eats the apple, reward will be -10
            # this is negative so that it will "encourage" the snake to
            # move towards to the apple, since that is the only positive award
            reward = -10

            # check if snake dies
            if snake.is_alive() is False:
                game_over = True
                # if snake dies, award is -100
                reward = -100
                s.append(snake.snake_length - 1)

            gameDisplay.fill(white)

            # if snake eats the apple, make a random new apple
            if snake.eaten is True:
                apple.update_apple_pos(snake.snake_list)
                # if snake eats the apple, reward is 500
                reward = 500

            #############################################
            # get he new state and new action, then we can update the Q table
            state = snake.get_state([a_x, a_y])
            action = snake_agent.getA(tuple(state))

            snake_agent.updateQ(tuple(old_state), old_action, tuple(state),
                                action, reward)
            old_action = action

            # training will take a lot of time, so archive the Q table
            snake_agent.saveQ()

            # this part is using the snake position and apple
            # position to use the Sarsa method to get the action
            a_x, a_y = apple.get_apple_pos()
            old_state = snake.get_state([a_x, a_y])
            snake.set_direction_by_action(action)
            #############################################

            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            clock.tick(FPS)
    # after traning is done, print out the average score
    print("Average score is: {}".format(sum(s) / len(s)))
示例#16
0
def training_game(times=100):
    # s list will store the score of each game
    s = []
    for i in range(times):
        # print out the game number
        print(i)

        pygame.event.pump()
        game_over = False

        # Will be the leader of the #1 block of the snake
        lead_x = 70
        lead_y = 70

        # snake default direction is right
        snake = DQN_Snake(gameDisplay, display_width, display_height, img,
                          lead_x, lead_y)
        apple = Apple(gameDisplay, display_width, display_height, block_size,
                      img2, snake.snake_list)

        a_x, a_y = apple.get_apple_pos()

        # get the initial state, and action will be "up" starting
        action = "up"
        old_state = snake.get_state([a_x, a_y])

        while not game_over:

            # based on the direction, we can work out the x, y changes to update the snake
            a_x, a_y = apple.get_apple_pos()
            snake.update_snake_list(a_x, a_y)

            # get the new state
            state = snake.get_state([a_x, a_y])

            # snake not die or eats the apple, reward will be -10
            # this is negative so that it will "encourage" the snake to
            # move towards to the apple, since that is the only positive award
            reward = -10

            # check if snake dies
            if snake.is_alive() is False:
                game_over = True
                # copy the weights from q_model to target_model
                agent.copy_weights()
                # if snake dies, award is -100
                reward = -100
                s.append(snake.snake_length - 1)

            gameDisplay.fill(white)

            # if snake eats the apple, make a random new apple
            if snake.eaten is True:
                apple.update_apple_pos(snake.snake_list)
                # if snake eats the apple, reward is 100
                reward = 100

            #############################################

            # store the train_data to the memory
            agent.store_train_data(np.reshape(old_state, [1, 5]),
                                   look_up[action], reward,
                                   np.reshape(state, [1, 5]), game_over)

            # if the memory size is larger than the batch_size, start training
            if len(agent.memory) > batch_size:
                agent.train(batch_size)

            # push state to old state
            a_x, a_y = apple.get_apple_pos()
            old_state = snake.get_state([a_x, a_y])

            # get the action from the DQN model
            action = actions[agent.get_action(np.reshape(old_state, [1, 5]))]
            snake.set_direction_by_action(action)
            #############################################

            apple.display()
            snake.eaten = False
            snake.display()
            snake.display_score()
            pygame.display.update()
            clock.tick(FPS)
    # when the training is finised, sae the model
    agent.save_model()
    # after traning is done, print out the average score
    print("Average score is: {}".format(sum(s) / len(s)))
示例#17
0
def all_apples():
    return jsonify(apples=[apple.json() for apple in Apple.select()])
示例#18
0
def status():
    apples = [{"apple": apple.table, "status": apple.status, "complete": apple.complete} for apple in Apple.select()]
    return jsonify(apples=apples)
示例#19
0
def status():
    # apples = [{"apple": apple.table, "status": apple.status, "complete": apple.complete} for apple in Apple.select()]
    apples = [{"apple": apple.table, "status": apple.status, "complete": apple.complete, "elected": apple.elected, "agreement": apple.agreement} for apple in Apple.select()]
    return jsonify(apples=apples)