Calculator
Lagrange multiplier calculators and the trackable-processor base class.
LagrangeCalculator()
¶
Bases: TrackableProcessor
Compute Lagrange multipliers \(\lambda_{ij}^z\) for the PPC-HLP model.
For each route \((i,j)\), the calculator sorts clients by their budget-to-weight ratio and computes the Lagrange multiplier sequence using the recursive formula:
The result is stored in
BilevelDataCol.LAGRANGE.
Uses the track_metric
decorator to measure computation time automatically.
Source code in src/oracle_paper/data/calculator/base.py
calculate_lagrange(dict_a, dict_b)
staticmethod
¶
Compute the Lagrange multiplier sequence for one route's clients.
$\(\lambda_k = b_k + \sum_{t=0}^{k-1} a_t \cdot \Delta_k\)$ where \(\Delta_k = b_k/a_k - b_{k-1}/a_{k-1}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict_a
|
dict[int, float]
|
Client weights \(\{z: a_{ij}^z\}\) sorted by \(z\). |
required |
dict_b
|
dict[int, float]
|
Client budgets \(\{z: b_{ij}^z\}\) sorted by \(z\). |
required |
Returns:
| Type | Description |
|---|---|
dict[int, float]
|
Mapping \(\{z: \lambda_{ij}^z\}\) of Lagrange multipliers. |
Source code in src/oracle_paper/data/calculator/lagrange.py
process(dataset)
¶
Compute Lagrange multipliers and add them to the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MultiEntityDataset
|
Dataset with client weights and budgets (modified in-place). |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If required entities are missing. |
Source code in src/oracle_paper/data/calculator/lagrange.py
RecursiveLagrangeCalculator()
¶
Bases: TrackableProcessor
Compute recursive Lagrange multipliers for the PC-HLP model.
Groups clients on the same route \((i,j)\) that have already been
sorted by
LinearClientRanker,
then merges adjacent client segments where the Lagrange-to-weight
ratio is non-increasing. The merged groups form aggregated clients
indexed by \((i,j,z)\) where \(z\) is now a group index.
Adds four entities to the dataset:
Source code in src/oracle_paper/data/calculator/base.py
process(dataset)
¶
Compute recursive Lagrange multipliers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MultiEntityDataset
|
Dataset with Lagrange multipliers, weights, budgets, and client keys (modified in-place). |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If required entities are missing. |
Source code in src/oracle_paper/data/calculator/recursive_lagrange.py
sort_lagrange_multipliers_dict(dict_lagrange, dict_keys, dict_a)
staticmethod
¶
Merge adjacent clients where Lagrange/weight ratio is non-increasing.
Uses a stack-based algorithm: iterates over sorted clients and merges when \(\lambda_k/a_k > \lambda_{k+1}/a_{k+1}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict_lagrange
|
dict[int, float]
|
\(\{z: \lambda_{ij}^z\}\). |
required |
dict_keys
|
dict[int, int]
|
\(\{z: \text{original client IDs}\}\). |
required |
dict_a
|
dict[int, float]
|
\(\{z: a_{ij}^z\}\). |
required |
Returns:
| Type | Description |
|---|---|
dict[int, float]
|
|
dict[int, list[int]]
|
and grouped client ID lists. |
Source code in src/oracle_paper/data/calculator/recursive_lagrange.py
TrackableProcessor()
¶
Bases: EntityProcessor
Mixin for entity processors that track calculation metrics.
Provides set_metric / get_metric / get_all_metrics so
processors can record timing and other data that flows into benchmark
results and solution metadata.
Initialize the processor with metadata storage.
Source code in src/oracle_paper/data/calculator/base.py
track_metric(metric_name)
¶
Decorator to automatically track calculation time for a method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
The name to store the metric under (e.g., "lagrange_time") |
required |
Example
@track_metric("lagrange_calculation_time") def process(self, dataset): # ... calculation code ...
The decorated method must be part of a TrackableProcessor instance. The elapsed time will be stored via set_metric(metric_name, elapsed_time).