-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions
objective
and example6
to examples/ex6.hpp
and `e…
…xamples/ex8.hpp` to calculate the objective value and run the examples with seed and command line options as input parameters. * Add `objective` function to `examples/ex6.hpp` and `examples/ex8.hpp` to calculate the objective value for examples 6 and 8. * Add `example6` and `example8` functions to `examples/ex6.hpp` and `examples/ex8.hpp` to run the examples with seed and command line options as input parameters. * Return `ga.statistics()` from `example6` and `example8` functions. Update `test/GAExamplesTest.cpp` to add unit tests for examples 6 and 8 using `example6` and `example8` functions. * Add unit test for example 6 using `example6` function. * Add unit test for example 8 using `example8` function. * Update existing unit tests to use `example1`, `example2`, `example3`, `example4`, `example7`, and `example9` functions. Remove main methods from `examples/ex6.C` and `examples/ex8.C` and move their content to `example6` and `example8` functions in `ex6.hpp` and `ex8.hpp`. * Remove main method from `examples/ex6.C` and move its content to `example6` function in `ex6.hpp`. * Remove main method from `examples/ex8.C` and move its content to `example8` function in `ex8.hpp`. * Include `ex6.hpp` and `ex8.hpp` header files in `examples/ex6.C` and `examples/ex8.C`. Update `examples/ex1.hpp`, `examples/ex2.hpp`, `examples/ex3.hpp`, and `examples/ex4.hpp` to add `objective` and `example` functions. * Add `objective` function to `examples/ex1.hpp`, `examples/ex2.hpp`, `examples/ex3.hpp`, and `examples/ex4.hpp` to calculate the objective value for examples 1, 2, 3, and 4. * Add `example1`, `example2`, `example3`, and `example4` functions to `examples/ex1.hpp`, `examples/ex2.hpp`, `examples/ex3.hpp`, and `examples/ex4.hpp` to run the examples with seed and command line options as input parameters. * Return `ga.statistics()` from `example1`, `example2`, `example3`, and `example4` functions. Remove main methods from `examples/ex2.C`, `examples/ex3.C`, and `examples/ex4.C` and move their content to `example2`, `example3`, and `example4` functions in `ex2.hpp`, `ex3.hpp`, and `ex4.hpp`. * Remove main method from `examples/ex2.C` and move its content to `example2` function in `ex2.hpp`. * Remove main method from `examples/ex3.C` and move its content to `example3` function in `ex3.hpp`. * Remove main method from `examples/ex4.C` and move its content to `example4` function in `ex4.hpp`. * Include `ex2.hpp`, `ex3.hpp`, and `ex4.hpp` header files in `examples/ex2.C`, `examples/ex3.C`, and `examples/ex4.C`.
- Loading branch information
Showing
53 changed files
with
2,027 additions
and
1,838 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ int main(int argc, char **argv) | |
} | ||
} | ||
|
||
ex1(); | ||
example1(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#pragma once | ||
|
||
#include <ga.h> | ||
#include <cmath> | ||
|
||
#define USE_RAW_SINE | ||
#define NBITS 8 | ||
|
||
#ifdef USE_RAW_SINE | ||
#define FUNCTION Function1 | ||
#define MIN_VALUE 0 | ||
#define MAX_VALUE 5 | ||
#else | ||
#define FUNCTION Function2 | ||
#define MIN_VALUE -100 | ||
#define MAX_VALUE 100 | ||
#endif | ||
|
||
float Function1(float); | ||
float Function2(float); | ||
float Objective(GAGenome &); | ||
float BitDistance(const GAGenome & a, const GAGenome & b); | ||
float PhenotypeDistance(const GAGenome & a, const GAGenome & b); | ||
|
||
float objective(GAGenome & g) | ||
{ | ||
return Objective(g); | ||
} | ||
|
||
GAStatistics example10(unsigned int seed, int argc, char **argv) | ||
{ | ||
GABin2DecPhenotype map; | ||
map.add(NBITS, MIN_VALUE, MAX_VALUE); | ||
|
||
GABin2DecGenome genome(map, objective); | ||
genome.crossover(GABin2DecGenome::UniformCrossover); | ||
genome.crossoverProbability(0.6); | ||
genome.mutationProbability(0.01); | ||
genome.comparator(BitDistance); | ||
genome.comparator(PhenotypeDistance); | ||
|
||
GASimpleGA ga(genome); | ||
ga.populationSize(30); | ||
ga.nGenerations(100); | ||
ga.pMutation(0.01); | ||
ga.pCrossover(0.6); | ||
ga.evolve(seed); | ||
|
||
return ga.statistics(); | ||
} | ||
|
||
float Function1(float v) | ||
{ | ||
return 1 + sin(v * 2 * M_PI); | ||
} | ||
|
||
float Function2(float v) | ||
{ | ||
float y; | ||
y = 100.0 * exp(-fabs(v) / 50.0) * (1.0 - cos(v * M_PI * 2.0 / 25.0)); | ||
if (v < -100 || v > 100) | ||
y = 0; | ||
if (y < 0) | ||
y = 0; | ||
return y + 0.00001; | ||
} | ||
|
||
float Objective(GAGenome & c) | ||
{ | ||
auto & genome = (GABin2DecGenome &)c; | ||
return FUNCTION(genome.phenotype(0)); | ||
} | ||
|
||
float BitDistance(const GAGenome & c1, const GAGenome & c2) | ||
{ | ||
auto & a = (GABin2DecGenome &)c1; | ||
auto & b = (GABin2DecGenome &)c2; | ||
|
||
float x = 0; | ||
for (int i = a.length() - 1; i >= 0; i--) | ||
x += (a[i] != b[i] ? 1 : 0); | ||
|
||
return x / a.length(); | ||
} | ||
|
||
float PhenotypeDistance(const GAGenome & c1, const GAGenome & c2) | ||
{ | ||
auto & a = (GABin2DecGenome &)c1; | ||
auto & b = (GABin2DecGenome &)c2; | ||
|
||
return fabs(a.phenotype(0) - b.phenotype(0)) / (MAX_VALUE - MIN_VALUE); | ||
} |
Oops, something went wrong.