Ejemplo n.º 1
0
 def in_list(self, folder = "states"):
     '''This function checks if the "self" object is equal to any graph in the states
     folder via linear search.
     '''
     i = 0
     graphs_left = True
     while graphs_left:
         try:
             if self.is_equal(fio.read_graph(i, folder = "states")):
                 return True
             i += 1
         except:
             graphs_left = False
     return None
Ejemplo n.º 2
0
def partition_states(folder="states"):
    '''This partitions all of the states (already generated) by the number of edges/nodes.
    The partitioning is done by a dictionary, where the key is the number of edges and the
    value is a list of the indices to the corresponding graphs.
    '''
    partition = {}
    i = 0
    while fio.graph_file(i, folder) != fio.next_graph(folder):
        graph = fio.read_graph(i, folder)
        try:
            partition[len(graph.nodes)].append(i)
        except:
            partition[len(graph.nodes)] = [i]
        i += 1
    return partition
Ejemplo n.º 3
0
 def find_in_list(self, folder = "states"):
     '''This function checks if the "self" object is equal to any graph in the specified
     folder via linear search. If so, it returns the file name. Otherwise, it returns
     None.
     '''
     i = 0
     graphs_left = True
     while graphs_left:
         try:
             if self.is_equal(fio.read_graph(i, folder)):
                 return fio.graph_file(i, folder)
             i += 1
         except:
             graphs_left = False
     return None
Ejemplo n.º 4
0
def get_all_states(i = 0, folder = "states", partial = False, reduced_only = False, same = True):
    '''This function produces the automaton generated by graphs in folder "states". It saves
    the generated states in folder "states."
    Set partial = True to allow partial folds.
    Set reduced_only = True to only allow graphs wihtout separating edges
    Set same = False to prohibit same-length folds.
    '''
    while fio.graph_file(i, folder) != fio.next_graph(folder):
        print(i)
        graph = fio.read_graph(i, folder)
        print("These are the nodes of the graph we are folding:")
        graph.print_nodes()
         graph.perform_legal_folds(folder, partial, reduced_only, same)
        fio.write_graph(graph, i, folder)
        i += 1
Ejemplo n.º 5
0
def partition_class_to_matrix(part_class, folder="states"):
    '''Given a list representing a part of a partition of states, this function creates an
    adjacency matrix for the provided states. (That is, the matrix returned is essentially
    the adjacency matrix of all of the states with the rows and columns pertaining to the
    states not listed removed.
    '''
    n = len(part_class)
    matrix = [[0 for i in range(n)]
              for j in range(n)]  # make an nxn matrix of 0s
    row = 0
    while row < n:
        graph = fio.read_graph(part_class[row], folder)
        for out in graph.can_get_to:
            col = bin_search(out[2], part_class)
            if col is not None:  # if the outneighbour is in the partition
                matrix[row][col] = 1
        row += 1
    return matrix
Ejemplo n.º 6
0
    ##    while fio.graph_file(i, folder) != fio.next_graph(folder):
    ##        print(i)
    ##        graph = fio.read_graph(i, folder)
    ##        can_get_to = []
    ##        folds = fh.fold_obj_to_name(graph.find_legal_folds())
    ##
    ##        for fold in folds:
    ##            new = partial_fold(graph, fh.find_edge(graph, fold[0]), fold[1],
    ##                               fh.find_edge(graph, fold[2]), fold[3])
    ##            ind = new.find_in_list(folder)
    ##            if ind is None:
    ##                file_name = fio.next_graph(folder)
    ##                fio.write_file(new, file_name)
    ##                can_get_to.append([fold, "p", fio.graph_index(file_name)])
    ##            else:
    ##                can_get_to.append([fold, "p", fio.graph_index(ind)])
    ##        graph.can_get_to = can_get_to
    ##        i += 1

    ##    folder = "partial_fold_test"
    ##    graph = fio.read_graph(1, folder)
    ##    fold = fh.fold_obj_to_name(graph.find_legal_folds())[0]
    ##    new = partial_fold(graph, fh.find_edge(graph, fold[0]), fold[1],
    ##                 fh.find_edge(graph, fold[2]), fold[3])
    ##    print(graph.is_equal(new))

    folder = "from_tri_reduced_partial"
    graph = fio.read_graph(6, folder)
    new = partial_fold(graph, fh.find_edge(graph, "b"), "t",
                       fh.find_edge(graph, "d"), "h")
Ejemplo n.º 7
0
    graph.nodes.append(make_new_edge(edge1, edge2, ht1 == ht2))
    add_turns(graph, edge1, edge2, ht1 == ht2)
    fix_turns(graph, edge1, edge2, ht1 == ht2)
    f.delete_folded_edges(graph.nodes, [edge1.name, edge2.name])
    for edge in graph.nodes:
        f.remove_self_connect(edge)
    mleg.remove_val_two_vertex(graph)
    graph.update_matrix()
    return graph

if __name__ == "__main__":
    import file_IO_helper as fio
    import adj_matrices as adj
    # Testing is out of date, but functions should still work
##    graphs = fio.read_file("graphs")
##    fold_from = graphs[0]
##    old_graph = copy.deepcopy(graphs[0])    # node order goes a, b, d, e, c
##
##    # check same length folds
##    new_graph = same_length_fold(fold_from, fold_from.nodes[0], "t", fold_from.nodes[3], "t")
##    print(fold_from.is_equal(old_graph))    # check that we don't change the original grpah
##    for node in new_graph.nodes:
##        print(node.name)
##    adj.print_m(new_graph.matrix)

    graph = fio.read_graph(28) # nodes are in the order b,d,c,a
    new = same_length_fold(graph, graph.nodes[3], 't', graph.nodes[0], 'h') # folding atbh;s
    print("This is the resulting graph")
    new.print_nodes()