예제 #1
0
def test_outer_approximation_cuts(problem):
    galini = Galini()
    galini.update_configuration(
        {'logging': {
            'level': 'DEBUG',
            'stdout': False,
        }})
    config = galini.get_configuration_group(
        'cuts_generator.outer_approximation')
    generator = OuterApproximationCutsGenerator(galini, config)

    bounds, mono, cvx = propagate_special_structure(problem)

    cvx_relaxation = ConvexRelaxation(problem, bounds, mono, cvx)
    relaxed_problem = RelaxedProblem(cvx_relaxation, problem).relaxed

    linear_relaxation = LinearRelaxation(relaxed_problem, bounds, mono, cvx)
    linear_problem = RelaxedProblem(linear_relaxation, relaxed_problem).relaxed

    solution = Solution(FakeStatus(FakeStatusEnum.Success),
                        [OptimalObjective('_objvar', 8.00)], [
                            OptimalVariable('x[0]', 4.0),
                            OptimalVariable('x[1]', 2.0),
                            OptimalVariable('_aux_0', 12.0),
                            OptimalVariable('_aux_1', 0.0),
                            OptimalVariable('_objvar', 8.0),
                            OptimalVariable('_aux_bilinear_x[0]_x[0]', 12.0),
                            OptimalVariable('_aux_bilinear_x[1]_x[1]', 0.0),
                        ])

    cuts = generator.generate(
        run_id=0,
        problem=problem,
        relaxed_problem=relaxed_problem,
        linear_problem=linear_problem,
        mip_solution=solution,
        tree=None,
        node=None,
    )
    assert len(cuts) == 2

    objective_cuts = [c for c in cuts if c.is_objective]
    constraint_cuts = [c for c in cuts if not c.is_objective]

    assert len(objective_cuts) == 0

    assert len(constraint_cuts) == 2
    _cut_map = [
        _check_g0,
        _check_g1,
    ]
    for i, cut in enumerate(constraint_cuts):
        _cut_map[i](cut)
예제 #2
0
    def execute_with_problem(self, model, problem, args):
        galini = Galini()
        if args.config:
            galini.update_configuration(args.config)

        solver_cls = galini.get_solver(args.solver.lower())

        if solver_cls is None:
            available = ', '.join(solvers_reg.keys())
            print('Solver {} not available. Available solvers: {}'.format(
                args.solver, available))
            sys.exit(1)

        apply_logging_config(galini.get_configuration_group('logging'))
        solver = solver_cls(galini)

        galini_group = galini.get_configuration_group('galini')
        timelimit = galini_group.get('timelimit')
        elapsed_counter = galini.telemetry.create_gauge('elapsed_time', 0.0)

        set_timelimit(timelimit)
        start_timelimit()
        start_time = current_time()

        solver.before_solve(model, problem)

        solution = solver.solve(
            problem, known_optimal_objective=args.known_optimal_objective)

        elapsed_counter.set_value(seconds_elapsed_since(start_time))

        if solution is None:
            raise RuntimeError('Solver did not return a solution')

        obj_table = OutputTable('Objectives', [
            {
                'id': 'name',
                'name': 'Objective',
                'type': 't'
            },
            {
                'id': 'value',
                'name': 'Value',
                'type': 'f'
            },
        ])

        value = solution.objective.value
        if not problem.objective.original_sense.is_minimization():
            if value is not None:
                value = -value

        obj_table.add_row({
            'name': solution.objective.name,
            'value': value,
        })

        var_table = OutputTable('Variables', [
            {
                'id': 'name',
                'name': 'Variable',
                'type': 't'
            },
            {
                'id': 'value',
                'name': 'Value',
                'type': 'f'
            },
        ])
        for var in solution.variables:
            var_table.add_row({
                'name': var.name,
                'value': var.value,
            })

        counter_table = OutputTable('Counters', [
            {
                'id': 'name',
                'name': 'Name',
                'type': 't'
            },
            {
                'id': 'value',
                'name': 'Value',
                'type': 'f'
            },
        ])
        for counter in galini.telemetry.counters_values():
            counter_table.add_row(counter)

        print_output_table([obj_table, var_table, counter_table], args)