KPIs¶
We define the KPIs for our instance. We are interested in the total cost of the tour.
The quickstart generated a skeleton at src/templates/kpi/tsp_kpi.py. We will replace that skeleton with a TotalCostsKPI implementation.
Create
total_costs.pyinsrc/templates/kpi/. First define the name, direction (which direction is better), and unit of measurement. Then implementcomputebased on theResultModel:
Code
from algomancy_scenario import BaseKPI, ImprovementDirection
from algomancy_utils import BaseMeasurement, QUANTITIES
from data_handling.result_model.result_model import ResultModel
class TotalCostsKPI(BaseKPI):
def __init__(self):
super().__init__(
"Total_costs",
ImprovementDirection.HIGHER,
BaseMeasurement(
QUANTITIES["money"]["$"], min_digits=1, max_digits=3, decimals=2
),
)
def compute(self, result: ResultModel) -> float:
total_costs = 0.0
if result.tour is not None:
for route in result.tour:
total_costs += route.cost
return total_costs
Create
__init__.pyinsrc/templates/kpi/to export the KPI template dictionary. The dict key is the name that appears in the dashboard; the value is the class:
from .total_costs import TotalCostsKPI
kpis = {
"Total_costs": TotalCostsKPI,
}
Update
main.pyto useTotalCostsKPI. The quickstart already added akpisargument toAppConfiguration— update the import and the dict:
from src.templates.kpi import kpis
app_cfg = AppConfiguration(
core_config=CoreConfig(
...
kpis=kpis,
...
),
)