示例#1
0
def RuntTest(seed):

    #Init random generator
    net.NetworkGraph.SetRandomSeed(seed)

    #Define flow bounds
    FLOW_BOUNDS = (0.001, 3)
    CAPACITY_BOUNDS = (0.3, 1)

    #Network
    world_init_data = [{
        'NodesNumber': 3,
        'EdgesNumber': 8,
        'ExternalEdgesNumber': 3
    }, {
        'NodesNumber': 6,
        'EdgesNumber': 4,
        'ExternalEdgesNumber': 2
    }, {
        'NodesNumber': 4,
        'EdgesNumber': 6,
        'ExternalEdgesNumber': 1
    }, {
        'NodesNumber': 1,
        'EdgesNumber': 3,
        'ExternalEdgesNumber': 1
    }]
    network = net.NetworkGraph.GenerateSmallWorld(world_init_data,
                                                  *CAPACITY_BOUNDS)
    network.GenerateRandomSrcDst(4)

    bnb_solver = bb.BranchAndBoundSolverD(network, FLOW_BOUNDS)
    bnb_res = bnb_solver.Solve(log=True)
    print('BnB:')
    print(bnb_res)

    #Get network params
    n_list = network.GetNodeList()
    f_list = network.GetFlowList()
    sd_dict = network.GetSrcDstDict()
    a_list = network.GetArcList()
    c_dict = network.GetCapacityParam()
    nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(),
                              mg.RouteConstraintsGenerator(),
                              mg.LinearCapacityConstraintsGenerator())
    rs_model = nmg.CreateCompletRsModel(f_list, sd_dict, n_list, a_list,
                                        c_dict, FLOW_BOUNDS)
    solver = sm.CplexSolver()
    solver_result = solver.Solve(rs_model.cmodel)
    print("SOLVER:")
    print(
        f"OBJECTIVE: {solver_result['Objective']}, TIME: {solver_result['Time']}"
    )
示例#2
0
 def __init__(self, network, flow_bounds):
     self.log = True
     self.BranchingCutsName = 'BranchingCuts'
     self.required_precision = 1e-4
     # get network params
     n_list = network.GetNodeList()
     f_list = network.GetFlowList()
     sd_dict = network.GetSrcDstDict()
     a_list = network.GetArcList()
     c_dict = network.GetCapacityParam()
     # create model
     nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(),
                               mg.RouteConstraintsGenerator(),
                               mg.NonlinearCapacityConstraintsGenerator())
     self.rs_model = nmg.CreateCompletRsModel(f_list, sd_dict, n_list,
                                              a_list, c_dict, flow_bounds)
def RunTest(seed):

    #desribe run
    print('#######################################################')
    print(f'Seed is equal: {seed}')

    #define flow bounds
    FLOW_BOUNDS = (0.001, 3)
    CAPACITY_BOUNDS = (0.3, 1)

    #random network
    net.NetworkGraph.SetRandomSeed(seed)

    #Network small
    world_init_data = [ {'NodesNumber': 3, 'EdgesNumber': 8,  'ExternalEdgesNumber': 3 }, 
                        {'NodesNumber': 12, 'EdgesNumber': 32, 'ExternalEdgesNumber': 3 },
                        {'NodesNumber': 6, 'EdgesNumber': 22,  'ExternalEdgesNumber': 2 },
                         {'NodesNumber': 4, 'EdgesNumber': 12,  'ExternalEdgesNumber': 2 }
                        ]
    network = net.NetworkGraph.GenerateSmallWorld(world_init_data, *CAPACITY_BOUNDS)
    network.GenerateRandomSrcDst(12)

    # #Network medium
    # world_init_data = [ {'NodesNumber': 3, 'EdgesNumber': 8,  'ExternalEdgesNumber': 3 }, 
    #                     {'NodesNumber': 12, 'EdgesNumber': 32, 'ExternalEdgesNumber': 3 },
    #                     {'NodesNumber': 6, 'EdgesNumber': 22,  'ExternalEdgesNumber': 2 },
    #                      {'NodesNumber': 4, 'EdgesNumber': 12,  'ExternalEdgesNumber': 2 },
    #                      {'NodesNumber': 9, 'EdgesNumber': 16,  'ExternalEdgesNumber': 1 },
    #                      {'NodesNumber': 8, 'EdgesNumber': 20,  'ExternalEdgesNumber': 2 },
    #                      {'NodesNumber': 4, 'EdgesNumber': 12,  'ExternalEdgesNumber': 1 },
    #                      {'NodesNumber': 3, 'EdgesNumber': 6,  'ExternalEdgesNumber': 1 },
    #                     ]
    # network = net.NetworkGraph.GenerateSmallWorld(world_init_data, *CAPACITY_BOUNDS)
    # network.GenerateRandomSrcDst(32)

    #get network params
    n_list = network.GetNodeList()
    f_list = network.GetFlowList()
    sd_dict = network.GetSrcDstDict()
    a_list = network.GetArcList()
    c_dict = network.GetCapacityParam()

    nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(), mg.RouteConstraintsGenerator(), mg.NonlinearCapacityConstraintsGenerator())
    rs_model = nmg.CreateCompletRsModel(f_list, sd_dict, n_list, a_list, c_dict, FLOW_BOUNDS)


    start_time_lb = t.perf_counter()
    estimate_result_lb = mp.EstimateObjectvie(rs_model, False)
    elapsed_time_lb = t.perf_counter() - start_time_lb
    objective_lb, strains_lb, routes_lb, time_lb = (estimate_result_lb['Objective'], estimate_result_lb['Strain'], 
                                                        estimate_result_lb['Route'], estimate_result_lb['Time'] )
    print('###################!RESULTS!#############################')
    print(f'LB:\nObjective: {objective_lb}, Time: {time_lb}, Total time: {elapsed_time_lb}')
    violations = mp.FindConstraintsViolation(rs_model.cmodel, strains_lb, routes_lb)
    cc_vn = violations['capacity_constraints'][1]
    rc_vn = violations['route_constraints'][1]
    if cc_vn == 0 and rc_vn == 0:
        print('Feasible')
    else:
        print(f'Capacity constraint violations number: {cc_vn}')
        print(f'Route constraint violations number: {rc_vn}')
    print('__________________________________________________________')


    start_time_ub = t.perf_counter()
    estimate_result_ub = mp.EstimateObjectvie(rs_model, True)
    elapsed_time_ub = t.perf_counter() - start_time_ub
    objective_ub, strains_ub, routes_ub, time_ub = (estimate_result_ub['Objective'], estimate_result_ub['Strain'], 
                                                        estimate_result_ub['Route'], estimate_result_ub['Time'] )
    print('###################!RESULTS!#############################')
    print(f'UB:\nObjective: {objective_ub}, Time: {time_ub}, Total time: {elapsed_time_ub}')
    violations = mp.FindConstraintsViolation(rs_model.cmodel, strains_ub, routes_ub)
    cc_vn = violations['capacity_constraints'][1]
    rc_vn = violations['route_constraints'][1]
    if cc_vn == 0 and rc_vn == 0:
        print('Feasible')
    else:
        print(f'Capacity constraint violations number: {cc_vn}')
        print(f'Route constraint violations number: {rc_vn}')
    print('__________________________________________________________')
示例#4
0
def RunTest(seed, network_size, formulation, decomposition=0, coordination=0):

    #desribe run
    network_description = ['Medium', 'Large']
    formulations_description = [
        'Original', 'Linearized Constraints', 'Alternative'
    ]
    decomposition_description = [
        'Solve undecomposed', 'Demands', 'Subnetworks', 'Variable separating'
    ]
    coordination_description = [
        'Solve unrelaxed', 'Proximal cutting planes',
        'Subgradient level evaluation'
    ]
    print('#######################################################')
    print(f'Seed is equal: {seed}')
    print(f'Network size: {network_description[network_size]}')
    print(f'Formulation: {formulations_description[formulation]}')
    print(f'Decomposition: {decomposition_description[decomposition]}')
    print(f'Coordination: {coordination_description[coordination]}')

    #define flow bounds
    FLOW_BOUNDS = (0.001, 3)
    CAPACITY_BOUNDS = (0.3, 1)

    #random network
    net.NetworkGraph.SetRandomSeed(seed)

    networks = [None, None]

    #Network medium
    world_init_data = [{
        'NodesNumber': 3,
        'EdgesNumber': 8,
        'ExternalEdgesNumber': 3
    }, {
        'NodesNumber': 12,
        'EdgesNumber': 32,
        'ExternalEdgesNumber': 3
    }, {
        'NodesNumber': 6,
        'EdgesNumber': 22,
        'ExternalEdgesNumber': 2
    }, {
        'NodesNumber': 4,
        'EdgesNumber': 12,
        'ExternalEdgesNumber': 2
    }]
    networks[0] = net.NetworkGraph.GenerateSmallWorld(world_init_data,
                                                      *CAPACITY_BOUNDS)
    networks[0].GenerateRandomSrcDst(12)

    #Network large
    world_init_data = [
        {
            'NodesNumber': 3,
            'EdgesNumber': 8,
            'ExternalEdgesNumber': 3
        },
        {
            'NodesNumber': 12,
            'EdgesNumber': 32,
            'ExternalEdgesNumber': 3
        },
        {
            'NodesNumber': 6,
            'EdgesNumber': 22,
            'ExternalEdgesNumber': 2
        },
        {
            'NodesNumber': 4,
            'EdgesNumber': 12,
            'ExternalEdgesNumber': 2
        },
        {
            'NodesNumber': 9,
            'EdgesNumber': 16,
            'ExternalEdgesNumber': 1
        },
        {
            'NodesNumber': 8,
            'EdgesNumber': 20,
            'ExternalEdgesNumber': 2
        },
        {
            'NodesNumber': 4,
            'EdgesNumber': 12,
            'ExternalEdgesNumber': 1
        },
        {
            'NodesNumber': 3,
            'EdgesNumber': 6,
            'ExternalEdgesNumber': 1
        },
    ]
    networks[1] = net.NetworkGraph.GenerateSmallWorld(world_init_data,
                                                      *CAPACITY_BOUNDS)
    networks[1].GenerateRandomSrcDst(32)

    network = networks[network_size]

    #get network params
    n_list = network.GetNodeList()
    f_list = network.GetFlowList()
    sd_dict = network.GetSrcDstDict()
    a_list = network.GetArcList()
    c_dict = network.GetCapacityParam()

    #select formulation
    if formulation == 0:
        nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(),
                                  mg.RouteConstraintsGenerator(),
                                  mg.NonlinearCapacityConstraintsGenerator())
    if formulation == 1:
        nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(),
                                  mg.RouteConstraintsGenerator(),
                                  mg.LinearCapacityConstraintsGenerator())
    if formulation == 2:
        nmg = mg.RsModelGenerator(mg.QuadObjectiveGenerator(),
                                  mg.ReformulatedConstraintsGenerator())

    rs_model = nmg.CreateCompletRsModel(f_list, sd_dict, n_list, a_list,
                                        c_dict, FLOW_BOUNDS)

    #initialize solvers
    opt_solver = sm.CplexSolver()
    mstr_solver = sm.IpoptSolver()
    rec_solver = sm.IpoptSolver()

    #select coordinator
    coordinator = None
    if coordination == 0:
        coordinator = None
    if coordination == 1:
        coordinator = cr.CoordinatorCuttingPlaneProximal(lm_min=-4.6,
                                                         lm_max=6.6)
    if coordination == 2:
        coordinator = cr.CoordinatorGradient(
            step_rule=cr.gradstep.ObjectiveLevelStepRule(3.2, 1.4))

    #select decomposer
    decomposer = None
    decomposed_solvers = []
    if decomposition == 0:
        decomposer = None
    if decomposition == 1:
        decomposed_solvers = [opt_solver for _ in f_list]
        decomposer = dec.FlowDecomposer(rs_model, coordinator)
    if decomposition == 2:
        decomposed_solvers = [opt_solver for _ in world_init_data]
        decomposer = dec.SubnetsDecomposer(rs_model, coordinator,
                                           network.GetWorldNodes())
    if decomposition == 3:
        decomposed_solvers = [opt_solver, opt_solver]
        decomposer = dec.SepVarDecomposer(rs_model, coordinator)

    #deduce run parameter
    run_decomposition = (coordination != 0) and (decomposition != 0)
    run_original = not run_decomposition

    #run computation
    tf.RunTest(network,
               rs_model,
               decomposer, {
                   'Original': opt_solver,
                   'Recovered': rec_solver,
                   'Master': mstr_solver,
                   'Decomposed': decomposed_solvers
               },
               solve_original=run_original,
               solve_decomposed=run_decomposition,
               max_iter=25,
               validate_feasability=True,
               recover_feasible=True,
               draw_progress=True,
               draw_solution=False)