Пример #1
0
def fitness(genome, window, timeStep=Settings.defaultTimeStep):
    for x in [failBox, statBox] + lines:
        x.draw(window)

    game = Pong.game()
    while game.gamesLeft:
        networkSprites = Draw.genomeToObjects(genome, 100)
        ball = graphics.Circle(
            graphics.Point(300 * game.ballX, 100 + (300 * game.ballY)), 15)
        ball.setFill('blue')
        paddle = graphics.Rectangle(
            graphics.Point(300, 100 + (300 * game.paddleY)),
            graphics.Point(310, 100 + (300 * (game.paddleY + game.paddleH))))
        paddle.setFill('black')
        for q in networkSprites + [ball, paddle]:
            q.draw(window)
        time.sleep(timeStep)
        for q in networkSprites + [ball, paddle]:
            q.undraw()
        game.updatePaddle(genome.evaluate(game.getState()))
        game.updateBall()

    for x in [failBox, statBox] + lines:
        x.undraw()
    return (game.bounces)
Пример #2
0
def register_all():
    if led_cube is not None:
        led_cube.register(Snake.Snake(api.cubeSize, frame_size),
                          Pong.Pong(api.cubeSize, frame_size),
                          PongMulti.PongMulti(api.cubeSize, frame_size),
                          Weather.Weather(api.cubeSize, frame_size),
                          FFT.AudioVis(api.cubeSize, frame_size),
                          Exit.Exit(api.cubeSize, frame_size))
Пример #3
0
    def run(self, teamname, oppname=None):
        self.name = teamname
        self._log.info("Starting game!")
        self.lastMove = 0
        self.myDirection = 0
        self.lastY = 0
        if oppname is None:
            self.game = Pong.PongGame(self._log, teamname)
            self._connection.send({'msgType': 'join', 'data': teamname})
        else:
            self.game = Pong.PongGame(self._log, teamname, oppname)
            self._connection.send({
                'msgType': 'requestDuel',
                'data': [teamname, oppname]
            })

        self._response_loop()
Пример #4
0
    def draw(self):
        if len(self.Game) == 2:  # counts if the game hasnt changed
            pyxel.cls(7)

            ''' text on screen'''
            pyxel.text(90, 40, "Adventure", 0)
            pyxel.text(90, 60, "Pong", 0)
            pyxel.text(0, 0, f'{datetime.datetime.now()}', 0)
            if self.choice == 1:  # shows the choice the player can select
                pyxel.rectb(85, 35, 50, 15, 0)
            if self.choice == 2:  # shows the choice the player can select
                pyxel.rectb(85, 55, 50, 15, 0)
        elif self.Game == {2}:  # starts the adventure game
            Adventure.App()
            Adventure.API_find()
        elif self.Game == {1}:  # starts pong
            Pong.Start()
        else:  # error occured
            logging.FATAL("game softlocked ")
            raise Game_exception("Game doesnt equel 2 or 1")
Пример #5
0
def trainGraph(inp, out, sess):
    # to calculate the argmax, we multiply the predicted output with a vector with one value 1 and rest as 0
    argmax = tf.placeholder("float", [None, ACTIONS])
    gt = tf.placeholder("float", [None])  # ground truth

    # action
    action = tf.reduce_sum(tf.multiply(out, argmax), reduction_indices=1)
    # cost function we will reduce through backpropagation
    cost = tf.reduce_mean(tf.square(action - gt))
    # optimization fucntion to reduce our minimize our cost function
    train_step = tf.train.AdamOptimizer(1e-6).minimize(cost)

    # initialize our game
    game = Pong.PongGame()

    # create a queue for experience replay to store policies
    D = deque()

    # intial frame
    frame = game.getPresentFrame()
    # convert rgb to gray scale for processing
    frame = cv2.cvtColor(cv2.resize(frame, (84, 84)), cv2.COLOR_BGR2GRAY)
    # binary colors, black or white
    ret, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY)
    # stack frames, that is our input tensor
    inp_t = np.stack((frame, frame, frame, frame), axis=2)

    # saver
    saver = tf.train.Saver()

    sess.run(tf.initialize_all_variables())

    t = 0
    epsilon = INITIAL_EPSILON

    # training time
    while (1):
        # output tensor
        out_t = out.eval(feed_dict={inp: [inp_t]})[0]
        # argmax function
        argmax_t = np.zeros([ACTIONS])

        #
        if (random.random() <= epsilon):
            maxIndex = random.randrange(ACTIONS)
        else:
            maxIndex = np.argmax(out_t)
        argmax_t[maxIndex] = 1

        if epsilon > FINAL_EPSILON:
            epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE

        # reward tensor if score is positive
        reward_t, frame, reward_dist = game.getNextFrame(argmax_t)
        # get frame pixel data
        frame = cv2.cvtColor(cv2.resize(frame, (84, 84)), cv2.COLOR_BGR2GRAY)
        ret, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY)
        frame = np.reshape(frame, (84, 84, 1))
        # new input tensor
        inp_t1 = np.append(frame, inp_t[:, :, 0:3], axis=2)

        # add our input tensor, argmax tensor, reward and updated input tensor tos tack of experiences
        D.append((inp_t, argmax_t, reward_t, inp_t1, reward_dist))

        # if we run out of replay memory, make room
        if len(D) > REPLAY_MEMORY:
            D.popleft()

        # training iteration
        if t > OBSERVE:

            # get values from our replay memory
            minibatch = random.sample(D, BATCH)

            inp_batch = [d[0] for d in minibatch]
            argmax_batch = [d[1] for d in minibatch]
            reward_batch = [d[2] for d in minibatch]
            inp_t1_batch = [d[3] for d in minibatch]

            gt_batch = []
            out_batch = out.eval(feed_dict={inp: inp_t1_batch})

            # add values to our batch
            for i in range(0, len(minibatch)):
                gt_batch.append(reward_batch[i] + lr * np.max(out_batch[i]))

            # train on that
            train_step.run(feed_dict={
                gt: gt_batch,
                argmax: argmax_batch,
                inp: inp_batch
            })

        # update our input tensor the the next frame
        inp_t = inp_t1
        t = t + 1

        # print our where wer are after saving where we are
        if t % 10000 == 0:
            saver.save(sess, './' + 'pong' + '-dqn', global_step=t)

        print("TIMESTEP", t, "/ EPSILON", epsilon, "/ ACTION", maxIndex,
              "/ REWARD", reward_t, "/ Q_MAX %e" % np.max(out_t))
Пример #6
0
# Autorzy - Norbert Daniluk, Piotr Palczewski
# Projekt gry Pong

import Pong

Pong.Pong().play()
Пример #7
0
def main():
    #Preparam les mides de les xarxes
    Imputs = [IMPUTS1, IMPUTS2, IMPUTS3]
    HiddenLayer = [4, 8, 15, 45]

    #Cream la llista que emagetzamara el nombre de generacions de cada xaxa
    gen = []

    #Inicialitzam el Joc
    game = Pong.PongGame()

    #Copiam la posicio inicial de e la pilota a les nostres variables de imput
    bx = game.getballXposition()
    by = game.getballYposition()
    bx1 = bx
    by1 = by
    bx2 = bx
    by2 = by
    bx3 = bx
    by3 = by
    bx4 = bx
    by4 = by

    IM = 0
    while (IM < Imputs.__len__()):
        HL = 0
        while (HL < HiddenLayer.__len__()):
            d = 0
            while (d < 20):
                #Inicialitzam la xarxa
                sizes = [Imputs[IM], HiddenLayer[HL], ACTIONS]

                #Guarda el nombre maxim de cops de la ultima generacio.
                max_hit = 0

                #Cream les llistes on enmagatzamarem les millors xarxes amb el nombre de cops que han assolit
                best_nets = []
                best_hits = []

                #Cream els 2 arrays que emprarem per a les generacions
                nets = []
                n = 0

                while (n < NUM_XARXES):
                    nets.append(Neural.Network(sizes))
                    n = n + 1

                hits = np.zeros(NUM_XARXES, dtype=np.int16)
                while (max_hit < HITS_GOAL and best_hits.__len__() < 3000):
                    x = 0
                    start = time.time()
                    while (x < NUM_XARXES and max_hit < HITS_GOAL):
                        if (x != 0):
                            start = time.time()
                        hits[x] = 0
                        score = -2
                        while (score != -1 and max_hit < HITS_GOAL):
                            if ((score != -2 and x != 0) or score != -2):
                                start = time.time()
                            # Copiam la posicio actual de la paleta de la qual decidim l'accio
                            px, py = game.getPaddlePos()
                            if (IM == 0):
                                #Cream l'array(matriu (n,1)) de imputs de la xarxa
                                a = np.asarray([[bx], [by], [bx1], [by1],
                                                [bx2], [by2], [bx3], [by3],
                                                [bx4], [by4], [px], [py]])
                            elif (IM == 1):
                                a = np.asarray([[bx], [by], [px], [py]])
                            else:
                                a = np.asarray([[bx], [by], [bx1], [by1],
                                                [bx2], [by2], [px], [py]])
                            # Decidim l'accio cridant a la Xarxa
                            Pa = nets[x].feedforward(a)
                            action = getaction(Pa)
                            # Actualitzam el Frame amb la nova accio
                            score, Posx, Posy = game.getNextFrame(action)
                            bx, by, bx1, by1, bx2, by2, bx3, by3, bx4, by4 = UpdateImputs(
                                bx, by, bx1, by1, bx2, by2, bx3, by3, Posx,
                                Posy)
                            #Si la pala ha pegat a la pilota, aumentam els hits de la xarxa
                            if (game.gethit()):
                                hits[x] = hits[x] + 1
                                if (hits[x] > max_hit):
                                    max_hit = hits[x]
                            #time.sleep(max(1. / game.getFPS() - (time.time() - start), 0))

                        x = x + 1
                    #Sort
                    i = 0
                    while (i < hits.__len__()):
                        j = i
                        while (j < hits.__len__()):
                            if (hits[i] < hits[j]):
                                auxc = hits[i]
                                hits[i] = hits[j]
                                hits[j] = auxc
                                auxn = nets[i]
                                nets[i] = nets[j]
                                nets[j] = auxn
                            j = j + 1
                        i = i + 1
                    #Emmagatzemam la xarxa i el nombre de cops
                    best_hits.append(hits[0])
                    best_nets.append(nets[0])
                    #Si cap xarxa a copetjat, generam noves aleatoriament
                    if (hits[0] == 0):
                        n = 0
                        while (n < NUM_XARXES):
                            nets[n] = Neural.Network(sizes)
                            n = n + 1
                    elif (max_hit < HITS_GOAL):
                        #Copiam les xarxes
                        copy_nets = nets
                        #Recorreguem les xarxes
                        for i, n in enumerate(nets):
                            if (i < nets.__len__() * 0.8):
                                #Numero aleatori de 0 a la suma de tots el hits
                                num = random.randint(0, hits.sum())
                                aux = 0
                                # Seleccionam l'index de la xarxa corresponent
                                while (num > hits[aux]):
                                    num = num - hits[aux]
                                    aux = aux + 1
                                #Generam la nova xarxa filla de la xarxa de l'index
                                nets[i] = copy_nets[aux].generate_child()
                            else:
                                # Numero aleatori de 0 a la suma de tots el hits
                                num = random.randint(0, nets.__len__())
                                aux = 1
                                # Seleccionam l'index de la xarxa corresponent
                                while (num > aux):
                                    aux = aux + 1
                                # Generam la nova xarxa filla de la xarxa de l'index
                                nets[i] = copy_nets[aux - 1].generate_child()

                if (best_hits.__len__() == 3000 and max_hit != 20):
                    print("/Case discarded, too much generacions", "/Imputs:",
                          Imputs[IM], " /Layer: ", HiddenLayer[HL], " /Length",
                          best_hits.__len__())
                    gen.append(-1)
                else:
                    print(
                        "/Imputs:",
                        Imputs[IM],
                        " /Layer: ",
                        HiddenLayer[HL],
                        " /Length",
                        best_hits.__len__(),
                    )
                    gen.append(best_hits.__len__())
                d = d + 1
            HL = HL + 1
        IM = IM + 1
    print(gen)
Пример #8
0
# Action List
actions = [1, 0, -1]
alpha = 0.2
gamma = 0.9
randomness = 200

qLearn = QLearn.QLearn(actions, randomness, 1.0, alpha, gamma)
trials = 100000

wins = 0
for i in range(trials):

    qLearn.epsilon = (trials - i) / trials

    pong = Pong.Pong(14, 14)
    pong.create_ball(0.5, 0.5, 0.03, 0.01)
    pong.create_paddle(0.5 - 0.2 / 2, 0.2, 0.04, 1)
    pong.create_paddle(0.5 - 0.2 / 2, 0.2, 0.02, 2)

    old_state = None

    # Make values discrete
    dis_x = int(pong.ball.x * (pong.board_x_size - 1))

    dis_y = int(pong.ball.y * (pong.board_y_size - 1))

    if pong.ball.x_velocity > 0:
        dis_x_velocity = 1
    else:
        dis_x_velocity = -1
Пример #9
0
#!/usr/bin/python3

import Pong

Pong.main()

Пример #10
0
 def start_game(self):
     player_data = self.get_info()
     self.main_window.destroy()
     Pong.main(player_data)
Пример #11
0
import tensorflow as tf
import numpy as np
import Pong
import random

print("Hello TensorFlow")

array = np.array([1, 2, 3])
print(array)
print(array.reshape([1, 3]))

### This doesn't work, it's just an example I found online that I tried to adapt.
### If you want to see the Pong simulation in action, run Pong.py as main.

statespace_size = len(Pong.flat())
actionspace_size = len(Pong.DIRECTIONS)
#actionspace_size = 1

#These lines establish the feed-forward part of the network used to choose actions
inputs1 = tf.placeholder(shape=[1, statespace_size], dtype=tf.float32)
W = tf.Variable(tf.random_uniform([statespace_size, actionspace_size], 0,
                                  0.01))  #weights
Qout = tf.matmul(inputs1, W)
predict = tf.argmax(Qout, 1)

#Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
nextQ = tf.placeholder(shape=[1, actionspace_size], dtype=tf.float32)
loss = tf.reduce_sum(tf.square(nextQ - Qout))
trainer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
updateModel = trainer.minimize(loss)
Пример #12
0
import Pong

if __name__ == "__main__":
    application = Pong.Pong()
    application.start()
    AI2.Stop()
    #AI1.Save()
    #AI2.Save()
    print('Stop Pong AI...')


atexit.register(exit_handler)

#STARTING APPLICATION
if __name__ == '__main__':

    print('\nStart Pong AI...')
    second = True

    #PONG INITIALIZE
    Pong.Init()

    #AI CREATE
    AI1 = NEAT.AI('L', 50)
    AI2 = NEAT.AI('R', 100)

    #AI INITIALIZE INPUTS
    Offset = Pong.HEIGHT - Pong.PAD_HEIGHT / 2
    AI1.Inputs = [0]
    AI2.Inputs = [0]
    AI1.Startup(['Up', 'Down'])
    AI2.Startup(['Up', 'Down'])

    LastScoreL = 0
    LastScoreR = 0
    LastKeyL = 'x'  #LAST KEY LEFT
Пример #14
0
 def __challengeOnPress(self):
     # dissable it
     self.challenge.configure(state="disabled")
     # get its background color
     bg = self.challenge.cget("bg")
     # if it is blue, player two challenged
     if bg == "blue":
         # make a new pong object and play
         pong = Pong.Pong()
         # pass a two into the play function to tell pong that player two challenged
         pongWin = pong.play(2)
         #  if player two wins the pong match
         if pongWin == 1:
             # set the spot that was player one's to player two's because he won
             self.buttons[self.currentMove].configure(state="disabled",
                                                      image=self.OIMG)
             # update the dictionary that holds the positions
             self.buttonsPictures[self.currentMove] = "O"
             # player two's turn again
             self.player = False
             # check for a game winning move
             gameWin = self.__checkWin("O")
             # if that was a game winning move make a crappy looking popup
             # and say congrats to player two. Also dissables the root window
             if gameWin == 1:
                 finWin = tk.Toplevel()
                 finWin.grab_set()
                 finWin.grab_set()
                 finWin.protocol('WM_DELETE_WINDOW', quit)
                 lbl = tk.Label(finWin,
                                text="Player 2 Wins!!",
                                font=("Courier", 44)).grid(row=10,
                                                           column=10)
             # if it was the last move possible and no one won, make a crappy
             # popup saying it was a tie
             elif gameWin == 0:
                 finWin = tk.Toplevel()
                 finWin.grab_set()
                 finWin.grab_set()
                 finWin.protocol('WM_DELETE_WINDOW', quit)
                 lbl = tk.Label(finWin, text="Tie!!",
                                font=("Courier", 44)).grid(row=10,
                                                           column=10)
         # if player two lost the pong game, shame him by making it player one's turn
         if pongWin == 2:
             self.player = True
     # if background of challenge button is red, player one challenged
     elif bg == "red":
         pong = Pong.Pong()
         pongWin = pong.play(1)
         #  if player one wins the pong match
         if pongWin == 1:
             # set the spot that was player two's to player one's because he won
             self.buttons[self.currentMove].configure(state="disabled",
                                                      image=self.XIMG)
             # update the dictionary that holds the positions
             self.buttonsPictures[self.currentMove] = "X"
             # player one's turn again
             self.player = True
             # check for a game winning move
             gameWin = self.__checkWin("X")
             # if that was a game winning move make a crappy looking popup
             # and say congrats to player one. Also dissables the root window
             if gameWin == 1:
                 finWin = tk.Toplevel()
                 finWin.grab_set()
                 finWin.protocol('WM_DELETE_WINDOW', quit)
                 lbl = tk.Label(finWin,
                                text="Player 1 Wins!!",
                                font=("Courier", 44)).grid(row=10,
                                                           column=10)
             # if it was the last move possible and no one won, make a crappy
             # popup saying it was a tie
             elif gameWin == 0:
                 finWin = tk.Toplevel()
                 finWin.grab_set()
                 finWin.protocol('WM_DELETE_WINDOW', quit)
                 lbl = tk.Label(finWin, text="Tie!!",
                                font=("Courier", 44)).grid(row=10,
                                                           column=10)
         # if player one lost the pong game, shame him by making it player two's turn
         if pongWin == 2:
             self.player = False
Пример #15
0
def enter_game():
    import Pong
    Pong.player1name = player1entry.get()
    Pong.player2name = player2entry.get()
    Pong.main()