# END_YOUR_CODE def is_end(self, state): # BEGIN_YOUR_CODE return state == '' # END_YOUR_CODE def succ_and_cost(self, state): results = [] # BEGIN_YOUR_CODE for i in range(len(state)): action = state[:i + 1] next_state = state[i + 1:] cost = self.unigramCost(action) results.append((action, next_state, cost)) return results # END_YOUR_CODE unigramCost, bigramCost = wordsegUtil.makeLanguageModels('leo-will.txt') problem = SegmentationProblem('thisisnotmybeautifulhouse', unigramCost) import backtracking_search bts = backtracking_search.BacktrackingSearch(verbose=0) #print bts.solve(problem) import uniform_cost_search ucs = uniform_cost_search.UniformCostSearch(verbose=3) print ucs.solve(problem)
'e': 13, 'f': 45, 'g': 21, 'h': 14, 'i': -92, 'j': 33 } def startState(self): return 'root' def isGoal(self, state): if self._transitions[state] == (): return True def succAndCost(self, state): actions = ['left', 'right'] next_states = self._transitions[state] if next_states == (): return [] costs = [self._costs[next_states[0]], self._costs[next_states[1]]] return zip(actions, next_states, costs) if __name__ == '__main__': problem = PyramidProblem() algo = backtracking_search.BacktrackingSearch() algo.solve(problem) print(algo.cost) print(algo.actions)