Esempio n. 1
0
    def _presolve(self, *args, **kwds):
        """
        Peform presolves.
        """
        TempfileManager.push()

        self._keepfiles = kwds.pop("keepfiles", False)
        self._define_signal_handlers = kwds.pop('use_signal_handling',None)

        OptSolver._presolve(self, *args, **kwds)

        #
        # Verify that the input problems exists
        #
        for filename in self._problem_files:
            if not os.path.exists(filename):
                msg = 'Solver failed to locate input problem file: %s'
                raise ValueError(msg % filename)
        #
        # Create command line
        #
        self._command = self.create_command_line(
            self.executable(), self._problem_files)

        self._log_file=self._command.log_file
        #
        # The pre-cleanup is probably unncessary, but also not harmful.
        #
        if (self._log_file is not None) and \
           os.path.exists(self._log_file):
            os.remove(self._log_file)
        if (self._soln_file is not None) and \
           os.path.exists(self._soln_file):
            os.remove(self._soln_file)
Esempio n. 2
0
    def _presolve(self, **kwds):
        warmstart_flag = kwds.pop('warmstart', False)
        self._keepfiles = kwds.pop('keepfiles', False)
        self._save_results = kwds.pop('save_results', True)
        self._integer_only_warmstarts = kwds.pop('integer_only_warmstarts',
                                                 False)

        # create a context in the temporary file manager for
        # this plugin - is "pop"ed in the _postsolve method.
        TempfileManager.push()

        self.results = None

        model = self._pyomo_model

        # this implies we have a custom solution "parser",
        # preventing the OptSolver _presolve method from
        # creating one
        self._results_format = ResultsFormat.soln
        # use the base class _presolve to consume the
        # important keywords
        OptSolver._presolve(self, **kwds)

        # ***********************************************************
        # The following code is only needed for backwards compatability of load_solutions=False.
        # If we ever only want to support the load_vars, load_duals, etc. methods, then this can be deleted.
        if self._save_results:
            self._smap_id = id(self._symbol_map)
            if isinstance(self._pyomo_model, IBlock):
                # BIG HACK (see pyomo.core.kernel write function)
                if not hasattr(self._pyomo_model, "._symbol_maps"):
                    setattr(self._pyomo_model, "._symbol_maps", {})
                getattr(self._pyomo_model,
                        "._symbol_maps")[self._smap_id] = self._symbol_map
            else:
                self._pyomo_model.solutions.add_symbol_map(self._symbol_map)
        # ***********************************************************

        if warmstart_flag:
            if self.warm_start_capable():
                self._warm_start()
            else:
                raise ValueError(
                    '{0} solver plugin is not capable of warmstart.'.format(
                        type(self)))

        if self._log_file is None:
            self._log_file = TempfileManager.create_tempfile(suffix='.log')
Esempio n. 3
0
    def __init__(self, **kwargs):
        """ Constructor """

        executable = kwargs.pop('executable', None)
        validate = kwargs.pop('validate', True)

        OptSolver.__init__(self, **kwargs)
        self._keepfiles  = False
        self._results_file = None
        self._timer      = ''
        self._user_executable = None
        # broadly useful for reporting, and in cases where
        # a solver plugin may not report execution time.
        self._last_solve_time = None
        self._define_signal_handlers = None

        if executable is not None:
            self.set_executable(name=executable, validate=validate)
Esempio n. 4
0
 def available(self, exception_flag=False):
     """ True if the solver is available """
     if self._assert_available:
         return True
     if not OptSolver.available(self,exception_flag):
         return False
     try:
         ans = self.executable()
     except NotImplementedError:
         ans = None
     if ans is None:
         if exception_flag:
             msg = "No executable found for solver '%s'"
             raise ApplicationError(msg % self.name)
         return False
     return True
Esempio n. 5
0
 def available(self, exception_flag=False):
     """ True if the solver is available """
     if self._assert_available:
         return True
     if not OptSolver.available(self, exception_flag):
         return False
     try:
         # HACK: Suppress logged warnings about the executable not
         # being found
         cm = nullcontext() if exception_flag else LoggingIntercept()
         with cm:
             ans = self.executable()
     except NotImplementedError:
         ans = None
     if ans is None:
         if exception_flag:
             msg = "No executable found for solver '%s'"
             raise ApplicationError(msg % self.name)
         return False
     return True
Esempio n. 6
0
    def __init__(self, **kwds):
        OptSolver.__init__(self, **kwds)

        self._pyomo_model = None
        """The pyomo model being solved."""

        self._solver_model = None
        """The python instance of the solver model (e.g., the gurobipy Model instance)."""

        self._symbol_map = SymbolMap()
        """A symbol map used to map between pyomo components and their names used with the solver."""

        self._labeler = None
        """The labeler for creating names for the solver model components."""

        self._pyomo_var_to_solver_var_map = ComponentMap()
        """A dictionary mapping pyomo Var's to the solver variables."""

        self._pyomo_con_to_solver_con_map = ComponentMap()
        """A dictionary mapping pyomo constraints to solver constraints."""

        self._vars_referenced_by_con = ComponentMap()
        """A dictionary mapping constraints to a ComponentSet containt the pyomo variables referenced by that
        constraint. This is primarily needed for the persistent solvers. When a constraint is deleted, we need
        to decrement the number of times those variables are referenced (see self._referenced_variables)."""

        self._vars_referenced_by_obj = ComponentSet()
        """A set containing the pyomo variables referenced by that the objective.
        This is primarily needed for the persistent solvers. When a the objective is deleted, we need
        to decrement the number of times those variables are referenced (see self._referenced_variables)."""

        self._objective = None
        """The pyomo Objective object currently being used with the solver."""

        self.results = None
        """A results object return from the solve method."""

        self._skip_trivial_constraints = False
        """A bool. If True, then any constraints with a constant body will not be added to the solver model.
        Be careful with this. If a trivial constraint is skipped then that constraint cannot be removed from
        a persistent solver (an error will be raised if a user tries to remove a non-existent constraint)."""

        self._output_fixed_variable_bounds = False
        """A bool. If False then an error will be raised if a fixed variable is used in one of the solver constraints.
        This is useful for catching bugs. Ordinarily a fixed variable should appear as a constant value in the
        solver constraints. If True, then the error will not be raised."""

        self._python_api_exists = False
        """A bool indicating whether or not the python api is available for the specified solver."""

        self._version = None
        """The version of the solver."""

        self._version_major = None
        """The major version of the solver. For example, if using Gurobi 7.0.2, then _version_major is 7."""

        self._symbolic_solver_labels = False
        """A bool. If true then the solver components will be given names corresponding to the pyomo component names."""

        self._capabilites = Options()

        self._referenced_variables = ComponentMap()
        """dict: {var: count} where count is the number of constraints/objective referencing the var"""

        self._keepfiles = False
        """A bool. If True, then the solver log will be saved."""

        self._save_results = True
        """A bool. This is used for backwards compatability. If True, the solution will be loaded into the Solution
Esempio n. 7
0
 def _postsolve(self):
     return OptSolver._postsolve(self)
Esempio n. 8
0
    def _queue_object_solves(self,
                             object_type,
                             objects,
                             ephemeral_solver_options,
                             disable_warmstart):

        if self.get_option("verbose"):
            print("Queuing %s solves" % (object_type[:-1]))

        assert object_type in ('bundles', 'scenarios')

        solver_dict = None
        instance_dict = None
        modify_kwds_func = None
        if object_type == 'bundles':
            if objects is None:
                objects = self.manager.scenario_tree._scenario_bundle_map
            solver_dict = self._bundle_solvers
            instance_dict = self.manager._bundle_binding_instance_map
            for bundle_name in objects:
                for scenario_name in self.manager.scenario_tree.\
                    get_bundle(bundle_name).\
                       scenario_names:
                    self.manager.scenario_tree.get_scenario(scenario_name).\
                        _instance_objective.deactivate()
            if self.preprocessor is not None:
                self.preprocessor.preprocess_bundles(bundles=objects)
                modify_kwds_func = self.preprocessor.modify_bundle_solver_keywords
        else:
            if objects is None:
                objects = self.manager.scenario_tree._scenario_map
            solver_dict = self._scenario_solvers
            instance_dict = self.manager._instances
            if self.manager.scenario_tree.contains_bundles():
                for scenario_name in objects:
                    self.manager.scenario_tree.get_scenario(scenario_name).\
                        _instance_objective.activate()
            if self.preprocessor is not None:
                self.preprocessor.preprocess_scenarios(scenarios=objects)
                modify_kwds_func = self.preprocessor.modify_scenario_solver_keywords
        assert solver_dict is not None
        assert instance_dict is not None

        # setup common solve keywords
        common_kwds = {}
        common_kwds['tee'] = self.get_option("output_solver_log")
        common_kwds['keepfiles'] = self.get_option("keep_solver_files")
        common_kwds['symbolic_solver_labels'] = \
            self.get_option("symbolic_solver_labels")
        # we always manually load solutions, so we can
        # control error reporting and such
        common_kwds['load_solutions'] = False

        # Load solver options
        solver_options = {}
        if type(self.get_option("solver_options")) is tuple:
            solver_options.update(
                OptSolver._options_string_to_dict(
                    "".join(self.get_option("solver_options"))))
        else:
            solver_options.update(self.get_option("solver_options"))
        common_kwds['options'] = solver_options

        #
        # override "persistent" values that are included from this
        # classes registered options
        #
        if ephemeral_solver_options is not None:
            common_kwds['options'].update(ephemeral_solver_options)

        # maps action handles to subproblem names
        action_handle_data = {}
        for object_name in objects:

            if modify_kwds_func is not None:
                # be sure to modify a copy of the kwds
                solve_kwds = modify_kwds_func(object_name, dict(common_kwds))
            else:
                solve_kwds = common_kwds
            opt = solver_dict[object_name]
            instance = instance_dict[object_name]
            if (not self.get_option("disable_warmstart")) and \
               (not disable_warmstart) and \
               opt.warm_start_capable():
                new_action_handle = \
                    self._solver_manager.queue(instance,
                                               opt=opt,
                                               warmstart=True,
                                               **solve_kwds)
            else:
                new_action_handle = \
                    self._solver_manager.queue(instance,
                                               opt=opt,
                                               **solve_kwds)

            action_handle_data[new_action_handle] = object_name

        return self.manager.AsyncResult(
            self._solver_manager,
            action_handle_data=action_handle_data)
Esempio n. 9
0
    def _queue_object_solves(self,
                             object_type,
                             objects,
                             ephemeral_solver_options,
                             disable_warmstart):

        if self.get_option("verbose"):
            print("Queuing %s solves" % (object_type[:-1]))

        assert object_type in ('bundles', 'scenarios')

        solver_dict = None
        instance_dict = None
        modify_kwds_func = None
        if object_type == 'bundles':
            if objects is None:
                objects = self.manager.scenario_tree._scenario_bundle_map
            solver_dict = self._bundle_solvers
            instance_dict = self.manager._bundle_binding_instance_map
            for bundle_name in objects:
                for scenario_name in self.manager.scenario_tree.\
                    get_bundle(bundle_name).\
                       scenario_names:
                    self.manager.scenario_tree.get_scenario(scenario_name).\
                        _instance_objective.deactivate()
            if self.preprocessor is not None:
                self.preprocessor.preprocess_bundles(bundles=objects)
                modify_kwds_func = self.preprocessor.modify_bundle_solver_keywords
        else:
            if objects is None:
                objects = self.manager.scenario_tree._scenario_map
            solver_dict = self._scenario_solvers
            instance_dict = self.manager._instances
            if self.manager.scenario_tree.contains_bundles():
                for scenario_name in objects:
                    self.manager.scenario_tree.get_scenario(scenario_name).\
                        _instance_objective.activate()
            if self.preprocessor is not None:
                self.preprocessor.preprocess_scenarios(scenarios=objects)
                modify_kwds_func = self.preprocessor.modify_scenario_solver_keywords
        assert solver_dict is not None
        assert instance_dict is not None

        # setup common solve keywords
        common_kwds = {}
        common_kwds['tee'] = self.get_option("output_solver_log")
        common_kwds['keepfiles'] = self.get_option("keep_solver_files")
        common_kwds['symbolic_solver_labels'] = \
            self.get_option("symbolic_solver_labels")
        # we always manually load solutions, so we can
        # control error reporting and such
        common_kwds['load_solutions'] = False

        # Load solver options
        solver_options = {}
        if type(self.get_option("solver_options")) is tuple:
            solver_options.update(
                OptSolver._options_string_to_dict(
                    "".join(self.get_option("solver_options"))))
        else:
            solver_options.update(self.get_option("solver_options"))
        common_kwds['options'] = solver_options

        #
        # override "persistent" values that are included from this
        # classes registered options
        #
        if ephemeral_solver_options is not None:
            common_kwds['options'].update(ephemeral_solver_options)

        # maps action handles to subproblem names
        action_handle_data = {}
        for object_name in objects:

            if modify_kwds_func is not None:
                # be sure to modify a copy of the kwds
                solve_kwds = modify_kwds_func(object_name, dict(common_kwds))
            else:
                solve_kwds = common_kwds
            opt = solver_dict[object_name]
            instance = instance_dict[object_name]
            if (not self.get_option("disable_warmstart")) and \
               (not disable_warmstart) and \
               opt.warm_start_capable():
                new_action_handle = \
                    self._solver_manager.queue(instance,
                                               opt=opt,
                                               warmstart=True,
                                               **solve_kwds)
            else:
                new_action_handle = \
                    self._solver_manager.queue(instance,
                                               opt=opt,
                                               **solve_kwds)

            action_handle_data[new_action_handle] = object_name

        return self.manager.AsyncResult(
            self._solver_manager,
            action_handle_data=action_handle_data)