summaryrefslogtreecommitdiffstats
path: root/ai/Layer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ai/Layer.cpp')
-rw-r--r--ai/Layer.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/ai/Layer.cpp b/ai/Layer.cpp
new file mode 100644
index 00000000..6f36be67
--- /dev/null
+++ b/ai/Layer.cpp
@@ -0,0 +1,163 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name: Layer.cpp
+// Purpose: A layer of a NeuralNet
+// Author: Ben Boeckel
+// Modified by: Ben Boeckel
+// Created: Thu May 5 15:55:21 2007
+// Copyright: ©2007 Ben Boeckel and Nerdy Productions
+// Licence:
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+/////////////////////////////////////////////////////////////////////////////
+
+#include "Layer.h"
+
+PokeGen::NeuralNetwork::Layer::Layer(Layer *par, Layer *child, int num)
+{
+ parent = par;
+ child = child;
+ for (int i = 0; i < num; ++i)
+ nodes.push_back(Node(this, i));
+ if (parent)
+ parent->RandomizeWeights();
+}
+
+int PokeGen::NeuralNetwork::Layer::GetNumNodes()
+{
+ return nodes.size();
+}
+
+void PokeGen::NeuralNetwork::Layer::RandomizeWeights()
+{
+ bias.clear();
+ for (int i = 0; i < child->GetNumNodes(); ++i)
+ bias.push_back((abs(rand())%201)/100.0);
+ for (int i = 0; i < GetNumNodes(); ++i)
+ nodes[i].RandomizeWeights();
+}
+
+void PokeGen::NeuralNetwork::Layer::CalculateErrors()
+{
+ for (int i = 0; i < GetNumNodes(); ++i)
+ nodes[i].CalculateError();
+}
+
+void PokeGen::NeuralNetwork::Layer::AdjustWeights()
+{
+ for (int i = 0; i < GetNumNodes(); ++i)
+ nodes[i].AdjustWeights();
+}
+
+void PokeGen::NeuralNetwork::Layer::CalculateValues()
+{
+ for (int i = 0; i < GetNumNodes(); ++i)
+ nodes[i].CalculateValue();
+}
+
+void PokeGen::NeuralNetwork::Layer::SetInput(int n, double i)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetInput(i);
+}
+
+void PokeGen::NeuralNetwork::Layer::SetDesired(int n, double o)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetDesired(o);
+}
+
+void PokeGen::NeuralNetwork::Layer::SetMomentum(int n, double m)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetMomentum(m);
+}
+
+void PokeGen::NeuralNetwork::Layer::SetMomentumFactor(int n, double m)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetMomentumFactor(m);
+}
+
+void PokeGen::NeuralNetwork::Layer::SetLearnRate(int n, double l)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetLearnRate(l);
+}
+
+void PokeGen::NeuralNetwork::Layer::SetFunction(int n, int f)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ nodes[n].SetFunction(f);
+}
+
+double PokeGen::NeuralNetwork::Layer::GetOutput(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetOutput();
+ return 0;
+}
+
+double PokeGen::NeuralNetwork::Layer::GetError(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetError();
+ return 0;
+}
+
+double PokeGen::NeuralNetwork::Layer::GetMomentum(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetMomentum();
+ return 0;
+}
+
+double PokeGen::NeuralNetwork::Layer::GetMomentumFactor(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetMomentumFactor();
+ return 0;
+}
+
+double PokeGen::NeuralNetwork::Layer::GetLearnRate(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetLearnRate();
+ return 0;
+}
+
+int PokeGen::NeuralNetwork::Layer::GetFunction(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetFunction();
+ return -1;
+}
+
+double PokeGen::NeuralNetwork::Layer::GetWeight(int n, int i)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return nodes[n].GetWeight(i);
+ return 0;
+}
+
+void PokeGen::NeuralNetwork::Layer::ChangeBias(int n, double d)
+{
+ bias[n] += d * GetError(n) * parent->GetBias(n);
+}
+
+double PokeGen::NeuralNetwork::Layer::GetBias(int n)
+{
+ if ((0 <= n) && (n <= GetNumNodes()))
+ return bias[n];
+ return 0;
+}