File: ex23.C

package info (click to toggle)
galib 2.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,216 kB
  • ctags: 3,153
  • sloc: cpp: 23,666; ansic: 520; makefile: 247; sh: 93
file content (126 lines) | stat: -rw-r--r-- 4,076 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ----------------------------------------------------------------------------
  ex23.C
  mbwall 5jan96
  Copyright (c) 1995-1996  Massachusetts Institute of Technology

 DESCRIPTION:
   This example shows how to use max/min feature of GAlib to maximize or 
minimize your objective functions.
---------------------------------------------------------------------------- */
#include <stdio.h>
#include <math.h>
#include <ga/ga.h>
#include <ga/std_stream.h>

#define cout STD_COUT
#define endl STD_ENDL
#define ofstream STD_OFSTREAM

#define INSTANTIATE_REAL_GENOME
#include <ga/GARealGenome.h>

#ifndef M_PI
#define M_PI            3.14159265358979323846
#endif

#define MIN_VALUE -2
#define MAX_VALUE 2
#define INC       0.005

float Objective(GAGenome &);
float Comparator(const GAGenome&, const GAGenome&);

int
main(int argc, char** argv)
{
  cout << "Example 23\n\n";
  cout << "This program tries to maximize or minimize, depending on the\n";
  cout << "command line argument that you give it.  Use the command-line\n";
  cout << "argument 'mm -1' to minimize (the default for this example), or\n";
  cout << "'mm 1' to maximize.  The objective function is a simple \n";
  cout << "sinusoidal.\n\n"; cout.flush();

// See if we've been given a seed to use (for testing purposes).  When you
// specify a random seed, the evolution will be exactly the same each time
// you use that seed number.

  for(int jj=1; jj<argc; jj++) {
    if(strcmp(argv[jj++],"seed") == 0) {
      GARandomSeed((unsigned int)atoi(argv[jj]));
    }
  }

  ofstream outfile;

  GARealAlleleSet alleles(MIN_VALUE, MAX_VALUE);
  GARealGenome genome(1, alleles, Objective);
  GASharing scale(Comparator);

  GASimpleGA ga(genome);
  ga.minimize();		// by default we want to minimize the objective
  ga.scaling(scale);		// set the scaling method to our sharing
  ga.populationSize(50);	// how many individuals in the population
  ga.nGenerations(25);		// number of generations to evolve
  ga.pMutation(0.001);		// likelihood of mutating new offspring
  ga.pCrossover(0.9);		// likelihood of crossing over parents
  ga.scoreFilename("bog.dat");	// name of file for scores
  ga.scoreFrequency(1);		// keep the scores of every generation
  ga.flushFrequency(10);	// specify how often to write the score to disk
  ga.selectScores(GAStatistics::AllScores);
  ga.parameters(argc, argv, gaTrue); // parse commands, complain if bogus args

  ga.initialize();

// dump the initial population to file

  cout << "printing initial population to file..." << endl;
  outfile.open("popi.dat", (STD_IOS_OUT | STD_IOS_TRUNC));
  for(int ii=0; ii<ga.population().size(); ii++){
    genome = ga.population().individual(ii);
    outfile << genome.gene(0) << "\t" << genome.score() << "\n";
  }
  outfile.close();

  while(!ga.done()) ga.step();

// dump the final population to file

  cout << "printing final population to file..." << endl;
  outfile.open("popf.dat", (STD_IOS_OUT | STD_IOS_TRUNC));
  for(int i=0; i<ga.population().size(); i++){
    genome = ga.population().individual(i);
    outfile << genome.gene(0) << "\t" << genome.score() << "\n";
  }
  outfile.close();

// dump the function to file so you can plot the population on it

  cout << "printing function to file..." << endl;
  outfile.open("sinusoid.dat", (STD_IOS_OUT | STD_IOS_TRUNC));
  for(float x=MIN_VALUE; x<=MAX_VALUE; x+=INC){
    outfile << genome.gene(0,x) << "\t" << genome.score() << "\n";
  }
  outfile.close();

  return 0;
}
 



// This objective function returns the sin of the value in the genome.
float
Objective(GAGenome& g){
  GARealGenome& genome = (GARealGenome &)g;
  return 1 + sin(genome.gene(0)*2*M_PI);
}

// The comparator returns a number in the interval [0,1] where 0 means that
// the two genomes are identical (zero diversity) and 1 means they are 
// completely different (maximum diversity).
float
Comparator(const GAGenome& g1, const GAGenome& g2) {
  GARealGenome& a = (GARealGenome &)g1;
  GARealGenome& b = (GARealGenome &)g2;
  return exp( -(a.gene(0) - b.gene(0)) * (a.gene(0) - b.gene(0)) / 1000.0);
}