예제 #1
0
def travel_maze(player, world, traversal_path):
    visited = set()
    cache = {}
    while len(visited) != len(world.rooms):
        current = player.current_room
        unvisited = []
        visited.add(current)
        travel_dead_ends(player, player.current_room, visited, traversal_path)
        possible_paths = utils.get_neighbors(player.current_room)
        end_loops = []
        others = []

        for direction, room in possible_paths.items():
            if room not in visited:
                unvisited.append([room, direction])

        if unvisited:
            for room, direction in unvisited:
                total = 0
                possible_paths = utils.get_neighbors(player.current_room)
                if possible_paths and len(possible_paths) > 1:
                    for direction2, room2 in possible_paths.items():
                        if room != room2:
                            check = utils.get_island_entrance(
                                room, room2, player.current_room)
                            if check:
                                total += 1
                                others.append([room, direction])
                    if total == 0:
                        end_loops.append([room, direction])

            if end_loops:
                best_direction = None
                best = None
                length_of_best = float('inf')
                for room, direction in end_loops:
                    now = bft(room, player.current_room)
                    if now < length_of_best:
                        length_of_best = now
                        best = room
                        best_direction = direction
                player.travel(best_direction)
                traversal_path.append(best_direction)
                visited.add(best)
            elif others:
                player.travel(others[0][1])
                traversal_path.append(others[0][1])
                visited.add(others[0][0])
        else:
            utils.bft(player, traversal_path, visited)

    return traversal_path
예제 #2
0
    def __create_forest(self):
        """
        Create randomly shaped, randomly placed forest in a map.

        Private method.
        """
        # find suitable start point
        start_x = random.randint(0, self.GRID_X-1)
        start_y = random.randint(0, self.GRID_Y-1)
        if self.map[start_x][start_y]["castle"]:
            self.__create_forest()
            return

        # generate points of forest
        forest_points = []
        for i in range(self.FOREST_SIZE):
            if i == 0:
                forest_points.append((start_x, start_y))
            else:
                # get neighbors of current points and pick random suitable one
                neighbors = utils.get_neighbors(forest_points,
                                                (self.GRID_X, self.GRID_Y))
                point = neighbors[random.randint(0, len(neighbors)-1)]
                while self.map[point[0]][point[1]]["castle"]:
                    point = neighbors[random.randint(0, len(neighbors)-1)]
                    neighbors.remove(point)
                while self.map[point[0]][point[1]]["village"]:
                    point = neighbors[random.randint(0, len(neighbors)-1)]
                    neighbors.remove(point)
                forest_points.append(point)

        # put points on map
        for point in forest_points:
            self.map[point[0]][point[1]]["forest"] = True
예제 #3
0
def simulated_anealing(graph, step=10000, a=0.5, q=1000, t=50, t_min=0.000001, count_max=100):
    count = 0
    n = len(graph)
    s = np.random.permutation(n).tolist()  # initil solution

    for i in range(step):
        s_next = find_better_solusion(get_neighbors(s), s, graph)

        e = get_cost(s, graph)
        e_next = get_cost(s_next, graph)

        if s == s_next:
            count += 1
        else:
            count = 0

        if e_next < e:
            s = s_next
        else:
            if random.random() <= probability(e, e_next, t):
                s = s_next

        if i % q == 0:  # Equilibrium state
            t = a * t

        if count > count_max:
            return s

    return s
예제 #4
0
def tabu_search(graph, tabu_max=500, step=10000, count_max=100):
    count = 0
    tabu_list = []
    n = len(graph)
    s = np.random.permutation(n).tolist()  # initil solution
    tabu_list.append(s)

    for i in range(step):

        feisible_list = []
        neighbors = get_neighbors(s)

        for x in neighbors:
            if not x in tabu_list:
                feisible_list.append(x)

        s_next = find_better_solusion(feisible_list, s, graph)

        if s == s_next:
            count += 1
        else:
            count = 0

        s = s_next

        if not s in tabu_list:
            tabu_list.append(s)

        if len(tabu_list) > tabu_max:
            tabu_list.pop(0)

        if count > count_max:
            return s

    return s
예제 #5
0
    def a_star_algorithm(self, f):
        open_nodes = [self.root]

        while True:
            curr_node = self.get_minor_node(open_nodes)

            if self.max_nodes < len(open_nodes):
                self.max_nodes = len(open_nodes)

            if np.array_equal(curr_node.get_board(), self.sorted_matrix):
                self.solution_path = utils.get_solution(curr_node)
                self.memory_usage = self.determine_memory_usage()
                return

            neighbors = utils.get_neighbors(curr_node.get_board(),
                                            self.board_size)
            for neighbor in neighbors:
                new_board = utils.swap_position(
                    copy.copy(curr_node.get_board()), neighbor)
                new_node = node.Node(
                    curr_node,
                    curr_node.get_board()[neighbor[0]][neighbor[1]],
                    curr_node.get_depth() + 1, new_board)
                new_node.set_h_cost(f(new_node.get_board(), self.board_size))
                if new_node.get_value() != new_node.get_upper().get_value():
                    open_nodes.append(new_node)
예제 #6
0
    def search_a_star(self, initial_x, initial_y, final_x, final_y):
        openn = []
        closed = []
        parents = {}

        start = (initial_x, initial_y)
        goal = (final_x, final_y)

        start_distances = {}
        goal_distances = {}

        openn.append(start)
        parents[start] = None
        start_distances[start] = 0

        while openn:
            current = min(openn, key=lambda o: start_distances.get(
                o, 0) + goal_distances.get(o, 0))

            if current == goal:
                path = []
                path.append(current)

                while parents[current]:
                    current = parents[current]
                    path.append(current)

                return path[::-1]

            openn.remove(current)
            closed.append(current)

            neigbhors = utils.get_neighbors(
                self.__repository.mapp, current[0], current[1])

            for node in neigbhors:
                if node in closed:
                    continue

                new_distance = start_distances[current] + 1

                if node in openn:
                    if start_distances[node] > new_distance:
                        start_distances[node] = new_distance
                        parents[node] = current

                else:
                    start_distances[node] = new_distance
                    goal_distances[node] = utils.manhattan(
                        node[0], node[1], goal[0], goal[1])
                    parents[node] = current

                    openn.append(node)

        return []
예제 #7
0
def index():
    if request.method == 'POST':
        v = int(request.form['number'])
        values = get_neighbors(v, zipcodes)
        print(values)
        results = User.query.filter(User.zipcode.in_(values)).all()
        print(results)
        return render_template('welcome.html',
                               string='Make Friends by Zipcode',
                               value=v,
                               values=values)
    return render_template('index.html', string='Make Friends by Zipcode')
예제 #8
0
    def boundary_cells(self):
        H, W, _ = self.dmap.shape
        on_boundary = np.full((H, W), 1.0)
        for row in range(H):
            for col in range(W):
                neighbors = get_neighbors(row, col, H, W, n8=True)
                cell_hue = self.dmap[row, col, 0]

                for n in neighbors:
                    neighbor_hue = self.dmap[n[0], n[1], 0]
                    if self.hue_cmp(cell_hue, neighbor_hue) == 1:
                        on_boundary[row, col] = 0.0
                        on_boundary[n[0], n[1]] = 0.0
        return on_boundary
예제 #9
0
파일: dfs.py 프로젝트: pHgon/8Puzzle-FIA
    def DFS_algorithm(self):
        not_visited_nodes = [self.root]

        while not_visited_nodes:
            if self.max_nodes < len(not_visited_nodes):
                self.max_nodes = len(not_visited_nodes)
            current_node = not_visited_nodes.pop()

            if np.array_equal(np.array(current_node.get_board()),
                              np.array(self.sorted_matrix)):
                #print("Tabuleiro final")
                #print(current_node.get_board())
                #print('Completou puzzle')
                self.solution_path = self.get_solution(current_node)
                self.memory_usage = self.determine_memory_usage(
                    not_visited_nodes)
                #print("Movimentos para completar: " + str(self.solution_path))
                return
            else:
                if current_node.get_depth() <= 31:
                    #Pega todos os visinhos possiveis
                    neighbors = utils.get_neighbors(current_node.get_board(),
                                                    self.board_size)

                    #laço para percorrer os possiveis vizinhos e adicionar na lista de nao visitados
                    for neighbor in neighbors:
                        if current_node.get_value() == -1:
                            new_board = utils.swap_position(
                                copy.copy(current_node.get_board()), neighbor)
                            new_node = node.Node(
                                current_node,
                                current_node.get_board()[neighbor[0],
                                                         neighbor[1]],
                                current_node.get_depth() + 1)
                            new_node.set_board(new_board)
                            not_visited_nodes.append(new_node)

                        elif current_node.get_value(
                        ) != current_node.get_board()[neighbor[0],
                                                      neighbor[1]]:
                            #print ("value 1 " + str(current_node.get_value()) + " Value 2 " + str(current_node.get_board()[neighbor[0],neighbor[1]]))
                            new_board = utils.swap_position(
                                copy.copy(current_node.get_board()), neighbor)
                            new_node = node.Node(
                                current_node,
                                current_node.get_board()[neighbor[0],
                                                         neighbor[1]],
                                current_node.get_depth() + 1)
                            new_node.set_board(new_board)
                            not_visited_nodes.append(new_node)
예제 #10
0
    def _run_cycle(self):
        new_active_points = set()
        active_neighbor_count = collections.defaultdict(int)

        for point in self.active_points:
            for neighbor in utils.get_neighbors(point, include_diagonals=True):
                active_neighbor_count[neighbor] += 1

        for point, active_neighbors in active_neighbor_count.items():
            if point in self.active_points and active_neighbors in [2, 3]:
                new_active_points.add(point)
            elif point not in self.active_points and active_neighbors == 3:
                new_active_points.add(point)

        self.active_points = new_active_points
예제 #11
0
def bft(starting_room, exce):
    visited = set()
    q = Queue()
    q.enqueue([starting_room])
    paths = []
    while q.size() > 0:
        path = q.dequeue()
        current = path[-1]
        paths.append(path)
        possible_paths = utils.get_neighbors(current)
        for direction, room in possible_paths.items():
            if room not in path and room != exce:
                new_path = path + [room]
                q.enqueue(new_path)

    paths.sort(key=len)
    return len(paths[-1])
예제 #12
0
 def maximise_own(self, board, p1_c, AI_c, turn):
     ## This is now considering when it is AI's first turn
     ## where we intend to place beside player's seed
     ## (Note: colour is player's colour, not AI's colour)
     if turn == 2:
         width, height = len(board), len(board[0])
         try:
             index = [[i, j] for i in range(width) for j in range(height)
                      if board[i][j] == p1_c][0]
         except:
             index = [[i, j] for i in range(width) for j in range(height)
                      if board[i][j] == 3 - p1_c][0]
         # If row index > column index i.e. bottom left triangle,
         # then place on top of opponent seed. Vice versa
         if index[0] > index[1]:
             return [index[0] - 1, index[1]]
         else:
             # else case includes when both indices are equal
             return [index[0] + 1, index[1]]
     ## This is where we consider connecting as many consecutive seeds
     ##  as possible
     else:
         score = {}
         width, height = len(board), len(board[0])
         for row in range(width):
             for col in range(height):
                 if board[row][col] == empty:
                     try:  # where we place our seed
                         score.update(
                             self.check_surroundings(board, AI_c, row, col))
                     except IndexError:
                         pass
         ##print("Scores: ",score)
         v = list(score.values())
         k = list(score.keys())
         m = max(v)
         indices = [i for i, j in enumerate(v) if j == m]
         best_score = []
         for each in indices:
             if k[each][0] >= 0 and k[each][1] >= 0:
                 best_score.append(k[each])
         ##print("Best Placements: ",best_score)
         for pair in best_score:
             res = utils.get_neighbors(pair, board)
             if sum([sum(lis) for lis in res]) > 0:
                 return pair
예제 #13
0
    def refine_boundaries(self, db_map):
        H, W, _ = self.dmap.shape
        for row in range(H):
            for col in range(W):
                cell_hue = self.dmap[row, col, 0]
                db = db_map[row, col]
                other_hue = self.dmap[db[0], db[1], 0]

                if self.hue_cmp(cell_hue, other_hue) != 0:
                    continue

                neighbors = get_neighbors(db[0], db[1], H, W, n8=True)
                for n in neighbors:
                    n_hue = self.dmap[n[0], n[1], 0]
                    if self.hue_cmp(cell_hue, n_hue) != 1:
                        continue
                    db_map[row, col] = n
                    break
예제 #14
0
    def __create_village(self):
        """
        Create randomly shaped, randomly placed village in a map.

        Private method.
        """
        # find suitable random start point for village
        start_x = random.randint(0, self.GRID_X-1)
        start_y = random.randint(0, self.GRID_Y-1)
        if self.map[start_x][start_y]["river"]:
            self.__create_village()
            return
        while self.map[start_x][start_y]["castle"]:
            self.__create_village()
            return

        # generate points of village
        village_points = []
        for i in range(self.VILLAGE_SIZE):
            if i == 0:
                village_points.append((start_x, start_y))
            else:
                # get neighbors of current points and pick random suitable
                neighbors = utils.get_neighbors(village_points,
                                                (self.GRID_X, self.GRID_Y))
                point = neighbors[random.randint(0, len(neighbors)-1)]
                while self.map[point[0]][point[1]]["river"]:
                    point = neighbors[random.randint(0, len(neighbors)-1)]
                    neighbors.remove(point)
                while self.map[point[0]][point[1]]["castle"]:
                    point = neighbors[random.randint(0, len(neighbors)-1)]
                    neighbors.remove(point)
                village_points.append(point)

        # put village points on map
        for point in village_points:
            self.map[point[0]][point[1]]["village"] = True
예제 #15
0
        def make_threats(old_board, root_node, parent_node, depth):
            ### Calling this func takes in a root node and initial board,
            ### loops through each square, place stone on square and check
            ### for any threats made. If a single threat is found, then
            ### update modified_board with the cost squares. Add that board
            ### to new_boards list. As long as winning sol is not found and
            ### there are some new boards, recursively call make_threats()
            ### until a sol is found or no more possible threats can be
            ### sequenced up.
            nonlocal found_sol
            nonlocal sol_seq
            if depth < 3:
                new_boards = []

                width, height = len(old_board), len(old_board[0])
                score_matrix1 = utils.my_score_matrix(old_board)
                score_matrix2 = utils.opponent_score_matrix(old_board)
                score_lis = []
                for i in range(width):
                    for j in range(height):
                        max_ = max(score_matrix1[i][j], score_matrix2[i][j])
                        min_ = min(score_matrix1[i][j], score_matrix2[i][j])
                        score_lis.append((max_, min_, (i, j)))
                        '''
                        num = score_matrix1[i][j] + score_matrix2[i][j]
                        score_lis.append( ( num, (i, j) ) )
                        '''
                score_lis.sort(reverse=True)

                #count = 10
                #score_lis = score_lis[:count]
                #res_lis = [ pair[1] for pair in score_lis ]

                count = 0
                res_lis = []
                for i in range(len(score_lis)):
                    if count == 6:  # change
                        break
                    _, _, location = score_lis[i]
                    res = utils.get_neighbors(location, old_board)
                    #if sum([ sum(lis) for lis in res ]) > 0:    # not so empty
                    res_lis.append(location[:])
                    count += 1
                #res_lis.sort()
                shuffle(res_lis)
                '''
                res_lis = []
                for i in range(len(score_lis)):
                    _, _, location = score_lis[i]
                    m, n = location
                    res = utils.get_neighbors(location, old_board)
                    if sum([ sum(lis) for lis in res ]) > 0:    # not so empty
                        res_lis.append(score_lis[i][2][:])
                '''

                for pair in res_lis:
                    i, j = pair
                    modified_board = deepcopy(old_board)
                    modified_board[i][j] = AI_c
                    bool_l = 0
                    bool_ll = 0
                    a = loop_board(size, 5, AI_c, modified_board, 0)
                    b = loop_board(size, 6, AI_c, modified_board, 0)
                    c = loop_board(size, 7, AI_c, modified_board, 0)
                    merged_threat_list = a + b + c
                    if len(merged_threat_list) == 1:
                        #print("Position: ",i,j)
                        #print("Threat List: ", merged_threat_list)
                        x = self.node([i, j])
                        x.set_parent(parent_node)
                        parent_node.set_child(x)
                        for each in merged_threat_list[0]:
                            modified_board[each[0]][each[1]] = p1_c
                        new_boards.append([modified_board, x])
                    elif len(merged_threat_list) == 2:
                        # a secondary confirmation as a double
                        # threat may have been found but in fact
                        # the cost square of one interferes with the
                        # solution of the other

                        confirmed = False
                        confirmed2 = False
                        for t in merged_threat_list[0]:
                            sol_board = modified_board.copy()
                            sol_board[t[0]][t[1]] = p1_c
                        for p in merged_threat_list[1]:
                            if not confirmed:
                                if sol_board[p[0]][p[1]] == empty:
                                    sol_board1 = sol_board.copy()
                                    sol_board1[p[0]][p[1]] = AI_c
                                    if loop_board(size, 5, AI_c, sol_board1,
                                                  1) or loop_board(
                                                      size, 6, AI_c,
                                                      sol_board1, 1):
                                        confirmed = True
                        if confirmed:
                            for t in merged_threat_list[
                                    1]:  # assuming double-threat only
                                sol_board = modified_board.copy()
                                sol_board[t[0]][t[1]] = p1_c
                            for p in merged_threat_list[0]:
                                if not confirmed2:
                                    if sol_board[p[0]][p[1]] == empty:
                                        sol_board1 = sol_board.copy()
                                        sol_board1[p[0]][p[1]] = AI_c
                                        if loop_board(
                                                size, 5, AI_c, sol_board1,
                                                1) or loop_board(
                                                    size, 6, AI_c, sol_board1,
                                                    1):
                                            confirmed2 = True

                        if confirmed and confirmed2:
                            #print(merged_threat_list)
                            #print('found a sol!')
                            x = self.node([i, j])
                            x.set_parent(parent_node)
                            parent_node.set_child(x)
                            root_node.set_sol(merged_threat_list)
                            sol_seq = []
                            store_seq(x)
                            found_sol = True
                            ##for each in merged_threat_list:
                            ##    y = self.node(each)
                            ##    y.set_parent(x)
                            ##    x.set_child(y)

                if len(new_boards) != 0 and found_sol == False:
                    for each in new_boards:
                        #print('test')
                        make_threats(each[0], root_node, each[1], depth + 1)
예제 #16
0
 def shuffle_algorithm(self, n_moves):
     self.reset_shuffle_algorithm()
     for n in range(n_moves):
         self.moves_list.append(
             self.swap_positions(utils.get_neighbors(
                 self.matrix, self.nmax)))
예제 #17
0
pathlens = []

for j in range(100):
    # todo: throw this all into a function.
    pos = np.array(start)

    maxit = 10000
    path = []
    actions = []
    path.append( pos )
    completed = False
    for k in range(maxit):
        action = utils.select_action(transition[pos[0],pos[1],:])

        pos = utils.get_neighbors(pos,level)[action]
        path.append( pos )
        actions.append( action )

        if all( pos==finish ):
            completed = True
            break
        #
        #_ = input()
    #

    path = np.array(path)

    rv = utils.reward(path, completed, maxit)
    transition = utils.update_transitions(transition, path, actions, rv)
예제 #18
0
x0 = 2
y0 = 12

map_width = 15
map_height = 15

my_map = utils.generate_map(map_width, map_height)

utils.add_obstacle(my_map, 6, 5, 3, 4)

utils.print_map(my_map)

start_location = utils.Location()
start_location.x = x0
start_location.y = y0

locations = [start_location]
weight = 1
while len(locations) > 0:
    nextLocations = []
    for location in locations:
        utils.set_weight(my_map, location, weight)
        nextLocations += utils.get_neighbors(my_map, location)
    utils.print_map(my_map)
    locations = utils.get_zero_weight_unique_locations(my_map, nextLocations)
    weight += 1
    input("Press Enter to continue ...\n")

print()
utils.print_map(my_map)
예제 #19
0
def get_features(dataset,k,n_exs,n_test,b,mode,N_SAMPLES,Neigh_SAMPLE=0, Neigh_PROB=0):
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    log = open("./logs/log-"+dataset+"-features.out","w",0)
    opt = "train"
    x_file = "./"+dataset+"/"+opt+"_x.txt"
    graph_file = "./"+dataset+"/graph_1.txt"
    node_type_file = "./"+dataset+"/node_type.txt"
    #build graph
    G = build_graph(graph_file, node_type_file)
    if dataset == "DBLP":
        to_remove = []
        for n in G.nodes():
            if nx.get_node_attributes(G,'type')[n] == 4:
                to_remove.append(n)
        G.remove_nodes_from(to_remove)
    H = build_bliss_graph(G)
    #load x
    X = np.loadtxt(x_file)
    #gather all patterns in data
    patterns = {}
    patterns[0] = {}
    ex_index = 0
    for x in X:
    	#print(ex_index)
        x = map(int,x)
        neighbors = get_neighbors(G,x, Neigh_SAMPLE, Neigh_PROB)
        if len(neighbors) > N_SAMPLES and N_SAMPLES > 0:
            n_hash = []
            xl = list(x)
            for n in neighbors:
                xl.append(n)
                xl = map(int, xl)
                n_hash.append(graph_hash(bliss_subgraph(H,xl)))
                xl.pop()
            prob = 1/float(len(set(n_hash)))
            counter=collections.Counter(n_hash)
            for h in range(0,len(n_hash)):
                n_hash[h] = prob*(1/float(counter[n_hash[h]]))
            neighbors = np.random.choice(neighbors,N_SAMPLES,replace=False,p=n_hash)
        for n in neighbors:
            
            xl = list(x)
            xl.append(n)
            xl = map(int, xl)
            #print xl
            gh = graph_hash(bliss_subgraph(H,xl))
            conn = 1
            if gh not in patterns[0]:
                patterns[0][gh] = []
                patterns[0][gh].append(ex_index)
            else:
                patterns[0][gh].append(ex_index)

            if gh not in patterns:
                g = nx.Graph(G.subgraph(xl))
                if nx.is_connected(g):
                    #print g.nodes(data=True)
                    #print g.edges()
                    patterns[gh] = {}
                else:
                    conn = 0
            if conn == 1:
                #print >>log, datetime.now() - startTime
                second_neighbors = get_neighbors(G,xl,Neigh_SAMPLE, Neigh_PROB)
                # second_neighbors = G[n]
                # second_neighbors = list((set(neighbors) | set(second_neighbors)) - set(xl))
                for sn in second_neighbors:
                    to_del = list(xl)
                    for d in to_del:
                        input_pattern = list(xl)
                        input_pattern.remove(d)
                        input_pattern.append(int(sn))
                        ghi = graph_hash(bliss_subgraph(H,input_pattern))
                        if ghi not in patterns[gh] and ghi not in patterns:
                            #if nx.is_connected(nx.Graph(G.subgraph(input_pattern))):
                            patterns[gh][ghi] = []
                            patterns[gh][ghi].append(ex_index)
                        elif ghi not in patterns[gh] and ghi in patterns:
                            patterns[gh][ghi] = []
                            patterns[gh][ghi].append(ex_index)
                        else:
                            patterns[gh][ghi].append(ex_index)
                        #print >>log, datetime.now() - startTime
            xl.pop()
        #print datetime.now() - startTime
        ex_index = ex_index + 1
        if ex_index == n_exs:
            break
        print >>log, datetime.now() - startTime
        print >>log, ex_index/float(n_exs)

    opt = "test"
    x_file = "./"+dataset+"/"+opt+"_x.txt"
    graph_file = "./"+dataset+"/graph_2.txt"
    node_type_file = "./"+dataset+"/node_type.txt"
    #build graph
    G = build_graph(graph_file, node_type_file)
    if dataset == "DBLP":
        to_remove = []
        for n in G.nodes():
            if nx.get_node_attributes(G,'type')[n] == 4:
                to_remove.append(n)
        G.remove_nodes_from(to_remove)
    H = build_bliss_graph(G)
    #load x
    X = np.loadtxt(x_file)
    #test_IDS = range(0,len(X[:,]))
    test_IDS = False
    print >>log, "CONSTRUCTING TEST FEATURES"
    ex_index = 0
    for x in X:
        #print(ex_index + n_exs)
        x = x.tolist()
        x = map(int,x)
        neighbors = get_neighbors(G,x,Neigh_SAMPLE, Neigh_PROB)
        if len(neighbors) > N_SAMPLES and N_SAMPLES > 0:
            n_hash = []
            xl = list(x)
            for n in neighbors:
                xl.append(n)
                xl = map(int, xl)
                n_hash.append(graph_hash(bliss_subgraph(H,xl)))
                xl.pop()
            prob = 1/float(len(set(n_hash)))
            counter=collections.Counter(n_hash)
            for h in range(0,len(n_hash)):
                n_hash[h] = prob*(1/float(counter[n_hash[h]]))
            neighbors = np.random.choice(neighbors,N_SAMPLES,replace=False,p=n_hash,)
        for n in neighbors:
            xl = list(x)
            xl.append(n)
            xl = map(int, xl)
            gh = graph_hash(bliss_subgraph(H,xl))
            if gh in patterns[0]:
                
                patterns[0][gh].append(ex_index + n_exs)

            if gh in patterns:
                #print >>log, datetime.now() - startTime
                second_neighbors = get_neighbors(G,xl,Neigh_SAMPLE, Neigh_PROB)
                # second_neighbors = G[n]
                # second_neighbors = list((set(neighbors) | set(second_neighbors)) - set(xl))
                for sn in second_neighbors:
                    to_del = list(xl)
                    for d in to_del:
                        input_pattern = list(xl)
                        #print input_pattern
                        input_pattern.remove(d)
                        input_pattern.append(int(sn))
                        ghi = graph_hash(bliss_subgraph(H,input_pattern))
                        if ghi in patterns[gh]:
                            patterns[gh][ghi].append(ex_index + n_exs)
                        #print >>log, datetime.now() - startTime
            xl.pop()
        print >>log, datetime.now() - startTime
        ex_index = ex_index + 1
        if ex_index == n_test:
            break

    print >>log, datetime.now() - startTime

    num_dim = 0
    print >>log, len(patterns)
    PL = []
    for p in patterns:
        num_dim = num_dim + len(patterns[p])
        PL.append(len(patterns[p]))
    print >>log, PL
    print >>log, num_dim
    new_X = np.zeros((n_exs + n_test, num_dim))
    norm = np.zeros((n_exs + n_test, len(PL)))
    j = 0
    k = 0
    for p in patterns:
        for pp in patterns[p]:
            for i in patterns[p][pp]:
                new_X[i,j] = new_X[i,j] + 1
                norm[i,k] = norm[i,k] + 1
            j = j + 1
        k = k + 1
    s = 0
    f = 0
    pi = 0
    for p in PL:
        f = s + p
        if mode == 'avg':
            new_X[:,s:f] = new_X[:,s:f]/(np.transpose(np.tile(norm[:,pi],(p,1))))
        s = f
        pi = pi + 1
    new_X = np.nan_to_num(new_X)
    np.savetxt(dataset+'train_x.txt',new_X[0:n_exs,:], '%5.0f')
    np.savetxt(dataset+'test_x.txt',new_X[n_exs:(n_exs+n_test),:], '%5.0f')
    np.savetxt(dataset+'PL.txt',np.asarray(PL), '%5.0f')
    np.savetxt(dataset+'test_IDS.txt',np.atleast_1d(test_IDS), '%5.0f')

    return new_X[0:n_exs,:].astype(float), new_X[n_exs:(n_exs+n_test),:].astype(float),PL,test_IDS