예제 #1
0
 def solver(self, value):
     not_valid_interface = SolverNotFound(
         "'{}' is not a valid solver interface. Pick one from {}.".format(
             value, ", ".join(list(solvers))))
     if isinstance(value, string_types):
         if value not in solvers:
             raise not_valid_interface
         interface = solvers[value]
     elif isinstance(value, types.ModuleType) and hasattr(value, 'Model'):
         interface = value
     else:
         raise not_valid_interface
     self._solver = interface
예제 #2
0
 def solver(self, value) -> None:
     """Set the optlang solver interface."""
     not_valid_interface = SolverNotFound(
         f"'{value}' is not a valid solver interface. "
         f" Please pick one from {', '.join(SOLVERS)}.")
     if isinstance(value, str):
         if value not in SOLVERS:
             raise not_valid_interface
         interface = SOLVERS[value]
     elif isinstance(value, types.ModuleType) and hasattr(value, "Model"):
         interface = value
     else:
         raise not_valid_interface
     self._solver = interface
예제 #3
0
    def solver(self, value):
        not_valid_interface = SolverNotFound(
            "%s is not a valid solver interface. Pick from %s." %
            (value, list(solvers)))
        if isinstance(value, six.string_types):
            try:
                interface = solvers[interface_to_str(value)]
            except KeyError:
                raise not_valid_interface
        elif isinstance(value, types.ModuleType) and hasattr(value, "Model"):
            interface = value
        elif isinstance(value, optlang.interface.Model):
            interface = value.interface
        else:
            raise not_valid_interface

        # Do nothing if the solver did not change
        if self.problem == interface:
            return
        self._solver = interface.Model.clone(self._solver)
예제 #4
0
파일: solver.py 프로젝트: cgaray/cobrapy
def check_solver(obj):
    """Check whether the chosen solver is valid.

    Check whether chosen solver is valid and also warn when using
    a specialized solver. Will return the optlang interface for the
    requested solver.

    Parameters
    ----------
    obj : str or optlang.interface or optlang.interface.Model
        The chosen solver.

    Raises
    ------
    SolverNotFound
        If the solver is not valid.
    """
    not_valid_interface = SolverNotFound(
        f"{obj} is not a valid solver interface. Pick one from {', '.join(solvers)}."
    )
    if isinstance(obj, str):
        try:
            interface = solvers[interface_to_str(obj)]
        except KeyError:
            raise not_valid_interface
    elif isinstance(obj, ModuleType) and hasattr(obj, "Model"):
        interface = obj
    elif isinstance(obj, optlang.interface.Model):
        interface = obj.interface
    else:
        raise not_valid_interface

    if interface_to_str(interface) in ["osqp", "coinor_cbc"]:
        logger.warning(
            "OSQP and CBC are specialized solvers for quadratic programming (QP) and "
            "mixed-integer programming (MIP) problems and may not perform well on "
            "general LP problems. So unless you intend to solve a QP or MIP problem, "
            "we recommend to change the solver back to a general purpose solver "
            "like `model.solver = 'glpk'` for instance.")

    return interface