summaryrefslogtreecommitdiffstats
path: root/ai/Node.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ai/Node.cpp')
-rw-r--r--ai/Node.cpp196
1 files changed, 196 insertions, 0 deletions
diff --git a/ai/Node.cpp b/ai/Node.cpp
new file mode 100644
index 00000000..a6702b09
--- /dev/null
+++ b/ai/Node.cpp
@@ -0,0 +1,196 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name: Node.cpp
+// Purpose: A node in a Neural Network layer
+// Author: Ben Boeckel
+// Modified by: Ben Boeckel
+// Created: Sun Apr 15 16:56:23 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 "Node.h"
+
+PokeGen::NeuralNetwork::Node::Node(Layer *own, unsigned idx, double mom, double mfc, double lrn, int func)
+{
+ ownerLayer = own;
+ id = idx;
+ momentum = mom;
+ momentumFactor = mfc;
+ learnRate = lrn;
+ if ((func <= FUNC_NONE) || (FUNC_END <= func))
+ function = FUNC_SIGMOID;
+ else
+ function = func;
+}
+
+void PokeGen::NeuralNetwork::Node::SetInput(double i)
+{
+ input = i;
+}
+
+void PokeGen::NeuralNetwork::Node::SetDesired(double o)
+{
+ desired = o;
+}
+
+void PokeGen::NeuralNetwork::Node::SetMomentum(double m)
+{
+ momentum = m;
+}
+
+void PokeGen::NeuralNetwork::Node::SetMomentumFactor(double m)
+{
+ momentumFactor = m;
+}
+
+void PokeGen::NeuralNetwork::Node::SetLearnRate(double r)
+{
+ learnRate = r;
+}
+
+void PokeGen::NeuralNetwork::Node::SetFunction(int f)
+{
+ function = f;
+}
+
+void PokeGen::NeuralNetwork::Node::RandomizeWeights()
+{
+ weights.clear();
+ int i = 0;
+ for (; i < ownerLayer->child->GetNumNodes(); ++i)
+ weights.push_back((abs(rand())%201)/100.0);
+ weightChanges.clear();
+ weightChanges.resize(i - 1, 0);
+}
+
+double PokeGen::NeuralNetwork::Node::GetOutput()
+{
+ if (ownerLayer->parent == NULL)
+ return input;
+ return output;
+}
+
+double PokeGen::NeuralNetwork::Node::GetError()
+{
+ return error;
+}
+
+double PokeGen::NeuralNetwork::Node::GetMomentum()
+{
+ return momentum;
+}
+
+double PokeGen::NeuralNetwork::Node::GetMomentumFactor()
+{
+ return momentumFactor;
+}
+
+double PokeGen::NeuralNetwork::Node::GetLearnRate()
+{
+ return learnRate;
+}
+
+int PokeGen::NeuralNetwork::Node::GetFunction()
+{
+ return function;
+}
+
+double PokeGen::NeuralNetwork::Node::GetWeight(unsigned i)
+{
+ if ((0 <= i) && (i <= weights.size()))
+ return weights[i];
+ return 0;
+}
+
+void PokeGen::NeuralNetwork::Node::CalculateError()
+{
+ double error = 0;
+ if (ownerLayer->child->GetNumNodes())
+ {
+ for (int i = 0; i < ownerLayer->child->GetNumNodes(); ++i)
+ error += ownerLayer->child->GetError(i) * weights[i];
+ }
+ else
+ error = desired - output;
+ switch (function)
+ {
+ case FUNC_STEP:
+ error *= .01;
+ break;
+ case FUNC_SYMMETRIC_SIGMOID:
+ error *= 2;
+ case FUNC_SIGMOID:
+ error *= output * (1 - output);
+ break;
+ case FUNC_INVERSE_TANGENT:
+ error *= 1 / (1 + (output * output));
+ break;
+ case FUNC_HYPERBOLIC_TANGENT:
+ error *= 1 - tanh(output) * tanh(output);
+ break;
+ case FUNC_RADIAL:
+ error *= 2 * output * exp(output * output);
+ }
+}
+
+void PokeGen::NeuralNetwork::Node::AdjustWeights()
+{
+ double delta;
+ for (int i = 0; i < ownerLayer->child->GetNumNodes(); ++i)
+ {
+ delta = learnRate * ownerLayer->child->GetError(i) * output;
+ weights[i] += delta + momentum * weightChanges[i] + momentumFactor * weightChanges[i];
+ weightChanges[i] = delta;
+ }
+ for (int i = 0; i < ownerLayer->child->GetNumNodes(); ++i)
+ ownerLayer->child->ChangeBias(i, learnRate);
+}
+
+void PokeGen::NeuralNetwork::Node::CalculateValue()
+{
+ double val = 0;
+ for (int i = 0; i < ownerLayer->parent->GetNumNodes(); ++i)
+ val += ownerLayer->parent->GetOutput(i) * ownerLayer->parent->GetWeight(i, id);
+ val += ownerLayer->parent->GetBias(id);
+ switch (function)
+ {
+ case FUNC_IDENTITY:
+ output = val;
+ break;
+ case FUNC_STEP:
+ output = (val <= 0) ? 0 : 1;
+ break;
+ case FUNC_SIGMOID:
+ output = 1.0 / (1 + exp(val));
+ break;
+ case FUNC_SYMMETRIC_SIGMOID:
+ output = 2.0 / (1 + exp(val)) - 1;
+ break;
+ case FUNC_INVERSE_TANGENT:
+ output = atan(val);
+ break;
+ case FUNC_HYPERBOLIC_TANGENT:
+ output = tanh(val);
+ break;
+ case FUNC_RADIAL:
+ output = exp(val * val);
+ }
+}
+
+void PokeGen::NeuralNetwork::Node::DumpData(const std::string &f)
+{
+
+}