Tutorial 07: Evaluate solution using cost functionsΒΆ

With our implementation of the cost functions from the CommonRoad benchmark, solutions of scenarios can be assessed locally before submitting them. More details about the cost functions can be found here.

[1]:
import os
from commonroad.common.file_reader import CommonRoadFileReader
from commonroad.common.solution import CommonRoadSolutionReader
from commonroad_dc.costs.evaluation import CostFunctionEvaluator

path = os.path.abspath("")

The solution object can either be read from a Commonroad solution xml file (this tutorial shows how to create one), or can be created on the fly from a Trajectory. In this example we use an xml file:

[2]:
solution = CommonRoadSolutionReader.open(os.path.join(path, "example_solution_tutorial_cost_functions.xml"))
scenario, planning_problem_set = CommonRoadFileReader(os.path.join(path, "DEU_Guetersloh-36_1_T-1.xml")).open()

ce = CostFunctionEvaluator.init_from_solution(solution)
cost_result = ce.evaluate_solution(scenario, planning_problem_set, solution)

print(cost_result)
Total costs for benchmark KS2:SM1:DEU_Guetersloh-36_1_T-1:2020a:
1402.0266741781913
Partial costs for solution of planning problem 1:
A:      1386.666666666668
SA:     2.6351885203703707
SR:     11.153123703703699
L:      0.31943261087593494
V:      0.0
O:      1.2522626765733225