예제 #1
0
def set_up_mip_solver(solve_data, config, regularization_problem):
    """Set up the MIP solver.

    Args:
        solve_data (MindtPySolveData): data container that holds solve-instance data.
        config (ConfigBlock): the specific configurations for MindtPy.
        regularization_problem (bool): whether it is solving a regularization problem.

    Returns:
        mainopt (SolverFactory): the customized MIP solver.
    """
    # Deactivate extraneous IMPORT/EXPORT suffixes
    if config.nlp_solver == 'ipopt':
        getattr(solve_data.mip, 'ipopt_zL_out', _DoNothing()).deactivate()
        getattr(solve_data.mip, 'ipopt_zU_out', _DoNothing()).deactivate()
    if regularization_problem:
        mainopt = SolverFactory(config.mip_regularization_solver)
    else:
        if config.mip_solver == 'gurobi_persistent' and config.single_tree:
            mainopt = GurobiPersistent4MindtPy()
            mainopt.solve_data = solve_data
            mainopt.config = config
        else:
            mainopt = SolverFactory(config.mip_solver)

    # determine if persistent solver is called.
    if isinstance(mainopt, PersistentSolver):
        mainopt.set_instance(solve_data.mip, symbolic_solver_labels=True)
    if config.single_tree and not regularization_problem:
        # Configuration of cplex lazy callback
        if config.mip_solver == 'cplex_persistent':
            lazyoa = mainopt._solver_model.register_callback(
                single_tree.LazyOACallback_cplex)
            # pass necessary data and parameters to lazyoa
            lazyoa.main_mip = solve_data.mip
            lazyoa.solve_data = solve_data
            lazyoa.config = config
            lazyoa.opt = mainopt
            mainopt._solver_model.set_warning_stream(None)
            mainopt._solver_model.set_log_stream(None)
            mainopt._solver_model.set_error_stream(None)
        if config.mip_solver == 'gurobi_persistent':
            mainopt.set_callback(single_tree.LazyOACallback_gurobi)
    if config.use_tabu_list:
        tabulist = mainopt._solver_model.register_callback(
            tabu_list.IncumbentCallback_cplex)
        tabulist.solve_data = solve_data
        tabulist.opt = mainopt
        tabulist.config = config
        mainopt._solver_model.parameters.preprocessing.reduce.set(1)
        # If the callback is used to reject incumbents, the user must set the
        # parameter c.parameters.preprocessing.reduce either to the value 1 (one)
        # to restrict presolve to primal reductions only or to 0 (zero) to disable all presolve reductions
        mainopt._solver_model.set_warning_stream(None)
        mainopt._solver_model.set_log_stream(None)
        mainopt._solver_model.set_error_stream(None)
    return mainopt