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.

  1. Create total_costs.py in src/templates/kpi/. First define the name, direction (which direction is better), and unit of measurement. Then implement compute based on the ResultModel:

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
  1. Create __init__.py in src/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,
}
  1. Update main.py to use TotalCostsKPI. The quickstart already added a kpis argument to AppConfiguration — update the import and the dict:

from src.templates.kpi import kpis
app_cfg = AppConfiguration(
    core_config=CoreConfig(
        ...
        kpis=kpis,
        ...
    ),
)