Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to subclass RoutingCost in Python #380

Open
tambetm opened this issue Jan 12, 2025 · 0 comments · May be fixed by #384
Open

Add option to subclass RoutingCost in Python #380

tambetm opened this issue Jan 12, 2025 · 0 comments · May be fixed by #384

Comments

@tambetm
Copy link

tambetm commented Jan 12, 2025

RoutingCost and its subclasses RoutingCostDistance and RoutingCostTravelTime currently cannot be subclassed in Python. I implemented custom module for this, but it would be nicer to have the functionality included in the standard library. I included my Boost.Python code for RoutingCostDistance below, but unfortunately I do not have time to convert it to proper pull request.

custom_routing_cost.cpp:

#include <boost/python.hpp>
#include <boost/python/wrapper.hpp>
#include <iostream>

#include <lanelet2_core/primitives/Lanelet.h>
#include <lanelet2_traffic_rules/TrafficRules.h>
#include <lanelet2_routing/RoutingCost.h>

namespace bp = boost::python;

class PyRoutingCostDistance : public lanelet::routing::RoutingCostDistance, public bp::wrapper<lanelet::routing::RoutingCostDistance> {
public:
    PyRoutingCostDistance(double laneChangeCost, double minLaneChangeLength=0.0)
        : lanelet::routing::RoutingCostDistance(laneChangeCost, minLaneChangeLength) {}

    virtual double getCostSucceeding(const lanelet::traffic_rules::TrafficRules& tr,
                                     const lanelet::ConstLaneletOrArea& src,
                                     const lanelet::ConstLaneletOrArea& dst) const override {
        if (bp::override f = this->get_override("getCostSucceeding")) {
            //std::cout << "Calling custom getCostSucceeding" << std::endl;
            return f(boost::ref(tr), src, dst);
        }
        //std::cout << "Returning parent getCostSucceeding" << std::endl;
        return lanelet::routing::RoutingCostDistance::getCostSucceeding(tr, src, dst);
    }

    double default_getCostSucceeding(const lanelet::traffic_rules::TrafficRules& tr,
                                     const lanelet::ConstLaneletOrArea& src,
                                     const lanelet::ConstLaneletOrArea& dst) const {
        //std::cout << "Calling default getCostSucceeding" << std::endl;
        return lanelet::routing::RoutingCostDistance::getCostSucceeding(tr, src, dst);
    }

    virtual double getCostLaneChange(const lanelet::traffic_rules::TrafficRules& tr,
                                     const lanelet::ConstLanelets& src,
                                     const lanelet::ConstLanelets& dst) const noexcept override {
        if (bp::override f = this->get_override("getCostLaneChange")) {
            //std::cout << "Calling custom getCostLaneChange" << std::endl;
            return f(boost::ref(tr), src, dst);
        }
        //std::cout << "Returning parent getCostLaneChange" << std::endl;
        return lanelet::routing::RoutingCostDistance::getCostLaneChange(tr, src, dst);
    }

    double default_getCostLaneChange(const lanelet::traffic_rules::TrafficRules& tr,
                                     const lanelet::ConstLanelets& src,
                                     const lanelet::ConstLanelets& dst) const {
        //std::cout << "Calling default getCostLaneChange" << std::endl;
        return lanelet::routing::RoutingCostDistance::getCostLaneChange(tr, src, dst);
    }
};

BOOST_PYTHON_MODULE(custom_routing_cost) {
    bp::class_<PyRoutingCostDistance, std::shared_ptr<PyRoutingCostDistance>, boost::noncopyable>(
            "RoutingCostDistance",
            bp::init<double, double>((bp::arg("laneChangeCost"), bp::arg("minLaneChangeLength")=0.0))
        )
        .def("getCostSucceeding", &PyRoutingCostDistance::default_getCostSucceeding)
        .def("getCostLaneChange", &PyRoutingCostDistance::default_getCostLaneChange)
    ;
}

Example usage in Python:

from .custom_routing_cost import RoutingCostDistance

class BuslaneRoutingCost(RoutingCostDistance):
    def __init__(self, laneChangeCost, minLaneChangeLength=0.0):
        super(BuslaneRoutingCost, self).__init__(laneChangeCost, minLaneChangeLength)
    
    def getCostSucceeding(self, trafficRules, src, dst):
        cost = super().getCostSucceeding(trafficRules, src, dst)
        if ('subtype' in src.attributes and src.attributes['subtype'] == 'bus_lane') or \
            ('subtype' in dst.attributes and dst.attributes['subtype'] == 'bus_lane'):
            # double the cost for bus lanes to not drive into bus stops
            return cost * 2;
        else:
            return cost
@poggenhans poggenhans linked a pull request Jan 23, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant