19 #include <core/darwin.h> 23 template <
class TRAITS>
25 using HiddenLayerGene =
typename TRAITS::HiddenLayerGene;
26 using OutputLayerGene =
typename TRAITS::OutputLayerGene;
29 vector<HiddenLayerGene> hidden_layers;
30 OutputLayerGene output_layer;
36 size_t prev_size = g_inputs;
37 for (
size_t size : g_config.hidden_layers) {
38 hidden_layers.emplace_back(prev_size, size);
41 output_layer = { prev_size, g_outputs };
44 void reset()
override {
45 darwin::Genotype::reset();
48 unique_ptr<darwin::Genotype> clone()
const override {
49 return make_unique<Genotype>(*this);
52 json save()
const override {
54 json_obj[
"hidden_layers"] = hidden_layers;
55 json_obj[
"output_layer"] = output_layer;
59 void load(
const json& json_obj)
override {
61 Genotype tmp_genotype;
62 tmp_genotype.hidden_layers =
63 json_obj.at(
"hidden_layers").get<vector<HiddenLayerGene>>();
64 tmp_genotype.output_layer = json_obj.at(
"output_layer");
67 size_t prev_size = g_inputs;
68 for (
const auto& layer : tmp_genotype.hidden_layers) {
69 if (prev_size + 1 != layer.w.rows)
71 prev_size = layer.w.cols;
75 if (prev_size + 1 != tmp_genotype.output_layer.w.rows)
77 if (tmp_genotype.output_layer.w.cols != g_outputs)
81 std::swap(*
this, tmp_genotype);
84 unique_ptr<darwin::Brain> grow()
const override;
86 void inherit(
const Genotype& parent1,
const Genotype& parent2,
float preference) {
88 const size_t layers_count = hidden_layers.size();
89 CHECK(layers_count == parent1.hidden_layers.size());
90 CHECK(layers_count == parent2.hidden_layers.size());
91 for (
size_t i = 0; i < layers_count; ++i) {
92 hidden_layers[i].crossover(
93 parent1.hidden_layers[i], parent2.hidden_layers[i], preference);
97 output_layer.crossover(parent1.output_layer, parent2.output_layer, preference);
101 for (
auto& layer : hidden_layers) {
102 layer.mutate(ann::g_config.mutation_std_dev);
104 output_layer.mutate(ann::g_config.mutation_std_dev);
107 void createPrimordialSeed() {
109 for (
auto& layer : hidden_layers) {
112 output_layer.randomize();
The base for exception types in the Darwin framework.
Definition: exception.h:27
Conventional Neuroevolution (CNE) populations.
Definition: brain.h:26
void reset(std::vector< T > &v)
Reset the values in a vector to 0.
Definition: ann_dynamic.h:32
The interface to the population-specific "genetic material", the Genotype
Definition: darwin.h:126