google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11k stars 2.11k forks source link

How to solver name #2092

Closed ShivKJ closed 4 years ago

ShivKJ commented 4 years ago

I am using python language.

from ortools.linear_solver.pywraplp import Solver
val = Solver.CBC_MIXED_INTEGER_PROGRAMMING

val is an integer.

I wanted the corresponding solver name for given val. So for example, in this case i am expecting 'CBC_MIXED_INTEGER_PROGRAMMING'

As of now, I am using the following method to get solver name from solve_type (int value)

def solver_type_name(solver_type: int) -> str:
    """
    :param solver_type:
    :return: returns solver type name from given solver type value
    """

    possible_solvers = [
                        'CBC_MIXED_INTEGER_PROGRAMMING',
                        'SAT_INTEGER_PROGRAMMING',
                        'SAT_INTEGER_PROGRAMMING',]

    if Solver_SupportsProblemType(solver_type):
        for solver_name in possible_solvers:
            if getattr(Solver, solver_name, None) == solver_type:
                return solver_name

        raise ValueError(f'solver type{solver_type} is not recognised')

    raise ValueError(f'solver type={solver_type} is not supported')

Can we have a function in ortools which provides solver name for given Solver type? Also, OptimizationProblemType is not exposed in python ortools

lperron commented 4 years ago

If you look there: https://developers.google.com/protocol-buffers/docs/reference/python-generated#enum

There is supposed to be a Name() method on the enum.

Mizux commented 4 years ago

@lperron Not sure it seem to comes from: https://github.com/google/or-tools/blob/45770b833997f827d322e929b1ed4781c4e60d44/ortools/linear_solver/linear_solver.h#L185-L236

We could use XMacro to generate std::string <=> enum convertion.

note: Please note, here we are using a regular enum and not a C++ enum class so swig will usually convert it to int....

ShivKJ commented 4 years ago

How to access it from python code?

lperron commented 4 years ago

I checked on Stack Overflow. Your code is the best proposed solution. Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53 00

Le lun. 6 juil. 2020 à 19:03, Shiv Krishna Jaiswal notifications@github.com a écrit :

How to access from python code?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/or-tools/issues/2092#issuecomment-654357202, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUPL3PEMZ4IIJFVNAPSPZDR2H7VBANCNFSM4OQF7XSA .