-
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 `exampleXX` to `examples/ex6.hpp` and `examples/ex8.hpp` to calculate the objective value and run the examples with seed and command line options as input parameters. Modify `test/GAExamplesTest.cpp` to add unit tests for examples 6 and 8 using the new `example6` and `example8` functions, and update existing unit tests to use the new functions. Remove main methods from `examples/ex6.C`, `examples/ex8.C`, `examples/ex1.C`, `examples/ex2.C`, `examples/ex3.C`, `examples/ex4.C`, `examples/ex7.C`, `examples/ex9.C`, and move their content to the corresponding `exampleXX` functions in the respective header files. Add functions `objective` and `exampleXX` to `examples/ex1.hpp`, `examples/ex2.hpp`, `examples/ex3.hpp`, `examples/ex4.hpp`, `examples/ex7.hpp`, `examples/ex9.hpp` to calculate the objective value and run the examples with seed and command line options as input parameters. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/s-martin/galib?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
Showing
52 changed files
with
2,026 additions
and
1,837 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
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.