Skip to content

IntVarLocalSearchFilter Initialization Failure in Python #4623

@celalblgn

Description

@celalblgn

Bug Report Summary: IntVarLocalSearchFilter Initialization Failure in Python (v9.12.4544)
Environment:
OR-Tools Version: 9.12.4544 (Installed via pip)
Python Version: 3.13
Operating System: macOS
Architecture: Arm (Apple Silicon)
Problem Description:
It is currently impossible to initialize a custom local search filter class inheriting from ortools.constraint_solver.pywrapcp.IntVarLocalSearchFilter using the standard Python constructor signature in OR-Tools v9.12.4544.

Intended Use Case:

We are trying to implement a custom LocalSearchFilter to perform a Bin Packing feasibility check during the VRP solver's local search phase. The filter needs access to the routing variables (NextVar instances) to evaluate proposed route changes.
Steps Taken & Errors Encountered:
Defining the Custom Filter:

    from ortools.constraint_solver import pywrapcp

    class CustomFilter(pywrapcp.IntVarLocalSearchFilter):
        def __init__(self, routing: pywrapcp.RoutingModel, manager: pywrapcp.RoutingIndexManager):
            # Get the list of relevant IntVars (e.g., NextVar)
            variables = [routing.NextVar(i) for i in range(manager.GetNumberOfIndices())]
            # Call the base class constructor as per documentation
            super().__init__(variables) # <-- Error occurs here
            # ... rest of init ...

        def Accept(self, delta, assignment, _):
            # ... implementation ...
            return True # or False

Registering the Filter:

    # Inside solver setup
    my_filter = CustomFilter(routing_model, routing_manager)
    routing_model.AddLocalSearchFilter(my_filter)

Observed Behavior & Errors:

Standard Call (super().init(variables)): This is the correct call according to the documentation 1, which expects a single vars argument. However, this fails with:

TypeError: sequence(operations_research::IntVar*) expected

This indicates the Python list of IntVar objects is not being correctly converted/accepted by the underlying C++ binding as the expected sequence type.

Unpacking Call (super().__init__(*variables)): Attempting to unpack the list fails with:
TypeError: IntVarLocalSearchFilter.__init__() takes 2 positional arguments but N were given (where N > 2)

This confirms the constructor expects a single sequence argument, not multiple individual variable arguments.

Parameterless Call (super().__init__()): Fails with:
TypeError: IntVarLocalSearchFilter.__init__() missing 1 required positional argument: 'vars'

This confirms the vars argument is mandatory.

Conclusion:

The Python bindings for IntVarLocalSearchFilter.init in OR-Tools v9.12.4544 appear to be broken. The standard method of passing a Python list of IntVar objects, as required by the constructor's signature, results in a TypeError indicating the Python list is not being correctly interpreted as the required C++ sequence type. This prevents the use of custom IntVarLocalSearchFilters in Python for this version.
Workaround:
We are currently unable to integrate the BPP check during local search and are resorting to a post-processing check after the VRP solution is found.

Metadata

Metadata

Labels

BugLang: PythonPython wrapper issueSolver: RoutingUses the Routing library and the original CP solver

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions