Beispiel #1
0
    def work(self):

        net = neat.nn.recurrent.RecurrentNetwork.create(
            self.genome, self.config)
        self.makenewgame()
        current_max_fitness = 0
        gamenumber = 0
        done = False
        self.game = tictactoe.game()

        while not done:

            if self.start % 2 and not self.game.win(self.moves, self.fieldX,
                                                    self.fieldO):
                nnOutput = net.activate(self.posarray)

                self.game.tictactoeX(decision_nn(nnOutput), self.start,
                                     self.moves, self.fieldX, self.fieldO,
                                     self.dict)

                current_max_fitness = current_max_fitness + self.game.fit
                self.return_values()

            if not self.start % 2 and not self.game.win(
                    self.moves, self.fieldX, self.fieldO):

                self.game.tictactoeO(self.start, self.moves, self.fieldX,
                                     self.fieldO, self.dict)

                self.posarray = return_pos(self.fieldX, self.fieldO)
                self.return_values()

            if not self.game.fit == 0:
                current_max_fitness = current_max_fitness + 5
            else:
                gamenumber += 1
                self.makenewgame()

            if self.game.win(self.moves, self.fieldX, self.fieldO):
                gamenumber += 1
                self.makenewgame()

            if gamenumber == 30:
                done = True
        final_fitness = current_max_fitness / 30
        return final_fitness
Beispiel #2
0
def server_program():
    host = socket.gethostname()
    print("Host name: " + str(host))

    if (len(sys.argv) != 2):
        print("Usage: python server.py <port_number>")
        sys.exit()

    port = int(sys.argv[1])

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_socket.bind((host, port))

    server_socket.listen(5)

    while True:
        conn, address = server_socket.accept()

        print("Connection from: " + str(address))

        totalGame = 0
        lastResult = "us"
        serverWin = 0
        clientWin = 0
        message = conn.recv(1024)

        while str(message).strip() != '.':
            if not message:
                break

            if (message == "HISTORY"):
                conn.send(
                    "We have played %d games. You won %d games and I won %d games. The winner of last game is %s."
                    % (totalGame, clientWin, serverWin, lastResult))
                message = conn.recv(1024)
                # print("We have played %d games. You won %d games and I won %d games. The winner of last game is %s." % (totalGame,clientWin,serverWin,lastResult))

            elif (message == "RPS"):

                conn.send("READY? ")

                readyState = conn.recv(1024)
                if (readyState != "Yes"):
                    print("Not Ready for the game...")
                    continue

                print("Let's Start!")
                totalGame += 1

                shape = raw_input("What shape do you want? ")
                oppoShape = conn.recv(1024)
                winner = whoWin(
                    shape[0], oppoShape[0]
                )  # only use first character to determine who wins

                if (winner == "T"):
                    print("TIE GAME")
                    lastResult = "a tie"
                    conn.send("TIE GAME")
                elif (winner == "S"):
                    print("You won this one!")
                    serverWin += 1
                    lastResult = "me"
                    conn.send("You lost on this one..")
                else:
                    print("You lost on this one..")
                    clientWin += 1
                    lastResult = "you"
                    conn.send("You won this one!")

                message = conn.recv(1024)

            elif (message == "TTT"):
                totalGame, clientWin, serverWin, lastResult = tictactoe.game(
                    conn, totalGame, clientWin, serverWin, lastResult)

                message = conn.recv(1024)

            else:
                print("Invalid Command!")

        conn.close
        dqn_agent.target_train()
        dqn_agent.epsilon *= dqn_agent.epsilon_decay
        print("Trial: " + str(trial) + " WR: " + str(totalwin / (trial + 1)) +
              "\n")
    """
    plt.plot(timeAxis,stepAxis)
    plt.plot(timeAxis,avgAxis)
    plt.savefig("mountainDDQNsmallnetwork.png")
    """
    dqn_agent.model.save('./ttt50k.h5')


if __name__ == "__main__":
    #main()

    env = game()
    trials = 1000
    trial_len = 20
    dqn_agent = DQNAgent(env=env)
    dqn_agent.model.load_weights('./ttt99k.h5')
    dqn_agent.epsilon = 0
    state = env.reset().reshape(1, env.obsSpace)
    env.render()
    while True:
        action = dqn_agent.act(state)
        newstate, reward, done = env.step(action)
        env.render()
        action = int(input())
        newstate, reward, done = env.step(action)
        env.render()
def main():
    print("It started")
    env = game()
    trials = 100000
    trial_len = 20
    dqn_agent = DQNAgent(env=env)
    bestMan = 200
    timeAxis = []
    stepAxis = []
    avgAxis = []
    totalwin = 0
    totalStep = 0
    tstart = time.time()
    for trial in range(trials):

        cur_state = env.reset().reshape(1, env.obsSpace)
        step = 0
        while True:
            step += 1
            action = dqn_agent.act(cur_state)
            new_state, reward, done = env.step(action)
            new_state = new_state.reshape(1, env.obsSpace)
            totalStep += 1

            if trial % 100 == 0:
                env.render()

            if not done[2]:
                dqn_agent.remember(cur_state, action, reward, new_state, done)
                dqn_agent.replay()
                cur_state = new_state

            else:
                if done[0] and not done[1] and env.turn == -1:
                    reward += 100
                    totalwin += 1
                    dqn_agent.remember(cur_state, action, reward, new_state,
                                       done)
                    dqn_agent.replay()
                    break
                elif done[0] and done[1]:
                    reward += 10
                    dqn_agent.remember(cur_state, action, reward, new_state,
                                       done)
                    dqn_agent.replay()
                    break
                elif not done[0]:
                    possible = []
                    for i in range(9):
                        if env.table[i] == 0:
                            possible.append(i)
                    new_state, rreward, done = env.step(
                        possible[np.random.randint(0, len(possible))])
                    new_state = new_state.reshape(1, env.obsSpace)
                    if trial % 100 == 0:
                        env.render()
                    if done[0] and done[1]:
                        reward += 10
                        dqn_agent.remember(cur_state, action, reward,
                                           new_state, done)
                        dqn_agent.replay()
                        break
                    elif done[0] and not done[1]:

                        dqn_agent.remember(cur_state, action, reward,
                                           new_state, done)
                        dqn_agent.replay()
                        break
                    elif not done[0]:
                        dqn_agent.remember(cur_state, action, reward,
                                           new_state, done)
                        dqn_agent.replay()
                        cur_state = new_state
        """
        if step >= 199:
            print("Episode: " + str(trial) + " failed.")
        else:
            print("Epside: " + str(trial) + " completed." + str(step))
            if step < bestMan:
                print("New best score!!")
                bestMan = step
                dqn_agent.model.save('./MountainCarModels/mountainDDQN.h5')
        print(str(trial))
        tend = time.time()
        timeAxis.append((tend-tstart)/60)
        avgAxis.append(totalStep/(trial+1))
        stepAxis.append(step)
        """

        if trial % 1000 == 0:
            dqn_agent.model.save('./ttt' + str(int(trial / 1000)) + 'k.h5')
        dqn_agent.target_train()
        dqn_agent.epsilon *= dqn_agent.epsilon_decay
        print("Trial: " + str(trial) + " WR: " + str(totalwin / (trial + 1)) +
              "\n")
    """
    plt.plot(timeAxis,stepAxis)
    plt.plot(timeAxis,avgAxis)
    plt.savefig("mountainDDQNsmallnetwork.png")
    """
    dqn_agent.model.save('./ttt50k.h5')
Beispiel #5
0
    pychallenge.py_challenge()

elif ans == 2:
    print("\n Text Adventure it is!")
    typing.tyingClub()

elif ans == 3:
    print("\nGuess my Color it is!")
    from random import randint

    colors = ['red', 'blue', 'yellow', 'purple', 'green', 'orange']
    generator = randint(0, len(colors) - 1)

    guess = input("Please guess a color from the color wheel: ")

    while True:  #you want to keep guessing until you actually get it
        if guess != colors[generator]:
            print("Incorrect. Please try again.")
            guess = input("Please guess a color: ").strip()
        elif guess == colors[generator]:
            break

    print("Correct!  The color is " + colors[generator] + ".")

elif ans == 4:
    tictactoe.game()

elif ans == 5:
    print("\n Goodbye!")
elif ans != "":
    print("\n Not Valid Choice Try again")