def classify_one(self, test_example: Examples) -> int: committee = [] for train_example in self._train_examples: insort(committee, CommitteeWrapper(train_example[0], euclidean_distance(test_example, train_example))) votes_num, vote_for, vote_against, under_bound = 0, 0, 0, (0, 0) for vote in committee: if vote.distance < self._bound: under_bound = (vote_for, vote_against) if votes_num >= self._k: break if vote == 1: vote_for += 1 else: vote_against += 1 votes_num += 1 if vote_for >= vote_against: return 1 # KNN wants to classify as Negative to disease (healthy), let's get a second opinion if self._id3_classifier is None: self._id3_classifier = ID3ContinuousFeatures(self._train_examples) if self._id3_classifier.classify_one(test_example): return 1 # Both KNN and ID3 want to classify as Negative to disease (healthy), let's get a third final opinion return under_bound[0] > under_bound[1]
def update_others(self, nodelist=[], b=4): for node in nodelist: if node != self: node.routing_table = compare_tables(node.routing_table, self.routing_table, node) node.neighbours = insort(node.neighbours, self) if (node.ide < self.ide): node.leaf_set_big = insort(node.leaf_set_big, self) else: node.leaf_set_small = insort(node.leaf_set_small, self) if len(node.neighbours) > pow(2, b + 1): node.neighbours = node.neighbours[:-1] if len(node.leaf_set_small) > pow(2, b - 1): node.leaf_set_small = node.leaf_set_small[1:] if len(node.leaf_set_big) > pow(2, b - 1): node.leaf_set_big = node.leaf_set_big[:-1]
def join(self, node=None, nodelist=[], b=4): closest_node = self.routing(node=node, nodelist=nodelist, init=True) node.neighbours = [i for i in self.neighbours] node.neighbours = insort(node.neighbours, self) node.leaf_set_small = [i for i in closest_node.leaf_set_small] node.leaf_set_big = [i for i in closest_node.leaf_set_big] if closest_node.ide > node.ide: node.leaf_set_big = insort(node.leaf_set_big, closest_node) else: node.leaf_set_small = insort(node.leaf_set_small, closest_node) if len(node.neighbours) > pow(2, b + 1): node.neighbours = node.neighbours[:-1] if len(node.leaf_set_small) > pow(2, b - 1): node.leaf_set_small = node.leaf_set_small[1:] if len(node.leaf_set_big) > pow(2, b - 1): node.leaf_set_big = node.leaf_set_big[:-1] node.table_initialization(nodelist, b)
def classify_one(self, test_example: Examples) -> int: committee = [] for train_example in self._train_examples: insort( committee, CommitteeWrapper( train_example[0], euclidean_distance(test_example, train_example))) vote_for, vote_against = 0, 0 for vote in committee: if vote_for + vote_against >= self._k: break if vote == 1: vote_for += 1 else: vote_against += 1 return 1 if vote_for >= vote_against else 0
def classify_one(self, test_example: Examples) -> int: committee = [] for tree in self._forest: insort( committee, CommitteeWrapper( tree.classifier, euclidean_distance(tree.centroid, test_example[1:]))) votes_num, vote_for, vote_against, distance_range, vote_weight = 0, 0, 0, 1, self._vote_weight for classifier in committee: if votes_num >= self._num_chosen_trees: break if classifier.distance > distance_range: vote_weight /= self._vote_weight distance_range = classifier.distance + self._area_length if classifier.classification_or_classifier.classify_one( test_example) == 1: vote_for += vote_weight else: vote_against += vote_weight votes_num += 1 return 1 if vote_for >= vote_against else 0
def plan(map, start, goal): visited_nodes = [] visited_poses = [] priority_queue = [] priority_poses = [] priority_lens = [] x_bound = map.shape[0] y_bound = map.shape[1] occupancy_threshold = 0 if goal[0] < 0 or goal[0] > x_bound: raise Exception("Goal is out of x bound") if goal[1] < 0 or goal[1] > y_bound: raise Exception("Goal is out of y bound") if map[goal[0]][goal[1]] > occupancy_threshold: raise Exception("Goal is occupied") if map[goal[0]][goal[1]] < 0: raise Exception("Goal is in unexplored region") """configuration of node search""" list_of_directions = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 0]] """initialization""" ### building starting node ### node = [D, pose, path] init_dist = manhattan_dist(start, goal) start_node = [init_dist, start, [start]] new_parent = start_node """node iterations""" while (new_parent[1] != goal): parent_pose = new_parent[1] parent_path = new_parent[2] ### creating new nodes for x in list_of_directions: """node creation""" history = list(parent_path) + [[ parent_pose[0] + x[0], parent_pose[1] + x[1] ]] new_node = [ manhattan_dist([parent_pose[0] + x[0], parent_pose[1] + x[1]], goal) + len(history), #D [parent_pose[0] + x[0], parent_pose[1] + x[1]], # new pose history #path ] """filtering""" ### within map bounds ### if new_node[1][0] < 0 or new_node[1][0] > x_bound: continue if new_node[1][1] < 0 or new_node[1][1] > y_bound: continue ### not occupied ### if map[new_node[1][0]][new_node[1][1]] > occupancy_threshold: continue ### not considered before ### if new_node[1] in priority_poses and len( new_node[2]) in priority_lens: continue """adding to queue""" priority_queue = insort(priority_queue, new_node) priority_poses.append(new_node[1]) priority_lens.append(len(new_node[2])) """selection of new parent node""" new_parent = priority_queue[0] priority_queue.pop(0) priority_poses.pop(priority_poses.index(new_parent[1])) priority_lens.pop(len(new_parent[2])) """return path""" return (new_parent[2])
def plan_a_star_3D(map3D, start, goal): ### Modified A* that creates nodes according to next map frame ### input: map3D - map frames in time, start pose, goal pose try: priority_queue = [] priority_poses = [] priority_lens = [] x_bound = map3D.shape[1] y_bound = map3D.shape[2] map_range = map3D.shape[0] occupancy_threshold = 0 extension = 0 if goal[0]<0 or goal[0]>x_bound: raise Exception("Goal is out of x bound") if goal[1]<0 or goal[1]>y_bound: raise Exception("Goal is out of y bound") if map3D[0,goal[0],goal[1]]>occupancy_threshold: raise Exception("Goal is occupied") if map3D[0,goal[0],goal[1]]<0: raise Exception("Goal is in unexplored region") """configuration of node search""" list_of_directions = [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,0]] """initialization""" ### building starting node ### node = [D, pose, path] init_dist = manhattan_dist(start,goal) start_node = [init_dist, start, [start]] new_parent = start_node """node iterations""" while(new_parent[1]!=goal): parent_pose = new_parent[1] parent_path = new_parent[2] current_map_num = len(parent_path) ## extension of the 3D map # print(map3D.shape) if current_map_num == (map_range-2): current_map = np.copy(map3D[-1,:,:]) map3D = np.append(map3D,[current_map],axis=0) map_range = map3D.shape[0] extension = extension + 1 # print("extended") # print(map3D.shape) ### creating new nodes for x in list_of_directions: """node creation""" history = list(parent_path) + [[parent_pose[0]+x[0],parent_pose[1]+x[1]]] next_map_num = current_map_num+1 new_node = [manhattan_dist([parent_pose[0]+x[0],parent_pose[1]+x[1]], goal) + len(history), #D [parent_pose[0]+x[0],parent_pose[1]+x[1]], # new pose history #path ] """filtering""" ### within map bounds ### if new_node[1][0]<0 or new_node[1][0]>x_bound: continue if new_node[1][1]<0 or new_node[1][1]>y_bound: continue ### not occupied ### ### if range is out extend 3D map with the last map if map3D[next_map_num,new_node[1][0],new_node[1][1]]>occupancy_threshold: continue ### not considered before ### if new_node[1] in priority_poses and len(new_node[2]) in priority_lens: continue """adding to queue""" priority_queue = insort(priority_queue, new_node) priority_poses.append(new_node[1]) priority_lens.append(len(new_node[2])) """selection of new parent node""" new_parent = priority_queue[0] priority_queue.pop(0) priority_poses.pop(priority_poses.index(new_parent[1])) priority_lens.pop(len(new_parent[2])) return (new_parent[2]),extension except: print("Cannot plan A* 3D")