コード例 #1
0
#! /usr/bin/env python

import rospy
import sensor_msgs.msg
from agent import qAgent
from geometry_msgs.msg import Twist

from environment import turtleBotEnv
from qTable import qTable
from game import playGame

rospy.init_node('gameMode', log_level=rospy.DEBUG, anonymous=True)
rospy.loginfo("node Created")
env = turtleBotEnv()
qA1 = qAgent(env)
rospy.Subscriber("scan", sensor_msgs.msg.LaserScan, qA1.LaserScanProcess)
qt = qTable(env.numActions)
game = playGame(env, qA1, qt)
rospy.loginfo("Variables Initialized")

qt.epsilon = 1
qt.loadQt()
print(qt.qTable)
game.learning = False

for i in range(3000):
    rospy.loginfo("Game number= " + str(i))
    game.runGame()

    pass
コード例 #2
0
from dotBoxEnv import dotsBoxesEnv
from dotBoxEnv import display
from dotBoxEnv import reward
from qTable import qTable
from agent import agent
from agent import qAgent
from agent import randomAgent
from agent import simpleAgent
from gameController import gameController
from matplotlib import pyplot as plt

dbe = dotsBoxesEnv(3)
qt = qTable(dbe.allActions,dbe.numActions)

agent1 = qAgent([255,0,0], qt.actionDict) #blue
agent2 = qAgent([0,255,0], qt.actionDict) #green
agent1.islearning = True
agent2.islearning = True
dbe.disp.updateDisplay = False
game = gameController(dbe, agent1, agent2)
for gameNo in range(Number_of_Training_Games):
    game.reset()
    game.play(qt, True)
    game.distributeTropy()
#    print("a1: ",agent1.score," a2: ", agent2.score, " draw: ", agent1.draws)
#    dbe.disp.waitForUser()
    if gameNo%100 == 0:
        print("iterations:" , gameNo ,"a1:", agent1.wins, " a2:", agent2.wins, " draw:" , agent1.draws)
    
print("a1:", agent1.wins, " a2:", agent2.wins, " draw:" , agent1.draws)
コード例 #3
0
        numGame += 1
        winPerc = (
            _game.agent1.wins /
            (_game.agent1.wins + _game.agent2.wins + _game.agent1.draws))
        print("win%:", winPerc)
        y.append(winPerc)
        x.append(numGame)
    plt.plot(x, y, 'r-')
    plt.show()
    return _game.agent1.wins, _game.agent2.wins, _game.agent1.draws, _qClass


def saveQtable(size, iterations, qTable):
    fileName = "qTable/qTable_n" + str(size) + "_i" + str(iterations)
    np.save(fileName, qTable)


def loadQtable(fileName):
    return np.load(fileName)


games2Play = 100
env = dotsBoxesEnv(2)
agent1 = qAgent()
agent2 = randomAgent()
qClass = qLearn(env.numStates, env.numActions)
game1 = game(env, agent1, agent2)

qClass.qTable = loadQtable('../qTable/qTable_n3_i10000.npy')
agent1Wins, agent2Wins, draws, qClass = perf_Test(games2Play, game1, qClass)
print(agent1Wins, agent2Wins, draws)
コード例 #4
0
"""
Created on Fri May  3 21:51:08 2019

@author: Oshi
"""

from dotsandboxes import dotsBoxesEnv
from agent import qAgent
from qLearning import qLearn
from game import game
from randomagent import rAgent

games2Play = 100
def train(games2Play,
          _game:qAgent,
          _qClass):
    numGame = 0
    while numGame < games2Play:
        _qClass = _game.playGameAndLearn(_qClass)
        _game.resetGame()
        numGame+=1
    return _game.agent1.wins, _game.agent2.wins, _game.agent1.draws, _qClass
    
env = dotsBoxesEnv(2)# cahnge the number to 2 for 2x2 training or 3 for 3x3 training
agent1 = qAgent() 
agent2 = qAgent()#change to random agent for random game training(rAgent)
qClass = qLearn(env.numStates, env.numActions)
game1 = game(env, agent1, agent2)

agent1Wins, agent2Wins, draws, qClass = train(games2Play,game1,qClass)
print(agent1Wins, agent2Wins, draws)
コード例 #5
0
    while numGame < games2Play:
        _qClass = _game.playGameAndLearn(_qClass)
        _game.resetGame()
        numGame+=1
        if numGame in i:
             saveQtable(_game.env.size, numGame, _qClass.qTable)
             print("saved")
        winPerc = (_game.agent1.wins/(_game.agent1.wins + _game.agent2.wins +_game.agent1.draws))
        print("win%:", winPerc)
        y.append(winPerc)
        x.append(numGame)
    plt.plot(x,y,'r-')
    plt.show()
    return _game.agent1.wins, _game.agent2.wins, _game.agent1.draws, _qClass

def saveQtable(size, iterations, qTable):
    fileName = "../qTable/qTable_n"+ str(size)+ "_i" + str(iterations)
    np.save(fileName, qTable)
    
def loadQtable(fileName):
    return np.load('fileName')
 
games2Play = 10000
env = dotsBoxesEnv(2)
agent1 = qAgent()
agent2 = qAgent()
qClass = qLearn(env.numStates, env.numActions)
game1 = game(env, agent1, agent2)

agent1Wins, agent2Wins, draws, qClass = train(games2Play,game1,qClass)
print(agent1Wins, agent2Wins, draws)