Exemplo n.º 1
0
def ml_loop():
    mode = "TestTrain"  # TestTrain RuleBase
    predictFunction = "knn"  #svm
    aid = 0
    past_ball_position = []
    ball_down = False
    comm.ml_ready()

    if mode == "RuleBase":
        while True:
            scene_info = comm.get_scene_info()

            if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue
            now_ball_position = scene_info.ball

            if len(past_ball_position) == 0:
                past_ball_position = now_ball_position
            else:
                if (now_ball_position[1] - past_ball_position[1]) > 0:
                    ball_down = True
                else:
                    ball_down = False
            now_platform_positionX = scene_info.platform[0] + 20

            if ball_down == True and now_ball_position[1] > 200:
                m = (now_ball_position[1] - past_ball_position[1]) / (
                    now_ball_position[0] - past_ball_position[0])
                aid = now_ball_position[0] - ((now_ball_position[1] - 395) / m)
                if aid < 0:
                    aid = -aid
                elif aid > 200:
                    aid = 200 - (aid - 200)
            else:
                aid = 100

            if aid > now_platform_positionX:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            if aid < now_platform_positionX:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            if aid == now_platform_positionX:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
            past_ball_position = now_ball_position

    if mode == "TestTrain":
        filename = predictFunction + ".sav"
        model = pickle.load(open(filename, 'rb'))

        while True:
            scene_info = comm.get_scene_info()
            now_ball_position = scene_info.ball

            if len(past_ball_position) != 0:
                inp_temp = np.array([
                    past_ball_position[0], past_ball_position[1],
                    now_ball_position[0], now_ball_position[1],
                    scene_info.platform[0]
                ])
                input = inp_temp[np.newaxis, :]

                if scene_info.status == GameStatus.GAME_OVER or \
                    scene_info.status == GameStatus.GAME_PASS:
                    comm.ml_ready()
                    continue
                move = model.predict(input)

                if move < 0:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif move > 0:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
            past_ball_position = now_ball_position
Exemplo n.º 2
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    filename = path.join(path.dirname(__file__),
                         'save/clf_KMeans_BallAndDirection.pickle')
    with open(filename, 'rb') as file:
        clf = pickle.load(file)
    s = [95, 400]
    v_pre = [0, 0]

    def get_direction(ball_x, ball_y, ball_pre_x, ball_pre_y):
        VectorX = ball_x - ball_pre_x
        VectorY = ball_y - ball_pre_y
        if (VectorX == 0 and VectorY == 0):
            return 0
        elif (VectorX == 0 and VectorY > 0):
            return 1
        elif (VectorX == 0 and VectorY < 0):
            return 2
        elif (VectorX > 0 and VectorY == 0):
            return 3
        elif (VectorX < 0 and VectorY == 0):
            return 4
        elif (VectorX > 0 and VectorY > 0):
            return 5
        elif (VectorX > 0 and VectorY < 0):
            return 6
        elif (VectorX < 0 and VectorY > 0):
            return 7
        elif (VectorX < 0 and VectorY < 0):
            return 8

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        feature = []
        vectorX = scene_info.ball[0] - s[0]
        vectorY = scene_info.ball[1] - s[1]

        feature.append(scene_info.ball[0])
        feature.append(scene_info.ball[1])
        feature.append(vectorX)
        feature.append(vectorY)
        feature.append(
            get_direction(scene_info.ball[0], scene_info.ball[1], s[0], s[1]))

        if (vectorX * v_pre[0] < 0) and (vectorY * v_pre[1] < 0):
            if (v_pre[0] > 0) and (v_pre[1] > 0):
                feature.append(1)
            elif (v_pre[0] > 0) and (v_pre[1] < 0):
                feature.append(2)
            elif (v_pre[0] < 0) and (v_pre[1] > 0):
                feature.append(3)
            else:
                feature.append(4)
        elif (vectorX * v_pre[0] < 0):
            if (v_pre[0] > 0):
                feature.append(5)
            else:
                feature.append(6)
        elif (vectorY * v_pre[1] < 0):
            if (v_pre[1] > 0):
                feature.append(7)
            else:
                feature.append(8)
        else:
            feature.append(0)

        s = [scene_info.ball[0], scene_info.ball[1]]
        v_pre = [vectorX, vectorY]
        feature = np.array(feature)
        feature = feature.reshape((1, -1))
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:

            y = clf.predict(feature)

            if y == 0:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
                print('NONE')
            elif y == 1:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
                print('LEFT')
            elif y == 2:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
                print('RIGHT')
Exemplo n.º 3
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        ball_x = scene_info.ball[0]
        ball_y = scene_info.ball[1]
        platform = scene_info.platform[0] + 20

        #print(ball_y)
        #print(platform_C)
        #print(i)
        if ball_y <= 260 or ball_ey > ball_y:
            u = 0
            if platform > 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            if platform < 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
        else:
            if u != 1:
                if ball_ex > ball_x:
                    if ball_x - 140 > 0:
                        i = ball_x - 140
                    else:
                        i = -(ball_x - 140)
                else:
                    if ball_x + 140 < 200:
                        i = i = ball_x + 140
                    else:
                        i = 400 - (ball_x + 140)
                u = 1

            if platform > i:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif platform < i:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)

        ball_ey = ball_y
        ball_ex = ball_x
Exemplo n.º 4
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    a = 0
    b = 0
    c = 0
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            ball_x = scene_info.ball[0]
            ball_y = scene_info.ball[1]
            platform_x = scene_info.platform[0]
            platform_y = scene_info.platform[1]
            if ball_y < 150:
                if platform_x <75:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)   
                elif platform_x >75:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame, PlatformAction.NONE)
            else:
                if ball_y - a >0:#down
                    if ball_x - b >0:#right 395-ball_y == height 
                        if (395 - ball_y)>(200-ball_x): #bounce
                            c = 200- ((395 - ball_y)-(200-ball_x))
                        else:
                            c = ball_x + (395 - ball_y)    

                        #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                    else:#left
                        if (395 - ball_y)>ball_x: #bounce
                            c = ((395 - ball_y)-ball_x)
                        else:
                            c = ball_x - (395 - ball_y)   
                    b = ball_x
                    if platform_x <(c-20):
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)   
                    elif platform_x >(c-20):
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                    else:
                        comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 5
0
def ml_loop():
    """The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        platform_center_x = scene_info.platform[0] + 20
        if (len(ball_position_history)) == 1:
            ball_going_down = 0
        elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0:
            ball_going_down = 1
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]
        else:
            ball_going_down = 0
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER:
            print("game over")
            comm.ml_ready()
            continue
        elif scene_info.status == GameStatus.GAME_PASS:
            #scene_info = comm.get_scene_info()
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            print("game pass")
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if ball_going_down == 1 and ball_position_history[-1][
                1] >= 200 and ball_position_history[-1][1] <= 270:
            ball_destination = ball_position_history[-1][0] + (
                (395 - ball_position_history[-1][1]) / vy) * vx
            if ball_position_history[-1][0] < 145 and ball_position_history[
                    -1][0] > 50:
                if ball_destination >= 145:
                    ball_destination = 145 - (ball_destination - 145)
                elif ball_destination <= 50:
                    ball_destination = 50 + (50 - ball_destination)
            elif ball_position_history[-1][0] < 170 and ball_position_history[
                    -1][0] > 25:
                if ball_destination >= 170:
                    ball_destination = 170 - (ball_destination - 170)
                elif ball_destination <= 25:
                    ball_destination = 25 + (25 - ball_destination)
            elif ball_position_history[-1][0] < 195 and ball_position_history[
                    -1][0] > 0:
                if ball_destination >= 195:
                    ball_destination = 195 - (ball_destination - 195)
                elif ball_destination <= 0:
                    ball_destination = -ball_destination
        elif ball_going_down == 1 and ball_position_history[-1][1] > 270:
            ball_destination = ball_position_history[-1][0] + (
                (395 - ball_position_history[-1][1]) / vy) * vx
            if ball_destination >= 195:
                ball_destination = 195 - (ball_destination - 195)
            elif ball_destination <= 0:
                ball_destination = -ball_destination
        else:
            ball_destination = platform_center_x

        # 3.4. Send the instruction for this frame to the game process

        if ball_going_down == 1:
            if platform_center_x < ball_destination - 5:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > ball_destination + 5:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        elif ball_going_down == 0 and ball_position_history[-1][1] <= 300:
            if platform_center_x < 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > 100:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        elif ball_going_down == 0 and ball_position_history[-1][0] > 155:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif ball_going_down == 0 and ball_position_history[-1][0] < 40:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        elif ball_going_down == 0 and ball_position_history[-1][
                0] > 40 and ball_position_history[-1][
                    0] < 90 and ball_position_history[-1][
                        0] - ball_position_history[-2][0] < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif ball_going_down == 0 and ball_position_history[-1][
                0] < 155 and ball_position_history[-1][
                    0] > 95 and ball_position_history[-1][
                        0] - ball_position_history[-2][0] > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)


#        try :
#            print (vx)
#            print (ball_position_history[-1][0],ball_position_history[-2][0])
#        except :
#            print (ball_position_history[-1][0])
Exemplo n.º 6
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    platform_arrived = False
    ball_dir = 0
    ball_preposition = -1
    destination = -1
    position = 0
    fall = False
    ball_high = -1

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True

            platform_arrived = False
            ball_dir = 0
            ball_preposition = -1
            destination = -1
            position = 0
            fall = False
        else:
            position = scene_info.platform[0] + 20

            if scene_info.ball[1] >= 280 and scene_info.ball[
                    1] <= 320 and fall == True:
                if ball_dir == 0 and platform_arrived == False:
                    if scene_info.ball[0] >= 10 and scene_info.ball[0] <= 190:
                        if ball_preposition == -1:
                            ball_preposition = scene_info.ball[0]
                        else:
                            if scene_info.ball[0] - ball_preposition > 0:
                                ball_dir = 1
                            else:
                                ball_dir = -1

            if scene_info.ball[1] >= 350:
                ball_dir = 0
                ball_preposition = -1

            if platform_arrived == False and destination == -1:
                if scene_info.ball[1] >= 280 and not ball_dir == 0:
                    temp = 400 - scene_info.ball[1]
                    destination = scene_info.ball[0] + ball_dir * temp
                    fall = False
                    if destination > 200:
                        destination = 200 - (destination - 200)
                    if destination < 0:
                        destination = -destination

            if abs(position - destination) <= 5:
                platform_arrived = True
                destination = -1

            if scene_info.ball[1] < 270:
                platform_arrived = False
                fall = True
            ##if scene_info.ball[1]>390:
            ##    platform_arrived=False

            if scene_info.ball[1] > 390:
                if ball_high == -1:
                    ball_high = scene_info.ball[1]
                    ##print(1)

            if scene_info.ball[1] > 380 and scene_info.ball[1] < 390:
                if ball_high > 0 and scene_info.ball[1] < ball_high:
                    platform_arrived = False
                    ##print(2)
            if scene_info.ball[1] < 360:
                ball_high = -1

            if platform_arrived == True:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)

            else:
                if destination == -1:

                    if position < 90:
                        ##print(1)
                        comm.send_instruction(scene_info.frame,
                                              PlatformAction.MOVE_RIGHT)
                    if position >= 90 and position <= 110:
                        ##print(2)
                        comm.send_instruction(scene_info.frame,
                                              PlatformAction.NONE)
                    if position > 110:
                        ##print(3)
                        comm.send_instruction(scene_info.frame,
                                              PlatformAction.MOVE_LEFT)
                else:
                    if position - destination > 5:
                        ##print(4)
                        comm.send_instruction(scene_info.frame,
                                              PlatformAction.MOVE_LEFT)
                    if position - destination < -5:
                        ##print(5)
                        comm.send_instruction(scene_info.frame,
                                              PlatformAction.MOVE_RIGHT)
Exemplo n.º 7
0
def ml_loop():
    """The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    aid = 0
    past_ball_position = []
    ball_down = False

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        now_ball_position = scene_info.ball
        if len(past_ball_position) == 0:
            past_ball_position = now_ball_position
        else:
            if (now_ball_position[1] - past_ball_position[1]) > 0:
                ball_down = True
            else:
                ball_down = False

        now_platform_positionX = scene_info.platform[0] + 20

        if ball_down == True and now_ball_position[1] > 200:
            m = (now_ball_position[1] - past_ball_position[1]) / (
                now_ball_position[0] - past_ball_position[0])
            aid = now_ball_position[0] - ((now_ball_position[1] - 395) / m)
            if aid < 0:
                aid = -aid
            elif aid > 200:
                aid = 200 - (aid - 200)
        else:
            aid = 100

        if aid > now_platform_positionX:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        if aid < now_platform_positionX:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        if aid == now_platform_positionX:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)

        past_ball_position = now_ball_position
Exemplo n.º 8
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    last_x = 75
    last_y = 400
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        (ball_x, ball_y) = scene_info.ball
        platform_x = scene_info.platform[0]
        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            #if (last_y - ball_y) < 0 and ball_y > 350:
            if ball_y > 300:
                x = predict(ball_x, last_x, ball_y, last_y)
                if x < (platform_x + 20):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif x > (platform_x + 25):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
            """
                else:
                    if ball_x < (platform_x + 20):
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                    elif ball_x > (platform_x + 25):
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                    else:
                        comm.send_instruction(scene_info.frame, PlatformAction.NONE)
                """
            last_x = ball_x
            last_y = ball_y
Exemplo n.º 9
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        ball_x.append(scene_info.ball[0])
        ball_y.append(scene_info.ball[1])
        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_RIGHT)
            ball_served = True
        else:
            global final
            if (scene_info.ball[1] > 200):
                if (scene_info.ball[1] - ball_y[-2] > 0):  # 球往下
                    if (scene_info.ball[0] - ball_x[-2] > 0):  # 球往右
                        final = (400 - scene_info.ball[1]) + scene_info.ball[0]
                        if (final > 200):
                            final = 400 - final
                    else:  # 往左
                        final = scene_info.ball[0] - (400 - scene_info.ball[1])
                        if (final < 0):
                            final = -final
                if (scene_info.platform[0] + 10 > final):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif (scene_info.platform[0] + 10 < final):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
            else:
                if (scene_info.platform[0] + 10 > scene_info.ball[0]):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
Exemplo n.º 10
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    lowest = -1 #lowest block position
    lock = False #locker
    old_x = 0
    old_y = 0
    est = -1 # the estimate position of x

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        ball_x = scene_info.ball[0]
        ball_y = scene_info.ball[1]
        plat_x = scene_info.platform[0]
        plat_y = scene_info.platform[1]

        for x in scene_info.bricks:
            if x[1] > lowest:
                lowest = x[1]
        for x in scene_info.hard_bricks:
            if x[1] > lowest:
                lowest = x[1]
        print(scene_info.ball)
        #print("con1" + str(ball_y>(lowest+10)))
        #print("con2" + str(not lock))
        #print("con3" + str(ball_y-old_y))
        #print(est)

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame, PlatformAction.SERVE_TO_RIGHT)
            ball_served = True
        elif lock == True:
            if ball_y == plat_y-5:
                print("unlock")
                lock = False
            if plat_x > est-20:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            elif plat_x < est-20:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)    
        elif ball_y > (lowest+110) and not lock and (ball_y - old_y) > 0:
            lock = True
            print("locked")
            vy = (ball_y-old_y)
            vx = abs(ball_x-old_x) #velocity of x
            if vx < 7:
                vx = 7
            print("velocity of x =" + str(vx))
            print("velocity of y =" + str(vy))
            #plat_y = 400
            if ball_x - old_x > 0: #move right
                if (200-ball_x)/vx > (plat_y-ball_y)/vy:
                    est = (plat_y-ball_y)/vy*vx+ball_x
                else: #hit x = 200
                    hit_y = ball_y+(200-ball_x)/vx*vy
                    est = 200 - (plat_y-hit_y)/vy*vx
            else: #move left
                if (ball_x-0)/vx > (plat_y-ball_y)/vy:
                    est = ball_x-(plat_y-ball_y)/vy*vx
                else: #hit x = 0
                    hit_y = ball_y+ball_x/vx*vy
                    est = (plat_y-hit_y)/(ball_y-old_y)*vx
            print("begin with " + str(ball_x) + "," + str(ball_y))
            print("est=" + str(est))
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        else:
            if plat_x < 100:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif plat_x > 100:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
        
        old_x = ball_x
        old_y = ball_y
def ml_loop():
    """The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    import pickle
    import numpy as np
    filename = "C:\\Users\\ASUS\\Desktop\\108-1\\Mechine_learning\\MLGame-master\\games\\arkanoid\\Train\\svr_example1.sav"
    model = pickle.load(open(filename, 'rb'))
    #print(model)
    ball_position_history = []
    vx = 0
    vy = 0
    ball_destination = 0
    ball_going_down = 0
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        platform_center_x = scene_info.platform[0]  #platform length = 40

        if len(ball_position_history) > 1:
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]

        inp_temp = np.array([
            scene_info.ball[0], scene_info.ball[1], platform_center_x, vx, vy
        ])
        input = inp_temp[np.newaxis, :]
        #print(input)
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        #if scene_info.status == GameStatus.GAME_OVER or \
        if scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            #scene_info = comm.get_scene_info()
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue
        platform_center_y = scene_info.platform[1]
        ball_height = scene_info.ball[1]  #球Y位點
        # 3.3. Put the code here to handle the scene information
        if (len(ball_position_history) > 1):
            if platform_center_y - ball_height <= 150:
                move = model.predict(input)
            else:
                move = 0
        else:
            move = 0

        # 3.4. Send the instruction for this frame to the game process
        #print(ball_destination)
        #print(platform_center_x)
        if move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)

        elif move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            if platform_center_x < 80:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center_x > 80:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            pass
Exemplo n.º 12
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    filename = path.join(path.dirname(__file__), 'save',
                         'clf_KMeans_BallAndDirection.pickle')
    with open(filename, 'rb') as file:
        clf = pickle.load(file)
    s = [93, 93]

    def get_direction(ball_x, ball_y, ball_pre_x, ball_pre_y):
        VectorX = ball_x - ball_pre_x
        VectorY = ball_y - ball_pre_y
        if (VectorX >= 0 and VectorY >= 0):
            return 0
        elif (VectorX > 0 and VectorY < 0):
            return 1
        elif (VectorX < 0 and VectorY > 0):
            return 2
        elif (VectorX < 0 and VectorY < 0):
            return 3

    def get_point(ball_x, ball_y, dir, dir_x_abs):
        if (dir_x_abs == 0):
            return scene_info.platform[0]
        crush = -1
        while (ball_y < 400):
            ball_x = ball_x + dir
            ball_y = ball_y + dir_x_abs
            if (ball_x < 0):
                ball_x = 0
                dir = -dir
                crush = crush * -1
            elif (ball_x > 200):
                ball_x = 200
                dir = -dir
                crush = crush * -1
        while (ball_y > 400):
            ball_x = ball_x - dir / dir_x_abs
            ball_y = ball_y - 1
        return ball_x * crush

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        feature = []
        feature.append(scene_info.ball[0])
        feature.append(scene_info.ball[1])
        feature.append(scene_info.platform[0])

        dir = feature[0] - s[0]
        dir_x_abs = abs(dir)
        direction = get_direction(feature[0], feature[1], s[0], s[1])
        feature.append(direction)
        s = [feature[0], feature[1]]
        feature = np.array(feature)
        feature = feature.reshape((-1, 4))

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            """y = clf.predict(feature)
            
            if y == 0:
                instruction = 'NONE'
            elif y == 1:
                instruction = 'LEFT'
            elif y == 2:
                instruction = 'RIGHT'"""

            instruction = 'NONE'
            if (scene_info.ball[1] >
                    255):  #and (direction == 0 or direction == 2)):
                point = get_point(scene_info.ball[0], scene_info.ball[1], dir,
                                  dir_x_abs)
                if (point < 0):
                    point = -point
                elif (direction == 0):
                    direction = 2
                else:
                    direction = 0
                if (direction == 0):
                    if (point - scene_info.platform[0] > 39):
                        instruction = 'RIGHT'
                    elif (point - scene_info.platform[0] < 28):
                        instruction = 'LEFT'
                else:
                    if (point - scene_info.platform[0] < 1):
                        instruction = 'LEFT'
                    elif (point - scene_info.platform[0] > 12):
                        instruction = 'RIGHT'
            elif (scene_info.ball[1] >
                  200):  #and (direction == 0 or direction == 2)):
                if (scene_info.platform[0] < 60):
                    instruction = 'RIGHT'
                elif (scene_info.platform[0] > 90):
                    instruction = 'LEFT'

            if instruction == 'LEFT':
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
                #print('LEFT')
            elif instruction == 'RIGHT':
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
                #print('RIGHT')
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 13
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history=[]
    ball_center_record = []
    test=-1
    test2=-1
    test_x=0
    test_y=0
    test2_x=0
    test2_y=0
    two_down =-1
    two_down1 =-1
    new_downHeight=0
    new_down=0
    new_downHeight1=0
    new_down1=0
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    mx =0
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        if len(ball_position_history) == 1:
            ball_going_down = 0
        elif ball_position_history [-1][1] - ball_position_history[-2][1] >0 :
            ball_going_down =1
            my = ball_position_history[-1][1] - ball_position_history[-2][1]
            mx = ball_position_history[-1][0] - ball_position_history[-2][0]
            #print(ball_going_down)
        platform_center_x = scene_info.platform[0]+20
        platform_center_y = scene_info.platform[1]

          #400 球初始 100
          #球一步X,Y= 7 平台一步 = 5

        ball_center = scene_info.ball[0]#球X位點左上角+2.5為正中央
        ball_height = scene_info.ball[1]#球Y位點
        #if scene_info.status == GameStatus.GAME_OVER or \
        if  scene_info.status == GameStatus.GAME_PASS:
            print("succcess")
            comm.ml_ready()
            continue
            #scene_info = comm.get_scene_info()

            # Do some stuff if needed
            # 3.2.1. Inform the game process that ml process is ready


            # Do some stuff if needed
            # 3.2.1. Inform the game process that ml process is ready

        if platform_center_y-ball_height>=125:
            vx = ball_center #紀錄 在100 x是多少
            one_mx = mx
            record_vx = 1
            test = -1
            test2 = -1
            if platform_center_x < 100:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif platform_center_x > 100:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            pass
        else:  ##第一次回擊

            if platform_center_y-ball_height>=112:
                vx = ball_center #紀錄 在100 x是多少
                one_mx = mx

            if record_vx ==1:
                if ball_position_history[-1][1] - ball_position_history[-2][1] >0:
                    up = platform_center_x
                    print(one_mx)
                    print(vx)
                    if one_mx > 0 :
                        if vx<=88: ##在左邊往右
                            if platform_center_x < vx+112:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > vx+112:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if mx>0:
                                test = 0
                                test_x = ball_center
                                test_y = platform_center_y-ball_height
                                if test_x>170:
                                    test = 1
                                    if platform_center_x < 200-abs(88-vx):
                                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                    elif platform_center_x > 200-abs(88-vx):
                                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                    pass
                                else:
                                    if platform_center_x < (200-abs(88-vx)+150)/2:
                                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                    elif platform_center_x > (200-abs(88-vx)+150)/2:
                                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                    pass
                            elif test ==0 :

                                if platform_center_x < test_x-test_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > test_x-test_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            elif one_mx == 5 or one_mx == 4:
                                if platform_center_x < vx-112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x >vx-112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            elif test != 1:
                                print("在112以上撞到反彈下")
                                print(test)
                                if platform_center_x < vx-112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > vx-112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                print("HALO1")
                                print(test2_x-test2_y)
                                if platform_center_x < test_x-test_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > test_x-test_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                    elif one_mx < 0 :
                        if vx>=112:##在又邊往左
                            if platform_center_x < vx-112:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > vx-112:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if mx<0:
                                print(test2)
                                print(platform_center_x)
                                test2 = 0
                                test2_x = ball_center
                                test2_y = platform_center_y-ball_height
                                if test2_x<30:
                                    test2 = 1
                                    if platform_center_x < abs(vx-112):
                                            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                    elif platform_center_x > abs(vx-112):
                                            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                    pass
                                else:
                                    if platform_center_x < (abs(vx-112)+70)/2:
                                            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                    elif platform_center_x > (abs(vx-112)+70)/2:
                                            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                    pass
                            elif test2 == 0 :
                                print(test2_x+test2_y)
                                if platform_center_x < test2_x+test2_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > test2_x+test2_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            elif one_mx == -5 or one_mx == -4 or one_mx == -2:
                                print(vx+112)
                                if platform_center_x < vx+112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x >vx+112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            elif test2 != 1:
                                print("在112以上撞到反彈下")
                                print(test2)
                                if platform_center_x < vx+112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > vx+112:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                print("HALO")
                                print(test2_x+test2_y)
                                if platform_center_x < test2_x+test2_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > test2_x+test2_y:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                else:
                    record_vx = 0

            else:
                if ball_position_history[-1][1] - ball_position_history[-2][1] >0:##第二次下降

                    if down_mx > 0:
                        print("down_mx又")
                        print(mx)
                        if mx>0 :
                            two_down=1
                            new_downHeight = platform_center_y-ball_height
                            newdown =ball_center
                            if newdown<190:
                                two_down=0
                                if platform_center_x <200-abs(down + down_height-200):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > 200-abs(down + down_height-200):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                if platform_center_x <(330-abs(down + down_height-200))/2:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > (330-abs(down + down_height-200))/2:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                        elif two_down==1:
                            print(newdown)
                            print(new_downHeight)
                            if platform_center_x <abs(newdown - new_downHeight):
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > abs(newdown - new_downHeight):
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if mx>0:
                                if platform_center_x <200-abs(200-(down+down_height)):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x >200-abs(200-(down+down_height)):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                if platform_center_x <abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                    else:

                        if mx<0:
                            two_down1=1
                            new_downHeight1 = platform_center_y-ball_height
                            newdown1 =ball_center
                            print(newdown1)
                            if newdown1>10:
                                two_down1=0
                                print(abs(down - down_height))
                                if platform_center_x <abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                if platform_center_x <(abs(down - down_height)+100)/2:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > (abs(down - down_height)+100)/2:
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                        elif two_down1 ==1:
                            if platform_center_x <abs(newdown1 + new_downHeight1):
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > abs(newdown1 + new_downHeight1):
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if mx>0:
                                if platform_center_x <200-abs(200-(down+down_height)):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x >200-abs(200-(down+down_height)):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass
                            else:
                                if platform_center_x <abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                                elif platform_center_x > abs(down - down_height):
                                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                                pass


                else: #回擊後上去
                    print("回擊後上去")
                    print(up) #回擊後托盤X位置
                    print(platform_center_x)
                    down = ball_center

                    down_height = platform_center_y-ball_height
                    down_mx = ball_position_history[-1][0] - ball_position_history[-2][0]
                    newdown=ball_center
                    new_downHeight=platform_center_y-ball_height
                    newdown1=ball_center
                    new_downHeight1=platform_center_y-ball_height
                    if up > 160: #UP第一次回及時托盤X
                        if mx>0:
                            if platform_center_x <up -30:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > up-30:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if platform_center_x < 70:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > 70:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                    elif up < 40 :
                        if mx<0:
                            if platform_center_x < up +30:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > up +30:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        else:
                            if platform_center_x < 130:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > 130:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                    else:
                        if mx<0:
                            if platform_center_x < 70:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > 70:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
                        elif mx>0:
                            if platform_center_x < 130:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                            elif platform_center_x > 130:
                                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                            pass
Exemplo n.º 14
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    BallX = -1
    BallY = -1
    BallNewX = -1
    BallNewY = -1
    downX = -1

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        platform = scene_info.platform[0]
        if (BallX == -1):
            BallX, BallY = scene_info.ball
        else:
            BallNewX, BallNewY = scene_info.ball
            nowVec = BallNewX - BallX, BallNewY - BallY
            BallX = BallNewX
            BallY = BallNewY
        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            if (nowVec[1] < 0):
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
            elif (nowVec[0] != 0):
                ins = nowVec[0]
                downStep = int((400 - BallNewY) / nowVec[1])
                #print(downStep)
                downX = BallNewX + int(downStep * ins)
                #print(downX)
                while (downX < 0 or downX > 200):
                    if (downX < 0):
                        downX = -downX
                    else:
                        downX = 400 - downX
                #print(downX)
                if (scene_info.platform[0] + 20 > downX):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif (scene_info.platform[0] - 20 < downX):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    filename = "C:\\Users\\fghj8\\MLGame-master\\games\\arkanoid\\svr_9class.sav"
    model = pickle.load(open(filename,'rb'))

    last_ball_x = 0
    last_ball_y = 0
    games = 0
    gameover = 0
    inputx = 0
    inputy = 0
    hit = False
    LRUP = 0
    ball_to_200 = 0
    comm.ml_ready()
    bricks = []
    # 3. Start an endless loop.
    scene_info = comm.get_scene_info()
    while True:
        # 3.1. Receive the scene information sent from the game process.
        
    
        table = np.array(np.zeros((208,408)))
        
        last_ball_x = scene_info.ball[0]
        last_ball_y = scene_info.ball[1]
        
        scene_info = comm.get_scene_info()
        bricks = scene_info.bricks
        for k in range(len(bricks)):
            for p in range(0,25):
                for j in range(0,10):
                    table[bricks[k][0] + p][bricks[k][1] + j ] = 1
        
        if(last_ball_x - scene_info.ball[0] > 0):
           
                if(last_ball_y - scene_info.ball[1] > 0):
                    #going up
                    inputx = scene_info.ball[0]
                    inputy = scene_info.ball[1]
                    LRUP = 3
                    #U.L
                    j = scene_info.ball[0]
                    k = scene_info.ball[1]
                    while(j > 0 and k > 0):
                        if(table[j][k] == 1):
                            hit = True
                            break
                        j = j-1
                        k = k-1
                        hit = False
                    if(hit==False):
                        j = scene_info.ball[0]
                        k = scene_info.ball[1]+3
                        while(j > 0 and k > 0):
                            if(table[j][k] == 1):
                                hit = True
                                break
                            j = j-1
                            k = k-1
                            hit = False
                        
                  
                else:
                    #going down
                    #DL
                    LRUP = 1
                    inputx = scene_info.ball[0]
                    inputy = scene_info.ball[1]
                    j = scene_info.ball[0]
                    k = scene_info.ball[1]
                    while(j > 0 and k < 400):
                        if(table[j][k] == 1):
                            hit = True
                            break
                        j = j-1
                        k = k+1
                        hit = False
                    if(hit==False):
                        j = scene_info.ball[0]
                        k = scene_info.ball[1]+3
                        while(j > 0 and k < 400):
                            if(table[j][k] == 1):
                                hit = True
                                break
                            j = j-1
                            k = k+1
                            hit = False
                   
                    
        else:
                
                if(last_ball_y - scene_info.ball[1] > 0):
                    #going up
                    LRUP = 4
                    inputx = scene_info.ball[0]+3
                    inputy = scene_info.ball[1]
                    #U.R
                    j = scene_info.ball[0]
                    k = scene_info.ball[1]
                    while(j < 200 and k > 0):
                        if(table[j][k] == 1):
                            hit = True
                            break
                        j = j+1
                        k = k-1
                        hit = False
                    if(hit==False):
                        j = scene_info.ball[0]+3
                        k = scene_info.ball[1]+3
                        while(j < 200 and k > 0):
                            if(table[j][k] == 1):
                                hit = True
                                break
                            j = j+1
                            k = k-1
                            hit = False
                   
                else:
                    #going down
                    inputx = scene_info.ball[0]+3
                    inputy = scene_info.ball[1]
                    LRUP = 2
                    #D.R.
                    j =scene_info.ball[0]+3
                    k = scene_info.ball[1]
                    while(j < 200 and k < 400):
                        if(table[j][k] == 1):
                            hit = True
                            break
                        j = j+1
                        k = k+1
                        hit = False
                    if(hit==False):
                        j = scene_info.ball[0]+3
                        k = scene_info.ball[1]+3
                        while(j < 200 and k < 400):
                            if(table[j][k] == 1):
                                hit = True
                                break
                            j = j+1
                            k = k+1
                            hit = False
                    
                    
        ballx_to_platmid = np.abs((scene_info.ball[0]-scene_info.platform[0]))
        bally_to_platmid = 400-scene_info.ball[1]
#        print(j,k)
        inp_temp = np.array([inputx,inputy,j,k,LRUP,(200 - int(scene_info.ball[0])),ballx_to_platmid,bally_to_platmid,scene_info.platform[0]+20])
        input = inp_temp[np.newaxis,:]
        
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            games = games + 1
#            print(games)
            if(scene_info.status == GameStatus.GAME_OVER):
#                print('game over')
#                gameover = gameover + 1
#                print((games - gameover) / games)
                 pass
            # Do some stuff if needed
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        move = model.predict(np.around(input))

    
        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        
        if(move < -0.1):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif(move > 0.1):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 16
0
def ml_loop():
    mode = "TestTrain" # TestTrain RuleBase
    predictFunction = "svm" #svm
    aid = 100
    past_ball_position = []
    ball_down = False
    comm.ml_ready()

    if mode == "RuleBase":
        while True:
            scene_info = comm.get_scene_info()

            if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
                comm.ml_ready()
                continue
            now_ball_position = scene_info.ball

            if len(past_ball_position) == 0:
                past_ball_position = now_ball_position
                bricks_history = scene_info.bricks
            else:
                if (now_ball_position[1] - past_ball_position[1]) > 0:
                    ball_down = True
                else:
                    ball_down = False
        
            m = 0
            if ball_down == True and now_ball_position[1] > 250:
                if now_ball_position[0] - past_ball_position[0] != 0:
                    m = (now_ball_position[1] - past_ball_position[1]) / (now_ball_position[0] - past_ball_position[0])
                    aid = round(now_ball_position[0] - ((now_ball_position[1] - 395) / m))
                if aid < 0:
                    aid = -aid
                elif aid > 195:
                    aid = 200 - (aid - 200)
            else:
                if now_ball_position[0] < 10 or now_ball_position[0] > 175 or now_ball_position[1] < 250:
                    aid = 100

            move = True
            bricks = list(filter(lambda x: x[1] > now_ball_position[1], scene_info.bricks))
            if check_rect_collide(now_ball_position, m, bricks):
                move = False

            now_platform_positionX = scene_info.platform[0] + 20
            if m != 0:
                if (m > 0 and aid >= 100 and aid <= 120) or (m > 0 and aid < 80):
                    now_platform_positionX = scene_info.platform[0] + 5
                elif (m < 0 and aid <= 100 and aid >= 80) or (m < 0 and aid > 120):
                    now_platform_positionX = scene_info.platform[0] + 35

            if move and aid > now_platform_positionX:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            if move and aid < now_platform_positionX:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            if move and aid == now_platform_positionX:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)

            past_ball_position = now_ball_position

    if mode == "TestTrain":   
        filename = predictFunction + ".sav"
        model = pickle.load(open(filename, 'rb')) 
        
        while True:
            scene_info = comm.get_scene_info()
            now_ball_position = scene_info.ball

            if len(past_ball_position) != 0:
                m = 0
                if now_ball_position[0] - past_ball_position[0] != 0:
                    m = (now_ball_position[1] - past_ball_position[1]) / (now_ball_position[0] - past_ball_position[0])
                bricks = list(filter(lambda x: x[1] > now_ball_position[1], scene_info.bricks))
                overlap = get_rect_collide(now_ball_position, m, bricks)

                inp_temp = np.array([past_ball_position[0], past_ball_position[1], now_ball_position[0], now_ball_position[1], scene_info.platform[0], overlap[0], overlap[1], m])
                input = inp_temp[np.newaxis, :]
                
                if scene_info.status == GameStatus.GAME_OVER or \
                    scene_info.status == GameStatus.GAME_PASS:
                    comm.ml_ready()
                    continue
                move = model.predict(input)

                if move < 0:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
                elif move > 0:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame, PlatformAction.NONE)    
            past_ball_position = now_ball_position
Exemplo n.º 17
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_center_record = []
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_center_record.append(scene_info.ball)
        platform_center = scene_info.platform[0] + 20
        ball_center = scene_info.ball[0] + 2.5
        ball_height = scene_info.ball[1]
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if ball_height <= 300:
            if ball_center > 100:
                if platform_center < 40:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif platform_center > 40:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                pass
            elif ball_center < 100:
                if platform_center < 160:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif platform_center > 160:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                pass
            pass
        elif ball_height >= 350:
            if platform_center < ball_center:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_center > ball_center:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            pass
        pass
Exemplo n.º 18
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """
    ball_pos_history = []
    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_pos_history.append(scene_info.ball)
        #判定球為上升還是落下
        if (len(ball_pos_history)) == 1:
            ball_godown = 0
        elif ball_pos_history[-1][1] - ball_pos_history[-2][
                1] > 0:  #[-1]代表倒數最後一列
            ball_godown = 1
            vy = ball_pos_history[-1][1] - ball_pos_history[-2][1]
            vx = ball_pos_history[-1][0] - ball_pos_history[-2][0]
            m = vy / vx
        else:
            ball_godown = 0

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            ball_x = scene_info.ball[0]  #the x of tuple(x, y)
            ball_y = scene_info.ball[1]
            platform_x = scene_info.platform[0]
            if ball_godown == 1 and ball_y >= 45:
                final_x = (400 - ball_y) / m + ball_x
                if final_x > 200:
                    final_x = 200 - (final_x - 200)
                elif final_x < 0:
                    final_x = -final_x
                if final_x > platform_x + 40:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif final_x < platform_x:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
Exemplo n.º 19
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.

    import pickle
    import numpy as np
    filename = "C:\\Users\\uers\\Desktop\\MLGame-master\\knn_ex.sav"
    model = pickle.load(open(filename, 'rb'))
    comm.ml_ready()

    ball_postition_history = []
    # 3. Start an endless loop.
    while True:

        #***********************************

        #********************************
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        platform_center_x = scene_info.platform[0]
        #***********************************

        #********************************

        ball_postition_history.append(scene_info.ball)

        if (len(ball_postition_history) > 1):
            vx = ball_postition_history[-1][0] - ball_postition_history[-2][0]
            vy = ball_postition_history[-1][1] - ball_postition_history[-2][1]
            # print(vx,vy)
            #inp_temp=np.array([scene_info.ball[0],scene_info.ball[1],scene_info.platform[0],scene_info.platform[1],vx,vy])
            inp_temp = np.array([
                scene_info.ball[0], scene_info.ball[1], scene_info.platform[0],
                vx, vy
            ])
            input = inp_temp[np.newaxis, :]
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            print("end", end='\n')
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        if (len(ball_postition_history) > 1):
            move = model.predict(input)
        else:
            move = 0
        print(move)
        if move < 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)

        elif move > 0:
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
Exemplo n.º 20
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    last_ball_position = [0, 0]
    final_x = 0
    # speed = 7
    pad = 0
    lastbrick = 0
    is_calcu = False

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    random.seed(datetime.now())

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        #print(scene_info)

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        delta_x = 0
        # 0:left, 1:right, 2:none
        platform_direction = 0
        # delta_y = 0
        """
        if scene_info.ball[1] >= 393 :
            if final_x < (scene_info.platform[0] + 20) :
                platform_direction = 0
            elif final_x > (scene_info.platform[0] + 20) :
                platform_direction = 1
            else :
                platform_direction = 2
        """
        """if scene_info.ball[1] > 230 :
            if is_speed_up and final_x % 5 != 0 :
                speed += 1
                is_speed_up = False
        else :
            is_speed_up = True"""

        #print(lastbrick)
        if scene_info.frame == 0:
            if (not scene_info.bricks) and (not scene_info.hard_bricks):
                lastbrick = 0
            elif not scene_info.hard_bricks:
                lastbrick = scene_info.bricks[-1][1]
            elif not scene_info.bricks:
                lastbrick = scene_info.hard_bricks[-1][1]
            else:
                if scene_info.bricks[-1][1] > scene_info.hard_bricks[-1][1]:
                    lastbrick = scene_info.bricks[-1][1]
                else:
                    lastbrick = scene_info.hard_bricks[-1][1]

        if scene_info.ball[1] < lastbrick + 20:
            is_calcu = False
            pad = 0

        if last_ball_position[1] < scene_info.ball[1]:
            if (is_calcu == False) and (scene_info.ball[1] > lastbrick + 30):
                delta_x = scene_info.ball[0] - last_ball_position[0]

                if delta_x == -10:
                    final_x = calculate_position(2, scene_info.ball[0],
                                                 scene_info.ball[1])
                    is_calcu = True
                    #print(1)
                    pad = 0
                elif delta_x == 10:
                    final_x = calculate_position(3, scene_info.ball[0],
                                                 scene_info.ball[1])
                    #print(2)
                    is_calcu = True
                    pad = 0
                elif delta_x == -7:
                    pad += 1
                    if pad == 3:
                        #print(3)
                        final_x = calculate_position(0, scene_info.ball[0],
                                                     scene_info.ball[1])
                        is_calcu = True
                        pad = 0
                elif delta_x == 7:
                    pad += 1
                    if pad == 3:
                        #print(4)
                        final_x = calculate_position(1, scene_info.ball[0],
                                                     scene_info.ball[1])
                        is_calcu = True
                        pad = 0
                """
                elif scene_info.ball[0] < 100 :
                    final_x = calculate_position(1, scene_info.ball[0], scene_info.ball[1])
                    is_calcu = True
                    pad = 0
                else :
                    final_x = calculate_position(0, scene_info.ball[0], scene_info.ball[1])
                    is_calcu = True
                    pad = 0
                """
                # print(final_x)
            """
            if final_x < (scene_info.platform[0] + 20) :
                platform_direction = 0
            elif final_x > (scene_info.platform[0] + 20) :
                platform_direction = 1
            else :
                platform_direction = 2
            """

            # print(final_x)
        else:
            final_x = 100
            # final_x = scene_info.ball[0]
            """
            if scene_info.platform[0] > 80 :
                platform_direction = 0
            elif scene_info.platform[0] < 80:
                platform_direction = 1
            else :
                platform_direction = 2
            """

        if final_x < (scene_info.platform[0] + 20):
            platform_direction = 0
        elif final_x > (scene_info.platform[0] + 20):
            platform_direction = 1
        else:
            platform_direction = 2

        last_ball_position = scene_info.ball
        """print(scene_info.bricks[-1], scene_info.hard_bricks[-1])

        if (not scene_info.bricks) and (not scene_info.hard_bricks):
            lastbrick = 400
        elif not scene_info.hard_bricks:
            lastbrick = scene_info.bricks[-1][1];
        elif not scene_info.bricks:
            lastbrick = scene_info.hard_bricks[-1][1];
        else:
            if scene_info.bricks[-1][1] > scene_info.hard_bricks[-1][1]:
                lastbrick = scene_info.bricks[-1][1]
            else:
                lastbrick = scene_info.hard_bricks[-1][1];
        """
        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            if platform_direction == 0:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            elif platform_direction == 1:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif platform_direction == 2:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 21
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    filename = "C:\\Users\\fghj8\\MLGame-master\\games\\arkanoid\\knn_example.sav"
    model = pickle.load(open(filename, 'rb'))

    last_ball_x = 0
    last_ball_y = 0
    comm.ml_ready()
    # 3. Start an endless loop.
    scene_info = comm.get_scene_info()
    while True:
        # 3.1. Receive the scene information sent from the game process.

        last_ball_x = scene_info.ball[0]
        last_ball_y = scene_info.ball[1]
        scene_info = comm.get_scene_info()
        plat_cneter_x = scene_info.platform[0] + 20

        if (last_ball_x - scene_info.ball[0] > 0):
            LR = 1
        else:
            LR = 0
        if (last_ball_y - scene_info.ball[1] > 0):
            UP = 0
        else:
            UP = 1

        inp_temp = np.array([
            scene_info.ball[0], scene_info.ball[1], LR, UP,
            scene_info.platform[0]
        ])
        input = inp_temp[np.newaxis, :]

        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            # 3.2.1. Inform the game process that ml process is ready
            scene_info = comm.get_scene_info()

        move = model.predict(input)

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process

        #plat_location拿到的位置是平板的最左
        #平板中心為 平板長度/2 + plat_location
        if (move < 0):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif (move > 0):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.

    # 2. Inform the game process that ml process is ready before start the loop.
    ball_location = [0, 0]
    plat_location = [0, 0]
    bricks = []
    hit = False
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            if scene_info.status == GameStatus.GAME_OVER:
                m = 10 / 0
            # Do some stuff if needed
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue
        last_ball_location = ball_location
        ball_location = scene_info.ball
        plat_location = scene_info.platform
        bricks = scene_info.bricks
        table = np.array(np.zeros((208, 408)))

        for k in range(len(bricks)):
            for p in range(0, 25):
                for j in range(0, 10):
                    table[bricks[k][0] + p][bricks[k][1] + j] = 1

        delta_x = int(ball_location[0]) - int(last_ball_location[0])
        delta_y = int(ball_location[1]) - int(last_ball_location[1])
        #get Slope
        try:
            m = delta_y / delta_x
        except:
            m = 0.0001

        if (int(ball_location[1]) - int(last_ball_location[1]) > 0):
            # dowing

            next_x = (400 - int(ball_location[1])) / m + int(ball_location[0])

            if (next_x > 200):
                next_x = 400 - next_x
            if (next_x < 0):
                if (next_x > -200):
                    next_x = np.abs(next_x)
                else:
                    next_x = next_x + 400

            if (int(ball_location[0]) > int(last_ball_location[0])):
                #D.R.
                j = ball_location[0] + 3
                k = ball_location[1]
                while (j < 200 and k < 400):
                    if (table[j][k] == 1):
                        hit = True
                        break
                    j = j + 1
                    k = k + 1
                    hit = False
                if (hit == False):
                    j = ball_location[0] + 3
                    k = ball_location[1] + 3
                    while (j < 200 and k < 400):
                        if (table[j][k] == 1):
                            hit = True
                            break
                        j = j + 1
                        k = k + 1
                        hit = False
                if (hit):
                    print('DR')
                    print(j, k)
                    if (table[j - 1][k] == 0):
                        if (j + k < 400):
                            next_x = k - j
                        else:
                            next_x = j + k - 400

            else:
                #D.L
                j = ball_location[0]
                k = ball_location[1]
                while (j > 0 and k < 400):
                    if (table[j][k] == 1):
                        hit = True
                        break
                    j = j - 1
                    k = k + 1
                    hit = False
                if (hit == False):
                    j = ball_location[0]
                    k = ball_location[1] + 3
                    while (j > 0 and k < 400):
                        if (table[j][k] == 1):
                            hit = True
                            break
                        j = j - 1
                        k = k + 1
                        hit = False
                if (hit):
                    print('DL')
                    print(j, k)
                    if (table[j + 1][k] == 0):
                        if (200 - j + k < 400):
                            next_x = k - j
                        else:
                            next_x = 400 - k + j

                if (next_x > 200):
                    next_x = next_x - 200
                if (next_x < 0):
                    next_x = next_x * -1

        else:
            if (int(ball_location[0]) > int(last_ball_location[0])):
                #U.R
                j = ball_location[0] + 3
                k = ball_location[1]
                while (j < 200 and k > 0):

                    if (table[j][k] == 1):
                        hit = True
                        break
                    j = j + 1
                    k = k - 1
                    hit = False

                if (hit == False):
                    j = ball_location[0] + 3
                    k = ball_location[1] + 3
                    while (j < 200 and k > 0):
                        if (table[j][k] == 1):
                            hit = True
                            break
                        j = j + 1
                        k = k - 1
                        hit = False

                if (hit):
                    print('UR')
                    print(j, k)
                    if (table[j][k + 1] == 0):
                        if (200 - j + k < 400):
                            next_x = k - j
                        else:
                            next_x = 400 - k + j

            else:
                #U.L
                j = ball_location[0]
                k = ball_location[1]
                while (j > 0 and k > 0):
                    if (table[j][k] == 1):
                        hit = True
                        break
                    j = j - 1
                    k = k - 1
                    hit = False
                if (hit == False):
                    j = ball_location[0]
                    k = ball_location[1] + 3
                    while (j > 0 and k > 0):
                        if (table[j][k] == 1):
                            hit = True
                            break
                        j = j - 1
                        k = k - 1
                        hit = False

                if (hit):
                    print('UL')
                    print(j, k)
                    if (table[j][k + 1] == 0):
                        if (j + k < 400):
                            next_x = 400 - (j + k)
                        else:
                            next_x = j + k - 400
                else:
                    next_x = 100

                if (next_x > 200):
                    next_x = next_x - 200
                if (next_x < 0):
                    next_x = next_x * -1

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process

        next_x = next_x - next_x % 5
        if (int(plat_location[0]) + 20 > next_x):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif (int(plat_location[0]) + 20 < next_x):
            comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
        else:
            comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 23
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    change_D = False
    change = 0  #calculate the prediction
    place_check = 75  #set the prediction point
    placex = 0  #x,y +- about direction
    placey = 0
    bricks = []  #to see if bricks have decrease
    ball_place = (95, 395)  # frame of ball place

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False
            change_D = False
            ball_place = (95, 395)
            placex = 0
            placey = 0
            change = 0
            bricks = scene_info.bricks.copy()
            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
            ball_place = scene_info.ball
            bricks = scene_info.bricks.copy() + scene_info.hard_bricks.copy()
            continue
        elif ball_served:
            placex = scene_info.ball[0] - ball_place[0]
            placey = scene_info.ball[1] - ball_place[1]
            ball_place = scene_info.ball
        # the ball pull up follow the ball
        if placey < 0:
            change_D = True
            if scene_info.ball[1] < 300:
                if (scene_info.platform[0] < 80):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
            else:

                if (placex > 0):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)

        # the ball going down
        else:
            if (abs(placex) > abs(change)):  #check if the ball is speeding up
                change_D = True
            if change_D == True and scene_info.ball[
                    1] > 250:  # latter one avoid frame delay
                change_D = False
                Y = scene_info.ball[1]  #get the ball place
                X = scene_info.ball[0]
                if abs(placex) < 7 and abs(placex) != 10:
                    if placex > 0:
                        change = 7
                    else:
                        change = -7
                else:
                    change = placex
                Num = int((395 - scene_info.ball[1]) /
                          placey) + 1  #count the ball step to plateform
                '''---------------check the route----------------------'''
                for i in range(Num):
                    if change > 0:
                        for item in bricks:
                            if (Y >= item[1] - 5 and Y <= item[1] + 10
                                    and X <= item[0]
                                    and X >= item[0] - change):
                                change = -change
                                X = item[0] - 5
                                break
                    else:
                        for item in bricks:
                            if (Y >= item[1] - 5 and Y <= item[1] + 10
                                    and X <= item[0] + 25
                                    and X >= item[0] - change):
                                change = -change
                                X = item[0] + 25
                                break
                    Y += placey
                    X += change
                    if X >= 195 or X <= 0:
                        change = -change
                        if X > 180:
                            X = 195
                        else:
                            X = 0
                place_check = X
            elif scene_info.ball[1] < 250:  #get the platform back to center
                place_check = 80
            '''
            if scene_info.ball[1]>390:
                    if placex>0:
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                    else:
                        comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            '''
            # move the platform

            if scene_info.platform[0] + 10 > place_check:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)

            elif scene_info.platform[0] + 30 < place_check:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)

        #if hit the brick recheck the place
        if len(bricks) != len(scene_info.bricks) + len(scene_info.hard_bricks):
            #retD = list(set(bricks).difference(set(scene_info.bricks)))
            change_D = True
            bricks = scene_info.bricks.copy() + scene_info.hard_bricks.copy()
Exemplo n.º 24
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False  #有沒有發球
    ball_x = 93
    ball_y = 93
    preball_y = 93

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()  #保留

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            preball_x = ball_x
            if preball_y > ball_y:
                up = True
            else:
                up = False
            preball_y = ball_y
            ball_x = scene_info.ball[0] + 2.5
            if preball_x > ball_x:
                left = True
            else:
                left = False
            ball_y = scene_info.ball[1] + 2.5
            platform_x = scene_info.platform[0] + 20
            if up:
                if platform_x == 95:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
                elif platform_x > 95:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
            else:
                if left:
                    if ball_x - (397.5 - ball_y) < 0:
                        drop_x = -(ball_x - (397.5 - ball_y))
                    else:
                        drop_x = ball_x - (397.5 - ball_y)
                else:
                    if ball_x + (397.5 - ball_y) > 200:
                        drop_x = 400 - (ball_x + (397.5 - ball_y))
                    else:
                        drop_x = ball_x + (397.5 - ball_y)
                if platform_x > drop_x + 3.5:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif platform_x < drop_x - 3.5:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
Exemplo n.º 25
0
def ml_loop():
    """The main loop of the machine learning process
    This loop is run in a separate process, and communicates with the game process.
    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_position_history = []
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    # 3. Start an endless loop.
    while True:

        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        platform_center_x = scene_info.platform[0] + 20
        if (len(ball_position_history)) == 1:
            ball_going_down = 0
        elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0:
            ball_going_down = 1
            vy = ball_position_history[-1][1] - ball_position_history[-2][1]
            vx = ball_position_history[-1][0] - ball_position_history[-2][0]
        else:
            ball_going_down = 0
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if len(scene_info.bricks) > 15:
            if ball_going_down == 1 and ball_position_history[-1][1] >= 0:
                ball_destination = ball_position_history[-1][0] + (
                    (395 - ball_position_history[-1][1]) / vy) * vx
                if ball_destination >= platform_center_x:
                    ball_destination = 195 - (ball_destination - 195)
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif ball_destination <= platform_center_x:
                    ball_destination = -ball_destination
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)

            else:
                if platform_center_x < 100:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif platform_center_x > 100:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
        else:

            if ball_going_down == 1 and ball_position_history[-1][1] >= 200:
                ball_destination = ball_position_history[-1][0] + (
                    (395 - ball_position_history[-1][1]) / vy) * vx
                if ball_destination >= platform_center_x:
                    ball_destination = 195 - (ball_destination - 200)
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif ball_destination <= platform_center_x:
                    ball_destination = -ball_destination
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)

            else:
                if platform_center_x < 80:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif platform_center_x > 80:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
Exemplo n.º 26
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False  #判斷發球

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()  #

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        plat = scene_info.platform[0]
        try:
            vX = scene_info.ball[0] - bX
            vY = scene_info.ball[1] - bY
        except:
            vX = 0
            vY = 0
        bX = scene_info.ball[0]
        bY = scene_info.ball[1]

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)

            ball_served = True
        else:
            #print(vX,end=",")
            #print(vY)

            if vY > 0:

                dX = 10 + bX + ((400 - bY) / vY) * vX

                if dX > 200:
                    dX = abs(400 - dX)
                elif dX < 0 and dX > -200:
                    dX = -dX
                elif dX < -200:
                    dX = 400 + dX
                #print(dX,end=",")
                #print(scene_info.platform)

                if plat + 40 == 200:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif plat == 0:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif plat + 30 < dX:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif plat + 30 > dX:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)

            else:

                dX = (bX + vX * 10) % 200
                if plat + 30 < dX:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                elif plat + 30 > dX:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
Exemplo n.º 27
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    i = 0
    ball_x = [0]
    ball_y = [0]

    def destination():
        if i == 0:
            return 0

        direction_x = (ball_x[i] - ball_x[i - 1])
        direction_y = (ball_y[i] - ball_y[i - 1])
        target_x = ball_x[i]
        target_y = ball_y[i]

        # first bounce
        while target_y < 400:
            target_x += direction_x
            target_y += direction_y

            if target_x < 0 or target_x > 200:
                direction_x = -direction_x
                target_x += direction_x
            elif target_y < 0:
                direction_y = -direction_y
                target_y += direction_y
        #print(str(target_x) + " " + str(target_y))
        return target_x

    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
                scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information

        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            platform_x = scene_info.platform[0] + 20
            ball_x.append(scene_info.ball[0])
            ball_y.append(scene_info.ball[1])
            i = i + 1
            target = destination()
            if target - platform_x > 0:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_RIGHT)
            elif target - platform_x < 0:
                comm.send_instruction(scene_info.frame,
                                      PlatformAction.MOVE_LEFT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
Exemplo n.º 28
0
def ml_loop():   
    comm.ml_ready()   
    while True:        
        scene_info = comm.get_scene_info()
        platform_center = scene_info.platform[0]+20
        ball_center = scene_info.ball[0]+2.5
        ball_Y = scene_info.ball[1]
        BIG_Y = 325#球會彈兩次的界線
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            comm.ml_ready()
            continue
        
        if ball_Y<= BIG_Y:
            if ball_center > 100:
                if platform_center < 20	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                elif platform_center > 20	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            elif ball_center < 100: 
                if platform_center < 180	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
                elif platform_center > 180	:
                    comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
        elif ball_Y>= BIG_Y:
            if platform_center < ball_center:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif platform_center > ball_center:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
Exemplo n.º 29
0
def ml_loop():
    """The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()
    ball_position_history=[]
    need_to_go=0.00
    M=0.00
    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()
        ball_position_history.append(scene_info.ball)
        #球和平板的X位置
        ball_posistion_x = scene_info.ball[0]+2.5
        platform_center_x = scene_info.platform[0]+20
        #球是否下墜
        if (len(ball_position_history))==1:
            ball_going_down=0
        elif ball_position_history[-1][1]-ball_position_history[-2][1] > 0:
            Dx = ball_position_history[-1][0]-ball_position_history[-2][0]
            Dy = ball_position_history[-1][1]-ball_position_history[-2][1]
            if Dy == 0:
                Dy = 0.000001
            elif Dx == 0:
                Dx = 0.000001
            M=Dx/Dy
            ball_going_down = 1
        else:
            ball_going_down = 0
        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        if ball_going_down == 1 and ball_position_history[-1][1]>=0:
            need_to_go = ball_position_history[-1][0] + (((400-ball_position_history[-1][1])/Dy)*Dx)
            if need_to_go >= 200:
                need_to_go = 400 - need_to_go
            elif need_to_go <= 0:
                need_to_go = 0 - need_to_go
                 
        # 3.4. Send the instruction for this frame to the game process
        if ball_going_down == 1 and ball_position_history[-1][1]>=0:
            if platform_center_x < need_to_go:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            elif platform_center_x > need_to_go:
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)        
Exemplo n.º 30
0
def ml_loop():
    """
    The main loop of the machine learning process

    This loop is run in a separate process, and communicates with the game process.

    Note that the game process won't wait for the ml process to generate the
    GameInstruction. It is possible that the frame of the GameInstruction
    is behind of the current frame in the game process. Try to decrease the fps
    to avoid this situation.
    """

    # === Here is the execution order of the loop === #
    # 1. Put the initialization code here.
    ball_served = False
    last_x = 75
    last_y = 400
    predict_x = 75
    predict_y = 401
    direction = 0
    # 0:leftup, 1:rightup, 2:leftdown, 3:rightdown
    # 2. Inform the game process that ml process is ready before start the loop.
    comm.ml_ready()

    # 3. Start an endless loop.
    while True:
        # 3.1. Receive the scene information sent from the game process.
        scene_info = comm.get_scene_info()

        # 3.2. If the game is over or passed, the game process will reset
        #      the scene and wait for ml process doing resetting job.
        if scene_info.status == GameStatus.GAME_OVER or \
            scene_info.status == GameStatus.GAME_PASS:
            # Do some stuff if needed
            ball_served = False

            # 3.2.1. Inform the game process that ml process is ready
            comm.ml_ready()
            continue

        # 3.3. Put the code here to handle the scene information
        (ball_x, ball_y) = scene_info.ball
        platform_x = scene_info.platform[0]
        platform_y = scene_info.platform[1]
        # 3.4. Send the instruction for this frame to the game process
        if not ball_served:
            comm.send_instruction(scene_info.frame,
                                  PlatformAction.SERVE_TO_LEFT)
            ball_served = True
        else:
            if ((ball_x - last_x) < 0 and (ball_y - last_y) < 0):
                direction = 0
            elif ((ball_x - last_x) > 0 and (ball_y - last_y) < 0):
                direction = 1
            elif ((ball_x - last_x) < 0 and (ball_y - last_y) > 0):
                direction = 2
            elif ((ball_x - last_x) > 0 and (ball_y - last_y) > 0):
                direction = 3
            m = (ball_y - last_y) / (ball_x - last_x)
            for i in range(30):
                if (direction == 0):
                    predict_x = 0
                    predict_y = m * (0 - ball_x) + ball_y
                    direction = 1
                    if (predict_y < 0):
                        predict_x = (0 - ball_y) / m + ball_x
                        predict_y = 0
                        direction = 2
                elif (direction == 1):
                    predict_x = 195
                    predict_y = m * (195 - ball_x) + ball_y
                    direction = 0
                    if (predict_y < 0):
                        predict_x = (0 - ball_y) / m + ball_x
                        predict_y = 0
                        direction = 3
                elif (direction == 2):
                    predict_x = 0
                    predict_y = m * (0 - ball_x) + ball_y
                    direction = 3
                    if (predict_y > 395):
                        predict_x = (400 - ball_y) / m + ball_x
                        predict_y = 400
                        direction = 0
                        break
                elif (direction == 3):
                    predict_x = 195
                    predict_y = m * (195 - ball_x) + ball_y
                    direction = 2
                    if (predict_y > 395):
                        predict_x = (400 - ball_y) / m + ball_x
                        predict_y = 400
                        direction = 1
                        break
                m = -m
            """
            if(predict_x < platform_x + 20):
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
            elif(predict_x > platform_x + 20):
                comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
            else:
                comm.send_instruction(scene_info.frame, PlatformAction.NONE)
            """
            if (last_y - ball_y) < 0:
                #x = predict(ball_x,last_x,ball_y,last_y)
                if predict_x < (platform_x + 20):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif predict_x > (platform_x + 25):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)
            else:
                if ball_x < (platform_x + 20):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_LEFT)
                elif ball_x > (platform_x + 25):
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.MOVE_RIGHT)
                else:
                    comm.send_instruction(scene_info.frame,
                                          PlatformAction.NONE)

            last_x = ball_x
            last_y = ball_y