<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
ola por favor alguien me puede explicar lo mas importante de este codigo, es un ejemplo de algoritmo genetico por cierto muy interesante, estoy empezando hacer mi tesis sobre AGs y no entiendo mucho este codigo<br>gracias de antemano<br><br>#pragma warning(disable:4786)        // disable debug warning  DESACTIVAR LA ALERTA DE DEPURACION<br><br>#include <iostream>                    // for cout etc.<br>#include <vector>                    // for vector class<br>#include <string>                    // for string class<br>#include <algorithm>                // for sort algorithm PARA ALGORITMO DE ORDENACION<br>#include <time.h>                    // for random seed<br>#include <math.h>                    // for abs()<br>#include <stdio.h><br>#include <conio.h>                   //getch()<br><br>#define GA_POPSIZE        2048        // ga population size  TAMAÑO DE LA POBLACION<br>#define GA_MAXITER        16384        // maximum iterations  MAXIMO DE ITERACIONES<br>#define GA_ELITRATE        0.10f        // elitism rate    TASA DE ELITISMO<br>#define GA_MUTATIONRATE    0.25f        // mutation rate   LA TASA DE MUTACION<br>#define GA_MUTATION        RAND_MAX * GA_MUTATIONRATE<br>#define GA_TARGET        std::string("Hola Mario!")<br><br>using namespace std;                // polluting global namespace, but hey...<br>int i=0;<br>struct ga_struct <br>{<br>    string str;                        // the string      LA CADENA<br>    unsigned int fitness;            // its fitness    SU APTITUD<br>};<br><br>typedef vector<ga_struct> ga_vector;// for brevity      PARA ABREVIAR<br><br>void init_population(ga_vector &population,<br>                     ga_vector &buffer ) <br>{<br>    int tsize = GA_TARGET.size();<br>     cout << tsize;<br><br>    for (int i=0; i<GA_POPSIZE; i++) {<br>        ga_struct citizen;<br>        <br>        citizen.fitness = 0;<br>        citizen.str.erase();<br><br>        for (int j=0; j<tsize; j++)<br>            citizen.str += (rand() % 90) + 32;<br><br>        population.push_back(citizen);<br>    }<br><br>    buffer.resize(GA_POPSIZE);<br>}<br><br>void calc_fitness(ga_vector &population)<br>{<br>    string target = GA_TARGET;<br>    int tsize = target.size();<br>    unsigned int fitness;<br><br>    for (int i=0; i<GA_POPSIZE; i++) {<br>        fitness = 0;<br>        for (int j=0; j<tsize; j++) {<br>            fitness += abs(int(population[i].str[j] - target[j]));<br>        }<br>        <br>        population[i].fitness = fitness;<br>    }<br>}<br><br>bool fitness_sort(ga_struct x, ga_struct y) <br>{ return (x.fitness < y.fitness); }<br><br>inline void sort_by_fitness(ga_vector &population)<br>{ sort(population.begin(), population.end(), fitness_sort); }<br><br>void elitism(ga_vector &population, <br>                ga_vector &buffer, int esize )<br>{<br>    for (int i=0; i<esize; i++) {<br>        buffer[i].str = population[i].str;<br>        buffer[i].fitness = population[i].fitness;<br>    }<br>}<br><br>void mutate(ga_struct &member)<br>{<br>    int tsize = GA_TARGET.size();<br>    int ipos = rand() % tsize;<br>    int delta = (rand() % 90) + 32;<br><br>    member.str[ipos] = ((member.str[ipos] + delta) % 122);<br>}<br><br>void mate(ga_vector &population, ga_vector &buffer)<br>{<br>    int esize = GA_POPSIZE * GA_ELITRATE;<br>    int tsize = GA_TARGET.size(), spos, i1, i2;<br><br>    elitism(population, buffer, esize);<br><br>    // Mate the rest         MATE EL RESTO<br>    for (int i=esize; i<GA_POPSIZE; i++) {<br>        i1 = rand() % (GA_POPSIZE / 2);<br>        i2 = rand() % (GA_POPSIZE / 2);<br>        spos = rand() % tsize;<br><br>        buffer[i].str = population[i1].str.substr(0, spos) + <br>                        population[i2].str.substr(spos, esize - spos);<br><br>        if (rand() < GA_MUTATION) mutate(buffer[i]);<br>    }<br>}<br><br>inline void print_best(ga_vector &gav)<br><br>{<br> i++;<br> cout << i;<br> cout << " Best: " << gav[0].str << " (" << gav[0].fitness << ")" << endl;<br> }<br><br>inline void swap(ga_vector *&population,<br>                 ga_vector *&buffer)<br>{ ga_vector *temp = population; population = buffer; buffer = temp; }<br><br>int main()<br>{<br>    srand(unsigned(time(NULL)));<br><br>    ga_vector pop_alpha, pop_beta;<br>    ga_vector *population, *buffer;<br><br>    init_population(pop_alpha, pop_beta);<br>    population = &pop_alpha;<br>    buffer = &pop_beta;<br><br>    for (int i=0; i<GA_MAXITER; i++) {<br>        calc_fitness(*population);        // calculate fitness CÁLCULO DE LA APTITUD<br>        sort_by_fitness(*population);    // sort them        ORDENARLOS<br>        print_best(*population);        // print the best one  INPRIME EL MEJOR<br><br>        if ((*population)[0].fitness == 0) break;<br><br><br><br>        mate(*population, *buffer);        // mate the population together<br>        swap(population, buffer);        // swap buffers  BUFFERS DE INTERCAMBIO<br>    }<br> getch();<br><br>    return 0;<br>}<br><br><h3 align="left"> <br></h3><br><br>                                    <br /><hr />¿Quieres saber qué móvil eres? <a href='http://www.quemovileres.com/' target='_new'>¡Descúbrelo aquí!</a></body>
</html>