Repository navigation
Add a common interface for the route generators - #254
Gonzalo-Mier merged 6 commits into
Conversation
The swath orderers and the TSP planner do the same job and both return an F2CRoute, but they share no base, so a caller has to know which family it is holding. RouteGeneratorBase gives them one call. The orderers already had a matching genRoute, so they only have to declare where it comes from.
RoutePlannerBase::genRoute keeps its signature and stays non-const. Making it const as well would leave the two overloads equally good candidates for a two-argument call, and genRoute(cells, swaths) is how the library calls it itself. The shared body moves to a private const helper, since a const override cannot delegate to a non-const method.
The route planning tutorial presented the metaheuristic planner and the known patterns as unrelated APIs, which they no longer are.
Gonzalo-Mier
left a comment
There was a problem hiding this comment.
My proposal will be to remove the tutorial changes and add an issue for v3.0.0 updating the tutorials
There was a problem hiding this comment.
Tutorial should be generic for newcomers. This addition reflects more what have changed from what was before. I believe the complete tutorial could be rewritten to present everything together in a more friendly way :)
|
It makes sense. I will remove it at the appropriate time. |
The tutorial is meant to read for newcomers, and this section described the change rather than the library. It will be rewritten as a whole.
|
Removed in 3ea81fb — the tutorial is back to what it is on You know the tutorials much better than I do, so I'd leave the v3.0.0 issue to you, to frame it the way the rewrite should go. Two things I ran into that might be worth a line in it, if useful:
|
|
Thank you @u33549. I will take that into account when rewritting the tutorials. Thank you for the PR again! |
|
@Gonzalo-Mier, to avoid breaking the API, we’re mostly just making some additions and not removing much. Sometimes I feel like the codebase is getting too bloated. Do you have any plans regarding this? |
That's a really good point. What do you want to remove? We can of course simplify things. Preferably, these changes should go to v3.0.0, as a way to tell the user there are major changes. Just as a note, the objective of the library is help other scientists to compare CPP algorithms, so we should try to avoid removing final algorithms just because they are not optimal. |
|
I'm not sure right now, but I'm certain there must be a way to remove certain features without removing the algorithms. |
|
I'm super open to this. Tell me if you look at it :) |
f2c::rphas five route planners in two families: the swath orderers —BoustrophedonOrder,SnakeOrder,SpiralOrderandCustomOrder, all underSingleCellSwathsOrderBase— and the TSP planner,RoutePlannerBase. Since #251 all five do the same job,(F2CCells, F2CSwathsByCells) → F2CRoute, with the same call. But the two families don't know about each other: no common base, no common virtual method. A caller that lets the user choose the planner has to keep a code path per family:This adds the base both families implement, so the choice is made once and everything after it holds one type.
The interface
It depends on
fields2cover/types.honly, so nothing from OR-Tools reaches it. The signature is the oneSingleCellSwathsOrderBase::genRoutegot in #251, which is also the onev3.0has there — the base carries over tov3without a signature change.v3.0has no common base either: it grewgenRouteto eight overloads on the TSP side and four on the orderer side, still unrelated.How each family joins it
SingleCellSwathsOrderBase: one word. ItsgenRoutealready has exactly this signature, so it gains a base class andoverride. The body is untouched, and none of the four orderers overridesgenRoute, so none of them changes.RoutePlannerBase: a second overload. ItsgenRoutetakes the optimizer settings and is notconst:That one stays as it is. The interface is answered by a new
constoverload with the default settings, and both delegate to one privateconstbody — the current body, moved unchanged, since it only callsconstmethods and writes no members:Making the existing overload
constinstead looks simpler and is wrong: with bothconst,genRoute(cells, swaths)fits either one equally well and stops compiling — andsrc/fields2cover.cpp:118makes exactly that call. Left non-const, the existing overload wins that tie on a non-const planner, so the two-argument call keeps going where it went.Source compatibility: one call shape changes
Each shape compiled against this branch, and the overload read back from the object file's symbols:
RoutePlannerBasegenRoute(cells, swaths)genRoute(cells, swaths, true)genRoute(cells, swaths, false, 1e-3)genRoute(cells, swaths, 1e-3)show_log = trued_tol = 1e-3The last shape is the exception, and it deserves a word. Before this PR it compiled and did something nobody writing it meant: the double went into
show_log, andd_tolstayed at its default. GCC now sends it to the new overload, where the value lands ind_tol— the call starts doing what it reads as. Clang calls it ambiguous: the settings overload is the better match for the object, the new one for the third argument, and neither wins both.Nothing in the repository makes that call — every
genRoutein the library, the tutorials, the C++ and Python tests passes either two arguments, or an explicitboolthird. A caller outside that does write it gets a compile error on Clang, pointing at the line where it was already passing a tolerance as a flag. If you'd rather it kept compiling everywhere, the way out is giving the new overload a name of its own instead of overloadinggenRoute, at the cost of the interface method no longer being calledgenRouteon the TSP planner.Calling
genRoutewith the settings on aconst RoutePlannerBasedoes not compile — but it didn't before either, since the settings overload was neverconst.Python
The base is exposed as
RP_Route_generator_base_class, following the naming the other bases use.route_generator_base_test.pychecks that all five planners are instances of it on the Python side too — not just classes that happen to have agenRoute— and routes a field with each.Tests
route_generator_base_test.cpp:everyPlannerAnswersTheSameCall— all five planners, held asunique_ptr<RouteGeneratorBase>, route the same field, with a connection for every swathorderersRouteTheSameThroughTheBase— an orderer gives the same route called directly and through the basetspRoutesTheSameThroughTheBase— the same for the TSP plannerdeletingThroughTheBaseIsSafe— the virtual destructorconstPlannersRoute— the interface method is callable on aconstorderer and aconstTSP planner, and gives the same route as on a non-const oneroute_planner_base_test.cppgainsgenRouteKeepsItsCallShapes: the two-, five- and seven-argument calls still compile, reach the settings overload and give the same route.