def buildRandomExperience(self, layoutName, displayActive=False, limit=None): import pacmanAgents, ghostAgents from pacman import ClassicGameRules import layout theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") display = None # Choose a display format if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) counter = 0 finished = False while not finished: rules = ClassicGameRules() agents = [pacmanAgents.RandomAgent()] + [ ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts()) ] game = rules.newGame(theLayout, agents[0], agents[1:], display) currentState = game.state display.initialize(currentState.data) while not (currentState.isWin() or currentState.isLose()): action = agents[0].getAction(currentState) newState = util.getSuccessor(agents, display, currentState, action) reward = newState.data.score - currentState.data.score self.remember(currentState, action, reward, newState) currentState = newState counter += 1 if counter % 100 == 0: self.persist() if counter % 2000 == 0: print("Explored " + str(counter) + " states") if limit is not None and counter > limit: finished = True display.finish() self.persist() self.replayMemory.close() print("Done")
def main(): ni.ifaddresses('eth0') HOST = str(ni.ifaddresses('eth0')[2][0]['addr']) PORT = 12420 try: global myID, mySuccessor, myPredecessor, myFingerTable, myPredecessorIP, mySuccessorIP, myIP mutex.acquire() myIP = HOST myID = util.generateID(HOST) print 'myip:', HOST, myID util.generateConfFileList() myFingerTable = util.generateFingerTable(myID) mySuccessor = util.getSuccessor(myFingerTable) myPredecessor = util.getPredecessor(myID) mySuccessorIP = (util.getSuccessorIP(myFingerTable)) myPredecessorIP = (util.getPredecessorIP(myID)) mutex.release() print "Server ID:", myID print myFingerTable, mySuccessor, myPredecessor except: print sys.exc_info()[0] print "Initialization Failed, Node Shutting down.." if mutex.locked(): mutex.release() sys.exit() start_new_thread(checkStatus, ()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created' #Bind socket to local host and port try: s.bind((HOST, PORT)) except socket.error as msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete' #Number of connections the OS will keep in queue while serving the current one s.listen(5) print 'Socket now listening\n' try: #Continuously accept Client Connections while 1: conn, addr = s.accept( ) #wait to accept a connection - blocking call print 'Connected with ' + addr[0] + ':' + str(addr[1]) start_new_thread( clientThreadStart, (conn, )) #start a new thread for each client connection except KeyboardInterrupt: print "Forced Stop" s.close()
def checkStatus(): """ Responsible for figuring out node Failures. Checks the Status of other nodes present in this nodes fingerTable. If a connection fails, that node is removed from the fingerTable and all necessary parameters are recomputed. Raises: SocketError: if connection fails for any reason """ try: time.sleep(20) print "Entered checkStatus" global myFingerTable, myPredecessor, mySuccessor, myID, myPredecessorIP, mySuccessorIP, myIP, seenBefore, port while True: print 'ok' mutex.acquire() confList = util.getConfFileList()[:] for i in range(0, len(confList)): host = confList[i][1] if host == myIP: #It'd be stupid to remove yourself from your FingerTable. continue try: ns = util.connectToServer(host, port, myIP) except: #Failed to connect to host ns = False #if ns is false, remove host from fingerTable and recompute params if ns == False: util.removeFromConfFileListByIP(host) myFingerTable = util.generateFingerTable(myID) mySuccessor = util.getSuccessor(myFingerTable) myPredecessor = util.getPredecessor(myID) mySuccessorIP = (util.getSuccessorIP(myFingerTable)) myPredecessorIP = (util.getPredecessorIP(myID)) print 'removed:', host, myFingerTable, util.getConfFileList( ) else: print 'host alive:', host ns.close() mutex.release() time.sleep(3) except: e = sys.exc_info()[0] print e if mutex.locked(): mutex.release() print "status thread dead" start_new_thread(checkStatus, ( )) #restart statusThread, essential for figuring out node failures.
def playOnce(self, displayActive): """ Play one game with what we have learned so far. Actions are taken with an epsilon of 0 :param displayActive: True or False to indicate if the display should be active :return: The score achieved in the game after winning or loosing. """ game = self.makeGame(displayActive=displayActive) currentState = game.state game.display.initialize(currentState.data) while not (currentState.isWin() or currentState.isLose()): action = self.qFuncManager.getAction(currentState, epsilon=0) currentState = util.getSuccessor(game.agents, game.display, currentState, action) return currentState.getScore()
def clientThreadStart(conn): """ Start function for the ChordDBNode threads. Handles the GET,PUT,REMOVE and LEAVE requests. Responsible for initiating ReadFrom or WriteTo Disk. Args: conn: the socket connection initiated with the client Returns: reply: either Key not found, Value for key in GET, node failure reason or echoed data from client on PUT Raises: SocketError: if connection fails at any time or sequence of send/recv not followed TypeError: if data not correctly serialized or desearialized (JSON) """ try: while 1: conn.send( 'Welcome to the ChordDB server 1.') #send only takes string dataFromClient = json.loads( conn.recv(1024)) #read what the client sent keyID = util.generateID(dataFromClient['KEY']) print "keyID:", keyID print "data from client:" print dataFromClient global myFingerTable, myPredecessor, mySuccessor, myID, myPredecessorIP, mySuccessorIP, myIP, seenBefore dataFromClientMethod = dataFromClient['METHOD'] dataFromClientValue = dataFromClient['VALUE'] dataFromClientKey = dataFromClient['KEY'] reply = dataFromClient mutex.acquire() if util.isResponsibleForKeyID( keyID, myID, myPredecessor, mySuccessor ) or dataFromClientMethod == 'LEAVE' or dataFromClientMethod == 'REMOVE' or dataFromClientMethod == 'PUTn': #Forced True just to enter if condition (remove True after writing util.isResponsibleForKeyID method) print 'Responsible for Key ID: ', keyID mutex.release() #check if key value pair present on disk and return if dataFromClientMethod == 'GET': readWriteMutex.acquire() reply = util.getFromDisk(dataFromClientKey, myID) readWriteMutex.release() pass #write or update key value pair on disk elif dataFromClientMethod == 'PUT': readWriteMutex.acquire() try: util.writeToDisk(dataFromClientKey, dataFromClientValue, myID) except Exception, e: print str(e) readWriteMutex.release() pass #auxiliary method used to transfer keys during voluntary node leave elif dataFromClientMethod == 'PUTn': readWriteMutex.acquire() util.writeToDisk(dataFromClientKey, dataFromClientValue, myID) reply = 'OK' readWriteMutex.release() #initiate node leave elif dataFromClientMethod == 'LEAVE': readWriteMutex.acquire() util.leaveCluster(myID, mySuccessorIP, myPredecessorIP, myIP) readWriteMutex.release() print 'LEAVE Successful' conn.send(json.dumps('LEAVE Successful')) conn.close() os._exit(1) pass #remove the node whose id is in dataFromClientKey elif dataFromClientMethod == 'REMOVE': print 'Removing Node from Cluster' mutex.acquire() util.removeFromConfFileList(dataFromClientKey) myFingerTable = util.generateFingerTable(myID) mySuccessor = util.getSuccessor(myFingerTable) myPredecessor = util.getPredecessor(myID) mySuccessorIP = (util.getSuccessorIP(myFingerTable)) myPredecessorIP = (util.getPredecessorIP(myID)) mutex.release() print myFingerTable, mySuccessor, myPredecessor, mySuccessorIP, myPredecessorIP else: reply['METHOD'] = 'Invalid METHOD' #node not responsible for the key, initiate connection with next closest node in fingerTable else: try: print 'Not Responsible for Key ID: ', keyID print 'myFingerTable:', myFingerTable nextClosestNodeToKeyIP = util.getClosestNodeIP( keyID, myID, mySuccessorIP, myFingerTable) print 'nextClosestNodeToKeyIP:', nextClosestNodeToKeyIP mutex.release() nextClosestNodeToKeyPort = 12420 newSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) newSocket.bind((myIP, 0)) newSocket.connect( (nextClosestNodeToKeyIP, nextClosestNodeToKeyPort)) welcomeMsg = newSocket.recv( 1024) #just to maintain sequence of send/recv jDump = json.dumps(dataFromClient) newSocket.send(jDump) reply = json.loads(newSocket.recv(1024)) newSocket.close() except: reply = 'Node Down, Try again in a few seconds' print "Closing newSocket because of Exception" if mutex.locked(): mutex.release() newSocket.close() conn.send(json.dumps(reply)) except: e = sys.exc_info()[0] if readWriteMutex.locked(): readWriteMutex.release() if mutex.locked(): mutex.release() conn.close() print "Closing Connection\n"
def _train(self): startTime = time.time() print("Beginning " + str(self.trainingEpisodes) + " training episodes") print("Collecting minimum experience before training...") game = self.makeGame(displayActive=False) currentState = game.state episodes = 0 trainingLossSum = 0 accuracySum = 0 totalWins = 0 totalDeaths = 0 lastEpisodesRewardSum = 0 lastEpisodesDeaths = 0 lastEpisodesWins = 0 while episodes < self.trainingEpisodes: # Get an action action = self.qFuncManager.getAction(currentState, epsilon=self.epsilon) # Execute it newState = util.getSuccessor(game.agents, game.display, currentState, action) # Find the reward reward = newState.getScore() - currentState.getScore() if isinstance(self.qFuncManager, qFunctionManagers.NNQFunctionManager): # If we have a NNQFunctionManager we need to pre-calculate the processed state representations qState = self.featuresExtractor.getFeatures(state=currentState, action=None) newQState = self.featuresExtractor.getFeatures(state=newState, action=None) experience = (qState, action, reward, newQState, newState.isWin() or newState.isLose(), newState.getLegalActions()) else: experience = (currentState, action, reward, newState) # Update replay memory self.replayMemory.append(experience) if len(self.replayMemory) > self.memoryLimit: self.replayMemory.pop(0) # Test: Prioritize relevant experiences a bit # if abs(reward) > 1: # for _ in range(4 if abs(reward) <= 20 else 10): # self.replayMemory.append(experience) currentState = newState lastEpisodesRewardSum += reward # If the game is over create a new one if newState.isWin() or newState.isLose(): game = self.makeGame(displayActive=False) currentState = game.state if newState.isWin(): totalWins += 1 else: totalDeaths += 1 lastEpisodesDeaths += 1 # If we don't have the minimum experience we can not continue if len(self.replayMemory) < self.minExperience: continue # Take a batch from replay memory and train batch = self._sampleReplayBatch() loss, accuracy = self.qFuncManager.update(batch) trainingLossSum += loss accuracySum += accuracy # Decrease epsilon self.epsilon = max( self.finalEpsilon, 1.00 - float(episodes) / float(self.epsilonSteps)) # Bookkeeping if episodes % 100 == 0 and episodes != 0: self._recordCheckpointStats(accuracySum, episodes, lastEpisodesDeaths, lastEpisodesRewardSum, lastEpisodesWins, totalDeaths, totalWins, trainingLossSum) trainingLossSum = 0 accuracySum = 0 lastEpisodesRewardSum = 0 lastEpisodesDeaths = 0 lastEpisodesWins = 0 episodes += 1 # If we are not using experience replay, we clear the transitions we just experienced if not self.useExperienceReplay: self.replayMemory = [] print("Finished training, turning off epsilon...") print("Calculating average score...") self._finishTrainingAndCalculateAvgScore(startTime)
def buildExperience(self, layoutName, displayActive=False, limit=None): import pacmanAgents, ghostAgents from pacman import ClassicGameRules from game import Directions import layout theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") display = None # Choose a display format if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts()) ] game = rules.newGame(theLayout, agents[0], agents[1:], display) initialState = game.state display.initialize(initialState.data) exploredStateHashes = {initialState.__hash__()} pendingStates = {initialState} counter = 0 while pendingStates: pendingState = pendingStates.pop() for action in pendingState.getLegalActions(): if action == Directions.STOP: continue try: # Execute the action newState = util.getSuccessor(agents, display, pendingState, action) reward = newState.data.score - pendingState.data.score self.remember(pendingState, action, reward, newState) counter += 1 if not (newState.isWin() or newState.isLose( )) and newState.__hash__() not in exploredStateHashes: exploredStateHashes.add(newState.__hash__()) pendingStates.add(newState) except Exception, e: #print(e) pass if counter % 100 == 0: self.persist() if counter % 2000 == 0: print("Explored " + str(counter) + " states") if limit is not None and counter > limit: break