Пример #1
0
 def test_random_opt2(self):
     """
     Check that *Algorithm OPT2* works on 10 randomly generated synchronous circuits.
     """
     for _ in range(10):
         g = gen_random_circuit()
         gr = opt2(g)
         self.assertLessEqual(cp(gr), cp(g))
Пример #2
0
def random_test(n=10000):
    for _ in trange(n):
        V = np.random.randint(5, 30)
        E = np.random.randint(5, 30)
        g = gen_random_circuit(V, E)
        cpg = cp(g)
        cpr1 = cp(opt1(g))
        cpr2 = cp(opt2(g))
        assert cpr1 == cpr2 and cpr1 <= cpg and cpr2 <= cpg
Пример #3
0
 def test_random_wd(self):
     """
     Check that the computed :math:`W` and :math:`D` matrices correspond to the definition.
     """
     for _ in range(10):
         g = gen_random_circuit()
         W, D = wd(g)
         for u in g.nodes:
             for v in g.nodes:
                 if nx.has_path(g, u, v):
                     if u == v:
                         self.assertEqual(W[u, v], 0)
                         self.assertEqual(D[u, v], d(g, u))
                     else:
                         self.assertEqual(W[u, v], min([w_path(g, p) for p in nx.all_simple_paths(g, u, v)]))
                         self.assertEqual(D[u, v], max([d_path(g, p) for p in nx.all_simple_paths(g, u, v) if w_path(g, p) == W[u, v]]))
                 else:
                     self.assertFalse((u, v) in W)
                     self.assertFalse((u, v) in D)
Пример #4
0
 def gen_random_circuit_edges(n):
     n_nodes = round(n / 11 * 8)
     # print(f'Generating circuit with {n_nodes} vertices and {n} edges...')
     return gen_random_circuit(n_nodes, n)
Пример #5
0
 def gen_random_circuit_nodes(n):
     n_edges = round(n / 8 * 11)
     # print(f'Generating circuit with {n} vertices and {n_edges} edges...')
     return gen_random_circuit(n, n_edges)
Пример #6
0
    if save is not None:
        path = save+'_opt2.dot'
        save_graph(g2, path)
        print(f'Output graph saved to {path}')
    cpr2 = cp(g2)
    print(f'The graph returned by OPT2 has a clock period of {cpr2}')


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--output', '-o', help='File where to save the output graphs (please omit the extension, it '
                                               'will be added automatically')
    parser.add_argument('--show-wd', action='store_true', help='Show matrices W and D')
    subparsers = parser.add_subparsers()
    parser_random = subparsers.add_parser('random', help='Run the algorithms on a random graph')
    parser_random.add_argument('--nodes', '-n', type=int, default=8, help='The number of nodes (default 8)')
    parser_random.add_argument('--edges', '-e', type=int, default=11, help='The number of edges (default 11)')
    parser_file = subparsers.add_parser('file', help='Run the algorithms on a graph loaded from a provided DOT file')
    parser_file.add_argument('file', help='The DOT file from which the graph is loaded')
    args = parser.parse_args()
    if 'nodes' in args and 'edges' in args:
        print(f'Generating random graph with {args.nodes} nodes and {args.edges} edges')
        g = gen_random_circuit(args.nodes, args.edges)
        run(g, save=args.output, show_wd=args.show_wd)
    elif 'file' in args:
        print(f'Loading graph from {args.file}')
        g = load_graph(args.file)
        run(g, save=args.output, show_wd=args.show_wd)
    else:
        print('ERROR: unrecognized argument')