1

So I have an assignment from college where I have to apply multiple metaheuristics to different problems. I thought that I should make everything as modular and reusable as possible to reuse metaheuristics with different problems with as little change as possible.

I need this classes:

  • Solution
  • NeighbourOperator
  • ExplorationStrategy
  • Metaheuristic

My thoughts initially were that Metaheuristic would contain an ExplorationStrategy which would contain a NeighbourOperator. The strategy would receive a solution and return a neighbour based on its members. The exploration strategy and metaheuristc would have the solution class as a template.

My ideal main.cpp would look like:

SomeProblemSolution<int> solt = randomGeneratedSolution();
SomeNeighbourOperator op;
SomeExplorationStrategy<SomeProblemSolution> strat(op);
SomeMetaheuristic<SomeProblemSolution> mthr(strat);

mthr.apply(solt);

But this is what the code would look like:

SomeProblemSolution<int> solt = randomGeneratedSolution();
SomeNeighbourOperator<int> op;
SomeExplorationStrategy<SomeProblemSolution, int> strat(op);
SomeMetaheuristic<SomeProblemSolution, int> mthr(strat);

mthr.apply(solt);

Now every class that interacts with a solution requires a template.

template<class T>
class NeighOperator {
    public:
        virtual void applyOp(Solution<T> &, int) = 0;
....

template<class T, class S>
class NeighExplorator {
    protected:
        NeighOperator<S> *_neighOperator;

    public:
        virtual T exploreNg(T &sol) = 0;
....

So

Is there any design error by which I need the template type of the solution in every other class?

Is there anyway to make the program generic as to only use the template type (the one that represents the solution) in Solution and not in every place where there's a solution involved?

Am I doing something wrong with templates (should I somehow infer the type) or should I just friend the classes I need?

Any help is much appreciated.

  • I'm pretty sure you can do template<class T> on individual methods rather than the whole class, as long as the arguments to those methods are the only things in the class which vary based on T. Does that solve your problem? – Ixrec Apr 26 '16 at 22:24
  • @lxrec Yes, it works with functions but not with member variables. The problem is that I need to have a NeighOperator, which needs a template, in NeighExplorator. – carloscb Apr 27 '16 at 10:02
  • In that case, you're probably stuck between this and changing the API. If one of the member variables is affected by the template type, then in principle it's impossible to do anything with the class without knowing what that template type is; it's not a question of "it doesn't work" so much as "it's logically impossible". – Ixrec Apr 27 '16 at 10:14
  • Ok thanks. I wanted to know if there was a workaround that I was missing. – carloscb Apr 27 '16 at 10:43
0

Is there any design error by which I need the template type of the solution in every other class?

That dependency is quite common in Genetic Algorithm / Genetic Programming frameworks and it isn't per se a design error.

There are two reasons because of which your code looks that way:

  • classes that interact with Solutions often need to store them (trials, best so far...) or classes depending on them.

    Type erasure (e.g. boost::any) doesn't mix well and boost::variant introduces unwanted constraints.

  • member function templates cannot be declared virtual but virtual methods are often a necessity (in your snippet applyOp, exploreNg...). In contrast ordinary members of class templates can be virtual and this is a further "incentive".

Consider that template template parameters (and good default values) could be a way to lower the perceived complexity of using the library and help with the coordination of the types among the various classes.

  • Template templates are definitely a step in the direction that I wanted. Now I have to grasp the concept and (hopefully) get everything working. Thanks a lot. – carloscb Apr 27 '16 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.