Exemple #1
0
def show(pathname):
    print(pathname)
    if len(pathname)==0 or pathname=="/visuals/" or len(pathname)<30 and "%20" not in pathname:
        return dcc.Location(id="home",href="/404")
   

    time.sleep(2)
    return show_graph(pathname)
Exemple #2
0
def compare_trajectory():
    n = None
    set_x = []
    set_y = []

    while True:
        try:
            n = int(input("How many trajectories? "))
            if (type(n) == int) and (n > 0):
                break
            print("Enter a positive integer number")
        except Exception as e:
            print(e)

    for _ in range(n):
        while True:
            try:
                ivelo = float(input("Enter initial velocity (m/s): "))
                if ivelo > 0:
                    break
                print("Initial velocity cannot be negative")
            except Exception as e:
                print(e)

        while True:
            try:
                angle = float(input("Enter angle of trajectory (deg): "))
                if (angle > 0) and (angle < 180):
                    break
                print("Enter a positive number")
            except Exception as e:
                print(e)

        print(f"${n, ivelo, angle}")
        t_x, t_y, t_ax = get_coords_trajectory(ivelo, angle)

        set_x.extend(t_x)
        set_x = list(set(set_x))

        set_y.extend(t_y)
        set_y = list(set(set_y))

        t_ax = [min(set_x), max(set_x), min(set_y), max(set_y)]
        graph.draw_graph(t_x, t_y, t_ax)
    graph.show_graph()
Exemple #3
0
def test3():
    tree = MyBPlusTree()
    tree.add_key(1)
    tree.add_key(6)
    tree.add_key(3)
    tree.add_key(4)
    # tree.delete_key(3)
    tree.add_key(9)
    tree.add_key(7)
    tree.add_key(11)
    tree.add_key(12)
    tree.add_key(13)
    tree.delete_key(7)
    tree.add_key(14)
    print(tree.find_key(13))
    tree.add_key(15)
    tree.add_key(17)
    tree.add_key(19)
    tree.add_key(21)
    tree.add_key(23)
    tree.add_key(25)
    print(tree.find_key(23))
    graph.show_graph(tree)
Exemple #4
0
                   help='input output filetypes separated by space')
group.add_argument('--show', '-s', action="store_true", help='show the graph')
parser.add_argument('--dir',
                    '-d',
                    default=str(Path.cwd()) + "/out",
                    help='input directory for output files')
parser.add_argument('--name', '-n', help='input filename for the output file')

args = parser.parse_args()
assets = args.asset
filetypes = args.output

# TODO: if filename exists, auto-increment

try:
    current_fig = graph.draw_graph(assets)
except (data.AssetError, data.RequestError, graph.StyleError):
    pass
else:
    if filetypes:
        if not args.name:
            args.name = '_'.join(assets) + datetime.date.today().strftime(
                "%y%m%d")
        print("Program running...Press ^C to exit.")
        # generate output path with dir/filename
        path = Path(args.dir) / args.name
        graph.get_exports(current_fig, filetypes, path)
        print("Export succeeded.")
    else:
        graph.show_graph()
Exemple #5
0
def solve_graph_greedy(graph,
                       task,
                       depth,
                       runner,
                       x0=None,
                       optimizer='COBYLA',
                       max_iter=1000,
                       var_mask=None):
    '''solve a problem defined by a graph.'''
    num_bit = graph.vcount()
    N = 2**num_bit

    if task == 'max-cut':
        clause_list = get_max_cut_clauses(graph)
        loss_func = lambda z: max_cut_loss(z, clause_list)
        valid_mask = None
    elif task == 'vertex-cover':
        valid_mask = [is_vertex_cover(graph, i) for i in range(N)]
        loss_func = lambda z: vertex_cover_loss(z, graph, valid_mask)
        if runner == 'projectq':
            print(
                'warning, solving vertex_cover problem using projectq can not use `walk_mask`!'
            )
            clause_list = get_vertex_cover_clauses(graph)
    else:
        raise

    loss_table = np.array([loss_func(z) for z in range(N)])

    if runner == 'scipy':
        cc = circuit.build_qaoa_circuit(loss_table,
                                        num_bit,
                                        depth,
                                        v0=None,
                                        walk_mask=valid_mask)
    elif runner == 'projectq':
        cc = qcircuit.build_qaoa_circuit(clause_list, num_bit, depth)
    else:
        raise

    # obtain and analyse results
    if x0 is None: x0 = np.zeros(cc.num_param)
    var_mask = np.zeros(cc.num_param, dtype='bool')
    for i in range(cc.depth):
        var_mask[i] = True
        if i > 0:
            var_mask[i + cc.depth - 1] = True
        x = x0[var_mask]
        print('i=%d, mask=%s' % (i, var_mask))
        qaoa_loss, log = get_qaoa_loss(
            cc, loss_table, var_mask,
            x0)  # the expectation value of loss function
        if optimizer == 'CMA-ES':
            import cma
            es = cma.CMAEvolutionStrategy(x,
                                          0.1 * np.pi,
                                          inopts={
                                              'seed':
                                              np.random.randint(999999),
                                          })
            es.optimize(qaoa_loss, iterations=max_iter, verb_disp=1)
            best_x = es.best.x
        elif optimizer == 'COBYLA':
            best_x = minimize(qaoa_loss,
                              x0=x,
                              method='COBYLA',
                              options={
                                  'maxiter': max_iter
                              }).x
        elif optimizer == 'BRUTE':
            best_x = brute(qaoa_loss,
                           ranges=[(0, np.pi)] + [(0, 2 * np.pi)] * (i > 0),
                           Ns=10)
        else:
            raise
        x0[var_mask] = best_x
    plt.ion()
    plt.plot(log['loss'])
    pdb.set_trace()
    ans = qaoa_result_digest(x0, cc, loss_table)
    show_graph(graph, ans[2])
Exemple #6
0
def generate_minimum_network():
    initial_city = input('Digita a cidade inicial: ')
    show_graph(graph, 'initial_graph.png')
    new_graph = minimum_spanning_tree(graph, initial_city)
    show_graph(new_graph, 'minimum_spanning_tree.png')
    main_menu()