我是新的OpenMPI工作...我構建了一個GA算法(C++)來解決第n個變量方程,現在我試圖通過使用OpenMPI並行化來提高其性能。使用OpenMPI分配GA算法
的代碼結構去如下:
int main(int argc, char *argv[]){
int i=1;
int print=0;
int fitness_check;
if (argc < 2) print=1;
//initialize rand parameter
srand(time(0));
//start counting clock
auto start_time = std::chrono::high_resolution_clock::now();
//start GA
population *pop=new population();
pop->calcPopFitness();
pop->popSort();
fitness_check=pop->getElement(0).getFitness();
while(pop->getElement(0).getFitness()!=0){
pop->evolvePop();
pop->calcPopFitness();
pop->popSort();
if(fitness_check<(pop->getElement(0).getFitness())){
cout<<"Error in elitism\n";
cout<<"---------------------------\nPrinting after sort...\n";
pop->printPopulation();
cout<<"\n-------------------------------------------\n";
exit(1);
}else{
if(fitness_check>(pop->getElement(0).getFitness()))
fitness_check=(pop->getElement(0).getFitness());
}
if(print==1)cout<<"\nBest string fit in ("+to_string(i)+") iteration: "+string(pop->getElement(0).getString())+"\n";
i++;
}
if(print==1)cout<<"\nGA algorithms work!\n";
//end of GA algorithm and stop counting time
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
if(print==1)std::cout << "It took " <<
std::chrono::duration_cast<std::chrono::milliseconds>(time).count() << " milliseconds to run.\n";
writeFile(pop->getElement(0).getValues(), to_string(std::chrono::duration_cast<std::chrono::milliseconds>(time).count()));
pop->cleanup();
delete pop;
return 0;
}
我的類別是:
class chromossome{
private:
int * values;
public:
unsigned int fitness;
//constructor
chromossome();
chromossome(int *vector);
void deleteVector();
bool operator<(const chromossome& other) const {
return fitness < other.fitness;
}
unsigned int getFitness();
int* getValues();
void calcFitness();
void setGene(int i, int gene);
int getGene(int i);
//int constgetGene(int i) const;
void mutate();
string getString() const;
};
和
class population{
private:
int population_size;
vector<chromossome> ChromoPopulation;
public:
population();
population(bool newIteration);
int getSize();
void printPopulation();
void removeChromossome();
chromossome getElement(int position);
void calcPopFitness();
void popSort();
void addChromossome(chromossome individual);
chromossome *tournamentSelection();
chromossome* crossover(chromossome a, chromossome b);
void mutate();
chromossome * cloneChromossome(chromossome c);
vector<chromossome> getList();
void evolvePop();
void cleanup();
};
作爲第一個方法,我只是想分配的適應度函數,使得每個處理計算的人口的一部分的適應度。我認爲這可以通過使索引來實現的範圍內執行計算(這將要求每個進程具有訪問相同的人口),或者通過發送人口元件。
void population::calcPopFitness(){
for_each(ChromoPopulation.begin(), ChromoPopulation.end(), [=](chromossome & n)
{n.calcFitness();});
return;
}
void chromossome::calcFitness(){
int result=0;
for(int i=0; i<NUMBERVARIABLES; i++){
result+=values[i]*(i+1);
}
result-=1024;
fitness=result;
return;
}
我的目標是再大的人口和大量的變量執行此計算。
誰能告訴我什麼是最好的辦法,如果可能的話給我一些代碼的例子嗎?我一直在爲此奮鬥了一個星期,至今沒有取得任何進展......
在此先感謝...任何幫助是一個巨大的幫助。
感謝您的答覆!我在外面檢查代碼,讓你知道它是否儘快解決我的問題,因爲可能! – Bernardo
謝謝!這種爲個人傳球的方式爲我工作! – Bernardo