summaryrefslogtreecommitdiffstats
path: root/ai/Layer.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2007-07-21 01:39:22 +0000
committerBen Boeckel <MathStuf@gmail.com>2007-07-21 01:39:22 +0000
commite94d9893b8753e72adb92b2c5eb203830ddf641c (patch)
treefe283d6ede1cfe1a1613742811fb5b34fb8db68c /ai/Layer.cpp
parent65cc463f1d91fe99acf1c4dd9bce7e0038593022 (diff)
downloadsigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.tar.gz
sigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.tar.xz
sigen-e94d9893b8753e72adb92b2c5eb203830ddf641c.zip
Moved to GPLv3 and Qt4, Changed String -> QString, other minor fixes
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@23 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'ai/Layer.cpp')
-rw-r--r--ai/Layer.cpp129
1 files changed, 74 insertions, 55 deletions
diff --git a/ai/Layer.cpp b/ai/Layer.cpp
index 5ba794a1..67d18d13 100644
--- a/ai/Layer.cpp
+++ b/ai/Layer.cpp
@@ -6,61 +6,80 @@
// Created: Thu May 5 15:55:21 2007
// Copyright: ©2007 Nerdy Productions
// Licence:
-// This program is free software; you can redistribute it and/or modify
+// 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
+// the Free Software Foundation, either version 3 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.
+// with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////
#include "Layer.h"
-PokeGen::NeuralNetwork::Layer::Layer(Layer *par, Layer *child, int num)
+PokeGen::NeuralNetwork::Layer::Layer(const Layer *par, const Layer *cld, const unsigned num) :
+ parent(par),
+ child(cld)
{
- parent = par;
- child = child;
- for (int i = 0; i < num; ++i)
- nodes.push_back(Node(this, i));
+ for (unsigned i = 0; i < num; ++i)
+ nodes.append(Node(this, i));
if (parent)
parent->RandomizeWeights();
}
+void PokeGen::NeuralNetwork::Layer::SaveData(QFile &fout) const
+{
+ fout << "-Layer\n";
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
+ nodes[i].SaveData(fout);
+}
+
+void PokeGen::NeuralNetwork::Layer::LoadData(const QFile &fin)
+{
+ QChar next;
+ nodes.clear();
+ fin >> next;
+ while (next == '+')
+ {
+ AddNode();
+ nodes[GetNumNodes() - 1].LoadData(fin.readLine());
+ fin >> next;
+ }
+}
+
void PokeGen::NeuralNetwork::Layer::AddNode()
{
- nodes.push_back(Node(this, GetNumNodes()));
+ nodes.append(Node(this, GetNumNodes()));
parent->UpdateBias();
}
-void PokeGen::NeuralNetwork::Layer::DeleteNode(int n)
+void PokeGen::NeuralNetwork::Layer::DeleteNode(const unsigned n)
{
- if ((0 < n) && (n < GetNumNodes()))
+ if (n < GetNumNodes())
{
nodes.erase(nodes.begin() + n);
parent->UpdateBias(n);
}
}
-void PokeGen::NeuralNetwork::Layer::UpdateBias(int n)
+void PokeGen::NeuralNetwork::Layer::UpdateBias(const unsigned n)
{
- if (n < 0)
- bias.push_back((abs(std::rand()) % 201) / 100.0);
+ if (n == UINT_MAX)
+ bias.append((abs(std::rand()) % 201) / 100.0);
else if (n < GetNumNodes())
bias.erase(bias.begin() + n);
else
return;
- for (int i = 0; i < GetNumNodes(); ++i)
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
nodes[i].UpdateWeights(n);
}
-int PokeGen::NeuralNetwork::Layer::GetNumNodes()
+unsigned PokeGen::NeuralNetwork::Layer::GetNumNodes() const
{
return nodes.size();
}
@@ -68,123 +87,123 @@ int PokeGen::NeuralNetwork::Layer::GetNumNodes()
void PokeGen::NeuralNetwork::Layer::RandomizeWeights()
{
bias.clear();
- for (int i = 0; i < child->GetNumNodes(); ++i)
- bias.push_back((abs(std::rand()) % 201) / 100.0);
- for (int i = 0; i < GetNumNodes(); ++i)
+ for (unsigned i = 0; i < child->GetNumNodes(); ++i)
+ bias.append((abs(std::rand()) % 201) / 100.0);
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
nodes[i].RandomizeWeights();
}
void PokeGen::NeuralNetwork::Layer::CalculateErrors()
{
- for (int i = 0; i < GetNumNodes(); ++i)
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
nodes[i].CalculateError();
}
void PokeGen::NeuralNetwork::Layer::AdjustWeights()
{
- for (int i = 0; i < GetNumNodes(); ++i)
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
nodes[i].AdjustWeights();
}
void PokeGen::NeuralNetwork::Layer::CalculateValues()
{
- for (int i = 0; i < GetNumNodes(); ++i)
+ for (unsigned i = 0; i < GetNumNodes(); ++i)
nodes[i].CalculateValue();
}
-void PokeGen::NeuralNetwork::Layer::SetInput(int n, double i)
+void PokeGen::NeuralNetwork::Layer::SetInput(const unsigned n, const double i)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetInput(i);
}
-void PokeGen::NeuralNetwork::Layer::SetDesired(int n, double o)
+void PokeGen::NeuralNetwork::Layer::SetDesired(const unsigned n, const double o)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetDesired(o);
}
-void PokeGen::NeuralNetwork::Layer::SetMomentum(int n, double m)
+void PokeGen::NeuralNetwork::Layer::SetMomentum(const unsigned n, const double m)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetMomentum(m);
}
-void PokeGen::NeuralNetwork::Layer::SetMomentumFactor(int n, double m)
+void PokeGen::NeuralNetwork::Layer::SetMomentumFactor(const unsigned n, const double m)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetMomentumFactor(m);
}
-void PokeGen::NeuralNetwork::Layer::SetLearnRate(int n, double l)
+void PokeGen::NeuralNetwork::Layer::SetLearnRate(const unsigned n, const double l)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetLearnRate(l);
}
-void PokeGen::NeuralNetwork::Layer::SetFunction(int n, int f)
+void PokeGen::NeuralNetwork::Layer::SetFunction(const unsigned n, const unsigned f)
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
nodes[n].SetFunction(f);
}
-double PokeGen::NeuralNetwork::Layer::GetOutput(int n)
+double PokeGen::NeuralNetwork::Layer::GetOutput(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetOutput();
return 0;
}
-double PokeGen::NeuralNetwork::Layer::GetError(int n)
+double PokeGen::NeuralNetwork::Layer::GetError(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetError();
return 0;
}
-double PokeGen::NeuralNetwork::Layer::GetMomentum(int n)
+double PokeGen::NeuralNetwork::Layer::GetMomentum(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetMomentum();
return 0;
}
-double PokeGen::NeuralNetwork::Layer::GetMomentumFactor(int n)
+double PokeGen::NeuralNetwork::Layer::GetMomentumFactor(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetMomentumFactor();
return 0;
}
-double PokeGen::NeuralNetwork::Layer::GetLearnRate(int n)
+double PokeGen::NeuralNetwork::Layer::GetLearnRate(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetLearnRate();
return 0;
}
-int PokeGen::NeuralNetwork::Layer::GetFunction(int n)
+unsigned PokeGen::NeuralNetwork::Layer::GetFunction(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetFunction();
- return -1;
+ return UINT_MAX;
}
-double PokeGen::NeuralNetwork::Layer::GetWeight(int n, int i)
+double PokeGen::NeuralNetwork::Layer::GetWeight(const unsigned n, const unsigned i) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return nodes[n].GetWeight(i);
return 0;
}
-void PokeGen::NeuralNetwork::Layer::ChangeBias(int n, double d)
+void PokeGen::NeuralNetwork::Layer::ChangeBias(const unsigned n, const double d)
{
bias[n] += d * GetError(n) * parent->GetBias(n);
}
-double PokeGen::NeuralNetwork::Layer::GetBias(int n)
+double PokeGen::NeuralNetwork::Layer::GetBias(const unsigned n) const
{
- if ((0 <= n) && (n <= GetNumNodes()))
+ if (n < GetNumNodes())
return bias[n];
return 0;
}