libfgen  0.1.15
Library for optimization using a genetic algorithm or particle swarm optimization
migration.c
00001 /*
00002     migration.c -- genetic algorithm migration implementation.
00003 
00004     fgen -- Library for optimization using a genetic algorithm or particle swarm optimization.
00005     Copyright 2012, Harm Hanemaaijer
00006 
00007     This file is part of fgen.
00008 
00009     fgen is free software: you can redistribute it and/or modify it
00010     under the terms of the GNU Lesser General Public License as published
00011     by the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     fgen is distributed in the hope that it will be useful, but WITHOUT
00015     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00017     License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public
00020     License along with fgen.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <math.h>
00027 #include <limits.h>
00028 #include <float.h>
00029 #include "fgen.h"
00030 #include "parameters.h"
00031 #include "error.h"
00032 #include "population.h"
00033 #include "win32_compat.h"
00034 
00035 /*
00036  * Each individual has a chance to migrate to the previous island.
00037  * The least fit individual on the previous island is replaced (ring topology).
00038  */
00039 
00040 static int IndexOfWorstIndividualOfPopulation(FgenPopulation *pop) {
00041         int i, worst;
00042         double worst_fitness;
00043         CalculatePopulationFitness(pop, NULL, NULL); /* Make sure every fitness is updated. */
00044         worst_fitness = POSITIVE_INFINITY_DOUBLE;
00045         for (i = 0; i < pop->size; i++)
00046                 if (pop->ind[i]->fitness < worst_fitness) {
00047                         worst_fitness = pop->ind[i]->fitness;
00048                         worst = i;
00049                 }
00050         return worst;
00051 }
00052 
00053 static void Migrate(FgenIndividual *ind, FgenPopulation *dest_pop) {
00054         int worst_index;
00055         worst_index = IndexOfWorstIndividualOfPopulation(dest_pop);
00056         FreeIndividual(dest_pop->ind[worst_index]);
00057         dest_pop->ind[worst_index] = NewIndividual(dest_pop);
00058         CopyIndividualBitstring(dest_pop, ind, dest_pop->ind[worst_index]);
00059         // It is possible to copy the fitness value here, but that wouldn't work
00060         // in the academic case of the fitness function being different between islands
00061         // (which is unlikely).
00062 }
00063 
00064 void DoMigration(int nu_pops, FgenPopulation **pops) {
00065         int i;
00066         for (i = 0; i < nu_pops; i++) {
00067                 int previous_i;
00068                 int j;
00069                 previous_i = i - 1;
00070                 if (previous_i < 0)
00071                         previous_i = nu_pops - 1;
00072                 for (j = 0; j < pops[i]->size; j++)  {
00073                         int r = fgen_random_16(pops[i]->rng);
00074                         if (r < pops[i]->migration_probability)
00075                                 Migrate(pops[i]->ind[j], pops[previous_i]);
00076                 }
00077         }
00078 }
00079 
 All Data Structures Functions Variables