Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Commit past changes to neural network and genetic learning test code

  • Loading branch information...
commit f651df048f6e12b6540f44ac51a44ddad280fdb6 1 parent cffad80
@jkivilin authored
Showing with 52 additions and 22 deletions.
  1. +3 −0  geneticalg.cpp
  2. +1 −1  geneticalg.h
  3. +48 −21 neuralnet.cpp
View
3  geneticalg.cpp
@@ -298,11 +298,14 @@ void CGeneticAlgorithm::reset_fitness_values(void)
m_best_fitness = 0;
m_worst_fitness = 9999999999.9;
m_average_fitness = 0;
+}
int CGeneticAlgorithm::fitness_sort(const CGenome *a, const CGenome *b)
{
/*
if (a->m_fitness < b->m_fitness)
+ return -1;
+ if (a->m_fitness > b->m_fitness)
return 1;
return 0;
*/
View
2  geneticalg.h
@@ -92,7 +92,7 @@ class CGeneticAlgorithm : public class_new_baseclass {
static int fitness_sort(const CGenome *a, const CGenome *b);
public:
- CGeneticAlgorithm(double mutation_rate, double crossover_rate, int crossover_interleave):
+ CGeneticAlgorithm(double mutation_rate, double crossover_rate, int crossover_interleave = 0):
m_population(NULL),
m_total_fitness(0),
m_best_fitness(0),
View
69 neuralnet.cpp
@@ -417,9 +417,9 @@ int main()
}
*/
-double f(double x/*, double y, double z*/) {
- //return (x + y + z + 1) * 10;
- return x*6;
+double f(double x, double y, double z) {
+ return x*x;
+ //return x*6;
}
static double scale_output(double max, double min, double output)
@@ -433,37 +433,60 @@ int main()
{
int i, j, k;
int rnd_idx;
- double input[1];
+ double input[3];
double output[1];
double scale[1] = {100};
double diff = 0;
fast_random_seed(time(0) ^ (long)&diff ^ (long)&main);
- CNeuralNet *nnet = new CNeuralNet(1, 1, 1, 5);
- CPopulation population = CPopulation(20, nnet->get_num_weights());
- CGeneticAlgorithm genalg(0.2, 0.7, 4);
+ CNeuralNet *nnet = new CNeuralNet(1, 1, 1, 4);
+ CPopulation population = CPopulation(25, nnet->get_num_weights());
+ CGeneticAlgorithm genalg(0.2, 0.7);
printf("ok\n");
fflush(stdout);
CGenome *best_genome;
- for (k = 0; k < 1000; k++) {
+ for (k = 0; k < 5000; k++) {
//printf("k: %i\n", k);
- for (i = 0; i < 1000; i++) {
- //printf(" i: %i\n", i);
- for (j = 0; j < population.get_size(); j++) {
- //printf(" j: %i\n", j);
- CGenome *genome = population.get_individual(j);
+ for (j = 0; j < population.get_size(); j++) {
+ double x, y, z, foutput;
+ CGenome *genome = population.get_individual(j);
- input[0] = get_random() * 4;
+ nnet->put_weights(genome->m_genes);
+
+ for (x = -0.5; x <= 4.5; x += 1) {
+ for (y = -0.5; y <= 4.5; y += 1) {
+ for (z = -0.5; z <= 4.5; z += 1) {
+ input[0] = x;
+ input[1] = y;
+ input[2] = z;
+
+ nnet->run(input, output, scale);
+
+ foutput = f(input[0], input[1], input[2]);
+ diff = output[0] - foutput;
+
+ genome->m_fitness += -(diff*diff)*0.0002;
+ }
+ }
+ }
+
+ //printf(" j: %i\n", j);
+ for (i = 0; i < 500; i++) {
+ //printf(" i: %i\n", i);
+ input[0] = get_random() * 5 - 0.5;
+ input[1] = get_random() * 5 - 0.5;
+ input[2] = get_random() * 5 - 0.5;
- nnet->put_weights(genome->m_genes);
nnet->run(input, output, scale);
- diff = fabs(output[0] - f(input[0]));
- genome->m_fitness += 1 - diff / fabs(output[0]);
+ foutput = f(input[0], input[1], input[2]);
+ diff = output[0] - foutput;
+
+ genome->m_fitness += -(diff*diff)*0.0001;
//diff = fabs(output[1] - sin(random_angle));
//if (diff < 1.0)
@@ -484,13 +507,15 @@ int main()
printf("generation: %i\n", genalg.get_generation());
printf("best fitness: %f\n", genome->m_fitness);
- input[0] = (int)(0.4 * 9) * 0.5;
+ input[0] = 1;
+ input[1] = 2;
+ input[2] = 3;
nnet->run(input, output, scale);
- printf("input: %f (=> %f)\n", input[0], f(input[0]));
+ printf("input: %f:%f:%f (=> %f)\n", input[0], input[1], input[2], f(input[0], input[1], input[2]));
printf("output: %f\n", output[0]);
}
- if (k+1 < 1000) {
+ if (k+1 < 5000) {
CPopulation new_pop = CPopulation(population.get_size(), nnet->get_num_weights());
genalg.epoch(population, new_pop);
population.free_mem();
@@ -510,8 +535,10 @@ int main()
for (i = 0; i < 9; i++) {
input[0] = i * 0.5;
+ input[1] = (9 - i) * 0.5;
+ input[2] = i % 4;
nnet->run(input, output, scale);
- printf("input: %f (=> %f)\n", input[0], f(input[0]));
+ printf("input: %f:%f:%f (=> %f)\n", input[0], input[1], input[2], f(input[0], input[1], input[2]));
printf("output: %f\n", output[0]);
}
Please sign in to comment.
Something went wrong with that request. Please try again.