Hello all,
I got a C++ object oriented program that was working right. I have decided to modify it by adding some polimorpysm definining a class hierarchy with virtual methods. When I call the virtual method it produces an fault segmentation error, likely because I have trash in the object.
This is the call and the warming up
GPUAntColony *colony; // Base class
GPUAntColonyConfiguration config;
set_config(config);
set_initial_pheromone(problem, config);
colony = (GPUAntColony *)new GPUSimpleAntColony(problem, config);//inhereted class
colony->run(); //Virtual method
Now let me show you the base class
class GPUAntColony {
private:
void reset_ants() {
for(unsigned int i=0; i<configuration_.number_of_ants;i++) {
ants_[i]= Util::random_number(problem_->number_of_vertices());
}
}
void initialize_Pheromone(){
for(unsigned int i=0; i<problem_->number_of_vertices()*problem_->number_of_vertices();i++) {
pheromones_[i]=(float)configuration_.initial_pheromone;
}
}
protected:
float * pheromones_;
float alpha_;
float beta_;
unsigned int iterations;
GPUAntColonyConfiguration::LocalSearchType local_search_type_;
GPUAntColonyConfiguration configuration_;
unsigned int * ants_;
GPUOptimizationProblem *problem_;
public:
///Class Constructor for the Class GPU Ant Colony
GPUAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config){
iterations=4096;
problem_ = problem; // Including distance array
configuration_ = config;
ants_= (unsigned int*) malloc(config.number_of_ants*sizeof(unsigned int));
pheromones_ = (float *) malloc(problem->number_of_vertices()*problem->number_of_vertices()*sizeof(float));
alpha_ = config.alpha;
std::cout << "alpha_ " << alpha_ << std::endl;
beta_ = config.beta;
local_search_type_ = config.local_search;
}
virtual void run();
virtual ~GPUAntColony() {
delete problem_;
free(ants_);
free (pheromones_);
};
};
The definition of the child class
class GPUSimpleAntColony : public GPUAntColony{
public:
GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config);
void run();
};
And finally the implementation of such method
void GPUAntColony::run(){
reset_ants();
initialize_Pheromone();
}
GPUSimpleAntColony::GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config):GPUAntColony(problem, config) {
}
void GPUSimpleAntColony::run() {
GPUAntColony::run();
antColonyGPULauncher(configuration_.number_of_ants, problem_->number_of_vertices(), problem_->get_distances(), pheromones_,ants_,alpha_, beta_,
configuration_.evaporation_rate, iterations, 0, 0, 0, 0, ACO_SIMPLE);
}
Hopefully you can help me.
Many thanks in advance.