Пример #1
0
def setup_main(solve_data, config, fp, regularization_problem):
    """Set up main problem/main regularization problem for OA, ECP, Feasibility Pump and ROA methods.

    Args:
        solve_data (MindtPySolveData): data container that holds solve-instance data.
        config (ConfigBlock): the specific configurations for MindtPy.
        fp (bool): whether it is in the loop of feasibility pump.
        regularization_problem (bool): whether it is solving a regularization problem.
    """
    MindtPy = solve_data.mip.MindtPy_utils

    for c in MindtPy.constraint_list:
        if c.body.polynomial_degree() not in {1, 0}:
            c.deactivate()

    MindtPy.cuts.activate()

    sign_adjust = 1 if solve_data.objective_sense == minimize else -1
    MindtPy.del_component('mip_obj')
    if regularization_problem and config.single_tree:
        MindtPy.del_component('loa_proj_mip_obj')
        MindtPy.cuts.del_component('obj_reg_estimate')
    if config.add_regularization is not None and config.add_no_good_cuts:
        if regularization_problem:
            MindtPy.cuts.no_good_cuts.activate()
        else:
            MindtPy.cuts.no_good_cuts.deactivate()

    if fp:
        MindtPy.del_component('fp_mip_obj')
        if config.fp_main_norm == 'L1':
            MindtPy.fp_mip_obj = generate_norm1_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
        elif config.fp_main_norm == 'L2':
            MindtPy.fp_mip_obj = generate_norm2sq_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
        elif config.fp_main_norm == 'L_infinity':
            MindtPy.fp_mip_obj = generate_norm_inf_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
    elif regularization_problem:
        if MindtPy.objective_list[0].expr.polynomial_degree() in {1, 0}:
            MindtPy.objective_constr.activate()
        if config.add_regularization == 'level_L1':
            MindtPy.loa_proj_mip_obj = generate_norm1_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization == 'level_L2':
            MindtPy.loa_proj_mip_obj = generate_norm2sq_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization == 'level_L_infinity':
            MindtPy.loa_proj_mip_obj = generate_norm_inf_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization in {
                'grad_lag', 'hess_lag', 'hess_only_lag', 'sqp_lag'
        }:
            MindtPy.loa_proj_mip_obj = generate_lag_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                config,
                solve_data,
                discrete_only=False)
        if solve_data.objective_sense == minimize:
            MindtPy.cuts.obj_reg_estimate = Constraint(
                expr=MindtPy.objective_value <= (1 - config.level_coef) *
                solve_data.UB + config.level_coef * solve_data.LB)
        else:
            MindtPy.cuts.obj_reg_estimate = Constraint(
                expr=MindtPy.objective_value >= (1 - config.level_coef) *
                solve_data.LB + config.level_coef * solve_data.UB)

    else:
        if config.add_slack:
            MindtPy.del_component('aug_penalty_expr')

            MindtPy.aug_penalty_expr = Expression(
                expr=sign_adjust * config.OA_penalty_factor *
                sum(v for v in MindtPy.cuts.slack_vars[...]))
        main_objective = MindtPy.objective_list[-1]
        MindtPy.mip_obj = Objective(
            expr=main_objective.expr +
            (MindtPy.aug_penalty_expr if config.add_slack else 0),
            sense=solve_data.objective_sense)

        if config.use_dual_bound:
            # Delete previously added dual bound constraint
            MindtPy.cuts.del_component('dual_bound')
            if solve_data.objective_sense == minimize:
                MindtPy.cuts.dual_bound = Constraint(
                    expr=main_objective.expr +
                    (MindtPy.aug_penalty_expr if config.add_slack else 0) >=
                    solve_data.LB,
                    doc=
                    'Objective function expression should improve on the best found dual bound'
                )
            else:
                MindtPy.cuts.dual_bound = Constraint(
                    expr=main_objective.expr +
                    (MindtPy.aug_penalty_expr if config.add_slack else 0) <=
                    solve_data.UB,
                    doc=
                    'Objective function expression should improve on the best found dual bound'
                )
Пример #2
0
def setup_main(solve_data, config, fp, regularization_problem):
    """Set up main problem/main regularization problem for OA, ECP, Feasibility Pump and ROA methods.

    Parameters
    ----------
    solve_data : MindtPySolveData
        Data container that holds solve-instance data.
    config : ConfigBlock
        The specific configurations for MindtPy.
    fp : bool
        Whether it is in the loop of feasibility pump.
    regularization_problem : bool
        Whether it is solving a regularization problem.
    """
    MindtPy = solve_data.mip.MindtPy_utils

    for c in MindtPy.constraint_list:
        if c.body.polynomial_degree(
        ) not in solve_data.mip_constraint_polynomial_degree:
            c.deactivate()

    MindtPy.cuts.activate()

    sign_adjust = 1 if solve_data.objective_sense == minimize else -1
    MindtPy.del_component('mip_obj')
    if regularization_problem and config.single_tree:
        MindtPy.del_component('loa_proj_mip_obj')
        MindtPy.cuts.del_component('obj_reg_estimate')
    if config.add_regularization is not None and config.add_no_good_cuts:
        if regularization_problem:
            MindtPy.cuts.no_good_cuts.activate()
        else:
            MindtPy.cuts.no_good_cuts.deactivate()

    if fp:
        MindtPy.del_component('fp_mip_obj')
        if config.fp_main_norm == 'L1':
            MindtPy.fp_mip_obj = generate_norm1_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
        elif config.fp_main_norm == 'L2':
            MindtPy.fp_mip_obj = generate_norm2sq_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
        elif config.fp_main_norm == 'L_infinity':
            MindtPy.fp_mip_obj = generate_norm_inf_objective_function(
                solve_data.mip,
                solve_data.working_model,
                discrete_only=config.fp_discrete_only)
    elif regularization_problem:
        # The epigraph constraint is very "flat" for branching rules.
        # In ROA, if the objective function is linear(or quadratic when quadratic_strategy = 1 or 2), the original objective function is used in the MIP problem.
        # In the MIP projection problem, we need to reactivate the epigraph constraint(objective_constr).
        if MindtPy.objective_list[0].expr.polynomial_degree(
        ) in solve_data.mip_objective_polynomial_degree:
            MindtPy.objective_constr.activate()
        if config.add_regularization == 'level_L1':
            MindtPy.loa_proj_mip_obj = generate_norm1_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization == 'level_L2':
            MindtPy.loa_proj_mip_obj = generate_norm2sq_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization == 'level_L_infinity':
            MindtPy.loa_proj_mip_obj = generate_norm_inf_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                discrete_only=False)
        elif config.add_regularization in {
                'grad_lag', 'hess_lag', 'hess_only_lag', 'sqp_lag'
        }:
            MindtPy.loa_proj_mip_obj = generate_lag_objective_function(
                solve_data.mip,
                solve_data.best_solution_found,
                config,
                solve_data,
                discrete_only=False)
        if solve_data.objective_sense == minimize:
            MindtPy.cuts.obj_reg_estimate = Constraint(
                expr=sum(MindtPy.objective_value[:]) <=
                (1 - config.level_coef) * solve_data.primal_bound +
                config.level_coef * solve_data.dual_bound)
        else:
            MindtPy.cuts.obj_reg_estimate = Constraint(
                expr=sum(MindtPy.objective_value[:]) >=
                (1 - config.level_coef) * solve_data.primal_bound +
                config.level_coef * solve_data.dual_bound)
    else:
        if config.add_slack:
            MindtPy.del_component('aug_penalty_expr')

            MindtPy.aug_penalty_expr = Expression(
                expr=sign_adjust * config.OA_penalty_factor *
                sum(v for v in MindtPy.cuts.slack_vars[...]))
        main_objective = MindtPy.objective_list[-1]
        MindtPy.mip_obj = Objective(
            expr=main_objective.expr +
            (MindtPy.aug_penalty_expr if config.add_slack else 0),
            sense=solve_data.objective_sense)

        if config.use_dual_bound:
            # Delete previously added dual bound constraint
            MindtPy.cuts.del_component('dual_bound')
            if solve_data.objective_sense == minimize:
                MindtPy.cuts.dual_bound = Constraint(
                    expr=main_objective.expr +
                    (MindtPy.aug_penalty_expr if config.add_slack else 0) >=
                    solve_data.dual_bound,
                    doc=
                    'Objective function expression should improve on the best found dual bound'
                )
            else:
                MindtPy.cuts.dual_bound = Constraint(
                    expr=main_objective.expr +
                    (MindtPy.aug_penalty_expr if config.add_slack else 0) <=
                    solve_data.dual_bound,
                    doc=
                    'Objective function expression should improve on the best found dual bound'
                )