Multi step reasoner in c++ templates -
i looking simple implementation of planner/search algorithm implemented compile time c++ templates. application such thing partly academic, partly driven practical needs code verification.
here essence of looking for: imagine search algorithm plan actions 'start' state 'end' state.
template<a, b> struct step{ //all steps have: static void doaction(); } struct start{}; struct state1{}; struct state2{}; struct end{}; //transition between neighbor-states known: template<> struct<start, state1>{ static void doaction(){ std::cout << "start -> state1" << std::endl; } }; template<> struct<state1, state2>{ static void doaction(){ std::cout << "state1 -> state2" << std::endl; } }; template<> struct<state2, end>{ static void doaction(){ std::cout << "state2 -> end" << std::endl; } };
now use magic of templates find sequence of steps (using sfinae or other tricks). how can following achieved?
//todo: implement reasoner: template<typename a, typename b, typename c> struct step<a,c>{ typedef step<a,b> s1; typedef step<b,c> s2; static void doaction(){ s1::doaction(); s2::doaction(); } }; // code using it: step<start, state1>::doaction(); //expect: "start -> state1" step<start, state2>::doaction(); // expect: "start -> state1" // "state1 -> state2" step<start, end>::doaction(); // expect: "start -> state1" // "state1 -> state2" // "state2 -> end"
this best solved backwards. define path<end>::cost == 0
, reasoning backwards path<state2,end>::cost==path<end>::cost + 1
because there's step<state2,end>
.
your pathfinder
algorithm need list of states consider, though: can't discover them given definition of states , step<>
Comments
Post a Comment