google / google/or-tools

IntVarLocalSearchFilter Initialization Failure in Python

Open
#4,623 4 comments 0 reactions 2 assignees Claimed by @StevenGay View on GitHub
Bug Lang: Python Solver: Routing
Dominant language
C++
Stars
14.1k
Forks
2.5k
Avg merge
8h 39m
Merged PRs (30d)
72

Description

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:

```python
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:
```py
# 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.