def _main(f): """ >>> _main('aa') H(Xn, Xn+1) = 0.000 H(Xn+1 | Xn) = 0.000 >>> _main('ab') H(Xn, Xn+1) = 0.000 H(Xn+1 | Xn) = -1.000 >>> _main('abbabaab') H(Xn, Xn+1) = 1.842 H(Xn+1 | Xn) = 0.842 >>> _main('abcacbbca') H(Xn, Xn+1) = 2.500 H(Xn+1 | Xn) = 0.915 >>> _main('aaaaaabcac') H(Xn, Xn+1) = 1.880 H(Xn+1 | Xn) = 0.723 """ text = ''.join(l.strip() for l in f) bigram_pd = Counter(bigrams(text)).to_probability_distribution() unigram_pd = Counter(text).to_probability_distribution() Hx = unigram_pd.entropy() Hxy = bigram_pd.entropy() print 'H(Xn, Xn+1) = %.3f' % Hxy print 'H(Xn+1 | Xn) = %.3f' % (Hxy - Hx)
def aStarSearch(problem, heuristic=nullHeuristic): "Search the node that has the lowest combined cost and heuristic first." "*** YOUR CODE HERE ***" # define the node as (state, trajectory) from util import PriorityQueue from util import Counter queue, seen, f, g = PriorityQueue(), set(), Counter(), Counter() init_state = problem.getStartState() queue.push((init_state, []), 0) g[init_state] = 0 f[init_state] = 0 + heuristic(init_state, problem) while not queue.isEmpty(): state, trajectory = queue.pop() if state in seen: continue seen.add(state) if problem.isGoalState(state): return trajectory else: for successor, action, cost in problem.getSuccessors(state): if successor not in seen: g[successor] = g[state] + cost f[successor] = g[successor] + heuristic(successor, problem) queue.push((successor, trajectory + [action]), f[successor])
def __init__(self, plane, Map=None, actionFn=None, alpha=0.5, gamma=0.95, epsilon=0.1): "You can initialize Q-values here..." # if actionFn == None: # actionFn = lambda state: state.getPossibleActions() self.running = 0 self.actionFn = actionFn self.plane = plane self.Map = Map # self.episodesSoFar = 0 # self.accumTrainRewards = 0.0 # self.accumTestRewards = 0.0 # self.numTraining = int(numTraining) self.epsilon = float(epsilon) self.alpha = float(alpha) self.discount = float(gamma) self.values = Counter() self.oldValues = Counter() # print(plane.position,plane.altitude) self.X, self.Y = self.Map.XYInDistToCoordinate( self.Map.longLaToXYInDist(plane.position)) self.Z = int((plane.altitude - 1000) // self.Map._heightResolution) self.kmX, self.kmY = self.Map.longLaToXYInDist(plane.position) self.kmZ = plane.altitude
def _main(f): """ >>> _main('abaab') unigram header: nbits = 16 unigram message: nbits < 6.854762 bigram header: nbits = 32 bigram message: nbits < 5.203200 >>> _main('bccccba') unigram header: nbits = 24 unigram message: nbits < 11.651541 bigram header: nbits = 72 bigram message: nbits < 7.706959 """ header_bits_per_symbol = 8 text = ''.join(l.strip() for l in f) nsymbols = len(set(text)) unigram_header_size = nsymbols * header_bits_per_symbol print 'unigram header: nbits = %d' % unigram_header_size p_unigram = Counter(text).to_probability_distribution() q_unigram = approximate(p_unigram, header_bits_per_symbol) unigram_textprob = q_unigram.logprob(text) print 'unigram message: nbits < %f' % (2 - unigram_textprob) text_bigrams = bigrams(text) nsymbols = len(set(text))**2 bigram_header_size = nsymbols * header_bits_per_symbol print 'bigram header: nbits = %d' % bigram_header_size p_bigram = Counter(text_bigrams).to_probability_distribution() q_bigram = approximate(p_bigram, header_bits_per_symbol) bigram_textprob = q_bigram.conditional_logprob(q_unigram, text_bigrams) print 'bigram message: nbits < %f' % (2 - bigram_textprob)
def __init__(self): self._counter_map = { "Controller": Counter(), "Switch": Counter(), "Request": Counter(), "Scheduler": Counter(), "DataSource": Counter(), }
def __init__(self, view): # multiple threads often modify a single timetable self.lock = RLock() self.times = dict() self.global_history = SortedTable("tid", "ns") # tid dictionary self.on_add = self._callback_gen(view) self.lost = 0 # generate additional stats counters self.counters = dict(); self.counters["probe"] = Counter() self.counters["size"] = Counter()
def __init__(self): self._horizontalActions = ["L_30", "L_60", "R_60", "R_30", "S"] self._verticalActions = ["up", "down", "None"] self._speedActions = [8, -8, 0] self._heightResolution = 300 self._lengthResolution = 2500 self._widthResolution = 2500 runwayLocation1 = (30.560549, 103.951040) runwayLocation2 = (30.516492, 103.936683) self._airportElevation = 496 self._left = 102.28 self._right = 106.22 self._lower = 29.51 self._upper = 31.13 self._radius = 6371000 self._maxX, self._maxY = self.XYInDistToCoordinate( self.longLaToXYInDist((self._upper, self._right))) self._maxZ = (6000 - 1000) // self._heightResolution # print('maxXYZ:',self._maxX,self._maxY,self._maxZ) self.runwayLocationCoordinate1 = self.XYInDistToCoordinate( self.longLaToXYInDist(runwayLocation1)) self.runwayLocationCoordinate2 = self.XYInDistToCoordinate( self.longLaToXYInDist(runwayLocation2)) self._map = [] for a in range(self._maxX + 1): self._map.append([]) for b in range(self._maxY + 1): self._map[a].append([]) for c in range(self._maxZ + 1): if c != 0: if a == 0 or a == self._maxX or b == 0 or b == self._maxY: self._map[a][b].append(-50) else: self._map[a][b].append(3 * (self._maxZ - c)) elif c == 0: if ((a, b) != self.runwayLocationCoordinate1) and ( (a, b) != self.runwayLocationCoordinate2): self._map[a][b].append(-100) else: self._map[a][b].append(100) self._actions = [] for horizontalAction in self._horizontalActions: for verticalAction in self._verticalActions: for speedAction in self._speedActions: self._actions.append( (horizontalAction, verticalAction, speedAction)) # print("mapsize:",len(self._map),len(self._map[0]),len(self._map[0][0])) # print(self.runwayLocationCoordinate1,self.runwayLocationCoordinate2) # print(self._map[45][74][0],self._map[45][74][1],self._map[45][74][2],self._map[45][74][3]) self.values = Counter() self.tempValue = Counter()
def __init__(self): # original: (30.563688,103.940061),(30.519631,103.936683) # vertical: (30.563688,103.951040),(30.519631,103.936683) 0.003139 # vertical + 20km: (30.560549,103.951040),(30.516492,103.936683) # (30.559156,103.954450) # runway1 = (30.563688,103.936922) # runway2 = (30.519631,103.933544) runway1 = (30.560549, 103.951040) runway2 = (30.516492, 103.936683) way1 = (29.51, 103.836184) way2 = (29.51, 104.051539) self.Left = 102.28 self.Right = 106.22 self.Down = 29.51 self.Up = 31.13 self.R = 6371000 x1, y1 = self.LongLaToXY([self.Right, self.Up]) self.x, self.y = [x1 + 1, y1 + 1] self.z = int((6000 - 1000) / 1000) # self.z = 2 self.whole = [[[(4 - i) for i in range(self.z)] for _ in range(self.y)] for _ in range(self.x)] for i in range(self.x): for j in range(self.y): self.whole[i][j][0] = -100 # print(len(self.whole),len(self.whole[0]),len(self.whole[0][0])) positionR1 = self.LongLaToXY(runway1) positionR2 = self.LongLaToXY(runway2) self.whole[positionR1[0]][positionR1[1]][0] = 200 self.whole[positionR2[0]][positionR2[1]][0] = 200 positionW1 = self.LongLaToXY(way1) positionW2 = self.LongLaToXY(way2) print(positionW1, positionW2) self.End = [positionR1, positionR2] for i in range(positionR1[0] + 1): self.whole[i][positionR1[1]][1] = 100 + 2 * (i - positionR1[0]) for i in range(positionR2[0] + 1): self.whole[i][positionR2[1]][1] = 100 + 2 * (i - positionR2[0]) for i in range(self.x): self.whole[i][positionW1[1]][1] = 10 - 0.1 * i self.whole[i][positionW2[1]][1] = 10 - 0.1 * i # print(self.whole) # print('runway:',self.End) # print('dis:',self.LongLaToXY(way1),self.LongLaToXY(way2)) self.heightResolution = 1000 self.plane = Counter() self.planeLocation = Counter() self.values = Counter() self.tempValue = Counter() self.agent = None
def updateDistributions(self, gameState): pacPos = gameState.getAgentPosition(self.index) agentDistances = gameState.getAgentDistances() o0Distance = agentDistances[self.opponentIndexes[0]] o1Distance = agentDistances[self.opponentIndexes[1]] actions = [(0, 1), (0, -1), (1, 0), (-1, 0), (0, 0)] tempDistribution = Counter() if self.o0Distribution.totalCount() == 0: initPos = gameState.getInitialAgentPosition( self.opponentIndexes[0]) for pos in actions: newPos = (initPos[0] + pos[0], initPos[1] + pos[1]) if not newPos in self.walls: self.o0Distribution[newPos] = 1 self.o0Distribution.normalize() for position in self.o0Distribution: if self.o0Distribution[position] > 0: for pos in actions: newPos = (position[0] + pos[0], position[1] + pos[1]) if not newPos in self.walls: tempDistribution[newPos] = 1 for position in tempDistribution: distance = util.manhattanDistance(position, pacPos) tempDistribution[position] *= gameState.getDistanceProb( distance, o0Distance) tempDistribution.normalize() self.o0Distribution = tempDistribution tempDistribution = Counter() if self.o1Distribution.totalCount() == 0: initPos = gameState.getInitialAgentPosition( self.opponentIndexes[1]) for pos in actions: newPos = (initPos[0] + pos[0], initPos[1] + pos[1]) if not newPos in self.walls: self.o1Distribution[newPos] = 1 self.o1Distribution.normalize() for position in self.o1Distribution: if self.o1Distribution[position] > 0: for pos in actions: newPos = (position[0] + pos[0], position[1] + pos[1]) if not newPos in self.walls: tempDistribution[newPos] = 1 for position in tempDistribution: distance = util.manhattanDistance(position, pacPos) tempDistribution[position] *= gameState.getDistanceProb( distance, o1Distance) tempDistribution.normalize() self.o1Distribution = tempDistribution
def update_counters(self, counters): for field in self.fields: if field == "ns": continue if field not in counters: counters[field] = Counter() counters[field].encounter(getattr(self, field))
def elapseTime(self, gameState): """ Sample each particle's next state based on its current state and the gameState. """ new_particles = [] ghost_positions = Counter() for oldParticle in self.particles: newParticle = list(oldParticle) # A list of ghost positions # now loop through and update each entry in newParticle... "*** YOUR CODE HERE ***" prev_ghost_positions = list(oldParticle) for ghost_index in range(self.numGhosts): if (oldParticle, ghost_index) not in ghost_positions: poston_distribution = self.getPositionDistribution( gameState, prev_ghost_positions, ghost_index, self.ghostAgents[ghost_index]) ghost_positions[(oldParticle, ghost_index)] = poston_distribution newParticle[ghost_index] = poston_distribution.sample() else: newParticle[ghost_index] = ghost_positions[( oldParticle, ghost_index)].sample() """*** END YOUR CODE HERE ***""" new_particles.append(tuple(newParticle)) self.particles = new_particles
def get_most_successful(history): c = Counter() for member in cons.Choices: c[member] = 0 for game in history: c[game[cons.INDEX_OF_PLAY]] += cons.points[game[cons.INDEX_OF_RESULT]] return c.argMax()
def get_all_features(self, state, action): """ Returns a Counter from features to counts Usually, the count will just be 1.0 for indicator functions. """ if state.__class__.__name__ == 'Tetris': state = GameState(state.field, state.figure.type) successor_state = deepcopy(state) # simulate action on state for event in action: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: successor_state.rotate() if event.key == pygame.K_LEFT: successor_state.push_piece_x(-1) if event.key == pygame.K_RIGHT: successor_state.push_piece_x(1) if event.key == pygame.K_SPACE: successor_state.push_piece_y() rows, cols = len(state.field), len(state.field[0]) features = Counter() features["bias"] = 1.0 features["skyline_diff"] = self.get_height_differences( rows, cols, successor_state) features["max_skyline_diff"] = self.max_height_diff( rows, cols, successor_state) features["num_holes"] = self.get_num_holes(rows, cols, successor_state) features["max_height"] = self.get_max_height(rows, cols, successor_state) features["num_rows_cleared"] = successor_state.score - state.score features.divideAll(10.0) return features
def get_most_played_after_comp_move(history): last_comp_play = cons.neg[history[-1]][cons.INDEX_OF_PLAY] c = Counter() for i in range(len(history) - 1): if cons.neg[history[i]][cons.INDEX_OF_PLAY] == last_comp_play: c[history[i + 1][cons.INDEX_OF_PLAY]] += 1 return c.argMax() if c.argMax() else cons.NOT_AVAILABLE
def test(classifiedData, testingLabels, info): """ test() gets a classification for the test data and checks if it matches the labels. It then returns a performance metric on the test set. Keyword Arguments: classifiedData -- the labels outputted by the trained algorithm on the test set testingLabels -- the correct labels associated with test set info -- boolean to get information about common classification mistakes """ countCorrect = 0 problems = Counter() # check if classification matches label for i in range(len(testingLabels)): if testingLabels[i] == classifiedData[i]: countCorrect += 1 else: problems[(testingLabels[i], classifiedData[i])] += 1 print "Number of Correct Classifications" print "=================================" print countCorrect print "Percent of Correct Classifications" print "==================================" print float(countCorrect) / len(testingLabels) * 100.0 if info: getInfo(problems)
def create_model(d, f): model = Counter() for file in f: content = preprocess_text(d+file) c = ngrams(content, 2) model.update(c) return model
def __init__(self, rock_percentage, paper_percentage=None, scissors_percentage=None): """ usage options: * enter those 3 percentages * enter ROCK/PAPER/SCISSORS which means it is the prediction 100% * enter list of prediction to calculate percentages from """ if scissors_percentage is not None: self.rock_percentage = rock_percentage self.paper_percentage = paper_percentage self.scissors_percentage = scissors_percentage return if isinstance(rock_percentage, str): self.rock_percentage = int(rock_percentage == Rock) self.paper_percentage = int(rock_percentage == Paper) self.scissors_percentage = int(rock_percentage == Scissors) return # in the usage case of sending all the history: c = Counter() for prediction in rock_percentage: c[prediction[INDEX_OF_PLAY]] += 1 self.rock_percentage = c[Rock] / len(rock_percentage) self.paper_percentage = c[Paper] / len(rock_percentage) self.scissors_percentage = c[Scissors] / len(rock_percentage)
def deltaTime(self, observedState): newDistributions = dict() for agentIndex in self.getOpponents(observedState): distribution = Counter() for position in self.validPositions: newPositionDistribution = Counter() for neighboringPos in self.getLegalAdjPositions( observedState, position): newPositionDistribution[neighboringPos] = 1 newPositionDistribution.normalize() for newPos, prob in newPositionDistribution.items(): distribution[newPos] += self.beliefDistributions[ agentIndex][position] * prob distribution.normalize() newDistributions[agentIndex] = distribution self.beliefDistributions = newDistributions
def get_iv(index_of_attribute, examples): c = Counter() for ex in examples: c[ex[index_of_attribute]] += 1 percentages = np.array(list(c.values())) percentages = percentages / len(examples) return entropy(percentages, base=2)
def __init__( self, index, timeForComputing = .1 ): """ Lists several variables you can query: self.index = index for this agent self.red = true if you're on the red team, false otherwise self.agentsOnTeam = a list of agent objects that make up your team self.distancer = distance calculator (contest code provides this) self.observationHistory = list of GameState objects that correspond to the sequential order of states that have occurred so far this game self.timeForComputing = an amount of time to give each turn for computing maze distances (part of the provided distance calculator) """ # Agent index for querying state self.index = index # Whether or not you're on the red team self.red = None # Agent objects controlling you and your teammates self.agentsOnTeam = None # Maze distance calculator self.distancer = None # A history of observations self.observationHistory = [] # Time to spend each turn on computing maze distances self.timeForComputing = timeForComputing # Access to the graphics self.display = None self.counter = Counter()
def getMostFrequentLabel( self , labels ): freq = {} for l in set(labels): freq[l] = labels.count(l) return Counter(freq).argMax()
def costlessGraphSearch(problem, fringe): from util import Counter expanded = Counter() start = problem.getStartState() fringe.push((start, [], 0)) while True: # if fringe is empty, return that there is no solution if fringe.isEmpty(): # print "Empty fringe, done" return None # choose a fringe node to expand - dont expand already expanded nodes? curr = fringe.pop() state, actions, depth = curr #print "Choosing a node: ", state, actions, depth if expanded[state] != 0: #print "already seen expanded this node:", state continue expanded[state] += 1 #print "marked expanded:", state # if its the goal node, return the solution if problem.isGoalState(state): #print "found goal state, returning path", actions return actions # add the successors to the fringe for next in problem.getSuccessors(state): nextState, nextAction, nextCost = next fringe.push((nextState, actions + [nextAction], 0))
def update_ghost_scores(self, gameState): self.ghostScores = Counter() for opponent in self.getOpponents(gameState): opponentState = gameState.getAgentState(opponent) if not opponentState.isPacman and opponentState.scaredTimer == 0: position = gameState.getAgentPosition(opponent) if position: self.update_scores(-30.0, position, self.ghostScores, 3)
def chooseAction(self, gameState): """ Override this method to make a good agent. It should return a legal action within the time limit (otherwise a random legal action will be chosen for you). """ from util import Counter Counter(dict).raiseNotDefined()
def bigramSourceModel(segmentations): # compute all bigrams lm = {} vocab = {} vocab['end'] = 1 for s in segmentations: prev = 'start' for c in s: if not lm.has_key(prev): lm[prev] = Counter() lm[prev][c] = lm[prev][c] + 1 prev = c vocab[c] = 1 if not lm.has_key(prev): lm[prev] = Counter() lm[prev]['end'] = lm[prev]['end'] + 1 # smooth and normalize for prev in lm.iterkeys(): for c in vocab.iterkeys(): lm[prev][c] = lm[prev][c] + 0.5 # add 0.5 smoothing lm[prev].normalize() # convert to a FSA fsa = FSM.FSM(isProbabilistic=True) fsa.setInitialState('start') fsa.setFinalState('end') # Character states in bigram model for char in lm['start']: fsa.addEdge('start', char, char, lm['start'][char]) # Transitions between character states or to 'end' for char in lm.keys(): if not char == 'start': for second_char in lm[char]: if second_char == 'end': fsa.addEdge(char, second_char, None, prob=lm[char][second_char]) else: fsa.addEdge(char, second_char, second_char, prob=lm[char][second_char]) return fsa
def predict(self, datum): guesses = [] vectors = Counter() for l in self.labels: vectors[l] = self.weights[l] * datum.features guesses.append(vectors.argMax()) return guesses
def elapseTime(self): for enemy in self.enemies: count = Counter() for pos in self.Positions: count2 = Counter() allPossiblePos = [(pos[0]+i, pos[1]+j) for i in [-1,0,1] for j in [-1,0,1] if not (abs(i) == 1 and abs(j) == 1)] for pos2 in self.Positions: if pos2 in allPossiblePos: count2[pos2] = 1.0 count2.normalize() for newPos, prob in count2.items(): count[newPos] = count[newPos] + self.guess[self.enemy][newPos] * prob count.normalize() self.guess[enemy] = count
def squaredLossGradient(featureVector, y, weights): "*** YOUR CODE HERE (around 2 lines of code expected) ***" dotp = 0.0 for fKey in featureVector.iterkeys(): dotp += featureVector[fKey] * weights[fKey] grad = Counter() for fKey in featureVector.iterkeys(): grad[fKey] = (dotp - y) * featureVector[fKey] return grad
def logisticLossGradient(featureVector, y, weights): "*** YOUR CODE HERE (around 3 lines of code expected) ***" dotp = 0.0 for fKey in featureVector.iterkeys(): dotp += featureVector[fKey] * weights[fKey] grad = Counter() for fKey in featureVector.iterkeys(): grad[fKey] = -featureVector[fKey] * y / (1 + exp(dotp * y)) return grad
def ngrams(corpus, n): model = Counter() for sentence in corpus: sent = word_tokenize(sentence) for i in range(len(sent) - 1): key = tuple(sent[i:i + n]) key = tuple(w.lower() for w in key) model[key] += 1 return model