Base class pointer and polymorphism

Hi, I'm trying to implement an evolutionary algorithm for an optimization problem and I need some advice on how to structure my program and how to use polymorphism properly. I have a class Population that is supposed to contain different types of solutions that will be tested, selected, randomly modified and recombined and tested again etc. Thus I have a base class Solution, specific types of solutions are derived from it and the class Population contains a base class (Solution) pointer that can point to different specific types of solutions. So the structure of my code looks roughly like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Population{
public:
	int individuals;
	Solution* solutions;
	void recombine();
	(...)
};

void Population::recombine(){
	for (int i = 0; i < individuals/2; i++)
		recombine(solutions + i, solutions + individuals / 2 + i);
}

void recombine(SolutionType1* a, SolutionType1* b);
void recombine(SolutionType2* a, SolutionType2* b);

class Solution{
public:
	double fitness;
	(...)
};

class SolutionType1: public Solution{
public:
	std::vector<int> parameters;
	double another_parameter;
	(...)
};

class SolutionType2: public Solution{
public:
	std::vector<double> parameters;
	int another_parameter, ...;
	(...)
};


I'm wondering how to implement the recombination. The general recombination procedure does not depend on the specific type of solution. However, the parameters are specific to the derived classes, so the base class pointer cannot access them. What would be a good way to implement this? Thank!
If each solution specialization can produce T, then they can.
If you can recombine with the data in T, then you can.

So, can you have virtual T Solution::foo()?
Hi, thanks for your reply! Yes, I can do that.
However, the parameters are different for every derived class, so the class T containing them differs as well. So for every derived class, I would implement a corresponding T. Then, if I want to have
virtual T Solution::foo()
T would have to be a template parameter. So I would have to make my abstract base class a template and every derived class would be derived from the base class with the corresponding T.
Would that work and is it good style or practice? Or am I somehow overcomplicating things?
Last edited on
A vector<int> is not a vector<double>. They are similar though, so you can generate there different types from a template during compile-time.

Run-time polymorphism, however, does require common interface. There one can have vector<Base*> and call the member functions of Base for each element in the vector without knowing whether they refer to plain Base objects or to something derived.

How about Visitor-pattern?
Thanks again for your comments. To be honest, I've never heard of visitor-patterns before but after reading an introduction it seems to me that they could do what I need - I'll try that!
Topic archived. No new replies allowed.