//Realiza la cruza entre un padre y una madre, y guarda en hijos el resultado void AlgoritmoGenetico::cruza(Individuo & padre, Individuo & madre, std::vector<Individuo> &hijos) { hijos.clear(); unsigned int posicion_cruza; float prob = utils::randomDecimal(0.0,1.0); if (prob < this->probabilidad_cruza) { posicion_cruza = rand() % this->cantidad_genes; } else { //No se cruzan, "se clonan los padres" hijos.push_back(padre); hijos.push_back(madre); return; } //Aumento contador this->cantidad_cruzas++; //Algoritmo de cruza Individuo hijo1(this->cantidad_genes, this->id_funcion_fitness, escala, variables_fenotipo); Individuo hijo2(this->cantidad_genes, this->id_funcion_fitness, escala, variables_fenotipo); hijo1.genotipo.clear(); hijo2.genotipo.clear(); hijo1.genotipo.insert(hijo1.genotipo.begin(), padre.genotipo.begin(), padre.genotipo.begin() + posicion_cruza); hijo2.genotipo.insert(hijo2.genotipo.begin(), madre.genotipo.begin(), madre.genotipo.begin() + posicion_cruza); hijo1.genotipo.insert(hijo1.genotipo.end(), madre.genotipo.begin() + posicion_cruza, madre.genotipo.end() ); hijo2.genotipo.insert(hijo2.genotipo.end(), padre.genotipo.begin() + posicion_cruza, padre.genotipo.end() ); hijos.push_back(hijo1); hijos.push_back(hijo2); }
int main(int argc, char ** argv){ pid_t hijo; int status; int i; for(i=0; i<2; i++){ hijo = fork(); if(hijo == 0)//Hijo break; } switch(hijo){ case -1: perror("fork error"); printf("error value: %d", errno); exit(EXIT_FAILURE); break; case 0: if(i == 0){ //gnome-calculator hijo1(argv); } if(i == 1){ //Fichero hijo2(argv); } break; default: //Padre //Espera a sus hijos waitpid(hijo, &status, 0); } return 0; }