代码之家  ›  专栏  ›  技术社区  ›  Bernardo

基于OpenMPI的分布式遗传算法

  •  0
  • Bernardo  · 技术社区  · 7 年前

    我是OpenMPI的新手…我构建了一个GA算法(c++)来求解第n个变量方程,现在我正试图通过使用OpenMPI并行化来提高其性能。

    代码结构如下:

    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;
    }
    

    我的目标是使用大量人口和大量变量执行此计算。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mihai Oltean    7 年前

    您可以看看我们使用MPI实现的遗传编程变体:

    https://github.com/mihaioltean/evolve-tsp-heuristics/tree/master/train/mpi