def txt2dat(data_dir='data', kmax=10):
    nodes_path = os.path.join(data_dir, 'nodes.txt')
    links_path = os.path.join(data_dir, 'links.txt')
    assert os.path.exists(nodes_path)
    assert os.path.exists(links_path)

    nodes = []
    links = []
    with open(nodes_path, 'r') as fp:
        nodes += map(int, fp.readlines())
    with open(links_path, 'r') as fp:
        links += map(lambda r: map(int, r.split()), fp.readlines())

    make_bidlinks(links)

    fp = open(os.path.join(data_dir, 'base.dat'), 'w')
    fp.write('NumNodes = {};'.format(len(nodes)) + '\n')
    fp.write('NumLinks = {};'.format(len(links)) + '\n')
    fp.write('NumPaths = {};'.format(kmax))
    fp.close()
Exemple #2
0
    txt2dat(data_dir, kmax)
    # generate paths.dat, num_nodes_in_paths.dat, num_paths.dat
    G = load_graph(data_dir)
    candidate_paths = generate_candidate_paths(G, kmax)
    save_candidate_paths(candidate_paths,
                         filename=os.path.join(data_dir, 'paths.dat'))
    save_candidate_paths(candidate_paths,
                         filename=os.path.join(data_dir, 'paths.pickle'))
    # generate demand.dat
    nodes = G.nodes.keys()
    demand_mat, meta = generate_demand_mat(nodes, dist='norm')
    save_demand_mat(demand_mat, data_dir, fmt='.dat')
    save_demand_mat(demand_mat, data_dir, fmt='.pickle')
    # generate delta.dat
    links = G.edges.keys()
    make_bidlinks(links)
    delta = generate_delta(links, candidate_paths)
    save_delta(delta, data_dir)
    # generate capacity.dat
    capacity = generate_capacity(links)
    save_capacity(capacity, data_dir, '.dat')
    save_capacity(capacity, data_dir, '.pickle')
    print '[LOG] Data Created'

    # copy data/*.dat to cplex/
    register_dat_files = [
        'base.dat', 'demand.dat', 'delta.dat', 'capacity.dat'
    ]
    for filename in register_dat_files:
        shutil.copyfile(os.path.join(data_dir, filename),
                        os.path.join(cplex_dir, filename))