libfgen  0.1.15
Library for optimization using a genetic algorithm or particle swarm optimization
fgenpp.cpp
00001 /*
00002     fgenpp.cpp -- C++ wrapper API.
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 "fgenpp.h"
00027 
00028 
00029 // Function hooks from the C library.
00030 
00031 void fgenpp_generation_callback_func(FgenPopulation *pop, const int generation) {
00032         ((FgenppPopulation *)pop->user_data)->GenerationCallback(generation);
00033 }
00034 
00035 double fgenpp_calculate_fitness_func(const FgenPopulation *pop, const unsigned char *bitstring) {
00036         return ((FgenppPopulation *)pop->user_data)->CalculateFitness(bitstring);
00037 }
00038 
00039 void fgenpp_seed_func(FgenPopulation *pop, unsigned char *bitstring) {
00040         ((FgenppPopulation *)pop->user_data)->Seed(bitstring);
00041 }
00042 
00043 void fgenpp_mutation_func(FgenPopulation *pop, const unsigned char *parent,
00044 unsigned char *child) {
00045         ((FgenppPopulation *)pop->user_data)->Mutate(parent, child);
00046 }
00047 
00048 void fgenpp_crossover_func(FgenPopulation *pop, const unsigned char *parent1,
00049 const unsigned char *parent2, unsigned char *child1, unsigned char *child2) {
00050         ((FgenppPopulation *)pop->user_data)->Crossover(parent1, parent1, child1, child2);
00051 }
00052 
00053 // Default operator functions.
00054 
00059 void FgenppPopulation::GenerationCallback(int generation) {
00060 }
00061 
00062 
00068 double FgenppPopulation::CalculateFitness(const unsigned char *) {
00069 }
00070 
00076 void FgenppPopulation::Seed(unsigned char *bitstring) {
00077         SeedBitstringRandom(bitstring);
00078 }
00079 
00085 void FgenppPopulation::Mutate(const unsigned char *parent, unsigned char *child) {
00086         MutatePerBitPlusMacroMutation(parent, child);
00087 }
00088 
00094 void FgenppPopulation::Crossover(const unsigned char *parent1, const unsigned char *parent2, unsigned char *child1,
00095 unsigned char *child2) {
00096         CrossoverTwoPointPerBit(parent1, parent2, child1, child2);
00097 }
00098 
00099 // Supplied operator functions.
00100 
00101 void FgenppPopulation::SeedBitstringRandom(unsigned char *bitstring) {
00102         fgen_seed_random(pop, bitstring);
00103 }
00104 
00105 void FgenppPopulation::SeedPermutationRandom(unsigned char *bitstring) {
00106         fgen_seed_permutation_random(pop, bitstring);
00107 }
00108 
00109 void FgenppPopulation::MutatePerBitPlusMacroMutation(const unsigned char *parent, unsigned char *child) {
00110         fgen_mutation_per_bit_plus_macro_mutation(pop, parent, child);
00111 }
00112 
00113 void FgenppPopulation::MutatePerBit(const unsigned char *parent, unsigned char *child) {
00114         fgen_mutation_per_bit(pop, parent, child);
00115 }
00116 
00117 void FgenppPopulation::MutatePermutationSwap(const unsigned char *parent, unsigned char *child) {
00118         fgen_mutation_permutation_swap(pop, parent, child);
00119 }
00120 
00121 void FgenppPopulation::MutatePermutationInsert(const unsigned char *parent, unsigned char *child) {
00122         fgen_mutation_permutation_insert(pop, parent, child);
00123 }
00124 
00125 void FgenppPopulation::MutatePermutationInvert(const unsigned char *parent, unsigned char *child) {
00126         fgen_mutation_permutation_invert(pop, parent, child);
00127 }
00128 
00129 void FgenppPopulation::CrossoverOnePointPerBit(const unsigned char *parent1, const unsigned char *parent2,
00130 unsigned char *child1, unsigned char *child2) {
00131         fgen_crossover_one_point_per_bit(pop, parent1, parent2, child1, child2);
00132 }
00133 
00134 void FgenppPopulation::CrossoverTwoPointPerBit(const unsigned char *parent1, const unsigned char *parent2,
00135 unsigned char *child1, unsigned char *child2) {
00136         fgen_crossover_two_point_per_bit(pop, parent1, parent2, child1, child2);
00137 }
00138 
00139 void FgenppPopulation::CrossoverOnePointPerElement(const unsigned char *parent1, const unsigned char *parent2,
00140 unsigned char *child1, unsigned char *child2) {
00141         fgen_crossover_one_point_per_element(pop, parent1, parent2, child1, child2);
00142 }
00143 
00144 void FgenppPopulation::CrossoverTwoPointPerElement(const unsigned char *parent1, const unsigned char *parent2,
00145 unsigned char *child1, unsigned char *child2) {
00146         fgen_crossover_two_point_per_element(pop, parent1, parent2, child1, child2);
00147 }
00148 
00149 void FgenppPopulation::CrossoverUniformPerBit(const unsigned char *parent1, const unsigned char *parent2,
00150 unsigned char *child1, unsigned char *child2) {
00151         fgen_crossover_uniform_per_bit(pop, parent1, parent2, child1, child2);
00152 }
00153 
00154 void FgenppPopulation::CrossoverUniformPerElement(const unsigned char *parent1, const unsigned char *parent2,
00155 unsigned char *child1, unsigned char *child2) {
00156         fgen_crossover_uniform_per_element(pop, parent1, parent2, child1, child2);
00157 }
00158 
00159 void FgenppPopulation::CrossoverPermutationOrderBased(const unsigned char *parent1, const unsigned char *parent2,
00160 unsigned char *child1, unsigned char *child2) {
00161         fgen_crossover_permutation_order_based(pop, parent1, parent2, child1, child2);
00162 }
00163 
00164 void FgenppPopulation::CrossoverPermutationPositionBased(const unsigned char *parent1, const unsigned char *parent2,
00165 unsigned char *child1, unsigned char *child2) {
00166         fgen_crossover_permutation_position_based(pop, parent1, parent2, child1, child2);
00167 }
00168 
00169 void FgenppPopulation::CrossoverNoop(const unsigned char *parent1, const unsigned char *parent2,
00170 unsigned char *child1, unsigned char *child2) {
00171         fgen_crossover_noop(pop, parent1, parent2, child1, child2);
00172 }
00173 
00174 // FgenppPopulation
00175 
00181 void FgenppPopulation::Initialize(int population_size, int individual_size_in_bits, int data_element_size) {
00182         pop = new FgenPopulation;
00183         fgen_initialize(
00184                 pop,
00185                 population_size,
00186                 individual_size_in_bits,
00187                 data_element_size,
00188                 fgenpp_generation_callback_func,
00189                 fgenpp_calculate_fitness_func,
00190                 fgenpp_seed_func,
00191                 fgenpp_mutation_func,
00192                 fgenpp_crossover_func);
00193         // Trick to allow access to the class given an FgenPopulation.
00194         pop->user_data = this;
00195 }
00196 
00201 void FgenppPopulation::Destroy() {
00202         fgen_destroy(pop);
00203 }
00204 
00205 void FgenppPopulation::SetParameters(
00206 int selection_type,
00207 int selection_fitness_type,
00208 float crossover_probability_float,
00209 float mutation_per_bit_probability_float,
00210 float macro_mutation_probability_float) {
00211         fgen_set_parameters(pop,
00212                 selection_type,
00213                 selection_fitness_type,
00214                 crossover_probability_float,
00215                 mutation_per_bit_probability_float,
00216                 macro_mutation_probability_float);
00217 
00218 }
00219 
00220 void FgenppPopulation::Run(int max_generation) {
00221         fgen_run(pop, max_generation);
00222 }
00223 
00224 void FgenppPopulation::RunThreaded(int max_generation) {
00225         fgen_run_threaded(pop, max_generation);
00226 }
00227 
00228 void FgenppPopulation::RunSteadyState(int max_generation) {
00229         fgen_run_steady_state(pop, max_generation);
00230 }
00231 
00232 void FgenppPopulation::SetMutationProbability(float prob) {
00233         fgen_set_mutation_probability(pop, prob);
00234 }
00235 
00236 void FgenppPopulation::SetMacroMutationProbability(float prob) {
00237         fgen_set_macro_mutation_probability(pop, prob);
00238 }
00239 
00240 void FgenppPopulation::SetCrossoverProbability(float prob) {
00241         fgen_set_crossover_probability(pop, prob);
00242 }
00243 
00244 void FgenppPopulation::SetSelectionFitnessType(int type) {
00245         fgen_set_selection_fitness_type(pop, type);
00246 }
00247 
00248 void FgenppPopulation::SetSelectionType(int type) {
00249         fgen_set_selection_type(pop, type);
00250 }
00251 
00252 void FgenppPopulation::SetTournamentSize(int size) {
00253         fgen_set_tournament_size(pop, size);
00254 }
00255 
00256 void FgenppPopulation::SetDataElementSize(int size) {
00257         fgen_set_data_element_size(pop, size);
00258 }
00259 
00260 void FgenppPopulation::SetNumberOfElites(int n) {
00261         fgen_set_number_of_elites(pop, n);
00262 }
00263 
00264 void FgenppPopulation::SetPermutationSize(int size) {
00265         fgen_set_permutation_size(pop, size);
00266 }
00267 
00268 void FgenppPopulation::SetMigrationInterval(int interval) {
00269         fgen_set_migration_interval(pop, interval);
00270 }
00271 
00272 void FgenppPopulation::SetMigrationProbability(float prob) {
00273         fgen_set_migration_probability(pop, prob);
00274 }
00275 
00276 void FgenppPopulation::SetGenerationCallbackInterval(int interval) {
00277         fgen_set_generation_callback_interval(pop, interval);
00278 }
00279 
00280 FgenppIndividual *FgenppPopulation::BestIndividual() {
00281         return (FgenppIndividual *)fgen_best_individual_of_population(pop);
00282 }
00283 
00284 FgenppIndividual *FgenppPopulation::WorstIndividual() {
00285         return (FgenppIndividual *)fgen_worst_individual_of_population(pop);
00286 }
00287 
00288 void FgenppPopulation::UpdateFitness() {
00289         fgen_update_population_fitness(pop);
00290 }
00291 
00292 void FgenppPopulation::SignalStop() {
00293         fgen_signal_stop(pop);
00294 }
00295 
00296 FgenRNG *FgenppPopulation::GetRNG() {
00297         return fgen_get_rng(pop);
00298 }
00299 
00300 int FgenppPopulation::GetIsland() {
00301         return fgen_get_island(pop);
00302 }
00303 
00304 int FgenppPopulation::GetIndividualSizeInBytes() {
00305         return fgen_individual_size_in_bytes(pop);
00306 }
00307 
00308 // FgenppArchipelago
00309 
00314 FgenppArchipelago::FgenppArchipelago(int _max_size) {
00315         max_size = _max_size;
00316         size = 0;
00317         pops = new FgenPopulation *[max_size];
00318 }
00319 
00324 FgenppArchipelago::~FgenppArchipelago() {
00325         for (int i = 0; i < size; i++)
00326                 fgen_destroy(pops[i]);
00327         delete pops;
00328 }
00329 
00334 void FgenppArchipelago::AddIsland(FgenppPopulation *pop) {
00335         pops[size] = pop->pop;
00336         size++;
00337 }
00338 
00343 void FgenppArchipelago::Run(int max_generation) {
00344         fgen_run_archipelago(size, pops, max_generation);
00345 }
00346 
00347 void FgenppArchipelago::RunThreaded(int max_generation) {
00348         fgen_run_archipelago_threaded(size, pops, max_generation);
00349 }
00350 
00351 void FgenppArchipelago::RunSteadyState(int max_generation) {
00352         fgen_run_steady_state_archipelago(size, pops, max_generation);
00353 }
00354 
00355 void FgenppArchipelago::RunSteadyStateThreaded(int max_generation) {
00356         fgen_run_steady_state_archipelago_threaded(size, pops, max_generation);
00357 }
00358 
00363 FgenppIndividual *FgenppArchipelago::BestIndividual() {
00364         return (FgenppIndividual *)fgen_best_individual_of_archipelago(size, pops);
00365 }
00366 
00371 FgenppPopulation *FgenppArchipelago::GetPopulation(int index) {
00372         return (FgenppPopulation *)(pops[index]->user_data);
00373 }
00374 
 All Data Structures Functions Variables