示例#1
0
def TrainLargeAgent(NrGames, writeOn, savePath, show=False):
    from LargeAgent import LargeAgent as Agent

    #make the enviroment
    env = Enviroment(enhancedReward=True, autoFire=True)
    #because of the influence of tensorflow draw the enviroment once before making any call to tensorflow of don't draw at all
    if (show):
        env.reset()
        env.render()

    #make the agent
    agent = Agent()

    #see how many times to save the agent
    NrBatches = int(NrGames / float(writeOn)) + 1

    totalNrFrames = 0
    #Train some games then save the results keep repeating until all number of games have been played
    for i in range(NrBatches):
        print "batch", i, "of", NrBatches
        #train the agent
        reward, steps = trainAgent(env, agent, writeOn, show=show)

        totalNrFrames += np.sum(steps)
        #save the itermediate agent
        agent.save(savePath + "_" + str(i * writeOn))

        #save the stats
        f = open(savePath, 'a')
        for r, s in zip(reward, steps):
            f.write(str(r) + "," + str(s) + '\n')
        f.close()

        env.makeVideo(savePath + "_" + str(i * writeOn) + ".gif")
示例#2
0
def TrainSmallAgent(NrGames, writeOn, savePath, show=False):
    from SmallAgent import SmallAgent as Agent

    #make the enviroment
    env = Enviroment(fullImage=False, enhancedReward=True, autoFire=True)
    if (show):
        env.reset()
        env.render()

    agent = Agent()

    #see how many times to save the agent
    NrBatches = int(NrGames / float(writeOn)) + 1

    #Train some games then save the results keep repeating until all number of games have been played
    for i in range(NrBatches):
        print "batch", i, "of", NrBatches
        #train the agent
        reward, steps = trainAgent(env, agent, writeOn, show=show)

        #save the itermediate agent
        agent.save(savePath + "_" + str(i * writeOn))

        #save the stats
        f = open(savePath, 'a')
        for r, s in zip(reward, steps):
            f.write(str(r) + "," + str(s) + '\n')
        f.close()

        env.makeVideo(savePath + "_" + str(i * writeOn) + ".gif")
示例#3
0
        json.dump(
            self.dimensionTable,
            open("./" + folder + '/' + filename + '/' + filename + "dim.table",
                 'w'))
        json.dump(
            self.actionTable,
            open("./" + folder + '/' + filename + '/' + filename + "act.table",
                 'w'))
        # "./" + folder + '/' + filename + '/' + filename)


if (__name__ == "__main__"):
    import time
    from Eviroment import Enviroment

    env = Enviroment(gameMode="vector2", rewardMode="follow", autoFire=True)

    agent = DoraAgent(double=True)

    action = 0

    frame = -1
    skipFrame = 1

    done = False
    S = env.reset()
    while not done:
        frame += 1
        if (frame % skipFrame == 0):
            action = agent.getAction(S)
        S, r, done, info = env.step(action)
示例#4
0
from Eviroment import Enviroment
import numpy as np

if(__name__ == "__main__"):
  from SmallAgent import SmallAgent as Agent
  show = False
  
  #make the enviroment
  env = Enviroment(fullImage=False,enhancedReward=True,autoFire=True)
  if(show):
    env.reset()
    env.render()
  
  agent = Agent.load('')
  
  write = 0
  
  rweards = []
  for i in range(500):
    S = env.reset()
    done = False
    while not done:
      action = agent.getAction(S)
      Snew,reward,done,info = env.step(action)
      agent.update(S,action,Snew,reward,done)
      S = Snew
      if(show):
	env.render()
    if(write == 0):
      write = 1
      env.makeVideo('./randomMovement.gif')
示例#5
0
    return debugAgent()
  
  """
  nothing to save so do nothing
  """
  def save(self,filename):
    print self.gameStates
    np.savetxt(filename +"/X.csv",np.asarray(self.gameStates),delimiter=",")
    np.savetxt(filename +"/Y.csv",self.labels,delimiter=",")
    
  
if(__name__ == "__main__"):
  from Eviroment import Enviroment
  import time
  
  env = Enviroment(Breakout=True,gameMode="vector",rewardMode="label",autoFire=True)
  
  agent = debugAgent()
  batch=0
  done = False
  for game in range(100):
    S = env.reset()
    done = False
  
    while not done:
      action = agent.getAction(S)
      S1,r,done,info = env.step(action)
      agent.update(S,action,S1,r,done, batch)
      S = S1
      
    batch+=1
示例#6
0
    @staticmethod
    def load(filename):
        return randomAgent()

    """
  nothing to save so do nothing
  """

    def save(self, filename):
        pass


if (__name__ == "__main__"):
    from Eviroment import Enviroment

    env = Enviroment(Breakout=True,
                     enhancedReward=True,
                     fullImage=False,
                     autoFire=True)

    agent = designedAgent()

    done = False
    S = env.reset()
    while not done:
        action = agent.getAction(S)
        S, r, done, info = env.step(action)

        env.render()

    env.makeVideo('designedAgent.gif')
示例#7
0

if (__name__ == "__main__"):

    #The number game to train the agent on
    NrOfTrainingGames = 50000
    #save the result after every number game
    writeOn = 10000

    mode = sys.argv[1]
    saveFile = sys.argv[2]
    if (mode == "small"):
        double = sys.argv[3] == "true"
        trajectory = sys.argv[4] == "true"
        games = [
            Enviroment(gameMode="vector", rewardMode="label", autoFire=True),
            Enviroment(Breakout=False,
                       gameMode="vector",
                       rewardMode="label",
                       autoFire=True),
            Enviroment(gameMode="vector", rewardMode="label", autoFire=True)
        ]
        gameNames = ["Breakout_1", "Pong", "Breakout_2"]
        experimentOnSmall(saveFile, games, gameNames, double, trajectory)
    elif (mode == "large"):
        games = [
            Enviroment(gameMode="neural", rewardMode="label", autoFire=True),
            Enviroment(Breakout=False,
                       gameMode="neural",
                       rewardMode="label",
                       autoFire=True),
示例#8
0
# run Pong sim.

import experiment
from Eviroment import Enviroment
from DoraModelTime import DoraAgent as Agent
import numpy as np
import matplotlib as mat
mat.use("AGG")
from matplotlib import pyplot as plt
import pdb

# create the agent and load the saved data.
agent = Agent(double=False)
#actionTable = json.load(open("./" + folder + '/' + filename + '/' + filename + "act.table",'r'))
#dimensionTable = json.load(open("./" + folder + '/' + filename + '/' + filename + "dim.table",'r'))
#agent.actionTable = actionTable
#agent.dimensionTable = dimensionTable
agent = agent.load('Breakout_1_800')
agent.flipWeights("X", "Y")

#
enviroment = Enviroment(Breakout=False,
                        gameMode="vector",
                        rewardMode="follow",
                        autoFire=True)
NrGames = 200
writeOn = 10
savePath = 'Pong1'
experiment.testAgent(enviroment, agent, NrGames, writeOn, savePath, show=True)
示例#9
0
    def load(filename):
        return randomAgent()

    """
  nothing to save so do nothing
  """

    def save(self, filename):
        pass


if (__name__ == "__main__"):
    from Eviroment import Enviroment
    import time

    env = Enviroment(Breakout=True,
                     gameMode="vector2",
                     rewardMode="follow",
                     autoFire=True)

    agent = designedAgent()

    done = False
    S = env.reset()
    while not done:
        action = agent.getAction(S)
        S, r, done, info = env.step(action)

        env.render()

    env.makeVideo('designedAgent.gif')