Darwin Neuroevolution Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
brain.h
1 // Copyright 2018 The Darwin Neuroevolution Framework Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "cne.h"
18 
19 #include <core/darwin.h>
20 #include <core/ann_dynamic.h>
21 
22 #include <cmath>
23 #include <limits>
24 using namespace std;
25 
26 namespace cne {
27 
28 template <class TRAITS>
29 class Brain : public darwin::Brain {
30  using Genotype = typename TRAITS::Genotype;
31  using HiddenLayer = typename TRAITS::HiddenLayer;
32  using OutputLayer = typename TRAITS::OutputLayer;
33 
34  public:
35  explicit Brain(const Genotype* genotype) : output_layer_(genotype->output_layer) {
36  CHECK(g_inputs > 0);
37  inputs_.resize(g_inputs);
38  for (const auto& layer : genotype->hidden_layers)
39  hidden_layers_.emplace_back(layer);
40  }
41 
42  void setInput(int index, float value) override { inputs_[index] = value; }
43 
44  float output(int index) const override { return output_layer_.values[index]; }
45 
46  void think() override {
47  if (g_config.normalize_input)
48  ann::activateLayer(inputs_);
49 
50  vector<float>* prev_layer = &inputs_;
51 
52  for (auto& layer : hidden_layers_) {
53  layer.evaluate(*prev_layer);
54 
55  if (TRAITS::kNormalizeHiddenLayers)
56  ann::activateLayer(layer.values);
57 
58  prev_layer = &layer.values;
59  }
60 
61  output_layer_.evaluate(*prev_layer);
62 
63  if (g_config.normalize_output)
64  ann::activateLayer(output_layer_.values);
65 
66  // finally, map any NaNs to +Inf
67  // (since NaNs are not valid output values)
68  for (float& output_value : output_layer_.values) {
69  if (isnan(output_value)) {
70  output_value = numeric_limits<float>::infinity();
71  }
72  }
73  }
74 
75  void resetState() override {
76  ann::reset(inputs_);
77  for (auto& layer : hidden_layers_)
78  layer.resetState();
79  output_layer_.resetState();
80  }
81 
82  private:
83  vector<float> inputs_;
84  vector<HiddenLayer> hidden_layers_;
85  OutputLayer output_layer_;
86 };
87 
88 } // namespace cne
The interface to the Phenotype
Definition: darwin.h:69
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
STL namespace.
void activateLayer(vector< float > &out)
Apply the activation function over a set of values.
Definition: ann_dynamic.h:50