def getAction(self, state): # How we access the features. features = api.getFeatureVector( state ) # first 4: walls, next 4: food, next 8: ghosts, next 1: ghost infront, next 1: class # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* # from collected 'features' vector, classify as any of 0-3. # this gives your move # Get class from model and convert to action number = self.model.predict(features) action = self.convertNumberToMove(number) # Get the actions we can try. legal = api.legalActions(state) return api.makeMove(action, legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* nn = self.nn # use the trained neural network to predict y_pred = nn.predict(features) # transform the output value from a list of 0 and 1 to a single value n = 0 for i in range(len(y_pred)): if y_pred[i] > y_pred[n]: n = i # Get the actions we can try. legal = api.legalActions(state) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove(self.convertNumberToMove(n), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. features = np.append(features, 0) number = self.PredictMove(features) move = self.convertNumberToMove(number) # # ******************************************************* # Get the actions we can try. legal = api.legalActions(state) # Add some randomness to not get pacman completely stuck in the corner. if move not in legal: move = random.choice(legal) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove(move, legal)
def getAction(self, state): features = api.getFeatureVector(state) # Return majority classification from ensemble moveNumber = majorityVote(features, self.classifier) legal = api.legalActions(state) return api.makeMove(self.convertNumberToMove(moveNumber), legal)
def getAction(self, state): # Get the current feature vector and identify the legal moves features = api.getFeatureVector(state) legal = api.legalActions(state) # We use the predict method of our tree to predict the best move. move = self.prunedTree.predict(features) move = self.convertNumberToMove(move) print move return api.makeMove(move, legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # This is the test data # ============================================================================= # Calling classifier & metrics # ============================================================================= # Calculating learning metrics for fine tuning of hyperparameters (if needed). if self.score == 0: print "The training accuracy for the full set of data is: %.2f" % ( self.training_score) print "\nThe 0.2 split validation resulted in an accuracy of: %.2f" % ( self.split_score) print "\nThe 10-fold cross validation score for the full set of data is: %.2f" % ( self.cross_val_score) print "\nMy classifer confusion matrix is: \n %s" % (self.matrix) # Comparing metrics with those from scikit learn print "\nThe training accuracy for SCIKIT is: %.2f" % ( self.scikit_score) print "\nThe 10-fold cross validation score for SCIKIT is: %.2f" % ( self.scikit_cross_val) print "\nThe SCIKIT confusion matrix is: \n%s\n" % ( self.scikit_matrix) self.score = 1 # ============================================================================= # Classification of new instances # ============================================================================= # Using Naive Bayes to classify feature vector classification = self.Naive_Bayes_Test(features) # Turning the classifcation into a move. best_move = self.convertNumberToMove(classification) # Get the actions we can try. legal = api.legalActions(state) # Checks to see if it's possible to make that move. If not, it will try the next best move based on the probability. If no moves are possible it will return a random move. if best_move in legal: return api.makeMove(best_move, legal) else: return api.makeMove(random.choice(legal), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # Call Classifier to decide which move to make action = self.NB_ClassifyFeature(features, self.PConditional, self.Priors)['action'] # ******************************************************* # Get the actions we can try. legal = api.legalActions(state) if action in legal: return api.makeMove(action, legal) else: print 'Classifier predicted an illegal move, picking random!' return api.makeMove(random.choice(legal), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # test Likelihood, Prior = self.buildNBmodel() actionum = self.testNBmodel(features, self.Prior, self.Likelihood) # convert actionum into direction action = self.convertNumberToMove(actionum) # Get the actions we can try. legal = api.legalActions(state) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. if action in legal: return api.makeMove(action, legal) else: return api.makeMove(random.choice(legal), legal)
def getAction(self, state): # How we access the features. features = api.getFeatureVector(state) # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* # Get the actions we can try. legal = api.legalActions(state) #Get the best number by inference # best_number = self.inference_RF(features) values, freq = self.inference_RF(features) best_index = np.argmax(freq) best_number = values[best_index] best_action = self.convertNumberToMove(best_number) while best_action not in legal: values = np.delete(values,best_index) freq = np.delete(freq,best_index) print(freq) best_index = np.argmax(freq) best_number = values[best_index] best_action = self.convertNumberToMove(best_number) # Sometime the best action is not legal! # for index in len(values): # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove(best_action, legal)
def getAction(self, state): ### classification function def classifyDT(x, DTnode): if isinstance(DTnode, self.Leaf): return DTnode.pred if DTnode.evaluation.findMatch(x): return classifyDT(x, DTnode.truePart) else: return classifyDT(x, DTnode.falsePart) # How we access the features. features = np.array(api.getFeatureVector(state)) results = classifyDT(features, self.DT) pred = max( results, key=results.get) #get the result with the highest probability print results print pred # ***************************************************** # # Here you should insert code to call the classifier to # decide what to do based on features and use it to decide # what action to take. # # ******************************************************* # Get the actions we can try. legal = api.legalActions(state) # getAction has to return a move. Here we pass "STOP" to the # API to ask Pacman to stay where they are. We need to pass # the set of legal moves to teh API so it can do some safety # checking. return api.makeMove( self.convertNumberToMove(pred), legal ) #use the convertNumberToMove function to translate our intended moves