Ejemplo n.º 1
0
def test_stab():
    graph = nx.cycle_graph(5)
    weights = [1., 1., 1., 1., 1.]
    instance = MaxWeightStableSetInstance(graph, weights)
    solver = LearningSolver()
    solver.solve(instance)
    assert instance.model.OBJ() == 2.
Ejemplo n.º 2
0
def test_solver():
    instance = _get_instance()
    for mode in ["exact", "heuristic"]:
        for internal_solver in ["cplex", "gurobi"]:
            solver = LearningSolver(time_limit=300,
                                    gap_tolerance=1e-3,
                                    threads=1,
                                    solver=internal_solver,
                                    mode=mode)

            solver.solve(instance)
            assert instance.solution["x"][0] == 1.0
            assert instance.solution["x"][1] == 0.0
            assert instance.solution["x"][2] == 1.0
            assert instance.solution["x"][3] == 1.0
            assert instance.lower_bound == 1183.0
            assert instance.upper_bound == 1183.0

            assert round(instance.lp_solution["x"][0], 3) == 1.000
            assert round(instance.lp_solution["x"][1], 3) == 0.923
            assert round(instance.lp_solution["x"][2], 3) == 1.000
            assert round(instance.lp_solution["x"][3], 3) == 0.000
            assert round(instance.lp_value, 3) == 1287.923

            solver.fit([instance])
            solver.solve(instance)
Ejemplo n.º 3
0
def test_learning_solver():
    instance = _get_instance()
    for mode in ["exact", "heuristic"]:
        for internal_solver in ["cplex", "gurobi", GurobiSolver]:
            solver = LearningSolver(time_limit=300,
                                    gap_tolerance=1e-3,
                                    threads=1,
                                    solver=internal_solver,
                                    mode=mode)

            solver.solve(instance)
            assert instance.solution["x"][0] == 1.0
            assert instance.solution["x"][1] == 0.0
            assert instance.solution["x"][2] == 1.0
            assert instance.solution["x"][3] == 1.0
            assert instance.lower_bound == 1183.0
            assert instance.upper_bound == 1183.0
            assert round(instance.lp_solution["x"][0], 3) == 1.000
            assert round(instance.lp_solution["x"][1], 3) == 0.923
            assert round(instance.lp_solution["x"][2], 3) == 1.000
            assert round(instance.lp_solution["x"][3], 3) == 0.000
            assert round(instance.lp_value, 3) == 1287.923
            assert instance.found_violations == []
            assert len(instance.solver_log) > 100

            solver.fit([instance])
            solver.solve(instance)

            # Assert solver is picklable
            with tempfile.TemporaryFile() as file:
                pickle.dump(solver, file)
Ejemplo n.º 4
0
def _get_instances():
    instances = [
        KnapsackInstance(
            weights=[23., 26., 20., 18.],
            prices=[505., 352., 458., 220.],
            capacity=67.,
        ),
    ] * 5
    models = [inst.to_model() for inst in instances]
    solver = LearningSolver()
    for i in range(len(instances)):
        solver.solve(instances[i], models[i])
    return instances, models
Ejemplo n.º 5
0
def get_training_instances_and_models():
    instances = [
        KnapsackInstance(
            weights=[23., 26., 20., 18.],
            prices=[505., 352., 458., 220.],
            capacity=67.,
        ),
        KnapsackInstance(
            weights=[25., 30., 22., 18.],
            prices=[500., 365., 420., 150.],
            capacity=70.,
        ),
    ]
    models = [instance.to_model() for instance in instances]
    solver = LearningSolver()
    for i in range(len(instances)):
        solver.solve(instances[i], models[i])
    return instances, models
Ejemplo n.º 6
0
def _get_instances():
    instances = [
        KnapsackInstance(
            weights=[1., 2., 3.],
            prices=[10., 20., 30.],
            capacity=2.5,
        ),
        KnapsackInstance(
            weights=[3., 4., 5.],
            prices=[20., 30., 40.],
            capacity=4.5,
        ),
    ]
    models = [instance.to_model() for instance in instances]
    solver = LearningSolver()
    for (i, instance) in enumerate(instances):
        solver.solve(instances[i], models[i])
    return instances, models
Ejemplo n.º 7
0
def test_instance():
    n_cities = 4
    distances = np.array([
        [0., 1., 2., 1.],
        [1., 0., 1., 2.],
        [2., 1., 0., 1.],
        [1., 2., 1., 0.],
    ])
    instance = TravelingSalesmanInstance(n_cities, distances)
    for solver_name in ['gurobi', 'cplex']:
        solver = LearningSolver(solver=solver_name)
        solver.solve(instance)
        x = instance.solution["x"]
        assert x[0, 1] == 1.0
        assert x[0, 2] == 0.0
        assert x[0, 3] == 1.0
        assert x[1, 2] == 1.0
        assert x[1, 3] == 0.0
        assert x[2, 3] == 1.0
        assert instance.lower_bound == 4.0
        assert instance.upper_bound == 4.0
Ejemplo n.º 8
0
def test_subtour():
    n_cities = 6
    cities = np.array([
        [0., 0.],
        [1., 0.],
        [2., 0.],
        [3., 0.],
        [0., 1.],
        [3., 1.],
    ])
    distances = squareform(pdist(cities))
    instance = TravelingSalesmanInstance(n_cities, distances)
    for solver_name in ['gurobi', 'cplex']:
        solver = LearningSolver(solver=solver_name)
        solver.solve(instance)
        x = instance.solution["x"]
        assert x[0, 1] == 1.0
        assert x[0, 4] == 1.0
        assert x[1, 2] == 1.0
        assert x[2, 3] == 1.0
        assert x[3, 5] == 1.0
        assert x[4, 5] == 1.0