-
Notifications
You must be signed in to change notification settings - Fork 0
/
Genome.h
95 lines (71 loc) · 2.77 KB
/
Genome.h
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
// Genome.h
// I. Ahmed
//
// This declares the Genome class, which represents a genome (neural
// network) in the NEAT algorithm.
#pragma once
#include <vector>
#include "Neuron_gene.h"
#include "Link_gene.h"
class Genome
{
public:
// ID.
int id;
// Input length.
int input_length = 0;
// Output length.
int output_length = 0;
// Fitness.
double fitness = 0;
// Neuron genes.
std::vector<Neuron_gene> neuron_genes;
// Link genes.
std::vector<Link_gene> link_genes;
// Constructor.
Genome() = default;
// Finds a neuron.
std::vector<Neuron_gene>::const_iterator find_neuron_const(const int id)
const;
std::vector<Neuron_gene>::iterator find_neuron(const int id);
// Finds a link.
std::vector<Link_gene>::const_iterator find_link_const(const int innov_id)
const;
std::vector<Link_gene>::iterator find_link(const int innov_id);
std::vector<Link_gene>::const_iterator find_link_const(
const Link_gene &link_gene) const;
// Determines if a neuron is in this genome.
bool contains_neuron(const int id) const;
bool contains_neuron(Neuron_gene &neuron_gene) const;
// Determines if a link is in this genome.
bool contains_link(const int innov_id) const;
bool contains_link(const Link_gene &link_gene) const;
// Determines if a link is in this genome and is enabled.
bool contains_enabled_link(const int innov_id) const;
// Sets the neurons' inputs and activations to 0.
void flush();
// Sorts link genes by the to neuron's depth.
void sort_link_genes();
// Calculates the output of the genome given an input.
void activate(const std::vector<double>& input);
// Returns the output of the genome.
std::vector<double> output();
// Randomizes link weights between a low value and a high value.
void randomize_weights(const double low, const double high);
// Compares the fitnesses of 2 genomes (a higher fitness is better).
// If the fitnesses are the same, compare the sizes (fewer links is better).
bool operator<(const Genome &right_val) const;
bool operator>(const Genome &right_val) const;
// Adds a link gene.
void add_link_gene(const Link_gene &link_gene);
// Adds a neuron gene.
void add_neuron_gene(const Neuron_gene &neuron_gene);
// Inherits a link gene from another genome.
void inherit_link_gene(const Genome &genome, const Link_gene &link_gene);
// Counts the number of links present in this genome that are not present
// in another genome. This means that excess links are included.
int links_not_present_in(const Genome &genome) const;
};
// Finds a genome.
std::vector<Genome>::const_iterator find_genome(
const std::vector<Genome> &genomes, const Genome &genome);