コード例 #1
0
ファイル: gspan.py プロジェクト: lireland04/urbcomp-scripts
 def _generate_1edge_frequent_subgraphs(self):
     vlb_counter = collections.Counter()
     vevlb_counter = collections.Counter()
     vlb_counted = set()
     vevlb_counted = set()
     for g in self.graphs.values():
         for v in g.vertices.values():
             if (g.gid, v.vlb) not in vlb_counted:
                 vlb_counter[v.vlb] += 1
             vlb_counted.add((g.gid, v.vlb))
             for to, e in v.edges.items():
                 vlb1, vlb2 = v.vlb, g.vertices[to].vlb
                 if self._is_undirected and vlb1 > vlb2:
                     vlb1, vlb2 = vlb2, vlb1
                 if (g.gid, (vlb1, e.elb, vlb2)) not in vevlb_counter:
                     vevlb_counter[(vlb1, e.elb, vlb2)] += 1
                 vevlb_counted.add((g.gid, (vlb1, e.elb, vlb2)))
     # add frequent vertices.
     for vlb, cnt in vlb_counter.items():
         if cnt >= self._min_support:
             g = Graph(gid=next(self._counter),
                       is_undirected=self._is_undirected)
             g.add_vertex(0, vlb)
             self._frequent_size1_subgraphs.append(g)
             if self._min_num_vertices <= 1:
                 self._report_size1(g, support=cnt)
         else:
             continue
     if self._min_num_vertices > 1:
         self._counter = itertools.count()
コード例 #2
0
ファイル: sndlib2graphs.py プロジェクト: daajoe/sndlib2graphs
def create_graph(doc):
    G = Graph()

    for node in doc['network']['networkStructure']['nodes']['node']:
        G.add_node(node['@id'], lat=node['coordinates']['x'], lon=node['coordinates']['y'])

    for edge in doc['network']['networkStructure']['links']['link']:
        modules = []
        try: 
            for _,v in edge['additionalModules'].iteritems():
                if isinstance(v,list):
                    for e in v:
                        m = {'capacity': e['capacity'], 'cost': e['cost']}
                        modules.append(m)
                else:
                    m = {'capacity': v['capacity'], 'cost': v['cost']}
                    modules.append(m)
        except KeyError:
            modules = []
        try:
            preInstalled=edge['preInstalledModule']
        except KeyError:
            preInstalled=''

        G.add_edge(edge['source'], edge['target'], preInstalledModule=preInstalled, additionalModules=modules)
    return G
コード例 #3
0
def check_if_eulerian(G: Graph):
    old_representation_type = G.representation_type
    G.change_graph_representation_to(GraphRepresentationType.ADJACENCY_LIST)

    old_graph_representation = copy.deepcopy(G.graph_representation)

    starting_vertex = 0

    path = []

    if(len(G.graph_representation) == 1):               # graf składa się z jednego wierzchołka
        return True, path
    else:
        for key, l in G.graph_representation.items():    # jezeli jakis wierzcholek jest odosobniony
            if(len(l) == 0):
                return False, path

    
    last_vertex = choose_edge_and_delete(G, starting_vertex, path)

    is_eulerian_graph = last_vertex == starting_vertex and check_if_graph_has_no_edges(G)

    G.graph_representation = old_graph_representation
    G.change_graph_representation_to(old_representation_type)

    return is_eulerian_graph, path
コード例 #4
0
 def setUp(self):
     self._possible_cell_values = {1, 2, 3, 4}
     self._size = 4
     self.graph = Graph(size=self._size,
                        possible_values=self._possible_cell_values)
     self.expected_neighbours_for_0_0 = {(1, 0), (1, 1), (0, 1), (0, 2),
                                         (0, 3), (2, 0), (3, 0)}
コード例 #5
0
def test_create3():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    assert [node.name for node in G.dfs()] == ['test1', 'test2', 'test3']
    assert not G.verify_edges()
コード例 #6
0
def create_graph(filepath):
    g=Graph()
    with open(filepath) as csvfile:
        reader  = csv.DictReader(csvfile)
        #print reader.fieldnames
        for row in reader:
            g.add_edge(row['fromNode'],row['toNode'])
    return g
コード例 #7
0
 def get_unlinked_graph(self):
     graph = Graph(size=self._size)
     graph.vertexes = {}
     for vertex_id in itertools.product(range(self._size),
                                        range(self._size)):
         graph.vertexes[vertex_id] = Vertex(self._possible_cell_values,
                                            vertex_id=vertex_id,
                                            graph=graph)
     return graph
コード例 #8
0
    def _get_samples(self):
        samples_map = {}
        derived_from_graph = Graph()

        project = self.manifest.get_project()
        for biomaterial in self.manifest.get_biomaterials():
            archive_entity = ArchiveEntity()
            archive_entity.manifest_id = self.manifest.manifest_id
            archive_type = "sample"
            archive_entity.archive_entity_type = archive_type
            archive_entity.id = self.generate_archive_entity_id(
                archive_type, biomaterial.data)

            archive_entity.data = {
                'biomaterial': biomaterial.data,
                'project': project
            }

            archive_entity.metadata_uuids = [
                biomaterial.data['uuid']['uuid'], project['uuid']['uuid']
            ]
            archive_entity.accessioned_metadata_uuids = [
                biomaterial.data['uuid']['uuid']
            ]

            if biomaterial.derived_by_process:
                # TODO protocols will be needed for samples conversion
                # archive_entity.data.update(biomaterial.derived_with_protocols)

                sample_links = []
                for derived_from in biomaterial.derived_from_biomaterials:
                    derived_from_alias = self.generate_archive_entity_id(
                        'sample', derived_from)
                    derived_from_graph.add_edge(derived_from_alias,
                                                archive_entity.id)
                    sample_links.append({
                        'alias': derived_from_alias,
                        'relationshipNature': 'derived from'
                    })

                links = {'sampleRelationships': sample_links}
                archive_entity.links = links

            samples_map[archive_entity.id] = archive_entity

        sorted_samples = derived_from_graph.topological_sort()
        priority_samples = [
            samples_map.get(sample) for sample in sorted_samples
            if samples_map.get(sample)
        ]
        orphan_samples = [
            samples_map.get(sample) for sample in samples_map.keys()
            if sample not in priority_samples
        ]

        return priority_samples + orphan_samples
コード例 #9
0
 def __init__(self, puzzle, init_graph=True):
     self._puzzle_array = puzzle
     self._size = self._puzzle_array.shape[0]
     self._graph = None
     if init_graph:
         self._graph = Graph(
             self._size,
             possible_values={i
                              for i in range(1, self._size + 1)})
         self._init_values()
コード例 #10
0
def test_reverse_dfs1():
    G = Graph()
    G.add_node(Node("test1"))
    G.add_node(Node('test2'))
    G.add_edge(Edge('test1', 'test2'))
    G.add_edge(Edge('test1', Node('test3')))
    expect = ['test1', 'test2', 'test3']
    assert [node.name for node in G.dfs()] == expect
    expect.reverse()
    assert [node.name for node in G.dfs(reverse=True)] == expect
コード例 #11
0
ファイル: sokoban.py プロジェクト: xwu20/wmg_agent
    def __init__(self, seed):
        self.rand = random.Random(seed)
        self.num_boxes = 4
        self.boxes_on_target = 0
        self.max_steps_per_episode = SOKOBAN_MAX_STEPS * SOKOBAN_BOXES_REQUIRED / self.num_boxes

        # Penalties and Rewards
        self.penalty_for_step = SOKOBAN_REWARD_PER_STEP
        self.penalty_box_off_target = -1
        self.reward_box_on_target = 1
        self.reward_finished = SOKOBAN_REWARD_SUCCESS * SOKOBAN_BOXES_REQUIRED / self.num_boxes
        self.reward_last = 0

        # Other Settings
        self.action_space = 5
        if SOKOBAN_OBSERVATION_FORMAT == 'grid':
            self.observation_space = 400
            self.observation = np.zeros((10, 10, 4), dtype=np.uint8)
        elif SOKOBAN_OBSERVATION_FORMAT == 'factored':
            self.observation = Graph()
            self.num_factor_positions = 15
            self.factor_position_offset = (self.num_factor_positions - 1) // 2
            self.cell_factor_size = 0
            self.cell_factor_size += 2 * (self.num_factor_positions + 1
                                          )  # Cell position.
            self.cell_factor_size += 3  # Cell identity.
            self.cell_factor_size += 4
            self.core_obs_size = self.action_space + 1
            self.core_obs_size += 1 + 4  # On target, four walls.
            self.observation.entity_type_sizes.append(self.core_obs_size)
            self.observation.entity_type_sizes.append(self.cell_factor_size)
            self.observation_space = self.observation
        self.use_display = False
        self.num_cols_or_rows = PUZZLE_SIZE
        self.pix_per_cell = PUZZLE_SCALE
        self.wid = self.num_cols_or_rows
        self.x_orig = -self.wid * self.pix_per_cell / 2
        self.y_orig = self.wid * self.pix_per_cell / 2
        self.agent_col = None
        self.agent_row = None
        self.reset_online_test_sums()
        self.score = 0.
        self.reward = 0.
        self.action = None

        # Cell channel encodings.
        self.encodings = np.array(
            ((1, 0, 0, 0), (0, 0, 0, 0), (0, 1, 0, 0), (0, 1, 1, 0),
             (0, 0, 1, 0), (0, 0, 0, 1), (0, 1, 0, 1)),
            dtype=np.uint8)

        self.total_steps = 0
        self.total_reward = 0.
        self.total_episodes = 0
        self.total_episodes_won = 0.
コード例 #12
0
    def test_indirect_cyclic_dependency_error(self):
        g = Graph()
        g.add_edge(5, 2)
        g.add_edge(5, 0)
        g.add_edge(4, 0)
        g.add_edge(4, 1)
        g.add_edge(2, 3)
        g.add_edge(3, 1)

        with self.assertRaises(CyclicDependencyError):
            g.add_edge(1, 2)
コード例 #13
0
    def test_indirect_cyclic_dependency_error_5_levels(self):
        g = Graph()
        g.add_edge(0, 1)
        g.add_edge(1, 2)
        g.add_edge(2, 3)
        g.add_edge(3, 4)
        g.add_edge(4, 5)

        with self.assertRaises(CyclicDependencyError) as context:
            g.add_edge(5, 0)

        self.assertEqual([5, 0, 1, 2, 3, 4], context.exception.cycle)
コード例 #14
0
def create_graph(filepath):
    g = Graph()
    with open(filepath) as csvfile:
        for line in csvfile:
            line = line.split()
            if line == []:
                continue
            if line[0] == '%':
                continue
            else:
                g.add_edge(line[0], line[1])
    return g
コード例 #15
0
def main():
    '''
    main entrance
    '''
    start = time.time()
    parser = argparse.ArgumentParser(
        description='Find Solutions for CARP Problem\nCoded by Edward FANG')
    parser.add_argument('instance',
                        type=argparse.FileType('r'),
                        help='filename for CARP instance')
    parser.add_argument('-t',
                        metavar='termination',
                        type=int,
                        help='termination time limit',
                        required=True)
    parser.add_argument('-s',
                        metavar='random seed',
                        help='random seed for stochastic algorithm')
    args = parser.parse_args()
    time_limit = args.t
    seed = args.s
    instance_file = args.instance
    # print(time_limit, seed)
    spec, data = read_instance_file(instance_file)
    network = Graph()
    network.load_from_data(data.tolist())
    solvers = list()
    solution_receiver = Queue()
    best_solution = bestSolution()
    thread1 = solution_updater(solution_receiver, best_solution)
    thread1.start()
    # multi processors processing
    for idx in range(N_PROCESSORS):
        if seed:
            unique_seed = seed + str(idx)
        else:
            unique_seed = None
        proc = Process(target=start_solver,
                       args=(network, spec, unique_seed, solution_receiver))
        solvers.append(proc)
        proc.start()
        # run_time = (time.time() - start)

    # start a thread for timing
    thread2 = Thread(target=time_up_sig, args=(time_limit, start, solvers))
    thread2.daemon = True
    thread2.start()
    # exit
    for proc in solvers:
        proc.join()
    thread1.stop()
    print(str(best_solution))
コード例 #16
0
ファイル: task5.py プロジェクト: Equilibrium23/Graphs
def get_graph_object(graph_input):
    adjacency_matrix = [[0] * len(graph_input)
                        for i in range(len(graph_input))]
    weights_matrix = [[0] * len(graph_input) for i in range(len(graph_input))]

    for vertex, neighborhood in graph_input.items():
        for neighbour_vertex in neighborhood:
            adjacency_matrix[vertex][neighbour_vertex[0]] = 1
            weights_matrix[vertex][neighbour_vertex[0]] = neighbour_vertex[1]

    graph = Graph(GraphRepresentationType.ADJACENCY_MATRIX, adjacency_matrix)
    graph.add_connection_weights_from_matrix(weights_matrix)
    return graph
コード例 #17
0
 def test_topological_sort(self):
     g = Graph()
     g.add_edge(5, 2)
     g.add_edge(5, 0)
     g.add_edge(4, 0)
     g.add_edge(4, 1)
     g.add_edge(2, 3)
     g.add_edge(3, 1)
     sorted_list = g.topological_sort()
     self.assertEqual([4, 5, 0, 2, 3, 1], sorted_list)
コード例 #18
0
ファイル: mrd.py プロジェクト: zywan/Kariz
 def dag_from_string2(self, raw_execplan):
     raw_dag = ast.literal_eval(raw_execplan)
     nodes = raw_dag['nodes']
     n_nodes = len(nodes)
     g = Graph(n_nodes)
     g.dag_id = raw_dag['id']
     g.nodes = nodes
     for src in raw_dag['edges']:
         dests = raw_dag['edges'][src]
         for dst in dests:
             g.add_edge(src, dst, 0)
     g.inputs = raw_dag['inputs']
     g.inputSize = raw_dag['size']
     g.timeValue = raw_dag['original_runtime']
     g.cachedtimeValue = raw_dag['cached_runtime']
     self.initialize_mrdtable(g)
     self.graphs[g.dag_id] = g
コード例 #19
0
    def _get_samples(self):
        samples_map = {}
        derived_from_graph = Graph()

        for biomaterial in self.manifest.get_biomaterials():
            archive_entity = ArchiveEntity()
            archive_entity.manifest_id = self.manifest.manifest_id
            archive_type = "sample"
            archive_entity.archive_entity_type = archive_type
            archive_entity.id = self.generate_archive_entity_id(
                archive_type, biomaterial.data)

            archive_entity.data = {'biomaterial': biomaterial.data}

            if biomaterial.derived_by_process:
                # TODO protocols will be needed for samples conversion
                # archive_entity.data.update(biomaterial.derived_with_protocols)

                derived_from_alias = self.generate_archive_entity_id(
                    'sample', biomaterial.derived_from)
                derived_from_graph.add_edge(derived_from_alias,
                                            archive_entity.id)
                links = {
                    'sampleRelationships': [{
                        'alias':
                        derived_from_alias,
                        'relationshipNature':
                        'derived from'
                    }]
                }
                archive_entity.links = links

            samples_map[archive_entity.id] = archive_entity

        sorted_samples = derived_from_graph.topological_sort()
        priority_samples = [
            samples_map.get(sample) for sample in sorted_samples
            if samples_map.get(sample)
        ]
        orphan_samples = [
            samples_map.get(sample) for sample in samples_map.keys()
            if sample not in priority_samples
        ]

        return priority_samples + orphan_samples
コード例 #20
0
    def test_topological_sort_for_strings(self):
        g = Graph()
        g.add_edge('donor', 'specimen')
        g.add_edge('specimen', 'cell_suspension')

        sorted_list = g.topological_sort()
        self.assertEqual(['donor', 'specimen', 'cell_suspension'], sorted_list)
コード例 #21
0
ファイル: gspan.py プロジェクト: lireland04/urbcomp-scripts
 def to_graph(self, gid=VACANT_GRAPH_ID, is_undirected=True):
     """Construct a graph according to the dfs code."""
     g = Graph(gid, is_undirected=is_undirected, eid_auto_increment=True)
     for dfsedge in self:
         frm, to, (vlb1, elb, vlb2) = dfsedge.frm, dfsedge.to, dfsedge.vevlb
         if vlb1 != VACANT_VERTEX_LABEL:
             g.add_vertex(frm, vlb1)
         if vlb2 != VACANT_VERTEX_LABEL:
             g.add_vertex(to, vlb2)
         g.add_edge(AUTO_EDGE_ID, frm, to, elb)
     return g
コード例 #22
0
    def _process_data(self, data):

        for key, _data in data.items():

            file_name = _data["image_file"]
            # image = Image.open(file_name).convert('L')

            image = Image.open(file_name)

            width, height = image.size

            labels = []
            vertexes = []
            for text, bbox_for_train, key_type, formal_key in zip(
                    _data["texts"], _data["bboxes"], _data["keys_type"],
                    _data["formal_keys"]):

                # construct the

                label = []
                if formal_key in self.key_index:
                    label.append(self.key_index[formal_key])
                    if key_type == 'key':
                        label.append(self.key_index[formal_key] * 2 - 1)
                    elif key_type == 'value':
                        label.append(self.key_index[formal_key] * 2)
                    else:
                        label.append(0)

                else:
                    label.append(0)
                    label.append(0)

                labels.append(label)

                # resize the bounding box
                bbox_for_train[0] = bbox_for_train[0] / width
                bbox_for_train[1] = bbox_for_train[1] / height
                bbox_for_train[2] = bbox_for_train[2] / width
                bbox_for_train[3] = bbox_for_train[3] / height

                bag_of_words = to_bag_of_words(text, 0, self.tok_to_id,
                                               self.blank_idx)
                vertexes.append(np.hstack((bbox_for_train, bag_of_words)))

            # construct the graph
            input_bboxs = _data["input_bboxes"]
            graph = Graph(input_bboxs)
            N = len(input_bboxs)
            A = graph.adj[:N, :, :N]

            _data["A"] = A
            _data["labels"] = np.array(labels)
            _data["image"] = 0
            _data["image_size"] = (width, height)
            _data["vertexes"] = np.array(vertexes)
コード例 #23
0
    def _define_graph(self, residual_connections=[]):
        self.layers[0].input_edges[INPUTS_NAME] = None
        self.layers[-1].output_edges[GRADIENTS_NAME] = None
        node_connections = [(self.layers[idx], self.layers[idx + 1],
                             (INPUTS_NAME, GRADIENTS_NAME))
                            for idx in range(0,
                                             len(self.layers) - 1)]
        node_connections += residual_connections

        self._graph = Graph(node_connections)
コード例 #24
0
    def create_graph(self) -> None:
        '''
        Metoda tworząca graf pełny z przypisanymi wagami.

        Lista krawędzi tego grafu jest odrazu posortowana rosnąco.
        '''
        vertices = self.create_vertices_dict()
        edges = self.create_edges()
        edges = sorted(edges, key=lambda edge: edge.weight)

        self.graph = Graph(vertices, edges)
コード例 #25
0
def generate_Gnp_graph(number_of_vertexes: int, probability: float):
    matrix = [[0 for i in range(number_of_vertexes)]
              for j in range(number_of_vertexes)]
    for i in range(number_of_vertexes):
        for j in range(number_of_vertexes):
            if j < i and randint(0, 100) < probability * 100:
                matrix[i][j] = 1
            matrix[j][i] = matrix[i][j]

    graph = Graph(GraphRepresentationType.ADJACENCY_MATRIX, matrix)
    return graph
コード例 #26
0
ファイル: gspan.py プロジェクト: lireland04/urbcomp-scripts
 def _read_graphs(self):
     self.graphs = dict()
     with codecs.open(self._database_file_name, 'r', 'utf-8') as f:
         lines = [line.strip() for line in f.readlines()]
         tgraph, graph_cnt = None, 0
         for i, line in enumerate(lines):
             cols = line.split(' ')
             if cols[0] == 't':
                 if tgraph is not None:
                     self.graphs[graph_cnt] = tgraph
                     graph_cnt += 1
                     tgraph = None
                 if cols[-1] == '-1' or graph_cnt >= self._max_ngraphs:
                     break
                 tgraph = Graph(graph_cnt,
                                is_undirected=self._is_undirected,
                                eid_auto_increment=True)
             elif cols[0] == 'v':
                 tgraph.add_vertex(cols[1], cols[2])
             elif cols[0] == 'e':
                 tgraph.add_edge(AUTO_EDGE_ID, cols[1], cols[2], cols[3])
         # adapt to input files that do not end with 't # -1'
         if tgraph is not None:
             self.graphs[graph_cnt] = tgraph
     return self
コード例 #27
0
ファイル: task2.py プロジェクト: Equilibrium23/Graphs
def print_dijkstra(G: Graph, s):
    old_representation = G.representation_type
    graph.change_graph_representation_to(
        GraphRepresentationType.ADJACENCY_MATRIX)

    p_s, d_s = dijkstra(G, s)
    w = G.graph_weights

    G.print_graph_representation()
    G.print_weights()

    print("\nSTART: s =", s)

    for v in range(len(p_s)):
        path = []
        print('d({}) = {:>4} ===> '.format(v, d_s[v]), end='')

        currNode = v
        path.append(currNode)

        while p_s[currNode] != None:
            currNode = p_s[currNode]
            path.append(currNode)

        path = path[::-1]  # reversing
        print(path)

    plot_graph(graph)

    G.change_graph_representation_to(old_representation)
コード例 #28
0
ファイル: graphnet.py プロジェクト: vojtechcima/estee
    def debug_graph(self, node_types=None):
        g = Graph()

        if node_types is None:
            node_types = self.gndef.node_types

        for nt in node_types:
            for i, data in enumerate(self.inits[nt]):
                n = g.node((nt, i))
                n.color = nt.color
                n.label = "{}\n{}\n{}".format(nt.name, i, data)
                n.shape = "box"

        for at, values in self.arcs.items():
            if at.source_nt not in node_types or at.target_nt not in node_types:
                continue
            for s, t in values:
                sn = g.node_get((at.source_nt, s))
                tn = g.node_get((at.target_nt, t))
                a = sn.add_arc(tn)

        return g
コード例 #29
0
def generate_Gnl_graph(number_of_vertexes: int, number_of_edges: int):
    possible_edges = list(itertools.combinations(range(number_of_vertexes), 2))
    count_of_possible_edges = len(possible_edges)
    if count_of_possible_edges > number_of_edges:
        return Graph(
            GraphRepresentationType.ADJACENCY_MATRIX,
            create_adjacency_matrix_from_edges_list(possible_edges,
                                                    number_of_vertexes,
                                                    number_of_edges))
    else:
        raise Exception(
            "{} of edges is too large with this number of vertexes - max = {}".
            format(number_of_edges, count_of_possible_edges))
コード例 #30
0
    def test_direct_cyclic_dependency_error_for_strings(self):
        g = Graph()
        g.add_edge('donor', 'specimen')

        with self.assertRaises(CyclicDependencyError) as context:
            g.add_edge('specimen', 'donor')

        self.assertEqual(['specimen', 'donor'], context.exception.cycle)
コード例 #31
0
ファイル: split_grid.py プロジェクト: MatsKBrun/gridding
def find_cell_color(g, cells):
    """
    Color the cells depending on the cell connections. Each group of cells
    that are connected (either directly by a shared face or through a series
    of shared faces of many cells) is are given different colors.
           c_1-c_3     c_4
         /
       c_7  |           |
         \
           c_2         c_5
    In this case, cells c_1, c_2, c_3 and c_7 will be given color 0, while
    cells c_4 and c_5 will be given color 1.

    Parameters:
    ----------
    g        - Grid for which the cells belong
    cells    - indecies of cells (=np.array([1,2,3,4,5,7]) for case above)
    """
    c = np.sort(cells)
    # Local cell-face and face-node maps.
    cf_sub, _ = __extract_submatrix(g.cell_faces, c)
    child_cell_ind = np.array([-1] * g.num_cells, dtype=np.int)
    child_cell_ind[c] = np.arange(cf_sub.shape[1])

    # Create a copy of the cell-face relation, so that we can modify it at
    # will
    cell_faces = cf_sub.copy()
    # Direction of normal vector does not matter here, only 0s and 1s
    cell_faces.data = np.abs(cell_faces.data)

    # Find connection between cells via the cell-face map
    c2c = cell_faces.transpose() * cell_faces
    # Only care about absolute values
    c2c.data = np.clip(c2c.data, 0, 1).astype('bool')

    graph = Graph(c2c)
    graph.color_nodes()
    return graph.color[child_cell_ind[cells]]