def predict_smart(self, x_test): y = [] number_of_test_examples = len(x_test) for i in xrange(number_of_test_examples): search_tree = SearchTree(self.number_of_labels) root = search_tree.add_vertex("root_", 0, 1.0, 1.0, [], x_test[i]) h = [] heapq.heappush(h, (root.get_inversed_cond_prob(), root)) best_leaf = None best_score = 0.0 while len(h) != 0: vertex = heapq.heappop(h)[1] if vertex.get_conditional_prob() < best_score: break else: greedy_leaf = self.look_ahead(search_tree, vertex, h, best_score) if greedy_leaf is None: continue else: best_leaf = greedy_leaf best_score = greedy_leaf.get_conditional_prob() y.append(best_leaf.get_labels()) return np.array(y)
def predict_full(self, x_test): y = [] number_of_test_examples = len(x_test) for i in xrange(number_of_test_examples): search_tree = SearchTree(self.number_of_labels) root = search_tree.add_vertex("root_", 0, 1.0, 1.0, [], x_test[i]) self.generate_search_tree(search_tree, root) best_leaf = search_tree.find_best_leaf() y.append(best_leaf.get_labels()) # print "Done: " + str(i) + "/" + str(number_of_training_examples) return np.array(y)
def predict_ucs(self, x_test, epsilon): y = [] number_of_test_examples = len(x_test) for i in xrange(number_of_test_examples): search_tree = SearchTree(self.number_of_labels) root = search_tree.add_vertex("root_", 0, 1.0, 1.0, [], x_test[i]) q = [] greedy_q = [] heapq.heappush(q, (root.get_inversed_cond_prob(), root)) best_leaf = None while len(q) != 0: vertex = heapq.heappop(q)[1] if len(vertex.get_labels() ) == self.number_of_labels: # If leaf best_leaf = vertex break else: children = self.generate_children(search_tree, vertex) no_children_inserted = True for child in children: # print child.get_inversed_cond_prob() if child.get_probability() > epsilon: heapq.heappush( q, (child.get_inversed_cond_prob(), child)) no_children_inserted = False if no_children_inserted: heapq.heappush( greedy_q, (vertex.get_inversed_cond_prob(), vertex)) if best_leaf is None: best_score = 0.0 while len(greedy_q) != 0: vertex = heapq.heappop(greedy_q)[1] greedy_leaf = self.look_ahead(search_tree, vertex, [], best_score) if greedy_leaf is None: continue else: best_leaf = greedy_leaf best_score = greedy_leaf.get_conditional_prob() # print best_leaf, best_score # print("------") y.append(best_leaf.get_labels()) # print "Done: " + str(i) + "/" + str(number_of_training_examples) return np.array(y)
def predict_ucs(self, x_test, epsilon): y = [] number_of_test_examples = len(x_test) for i in xrange(number_of_test_examples): search_tree = SearchTree(self.number_of_labels) root = search_tree.add_vertex("root_", 0, 1.0, 1.0, [], x_test[i]) q = [] greedy_q = [] heapq.heappush(q, (root.get_inversed_cond_prob(), root)) best_leaf = None while len(q) != 0: vertex = heapq.heappop(q)[1] if len(vertex.get_labels()) == self.number_of_labels: # If leaf best_leaf = vertex break else: children = self.generate_children(search_tree, vertex) no_children_inserted = True for child in children: # print child.get_inversed_cond_prob() if child.get_probability() > epsilon: heapq.heappush(q, (child.get_inversed_cond_prob(), child)) no_children_inserted = False if no_children_inserted: heapq.heappush(greedy_q, (vertex.get_inversed_cond_prob(), vertex)) if best_leaf is None: best_score = 0.0 while len(greedy_q) != 0: vertex = heapq.heappop(greedy_q)[1] greedy_leaf = self.look_ahead(search_tree, vertex, [], best_score) if greedy_leaf is None: continue else: best_leaf = greedy_leaf best_score = greedy_leaf.get_conditional_prob() # print best_leaf, best_score # print("------") y.append(best_leaf.get_labels()) # print "Done: " + str(i) + "/" + str(number_of_training_examples) return np.array(y)
def offset(mapA, mapB): #print('query start') init = time.time() * 1000 mean_dist = 0 st = SearchTree() #print('sizes:',len(mapA), len(mapB)) random.shuffle(mapA) #Reduce tree height for i in mapA: #print(i[0], i[1]) st.push(i[0], i[1]) #print('building done. Time was', (time.time() * 1000 - init)) init = time.time() * 1000 for j in mapB: #print(j, st.query(j[0], j[1], False)) res = st.query(j[0], j[1], False) mean_dist += res[0] #print('query end. Time was', (time.time() * 1000 - init), 'Visited count:', res[1]) return (mean_dist / len(mapB))
def __init__(self, world, name=None, init_vertex=1, bonus_vandal_records=None): super(SmartAStar, self).__init__(world=world, name=name, init_vertex=init_vertex, bonus_vandal_records=bonus_vandal_records) self.search_tree = SearchTree(init_state=self.init_state, strategy='A*', vandal_records=bonus_vandal_records)
def __init__(self, world, expand_limit, name=None, init_vertex=1, bonus_vandal_records=None): super(SmartRTA, self).__init__(world=world, name=name, init_vertex=init_vertex, bonus_vandal_records=bonus_vandal_records) self.search_tree = SearchTree(init_state=self.init_state, strategy='RTA', expand_limit=expand_limit, vandal_records=bonus_vandal_records)
output_size=board_size * board_size, hidden_layers=config.HIDDEN_LAYERS, activation=config.ACTIVATION).float() anet = ANET_MODEL(anet_nn, max_cases_in_buffer=config.REPLAY_BUFFER_MAX_SIZE) optimizer = config.OPTIMIZERS[config.OPTIMIZER](anet_nn.parameters(), lr=lr) for episode in range(num_episodes): while state_manager.game_is_on(): random_number = random.uniform(0, 1) if random_number < 0.3: action = state_manager.get_random_action() else: search_tree = SearchTree(state_manager.get_current_state(), exploration_bonus) action = search_tree.simulate_games_and_find_action( num_simulations, state_manager, anet) state_manager.take_action(action) # --------- LOGGING THE TIME MODELS USE TO RAIN -------------- time_finished = time.time() time_passed = str(round((time_finished - time_start) / 60, 2)) log_string = l = str(episode) + " of " + str( num_episodes) + " finished. TIME PASSED (minutes): " + time_passed utils.logging(folder_results, log_string) # ------------------------------------------------------------ anet.train_model(optimizer, minibatch_size=config.REPLAY_BUFFER_MINIBATCH_SIZE)