");
+ AddLine("");
+ AddLine("");
+ }
+
+ private void GenerateComment(EncogProgramNode commentNode)
+ {
+ AddLine("// " + commentNode.Name);
+ }
+
+ private void GenerateConst(EncogProgramNode node)
+ {
+ var line = new StringBuilder();
+ line.Append("var ");
+ line.Append(node.Name);
+ line.Append(" = \"");
+ line.Append(node.Args[0].Value);
+ line.Append("\";");
+
+ AddLine(line.ToString());
+ }
+
+ private void GenerateForChildren(EncogTreeNode parent)
+ {
+ foreach (EncogProgramNode node in parent.Children)
+ {
+ GenerateNode(node);
+ }
+ }
+
+ private void GenerateFunction(EncogProgramNode node)
+ {
+ AddBreak();
+
+ var line = new StringBuilder();
+ line.Append("function ");
+ line.Append(node.Name);
+ line.Append("() {");
+ IndentLine(line.ToString());
+
+ GenerateForChildren(node);
+ UnIndentLine("}");
+ }
+
+ private void GenerateFunctionCall(EncogProgramNode node)
+ {
+ AddBreak();
+ var line = new StringBuilder();
+ if (node.Args[0].Value.ToString().Length > 0)
+ {
+ line.Append("var ");
+ line.Append(node.Args[1].Value);
+ line.Append(" = ");
+ }
+
+ line.Append(node.Name);
+ line.Append("();");
+ AddLine(line.ToString());
+ }
+
+ private void GenerateMainFunction(EncogProgramNode node)
+ {
+ AddBreak();
+ GenerateForChildren(node);
+ }
+
+ private void GenerateNode(EncogProgramNode node)
+ {
+ switch (node.Type)
+ {
+ case NodeType.Comment:
+ GenerateComment(node);
+ break;
+ case NodeType.Class:
+ GenerateClass(node);
+ break;
+ case NodeType.MainFunction:
+ GenerateMainFunction(node);
+ break;
+ case NodeType.Const:
+ GenerateConst(node);
+ break;
+ case NodeType.StaticFunction:
+ GenerateFunction(node);
+ break;
+ case NodeType.FunctionCall:
+ GenerateFunctionCall(node);
+ break;
+ case NodeType.CreateNetwork:
+ EmbedNetwork(node);
+ break;
+ case NodeType.InitArray:
+ GenerateArrayInit(node);
+ break;
+ case NodeType.EmbedTraining:
+ EmbedTraining(node);
+ break;
+ }
+ }
+
+ private String ToSingleLineArray(
+ IActivationFunction[] activationFunctions)
+ {
+ var result = new StringBuilder();
+ result.Append('[');
+ for (int i = 0; i < activationFunctions.Length; i++)
+ {
+ if (i > 0)
+ {
+ result.Append(',');
+ }
+
+ IActivationFunction af = activationFunctions[i];
+ if (af is ActivationSigmoid)
+ {
+ result.Append("ENCOG.ActivationSigmoid.create()");
+ }
+ else if (af is ActivationTANH)
+ {
+ result.Append("ENCOG.ActivationTANH.create()");
+ }
+ else if (af is ActivationLinear)
+ {
+ result.Append("ENCOG.ActivationLinear.create()");
+ }
+ else if (af is ActivationElliott)
+ {
+ result.Append("ENCOG.ActivationElliott.create()");
+ }
+ else if (af is ActivationElliott)
+ {
+ result.Append("ENCOG.ActivationElliott.create()");
+ }
+ else
+ {
+ throw new AnalystCodeGenerationError(
+ "Unsupported activatoin function for code generation: "
+ + af.GetType().Name);
+ }
+ }
+ result.Append(']');
+ return result.ToString();
+ }
+
+ private String ToSingleLineArray(double[] d)
+ {
+ var line = new StringBuilder();
+ line.Append("[");
+ for (int i = 0; i < d.Length; i++)
+ {
+ line.Append(CSVFormat.EgFormat.Format(d[i],
+ EncogFramework.DefaultPrecision));
+ if (i < (d.Length - 1))
+ {
+ line.Append(",");
+ }
+ }
+ line.Append("]");
+ return line.ToString();
+ }
+
+ private String ToSingleLineArray(int[] d)
+ {
+ var line = new StringBuilder();
+ line.Append("[");
+ for (int i = 0; i < d.Length; i++)
+ {
+ line.Append(d[i]);
+ if (i < (d.Length - 1))
+ {
+ line.Append(",");
+ }
+ }
+ line.Append("]");
+ return line.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Generators/Java/GenerateEncogJava.cs b/encog-core-cs/App/Generate/Generators/Java/GenerateEncogJava.cs
new file mode 100644
index 00000000..76c9babf
--- /dev/null
+++ b/encog-core-cs/App/Generate/Generators/Java/GenerateEncogJava.cs
@@ -0,0 +1,396 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.IO;
+using System.Text;
+using Encog.App.Generate.Program;
+using Encog.ML;
+using Encog.ML.Data;
+using Encog.Persist;
+using Encog.Util.CSV;
+using Encog.Util.Simple;
+
+namespace Encog.App.Generate.Generators.Java
+{
+ ///
+ /// Generate Java.
+ ///
+ public class GenerateEncogJava : AbstractGenerator
+ {
+ private bool embed;
+
+ private void EmbedNetwork(EncogProgramNode node)
+ {
+ AddBreak();
+
+ var methodFile = (FileInfo) node.Args[0].Value;
+
+ var method = (IMLMethod) EncogDirectoryPersistence
+ .LoadObject(methodFile);
+
+ if (!(method is IMLFactory))
+ {
+ throw new EncogError("Code generation not yet supported for: "
+ + method.GetType().Name);
+ }
+
+ var factoryMethod = (IMLFactory) method;
+
+ String methodName = factoryMethod.FactoryType;
+ String methodArchitecture = factoryMethod.FactoryArchitecture;
+
+ // header
+ AddInclude("org.encog.ml.MLMethod");
+ AddInclude("org.encog.persist.EncogDirectoryPersistence");
+
+ var line = new StringBuilder();
+ line.Append("public static MLMethod ");
+ line.Append(node.Name);
+ line.Append("() {");
+ IndentLine(line.ToString());
+
+ // create factory
+ line.Length = 0;
+ AddInclude("org.encog.ml.factory.MLMethodFactory");
+ line.Append("MLMethodFactory methodFactory = new MLMethodFactory();");
+ AddLine(line.ToString());
+
+ // factory create
+ line.Length = 0;
+ line.Append("MLMethod result = ");
+
+ line.Append("methodFactory.create(");
+ line.Append("\"");
+ line.Append(methodName);
+ line.Append("\"");
+ line.Append(",");
+ line.Append("\"");
+ line.Append(methodArchitecture);
+ line.Append("\"");
+ line.Append(", 0, 0);");
+ AddLine(line.ToString());
+
+ line.Length = 0;
+ AddInclude("org.encog.ml.MLEncodable");
+ line.Append("((MLEncodable)result).decodeFromArray(WEIGHTS);");
+ AddLine(line.ToString());
+
+ // return
+ AddLine("return result;");
+
+ UnIndentLine("}");
+ }
+
+ private void EmbedTraining(EncogProgramNode node)
+ {
+ var dataFile = (FileInfo) node.Args[0].Value;
+ IMLDataSet data = EncogUtility.LoadEGB2Memory(dataFile);
+
+ // generate the input data
+
+ IndentLine("public static final double[][] INPUT_DATA = {");
+ foreach (IMLDataPair pair in data)
+ {
+ IMLData item = pair.Input;
+
+ var line = new StringBuilder();
+
+ NumberList.ToList(CSVFormat.EgFormat, line, item);
+ line.Insert(0, "{ ");
+ line.Append(" },");
+ AddLine(line.ToString());
+ }
+ UnIndentLine("};");
+
+ AddBreak();
+
+ // generate the ideal data
+
+ IndentLine("public static final double[][] IDEAL_DATA = {");
+ foreach (IMLDataPair pair in data)
+ {
+ IMLData item = pair.Ideal;
+
+ var line = new StringBuilder();
+
+ NumberList.ToList(CSVFormat.EgFormat, line, item);
+ line.Insert(0, "{ ");
+ line.Append(" },");
+ AddLine(line.ToString());
+ }
+ UnIndentLine("};");
+ }
+
+ public override void Generate(EncogGenProgram program, bool shouldEmbed)
+ {
+ embed = shouldEmbed;
+ GenerateForChildren(program);
+ GenerateImports(program);
+ }
+
+ private void GenerateArrayInit(EncogProgramNode node)
+ {
+ var line = new StringBuilder();
+ line.Append("public static final double[] ");
+ line.Append(node.Name);
+ line.Append(" = {");
+ IndentLine(line.ToString());
+
+ var a = (double[]) node.Args[0].Value;
+
+ line.Length = 0;
+
+ int lineCount = 0;
+ for (int i = 0; i < a.Length; i++)
+ {
+ line.Append(CSVFormat.EgFormat.Format(a[i],
+ EncogFramework.DefaultPrecision));
+ if (i < (a.Length - 1))
+ {
+ line.Append(",");
+ }
+
+ lineCount++;
+ if (lineCount >= 10)
+ {
+ AddLine(line.ToString());
+ line.Length = 0;
+ lineCount = 0;
+ }
+ }
+
+ if (line.Length > 0)
+ {
+ AddLine(line.ToString());
+ line.Length = 0;
+ }
+
+ UnIndentLine("};");
+ }
+
+ private void GenerateClass(EncogProgramNode node)
+ {
+ AddBreak();
+ IndentLine("public class " + node.Name + " {");
+ GenerateForChildren(node);
+ UnIndentLine("}");
+ }
+
+ private void GenerateComment(EncogProgramNode commentNode)
+ {
+ AddLine("// " + commentNode.Name);
+ }
+
+ private void GenerateConst(EncogProgramNode node)
+ {
+ var line = new StringBuilder();
+ line.Append("public static final ");
+ line.Append(node.Args[1].Value);
+ line.Append(" ");
+ line.Append(node.Name);
+ line.Append(" = \"");
+ line.Append(node.Args[0].Value);
+ line.Append("\";");
+
+ AddLine(line.ToString());
+ }
+
+ private void GenerateCreateNetwork(EncogProgramNode node)
+ {
+ if (embed)
+ {
+ EmbedNetwork(node);
+ }
+ else
+ {
+ LinkNetwork(node);
+ }
+ }
+
+ private void GenerateEmbedTraining(EncogProgramNode node)
+ {
+ if (embed)
+ {
+ EmbedTraining(node);
+ }
+ }
+
+ private void GenerateForChildren(EncogTreeNode parent)
+ {
+ foreach (EncogProgramNode node in parent.Children)
+ {
+ GenerateNode(node);
+ }
+ }
+
+ private void GenerateFunction(EncogProgramNode node)
+ {
+ AddBreak();
+
+ var line = new StringBuilder();
+ line.Append("public static void ");
+ line.Append(node.Name);
+ line.Append("() {");
+ IndentLine(line.ToString());
+
+ GenerateForChildren(node);
+ UnIndentLine("}");
+ }
+
+ private void GenerateFunctionCall(EncogProgramNode node)
+ {
+ AddBreak();
+ var line = new StringBuilder();
+ if (node.Args[0].Value.ToString().Length > 0)
+ {
+ line.Append(node.Args[0].Value);
+ line.Append(" ");
+ line.Append(node.Args[1].Value);
+ line.Append(" = ");
+ }
+
+ line.Append(node.Name);
+ line.Append("();");
+ AddLine(line.ToString());
+ }
+
+ private void GenerateImports(EncogGenProgram program)
+ {
+ var imports = new StringBuilder();
+ foreach (String str in Includes)
+ {
+ imports.Append("import ");
+ imports.Append(str);
+ imports.Append(";\n");
+ }
+
+ imports.Append("\n");
+
+ AddToBeginning(imports.ToString());
+ }
+
+ private void GenerateLoadTraining(EncogProgramNode node)
+ {
+ AddBreak();
+
+ var methodFile = (FileInfo) node.Args[0].Value;
+
+ AddInclude("org.encog.ml.data.MLDataSet");
+ var line = new StringBuilder();
+ line.Append("public static MLDataSet createTraining() {");
+ IndentLine(line.ToString());
+
+ line.Length = 0;
+
+ if (embed)
+ {
+ AddInclude("org.encog.ml.data.basic.BasicMLDataSet");
+ line.Append("MLDataSet result = new BasicMLDataSet(INPUT_DATA,IDEAL_DATA);");
+ }
+ else
+ {
+ AddInclude("org.encog.util.simple.EncogUtility");
+ line.Append("MLDataSet result = EncogUtility.loadEGB2Memory(new File(\"");
+ line.Append(methodFile);
+ line.Append("\"));");
+ }
+
+ AddLine(line.ToString());
+
+ // return
+ AddLine("return result;");
+
+ UnIndentLine("}");
+ }
+
+ private void GenerateMainFunction(EncogProgramNode node)
+ {
+ AddBreak();
+ IndentLine("public static void main(String[] args) {");
+ GenerateForChildren(node);
+ UnIndentLine("}");
+ }
+
+ private void GenerateNode(EncogProgramNode node)
+ {
+ switch (node.Type)
+ {
+ case NodeType.Comment:
+ GenerateComment(node);
+ break;
+ case NodeType.Class:
+ GenerateClass(node);
+ break;
+ case NodeType.MainFunction:
+ GenerateMainFunction(node);
+ break;
+ case NodeType.Const:
+ GenerateConst(node);
+ break;
+ case NodeType.StaticFunction:
+ GenerateFunction(node);
+ break;
+ case NodeType.FunctionCall:
+ GenerateFunctionCall(node);
+ break;
+ case NodeType.CreateNetwork:
+ GenerateCreateNetwork(node);
+ break;
+ case NodeType.InitArray:
+ GenerateArrayInit(node);
+ break;
+ case NodeType.EmbedTraining:
+ GenerateEmbedTraining(node);
+ break;
+ case NodeType.LoadTraining:
+ GenerateLoadTraining(node);
+ break;
+ }
+ }
+
+ private void LinkNetwork(EncogProgramNode node)
+ {
+ AddBreak();
+
+ var methodFile = (FileInfo) node.Args[0].Value;
+
+ AddInclude("org.encog.ml.MLMethod");
+ var line = new StringBuilder();
+ line.Append("public static MLMethod ");
+ line.Append(node.Name);
+ line.Append("() {");
+ IndentLine(line.ToString());
+
+ line.Length = 0;
+ line.Append("MLMethod result = (MLMethod)EncogDirectoryPersistence.loadObject(new File(\"");
+ line.Append(methodFile);
+ line.Append("\"));");
+ AddLine(line.ToString());
+
+ // return
+ AddLine("return result;");
+
+ UnIndentLine("}");
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Generators/MQL4/GenerateMQL4.cs b/encog-core-cs/App/Generate/Generators/MQL4/GenerateMQL4.cs
new file mode 100644
index 00000000..05c778a7
--- /dev/null
+++ b/encog-core-cs/App/Generate/Generators/MQL4/GenerateMQL4.cs
@@ -0,0 +1,291 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.IO;
+using System.Text;
+using Encog.App.Analyst;
+using Encog.App.Analyst.Script;
+using Encog.App.Analyst.Script.Normalize;
+using Encog.App.Analyst.Script.Prop;
+using Encog.ML;
+using Encog.Neural.Flat;
+using Encog.Neural.Networks;
+using Encog.Persist;
+using Encog.Util;
+using Encog.Util.Arrayutil;
+
+namespace Encog.App.Generate.Generators.MQL4
+{
+ ///
+ /// Generate MQL4.
+ ///
+ public class GenerateMQL4 : AbstractTemplateGenerator
+ {
+ public override String NullArray
+ {
+ get { return "{-1}"; }
+ }
+
+ public override String TemplatePath
+ {
+ get { return "Encog.Resources.mt4.mql4"; }
+ }
+
+ private void ProcessCalc()
+ {
+ AnalystField firstOutputField = null;
+ int barsNeeded = Math.Abs(Analyst.DetermineMinTimeSlice());
+
+ int inputCount = Analyst.DetermineInputCount();
+ int outputCount = Analyst.DetermineOutputCount();
+
+ IndentLevel = 2;
+ AddLine("if( _inputCount>0 && Bars>=" + barsNeeded + " )");
+ AddLine("{");
+ IndentIn();
+ AddLine("double input[" + inputCount + "];");
+ AddLine("double output[" + outputCount + "];");
+
+ int idx = 0;
+ foreach (AnalystField field in Analyst.Script.Normalize
+ .NormalizedFields)
+ {
+ if (field.Input)
+ {
+ DataField df = Analyst.Script.FindDataField(field.Name);
+ String str;
+
+ switch (field.Action)
+ {
+ case NormalizationAction.PassThrough:
+ str = EngineArray.Replace(df.Source, "##", "pos+"
+ + (-field.TimeSlice));
+ AddLine("input[" + idx + "]=" + str + ";");
+ idx++;
+ break;
+ case NormalizationAction.Normalize:
+ str = EngineArray.Replace(df.Source, "##", "pos+"
+ + (-field.TimeSlice));
+ AddLine("input[" + idx + "]=Norm(" + str + ","
+ + field.NormalizedHigh + ","
+ + field.NormalizedLow + ","
+ + field.ActualHigh + ","
+ + field.ActualLow + ");");
+ idx++;
+ break;
+ case NormalizationAction.Ignore:
+ break;
+ default:
+ throw new AnalystCodeGenerationError(
+ "Can't generate Ninjascript code, unsupported normalizatoin action: "
+ + field.Action.ToString());
+ }
+ }
+ if (field.Output)
+ {
+ if (firstOutputField == null)
+ {
+ firstOutputField = field;
+ }
+ }
+ }
+
+ if (firstOutputField == null)
+ {
+ throw new AnalystCodeGenerationError(
+ "Could not find an output field.");
+ }
+
+ AddLine("Compute(input,output);");
+
+ AddLine("ExtMapBuffer1[pos] = DeNorm(output[0]" + ","
+ + firstOutputField.NormalizedHigh + ","
+ + firstOutputField.NormalizedLow + ","
+ + firstOutputField.ActualHigh + ","
+ + firstOutputField.ActualLow + ");");
+ IndentOut();
+ AddLine("}");
+ IndentLevel = 2;
+ }
+
+ private void ProcessHeaders()
+ {
+ DataField[] fields = Analyst.Script.Fields;
+
+ var line = new StringBuilder();
+ line.Append("FileWrite(iHandle");
+
+ foreach (DataField df in fields)
+ {
+ line.Append(",");
+ line.Append("\"");
+ line.Append(df.Name);
+ line.Append("\"");
+ }
+
+ line.Append(");");
+ AddLine(line.ToString());
+ }
+
+ private void ProcessMainBlock()
+ {
+ EncogAnalyst analyst = Analyst;
+
+ String processID = analyst.Script.Properties
+ .GetPropertyString(ScriptProperties.PROCESS_CONFIG_SOURCE_FILE);
+
+ String methodID = analyst
+ .Script
+ .Properties
+ .GetPropertyString(
+ ScriptProperties.MlConfigMachineLearningFile);
+
+ FileInfo methodFile = analyst.Script.ResolveFilename(methodID);
+
+ FileInfo processFile = analyst.Script.ResolveFilename(processID);
+
+ IMLMethod method = null;
+ int[] contextTargetOffset = null;
+ int[] contextTargetSize = null;
+ bool hasContext = false;
+ int inputCount = 0;
+ int[] layerContextCount = null;
+ int[] layerCounts = null;
+ int[] layerFeedCounts = null;
+ int[] layerIndex = null;
+ double[] layerOutput = null;
+ double[] layerSums = null;
+ int outputCount = 0;
+ int[] weightIndex = null;
+ double[] weights = null;
+ int neuronCount = 0;
+ int layerCount = 0;
+ int[] activation = null;
+ double[] p = null;
+
+ if (methodFile.Exists)
+ {
+ method = (IMLMethod) EncogDirectoryPersistence
+ .LoadObject(methodFile);
+ FlatNetwork flat = ((BasicNetwork) method).Flat;
+
+ contextTargetOffset = flat.ContextTargetOffset;
+ contextTargetSize = flat.ContextTargetSize;
+ hasContext = flat.HasContext;
+ inputCount = flat.InputCount;
+ layerContextCount = flat.LayerContextCount;
+ layerCounts = flat.LayerCounts;
+ layerFeedCounts = flat.LayerFeedCounts;
+ layerIndex = flat.LayerIndex;
+ layerOutput = flat.LayerOutput;
+ layerSums = flat.LayerSums;
+ outputCount = flat.OutputCount;
+ weightIndex = flat.WeightIndex;
+ weights = flat.Weights;
+ activation = CreateActivations(flat);
+ p = CreateParams(flat);
+ neuronCount = flat.LayerOutput.Length;
+ layerCount = flat.LayerCounts.Length;
+ }
+
+ IndentLevel = 2;
+ IndentIn();
+ AddNameValue("string EXPORT_FILENAME", "\"" + processFile
+ + "\"");
+
+ AddNameValue("int _neuronCount", neuronCount);
+ AddNameValue("int _layerCount", layerCount);
+ AddNameValue("int _contextTargetOffset[]", contextTargetOffset);
+ AddNameValue("int _contextTargetSize[]", contextTargetSize);
+ AddNameValue("bool _hasContext", hasContext ? "true" : "false");
+ AddNameValue("int _inputCount", inputCount);
+ AddNameValue("int _layerContextCount[]", layerContextCount);
+ AddNameValue("int _layerCounts[]", layerCounts);
+ AddNameValue("int _layerFeedCounts[]", layerFeedCounts);
+ AddNameValue("int _layerIndex[]", layerIndex);
+ AddNameValue("double _layerOutput[]", layerOutput);
+ AddNameValue("double _layerSums[]", layerSums);
+ AddNameValue("int _outputCount", outputCount);
+ AddNameValue("int _weightIndex[]", weightIndex);
+ AddNameValue("double _weights[]", weights);
+ AddNameValue("int _activation[]", activation);
+ AddNameValue("double _p[]", p);
+
+ IndentOut();
+ IndentLevel = 0;
+ }
+
+ private void ProcessObtain()
+ {
+ IndentLevel = 3;
+
+ AddLine("FileWrite(iHandle, when,");
+
+ DataField[] fields = Analyst.Script.Fields;
+ String lastLine = null;
+ foreach (DataField field in fields)
+ {
+ DataField df = field;
+ if (string.Compare(df.Name, "time", true) != 0
+ && string.Compare(df.Name, "prediction", true) != 0)
+ {
+ String str = EngineArray.Replace(df.Source, "##",
+ "pos");
+ if (lastLine != null)
+ {
+ AddLine(lastLine + ",");
+ }
+ lastLine = str;
+ }
+ }
+
+ if (lastLine != null)
+ {
+ AddLine(lastLine);
+ }
+ AddLine(");");
+ IndentLevel = 0;
+ }
+
+ public override void ProcessToken(String command)
+ {
+ if (string.Compare(command, "MAIN-BLOCK", true) == 0)
+ {
+ ProcessMainBlock();
+ }
+ else if (command.Equals("CALC"))
+ {
+ ProcessCalc();
+ }
+ else if (command.Equals("OBTAIN"))
+ {
+ ProcessObtain();
+ }
+ else if (command.Equals("HEADERS"))
+ {
+ ProcessHeaders();
+ }
+ IndentLevel = 0;
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Generators/NinjaScript/GenerateNinjaScript.cs b/encog-core-cs/App/Generate/Generators/NinjaScript/GenerateNinjaScript.cs
new file mode 100644
index 00000000..824bdc57
--- /dev/null
+++ b/encog-core-cs/App/Generate/Generators/NinjaScript/GenerateNinjaScript.cs
@@ -0,0 +1,278 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.IO;
+using System.Text;
+using Encog.App.Analyst;
+using Encog.App.Analyst.Script;
+using Encog.App.Analyst.Script.Normalize;
+using Encog.App.Analyst.Script.Prop;
+using Encog.ML;
+using Encog.Neural.Flat;
+using Encog.Neural.Networks;
+using Encog.Persist;
+using Encog.Util;
+using Encog.Util.Arrayutil;
+using Encog.Util.File;
+
+namespace Encog.App.Generate.Generators.NinjaScript
+{
+ ///
+ /// Generate NinjaScript.
+ ///
+ public class GenerateNinjaScript : AbstractTemplateGenerator
+ {
+ public override String TemplatePath
+ {
+ get { return "Encog.Resources.ninja.cs"; }
+ }
+
+ public override String NullArray
+ {
+ get { return "null"; }
+ }
+
+
+ private void AddCols()
+ {
+ var line = new StringBuilder();
+ line.Append("public readonly string[] ENCOG_COLS = {");
+
+ bool first = true;
+
+ foreach (DataField df in Analyst.Script.Fields)
+ {
+ if (string.Compare(df.Name, "time", true) != 0 && string.Compare(df.Name, "prediction", true) != 0)
+ {
+ if (!first)
+ {
+ line.Append(",");
+ }
+
+ line.Append("\"");
+ line.Append(df.Name);
+ line.Append("\"");
+ first = false;
+ }
+ }
+
+ line.Append("};");
+ AddLine(line.ToString());
+ }
+
+ private void ProcessMainBlock()
+ {
+ EncogAnalyst analyst = Analyst;
+
+ String processID = analyst.Script.Properties.GetPropertyString(ScriptProperties.PROCESS_CONFIG_SOURCE_FILE);
+
+ String methodID = analyst
+ .Script
+ .Properties
+ .GetPropertyString(ScriptProperties.MlConfigMachineLearningFile);
+
+ FileInfo methodFile = analyst.Script.ResolveFilename(methodID);
+
+ FileInfo processFile = analyst.Script.ResolveFilename(processID);
+
+ IMLMethod method = null;
+ int[] contextTargetOffset = null;
+ int[] contextTargetSize = null;
+ bool hasContext = false;
+ int inputCount = 0;
+ int[] layerContextCount = null;
+ int[] layerCounts = null;
+ int[] layerFeedCounts = null;
+ int[] layerIndex = null;
+ double[] layerOutput = null;
+ double[] layerSums = null;
+ int outputCount = 0;
+ int[] weightIndex = null;
+ double[] weights = null;
+ ;
+ int[] activation = null;
+ double[] p = null;
+
+ if (methodFile.Exists)
+ {
+ method = (IMLMethod) EncogDirectoryPersistence
+ .LoadObject(methodFile);
+ FlatNetwork flat = ((BasicNetwork) method).Flat;
+
+ contextTargetOffset = flat.ContextTargetOffset;
+ contextTargetSize = flat.ContextTargetSize;
+ hasContext = flat.HasContext;
+ inputCount = flat.InputCount;
+ layerContextCount = flat.LayerContextCount;
+ layerCounts = flat.LayerCounts;
+ layerFeedCounts = flat.LayerFeedCounts;
+ layerIndex = flat.LayerIndex;
+ layerOutput = flat.LayerOutput;
+ layerSums = flat.LayerSums;
+ outputCount = flat.OutputCount;
+ weightIndex = flat.WeightIndex;
+ weights = flat.Weights;
+ activation = CreateActivations(flat);
+ p = CreateParams(flat);
+ }
+
+ IndentLevel = 2;
+ AddLine("#region Encog Data");
+ IndentIn();
+ AddNameValue("public const string EXPORT_FILENAME", "\""
+ + FileUtil.ToStringLiteral(processFile) + "\"");
+ AddCols();
+
+ AddNameValue("private readonly int[] _contextTargetOffset",
+ contextTargetOffset);
+ AddNameValue("private readonly int[] _contextTargetSize",
+ contextTargetSize);
+ AddNameValue("private const bool _hasContext", hasContext
+ ? "true"
+ : "false");
+ AddNameValue("private const int _inputCount", inputCount);
+ AddNameValue("private readonly int[] _layerContextCount",
+ layerContextCount);
+ AddNameValue("private readonly int[] _layerCounts", layerCounts);
+ AddNameValue("private readonly int[] _layerFeedCounts",
+ layerFeedCounts);
+ AddNameValue("private readonly int[] _layerIndex", layerIndex);
+ AddNameValue("private readonly double[] _layerOutput", layerOutput);
+ AddNameValue("private readonly double[] _layerSums", layerSums);
+ AddNameValue("private const int _outputCount", outputCount);
+ AddNameValue("private readonly int[] _weightIndex", weightIndex);
+ AddNameValue("private readonly double[] _weights", weights);
+ AddNameValue("private readonly int[] _activation", activation);
+ AddNameValue("private readonly double[] _p", p);
+ IndentOut();
+ AddLine("#endregion");
+ IndentLevel = 0;
+ }
+
+ private void ProcessCalc()
+ {
+ AnalystField firstOutputField = null;
+ int barsNeeded = Math.Abs(Analyst.DetermineMinTimeSlice());
+
+ IndentLevel = 2;
+ AddLine("if( _inputCount>0 && CurrentBar>=" + barsNeeded + " )");
+ AddLine("{");
+ IndentIn();
+ AddLine("double[] input = new double[_inputCount];");
+ AddLine("double[] output = new double[_outputCount];");
+
+ int idx = 0;
+ foreach (AnalystField field in Analyst.Script.Normalize
+ .NormalizedFields)
+ {
+ if (field.Input)
+ {
+ String str;
+ DataField df = Analyst.Script
+ .FindDataField(field.Name);
+
+ switch (field.Action)
+ {
+ case NormalizationAction.PassThrough:
+ str = EngineArray.Replace(df.Source, "##", "" + (-field.TimeSlice));
+ AddLine("input[" + idx + "]=" + str + ";");
+ idx++;
+ break;
+ case NormalizationAction.Normalize:
+ str = EngineArray.Replace(df.Source, "##", "" + (-field.TimeSlice));
+ AddLine("input[" + idx + "]=Norm(" + str + ","
+ + field.NormalizedHigh + ","
+ + field.NormalizedLow + ","
+ + field.ActualHigh + ","
+ + field.ActualLow + ");");
+ idx++;
+ break;
+ case NormalizationAction.Ignore:
+ break;
+ default:
+ throw new AnalystCodeGenerationError(
+ "Can't generate Ninjascript code, unsupported normalizatoin action: "
+ + field.Action.ToString());
+ }
+ }
+ if (field.Output)
+ {
+ if (firstOutputField == null)
+ {
+ firstOutputField = field;
+ }
+ }
+ }
+
+ if (firstOutputField != null)
+ {
+ AddLine("Compute(input,output);");
+ AddLine("Output.Set(DeNorm(output[0]" + ","
+ + firstOutputField.NormalizedHigh + ","
+ + firstOutputField.NormalizedLow + ","
+ + firstOutputField.ActualHigh + ","
+ + firstOutputField.ActualLow + "));");
+ IndentOut();
+ }
+
+ AddLine("}");
+ IndentLevel = 2;
+ }
+
+ private void ProcessObtain()
+ {
+ IndentLevel = 3;
+ AddLine("double[] result = new double[ENCOG_COLS.Length];");
+
+ int idx = 0;
+ foreach (DataField df in Analyst.Script.Fields)
+ {
+ if (string.Compare(df.Name, "time", true) != 0 && string.Compare(df.Name, "prediction", true) != 0)
+ {
+ String str = EngineArray.Replace(df.Source, "##", "0");
+ AddLine("result[" + idx + "]=" + str + ";");
+ idx++;
+ }
+ }
+ AddLine("return result;");
+ IndentLevel = 0;
+ }
+
+ public override void ProcessToken(String command)
+ {
+ if (string.Compare(command, "MAIN-BLOCK", true) == 0)
+ {
+ ProcessMainBlock();
+ }
+ else if (command.Equals("CALC"))
+ {
+ ProcessCalc();
+ }
+ else if (command.Equals("OBTAIN"))
+ {
+ ProcessObtain();
+ }
+ IndentLevel = 0;
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/EncogArgType.cs b/encog-core-cs/App/Generate/Program/EncogArgType.cs
new file mode 100644
index 00000000..1d750579
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/EncogArgType.cs
@@ -0,0 +1,50 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// The type of argument.
+ ///
+ public enum EncogArgType
+ {
+ ///
+ /// A string type.
+ ///
+ String,
+
+ ///
+ /// A floating point type.
+ ///
+ Float,
+
+ ///
+ /// An int type.
+ ///
+ Int,
+
+ ///
+ /// An object type.
+ ///
+ ObjectType
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/EncogGenProgram.cs b/encog-core-cs/App/Generate/Program/EncogGenProgram.cs
new file mode 100644
index 00000000..67e1093b
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/EncogGenProgram.cs
@@ -0,0 +1,55 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// Holds a generated Encog program. A language specific generator will take this
+ /// and generate actual source code from it.
+ ///
+ public class EncogGenProgram : EncogTreeNode
+ {
+ ///
+ /// Construct the program.
+ ///
+ public EncogGenProgram()
+ : base(null, null)
+ {
+ Program = this;
+ }
+
+ ///
+ /// Create a new class.
+ ///
+ /// The class name.
+ /// The newly created class.
+ public EncogProgramNode CreateClass(String className)
+ {
+ var node = new EncogProgramNode(this, this,
+ NodeType.Class, className);
+ Children.Add(node);
+ return node;
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/EncogProgramArg.cs b/encog-core-cs/App/Generate/Program/EncogProgramArg.cs
new file mode 100644
index 00000000..d761f521
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/EncogProgramArg.cs
@@ -0,0 +1,105 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// A function argument for Encog created code.
+ ///
+ public class EncogProgramArg
+ {
+ ///
+ /// The type of this argument.
+ ///
+ private readonly EncogArgType type;
+
+ ///
+ /// The value of this argument.
+ ///
+ private readonly Object value;
+
+ ///
+ /// Construct the argument. Default to float type.
+ ///
+ /// The argument value.
+ public EncogProgramArg(double value)
+ : this(EncogArgType.Float, "" + value)
+ {
+ }
+
+ ///
+ /// Construct the argument.
+ ///
+ /// The type of argument.
+ /// The value of the argument.
+ public EncogProgramArg(EncogArgType type, Object value)
+ {
+ this.type = type;
+ this.value = value;
+ }
+
+ ///
+ /// Construct a floating point arguement from an integer.
+ ///
+ /// The value.
+ public EncogProgramArg(int value)
+ : this(EncogArgType.Float, "" + value)
+ {
+ }
+
+ ///
+ /// Construct using an object.
+ ///
+ /// The argument value.
+ public EncogProgramArg(Object argValue)
+ : this(EncogArgType.ObjectType, argValue)
+ {
+ }
+
+ ///
+ /// Construct a string argument.
+ ///
+ /// The string value.
+ public EncogProgramArg(String value)
+ : this(EncogArgType.String, value)
+ {
+ }
+
+ ///
+ /// The type of argument.
+ ///
+ public EncogArgType Type
+ {
+ get { return type; }
+ }
+
+ ///
+ /// The value.
+ ///
+ public Object Value
+ {
+ get { return value; }
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/EncogProgramNode.cs b/encog-core-cs/App/Generate/Program/EncogProgramNode.cs
new file mode 100644
index 00000000..61c8942d
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/EncogProgramNode.cs
@@ -0,0 +1,265 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// A node that holds a program.
+ ///
+ public class EncogProgramNode : EncogTreeNode
+ {
+ ///
+ /// The argements to the program.
+ ///
+ private readonly IList args = new List();
+
+ ///
+ /// The name od this node.
+ ///
+ private readonly String name;
+
+ ///
+ /// The type of node that this is.
+ ///
+ private readonly NodeType type;
+
+ ///
+ /// Construct the program node.
+ ///
+ /// THe program.
+ /// The parent.
+ /// The node type.
+ /// The name of the node.
+ public EncogProgramNode(EncogGenProgram theProgram,
+ EncogTreeNode theParent, NodeType theNodeType,
+ String theName)
+ : base(theProgram, theParent)
+ {
+ type = theNodeType;
+ name = theName;
+ }
+
+ ///
+ /// The args.
+ ///
+ public IList Args
+ {
+ get { return args; }
+ }
+
+ ///
+ /// The name.
+ ///
+ public String Name
+ {
+ get { return name; }
+ }
+
+ ///
+ /// The type.
+ ///
+ public NodeType Type
+ {
+ get { return type; }
+ }
+
+ ///
+ /// Add a double argument.
+ ///
+ /// The argument value.
+ public void AddArg(double argValue)
+ {
+ var arg = new EncogProgramArg(argValue);
+ args.Add(arg);
+ }
+
+ ///
+ /// Add an int argument.
+ ///
+ /// The argument value.
+ public void AddArg(int argValue)
+ {
+ var arg = new EncogProgramArg(argValue);
+ args.Add(arg);
+ }
+
+ ///
+ /// Add an object argument.
+ ///
+ /// The argument value.
+ public void AddArg(Object argValue)
+ {
+ var arg = new EncogProgramArg(argValue);
+ args.Add(arg);
+ }
+
+ ///
+ /// Add a string argument.
+ ///
+ /// The argument value.
+ public void AddArg(string argValue)
+ {
+ var arg = new EncogProgramArg(argValue);
+ args.Add(arg);
+ }
+
+ ///
+ /// Create an array.
+ ///
+ /// THe name of the array.
+ /// The value to init the array to.
+ /// The newly creatred array.
+ public EncogProgramNode CreateArray(string name, double[] a)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.InitArray, name);
+ node.AddArg(a);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Create a function.
+ ///
+ /// The name of the function.
+ /// The newly created function.
+ public EncogProgramNode CreateFunction(string theName)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.StaticFunction, theName);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Create a function call.
+ ///
+ /// The function to call.
+ /// The type returned.
+ /// The value to assigne the function call to.
+ /// The newly created function call.
+ public EncogProgramNode CreateFunctionCall(EncogProgramNode funct,
+ String returnType, String returnVariable)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.FunctionCall, funct.Name);
+ node.AddArg(returnType);
+ node.AddArg(returnVariable);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Create a function call.
+ ///
+ /// The name of the function to call.
+ /// The return type.
+ /// The variable to assign the function to.
+ /// The newly created function call.
+ public EncogProgramNode CreateFunctionCall(string name,
+ string returnType, string returnVariable)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.FunctionCall, name);
+ node.AddArg(returnType);
+ node.AddArg(returnVariable);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Create a new main function.
+ ///
+ /// The newly created main function.
+ public EncogProgramNode CreateMainFunction()
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.MainFunction, null);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Create a new network function.
+ ///
+ /// The name of the network function.
+ /// The method to call.
+ /// The newly created network function.
+ public EncogProgramNode CreateNetworkFunction(string name,
+ FileInfo method)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.CreateNetwork, name);
+ node.AddArg(method);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Define a const.
+ ///
+ /// The type of const.
+ /// The name of the const.
+ /// The value of the const.
+ public void DefineConst(EncogArgType type, string name,
+ string value)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.Const, name);
+ node.AddArg(value);
+ node.AddArg(type.ToString());
+ Children.Add(node);
+ }
+
+ ///
+ /// Embed training data.
+ ///
+ /// The training data to embed.
+ /// The newly created embeded training data.
+ public EncogProgramNode EmbedTraining(FileInfo data)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.EmbedTraining, "");
+ node.AddArg(data);
+ Children.Add(node);
+ return node;
+ }
+
+ ///
+ /// Load the training data.
+ ///
+ /// The data.
+ /// The newly created data load.
+ public EncogProgramNode GenerateLoadTraining(FileInfo data)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.LoadTraining, "");
+ node.AddArg(data);
+ Children.Add(node);
+ return node;
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/EncogTreeNode.cs b/encog-core-cs/App/Generate/Program/EncogTreeNode.cs
new file mode 100644
index 00000000..5fcac71e
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/EncogTreeNode.cs
@@ -0,0 +1,86 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// A tree node that represents code to be generated.
+ ///
+ public class EncogTreeNode
+ {
+ ///
+ /// The child nodes.
+ ///
+ private readonly IList children = new List();
+
+ ///
+ /// The parent node.
+ ///
+ private readonly EncogTreeNode parent;
+
+ ///
+ /// Construct a tree node.
+ ///
+ /// The program.
+ /// The parent.
+ public EncogTreeNode(EncogGenProgram theProgram,
+ EncogTreeNode theParent)
+ {
+ Program = theProgram;
+ parent = theParent;
+ }
+
+ ///
+ /// The program that this node belogs to.
+ ///
+ public EncogGenProgram Program { get; set; }
+
+ ///
+ /// The children.
+ ///
+ public IList Children
+ {
+ get { return children; }
+ }
+
+ ///
+ /// The parent.
+ ///
+ public EncogTreeNode Parent
+ {
+ get { return parent; }
+ }
+
+ ///
+ /// Add a comment.
+ ///
+ /// The comment.
+ public void AddComment(string str)
+ {
+ var node = new EncogProgramNode(Program, this,
+ NodeType.Comment, str);
+ children.Add(node);
+ }
+ }
+}
diff --git a/encog-core-cs/App/Generate/Program/NodeType.cs b/encog-core-cs/App/Generate/Program/NodeType.cs
new file mode 100644
index 00000000..43f083a4
--- /dev/null
+++ b/encog-core-cs/App/Generate/Program/NodeType.cs
@@ -0,0 +1,95 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.App.Generate.Program
+{
+ ///
+ /// The type of node.
+ ///
+ public enum NodeType
+ {
+ ///
+ /// A comment.
+ ///
+ Comment,
+
+ ///
+ /// The main function.
+ ///
+ MainFunction,
+
+ ///
+ /// A function declaration.
+ ///
+ Function,
+
+ ///
+ /// A class declaration.
+ ///
+ Class,
+
+ ///
+ /// A for loop.
+ ///
+ ForLoop,
+
+ ///
+ /// A while loop.
+ ///
+ WhileLoop,
+
+ ///
+ /// A const value.
+ ///
+ Const,
+
+ ///
+ /// A static function.
+ ///
+ StaticFunction,
+
+ ///
+ /// A function call.
+ ///
+ FunctionCall,
+
+ ///
+ /// A network creation.
+ ///
+ CreateNetwork,
+
+ ///
+ /// Init an array.
+ ///
+ InitArray,
+
+ ///
+ /// Embedded training.
+ ///
+ EmbedTraining,
+
+ ///
+ /// Load training.
+ ///
+ LoadTraining
+ }
+}
diff --git a/encog-core-cs/App/Generate/TargetLanguage.cs b/encog-core-cs/App/Generate/TargetLanguage.cs
new file mode 100644
index 00000000..9d7430c6
--- /dev/null
+++ b/encog-core-cs/App/Generate/TargetLanguage.cs
@@ -0,0 +1,60 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.App.Generate
+{
+ ///
+ /// Specifies the target language for Encog code generation.
+ ///
+ public enum TargetLanguage
+ {
+ ///
+ /// No code generation.
+ ///
+ NoGeneration,
+
+ ///
+ /// Generate using Java.
+ ///
+ Java,
+
+ ///
+ /// Generate using Javascript.
+ ///
+ JavaScript,
+
+ ///
+ /// Generate using C#.
+ ///
+ CSharp,
+
+ ///
+ /// Generate for MetaTrader 4 using MQL4.
+ ///
+ MQL4,
+
+ ///
+ /// Generate for NinjaTrader 7 using NinjaScript.
+ ///
+ NinjaScript
+ }
+}
diff --git a/encog-core-cs/App/Quant/Indicators/Indicator.cs b/encog-core-cs/App/Quant/Indicators/Indicator.cs
index 425ebf67..b80a9083 100644
--- a/encog-core-cs/App/Quant/Indicators/Indicator.cs
+++ b/encog-core-cs/App/Quant/Indicators/Indicator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -27,15 +27,13 @@
namespace Encog.App.Quant.Indicators
{
///
- /// An indicator, used by Encog.
+ /// An indicator, used by Encog.
///
- ///
public abstract class Indicator : BaseCachedColumn
{
///
- /// Construct the indicator.
+ /// Construct the indicator.
///
- ///
/// The indicator name.
/// Is this indicator used to predict?
/// Is this indicator what we are trying to predict.
@@ -46,25 +44,19 @@ public Indicator(String name, bool input,
/// the beginningIndex to set
- public int BeginningIndex {
- get;
- set; }
+ public int BeginningIndex { get; set; }
/// the endingIndex to set.
- public int EndingIndex {
- get;
- set; }
+ public int EndingIndex { get; set; }
/// The number of periods this indicator is for.
- public abstract int Periods {
- get; }
+ public abstract int Periods { get; }
///
- /// Calculate this indicator.
+ /// Calculate this indicator.
///
- ///
/// The data available to this indicator.
/// The length of data to use.
public abstract void Calculate(IDictionary data,
@@ -72,9 +64,8 @@ public abstract void Calculate(IDictionary data,
///
- /// Require a specific type of underlying data.
+ /// Require a specific type of underlying data.
///
- ///
/// The data available.
/// The type of data we are looking for.
public void Require(IDictionary theData,
diff --git a/encog-core-cs/App/Quant/Indicators/MovingAverage.cs b/encog-core-cs/App/Quant/Indicators/MovingAverage.cs
index 2a960adc..add9ab6e 100644
--- a/encog-core-cs/App/Quant/Indicators/MovingAverage.cs
+++ b/encog-core-cs/App/Quant/Indicators/MovingAverage.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -27,27 +27,23 @@
namespace Encog.App.Quant.Indicators
{
///
- /// A simple moving average.
+ /// A simple moving average.
///
- ///
public class MovingAverage : Indicator
{
///
- /// The name of this indicator.
+ /// The name of this indicator.
///
- ///
public const String NAME = "MovAvg";
///
- /// The number of periods in this indicator.
+ /// The number of periods in this indicator.
///
- ///
private readonly int periods;
///
- /// Construct this object.
+ /// Construct this object.
///
- ///
/// The number of periods in this indicator.
/// True, if this indicator is predicted.
public MovingAverage(int thePeriods, bool output) : base(NAME, false, output)
@@ -64,9 +60,8 @@ public override int Periods
}
///
- /// Calculate this indicator.
+ /// Calculate this indicator.
///
- ///
/// The data to use.
/// The length to calculate over.
public override sealed void Calculate(IDictionary data,
diff --git a/encog-core-cs/App/Quant/Indicators/Predictive/BestClose.cs b/encog-core-cs/App/Quant/Indicators/Predictive/BestClose.cs
index b2b1256e..709f4a36 100644
--- a/encog-core-cs/App/Quant/Indicators/Predictive/BestClose.cs
+++ b/encog-core-cs/App/Quant/Indicators/Predictive/BestClose.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -27,27 +27,23 @@
namespace Encog.App.Quant.Indicators.Predictive
{
///
- /// Get the best close.
+ /// Get the best close.
///
- ///
public class BestClose : Indicator
{
///
- /// The name of this indicator.
+ /// The name of this indicator.
///
- ///
public const String NAME = "PredictBestClose";
///
- /// The number of periods this indicator is for.
+ /// The number of periods this indicator is for.
///
- ///
private readonly int periods;
///
- /// Construct the object.
+ /// Construct the object.
///
- ///
/// The number of periods.
/// True, if this indicator is to be predicted.
public BestClose(int thePeriods, bool output) : base(NAME, false, output)
@@ -64,9 +60,8 @@ public override int Periods
}
///
- /// Calculate the indicator.
+ /// Calculate the indicator.
///
- ///
/// The data available to the indicator.
/// The length available to the indicator.
public override sealed void Calculate(IDictionary data,
diff --git a/encog-core-cs/App/Quant/Indicators/Predictive/BestReturn.cs b/encog-core-cs/App/Quant/Indicators/Predictive/BestReturn.cs
index 789171b0..daf141d4 100644
--- a/encog-core-cs/App/Quant/Indicators/Predictive/BestReturn.cs
+++ b/encog-core-cs/App/Quant/Indicators/Predictive/BestReturn.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -27,27 +27,23 @@
namespace Encog.App.Quant.Indicators.Predictive
{
///
- /// Get the best return.
+ /// Get the best return.
///
- ///
public class BestReturn : Indicator
{
///
- /// The name of this indicator.
+ /// The name of this indicator.
///
- ///
public const String NAME = "PredictBestReturn";
///
- /// The number of periods this indicator is for.
+ /// The number of periods this indicator is for.
///
- ///
private readonly int periods;
///
- /// Construct the object.
+ /// Construct the object.
///
- ///
/// The number of periods.
/// True, if this indicator is to be predicted.
public BestReturn(int thePeriods, bool output) : base(NAME, false, output)
@@ -64,9 +60,8 @@ public override int Periods
}
///
- /// Calculate the indicator.
+ /// Calculate the indicator.
///
- ///
/// The data available to the indicator.
/// The length of the data to calculate.
public override sealed void Calculate(IDictionary data,
diff --git a/encog-core-cs/App/Quant/Indicators/ProcessIndicators.cs b/encog-core-cs/App/Quant/Indicators/ProcessIndicators.cs
index 2227bc04..88737c90 100644
--- a/encog-core-cs/App/Quant/Indicators/ProcessIndicators.cs
+++ b/encog-core-cs/App/Quant/Indicators/ProcessIndicators.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -29,9 +29,8 @@
namespace Encog.App.Quant.Indicators
{
///
- /// Process indicators and generate output.
+ /// Process indicators and generate output.
///
- ///
public class ProcessIndicators : BasicCachedFile
{
/// Get the beginning index.
@@ -78,9 +77,8 @@ private int EndingIndex
}
///
- /// Allocate storage.
+ /// Allocate storage.
///
- ///
private void AllocateStorage()
{
foreach (BaseCachedColumn column in Columns)
@@ -90,9 +88,8 @@ private void AllocateStorage()
}
///
- /// Calculate the indicators.
+ /// Calculate the indicators.
///
- ///
private void CalculateIndicators()
{
foreach (BaseCachedColumn column in Columns)
@@ -110,9 +107,8 @@ private void CalculateIndicators()
///
- /// Process and write the specified output file.
+ /// Process and write the specified output file.
///
- ///
/// The output file.
public void Process(FileInfo output)
{
@@ -125,9 +121,8 @@ public void Process(FileInfo output)
}
///
- /// Read the CSV file.
+ /// Read the CSV file.
///
- ///
private void ReadFile()
{
ReadCSV csv = null;
@@ -170,9 +165,8 @@ private void ReadFile()
}
///
- /// Rename a column.
+ /// Rename a column.
///
- ///
/// The column index.
/// The new name.
public void RenameColumn(int index, String newName)
@@ -183,9 +177,8 @@ public void RenameColumn(int index, String newName)
}
///
- /// Write the CSV.
+ /// Write the CSV.
///
- ///
/// The target filename.
private void WriteCSV(FileInfo filename)
{
diff --git a/encog-core-cs/App/Quant/Loader/LoaderError.cs b/encog-core-cs/App/Quant/Loader/LoaderError.cs
index 91509bd5..90ab1ab1 100644
--- a/encog-core-cs/App/Quant/Loader/LoaderError.cs
+++ b/encog-core-cs/App/Quant/Loader/LoaderError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -25,26 +25,22 @@
namespace Encog.App.Quant.Loader
{
///
- /// Used to represent any error that occurs in the loader part of Encog.
+ /// Used to represent any error that occurs in the loader part of Encog.
///
- ///
[Serializable]
public class LoaderError : QuantError
{
-
///
- /// Construct a message exception.
+ /// Construct a message exception.
///
- ///
/// The exception message.
public LoaderError(String msg) : base(msg)
{
}
///
- /// Construct an exception that holds another exception.
+ /// Construct an exception that holds another exception.
///
- ///
/// The other exception.
public LoaderError(Exception t) : base(t)
{
diff --git a/encog-core-cs/App/Quant/Loader/MarketLoader.cs b/encog-core-cs/App/Quant/Loader/MarketLoader.cs
index bfcaf29a..de4dd6b3 100644
--- a/encog-core-cs/App/Quant/Loader/MarketLoader.cs
+++ b/encog-core-cs/App/Quant/Loader/MarketLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,9 +23,8 @@
namespace Encog.App.Quant.Loader
{
///
- /// Common interface for market loaders.
+ /// Common interface for market loaders.
///
- ///
public interface MarketLoader : QuantTask
{
}
diff --git a/encog-core-cs/App/Quant/Loader/OpenQuant/Bar.cs b/encog-core-cs/App/Quant/Loader/OpenQuant/Bar.cs
index 0f07b60e..855308af 100644
--- a/encog-core-cs/App/Quant/Loader/OpenQuant/Bar.cs
+++ b/encog-core-cs/App/Quant/Loader/OpenQuant/Bar.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -22,83 +22,89 @@
//
using System;
using System.Collections;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Drawing;
-using System.Linq;
using System.Runtime.CompilerServices;
-using System.Text;
namespace Encog.App.Quant.Loader.OpenQuant.Data
{
///
- /// This holds the various data used by Openquant
- /// Bars, and various custom series.
+ /// This holds the various data used by Openquant
+ /// Bars, and various custom series.
///
public class Data
{
+ ///
+ /// Different data , a bar holds
+ ///
+ public enum BarData
+ {
+ Close,
+ Open,
+ High,
+ Low,
+ Median,
+ Typical,
+ Weighted,
+ Volume,
+ OpenInt
+ }
- [Serializable]
- public class Bar : ICloneable
+ ///
+ /// Different possible prices on a bar.
+ ///
+ public enum BarPrice
{
- public DateTime DateTime { get; set; }
- public double Close { get; set; }
- public double Open { get; set; }
- public double High { get; set; }
- public double Low { get; set; }
- public DateTime BeginTime { get; set; }
- protected DateTime EndTime { get; set; }//end time for this bar.
- public TimeSpan Duration { get; set; }
- // Fields
- protected BarType barType { get; set; }
- protected DateTime beginTime { get; set; } //Begin time for this bar
- protected Color color { get; set; }//Color the bar should be drawed
- protected bool IsComplete { get; set; } // is the bar complete.
- protected long OpenInt { get; set; } // open interests on othis bar.
- protected byte ProviderId { get; set; } // provider for this bar (a byte , e.g Simulated executions 1.
- protected long Size { get; set; } // the size : Lenght in seconds of this bar.
- protected long Volume { get; set; } // the volume of this bar.
- protected DateTimeKind DateKind { get; set; } //Bar kind.
- protected double Weighted { get; set; }
- protected double Median { get; set; }
- protected double Typical { get; set;}
+ High,
+ Low,
+ Open,
+ Close,
+ Median,
+ Typical,
+ Weighted
+ }
+ public enum BarSlycing
+ {
+ Normal,
+ Equally
+ }
+ ///
+ /// The different bar types
+ ///
+ public enum BarType : byte
+ {
///
- /// Gets the last price for a bar price option
+ /// Dynamic types.
///
- /// The option.
- ///
- [MethodImpl(MethodImplOptions.NoInlining)]
- public double GetPrice(BarPrice option)
- {
- switch (option)
- {
- case BarPrice.High:
- return this.High;
-
- case BarPrice.Low:
- return this.Low;
+ Dynamic = 5,
- case BarPrice.Open:
- return this.Open;
+ ///
+ /// Range bars
+ ///
+ Range = 4,
- case BarPrice.Close:
- return this.Close;
+ ///
+ /// Tick bars
+ ///
+ Tick = 2,
- case BarPrice.Median:
- return this.Median;
+ ///
+ /// Time bars (open, high, low, close)
+ ///
+ Time = 1,
+ Volume = 3
+ }
- case BarPrice.Typical:
- return this.Typical;
+ ///
+ /// Adds two numbers.
+ ///
+ private Func Add = (x, y) => x + y;
- case BarPrice.Weighted:
- return this.Weighted;
- }
- return 0.0;
- }
+ [Serializable]
+ public class Bar : ICloneable, IDataObject
+ {
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class.
///
/// Type of the bar.
/// The size.
@@ -110,31 +116,30 @@ public double GetPrice(BarPrice option)
/// The close.
/// The volume.
/// The open int.
- public Bar(BarType barType, long size, DateTime beginTime, DateTime endTime, double open, double high, double low, double close, long volume, long openInt)
+ public Bar(BarType barType, long size, DateTime beginTime, DateTime endTime, double open, double high,
+ double low, double close, long volume, long openInt)
{
-
this.barType = barType;
- this.Size = size;
- this.BeginTime = beginTime;
- this.EndTime = endTime;
- this.Open = open;
- this.High = high;
- this.Low = low;
- this.Close = close;
- this.Volume = volume;
- this.OpenInt = openInt;
- this.ProviderId = 0;
- this.color = Color.Empty;
- this.IsComplete = false;
- this.DateKind = beginTime.Kind;
+ Size = size;
+ BeginTime = beginTime;
+ EndTime = endTime;
+ Open = open;
+ High = high;
+ Low = low;
+ Close = close;
+ Volume = volume;
+ OpenInt = openInt;
+ ProviderId = 0;
+ color = 0;
+ IsComplete = false;
+ DateKind = beginTime.Kind;
DateTimeKind kind = beginTime.Kind;
DateTimeKind kind2 = endTime.Kind;
}
-
///
- /// Initializes a new instance of the class.
+ /// Initializes a new instance of the class.
///
/// The size.
/// The open.
@@ -143,19 +148,18 @@ public Bar(BarType barType, long size, DateTime beginTime, DateTime endTime, dou
/// The close.
public Bar(long size, double open, double high, double low, double close)
{
-
- this.barType = barType;
- this.Size = size;
- this.BeginTime = beginTime;
- this.Open = open;
- this.High = high;
- this.Low = low;
- this.Close = close;
- this.ProviderId = 0;
- this.color = Color.Empty;
- this.IsComplete = false;
-
+ barType = barType;
+ Size = size;
+ BeginTime = beginTime;
+ Open = open;
+ High = high;
+ Low = low;
+ Close = close;
+ ProviderId = 0;
+ color = 0;
+ IsComplete = false;
}
+
#region ICloneable Members
public object Clone()
@@ -165,106 +169,102 @@ public object Clone()
#endregion
+ public DateTime DateTime { get; set; }
+ public double Close { get; set; }
+ public double Open { get; set; }
+ public double High { get; set; }
+ public double Low { get; set; }
+ public DateTime BeginTime { get; set; }
+ protected DateTime EndTime { get; set; } //end time for this bar.
+ public TimeSpan Duration { get; set; }
+ // Fields
+ protected BarType barType { get; set; }
+ protected DateTime beginTime { get; set; } //Begin time for this bar
+ protected int color { get; set; } //Color the bar should be drawed
+ protected bool IsComplete { get; set; } // is the bar complete.
+ protected long OpenInt { get; set; } // open interests on othis bar.
+ public byte ProviderId { get; set; } // provider for this bar (a byte , e.g Simulated executions 1.
+ protected long Size { get; set; } // the size : Lenght in seconds of this bar.
+ protected long Volume { get; set; } // the volume of this bar.
+ protected DateTimeKind DateKind { get; set; } //Bar kind.
+ protected double Weighted { get; set; }
+ protected double Median { get; set; }
+ protected double Typical { get; set; }
+
- }
+ ///
+ /// Gets the last price for a bar price option
+ ///
+ /// The option.
+ ///
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public double GetPrice(BarPrice option)
+ {
+ switch (option)
+ {
+ case BarPrice.High:
+ return High;
+ case BarPrice.Low:
+ return Low;
+ case BarPrice.Open:
+ return Open;
- ///
- /// Adds two numbers.
- ///
- Func Add = (x, y) => x + y;
+ case BarPrice.Close:
+ return Close;
+
+ case BarPrice.Median:
+ return Median;
+
+ case BarPrice.Typical:
+ return Typical;
+
+ case BarPrice.Weighted:
+ return Weighted;
+ }
+ return 0.0;
+ }
+ }
///
- /// holds arrays of bars
+ /// holds arrays of bars
///
public class BarArray : IEnumerable
{
// Methods
+
+ public DataArray BarSeries;
+
[MethodImpl(MethodImplOptions.NoInlining)]
public BarArray()
{
BarSeries = new DataArray();
-
}
- public DataArray BarSeries;
-
// Properties
///
- /// Gets the at the specified index.
+ /// Gets the at the specified index.
///
public Bar this[int index]
{
- [MethodImpl(MethodImplOptions.NoInlining)]
- get
- {
- return this[index];
- }
+ [MethodImpl(MethodImplOptions.NoInlining)] get { return this[index]; }
}
- ///
- /// Adds the specified bar.
- ///
- /// The bar.
- public void Add(Bar bar)
- {
- this.BarSeries.Add((Bar) bar);
-
- }
public IEnumerator GetEnumerator()
{
- return this.GetEnumerator();
+ return GetEnumerator();
}
- }
-
-
-
-
-
- public enum BarSlycing
- {
- Normal,
- Equally
- }
- ///
- /// The different bar types
- ///
- public enum BarType : byte
- {
- ///
- /// Dynamic types.
- ///
- Dynamic = 5,
///
- /// Range bars
- ///
- Range = 4,
- ///
- /// Tick bars
- ///
- Tick = 2,
- ///
- /// Time bars (open, high, low, close)
+ /// Adds the specified bar.
///
- Time = 1,
- Volume = 3
- }
-
- ///
- /// Different possible prices on a bar.
- ///
- public enum BarPrice
- {
- High,
- Low,
- Open,
- Close,
- Median,
- Typical,
- Weighted
+ /// The bar.
+ public void Add(Bar bar)
+ {
+ BarSeries.Add(bar);
+ }
}
public interface IDataObject
@@ -273,27 +273,5 @@ public interface IDataObject
DateTime DateTime { get; set; }
byte ProviderId { get; set; }
}
-
-
- ///
- /// Different data , a bar holds
- ///
- public enum BarData
- {
- Close,
- Open,
- High,
- Low,
- Median,
- Typical,
- Weighted,
- Volume,
- OpenInt
- }
-
-
-
-
}
-
}
diff --git a/encog-core-cs/App/Quant/Loader/OpenQuant/BarSeries.cs b/encog-core-cs/App/Quant/Loader/OpenQuant/BarSeries.cs
index 43903d6e..81c2aa62 100644
--- a/encog-core-cs/App/Quant/Loader/OpenQuant/BarSeries.cs
+++ b/encog-core-cs/App/Quant/Loader/OpenQuant/BarSeries.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,104 +23,121 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.ComponentModel;
-using System.IO;
-using System.Linq;
using System.Runtime.CompilerServices;
-using System.Text;
-using Bar = Encog.App.Quant.Loader.OpenQuant.Data.Data.Bar;
-using BarData = Encog.App.Quant.Loader.OpenQuant.Data.Data.BarData;
-using BarSlycing = Encog.App.Quant.Loader.OpenQuant.Data.Data.BarSlycing;
-
-using System.Drawing;
namespace Encog.App.Quant.Loader.OpenQuant
{
-
// Fields
[Serializable]
- public class DataArray : IEnumerable
+ public class DataArray : IEnumerable
{
-
-
protected double fDivisor;
- protected ArrayList fList;
+ protected List fList;
protected int fStopRecurant;
// Methods
[MethodImpl(MethodImplOptions.NoInlining)]
public DataArray()
{
- this.fList = new ArrayList();
+ fList = new List();
}
- [MethodImpl(MethodImplOptions.NoInlining)]
- public void Add(Data.Data.IDataObject obj)
+ ///
+ /// Gets the number of object in the array.
+ ///
+ public int Count
+ {
+ [MethodImpl(MethodImplOptions.NoInlining)] get { return fList.Count; }
+ }
+
+ ///
+ /// Gets the at the specified index.
+ ///
+ public Data.Data.IDataObject this[int index]
{
- this.fList.Add(obj);
+ [MethodImpl(MethodImplOptions.NoInlining)] get { return (fList[index] as Data.Data.IDataObject); }
}
///
- /// Clears this instance.
+ /// Returns an enumerator that iterates through a collection.
///
+ ///
+ /// An object that can be used to iterate through the collection.
+ ///
[MethodImpl(MethodImplOptions.NoInlining)]
- public void Clear()
+ public IEnumerator GetEnumerator()
{
- this.fList.Clear();
+ return fList.GetEnumerator();
}
///
- /// Determines whether [contains] [the specified obj].
+ /// Returns an enumerator that iterates through a collection.
///
- /// The obj.
///
- /// true if [contains] [the specified obj]; otherwise, false.
+ /// An object that can be used to iterate through the collection.
///
[MethodImpl(MethodImplOptions.NoInlining)]
- public bool Contains(Data.Data.IDataObject obj)
+ IEnumerator IEnumerable.GetEnumerator()
{
- return this.fList.Contains(obj);
+ return GetEnumerator();
}
+
[MethodImpl(MethodImplOptions.NoInlining)]
- public bool Contains(Bar bar)
+ public void Add(Data.Data.IDataObject obj)
{
- return this.fList.Contains(bar);
+ fList.Add(obj);
}
///
- /// Returns an enumerator that iterates through a collection.
+ /// Clears this instance.
///
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public void Clear()
+ {
+ fList.Clear();
+ }
+
+ ///
+ /// Determines whether [contains] [the specified obj].
+ ///
+ /// The obj.
///
- /// An object that can be used to iterate through the collection.
+ /// true if [contains] [the specified obj]; otherwise, false.
///
[MethodImpl(MethodImplOptions.NoInlining)]
- public IEnumerator GetEnumerator()
+ public bool Contains(Data.Data.IDataObject obj)
+ {
+ return fList.Contains(obj);
+ }
+
+ [MethodImpl(MethodImplOptions.NoInlining)]
+ public bool Contains(Data.Data.Bar bar)
{
- return this.fList.GetEnumerator();
+ return fList.Contains(bar);
}
///
- /// Gets the index for a certain datetime.
+ /// Gets the index for a certain datetime.
///
/// The datetime.
///
[MethodImpl(MethodImplOptions.NoInlining)]
public int GetIndex(DateTime datetime)
{
- if (this.Count != 0)
+ if (Count != 0)
{
DateTime dateTime = this[0].DateTime;
- DateTime time2 = this[this.Count - 1].DateTime;
+ DateTime time2 = this[Count - 1].DateTime;
if ((dateTime <= datetime) && (time2 >= datetime))
{
- return this.GetIndex(datetime, 0, this.Count - 1);
+ return GetIndex(datetime, 0, Count - 1);
}
}
return -1;
}
///
- /// Gets the index for a certain date time.
+ /// Gets the index for a certain date time.
///
/// The datetime.
/// The index1.
@@ -135,18 +152,18 @@ public int GetIndex(DateTime datetime, int index1, int index2)
long num3 = datetime.Ticks;
if (num2 != ticks)
{
- num4 = index1 + ((int)((index2 - index1) * ((num3 - ticks) / (num2 - ticks))));
+ num4 = index1 + ((int) ((index2 - index1)*((num3 - ticks)/(num2 - ticks))));
}
else
{
- num4 = (index1 + index2) / 2;
+ num4 = (index1 + index2)/2;
}
Data.Data.IDataObject obj2 = this[num4];
if (obj2.DateTime == datetime)
{
return num4;
}
- if (((index2 - num4) < this.fStopRecurant) || ((num4 - index1) < this.fStopRecurant))
+ if (((index2 - num4) < fStopRecurant) || ((num4 - index1) < fStopRecurant))
{
for (int i = index2; i >= index1; i--)
{
@@ -158,109 +175,84 @@ public int GetIndex(DateTime datetime, int index1, int index2)
}
return -1;
}
- int num6 = (int)(((double)(index2 - index1)) / this.fDivisor);
+ var num6 = (int) (((index2 - index1))/fDivisor);
int num7 = Math.Max(index1, num4 - num6);
obj2 = this[num7];
if (obj2.DateTime > datetime)
{
- return this.GetIndex(datetime, index1, num7);
+ return GetIndex(datetime, index1, num7);
}
int num8 = Math.Min(index2, num4 + num6);
obj2 = this[num8];
if (obj2.DateTime < datetime)
{
- return this.GetIndex(datetime, num8, index2);
+ return GetIndex(datetime, num8, index2);
}
- return this.GetIndex(datetime, num7, num8);
+ return GetIndex(datetime, num7, num8);
}
///
- /// Inserts an item at the specified index.
+ /// Inserts an item at the specified index.
///
/// The index.
/// The obj.
[MethodImpl(MethodImplOptions.NoInlining)]
public void Insert(int index, Data.Data.IDataObject obj)
{
- this.fList.Insert(index, obj);
+ fList.Insert(index, obj);
}
///
- /// Inserts an item at the specified index.
+ /// Inserts an item at the specified index.
///
/// The index.
/// The bar.
[MethodImpl(MethodImplOptions.NoInlining)]
- public void Insert(int index, Bar bar)
+ public void Insert(int index, Data.Data.Bar bar)
{
- this.fList.Insert(index, bar);
+ fList.Insert(index, bar);
}
///
- /// Removes the specified obj.
+ /// Removes the specified obj.
///
/// The obj.
[MethodImpl(MethodImplOptions.NoInlining)]
public void Remove(Data.Data.IDataObject obj)
{
- this.fList.Remove(obj);
+ fList.Remove(obj);
}
///
- /// Removes the specified obj.
+ /// Removes the specified obj.
///
/// The obj.
[MethodImpl(MethodImplOptions.NoInlining)]
- public void Remove(Bar bar)
+ public void Remove(Data.Data.Bar bar)
{
- this.fList.Remove(bar);
+ fList.Remove(bar);
}
+
///
- /// Removes an object at the specified index in the array of objects.
+ /// Removes an object at the specified index in the array of objects.
///
/// The index.
[MethodImpl(MethodImplOptions.NoInlining)]
public void RemoveAt(int index)
{
- this.fList.RemoveAt(index);
+ fList.RemoveAt(index);
}
+
// Properties
- ///
- /// Gets the number of object in the array.
- ///
- public int Count
- {
- [MethodImpl(MethodImplOptions.NoInlining)]
- get
- {
- return this.fList.Count;
- }
- }
- ///
- /// Gets the at the specified index.
- ///
- public Data.Data.IDataObject this[int index]
- {
- [MethodImpl(MethodImplOptions.NoInlining)]
- get
- {
- return (this.fList[index] as Data.Data.IDataObject);
- }
- }
///
- /// Adds the specified bar.
+ /// Adds the specified bar.
///
/// The bar.
- public void Add(Bar bar)
+ public void Add(Data.Data.Bar bar)
{
- this.Add(bar);
+ Add(bar);
}
}
-
}
-
-
-
-
diff --git a/encog-core-cs/App/Quant/Loader/OpenQuant/IDataseries.cs b/encog-core-cs/App/Quant/Loader/OpenQuant/IDataseries.cs
index 9c35bc03..c7124c81 100644
--- a/encog-core-cs/App/Quant/Loader/OpenQuant/IDataseries.cs
+++ b/encog-core-cs/App/Quant/Loader/OpenQuant/IDataseries.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -22,43 +22,33 @@
//
using System;
using System.Collections;
-using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Text;
namespace Encog.App.Quant.Loader.OpenQuant
{
public interface IDataSeries : IEnumerable
- {
- // Methods
- void Add(DateTime datetime, object obj);
- void Clear();
- bool Contains(DateTime datetime);
- DateTime DateTimeAt(int index);
- void Flush();
- int IndexOf(DateTime datetime);
- int IndexOf(DateTime datetime, SearchOption option);
- void Remove(DateTime datetime);
- void RemoveAt(int index);
- void Update(DateTime datetime, object obj);
- void Update(int index, object obj);
+ {
+ // Methods
+ int Count { get; }
+ string Description { get; set; }
+ DateTime FirstDateTime { get; }
+ object this[int index] { get; }
+ object this[DateTime datetime] { get; set; }
- // Properties
- int Count { get; }
- string Description { get; set; }
- DateTime FirstDateTime { get; }
- object this[int index] { get; }
- object this[DateTime datetime] { get; set; }
-
- DateTime LastDateTime { get; }
- string Name { get; }
- }
+ DateTime LastDateTime { get; }
+ string Name { get; }
+ void Add(DateTime datetime, object obj);
+ void Clear();
+ bool Contains(DateTime datetime);
+ DateTime DateTimeAt(int index);
+ void Flush();
+ int IndexOf(DateTime datetime);
+ int IndexOf(DateTime datetime, SearchOption option);
+ void Remove(DateTime datetime);
+ void RemoveAt(int index);
+ void Update(DateTime datetime, object obj);
+ void Update(int index, object obj);
-
-
-
-
-
+ // Properties
+ }
}
-
diff --git a/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuant.cs b/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuant.cs
index 4deb68f1..19b40596 100644
--- a/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuant.cs
+++ b/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuant.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -22,91 +22,55 @@
//
using System;
using System.Collections;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Drawing;
using System.IO;
-using System.Linq;
-using System.Text;
-using Encog.ML.Data;
-using Encog.ML.Data.Basic;
-using Encog.Neural.Networks;
-using Encog.Persist;
-using Encog.Util;
-using Encog.Util.File;
-using Encog.Util.Normalize;
-using Encog.Util.Normalize.Input;
-using Encog.Util.Normalize.Output;
-using Encog.Util.Normalize.Target;
-using Encog.App.Quant.Loader.OpenQuant;
-using Encog.App.Quant.Loader.OpenQuant.Data;
-using Bar = Encog.App.Quant.Loader.OpenQuant.Data.Data.Bar;
-using BarData = Encog.App.Quant.Loader.OpenQuant.Data.Data.BarData;
+
namespace Encog.App.Quant.Loader.OpenQuant
{
- class OpenQuant
+ internal class OpenQuant
{
-public interface IDataSeries : IEnumerable
-{
- // Methods
- void Add(DateTime datetime, object obj);
- void Clear();
- bool Contains(DateTime datetime);
- DateTime DateTimeAt(int index);
- void Flush();
- int IndexOf(DateTime datetime);
- int IndexOf(DateTime datetime, SearchOption option);
- void Remove(DateTime datetime);
- void RemoveAt(int index);
- void Update(DateTime datetime, object obj);
- void Update(int index, object obj);
-
- // Properties
- int Count { get; }
- string Description { get; set; }
- DateTime FirstDateTime { get; }
- object this[int index] { get; }
- object this[DateTime datetime] { get; set; }
- DateTime LastDateTime { get; }
- string Name { get; }
-}
-
-
-
-
+ protected DateTime EndTime { get; set; } //end time for this bar.
-
-
- protected DateTime EndTime { get; set; }//end time for this bar.
+ [Serializable]
+ public class BarSize
+ {
+ // Fields
+ public const long Day = 0x15180L;
+ public const long Hour = 0xe10L;
+ public const long Minute = 60L;
+ public const long Month = 0x278d00L;
+ public const long Second = 1L;
+ public const long Week = 0x93a80L;
+ public const long Year = 0x1e13380L;
+ }
+ public interface IDataSeries : IEnumerable
+ {
+ // Methods
+ int Count { get; }
+ string Description { get; set; }
+ DateTime FirstDateTime { get; }
+ object this[int index] { get; }
+ object this[DateTime datetime] { get; set; }
+ DateTime LastDateTime { get; }
+ string Name { get; }
+ void Add(DateTime datetime, object obj);
+ void Clear();
+ bool Contains(DateTime datetime);
+ DateTime DateTimeAt(int index);
+ void Flush();
+ int IndexOf(DateTime datetime);
+ int IndexOf(DateTime datetime, SearchOption option);
+ void Remove(DateTime datetime);
+ void RemoveAt(int index);
+ void Update(DateTime datetime, object obj);
+ void Update(int index, object obj);
- [Serializable]
- public class BarSize
- {
- // Fields
- public const long Day = 0x15180L;
- public const long Hour = 0xe10L;
- public const long Minute = 60L;
- public const long Month = 0x278d00L;
- public const long Second = 1L;
- public const long Week = 0x93a80L;
- public const long Year = 0x1e13380L;
- }
-
-
-
-
-
+ // Properties
+ }
#region collect
-
-
-
-
-
#endregion
-
}
}
diff --git a/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuantLoader.cs b/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuantLoader.cs
index e37863fd..b0acf5c8 100644
--- a/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuantLoader.cs
+++ b/encog-core-cs/App/Quant/Loader/OpenQuant/OpenQuantLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,44 +21,21 @@
// http://www.heatonresearch.com/copyright
//
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
namespace Encog.App.Quant.Loader.OpenQuant
{
public class OpenQuantLoader
{
-
- public class LoaderTypes
- {
- public DateTime from { get; set; }
- public DateTime to { get; set; }
- public long Barsize { get; set; }
- public string Instrument { get; set; }
- public Data.Data.BarType TypeBar { get; set; }
-
- public LoaderTypes(string instrument, DateTime StartingDate, DateTime ToDate,Data.Data.BarType Type, long SizeOfBars)
- {
- this.from = StartingDate;
- this.to = ToDate;
- this.Barsize = SizeOfBars;
- this.Instrument = instrument;
- this.TypeBar = Type;
- }
- }
-
-
private string instrument = "EURUSD";
+
///
- /// Froms the bar series to a double array.
+ /// Froms the bar series to a double array.
///
/// The thebarserie.
///
- public static double[] FromBarSeriestoDouble(Encog.App.Quant.Loader.OpenQuant.IDataSeries thebarserie)
+ public static double[] FromBarSeriestoDouble(IDataSeries thebarserie)
{
-
- double[] outputtedseries = new double[thebarserie.Count];
+ var outputtedseries = new double[thebarserie.Count];
int index = 0;
foreach (Data.Data.Bar abar in thebarserie)
{
@@ -69,17 +46,17 @@ public static double[] FromBarSeriestoDouble(Encog.App.Quant.Loader.OpenQuant.ID
}
///
- /// the data loader for openquant.
+ /// the data loader for openquant.
///
/// The instrument.
/// The dtfrom.
/// The dtto.
/// The barsize.
///
- public static LoaderTypes OpenquantDataLoader(string instrument, DateTime dtfrom, DateTime dtto, Data.Data.BarType bartype, long barsize)
+ public static LoaderTypes OpenquantDataLoader(string instrument, DateTime dtfrom, DateTime dtto,
+ Data.Data.BarType bartype, long barsize)
{
-
- DataArray BarSerie = new DataArray();
+ var BarSerie = new DataArray();
Console.WriteLine("Initialized the Openquant-Encog Loader");
try
{
@@ -87,20 +64,38 @@ public static LoaderTypes OpenquantDataLoader(string instrument, DateTime dtfrom
// BarSerie = this.GetHistoricalBars(this.MarketDataProvider,Instrument,dtfrom,dtto,(int)barsize);
// BarSerie = DataManager.GetHistoricalBars(instrument, Data.Data.BarType.Time, 3600);
- LoaderTypes typeLoaded = new LoaderTypes(instrument, dtfrom, dtto,bartype, barsize);
+ var typeLoaded = new LoaderTypes(instrument, dtfrom, dtto, bartype, barsize);
Console.WriteLine("Loaded Types instrument:" + typeLoaded.Instrument);
return typeLoaded;
}
catch (Exception ex)
{
- EncogError er = new EncogError(ex);
+ var er = new EncogError(ex);
Console.WriteLine("Error :" + ex.Message);
Console.WriteLine("Error :" + ex.StackTrace);
Console.WriteLine("Error:" + ex.InnerException);
Console.WriteLine("Full message:" + ex);
return null;
}
+ }
+
+ public class LoaderTypes
+ {
+ public LoaderTypes(string instrument, DateTime StartingDate, DateTime ToDate, Data.Data.BarType Type,
+ long SizeOfBars)
+ {
+ @from = StartingDate;
+ to = ToDate;
+ Barsize = SizeOfBars;
+ Instrument = instrument;
+ TypeBar = Type;
+ }
+ public DateTime from { get; set; }
+ public DateTime to { get; set; }
+ public long Barsize { get; set; }
+ public string Instrument { get; set; }
+ public Data.Data.BarType TypeBar { get; set; }
}
}
}
diff --git a/encog-core-cs/App/Quant/Loader/Yahoo/YahooDownload.cs b/encog-core-cs/App/Quant/Loader/Yahoo/YahooDownload.cs
index c31e9413..93a078db 100644
--- a/encog-core-cs/App/Quant/Loader/Yahoo/YahooDownload.cs
+++ b/encog-core-cs/App/Quant/Loader/Yahoo/YahooDownload.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -32,27 +32,27 @@
namespace Encog.App.Quant.Loader.Yahoo
{
///
- /// A loader, that will pull basic EOD data from YahooFinance.
+ /// A loader, that will pull basic EOD data from YahooFinance.
///
public class YahooDownload
{
///
- /// The DJIA index.
+ /// The DJIA index.
///
public const String IndexDjia = "^dji";
///
- /// The SP500 index.
+ /// The SP500 index.
///
public const String IndexSp500 = "^gspc";
///
- /// The NASDAQ index.
+ /// The NASDAQ index.
///
public const String IndexNasdaq = "^ixic";
///
- /// Construct the object.
+ /// Construct the object.
///
public YahooDownload()
{
@@ -60,21 +60,21 @@ public YahooDownload()
}
///
- /// The percision.
+ /// The percision.
///
public int Precision { get; set; }
///
- /// This method builds a URL to load data from Yahoo Finance for a neural
- /// network to train with.
+ /// This method builds a URL to load data from Yahoo Finance for a neural
+ /// network to train with.
///
/// The ticker symbol to access.
/// The beginning date.
/// The ending date.
/// The URL to read from
private static Uri BuildURL(String ticker, DateTime from,
- DateTime to)
+ DateTime to)
{
// construct the URL
var mstream = new MemoryStream();
@@ -98,7 +98,7 @@ private static Uri BuildURL(String ticker, DateTime from,
}
///
- /// Load financial data.
+ /// Load financial data.
///
/// The ticker symbol.
/// The output file.
@@ -158,6 +158,6 @@ public void LoadAllData(String ticker, String output, CSVFormat outputFormat, Da
{
throw new QuantError(ex);
}
- }
+ }
}
}
diff --git a/encog-core-cs/App/Quant/Ninja/NinjaFileConvert.cs b/encog-core-cs/App/Quant/Ninja/NinjaFileConvert.cs
index 985681ab..02bd8220 100644
--- a/encog-core-cs/App/Quant/Ninja/NinjaFileConvert.cs
+++ b/encog-core-cs/App/Quant/Ninja/NinjaFileConvert.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -28,13 +28,13 @@
namespace Encog.App.Quant.Ninja
{
///
- /// A simple class to convert financial data files into the format used by NinjaTrader for
- /// input.
+ /// A simple class to convert financial data files into the format used by NinjaTrader for
+ /// input.
///
public class NinjaFileConvert : BasicCachedFile
{
///
- /// Process the file and output to the target file.
+ /// Process the file and output to the target file.
///
/// The target file to write to.
public void Process(string target)
@@ -69,7 +69,7 @@ public void Process(string target)
}
///
- /// Analyze the input file.
+ /// Analyze the input file.
///
/// The name of the input file.
/// True, if headers are present.
diff --git a/encog-core-cs/App/Quant/Ninja/NinjaStreamWriter.cs b/encog-core-cs/App/Quant/Ninja/NinjaStreamWriter.cs
index 6387da7d..c7266d17 100644
--- a/encog-core-cs/App/Quant/Ninja/NinjaStreamWriter.cs
+++ b/encog-core-cs/App/Quant/Ninja/NinjaStreamWriter.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -31,43 +31,43 @@
namespace Encog.App.Quant.Ninja
{
///
- /// Can be used from within NinjaTrader to export data. This class is usually placed
- /// inside of a NinjaTrader indicator to export NinjaTrader indicators and data.
+ /// Can be used from within NinjaTrader to export data. This class is usually placed
+ /// inside of a NinjaTrader indicator to export NinjaTrader indicators and data.
///
public class NinjaStreamWriter
{
///
- /// The columns to use.
+ /// The columns to use.
///
private readonly IList columns = new List();
///
- /// True, if columns were defined.
+ /// True, if columns were defined.
///
private bool columnsDefined;
///
- /// The format of the CSV file.
+ /// The format of the CSV file.
///
private CSVFormat format;
///
- /// True, if headers are present.
+ /// True, if headers are present.
///
private bool headers;
///
- /// The output line, as it is built.
+ /// The output line, as it is built.
///
private StringBuilder line;
///
- /// The output file.
+ /// The output file.
///
private TextWriter tw;
///
- /// Construct the object, and set the defaults.
+ /// Construct the object, and set the defaults.
///
public NinjaStreamWriter()
{
@@ -76,12 +76,12 @@ public NinjaStreamWriter()
}
///
- /// The percision to use.
+ /// The percision to use.
///
public int Percision { get; set; }
///
- /// Open the file for output.
+ /// Open the file for output.
///
/// The filename.
/// True, if headers are present.
@@ -94,7 +94,7 @@ public void Open(String filename, bool headers, CSVFormat format)
}
///
- /// Write the headers.
+ /// Write the headers.
///
private void WriteHeaders()
{
@@ -120,7 +120,7 @@ private void WriteHeaders()
}
///
- /// Close the file.
+ /// Close the file.
///
public void Close()
{
@@ -130,7 +130,7 @@ public void Close()
}
///
- /// Begin a bar, for the specified date/time.
+ /// Begin a bar, for the specified date/time.
///
/// The date/time where the bar begins.
public void BeginBar(DateTime dt)
@@ -152,7 +152,7 @@ public void BeginBar(DateTime dt)
}
///
- /// End the current bar.
+ /// End the current bar.
///
public void EndBar()
{
@@ -177,7 +177,7 @@ public void EndBar()
}
///
- /// Store a column.
+ /// Store a column.
///
/// The name of the column.
/// The value to store.
diff --git a/encog-core-cs/App/Quant/QuantError.cs b/encog-core-cs/App/Quant/QuantError.cs
index fff5341c..54f87546 100644
--- a/encog-core-cs/App/Quant/QuantError.cs
+++ b/encog-core-cs/App/Quant/QuantError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -25,25 +25,22 @@
namespace Encog.App.Quant
{
///
- /// Used to represent any error that occurs in the quant part of Encog.
+ /// Used to represent any error that occurs in the quant part of Encog.
///
- ///
[Serializable]
public class QuantError : EncogError
{
///
- /// Construct a message exception.
+ /// Construct a message exception.
///
- ///
/// The exception message.
public QuantError(String msg) : base(msg)
{
}
///
- /// Construct an exception that holds another exception.
+ /// Construct an exception that holds another exception.
///
- ///
/// The other exception.
public QuantError(Exception t) : base(t)
{
diff --git a/encog-core-cs/App/Quant/QuantTask.cs b/encog-core-cs/App/Quant/QuantTask.cs
index 2873fb31..15888d15 100644
--- a/encog-core-cs/App/Quant/QuantTask.cs
+++ b/encog-core-cs/App/Quant/QuantTask.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,15 +23,13 @@
namespace Encog.App.Quant
{
///
- /// Defines an interface for Encog quant tasks.
+ /// Defines an interface for Encog quant tasks.
///
- ///
public interface QuantTask
{
///
- /// Request to stop.
+ /// Request to stop.
///
- ///
void RequestStop();
diff --git a/encog-core-cs/App/Quant/Util/BarBuffer.cs b/encog-core-cs/App/Quant/Util/BarBuffer.cs
index a25030f8..7213d5b0 100644
--- a/encog-core-cs/App/Quant/Util/BarBuffer.cs
+++ b/encog-core-cs/App/Quant/Util/BarBuffer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -27,27 +27,23 @@
namespace Encog.App.Quant.Util
{
///
- /// A buffer of bar segments.
+ /// A buffer of bar segments.
///
- ///
public class BarBuffer
{
///
- /// The bar data loaded.
+ /// The bar data loaded.
///
- ///
private readonly IList data;
///
- /// The number of periods.
+ /// The number of periods.
///
- ///
private readonly int periods;
///
- /// Construct the object.
+ /// Construct the object.
///
- ///
/// The number of periods.
public BarBuffer(int thePeriods)
{
@@ -63,9 +59,8 @@ public IList Data
///
- /// Determine if the buffer is full.
+ /// Determine if the buffer is full.
///
- ///
/// True if the buffer is full.
public bool Full
{
@@ -73,9 +68,8 @@ public bool Full
}
///
- /// Add a bar.
+ /// Add a bar.
///
- ///
/// The bar data.
public void Add(double d)
{
@@ -85,9 +79,8 @@ public void Add(double d)
}
///
- /// Add a bar.
+ /// Add a bar.
///
- ///
/// The bar data.
public void Add(double[] d)
{
@@ -99,9 +92,8 @@ public void Add(double[] d)
}
///
- /// Average all of the bars.
+ /// Average all of the bars.
///
- ///
/// The bar index to average.
/// The average.
public double Average(int idx)
@@ -117,9 +109,8 @@ public double Average(int idx)
}
///
- /// Get the average gain.
+ /// Get the average gain.
///
- ///
/// The field to get the average gain for.
/// The average gain.
public double AverageGain(int idx)
@@ -149,9 +140,8 @@ public double AverageGain(int idx)
}
///
- /// Get the average loss.
+ /// Get the average loss.
///
- ///
/// The index to check for.
/// The average loss.
public double AverageLoss(int idx)
@@ -182,9 +172,8 @@ public double AverageLoss(int idx)
///
- /// Get the max for the specified index.
+ /// Get the max for the specified index.
///
- ///
/// The index to check.
/// The max.
public double Max(int idx)
@@ -200,9 +189,8 @@ public double Max(int idx)
}
///
- /// Get the min for the specified index.
+ /// Get the min for the specified index.
///
- ///
/// The index to check.
/// The min.
public double Min(int idx)
@@ -218,9 +206,8 @@ public double Min(int idx)
}
///
- /// Pop (and remove) the oldest bar in the buffer.
+ /// Pop (and remove) the oldest bar in the buffer.
///
- ///
/// The oldest bar in the buffer.
public double[] Pop()
{
diff --git a/encog-core-cs/Bot/BotError.cs b/encog-core-cs/Bot/BotError.cs
index f49e3306..f4e7ec45 100644
--- a/encog-core-cs/Bot/BotError.cs
+++ b/encog-core-cs/Bot/BotError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/BotUtil.cs b/encog-core-cs/Bot/BotUtil.cs
index 6e69af20..a95141a6 100644
--- a/encog-core-cs/Bot/BotUtil.cs
+++ b/encog-core-cs/Bot/BotUtil.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -295,5 +295,11 @@ public static string POSTPage(Uri uri, Stream stream)
var sr = new StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
+
+ public static void DownloadPage(Uri uri, string file)
+ {
+ var Client = new WebClient();
+ Client.DownloadFile(uri, file);
+ }
}
}
diff --git a/encog-core-cs/Bot/Browse/Address.cs b/encog-core-cs/Bot/Browse/Address.cs
index 64afb493..275f586d 100644
--- a/encog-core-cs/Bot/Browse/Address.cs
+++ b/encog-core-cs/Bot/Browse/Address.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/BrowseError.cs b/encog-core-cs/Bot/Browse/BrowseError.cs
index 52aa3508..5f0885cf 100644
--- a/encog-core-cs/Bot/Browse/BrowseError.cs
+++ b/encog-core-cs/Bot/Browse/BrowseError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Browser.cs b/encog-core-cs/Bot/Browse/Browser.cs
index 78ca8f9d..7c0b7a8c 100644
--- a/encog-core-cs/Bot/Browse/Browser.cs
+++ b/encog-core-cs/Bot/Browse/Browser.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Extract/BasicExtract.cs b/encog-core-cs/Bot/Browse/Extract/BasicExtract.cs
index aca289f8..9a452e6c 100644
--- a/encog-core-cs/Bot/Browse/Extract/BasicExtract.cs
+++ b/encog-core-cs/Bot/Browse/Extract/BasicExtract.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Extract/IExtract.cs b/encog-core-cs/Bot/Browse/Extract/IExtract.cs
index f51ff9f5..9a8b7989 100644
--- a/encog-core-cs/Bot/Browse/Extract/IExtract.cs
+++ b/encog-core-cs/Bot/Browse/Extract/IExtract.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Extract/IExtractListener.cs b/encog-core-cs/Bot/Browse/Extract/IExtractListener.cs
index 820e6cf6..9bb4a09f 100644
--- a/encog-core-cs/Bot/Browse/Extract/IExtractListener.cs
+++ b/encog-core-cs/Bot/Browse/Extract/IExtractListener.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Extract/ListExtractListener.cs b/encog-core-cs/Bot/Browse/Extract/ListExtractListener.cs
index 04b635f1..93d4fa38 100644
--- a/encog-core-cs/Bot/Browse/Extract/ListExtractListener.cs
+++ b/encog-core-cs/Bot/Browse/Extract/ListExtractListener.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/LoadWebPage.cs b/encog-core-cs/Bot/Browse/LoadWebPage.cs
index 396afc1f..ebd7c992 100644
--- a/encog-core-cs/Bot/Browse/LoadWebPage.cs
+++ b/encog-core-cs/Bot/Browse/LoadWebPage.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/Div.cs b/encog-core-cs/Bot/Browse/Range/Div.cs
index eb460922..fa659c4b 100644
--- a/encog-core-cs/Bot/Browse/Range/Div.cs
+++ b/encog-core-cs/Bot/Browse/Range/Div.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/DocumentRange.cs b/encog-core-cs/Bot/Browse/Range/DocumentRange.cs
index f97b792b..1e2793f5 100644
--- a/encog-core-cs/Bot/Browse/Range/DocumentRange.cs
+++ b/encog-core-cs/Bot/Browse/Range/DocumentRange.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/Form.cs b/encog-core-cs/Bot/Browse/Range/Form.cs
index 54f4cabe..37222752 100644
--- a/encog-core-cs/Bot/Browse/Range/Form.cs
+++ b/encog-core-cs/Bot/Browse/Range/Form.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/FormElement.cs b/encog-core-cs/Bot/Browse/Range/FormElement.cs
index 1310fdae..74d24be1 100644
--- a/encog-core-cs/Bot/Browse/Range/FormElement.cs
+++ b/encog-core-cs/Bot/Browse/Range/FormElement.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/Input.cs b/encog-core-cs/Bot/Browse/Range/Input.cs
index a2680770..abde7ef8 100644
--- a/encog-core-cs/Bot/Browse/Range/Input.cs
+++ b/encog-core-cs/Bot/Browse/Range/Input.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/Link.cs b/encog-core-cs/Bot/Browse/Range/Link.cs
index 22057fcc..1e92f623 100644
--- a/encog-core-cs/Bot/Browse/Range/Link.cs
+++ b/encog-core-cs/Bot/Browse/Range/Link.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/Range/Span.cs b/encog-core-cs/Bot/Browse/Range/Span.cs
index ac667c2e..37d48251 100644
--- a/encog-core-cs/Bot/Browse/Range/Span.cs
+++ b/encog-core-cs/Bot/Browse/Range/Span.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/Browse/WebPage.cs b/encog-core-cs/Bot/Browse/WebPage.cs
index b3544702..cdf784f4 100644
--- a/encog-core-cs/Bot/Browse/WebPage.cs
+++ b/encog-core-cs/Bot/Browse/WebPage.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/DataUnits/CodeDataUnit.cs b/encog-core-cs/Bot/DataUnits/CodeDataUnit.cs
index 18722a50..c6718daf 100644
--- a/encog-core-cs/Bot/DataUnits/CodeDataUnit.cs
+++ b/encog-core-cs/Bot/DataUnits/CodeDataUnit.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/DataUnits/DataUnit.cs b/encog-core-cs/Bot/DataUnits/DataUnit.cs
index 4578b66e..af00ac0c 100644
--- a/encog-core-cs/Bot/DataUnits/DataUnit.cs
+++ b/encog-core-cs/Bot/DataUnits/DataUnit.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/DataUnits/TagDataUnit.cs b/encog-core-cs/Bot/DataUnits/TagDataUnit.cs
index b5bc278d..6fe45532 100644
--- a/encog-core-cs/Bot/DataUnits/TagDataUnit.cs
+++ b/encog-core-cs/Bot/DataUnits/TagDataUnit.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/DataUnits/TextDataUnit.cs b/encog-core-cs/Bot/DataUnits/TextDataUnit.cs
index 43d55d44..56b7950d 100644
--- a/encog-core-cs/Bot/DataUnits/TextDataUnit.cs
+++ b/encog-core-cs/Bot/DataUnits/TextDataUnit.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/RSS/RSS.cs b/encog-core-cs/Bot/RSS/RSS.cs
index 8844436f..7562544d 100644
--- a/encog-core-cs/Bot/RSS/RSS.cs
+++ b/encog-core-cs/Bot/RSS/RSS.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Bot/RSS/RSSItem.cs b/encog-core-cs/Bot/RSS/RSSItem.cs
index 861ae7a6..edcd9c8a 100644
--- a/encog-core-cs/Bot/RSS/RSSItem.cs
+++ b/encog-core-cs/Bot/RSS/RSSItem.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Genetic/Genes/DoubleGene.cs b/encog-core-cs/Cloud/CloudError.cs
similarity index 53%
rename from encog-core-cs/ML/Genetic/Genes/DoubleGene.cs
rename to encog-core-cs/Cloud/CloudError.cs
index f19a6976..edf1ffa9 100644
--- a/encog-core-cs/ML/Genetic/Genes/DoubleGene.cs
+++ b/encog-core-cs/Cloud/CloudError.cs
@@ -1,67 +1,60 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Genes
-{
- ///
- /// A gene that contains a floating point value.
- ///
- ///
- public class DoubleGene : BasicGene
- {
- ///
- /// The value of this gene.
- ///
- ///
- private double _value;
-
- ///
- /// Set the value of the gene.
- ///
- ///
- /// The gene's value.
- public double Value
- {
- get { return _value; }
- set { _value = value; }
- }
-
- ///
- /// Copy another gene to this one.
- ///
- ///
- /// The other gene to copy.
- public override sealed void Copy(IGene gene)
- {
- _value = ((DoubleGene)gene).Value;
- }
-
-
- ///
- public override sealed String ToString()
- {
- return "" + _value;
- }
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.Cloud
+{
+ ///
+ /// An error has occured in one of the Encog Cloud classes.
+ ///
+ public class CloudError : EncogError
+ {
+ ///
+ /// Construct a message exception.
+ ///
+ /// The message.
+ public CloudError(String str)
+ : base(str)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The other exception.
+ public CloudError(Exception e)
+ : base("Nested Exception", e)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The message.
+ /// The exception.
+ public CloudError(String msg, Exception e)
+ : base(msg, e)
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Basic/BasicIndicator.cs b/encog-core-cs/Cloud/Indicator/Basic/BasicIndicator.cs
new file mode 100644
index 00000000..7a27432e
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Basic/BasicIndicator.cs
@@ -0,0 +1,162 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.Cloud.Indicator.Server;
+
+namespace Encog.Cloud.Indicator.Basic
+{
+ ///
+ /// This abstract class provides low-level functionality to all Encog based
+ /// indicators.
+ ///
+ public abstract class BasicIndicator : IIndicatorListener
+ {
+ ///
+ /// Is this indicator blocking, should it wait for a result after each bar.
+ ///
+ private readonly bool _blocking;
+
+ ///
+ /// The number of bars requested per data item.
+ ///
+ private readonly IList _dataCount = new List();
+
+ ///
+ /// The data that has been requested from the remote side. This is
+ /// typically HLOC(High, Low, Open, Close) data that is needed by the
+ /// Encog indicator to compute.
+ ///
+ private readonly IList _dataRequested = new List();
+
+ ///
+ /// The communication link between the indicator and remote.
+ ///
+ private IndicatorLink _link;
+
+ ///
+ /// Construc the basic indicator.
+ ///
+ /// Are we blocking?
+ protected BasicIndicator(bool theBlocking)
+ {
+ _blocking = theBlocking;
+ }
+
+ ///
+ /// The current error message;
+ ///
+ public String ErrorMessage { get; set; }
+
+ ///
+ /// The data that has been requested from the remote side. This is
+ /// typically HLOC(High, Low, Open, Close) data that is needed by the
+ /// Encog indicator to compute.
+ ///
+ public IList DataRequested
+ {
+ get { return _dataRequested; }
+ }
+
+ ///
+ /// Are we blocking?
+ ///
+ public bool Blocking
+ {
+ get { return _blocking; }
+ }
+
+ ///
+ /// The link.
+ ///
+ public IndicatorLink Link
+ {
+ get { return _link; }
+ }
+
+ ///
+ /// The count.
+ ///
+ public IList DataCount
+ {
+ get { return _dataCount; }
+ }
+
+ #region IIndicatorListener Members
+
+ ///
+ /// Notify that this indicator is now connected. This is called
+ /// once, at the beginning of a connection. Indicators are not
+ /// reused.
+ ///
+ /// The link.
+ public void NotifyConnect(IndicatorLink theLink)
+ {
+ _link = theLink;
+ if (ErrorMessage != null)
+ {
+ String[] args = {ErrorMessage};
+ Link.WritePacket(IndicatorLink.PacketError, args);
+ }
+ else
+ {
+ _link.InitConnection(_dataRequested, _blocking);
+ }
+ }
+
+
+ public abstract void NotifyPacket(IndicatorPacket packet);
+
+ public abstract void NotifyTermination();
+
+ #endregion
+
+ ///
+ /// Request the specified data. i.e. HIGH, LOW, etc.
+ ///
+ /// The data being requested.
+ public void RequestData(String str)
+ {
+ _dataRequested.Add(str);
+
+ int idx = str.IndexOf('[');
+
+ if (idx == -1)
+ {
+ _dataCount.Add(1);
+ return;
+ }
+
+ int idx2 = str.IndexOf(']', idx);
+
+ if (idx2 == -1)
+ {
+ _dataCount.Add(1);
+ return;
+ }
+
+ String s = str.Substring(idx + 1, idx2 - (idx + 1));
+ _dataCount.Add(int.Parse(s));
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicator.cs b/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicator.cs
new file mode 100644
index 00000000..4c3d121a
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicator.cs
@@ -0,0 +1,177 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Encog.Cloud.Indicator.Server;
+
+namespace Encog.Cloud.Indicator.Basic
+{
+ ///
+ /// This indicator is used to download data from an external source. For example
+ /// you may want to export financial data from Ninja Trader, including Ninja Trader
+ /// native indicators. This class will download that data and write it to a CSV file.
+ ///
+ public class DownloadIndicator : BasicIndicator
+ {
+ ///
+ /// The default port.
+ ///
+ public const int Port = 5128;
+
+ ///
+ /// The instruments that we are downloading (i.e. ticker symbols, and their data)
+ ///
+ private readonly IDictionary _data = new Dictionary();
+
+ ///
+ /// The target CSV file.
+ ///
+ private readonly FileInfo _targetFile;
+
+ ///
+ /// The number of rows downloaded.
+ ///
+ private int _rowsDownloaded;
+
+ ///
+ /// Construct the download indicator.
+ ///
+ /// The local CSV file.
+ public DownloadIndicator(FileInfo theFile)
+ : base(false)
+ {
+ _targetFile = theFile;
+ }
+
+ ///
+ /// The number of rows downloaded.
+ ///
+ public int RowsDownloaded
+ {
+ get { return _rowsDownloaded; }
+ }
+
+ ///
+ /// Close.
+ ///
+ public void Close()
+ {
+ Link.Close();
+ _data.Clear();
+ }
+
+ ///
+ public override void NotifyPacket(IndicatorPacket packet)
+ {
+ if (string.Compare(packet.Command, IndicatorLink.PacketBar, true) == 0)
+ {
+ String security = packet.Args[1];
+ long when = long.Parse(packet.Args[0]);
+ String key = security.ToLower();
+ InstrumentHolder holder;
+
+ if (_data.ContainsKey(key))
+ {
+ holder = _data[key];
+ }
+ else
+ {
+ holder = new InstrumentHolder();
+ _data[key] = holder;
+ }
+
+ if (holder.Record(when, 2, packet.Args))
+ {
+ _rowsDownloaded++;
+ }
+ }
+ }
+
+ ///
+ public override void NotifyTermination()
+ {
+ Save();
+ }
+
+ ///
+ /// Save the file.
+ ///
+ public void Save()
+ {
+ try
+ {
+ if (_data.Count == 0)
+ {
+ return;
+ }
+
+
+ using (var outfile =
+ new StreamWriter(_targetFile.ToString()))
+ {
+ // output header
+ outfile.Write("\"INSTRUMENT\",\"WHEN\"");
+ int index = 0;
+ foreach (String str in DataRequested)
+ {
+ String str2;
+ int ix = str.IndexOf('[');
+ str2 = ix != -1 ? str.Substring(0, ix).Trim() : str;
+
+ int c = DataCount[index++];
+ if (c <= 1)
+ {
+ outfile.Write(",\"" + str2 + "\"");
+ }
+ else
+ {
+ for (int i = 0; i < c; i++)
+ {
+ outfile.Write(",\"" + str2 + "-b" + i + "\"");
+ }
+ }
+ }
+ outfile.WriteLine();
+
+ // output data
+ foreach (string ins in _data.Keys)
+ {
+ InstrumentHolder holder = _data[ins];
+ foreach (long key in holder.Sorted)
+ {
+ String str = holder.Data[key];
+ outfile.WriteLine("\"" + ins + "\"," + key + "," + str);
+ }
+ }
+
+ outfile.Close();
+ }
+ }
+ catch (IOException ex)
+ {
+ throw new EncogError(ex);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicatorFactory.cs b/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicatorFactory.cs
new file mode 100644
index 00000000..f142e06e
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Basic/DownloadIndicatorFactory.cs
@@ -0,0 +1,97 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace Encog.Cloud.Indicator.Basic
+{
+ ///
+ /// A factory used to produce indicators of the type DownloadIndicator.
+ /// Make sure to specify the file to download to, as well as the
+ /// data requested from the remote.
+ ///
+ public class DownloadIndicatorFactory : IIndicatorFactory
+ {
+ ///
+ /// The data requested.
+ ///
+ private readonly IList _dataRequested = new List();
+
+ ///
+ /// The file to download to.
+ ///
+ private readonly FileInfo _file;
+
+ ///
+ /// Construct the factory.
+ ///
+ /// The file to download to.
+ public DownloadIndicatorFactory(FileInfo theFile)
+ {
+ _file = theFile;
+ }
+
+ ///
+ /// The data requested.
+ ///
+ public IList DataRequested
+ {
+ get { return _dataRequested; }
+ }
+
+ #region IIndicatorFactory Members
+
+ ///
+ /// The name of this indicator, which is "Download".
+ ///
+ public String Name
+ {
+ get { return "Download"; }
+ }
+
+ ///
+ public IIndicatorListener Create()
+ {
+ var ind = new DownloadIndicator(_file);
+
+ foreach (String d in _dataRequested)
+ {
+ ind.RequestData(d);
+ }
+
+ return ind;
+ }
+
+ #endregion
+
+ ///
+ /// Request the specified item of data.
+ ///
+ /// The data requested.
+ public void RequestData(String str)
+ {
+ _dataRequested.Add(str);
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Basic/InstrumentHolder.cs b/encog-core-cs/Cloud/Indicator/Basic/InstrumentHolder.cs
new file mode 100644
index 00000000..acda053b
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Basic/InstrumentHolder.cs
@@ -0,0 +1,89 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Encog.Cloud.Indicator.Basic
+{
+ ///
+ /// Used to hold instruments, i.e. ticker symbols of securities.
+ /// Also holds financial data downloaded by ticker symbol.
+ ///
+ public class InstrumentHolder
+ {
+ ///
+ /// The downloaded financial data.
+ ///
+ private readonly IDictionary _data = new Dictionary();
+
+ ///
+ /// The sorted data.
+ ///
+ private readonly ICollection _sorted = new SortedSet();
+
+ ///
+ /// The data.
+ ///
+ public IDictionary Data
+ {
+ get { return _data; }
+ }
+
+ ///
+ /// Sorted keys.
+ ///
+ public ICollection Sorted
+ {
+ get { return _sorted; }
+ }
+
+ ///
+ /// Record one piece of data. Data with the same time stamp.
+ ///
+ /// The time the data occurred.
+ /// Where should we start from when storing, index into data.
+ /// Allows unimportant "leading data" to be discarded without creating a new
+ /// array.
+ /// The financial data.
+ /// True, if the data did not exist already.
+ public bool Record(long when, int starting, String[] data)
+ {
+ var str = new StringBuilder();
+
+ for (int i = starting; i < data.Length; i++)
+ {
+ if (i > starting)
+ {
+ str.Append(',');
+ }
+ str.Append(data[i]);
+ }
+
+ bool result = !_data.ContainsKey(when);
+ _sorted.Add(when);
+ this._data[when] = str.ToString();
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Mutate/IMutate.cs b/encog-core-cs/Cloud/Indicator/IIndicatorConnectionListener.cs
similarity index 62%
rename from encog-core-cs/ML/Genetic/Mutate/IMutate.cs
rename to encog-core-cs/Cloud/Indicator/IIndicatorConnectionListener.cs
index 67c99e8b..3a284ce1 100644
--- a/encog-core-cs/ML/Genetic/Mutate/IMutate.cs
+++ b/encog-core-cs/Cloud/Indicator/IIndicatorConnectionListener.cs
@@ -1,40 +1,39 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.ML.Genetic.Genome;
-
-namespace Encog.ML.Genetic.Mutate
-{
- ///
- /// Defines how a chromosome is mutated.
- ///
- ///
- public interface IMutate
- {
- ///
- /// Perform a mutation on the specified chromosome.
- ///
- ///
- /// The chromosome to mutate.
- void PerformMutation(Chromosome chromosome);
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.Cloud.Indicator.Server;
+
+namespace Encog.Cloud.Indicator
+{
+ ///
+ /// Listens for indicator connections.
+ ///
+ public interface IIndicatorConnectionListener
+ {
+ ///
+ /// Notify of a connection.
+ ///
+ /// The link to the connection.
+ /// True if this is a connect, false otherwise.
+ void NotifyConnections(IndicatorLink link, bool hasOpened);
+ }
+}
diff --git a/encog-core-cs/Neural/NEAT/Training/NEATParent.cs b/encog-core-cs/Cloud/Indicator/IIndicatorFactory.cs
similarity index 66%
rename from encog-core-cs/Neural/NEAT/Training/NEATParent.cs
rename to encog-core-cs/Cloud/Indicator/IIndicatorFactory.cs
index 4b4bbf51..646dfe16 100644
--- a/encog-core-cs/Neural/NEAT/Training/NEATParent.cs
+++ b/encog-core-cs/Cloud/Indicator/IIndicatorFactory.cs
@@ -1,43 +1,44 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-namespace Encog.Neural.NEAT.Training
-{
- ///
- /// The two parents.
- ///
- ///
- internal enum NEATParent
- {
- ///
- /// The father.
- ///
- ///
- Dad,
-
- ///
- /// The mother.
- ///
- ///
- Mom
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.Cloud.Indicator
+{
+ ///
+ /// A factory used to create indicators. This is passed to the indicator
+ /// server.
+ ///
+ public interface IIndicatorFactory
+ {
+ ///
+ /// The name of the indicator.
+ ///
+ String Name { get; }
+
+ ///
+ /// Create the indicator.
+ ///
+ /// The new indicator.
+ IIndicatorListener Create();
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/IIndicatorListener.cs b/encog-core-cs/Cloud/Indicator/IIndicatorListener.cs
new file mode 100644
index 00000000..3b0876fe
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/IIndicatorListener.cs
@@ -0,0 +1,51 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.Cloud.Indicator.Server;
+
+namespace Encog.Cloud.Indicator
+{
+ ///
+ /// This interface defines the actual indicator. This allows the indicator
+ /// to be notified on initial connection, when a packet is received,
+ /// and also connection termination.
+ ///
+ public interface IIndicatorListener
+ {
+ ///
+ /// Notify that a link has been established. This is called once.
+ ///
+ /// The link used.
+ void NotifyConnect(IndicatorLink theLink);
+
+ ///
+ /// Notify that a packet has been received.
+ ///
+ /// The packet.
+ void NotifyPacket(IndicatorPacket packet);
+
+ ///
+ /// The connection has been terminated.
+ ///
+ void NotifyTermination();
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genes/CharGene.cs b/encog-core-cs/Cloud/Indicator/IndicatorError.cs
similarity index 52%
rename from encog-core-cs/ML/Genetic/Genes/CharGene.cs
rename to encog-core-cs/Cloud/Indicator/IndicatorError.cs
index cbf30fd3..7b3c3684 100644
--- a/encog-core-cs/ML/Genetic/Genes/CharGene.cs
+++ b/encog-core-cs/Cloud/Indicator/IndicatorError.cs
@@ -1,67 +1,60 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Genes
-{
- ///
- /// A gene that holds a single character.
- ///
- ///
- public class CharGene : BasicGene
- {
- ///
- /// The character value of the gene.
- ///
- ///
- private char _value;
-
- ///
- /// Set the value of this gene.
- ///
- ///
- /// The new value of this gene.
- public char Value
- {
- get { return _value; }
- set { _value = value; }
- }
-
- ///
- /// Copy another gene to this gene.
- ///
- ///
- /// The source gene.
- public override sealed void Copy(IGene gene)
- {
- _value = ((CharGene) gene).Value;
- }
-
-
- ///
- public override sealed String ToString()
- {
- return "" + _value;
- }
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.Cloud.Indicator
+{
+ ///
+ /// An error has occured communicating with an external indicator.
+ ///
+ public class IndicatorError : CloudError
+ {
+ ///
+ /// Construct a message exception.
+ ///
+ /// The message.
+ public IndicatorError(String str)
+ : base(str)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The other exception.
+ public IndicatorError(Exception e)
+ : base("Nested Exception", e)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The message.
+ /// The exception.
+ public IndicatorError(String msg, Exception e)
+ : base(msg, e)
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Server/HandleClient.cs b/encog-core-cs/Cloud/Indicator/Server/HandleClient.cs
new file mode 100644
index 00000000..f0ee4482
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Server/HandleClient.cs
@@ -0,0 +1,140 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.Util.Logging;
+
+namespace Encog.Cloud.Indicator.Server
+{
+ ///
+ /// Handle a remote client, such as Ninja Trader.
+ ///
+ public class HandleClient
+ {
+ ///
+ /// The indicator server that we belog to.
+ ///
+ private readonly IndicatorServer _server;
+
+ ///
+ /// Are we done.
+ ///
+ private bool _done;
+
+ ///
+ /// The indicator that is listening.
+ ///
+ private IIndicatorListener _listener;
+
+ ///
+ /// Construct a client handler.
+ ///
+ /// The indicator server.
+ /// The indicator link.
+ public HandleClient(IndicatorServer s, IndicatorLink l)
+ {
+ RemoteType = "Unknown";
+ Link = l;
+ _server = s;
+ }
+
+ ///
+ /// The remote type, i.e. Ninja Trader.
+ ///
+ public string RemoteType { get; private set; }
+
+
+ ///
+ /// The indicator's name.
+ ///
+ public string IndicatorName { get; private set; }
+
+ ///
+ /// The link that we are using.
+ ///
+ public IndicatorLink Link { get; private set; }
+
+ ///
+ /// Background thread.
+ ///
+ public void Run()
+ {
+ EncogLogging.Log(EncogLogging.LevelDebug, "Waiting for packets");
+
+ try
+ {
+ while (!_done)
+ {
+ IndicatorPacket packet = Link.ReadPacket();
+
+ // really do not care if we timeout, just keep listening
+ if (packet == null)
+ {
+ continue;
+ }
+ if (string.Compare(packet.Command,
+ IndicatorLink.PacketHello, true) == 0)
+ {
+ RemoteType = packet.Args[0];
+ IndicatorName = packet.Args[1];
+ _listener = _server
+ .ResolveIndicator(IndicatorName);
+ _listener.NotifyConnect(Link);
+ }
+ else if (string.Compare(packet.Command,
+ IndicatorLink.PacketGoodbye, true) == 0)
+ {
+ _done = true;
+ }
+ else
+ {
+ _listener.NotifyPacket(packet);
+ }
+ }
+ }
+ catch (IndicatorError ex)
+ {
+ EncogLogging.Log(EncogLogging.LevelDebug,
+ "Client ended connection:" + ex.Message);
+
+ _done = true;
+ }
+ catch (Exception t)
+ {
+ EncogLogging.Log(EncogLogging.LevelCritical, t);
+ }
+ finally
+ {
+ _done = true;
+ _server.Connections.Remove(this);
+ if (_listener != null)
+ {
+ _listener.NotifyTermination();
+ }
+ _server.NotifyListenersConnections(Link, false);
+ EncogLogging.Log(EncogLogging.LevelDebug,
+ "Shutting down client handler");
+ Link.Close();
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Server/IndicatorLink.cs b/encog-core-cs/Cloud/Indicator/Server/IndicatorLink.cs
new file mode 100644
index 00000000..da6ec580
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Server/IndicatorLink.cs
@@ -0,0 +1,300 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net.Sockets;
+using System.Text;
+using Encog.Util.CSV;
+using Encog.Util.Logging;
+
+namespace Encog.Cloud.Indicator.Server
+{
+ ///
+ /// Managed a link to a remote indicator.
+ ///
+ public class IndicatorLink
+ {
+ ///
+ /// The HELLO packet, sent from the client to the server to provide version information.
+ ///
+ public const string PacketHello = "HELLO";
+
+ ///
+ /// The GOODBYE packet, sent from the client to the server to end communication.
+ ///
+ public const string PacketGoodbye = "GOODBYE";
+
+ ///
+ /// The SIGNALS packet, sent from the client to the server to specify requested data.
+ ///
+ public const string PacketSignals = "SIGNALS";
+
+ ///
+ /// The INIT packet, sent from the server to the client to provide config information.
+ ///
+ public const string PacketInit = "INIT";
+
+ ///
+ /// The BAR packet, sent from the client to the server at the end of each BAR.
+ ///
+ public const string PacketBar = "BAR";
+
+ ///
+ /// The IND packet, sent from the server to the clinet, in response to a BAR.
+ ///
+ public const string PacketInd = "IND";
+
+ ///
+ /// The ERROR packet, used to move to an error state.
+ ///
+ public const string PacketError = "ERROR";
+
+ ///
+ /// The WARNING packet, used to log a warning.
+ ///
+ public const string PacketWarning = "WARNING";
+
+ ///
+ /// Default socket timeout.
+ ///
+ public const int SocketTimeout = 1000;
+
+ ///
+ /// The ASCII decoder.
+ ///
+ private readonly ASCIIEncoding _ascii = new ASCIIEncoding();
+
+ ///
+ /// Used to hold data read from socket.
+ ///
+ private readonly char[] _charBuffer = new char[1024];
+
+ ///
+ /// The parent server.
+ ///
+ private readonly IndicatorServer _parentServer;
+
+ ///
+ /// Used to parse a CSV line(packet) read.
+ ///
+ private readonly ParseCSVLine _parseLine = new ParseCSVLine(CSVFormat.EgFormat);
+
+ ///
+ /// The socket to use. (client)
+ ///
+ private readonly Socket _socket;
+
+ ///
+ /// The actual size of data read.
+ ///
+ private int _actualSize;
+
+ ///
+ /// Current position as we read.
+ ///
+ private int _currentPosition;
+
+ ///
+ /// The number of packets received.
+ ///
+ private int _packets;
+
+ ///
+ /// Construct an indicator link.
+ ///
+ /// The parent server.
+ /// The socket. (client)
+ public IndicatorLink(IndicatorServer node, Socket s)
+ {
+ try
+ {
+ _currentPosition = 0;
+ _actualSize = 0;
+ _parentServer = node;
+ s.Blocking = true;
+ _socket = s;
+ _socket.ReceiveTimeout = SocketTimeout;
+ }
+ catch (IOException ex)
+ {
+ throw new IndicatorError(ex);
+ }
+ }
+
+ ///
+ /// The client socket.
+ ///
+ public Socket ClientSocket
+ {
+ get { return _socket; }
+ }
+
+ ///
+ /// The packet count that we've read.
+ ///
+ public int Packets
+ {
+ get { return _packets; }
+ }
+
+ ///
+ /// The server that created this link.
+ ///
+ public IndicatorServer ParentServer
+ {
+ get { return _parentServer; }
+ }
+
+ ///
+ /// Write a packet, basically a CSV line.
+ ///
+ /// The packet command (type).
+ /// The arguments for this packet.
+ public void WritePacket(String command, Object[] args)
+ {
+ try
+ {
+ var line = new StringBuilder();
+ line.Append("\"");
+ line.Append(command.ToUpper());
+ line.Append("\"");
+ foreach (object t in args)
+ {
+ line.Append(",\"");
+ line.Append(t.ToString());
+ line.Append("\"");
+ }
+ line.Append("\n");
+
+ byte[] b = Encoding.ASCII.GetBytes(line.ToString());
+ _socket.Send(b);
+ }
+ catch (IOException ex)
+ {
+ throw new IndicatorError(ex);
+ }
+ }
+
+ ///
+ /// Read the next block from the socket.
+ ///
+ private void ReadNextBlock()
+ {
+ var buffer = new byte[1024];
+ _currentPosition = _actualSize = 0;
+ _actualSize = _socket.Receive(buffer);
+ _ascii.GetChars(buffer, 0, _actualSize, _charBuffer, 0);
+ }
+
+ ///
+ /// Read the next char from the socket.
+ ///
+ /// The next char.
+ private char ReadNextChar()
+ {
+ if (_actualSize == _currentPosition)
+ {
+ ReadNextBlock();
+ }
+
+ return _charBuffer[_currentPosition++];
+ }
+
+ ///
+ /// Read a packet.
+ ///
+ /// The packet we read.
+ public IndicatorPacket ReadPacket()
+ {
+ try
+ {
+ var line = new StringBuilder();
+
+ for (;;)
+ {
+ char ch = ReadNextChar();
+
+ if (ch != '\n' && ch != '\r')
+ {
+ line.Append(ch);
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ IList list = _parseLine.Parse(line.ToString());
+ _packets++;
+
+ if (list.Count > 0)
+ {
+ list[0] = list[0].ToUpper();
+ }
+
+ EncogLogging.Log(EncogLogging.LevelDebug, "Received Packet: " + line);
+ return new IndicatorPacket(list);
+ }
+ catch (SocketException ex)
+ {
+ // was it just a timeout?
+ if (ex.ErrorCode == 10060)
+ {
+ return null;
+ }
+ throw new IndicatorError(ex);
+ }
+ }
+
+ ///
+ /// Close the socket.
+ ///
+ public void Close()
+ {
+ try
+ {
+ _socket.Close();
+ }
+ catch (IOException)
+ {
+ // ignore, we were trying to close
+ }
+ }
+
+ ///
+ /// Request the specified signals (i.e. HLOC(high, low, etc)).
+ ///
+ /// The data requested.
+ /// True, if blocking is used.
+ public void InitConnection(IList dataSource, bool blocking)
+ {
+ var args = new String[2];
+ args[0] = blocking ? "1" : "0";
+ args[1] = "1";
+ WritePacket(PacketSignals, dataSource.ToArray());
+ WritePacket(PacketInit, args);
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Server/IndicatorPacket.cs b/encog-core-cs/Cloud/Indicator/Server/IndicatorPacket.cs
new file mode 100644
index 00000000..53c477b9
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Server/IndicatorPacket.cs
@@ -0,0 +1,75 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+
+namespace Encog.Cloud.Indicator.Server
+{
+ ///
+ /// An indicator packet.
+ ///
+ public class IndicatorPacket
+ {
+ ///
+ /// The arguments.
+ ///
+ private readonly string[] _args;
+
+ ///
+ /// The command.
+ ///
+ private readonly string _command;
+
+ ///
+ /// Construct a packet from he list of arguments.
+ ///
+ /// The argument list.
+ public IndicatorPacket(IList list)
+ {
+ _command = list[0].ToUpper();
+
+ _args = list.Count == 1 ? new String[0] : new String[list.Count - 1];
+
+ for (int i = 0; i < list.Count - 1; i++)
+ {
+ _args[i] = list[i + 1];
+ }
+ }
+
+ ///
+ /// The command.
+ ///
+ public String Command
+ {
+ get { return _command; }
+ }
+
+ ///
+ /// The arguments.
+ ///
+ public String[] Args
+ {
+ get { return _args; }
+ }
+ }
+}
diff --git a/encog-core-cs/Cloud/Indicator/Server/IndicatorServer.cs b/encog-core-cs/Cloud/Indicator/Server/IndicatorServer.cs
new file mode 100644
index 00000000..b8f1ee0f
--- /dev/null
+++ b/encog-core-cs/Cloud/Indicator/Server/IndicatorServer.cs
@@ -0,0 +1,314 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Threading;
+using Encog.Util.Logging;
+
+namespace Encog.Cloud.Indicator.Server
+{
+ ///
+ /// The Encog Indicator server. This allows the Encog Framework Indicator to be
+ /// used in a trading platform, such as NinjaTrader or MT4/5. The remote indicator
+ /// is always created in whatever native language the trading platform requires. Then
+ /// a socketed connection is made back to Encog.
+ ///
+ public class IndicatorServer
+ {
+ ///
+ /// The default port.
+ ///
+ public const int StandardEncogPort = 5128;
+
+ ///
+ /// The connections that have been made to the server.
+ ///
+ private readonly IList _connections = new List();
+
+ ///
+ /// The indicator factories by name.
+ ///
+ private readonly IDictionary _factoryMap = new Dictionary();
+
+ ///
+ /// All registered listeners.
+ ///
+ private readonly IList _listeners = new List();
+
+ ///
+ /// The port actually being used.
+ ///
+ private readonly int _port;
+
+ ///
+ /// The socket that we are listening on.
+ ///
+ private Socket _listenSocket;
+
+ ///
+ /// True, if the server is running.
+ ///
+ private bool _running;
+
+ ///
+ /// The background thread used to listen.
+ ///
+ private Thread _thread;
+
+ ///
+ /// Construct a server.
+ ///
+ /// The port.
+ public IndicatorServer(int p)
+ {
+ _port = p;
+ }
+
+ ///
+ /// Construct a server, and use the standard port (5128).
+ ///
+ public IndicatorServer()
+ : this(StandardEncogPort)
+ {
+ }
+
+ ///
+ /// The port the server is listening on.
+ ///
+ public int Port
+ {
+ get { return _port; }
+ }
+
+ ///
+ /// The connections.
+ ///
+ public IList Connections
+ {
+ get { return _connections; }
+ }
+
+ ///
+ /// The connection listeners.
+ ///
+ public IList Listeners
+ {
+ get { return _listeners; }
+ }
+
+ ///
+ /// Background thread.
+ ///
+ public void Run()
+ {
+ try
+ {
+ _running = true;
+ _listenSocket.Listen(5);
+ while (_running)
+ {
+ try
+ {
+ EncogLogging.Log(EncogLogging.LevelDebug, "Begin listen");
+ Socket connectionSocket = _listenSocket.Accept();
+ EncogLogging.Log(EncogLogging.LevelDebug, "Connection from: " + connectionSocket.RemoteEndPoint);
+ var link = new IndicatorLink(this, connectionSocket);
+ NotifyListenersConnections(link, true);
+ var hc = new HandleClient(this, link);
+ _connections.Add(hc);
+ var t = new Thread(hc.Run);
+ t.Start();
+ }
+ catch (SocketException)
+ {
+ // just accept timing out
+ Thread.Sleep(100);
+ }
+ catch (IOException ex)
+ {
+ throw new IndicatorError(ex);
+ }
+ }
+
+ try
+ {
+ _listenSocket.Close();
+ }
+ catch (IOException ex)
+ {
+ throw new IndicatorError(ex);
+ }
+ }
+ finally
+ {
+ _running = false;
+ }
+ }
+
+ ///
+ /// Start the server.
+ ///
+ public void Start()
+ {
+ try
+ {
+ _running = true;
+ _listenSocket = new Socket(AddressFamily.InterNetwork,
+ SocketType.Stream, ProtocolType.IP);
+ _listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, _port));
+ _listenSocket.Blocking = false;
+ _thread = new Thread(Run);
+ _thread.Start();
+ }
+ catch (IOException ex)
+ {
+ throw new IndicatorError(ex);
+ }
+ }
+
+ ///
+ /// Shutdown the server.
+ ///
+ public void Shutdown()
+ {
+ _running = false;
+ }
+
+ ///
+ /// Add a listener.
+ ///
+ /// The listener to add.
+ public void AddListener(IIndicatorConnectionListener listener)
+ {
+ _listeners.Add(listener);
+ }
+
+ ///
+ /// Remove a listener.
+ ///
+ /// The listener to remove.
+ public void RemoveListener(IIndicatorConnectionListener listener)
+ {
+ _listeners.Remove(listener);
+ }
+
+ ///
+ /// Clear the listeners.
+ ///
+ public void ClearListeners()
+ {
+ _listeners.Clear();
+ }
+
+ ///
+ /// Notify connection listeners.
+ ///
+ /// The link.
+ /// Is a connection open?
+ public void NotifyListenersConnections(IndicatorLink link, bool hasOpened)
+ {
+ Object[] list = _listeners.ToArray();
+
+ foreach (object t in list)
+ {
+ var listener = (IIndicatorConnectionListener) t;
+ listener.NotifyConnections(link, hasOpened);
+ }
+ }
+
+ ///
+ /// Add an indicator factory.
+ ///
+ /// The factory to add.
+ public void AddIndicatorFactory(IIndicatorFactory ind)
+ {
+ _factoryMap[ind.Name] = ind;
+ }
+
+ ///
+ /// Wait for an indicator to first startup, and then return when that
+ /// indicator completes. Block until all of this completes.
+ ///
+ public void WaitForIndicatorCompletion()
+ {
+ // first wait for at least one indicator to start up
+ while (_connections.Count == 0)
+ {
+ Thread.Sleep(1000);
+ }
+
+ // now wait for indicators to go to zero
+ while (_connections.Count != 0)
+ {
+ Thread.Sleep(1000);
+ }
+
+ // shutdown
+ Shutdown();
+ }
+
+ ///
+ /// Create the specified indicator, if indicatorName is "default" and there is only
+ /// one indicator, then use that indicator.
+ ///
+ /// The name of the indicator.
+ /// The indicator.
+ public IIndicatorListener ResolveIndicator(String indicatorName)
+ {
+ if (_factoryMap.Count == 0)
+ {
+ throw new IndicatorError("No indicators defined.");
+ }
+ if (string.Compare(indicatorName, "default") == 0)
+ {
+ if (_factoryMap.Count > 1)
+ {
+ throw new IndicatorError("Default indicator requested, but more than one indicator defined.");
+ }
+
+ return _factoryMap.Values.First().Create();
+ }
+
+ if (!_factoryMap.ContainsKey(indicatorName))
+ {
+ throw new IndicatorError("Unknown indicator: " + indicatorName);
+ }
+ return _factoryMap[indicatorName].Create();
+ }
+
+ ///
+ /// Wait for the server to shutdown.
+ ///
+ public void WaitForShutdown()
+ {
+ while (_running)
+ {
+ Thread.Sleep(1000);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ConsoleStatusReportable.cs b/encog-core-cs/ConsoleStatusReportable.cs
index e489035d..a67d2381 100644
--- a/encog-core-cs/ConsoleStatusReportable.cs
+++ b/encog-core-cs/ConsoleStatusReportable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/EncogError.cs b/encog-core-cs/EncogError.cs
index 7a8de79c..27d2f9a0 100644
--- a/encog-core-cs/EncogError.cs
+++ b/encog-core-cs/EncogError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/EncogFramework.cs b/encog-core-cs/EncogFramework.cs
index 6a3b8b31..c00f81ad 100644
--- a/encog-core-cs/EncogFramework.cs
+++ b/encog-core-cs/EncogFramework.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,18 +20,14 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
-#if logging
using System;
using System.Collections.Generic;
using Encog.Plugin;
using Encog.Plugin.SystemPlugin;
-
-#endif
-
-
using Encog.Plugin;
using System.Collections.Generic;
using Encog.Plugin.SystemPlugin;
+using Encog.MathUtil.Randomize.Factory;
namespace Encog
{
///
@@ -44,7 +40,7 @@ public class EncogFramework
///
/// The current engog version, this should be read from the properties.
///
- public static string Version = "3.1.0";
+ public static string Version = "3.4.1";
///
/// The platform.
@@ -108,6 +104,11 @@ public static EncogFramework Instance
}
}
+ ///
+ /// Random number factory for use by Encog.
+ ///
+ public IRandomFactory RandomFactory { get; set; }
+
///
/// Get the properties as a Map.
///
@@ -119,6 +120,7 @@ public static EncogFramework Instance
///
private EncogFramework()
{
+ RandomFactory = new BasicRandomFactory();
_properties[EncogVersion] = Version;
_properties[EncogFileVersion] = FileVersion;
diff --git a/encog-core-cs/EncogFramework.cs.orig b/encog-core-cs/EncogFramework.cs.orig
deleted file mode 100644
index 7d69f67e..00000000
--- a/encog-core-cs/EncogFramework.cs.orig
+++ /dev/null
@@ -1,209 +0,0 @@
-//
-// Encog(tm) Core v3.0 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2011 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-#if logging
-using System;
-using System.Collections.Generic;
-using Encog.Plugin;
-using Encog.Plugin.SystemPlugin;
-
-#endif
-
-
-using Encog.Plugin;
-using System.Collections.Generic;
-using Encog.Plugin.SystemPlugin;
-namespace Encog
-{
- ///
- /// Main Encog class, does little more than provide version information.
- /// Also used to hold the ORM session that Encog uses to work with
- /// Hibernate.
- ///
- public class EncogFramework
- {
- ///
- /// The current engog version, this should be read from the properties.
- ///
-<<<<<<< HEAD
- public static string Version = "3.1.0";
-=======
- public static string Version = "3.0.1";
->>>>>>> 93c94b99afa08644abb78f1a6c75eb8fec51fd2e
-
- ///
- /// The platform.
- ///
- public static string PLATFORM = "DotNet";
-
- ///
- /// The current engog file version, this should be read from the properties.
- ///
- private const string FileVersion = "1";
-
-
- ///
- /// The default precision to use for compares.
- ///
- public const int DefaultPrecision = 10;
-
- ///
- /// Default point at which two doubles are equal.
- ///
- public const double DefaultDoubleEqual = 0.0000001;
-
- ///
- /// The version of the Encog JAR we are working with. Given in the form
- /// x.x.x.
- ///
- public const string EncogVersion = "encog.version";
-
- ///
- /// The encog file version. This determines of an encog file can be read.
- /// This is simply an integer, that started with zero and is incramented each
- /// time the format of the encog data file changes.
- ///
- public static string EncogFileVersion = "encog.file.version";
-
- ///
- /// The instance.
- ///
- private static EncogFramework _instance = new EncogFramework();
-
- ///
- /// The current logging plugin.
- ///
- ///
- private IEncogPluginLogging1 _loggingPlugin;
-
- ///
- /// The plugins.
- ///
- ///
- private readonly IList _plugins;
-
- ///
- /// Get the instance to the singleton.
- ///
- public static EncogFramework Instance
- {
- get
- {
- return _instance;
- }
- }
-
- ///
- /// Get the properties as a Map.
- ///
- private readonly IDictionary _properties =
- new Dictionary();
-
- ///
- /// Private constructor.
- ///
- private EncogFramework()
- {
- _properties[EncogVersion] = Version;
- _properties[EncogFileVersion] = FileVersion;
-
- _plugins = new List();
- RegisterPlugin(new SystemLoggingPlugin());
- RegisterPlugin(new SystemMethodsPlugin());
- RegisterPlugin(new SystemTrainingPlugin());
- RegisterPlugin(new SystemActivationPlugin());
- }
-
- ///
- /// The Encog properties. Contains version information.
- ///
- public IDictionary Properties
- {
- get { return _properties; }
- }
-
-
- ///
- /// Shutdown Encog.
- ///
- public void Shutdown()
- {
- }
-
- /// the loggingPlugin
- public IEncogPluginLogging1 LoggingPlugin
- {
- get { return _loggingPlugin; }
- }
-
- ///
- /// Register a plugin. If this plugin provides a core service, such as
- /// calculation or logging, this will remove the old plugin.
- ///
- ///
- /// The plugin to register.
- public void RegisterPlugin(EncogPluginBase plugin)
- {
- // is it not a general plugin?
- if (plugin.PluginServiceType != EncogPluginBaseConst.SERVICE_TYPE_GENERAL)
- {
- if (plugin.PluginServiceType == EncogPluginBaseConst.SERVICE_TYPE_LOGGING)
- {
- // remove the old logging plugin
- if (_loggingPlugin != null)
- {
- _plugins.Remove(_loggingPlugin);
- }
- _loggingPlugin = (IEncogPluginLogging1) plugin;
- }
- }
- // add to the plugins
- _plugins.Add(plugin);
- }
-
- ///
- /// Unregister a plugin. If you unregister the current logging or calc
- /// plugin, a new system one will be created. Encog will crash without a
- /// logging or system plugin.
- ///
- public void UnregisterPlugin(EncogPluginBase plugin)
- {
- // is it a special plugin?
- // if so, replace with the system, Encog will crash without these
- if (plugin == _loggingPlugin)
- {
- _loggingPlugin = new SystemLoggingPlugin();
- }
-
- // remove it
- _plugins.Remove(plugin);
- }
-
- ///
- /// The plugins.
- ///
- public IList Plugins
- {
- get { return _plugins; }
- }
- }
-}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationBiPolar.cs b/encog-core-cs/Engine/Network/Activation/ActivationBiPolar.cs
index a62e6387..7f9c5697 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationBiPolar.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationBiPolar.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -54,9 +54,12 @@ public virtual double DerivativeFunction(double b, double a)
/// Return true, bipolar has a 1 for derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationBipolarSteepenedSigmoid.cs b/encog-core-cs/Engine/Network/Activation/ActivationBipolarSteepenedSigmoid.cs
new file mode 100644
index 00000000..0c1da28f
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/ActivationBipolarSteepenedSigmoid.cs
@@ -0,0 +1,111 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Engine.Network.Activation
+{
+ ///
+ /// The bipolar sigmoid activation function is like the regular sigmoid activation function,
+ /// except Bipolar sigmoid activation function. TheOutput range is -1 to 1 instead of the more normal 0 to 1.
+ ///
+ /// This activation is typically part of a CPPN neural network, such as
+ /// HyperNEAT.
+ ///
+ /// It was developed by Ken Stanley while at The University of Texas at Austin.
+ /// http://www.cs.ucf.edu/~kstanley/
+ ///
+ [Serializable]
+ public class ActivationBipolarSteepenedSigmoid : IActivationFunction
+ {
+ ///
+ /// The parameters.
+ ///
+ private double[] _params;
+
+ ///
+ public void ActivationFunction(double[] d, int start, int size)
+ {
+ for (int i = start; i < start + size; i++)
+ {
+ if (d[i] < -1.0)
+ {
+ d[i] = -1.0;
+ }
+ if (d[i] > 1.0)
+ {
+ d[i] = 1.0;
+ }
+ }
+ }
+
+ ///
+ public double DerivativeFunction(double b, double a)
+ {
+ return 1;
+ }
+
+ ///
+ public bool HasDerivative
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+ ///
+ public Object Clone()
+ {
+ return new ActivationBipolarSteepenedSigmoid();
+ }
+
+ ///
+ public virtual String[] ParamNames
+ {
+ get
+ {
+ String[] result = { };
+ return result;
+ }
+ }
+
+
+ ///
+ public virtual double[] Params
+ {
+ get { return _params; }
+ }
+
+ ///
+ public string FactoryCode
+ {
+ get
+ {
+ return null;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationClippedLinear.cs b/encog-core-cs/Engine/Network/Activation/ActivationClippedLinear.cs
new file mode 100644
index 00000000..0af80637
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/ActivationClippedLinear.cs
@@ -0,0 +1,95 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Engine.Network.Activation
+{
+ ///
+ /// Linear activation function that bounds the output to [-1,+1]. This
+ /// activation is typically part of a CPPN neural network, such as
+ /// HyperNEAT.
+ ///
+ /// The idea for this activation function was developed by Ken Stanley, of
+ /// the University of Texas at Austin.
+ /// http://www.cs.ucf.edu/~kstanley/
+ ///
+ public class ActivationClippedLinear : IActivationFunction
+ {
+ ///
+ /// The parameters.
+ ///
+ private double[] _params;
+
+ ///
+ public void ActivationFunction(double[] d, int start, int size)
+ {
+ for (int i = start; i < start + size; i++)
+ {
+ d[i] = (2.0 / (1.0 + Math.Exp(-4.9 * d[i]))) - 1.0;
+ }
+ }
+
+ ///
+ public double DerivativeFunction(double b, double a)
+ {
+ return 1;
+ }
+
+ ///
+ public Object Clone()
+ {
+ return new ActivationBipolarSteepenedSigmoid();
+ }
+
+ ///
+ public virtual String[] ParamNames
+ {
+ get
+ {
+ String[] result = { };
+ return result;
+ }
+ }
+
+
+ ///
+ public virtual double[] Params
+ {
+ get { return _params; }
+ }
+
+ ///
+ public string FactoryCode
+ {
+ get
+ {
+ return null;
+ }
+ }
+
+ public bool HasDerivative { get { return true; } }
+ }
+}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationCompetitive.cs b/encog-core-cs/Engine/Network/Activation/ActivationCompetitive.cs
index 13af70b1..4fee5942 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationCompetitive.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationCompetitive.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -154,9 +154,12 @@ public virtual double[] Params
/// False, indication that no derivative is available for thisfunction.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return false;
+ get
+ {
+ return false;
+ }
}
}
}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationElliott.cs b/encog-core-cs/Engine/Network/Activation/ActivationElliott.cs
index b48e6610..532acd42 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationElliott.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationElliott.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ namespace Encog.Engine.Network.Activation
/// Elliott, D.L. "A better activation function for artificial neural networks", 1993
/// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.46.7204&rep=rep1&type=pdf
///
+ [Serializable]
public class ActivationElliott : IActivationFunction
{
///
@@ -99,9 +100,12 @@ public double[] Params
/// Return true, Elliott activation has a derivative.
///
/// Return true, Elliott activation has a derivative.
- public bool HasDerivative()
+ public bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
#endregion
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationElliottSymmetric.cs b/encog-core-cs/Engine/Network/Activation/ActivationElliottSymmetric.cs
index 7939623a..82864ee1 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationElliottSymmetric.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationElliottSymmetric.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ namespace Encog.Engine.Network.Activation
/// Elliott, D.L. "A better activation function for artificial neural networks", 1993
/// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.46.7204&rep=rep1&type=pdf
///
+ [Serializable]
public class ActivationElliottSymmetric : IActivationFunction
{
///
@@ -100,9 +101,12 @@ public double[] Params
/// Return true, Elliott activation has a derivative.
///
/// Return true, Elliott activation has a derivative.
- public bool HasDerivative()
+ public bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
#endregion
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationGaussian.cs b/encog-core-cs/Engine/Network/Activation/ActivationGaussian.cs
index 73f85439..a34b11c7 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationGaussian.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationGaussian.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -22,130 +22,74 @@
//
using System;
using Encog.MathUtil;
+using Encog.ML.Factory;
namespace Encog.Engine.Network.Activation
{
///
- /// An activation function based on the gaussian function.
+ /// An activation function based on the Gaussian function. The output range is
+ /// between 0 and 1. This activation function is used mainly for the HyperNeat
+ /// implementation.
+ ///
+ /// A derivative is provided, so this activation function can be used with
+ /// propagation training. However, its primary intended purpose is for
+ /// HyperNeat. The derivative was obtained with the R statistical package.
+ ///
+ /// If you are looking to implement a RBF-based neural network, see the
+ /// RBFNetwork class.
+ ///
+ /// The idea for this activation function was developed by Ken Stanley, of
+ /// the University of Texas at Austin.
+ /// http://www.cs.ucf.edu/~kstanley/
///
[Serializable]
public class ActivationGaussian : IActivationFunction
{
- ///
- /// The offset to the parameter that holds the width.
- ///
- ///
- public const int ParamGaussianCenter = 0;
-
- ///
- /// The offset to the parameter that holds the peak.
- ///
- ///
- public const int ParamGaussianPeak = 1;
-
- ///
- /// The offset to the parameter that holds the width.
- ///
- ///
- public const int ParamGaussianWidth = 2;
-
///
/// The parameters.
///
- ///
- private readonly double[] _paras;
+ private double[] _params;
///
- /// Create an empty activation gaussian.
+ /// Construct the activation function.
///
public ActivationGaussian()
{
+ _params = new double[0];
}
- ///
- /// Create a gaussian activation function.
- ///
- ///
- /// The center of the curve.
- /// The peak of the curve.
- /// The width of the curve.
- public ActivationGaussian(double center, double peak,
- double width)
- {
- _paras = new double[3];
- _paras[ParamGaussianCenter] = center;
- _paras[ParamGaussianPeak] = peak;
- _paras[ParamGaussianWidth] = width;
- }
-
- ///
- /// Clone the object.
- ///
- /// The cloned object.
+ ///
public object Clone()
{
- return new ActivationGaussian(Center, Peak,
- Width);
+ return new ActivationGaussian();
}
-
- ///
- /// The width of the function.
- ///
- private double Width
+ /**
+ * @return Return true, gaussian has a derivative.
+ */
+ public bool HasDerivative
{
- get { return Params[ParamGaussianWidth]; }
- }
-
-
- ///
- /// The center of the function.
- ///
- private double Center
- {
- get { return Params[ParamGaussianCenter]; }
- }
-
-
- ///
- /// The peak of the function.
- ///
- private double Peak
- {
- get { return Params[ParamGaussianPeak]; }
- }
-
-
- ///
- /// Return true, gaussian has a derivative.
- ///
- /// Return true, gaussian has a derivative.
- public virtual bool HasDerivative()
- {
- return true;
+ get
+ {
+ return true;
+ }
}
///
- public virtual void ActivationFunction(double[] x, int start,
- int size)
+ public void ActivationFunction(double[] x, int start, int size)
{
+
for (int i = start; i < start + size; i++)
{
- x[i] = _paras[ParamGaussianPeak]
- *BoundMath
- .Exp(-Math.Pow(x[i]
- - _paras[ParamGaussianCenter], 2)
- /(2.0d*_paras[ParamGaussianWidth]*_paras[ParamGaussianWidth]));
+ x[i] = BoundMath.Exp(-Math.Pow(2.5 * x[i], 2.0));
}
+
}
///
- public virtual double DerivativeFunction(double b, double a)
+ public double DerivativeFunction(double b, double a)
{
- double width = _paras[ParamGaussianWidth];
- double peak = _paras[ParamGaussianPeak];
- return Math.Exp(-0.5d*width*width*b*b)*peak*width*width
- *(width*width*b*b - 1);
+ return Math.Exp(Math.Pow(2.5 * b, 2.0) * 12.5 * b);
}
///
@@ -153,19 +97,17 @@ public virtual String[] ParamNames
{
get
{
- String[] result = {"center", "peak", "width"};
+ String[] result = { };
return result;
}
}
- ///
- /// {@inheritDoc}
- ///
- ///
+ ///
public virtual double[] Params
{
- get { return _paras; }
+ get { return _params; }
}
+
}
}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationLOG.cs b/encog-core-cs/Engine/Network/Activation/ActivationLOG.cs
index c11582a1..85cea5e7 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationLOG.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationLOG.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -62,9 +62,12 @@ public object Clone()
/// Return true, log has a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationLinear.cs b/encog-core-cs/Engine/Network/Activation/ActivationLinear.cs
index c97c02c1..2f8a99fc 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationLinear.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationLinear.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -60,9 +60,12 @@ public object Clone()
/// Return true, linear has a 1 derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationRamp.cs b/encog-core-cs/Engine/Network/Activation/ActivationRamp.cs
index abcea74d..2d8e7866 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationRamp.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationRamp.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -148,9 +148,12 @@ public double ThresholdLow
/// True, as this function does have a derivative.
///
/// True, as this function does have a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationReLU.cs b/encog-core-cs/Engine/Network/Activation/ActivationReLU.cs
new file mode 100644
index 00000000..5dabd52d
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/ActivationReLU.cs
@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Engine.Network.Activation
+{
+ ///
+ /// ReLU activation function. This function has a high and low threshold. If
+ /// the high threshold is exceeded a fixed value is returned.Likewise, if the
+ /// low value is exceeded another fixed value is returned.
+ ///
+ [Serializable]
+ public class ActivationReLU: IActivationFunction
+ {
+
+ ///
+ /// The ramp low threshold parameter.
+ ///
+ public const int PARAM_RELU_LOW_THRESHOLD = 0;
+
+ ///
+ /// The ramp low parameter.
+ ///
+ public const int PARAM_RELU_LOW = 0;
+
+ ///
+ /// The parameters.
+ ///
+ private readonly double[] _params;
+
+
+
+
+ ///
+ /// Default constructor.
+ ///
+ public ActivationReLU():
+ this(0,0)
+ {
+ }
+
+ ///
+ /// Construct a ramp activation function.
+ ///
+ /// The low threshold value.
+ /// The low value, replaced if the low threshold is exceeded.
+ public ActivationReLU(double thresholdLow, double low)
+ {
+ _params = new double[2];
+ _params[ActivationReLU.PARAM_RELU_LOW_THRESHOLD] = thresholdLow;
+ _params[ActivationReLU.PARAM_RELU_LOW] = low;
+ }
+
+
+
+ /// Return true, Rectifier has a derivative.
+ public bool HasDerivative
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+ ///
+ /// Clone the object.
+ ///
+ /// The cloned object.
+ public object Clone()
+ {
+ return new ActivationReLU();
+ }
+
+ ///
+ public void ActivationFunction(double[] x, int start, int size)
+ {
+ for (int i = start; i < start + size; i++)
+ {
+ if (x[i] <= _params[ActivationReLU.PARAM_RELU_LOW_THRESHOLD]) {
+ x[i] = _params[ActivationReLU.PARAM_RELU_LOW];
+ }
+}
+ }
+
+ ///
+ public double DerivativeFunction(double b, double a)
+ {
+ return 1 / (1 + Math.Pow(Math.E, -a));
+ }
+
+ ///
+ public virtual String[] ParamNames
+ {
+ get
+ {
+ String[] result = { };
+ return result;
+ }
+ }
+
+ ///
+ public virtual double[] Params
+ {
+ get { return _params; }
+ }
+
+ }
+}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationSIN.cs b/encog-core-cs/Engine/Network/Activation/ActivationSIN.cs
index 8be9d2cc..2ca19a66 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationSIN.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationSIN.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -26,7 +26,13 @@
namespace Encog.Engine.Network.Activation
{
///
- /// An activation function based on the sin function.
+ /// An activation function based on the sin function, with a double period.
+ ///
+ /// This activation is typically part of a CPPN neural network, such as
+ /// HyperNEAT.
+ ///
+ /// It was developed by Ken Stanley while at The University of Texas at Austin.
+ /// http://www.cs.ucf.edu/~kstanley/
///
[Serializable]
public class ActivationSIN : IActivationFunction
@@ -57,9 +63,12 @@ public object Clone()
/// Return true, sin has a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
@@ -68,14 +77,14 @@ public virtual void ActivationFunction(double[] x, int start,
{
for (int i = start; i < start + size; i++)
{
- x[i] = BoundMath.Sin(x[i]);
+ x[i] = BoundMath.Sin(2.0 * x[i]);
}
}
///
public virtual double DerivativeFunction(double b, double a)
{
- return BoundMath.Cos(b);
+ return BoundMath.Cos(2.0 * b);
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationSigmoid.cs b/encog-core-cs/Engine/Network/Activation/ActivationSigmoid.cs
index 182cdccf..d7315d4a 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationSigmoid.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationSigmoid.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -58,9 +58,12 @@ public object Clone()
}
/// True, sigmoid has a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationSmoothReLU.cs b/encog-core-cs/Engine/Network/Activation/ActivationSmoothReLU.cs
new file mode 100644
index 00000000..bc0756ca
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/ActivationSmoothReLU.cs
@@ -0,0 +1,76 @@
+using System;
+using Encog.MathUtil;
+
+namespace Encog.Engine.Network.Activation
+{
+ ///
+ /// Smooth ReLU.
+ /// https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
+ ///
+ [Serializable]
+ public class ActivationSmoothReLU : IActivationFunction
+ {
+ ///
+ /// The parameters.
+ ///
+ private readonly double[] _paras;
+
+ ///
+ /// Construct a basic Rectifier, slope of 1.
+ ///
+ public ActivationSmoothReLU()
+ {
+ _paras = new double[0];
+ }
+
+ /// Return true, Rectifier has a derivative.
+ public bool HasDerivative
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+ ///
+ /// Clone the object.
+ ///
+ /// The cloned object.
+ public object Clone()
+ {
+ return new ActivationSmoothReLU();
+ }
+
+ ///
+ public void ActivationFunction(double[] x, int start, int size)
+ {
+ for (int i = start; i < start + size; i++)
+ {
+ x[i] = Math.Log(1.0 + Math.Pow(Math.E, x[i]));
+ }
+ }
+
+ ///
+ public double DerivativeFunction(double b, double a)
+ {
+ return 1 / (1 + Math.Pow(Math.E, -a));
+ }
+
+ ///
+ public virtual String[] ParamNames
+ {
+ get
+ {
+ String[] result = { };
+ return result;
+ }
+ }
+
+ ///
+ public virtual double[] Params
+ {
+ get { return _paras; }
+ }
+
+ }
+}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationSoftMax.cs b/encog-core-cs/Engine/Network/Activation/ActivationSoftMax.cs
index 481baa47..449781ad 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationSoftMax.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationSoftMax.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -56,9 +56,12 @@ public object Clone()
}
/// Return false, softmax has no derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationSteepenedSigmoid.cs b/encog-core-cs/Engine/Network/Activation/ActivationSteepenedSigmoid.cs
new file mode 100644
index 00000000..66dddaf7
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/ActivationSteepenedSigmoid.cs
@@ -0,0 +1,119 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Engine.Network.Activation
+{
+ ///
+ /// The Steepened Sigmoid is an activation function typically used with NEAT.
+ ///
+ /// Valid derivative calculated with the R package, so this does work with
+ /// non-NEAT networks too.
+ ///
+ /// It was developed by Ken Stanley while at The University of Texas at Austin.
+ /// http://www.cs.ucf.edu/~kstanley/
+ ///
+ [Serializable]
+ public class ActivationSteepenedSigmoid : IActivationFunction
+ {
+ ///
+ /// The parameters.
+ ///
+ private double[] _params;
+
+ ///
+ /// Construct a steepend sigmoid activation function.
+ ///
+ public ActivationSteepenedSigmoid()
+ {
+ _params = new double[0];
+ }
+
+ ///
+ public void ActivationFunction(double[] x, int start,
+ int size)
+ {
+ for (int i = start; i < start + size; i++)
+ {
+ x[i] = 1.0 / (1.0 + Math.Exp(-4.9 * x[i]));
+ }
+ }
+
+ ///
+ /// Clone the object.
+ ///
+ /// The object cloned;
+ public Object Clone()
+ {
+ return new ActivationSteepenedSigmoid();
+ }
+
+ ///
+ public double DerivativeFunction(double b, double a)
+ {
+ double s = Math.Exp(-4.9 * a);
+ return Math.Pow(s * 4.9 / (1 + s), 2);
+ }
+
+ ///
+ public virtual String[] ParamNames
+ {
+ get
+ {
+ String[] result = { };
+ return result;
+ }
+ }
+
+
+ ///
+ public virtual double[] Params
+ {
+ get { return _params; }
+ }
+
+ ///
+ /// Return true, steepend sigmoid has a derivative.
+ ///
+ public virtual bool HasDerivative
+ {
+ get
+ {
+ return true;
+ }
+ }
+
+
+ ///
+ public string FactoryCode
+ {
+ get
+ {
+ return null;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationStep.cs b/encog-core-cs/Engine/Network/Activation/ActivationStep.cs
index fa804c48..edb38945 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationStep.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationStep.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -127,9 +127,12 @@ public object Clone()
}
/// Returns true, this activation function has a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/ActivationTANH.cs b/encog-core-cs/Engine/Network/Activation/ActivationTANH.cs
index 0d2cb7c9..23105aa8 100644
--- a/encog-core-cs/Engine/Network/Activation/ActivationTANH.cs
+++ b/encog-core-cs/Engine/Network/Activation/ActivationTANH.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -60,9 +60,12 @@ public object Clone()
/// Return true, TANH has a derivative.
- public virtual bool HasDerivative()
+ public virtual bool HasDerivative
{
- return true;
+ get
+ {
+ return true;
+ }
}
///
diff --git a/encog-core-cs/Engine/Network/Activation/IActivationFunction.cs b/encog-core-cs/Engine/Network/Activation/IActivationFunction.cs
index 29b35bb3..0adf0989 100644
--- a/encog-core-cs/Engine/Network/Activation/IActivationFunction.cs
+++ b/encog-core-cs/Engine/Network/Activation/IActivationFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -81,7 +81,9 @@ public interface IActivationFunction : ICloneable
double DerivativeFunction(double b, double a);
- /// Return true if this function has a derivative.
- bool HasDerivative();
+ ///
+ /// Does this activation function have a derivative.
+ ///
+ bool HasDerivative { get; }
}
}
diff --git a/encog-core-cs/Engine/Network/Activation/IActivationFunctionCL.cs b/encog-core-cs/Engine/Network/Activation/IActivationFunctionCL.cs
new file mode 100644
index 00000000..77a98391
--- /dev/null
+++ b/encog-core-cs/Engine/Network/Activation/IActivationFunctionCL.cs
@@ -0,0 +1,100 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Engine.Network.Activation
+{
+
+ ///
+ /// This interface allows various activation functions to be used with the neural
+ /// network. Activation functions are applied to the output from each layer of a
+ /// neural network. Activation functions scale the output into the desired range.
+ /// Methods are provided both to process the activation function, as well as the
+ /// derivative of the function. Some training algorithms, particularly back
+ /// propagation, require that it be possible to take the derivative of the
+ /// activation function.
+ /// Not all activation functions support derivatives. If you implement an
+ /// activation function that is not derivable then an exception should be thrown
+ /// inside of the derivativeFunction method implementation.
+ /// Non-derivable activation functions are perfectly valid, they simply cannot be
+ /// used with every training algorithm.
+ ///
+ ///
+ public interface IActivationFunctionCL : ICloneable
+ {
+ /// The params for this activation function.
+ double[] Params { get; }
+
+ /// The names of the parameters.
+ String[] ParamNames { get; }
+
+ ///
+ /// Implements the activation function. The array is modified according to
+ /// the activation function being used. See the class description for more
+ /// specific information on this type of activation function.
+ ///
+ ///
+ /// The input array to the activation function.
+ /// The starting index.
+ /// The number of values to calculate.
+ void ActivationFunction(double[] d, int start, int size);
+
+ ///
+ /// Calculate the derivative. For performance reasons two numbers are provided.
+ /// First, the value "b" is simply the number that we would like to calculate
+ /// the derivative of.
+ ///
+ /// Second, the value "a", which is the value returned by the activation function,
+ /// when presented with "b".
+ ///
+ /// We use two values because some of the most common activation functions make
+ /// use of the result of the activation function. It is bad for performance to
+ /// calculate this value twice. Yet, not all derivatives are calculated this way.
+ /// By providing both the value before the activation function is applied ("b"),
+ /// and after the activation function is applied("a"), the class can be constructed
+ /// to use whichever value will be the most efficient.
+ ///
+ /// The number to calculate the derivative of, the number "before" the
+ /// activation function was applied.
+ /// The number "after" an activation function has been applied.
+ /// @return The derivative.
+ ///
+ double DerivativeFunction(double b, double a);
+
+
+ /// Return true if this function has a derivative.
+ bool HasDerivative();
+
+
+ ///
+ /// Returns the OpenCL expression for this activation function.
+ ///
+ ///
+ /// True if we want the derivative, false otherwise.
+ /// The OpenCL expression for this activation function.
+ String GetOpenCLExpression(bool derivative);
+ }
+}
diff --git a/encog-core-cs/IStatusReportable.cs b/encog-core-cs/IStatusReportable.cs
index c3bb0100..46d565ec 100644
--- a/encog-core-cs/IStatusReportable.cs
+++ b/encog-core-cs/IStatusReportable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Anneal/SimulatedAnnealing.cs b/encog-core-cs/ML/Anneal/SimulatedAnnealing.cs
index 92025794..21e0df9a 100644
--- a/encog-core-cs/ML/Anneal/SimulatedAnnealing.cs
+++ b/encog-core-cs/ML/Anneal/SimulatedAnnealing.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/BasicML.cs b/encog-core-cs/ML/BasicML.cs
index ba2c2677..a1bfa2fa 100644
--- a/encog-core-cs/ML/BasicML.cs
+++ b/encog-core-cs/ML/BasicML.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BIF/BIFDefinition.cs b/encog-core-cs/ML/Bayesian/BIF/BIFDefinition.cs
index cc0ccc17..1cc6a955 100644
--- a/encog-core-cs/ML/Bayesian/BIF/BIFDefinition.cs
+++ b/encog-core-cs/ML/Bayesian/BIF/BIFDefinition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BIF/BIFVariable.cs b/encog-core-cs/ML/Bayesian/BIF/BIFVariable.cs
index 1a3a9eda..a8c50828 100644
--- a/encog-core-cs/ML/Bayesian/BIF/BIFVariable.cs
+++ b/encog-core-cs/ML/Bayesian/BIF/BIFVariable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BayesianChoice.cs b/encog-core-cs/ML/Bayesian/BayesianChoice.cs
index 89c2ec0c..27704170 100644
--- a/encog-core-cs/ML/Bayesian/BayesianChoice.cs
+++ b/encog-core-cs/ML/Bayesian/BayesianChoice.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BayesianError.cs b/encog-core-cs/ML/Bayesian/BayesianError.cs
index 6630b083..9d69ca83 100644
--- a/encog-core-cs/ML/Bayesian/BayesianError.cs
+++ b/encog-core-cs/ML/Bayesian/BayesianError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BayesianEvent.cs b/encog-core-cs/ML/Bayesian/BayesianEvent.cs
index 5425c2d4..bd1c1254 100644
--- a/encog-core-cs/ML/Bayesian/BayesianEvent.cs
+++ b/encog-core-cs/ML/Bayesian/BayesianEvent.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/BayesianNetwork.cs b/encog-core-cs/ML/Bayesian/BayesianNetwork.cs
index ef5acbf8..cb156416 100644
--- a/encog-core-cs/ML/Bayesian/BayesianNetwork.cs
+++ b/encog-core-cs/ML/Bayesian/BayesianNetwork.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/EventType.cs b/encog-core-cs/ML/Bayesian/EventType.cs
index 2e3ca892..bb4bfc00 100644
--- a/encog-core-cs/ML/Bayesian/EventType.cs
+++ b/encog-core-cs/ML/Bayesian/EventType.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Parse/ParseProbability.cs b/encog-core-cs/ML/Bayesian/Parse/ParseProbability.cs
index d76a9113..4b60cad3 100644
--- a/encog-core-cs/ML/Bayesian/Parse/ParseProbability.cs
+++ b/encog-core-cs/ML/Bayesian/Parse/ParseProbability.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Parse/ParsedChoice.cs b/encog-core-cs/ML/Bayesian/Parse/ParsedChoice.cs
index 3787c75b..c304a0c4 100644
--- a/encog-core-cs/ML/Bayesian/Parse/ParsedChoice.cs
+++ b/encog-core-cs/ML/Bayesian/Parse/ParsedChoice.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Parse/ParsedEvent.cs b/encog-core-cs/ML/Bayesian/Parse/ParsedEvent.cs
index 53a663bd..bfbf8046 100644
--- a/encog-core-cs/ML/Bayesian/Parse/ParsedEvent.cs
+++ b/encog-core-cs/ML/Bayesian/Parse/ParsedEvent.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -131,7 +131,7 @@ public IList ChoiceList
}
///
- public String ToString()
+ public override String ToString()
{
StringBuilder result = new StringBuilder();
result.Append("[ParsedEvent:label=");
diff --git a/encog-core-cs/ML/Bayesian/Parse/ParsedProbability.cs b/encog-core-cs/ML/Bayesian/Parse/ParsedProbability.cs
index 212f7fe6..4087b501 100644
--- a/encog-core-cs/ML/Bayesian/Parse/ParsedProbability.cs
+++ b/encog-core-cs/ML/Bayesian/Parse/ParsedProbability.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/PersistBayes.cs b/encog-core-cs/ML/Bayesian/PersistBayes.cs
index 4ab3c708..269a3797 100644
--- a/encog-core-cs/ML/Bayesian/PersistBayes.cs
+++ b/encog-core-cs/ML/Bayesian/PersistBayes.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Query/BasicQuery.cs b/encog-core-cs/ML/Bayesian/Query/BasicQuery.cs
index 5d7d93d4..137a0342 100644
--- a/encog-core-cs/ML/Bayesian/Query/BasicQuery.cs
+++ b/encog-core-cs/ML/Bayesian/Query/BasicQuery.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Query/Enumeration/EnumerationQuery.cs b/encog-core-cs/ML/Bayesian/Query/Enumeration/EnumerationQuery.cs
index 01ed5961..4d727604 100644
--- a/encog-core-cs/ML/Bayesian/Query/Enumeration/EnumerationQuery.cs
+++ b/encog-core-cs/ML/Bayesian/Query/Enumeration/EnumerationQuery.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Query/EventState.cs b/encog-core-cs/ML/Bayesian/Query/EventState.cs
index 3ad79fe8..bbddc194 100644
--- a/encog-core-cs/ML/Bayesian/Query/EventState.cs
+++ b/encog-core-cs/ML/Bayesian/Query/EventState.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Query/IBayesianQuery.cs b/encog-core-cs/ML/Bayesian/Query/IBayesianQuery.cs
index 47594197..e50668bc 100644
--- a/encog-core-cs/ML/Bayesian/Query/IBayesianQuery.cs
+++ b/encog-core-cs/ML/Bayesian/Query/IBayesianQuery.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Query/Sample/SamplingQuery.cs b/encog-core-cs/ML/Bayesian/Query/Sample/SamplingQuery.cs
index 66a538ce..a80592ac 100644
--- a/encog-core-cs/ML/Bayesian/Query/Sample/SamplingQuery.cs
+++ b/encog-core-cs/ML/Bayesian/Query/Sample/SamplingQuery.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Table/BayesianTable.cs b/encog-core-cs/ML/Bayesian/Table/BayesianTable.cs
index 98e3fb2e..6fedfa77 100644
--- a/encog-core-cs/ML/Bayesian/Table/BayesianTable.cs
+++ b/encog-core-cs/ML/Bayesian/Table/BayesianTable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -188,7 +188,7 @@ public int GenerateRandom(params int[] args)
}
///
- public String ToString()
+ public override String ToString()
{
StringBuilder result = new StringBuilder();
foreach (TableLine line in _lines)
diff --git a/encog-core-cs/ML/Bayesian/Table/TableLine.cs b/encog-core-cs/ML/Bayesian/Table/TableLine.cs
index 77bf5dea..65337961 100644
--- a/encog-core-cs/ML/Bayesian/Table/TableLine.cs
+++ b/encog-core-cs/ML/Bayesian/Table/TableLine.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/BayesianInit.cs b/encog-core-cs/ML/Bayesian/Training/BayesianInit.cs
index 4116dd7f..354473a1 100644
--- a/encog-core-cs/ML/Bayesian/Training/BayesianInit.cs
+++ b/encog-core-cs/ML/Bayesian/Training/BayesianInit.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Estimator/EstimatorNone.cs b/encog-core-cs/ML/Bayesian/Training/Estimator/EstimatorNone.cs
index a1dabfcb..0a2000ac 100644
--- a/encog-core-cs/ML/Bayesian/Training/Estimator/EstimatorNone.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Estimator/EstimatorNone.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Estimator/IBayesEstimator.cs b/encog-core-cs/ML/Bayesian/Training/Estimator/IBayesEstimator.cs
index 8d4d2dff..a2160246 100644
--- a/encog-core-cs/ML/Bayesian/Training/Estimator/IBayesEstimator.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Estimator/IBayesEstimator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Estimator/SimpleEstimator.cs b/encog-core-cs/ML/Bayesian/Training/Estimator/SimpleEstimator.cs
index da2ba8d3..9093c534 100644
--- a/encog-core-cs/ML/Bayesian/Training/Estimator/SimpleEstimator.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Estimator/SimpleEstimator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Search/IBayesSearch.cs b/encog-core-cs/ML/Bayesian/Training/Search/IBayesSearch.cs
index ece77b47..3448c0ea 100644
--- a/encog-core-cs/ML/Bayesian/Training/Search/IBayesSearch.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Search/IBayesSearch.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Search/SearchNone.cs b/encog-core-cs/ML/Bayesian/Training/Search/SearchNone.cs
index 51e4b71c..a6e7ae33 100644
--- a/encog-core-cs/ML/Bayesian/Training/Search/SearchNone.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Search/SearchNone.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/Search/k2/SearchK2.cs b/encog-core-cs/ML/Bayesian/Training/Search/k2/SearchK2.cs
index 833ce8d9..89a0ab9f 100644
--- a/encog-core-cs/ML/Bayesian/Training/Search/k2/SearchK2.cs
+++ b/encog-core-cs/ML/Bayesian/Training/Search/k2/SearchK2.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Bayesian/Training/TrainBayesian.cs b/encog-core-cs/ML/Bayesian/Training/TrainBayesian.cs
index 97e6e6d5..aa801e7d 100644
--- a/encog-core-cs/ML/Bayesian/Training/TrainBayesian.cs
+++ b/encog-core-cs/ML/Bayesian/Training/TrainBayesian.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLComplexData.cs b/encog-core-cs/ML/Data/Basic/BasicMLComplexData.cs
index 81f5f53a..c2e4bd2f 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLComplexData.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLComplexData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -194,7 +194,7 @@ public int Count
#endregion
///
- public String ToString()
+ public override String ToString()
{
var builder = new StringBuilder("[");
builder.Append(GetType().Name);
@@ -219,5 +219,11 @@ public ICentroid CreateCentroid()
{
return null;
}
- }
+
+ public void CopyTo(double[] target, int targetIndex, int count)
+ {
+ for(int i = 0; i < count; i++)
+ target[i + targetIndex] = _data[i].Real;
+ }
+ }
}
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLData.cs b/encog-core-cs/ML/Data/Basic/BasicMLData.cs
index 9e75e836..5d9d210e 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLData.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -32,21 +32,22 @@ namespace Encog.ML.Data.Basic
/// data in an array.
///
[Serializable]
- public class BasicMLData : IMLData
+ public class BasicMLData: IMLDataModifiable
{
- private double[] _data;
+ protected double[] _data;
///
/// Construct this object with the specified data.
///
/// The data to construct this object with.
- public BasicMLData(double[] d)
- : this(d.Length)
+ public BasicMLData(double[] d, bool copy = true)
{
- for (int i = 0; i < d.Length; i++)
- {
- _data[i] = d[i];
- }
+ if(copy)
+ {
+ _data = new double[d.Length];
+ EngineArray.ArrayCopy(d, _data);
+ }
+ else _data = d;
}
@@ -76,7 +77,6 @@ public virtual double this[int x]
public virtual double[] Data
{
get { return _data; }
- set { _data = value; }
}
///
@@ -153,7 +153,7 @@ public IMLData Plus(IMLData o)
/// The result.
public IMLData Times(double d)
{
- IMLData result = new BasicMLData(Count);
+ var result = new BasicMLData(Count);
for (int i = 0; i < Count; i++)
result[i] = this[i] * d;
@@ -171,12 +171,16 @@ public IMLData Minus(IMLData o)
if (Count != o.Count)
throw new EncogError("Counts must match.");
- IMLData result = new BasicMLData(Count);
+ var result = new BasicMLData(Count);
for (int i = 0; i < Count; i++)
result[i] = this[i] - o[i];
return result;
}
- }
+ public void CopyTo(double[] target, int targetIndex, int count)
+ {
+ EngineArray.ArrayCopy(_data, 0, target, targetIndex, count);
+ }
+ }
}
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLDataCentroid.cs b/encog-core-cs/ML/Data/Basic/BasicMLDataCentroid.cs
index 98eb2a98..143d93b6 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLDataCentroid.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLDataCentroid.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -33,7 +33,12 @@ public class BasicMLDataCentroid : ICentroid
///
/// The value this centroid is based on.
///
- private BasicMLData value;
+ private readonly BasicMLData _value;
+
+ ///
+ /// How many items have been added to the centroid.
+ ///
+ private int _size;
///
/// Construct the centroid.
@@ -41,33 +46,31 @@ public class BasicMLDataCentroid : ICentroid
/// The object to base the centroid on.
public BasicMLDataCentroid(IMLData o)
{
- this.value = (BasicMLData)o.Clone();
+ this._value = (BasicMLData)o.Clone();
+ _size = 1;
}
///
public void Add(IMLData d)
{
- double[] a = d.Data;
-
- for (int i = 0; i < value.Count; i++)
- value.Data[i] =
- ((value.Data[i] * value.Count + a[i]) / (value.Count + 1));
+ for (int i = 0; i < _value.Count; i++)
+ _value.Data[i] = ((_value.Data[i] * _size + d[i]) / (_size + 1));
+ _size++;
}
///
public void Remove(IMLData d)
{
- double[] a = d.Data;
-
- for (int i = 0; i < value.Count; i++)
- value[i] =
- ((value[i] * value.Count - a[i]) / (value.Count - 1));
+ for (int i = 0; i < _value.Count; i++)
+ _value[i] =
+ ((_value[i] * _size - d[i]) / (_size - 1));
+ _size--;
}
///
public double Distance(IMLData d)
{
- IMLData diff = value.Minus(d);
+ IMLData diff = _value.Minus(d);
double sum = 0.0;
for (int i = 0; i < diff.Count; i++)
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLDataPair.cs b/encog-core-cs/ML/Data/Basic/BasicMLDataPair.cs
index da446044..fbf579f8 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLDataPair.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLDataPair.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -137,9 +137,9 @@ public object Clone()
/// The size of the input data.
/// The size of the ideal data.
/// A new neural data pair object.
- public static IMLDataPair CreatePair(int inputSize, int idealSize)
+ public static BasicMLDataPair CreatePair(int inputSize, int idealSize)
{
- IMLDataPair result;
+ BasicMLDataPair result;
if (idealSize > 0)
{
@@ -154,26 +154,6 @@ public static IMLDataPair CreatePair(int inputSize, int idealSize)
return result;
}
- ///
- /// The supervised ideal data.
- ///
- public double[] IdealArray
- {
- get {
- return _ideal == null ? null : _ideal.Data;
- }
- set { _ideal.Data = value; }
- }
-
- ///
- /// The input array.
- ///
- public double[] InputArray
- {
- get { return _input.Data; }
- set { _input.Data = value; }
- }
-
///
/// Returns true, if supervised.
///
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLDataPairCentroid.cs b/encog-core-cs/ML/Data/Basic/BasicMLDataPairCentroid.cs
index 659c251e..19e84774 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLDataPairCentroid.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLDataPairCentroid.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -35,6 +35,11 @@ public class BasicMLDataPairCentroid : ICentroid
///
private readonly BasicMLData _value;
+ ///
+ /// How many items have been added to the centroid.
+ ///
+ private int _size;
+
///
/// Construct the centroid.
///
@@ -42,16 +47,15 @@ public class BasicMLDataPairCentroid : ICentroid
public BasicMLDataPairCentroid(BasicMLDataPair o)
{
_value = (BasicMLData)o.Input.Clone();
+ _size = 1;
}
///
public void Remove(IMLDataPair d)
{
- double[] a = d.InputArray;
-
for (int i = 0; i < _value.Count; i++)
- _value[i] =
- ((_value[i] * _value.Count - a[i]) / (_value.Count - 1));
+ _value[i] = ((_value[i] * _size - d.Input[i]) / (_size - 1));
+ _size--;
}
///
@@ -69,11 +73,10 @@ public double Distance(IMLDataPair d)
///
public void Add(IMLDataPair d)
{
- double[] a = d.InputArray;
-
for (int i = 0; i < _value.Count; i++)
_value[i] =
- ((_value[i] * _value.Count) + a[i]) / (_value.Count + 1);
+ ((_value[i] * _size) + d.Input[i]) / (_size + 1);
+ _size++;
}
}
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLDataSet.cs b/encog-core-cs/ML/Data/Basic/BasicMLDataSet.cs
index bed5642d..d4ed74a0 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLDataSet.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ namespace Encog.ML.Data.Basic
/// types extend this class.
///
[Serializable]
- public class BasicMLDataSet : IMLDataSet, IEnumerable
+ public class BasicMLDataSet : IMLDataSetAddable, IEnumerable
{
///
/// The enumerator for the basic neural data set.
@@ -159,16 +159,16 @@ public BasicMLDataSet(IMLDataSet set)
if (inputCount > 0)
{
input = new BasicMLData(inputCount);
- EngineArray.ArrayCopy(pair.InputArray, input.Data);
+ pair.Input.CopyTo(input.Data, 0, pair.Input.Count);
}
if (idealCount > 0)
{
ideal = new BasicMLData(idealCount);
- EngineArray.ArrayCopy(pair.IdealArray, ideal.Data);
+ pair.Ideal.CopyTo(ideal.Data, 0, pair.Ideal.Count);
}
- Add(new BasicMLDataPair(input, ideal));
+ Add(new BasicMLDataPair(input, ideal)); // should we copy Significance here?
}
}
@@ -222,14 +222,14 @@ public virtual int IdealSize
{
get
{
- if (_data == null || _data.Count == 0)
+ if (_data == null || _data.Count <= 0)
{
return 0;
}
IMLDataPair pair = _data[0];
- if (pair.IdealArray == null)
+ if (pair.Ideal == null)
{
return 0;
}
@@ -346,21 +346,6 @@ public int Count
get { return _data.Count; }
}
- ///
- /// Get one record from the data set.
- ///
- /// The index to read.
- /// The pair to read into.
- public void GetRecord(int index, IMLDataPair pair)
- {
- IMLDataPair source = _data[index];
- pair.InputArray = source.Input.Data;
- if (pair.IdealArray != null)
- {
- pair.IdealArray = source.Ideal.Data;
- }
- }
-
///
/// Open an additional instance of this dataset.
///
diff --git a/encog-core-cs/ML/Data/Basic/BasicMLSequenceSet.cs b/encog-core-cs/ML/Data/Basic/BasicMLSequenceSet.cs
index 742aa7e3..bde7b37f 100644
--- a/encog-core-cs/ML/Data/Basic/BasicMLSequenceSet.cs
+++ b/encog-core-cs/ML/Data/Basic/BasicMLSequenceSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public class BasicMLSequenceSet : IMLSequenceSet
///
private readonly IList _sequences = new List();
- private IMLDataSet _currentSequence;
+ private IMLDataSetAddable _currentSequence;
///
/// Default constructor.
@@ -98,13 +98,13 @@ public BasicMLSequenceSet(IMLDataSet set)
if (inputCount > 0)
{
input = new BasicMLData(inputCount);
- EngineArray.ArrayCopy(pair.InputArray, input.Data);
+ pair.Input.CopyTo(input.Data, 0, pair.Input.Count);
}
if (idealCount > 0)
{
ideal = new BasicMLData(idealCount);
- EngineArray.ArrayCopy(pair.IdealArray, ideal.Data);
+ pair.Ideal.CopyTo(ideal.Data, 0, pair.Ideal.Count);
}
_currentSequence.Add(new BasicMLDataPair(input, ideal));
@@ -165,25 +165,6 @@ public int InputSize
}
}
- ///
- public void GetRecord(int index, IMLDataPair pair)
- {
- int recordIndex = index;
- int sequenceIndex = 0;
-
- while (_sequences[sequenceIndex].Count < recordIndex)
- {
- recordIndex -= _sequences[sequenceIndex].Count;
- sequenceIndex++;
- if (sequenceIndex > _sequences.Count)
- {
- throw new MLDataError("Record out of range: " + index);
- }
- }
-
- _sequences[sequenceIndex].GetRecord(recordIndex, pair);
- }
-
///
public int Count
{
@@ -230,13 +211,13 @@ public int SequenceCount
///
public IMLDataSet GetSequence(int i)
{
- return _sequences[i];
+ return this._sequences[i];
}
///
public ICollection Sequences
{
- get { return _sequences; }
+ get { return this._sequences; }
}
///
@@ -244,7 +225,7 @@ public void Add(IMLDataSet sequence)
{
foreach (IMLDataPair pair in sequence)
{
- Add(pair);
+ this.Add(pair);
}
}
@@ -262,9 +243,18 @@ public IMLDataPair this[int x]
{
get
{
- IMLDataPair result = BasicMLDataPair.CreatePair(InputSize, IdealSize);
- GetRecord(x, result);
- return result;
+ int sequenceIndex = 0;
+ int recordIndex = x;
+ while(_sequences[sequenceIndex].Count <= recordIndex)
+ {
+ recordIndex -= _sequences[sequenceIndex].Count;
+ sequenceIndex++;
+ if(sequenceIndex > _sequences.Count)
+ {
+ throw new MLDataError("Record out of range: " + x);
+ }
+ }
+ return _sequences[sequenceIndex][recordIndex];
}
}
@@ -400,4 +390,4 @@ public void Reset()
#endregion
}
-}
\ No newline at end of file
+}
diff --git a/encog-core-cs/ML/Data/Buffer/BinaryDataLoader.cs b/encog-core-cs/ML/Data/Buffer/BinaryDataLoader.cs
index 40ee0a4f..7902ca1e 100644
--- a/encog-core-cs/ML/Data/Buffer/BinaryDataLoader.cs
+++ b/encog-core-cs/ML/Data/Buffer/BinaryDataLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -68,8 +68,7 @@ public IDataSetCODEC CODEC
/// The binary file to create.
public void External2Binary(String binaryFile)
{
- Status.Report(0, 0, "Importing to binary file: "
- + binaryFile);
+ Status.Report(0, 0, "Importing to binary file: " + binaryFile);
var egb = new EncogEGBFile(binaryFile);
diff --git a/encog-core-cs/ML/Data/Buffer/BufferedDataError.cs b/encog-core-cs/ML/Data/Buffer/BufferedDataError.cs
index 7eace377..6cb14b40 100644
--- a/encog-core-cs/ML/Data/Buffer/BufferedDataError.cs
+++ b/encog-core-cs/ML/Data/Buffer/BufferedDataError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Buffer/BufferedMLDataSet.cs b/encog-core-cs/ML/Data/Buffer/BufferedMLDataSet.cs
index ba5f5ee0..c5d736ef 100644
--- a/encog-core-cs/ML/Data/Buffer/BufferedMLDataSet.cs
+++ b/encog-core-cs/ML/Data/Buffer/BufferedMLDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ namespace Encog.ML.Data.Buffer
/// format, and can be used with any Encog platform. Encog binary files are
/// stored using "little endian" numbers.
///
- public class BufferedMLDataSet : IMLDataSet
+ public class BufferedMLDataSet : IMLDataSetAddable
{
///
/// Error message for ADD.
@@ -133,26 +133,6 @@ public int Count
}
}
- ///
- /// Read an individual record.
- ///
- /// The zero-based index. Specify 0 for the first record, 1 for
- /// the second, and so on.
- /// The data to read.
- public void GetRecord(int index, IMLDataPair pair)
- {
- double[] inputTarget = pair.InputArray;
- double[] idealTarget = pair.IdealArray;
-
- _egb.SetLocation(index);
- _egb.Read(inputTarget);
- if (idealTarget != null)
- {
- _egb.Read(idealTarget);
- }
- pair.Significance = _egb.Read();
- }
-
///
/// Open an additional training set.
///
@@ -175,7 +155,7 @@ public void Add(IMLData data1)
throw new IMLDataError(ErrorAdd);
}
- _egb.Write(data1.Data);
+ _egb.Write(data1);
_egb.Write(1.0);
}
@@ -192,8 +172,8 @@ public void Add(IMLData inputData, IMLData idealData)
throw new IMLDataError(ErrorAdd);
}
- _egb.Write(inputData.Data);
- _egb.Write(idealData.Data);
+ _egb.Write(inputData);
+ _egb.Write(idealData);
_egb.Write(1.0);
}
@@ -208,8 +188,8 @@ public void Add(IMLDataPair pair)
throw new IMLDataError(ErrorAdd);
}
- _egb.Write(pair.Input.Data);
- _egb.Write(pair.Ideal.Data);
+ _egb.Write(pair.Input);
+ _egb.Write(pair.Ideal);
_egb.Write(pair.Significance);
}
@@ -374,8 +354,18 @@ public IMLDataPair this[int x]
{
get
{
- IMLDataPair result = BasicMLDataPair.CreatePair(InputSize, IdealSize);
- GetRecord(x, result);
+ var input = new double[InputSize];
+ var ideal = new double[IdealSize];
+
+ _egb.SetLocation(x);
+ _egb.Read(input);
+ _egb.Read(ideal);
+
+ var inputData = new BasicMLData(input, false);
+ var idealData = new BasicMLData(ideal, false);
+
+ var result = new BasicMLDataPair(inputData, idealData);
+ result.Significance = _egb.Read();
return result;
}
}
diff --git a/encog-core-cs/ML/Data/Buffer/BufferedNeuralDataSetEnumerator.cs b/encog-core-cs/ML/Data/Buffer/BufferedNeuralDataSetEnumerator.cs
index 0ecb6484..595f8eb4 100644
--- a/encog-core-cs/ML/Data/Buffer/BufferedNeuralDataSetEnumerator.cs
+++ b/encog-core-cs/ML/Data/Buffer/BufferedNeuralDataSetEnumerator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ public class BufferedNeuralDataSetEnumerator : IEnumerator
///
/// The current record.
///
- private IMLDataPair _currentRecord;
+ private IMLDataPair _currentRecord;
///
/// Construct the buffered enumerator. This is where the file is actually
@@ -100,9 +100,7 @@ public bool MoveNext()
if (_current >= _data.Count)
return false;
- _currentRecord = BasicMLDataPair.CreatePair(_data
- .InputSize, _data.IdealSize);
- _data.GetRecord(_current++, _currentRecord);
+ _currentRecord = _data[_current++];
return true;
}
catch (EndOfStreamException)
@@ -112,12 +110,11 @@ public bool MoveNext()
}
///
- /// Not implemented.
+ /// Resets the enumeration.
///
- ///
public void Reset()
{
- throw new NotImplementedException();
+ _current = 0;
}
#endregion
diff --git a/encog-core-cs/ML/Data/Buffer/CODEC/ArrayDataCODEC.cs b/encog-core-cs/ML/Data/Buffer/CODEC/ArrayDataCODEC.cs
index b664d98d..5240208f 100644
--- a/encog-core-cs/ML/Data/Buffer/CODEC/ArrayDataCODEC.cs
+++ b/encog-core-cs/ML/Data/Buffer/CODEC/ArrayDataCODEC.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Buffer/CODEC/CSVDataCODEC.cs b/encog-core-cs/ML/Data/Buffer/CODEC/CSVDataCODEC.cs
index 1df631dc..d50d5a77 100644
--- a/encog-core-cs/ML/Data/Buffer/CODEC/CSVDataCODEC.cs
+++ b/encog-core-cs/ML/Data/Buffer/CODEC/CSVDataCODEC.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Buffer/CODEC/IDataSetCODEC.cs b/encog-core-cs/ML/Data/Buffer/CODEC/IDataSetCODEC.cs
index ea1bc711..d196a8ff 100644
--- a/encog-core-cs/ML/Data/Buffer/CODEC/IDataSetCODEC.cs
+++ b/encog-core-cs/ML/Data/Buffer/CODEC/IDataSetCODEC.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Buffer/CODEC/NeuralDataSetCODEC.cs b/encog-core-cs/ML/Data/Buffer/CODEC/NeuralDataSetCODEC.cs
index f3a7e502..09d9948a 100644
--- a/encog-core-cs/ML/Data/Buffer/CODEC/NeuralDataSetCODEC.cs
+++ b/encog-core-cs/ML/Data/Buffer/CODEC/NeuralDataSetCODEC.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
using System.Collections.Generic;
using Encog.ML.Data.Basic;
using Encog.Util;
+using System;
namespace Encog.ML.Data.Buffer.CODEC
{
@@ -86,8 +87,8 @@ public bool Read(double[] input, double[] ideal, ref double significance)
else
{
IMLDataPair pair = _enumerator.Current;
- EngineArray.ArrayCopy(pair.Input.Data, input);
- EngineArray.ArrayCopy(pair.Ideal.Data, ideal);
+ pair.Input.CopyTo(input, 0, pair.Input.Count);
+ pair.Ideal.CopyTo(ideal, 0, pair.Ideal.Count);
significance = pair.Significance;
return true;
}
@@ -96,11 +97,7 @@ public bool Read(double[] input, double[] ideal, ref double significance)
///
public void Write(double[] input, double[] ideal, double significance)
{
- IMLDataPair pair = BasicMLDataPair.CreatePair(_inputSize,
- _idealSize);
- EngineArray.ArrayCopy(input, pair.Input.Data);
- EngineArray.ArrayCopy(ideal, pair.Ideal.Data);
- pair.Significance = significance;
+ throw new NotSupportedException();
}
///
diff --git a/encog-core-cs/ML/Data/Buffer/CODEC/SQLCODEC.cs b/encog-core-cs/ML/Data/Buffer/CODEC/SQLCODEC.cs
index f1bcdbd8..3a482fd0 100644
--- a/encog-core-cs/ML/Data/Buffer/CODEC/SQLCODEC.cs
+++ b/encog-core-cs/ML/Data/Buffer/CODEC/SQLCODEC.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Buffer/EncogEGBFile.cs b/encog-core-cs/ML/Data/Buffer/EncogEGBFile.cs
index b561ce65..cb83ee0d 100644
--- a/encog-core-cs/ML/Data/Buffer/EncogEGBFile.cs
+++ b/encog-core-cs/ML/Data/Buffer/EncogEGBFile.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -375,7 +375,26 @@ public void Write(double[] v)
}
}
- ///
+ ///
+ /// Write the data from an IMLData
+ ///
+ /// The array to write.
+ public void Write(IMLData v)
+ {
+ try
+ {
+ for(int i = 0; i < v.Count; i++)
+ {
+ _binaryWriter.Write(v[i]);
+ }
+ }
+ catch(IOException ex)
+ {
+ throw new BufferedDataError(ex);
+ }
+ }
+
+ ///
/// Write a byte.
///
/// The byte to write.
@@ -467,7 +486,7 @@ public void Read(double[] d)
}
}
- ///
+ ///
/// Read a single double.
///
/// The double read.
diff --git a/encog-core-cs/ML/Data/Buffer/MemoryDataLoader.cs b/encog-core-cs/ML/Data/Buffer/MemoryDataLoader.cs
index 4305583a..ba255040 100644
--- a/encog-core-cs/ML/Data/Buffer/MemoryDataLoader.cs
+++ b/encog-core-cs/ML/Data/Buffer/MemoryDataLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Cross/DataFold.cs b/encog-core-cs/ML/Data/Cross/DataFold.cs
new file mode 100644
index 00000000..0763f3d7
--- /dev/null
+++ b/encog-core-cs/ML/Data/Cross/DataFold.cs
@@ -0,0 +1,53 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.Data.Versatile;
+
+namespace Encog.ML.Data.Cross
+{
+ ///
+ ///
+ public class DataFold
+ {
+ private readonly MatrixMLDataSet _training;
+ private readonly MatrixMLDataSet _validation;
+
+ public DataFold(MatrixMLDataSet theTraining, MatrixMLDataSet theValidation)
+ {
+ _training = theTraining;
+ _validation = theValidation;
+ }
+
+ public double Score { get; set; }
+ public IMLMethod Method { get; set; }
+
+ public MatrixMLDataSet Validation
+ {
+ get { return _validation; }
+ }
+
+ public MatrixMLDataSet Training
+ {
+ get { return _training; }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Cross/KFoldCrossvalidation.cs b/encog-core-cs/ML/Data/Cross/KFoldCrossvalidation.cs
new file mode 100644
index 00000000..7427ea3b
--- /dev/null
+++ b/encog-core-cs/ML/Data/Cross/KFoldCrossvalidation.cs
@@ -0,0 +1,194 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.MathUtil.Randomize.Generate;
+using Encog.ML.Data.Versatile;
+using Encog.Util;
+
+namespace Encog.ML.Data.Cross
+{
+ public class KFoldCrossvalidation
+ {
+ private readonly MatrixMLDataSet _baseDataset;
+ private readonly IList _folds = new List();
+ private readonly int _k;
+
+ public KFoldCrossvalidation()
+ {
+ Rnd = new MersenneTwisterGenerateRandom();
+ }
+
+ public KFoldCrossvalidation(MatrixMLDataSet theBaseDataset, int theK) : this()
+ {
+ _baseDataset = theBaseDataset;
+ _k = theK;
+ }
+
+ public IGenerateRandom Rnd { get; set; }
+
+ ///
+ /// The base data set.
+ ///
+ public MatrixMLDataSet BaseDataset
+ {
+ get { return _baseDataset; }
+ }
+
+ /**
+ * @return the k
+ */
+
+ public int K
+ {
+ get { return _k; }
+ }
+
+ /**
+ * @return the folds
+ */
+
+ public IList Folds
+ {
+ get { return _folds; }
+ }
+
+ private int[] BuildFirstList(int length)
+ {
+ var result = new int[length];
+
+ if (_baseDataset == null)
+ {
+ for (int i = 0; i < length; i++)
+ {
+ result[i] = i;
+ }
+ }
+ else
+ {
+ for (int i = 0; i < length; i++)
+ {
+ result[i] = _baseDataset.Mask[i];
+ }
+ }
+
+ return result;
+ }
+
+ private void ShuffleList(int[] list)
+ {
+ for (int i = list.Length - 1; i > 0; i--)
+ {
+ int n = Rnd.NextInt(i + 1);
+ int t = list[i];
+ list[i] = list[n];
+ list[n] = t;
+ }
+ }
+
+ private IList AllocateFolds()
+ {
+ IList folds = new List();
+ int countPer = _baseDataset.Count/K;
+ int countFirst = _baseDataset.Count - (countPer*(K - 1));
+
+ folds.Add(new int[countFirst]);
+ for (int i = 1; i < K; i++)
+ {
+ folds.Add(new int[countPer]);
+ }
+
+ return folds;
+ }
+
+ private void PopulateFolds(IList folds, int[] firstList)
+ {
+ int idx = 0;
+ foreach (var fold in folds)
+ {
+ for (int i = 0; i < fold.Length; i++)
+ {
+ fold[i] = firstList[idx++];
+ }
+ }
+ }
+
+ private void BuildSets(IList foldContents)
+ {
+ _folds.Clear();
+
+ for (int i = 0; i < K; i++)
+ {
+ // first calculate the size
+ int trainingSize = 0;
+ int validationSize = 0;
+ for (int j = 0; j < foldContents.Count; j++)
+ {
+ int foldSize = foldContents[j].Length;
+ if (j == i)
+ {
+ validationSize += foldSize;
+ }
+ else
+ {
+ trainingSize += foldSize;
+ }
+ }
+ // create the masks
+ var trainingMask = new int[trainingSize];
+ var validationMask = new int[validationSize];
+ int trainingIndex = 0;
+ for (int j = 0; j < foldContents.Count; j++)
+ {
+ int[] source = foldContents[j];
+ if (j == i)
+ {
+ EngineArray.ArrayCopy(source, 0, validationMask, 0, source.Length);
+ }
+ else
+ {
+ EngineArray.ArrayCopy(source, 0, trainingMask, trainingIndex, source.Length);
+ trainingIndex += source.Length;
+ }
+ }
+ // Build the set
+ var training = new MatrixMLDataSet(_baseDataset, trainingMask);
+ var validation = new MatrixMLDataSet(_baseDataset, validationMask);
+ _folds.Add(new DataFold(training, validation));
+ }
+ }
+
+ public void Process(bool shuffle)
+ {
+ int[] firstList = BuildFirstList(_baseDataset.Count);
+
+ if (shuffle)
+ {
+ ShuffleList(firstList);
+ }
+
+ IList foldContents = AllocateFolds();
+ PopulateFolds(foldContents, firstList);
+ BuildSets(foldContents);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Dynamic/DynamicMLDataSet.cs b/encog-core-cs/ML/Data/Dynamic/DynamicMLDataSet.cs
new file mode 100644
index 00000000..cd26ca27
--- /dev/null
+++ b/encog-core-cs/ML/Data/Dynamic/DynamicMLDataSet.cs
@@ -0,0 +1,258 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Data.Dynamic
+{
+ ///
+ /// For support of sliding windows or other custom input and ideal collection providers.
+ ///
+ public class DynamicMLDataSet : IMLDataSet
+ {
+ ///
+ /// Initializes a new instance of the DynamicMLDataSet class.
+ ///
+ /// The input.
+ /// The ideal.
+ public DynamicMLDataSet(IDynamicMLDataProvider input, IDynamicMLDataProvider ideal)
+ {
+ InputArgs = input;
+ IdealArgs = ideal;
+ Count = Math.Min(input.Count, ideal.Count);
+ }
+
+ ///
+ /// The ideal arguments.
+ ///
+ public readonly IDynamicMLDataProvider InputArgs, IdealArgs;
+
+ ///
+ /// The size of the ideal data (>0 supervised), 0 if no ideal data (unsupervised)
+ ///
+ ///
+ /// The size of the ideal.
+ ///
+ public int IdealSize { get { return IdealArgs.Size; } }
+
+ ///
+ /// The size of the input data.
+ ///
+ ///
+ /// The size of the input.
+ ///
+ public int InputSize { get { return InputArgs.Size; } }
+
+ ///
+ /// The number of records in the data set.
+ ///
+ ///
+ /// The count.
+ ///
+ public int Count { get; private set; }
+
+ ///
+ /// Return true if supervised.
+ ///
+ ///
+ /// true if supervised, false if not.
+ ///
+ public bool Supervised
+ {
+ get { return true; }
+ }
+
+ ///
+ /// Close this datasource and release any resources obtained by it, including any iterators
+ /// created.
+ ///
+ public void Close()
+ {
+ // nothing to close
+ }
+
+ ///
+ /// Get an enumerator to access the data.
+ ///
+ ///
+ /// The enumerator.
+ ///
+ public IEnumerator GetEnumerator()
+ {
+ return new DynamicMLDataSetEnumerator(this);
+ }
+
+ ///
+ /// Dynamic ML data set enumerator.
+ ///
+ private class DynamicMLDataSetEnumerator: IEnumerator
+ {
+ private int _position = -1;
+ private readonly DynamicMLDataSet _ds;
+ public DynamicMLDataSetEnumerator(DynamicMLDataSet ds) { _ds = ds; }
+
+ public IMLDataPair Current
+ {
+ get { return _position < 0 || _position >= _ds.Count ? null : _ds[_position]; }
+ }
+
+ public void Dispose()
+ {
+ }
+
+ object System.Collections.IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ public bool MoveNext()
+ {
+ return ++_position < _ds.Count;
+ }
+
+ public void Reset()
+ {
+ _position = -1;
+ }
+ }
+
+ ///
+ /// eg. Clone.
+ ///
+ ///
+ /// .
+ ///
+ public IMLDataSet OpenAdditional()
+ {
+ return new DynamicMLDataSet(InputArgs, IdealArgs);
+ }
+
+ ///
+ /// Get the specified record.
+ ///
+ ///
+ /// The indexed item.
+ ///
+ ///
+ /// ### The index to access.
+ /// ###
+ /// .
+ ///
+ public IMLDataPair this[int x]
+ {
+ get
+ {
+ return new DynamicMLDataPair(this, x);
+ }
+ }
+
+ ///
+ /// Dynamic ML data pair.
+ ///
+ private class DynamicMLDataPair: IMLDataPair
+ {
+ private readonly DynamicMLDataSet _ds;
+ private readonly int _index;
+ public DynamicMLDataPair(DynamicMLDataSet ds, int index)
+ {
+ _ds = ds;
+ _index = index;
+ Significance = 1.0;
+ Input = new DynamicWindowMLData(_ds.InputArgs, index);
+ Ideal = new DynamicWindowMLData(_ds.IdealArgs, index);
+ }
+
+ public IMLData Input { get; private set; }
+
+ public IMLData Ideal { get; private set; }
+
+ public bool Supervised
+ {
+ get { return true; }
+ }
+
+ public double Significance
+ {
+ get; set;
+ }
+
+ ///
+ /// Makes a deep copy of this object.
+ ///
+ ///
+ /// A copy of this object.
+ ///
+ public object Clone()
+ {
+ return new DynamicMLDataPair(_ds, _index);
+ }
+
+ public Util.KMeans.ICentroid CreateCentroid()
+ {
+ throw new NotImplementedException();
+ }
+
+ private class DynamicWindowMLData: IMLData
+ {
+ private readonly int _index;
+ private readonly IDynamicMLDataProvider _provider;
+ public DynamicWindowMLData(IDynamicMLDataProvider provider, int index)
+ {
+ _provider = provider;
+ _index = index;
+ }
+
+ public double this[int x]
+ {
+ get
+ {
+ return _provider[_index, x];
+ }
+ }
+
+ public int Count
+ {
+ get { return _provider.Size; }
+ }
+
+ public void CopyTo(double[] target, int targetIndex, int count)
+ {
+ for(int i = 0; i < count; i++)
+ target[i + targetIndex] = this[i];
+ }
+
+ public object Clone()
+ {
+ return new DynamicWindowMLData(_provider, _index);
+ }
+
+ public Util.KMeans.ICentroid CreateCentroid()
+ {
+ throw new NotImplementedException();
+ }
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Dynamic/IDynamicMLDataProvider.cs b/encog-core-cs/ML/Data/Dynamic/IDynamicMLDataProvider.cs
new file mode 100644
index 00000000..1192c300
--- /dev/null
+++ b/encog-core-cs/ML/Data/Dynamic/IDynamicMLDataProvider.cs
@@ -0,0 +1,47 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Data.Dynamic
+{
+ ///
+ /// Used for the input parameters to the dynamic dataset.
+ ///
+ public interface IDynamicMLDataProvider
+ {
+ ///
+ /// Total number of data chunks available.
+ ///
+ int Count { get; }
+
+ ///
+ /// The size of an individual chunk.
+ ///
+ int Size { get; }
+
+ double this[int chunk, int index] { get; }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Dynamic/Providers/FuncMLDataProvider.cs b/encog-core-cs/ML/Data/Dynamic/Providers/FuncMLDataProvider.cs
new file mode 100644
index 00000000..b3a489d1
--- /dev/null
+++ b/encog-core-cs/ML/Data/Dynamic/Providers/FuncMLDataProvider.cs
@@ -0,0 +1,71 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Data.Dynamic
+{
+ ///
+ /// Used for the input parameters to the sliding window dataset.
+ ///
+ public class FuncMLDataProvider: IDynamicMLDataProvider
+ {
+ Func _handler;
+
+ ///
+ /// Constructor.
+ ///
+ /// A method that takes as input the input/ideal sample and offset into that sample and returns the value at that location.
+ /// Total number of elements in this dataset.
+ /// The number of items per sample. eg. size of the input/output layer.
+ public FuncMLDataProvider(Func handler, int count, int size)
+ {
+ if(handler == null) throw new ArgumentNullException("handler");
+ if(count < 1) throw new ArgumentException("Value is too small.", "count");
+ if(size < 1) throw new ArgumentException("Value is too small.", "size");
+
+ _handler = handler;
+ Count = count;
+ Size = size;
+ }
+
+ public int Count
+ {
+ get;
+ protected set;
+ }
+
+ public int Size
+ {
+ get;
+ protected set;
+ }
+
+ public double this[int chunk, int index]
+ {
+ get { return _handler.Invoke(chunk, index); }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Dynamic/Providers/SlidingWindowMLDataProvider.cs b/encog-core-cs/ML/Data/Dynamic/Providers/SlidingWindowMLDataProvider.cs
new file mode 100644
index 00000000..c94af93d
--- /dev/null
+++ b/encog-core-cs/ML/Data/Dynamic/Providers/SlidingWindowMLDataProvider.cs
@@ -0,0 +1,103 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Data.Dynamic
+{
+ ///
+ /// Used for the input parameters to the sliding window dataset.
+ ///
+ public class SlidingWindowMLDataProvider: IDynamicMLDataProvider
+ {
+ public readonly IList List;
+ public readonly int WindowSize, WindowOffset, StepSize, Gap;
+
+ ///
+ /// Constructor.
+ ///
+ /// List of data to be used as input or ideal.
+ /// Size of Input/Ideal.
+ /// Shift +/- for a given index.
+ /// How much we move by for each discrete/outer index.
+ /// How much we move by for each inner index.
+ public SlidingWindowMLDataProvider(IList list, int windowSize, int windowOffset, int stepSize = 1, int gap = 1)
+ {
+ if(list == null) throw new ArgumentNullException("list");
+ if(list.Count < 2) throw new ArgumentException("List is too small.", "list");
+ if(stepSize < 1) throw new ArgumentException("Value is too small.", "stepSize");
+ if(windowSize < 1) throw new ArgumentException("Value is too small.", "windowSize");
+ if(gap < 1) throw new ArgumentException("Value is too small.", "gap");
+
+ List = list;
+ WindowSize = windowSize;
+ WindowOffset = windowOffset;
+ StepSize = stepSize;
+ Gap = gap;
+ }
+
+ ///
+ /// Number of chunks/windows/samplesets in this data.
+ ///
+ public virtual int Count
+ {
+ get { return List.Count / StepSize; }
+ }
+
+ ///
+ /// Size of any one chunk/window/sampleset.
+ ///
+ public virtual int Size
+ {
+ get { return WindowSize / Gap; }
+ }
+
+ ///
+ /// If true, the list boundaries are extended when the window takes us beyond the boundary.
+ ///
+ public bool PadWithNearest { get; set; }
+
+ ///
+ /// Defaults to 0.0
+ ///
+ public double DefaultPadValue { get; set; }
+
+ ///
+ /// Return data from the sliding window.
+ ///
+ /// The window we are on
+ /// An index into the window
+ public virtual double this[int chunk, int index]
+ {
+ get
+ {
+ var offset = chunk * StepSize + index * Gap + WindowOffset;
+ if(offset < 0) return PadWithNearest ? List.First() : DefaultPadValue;
+ if(offset >= List.Count) return PadWithNearest ? List.Last() : DefaultPadValue;
+ return List[offset];
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Folded/FoldedDataSet.cs b/encog-core-cs/ML/Data/Folded/FoldedDataSet.cs
index f0d40ced..effa429f 100644
--- a/encog-core-cs/ML/Data/Folded/FoldedDataSet.cs
+++ b/encog-core-cs/ML/Data/Folded/FoldedDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -37,11 +37,6 @@ namespace Encog.ML.Data.Folded
///
public class FoldedDataSet : IMLDataSet
{
- ///
- /// Error message: adds are not supported.
- ///
- public const String AddNotSupported = "Direct adds to the folded dataset are not supported.";
-
///
/// The underlying dataset.
///
@@ -173,34 +168,6 @@ public IMLDataSet Underlying
#region MLDataSet Members
- ///
- /// Not supported.
- ///
- /// Not used.
- public void Add(IMLData data1)
- {
- throw new TrainingError(AddNotSupported);
- }
-
- ///
- /// Not supported.
- ///
- /// Not used.
- /// Not used.
- public void Add(IMLData inputData, IMLData idealData)
- {
- throw new TrainingError(AddNotSupported);
- }
-
- ///
- /// Not supported.
- ///
- /// Not used.
- public void Add(IMLDataPair inputData)
- {
- throw new TrainingError(AddNotSupported);
- }
-
///
/// Close the dataset.
///
@@ -209,7 +176,6 @@ public void Close()
_underlying.Close();
}
-
///
/// The ideal size.
///
@@ -226,16 +192,6 @@ public int InputSize
get { return _underlying.InputSize; }
}
- ///
- /// Get a record.
- ///
- /// The index.
- /// The record.
- public void GetRecord(int index, IMLDataPair pair)
- {
- _underlying.GetRecord(CurrentFoldOffset + index, pair);
- }
-
///
/// The record count.
///
@@ -293,9 +249,7 @@ public IMLDataPair this[int x]
{
get
{
- IMLDataPair result = BasicMLDataPair.CreatePair(InputSize, IdealSize);
- this.GetRecord(x, result);
- return result;
+ return _underlying[CurrentFoldOffset + x];
}
}
}
diff --git a/encog-core-cs/ML/Data/Folded/FoldedEnumerator.cs b/encog-core-cs/ML/Data/Folded/FoldedEnumerator.cs
index 5e74a1a6..4ca61cec 100644
--- a/encog-core-cs/ML/Data/Folded/FoldedEnumerator.cs
+++ b/encog-core-cs/ML/Data/Folded/FoldedEnumerator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -120,10 +120,7 @@ public bool MoveNext()
{
if (HasNext())
{
- IMLDataPair pair = BasicMLDataPair.CreatePair(
- _owner.InputSize, _owner.IdealSize);
- _owner.GetRecord(_currentIndex++, pair);
- _currentPair = pair;
+ _currentPair = _owner[_currentIndex++];
return true;
}
_currentPair = null;
diff --git a/encog-core-cs/ML/Data/IMLComplexData.cs b/encog-core-cs/ML/Data/IMLComplexData.cs
index ac922309..18ec95d7 100644
--- a/encog-core-cs/ML/Data/IMLComplexData.cs
+++ b/encog-core-cs/ML/Data/IMLComplexData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/IMLData.cs b/encog-core-cs/ML/Data/IMLData.cs
index 7ad62fa4..0c5f7a34 100644
--- a/encog-core-cs/ML/Data/IMLData.cs
+++ b/encog-core-cs/ML/Data/IMLData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -31,26 +31,28 @@ namespace Encog.ML.Data
public interface IMLData : ICloneable, ICentroidFactory
{
///
- /// Get or set the specified index.
+ /// Get the specified index.
///
/// The index to access.
- ///
- double this[int x] { get; set; }
-
- ///
- /// Allowes indexed access to the data.
- ///
- double[] Data { get; set; }
+ double this[int x] { get; }
///
/// How many elements in this data structure.
///
int Count { get; }
- ///
- /// Clear the data to zero values.
- ///
- void Clear();
-
+ ///
+ /// Copy the data to the target array. The starting index is implementation-specific.
+ ///
+ void CopyTo(double[] target, int targetIndex, int count);
}
+
+ public interface IMLDataModifiable: IMLData
+ {
+ ///
+ /// Set the specified index.
+ ///
+ /// The index to access.
+ new double this[int x] { get; set; }
+ }
}
diff --git a/encog-core-cs/ML/Data/IMLDataError.cs b/encog-core-cs/ML/Data/IMLDataError.cs
index 423aa7bd..9c8daae7 100644
--- a/encog-core-cs/ML/Data/IMLDataError.cs
+++ b/encog-core-cs/ML/Data/IMLDataError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/IMLDataPair.cs b/encog-core-cs/ML/Data/IMLDataPair.cs
index c61f6e0d..9378218c 100644
--- a/encog-core-cs/ML/Data/IMLDataPair.cs
+++ b/encog-core-cs/ML/Data/IMLDataPair.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -48,16 +48,6 @@ public interface IMLDataPair : ICloneable, ICentroidFactory
///
bool Supervised { get; }
- ///
- /// The supervised ideal data.
- ///
- double[] IdealArray { get; set; }
-
- ///
- /// The input array.
- ///
- double[] InputArray { get; set; }
-
///
/// The significance of this training element.
///
diff --git a/encog-core-cs/ML/Data/IMLDataSet.cs b/encog-core-cs/ML/Data/IMLDataSet.cs
index 5cbff8fc..108c62f2 100644
--- a/encog-core-cs/ML/Data/IMLDataSet.cs
+++ b/encog-core-cs/ML/Data/IMLDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -59,33 +59,6 @@ public interface IMLDataSet
///
bool Supervised { get; }
- ///
- /// Add a NeuralData object to the dataset. This is used with unsupervised
- /// training, as no ideal output is provided. Note: not all implemenations
- /// support the add methods.
- ///
- /// The data to add.
- void Add(IMLData data1);
-
- ///
- /// Add a set of input and ideal data to the dataset. This is used with
- /// supervised training, as ideal output is provided. Note: not all
- /// implementations support the add methods.
- ///
- /// Input data.
- /// Ideal data.
- void Add(IMLData inputData, IMLData idealData);
-
- ///
- /// Add a NeuralData object to the dataset. This is used with unsupervised
- /// training, as no ideal output is provided. Note: not all implementations
- /// support the add methods.
- ///
- /// A NeuralDataPair object that contains both input and ideal
- /// data.
- void Add(IMLDataPair inputData);
-
-
///
/// Close this datasource and release any resources obtained by it, including
/// any iterators created.
@@ -98,13 +71,6 @@ public interface IMLDataSet
///
IEnumerator GetEnumerator();
- ///
- /// Get one record from the data set.
- ///
- /// The index to read.
- /// The pair to read into.
- void GetRecord(int index, IMLDataPair pair);
-
///
/// Open an additional instance of this dataset.
///
@@ -118,4 +84,34 @@ public interface IMLDataSet
///
IMLDataPair this[int x] { get; }
}
+
+ public interface IMLDataSetAddable: IMLDataSet
+ {
+ ///
+ /// Add a NeuralData object to the dataset. This is used with unsupervised
+ /// training, as no ideal output is provided. Note: not all implemenations
+ /// support the add methods.
+ ///
+ /// The data to add.
+ void Add(IMLData data1);
+
+ ///
+ /// Add a set of input and ideal data to the dataset. This is used with
+ /// supervised training, as ideal output is provided. Note: not all
+ /// implementations support the add methods.
+ ///
+ /// Input data.
+ /// Ideal data.
+ void Add(IMLData inputData, IMLData idealData);
+
+ ///
+ /// Add a NeuralData object to the dataset. This is used with unsupervised
+ /// training, as no ideal output is provided. Note: not all implementations
+ /// support the add methods.
+ ///
+ /// A NeuralDataPair object that contains both input and ideal
+ /// data.
+ void Add(IMLDataPair inputData);
+
+ }
}
diff --git a/encog-core-cs/ML/Data/IMLSequenceSet.cs b/encog-core-cs/ML/Data/IMLSequenceSet.cs
index 140fcc05..397f5ff3 100644
--- a/encog-core-cs/ML/Data/IMLSequenceSet.cs
+++ b/encog-core-cs/ML/Data/IMLSequenceSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ namespace Encog.ML.Data
/// machine learning methods are unaffected by them. Sequence sets are typically
/// used with Hidden Markov Models (HMM)'s.
///
- public interface IMLSequenceSet : IMLDataSet
+ public interface IMLSequenceSet : IMLDataSetAddable
{
///
/// Cause a "break" in the data by creating a the beginning of a new sequence.
diff --git a/encog-core-cs/ML/Data/Image/ImageMlData.cs b/encog-core-cs/ML/Data/Image/ImageMlData.cs
index cd209231..05560e36 100644
--- a/encog-core-cs/ML/Data/Image/ImageMlData.cs
+++ b/encog-core-cs/ML/Data/Image/ImageMlData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -84,7 +84,7 @@ public void Downsample(IDownSample downsampler,
*(hi - lo) + lo;
}
- Data = sample;
+ _data = sample;
}
///
diff --git a/encog-core-cs/ML/Data/Image/ImageMlDataSet.cs b/encog-core-cs/ML/Data/Image/ImageMlDataSet.cs
index 23b4ccd5..a98d3cca 100644
--- a/encog-core-cs/ML/Data/Image/ImageMlDataSet.cs
+++ b/encog-core-cs/ML/Data/Image/ImageMlDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/MLDataError.cs b/encog-core-cs/ML/Data/MLDataError.cs
index 32ec3141..121a3e48 100644
--- a/encog-core-cs/ML/Data/MLDataError.cs
+++ b/encog-core-cs/ML/Data/MLDataError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/CSVFinal.cs b/encog-core-cs/ML/Data/Market/Loader/CSVFinal.cs
index 53986cf7..565724d3 100644
--- a/encog-core-cs/ML/Data/Market/Loader/CSVFinal.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/CSVFinal.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/CSVTicksLoader.cs b/encog-core-cs/ML/Data/Market/Loader/CSVTicksLoader.cs
index 1130e86c..c669dd79 100644
--- a/encog-core-cs/ML/Data/Market/Loader/CSVTicksLoader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/CSVTicksLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/GoogleLoader.cs b/encog-core-cs/ML/Data/Market/Loader/GoogleLoader.cs
index a39a2acf..e4610a33 100644
--- a/encog-core-cs/ML/Data/Market/Loader/GoogleLoader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/GoogleLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/IMarketLoader.cs b/encog-core-cs/ML/Data/Market/Loader/IMarketLoader.cs
index c7809fdc..d2b9b646 100644
--- a/encog-core-cs/ML/Data/Market/Loader/IMarketLoader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/IMarketLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/LoadedMarketData.cs b/encog-core-cs/ML/Data/Market/Loader/LoadedMarketData.cs
index 6a764036..d06f2173 100644
--- a/encog-core-cs/ML/Data/Market/Loader/LoadedMarketData.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/LoadedMarketData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/LoaderError.cs b/encog-core-cs/ML/Data/Market/Loader/LoaderError.cs
index 75cfa9d3..2761d015 100644
--- a/encog-core-cs/ML/Data/Market/Loader/LoaderError.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/LoaderError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/MultiCsvLoader.cs b/encog-core-cs/ML/Data/Market/Loader/MultiCsvLoader.cs
index 92574e86..bdb03fff 100644
--- a/encog-core-cs/ML/Data/Market/Loader/MultiCsvLoader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/MultiCsvLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/YahooFinanceLoader.cs b/encog-core-cs/ML/Data/Market/Loader/YahooFinanceLoader.cs
index 75872e43..bd54ecb0 100644
--- a/encog-core-cs/ML/Data/Market/Loader/YahooFinanceLoader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/YahooFinanceLoader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -37,6 +37,12 @@ public class YahooFinanceLoader : IMarketLoader
{
#region IMarketLoader Members
+ public YahooFinanceLoader()
+ {
+ throw new LoaderError("Yahoo has dropped support for this feature.");
+ }
+
+
///
/// Load the specified financial data.
///
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvfileloader.cs b/encog-core-cs/ML/Data/Market/Loader/csvfileloader.cs
index eefc0797..9669a648 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvfileloader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvfileloader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvformloader.cs b/encog-core-cs/ML/Data/Market/Loader/csvformloader.cs
index 22001b54..ad819516 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvformloader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvformloader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvformloader.designer.cs b/encog-core-cs/ML/Data/Market/Loader/csvformloader.designer.cs
index 0fe27623..64e95be8 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvformloader.designer.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvformloader.designer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvformsimple.cs b/encog-core-cs/ML/Data/Market/Loader/csvformsimple.cs
index 0bdb9a9b..a92cc38f 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvformsimple.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvformsimple.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvformsimple.designer.cs b/encog-core-cs/ML/Data/Market/Loader/csvformsimple.designer.cs
index 8129ec1f..885e374d 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvformsimple.designer.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvformsimple.designer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvloader.cs b/encog-core-cs/ML/Data/Market/Loader/csvloader.cs
index 313ce6cf..cde8d9df 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvloader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvloader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/Loader/csvtickloader.cs b/encog-core-cs/ML/Data/Market/Loader/csvtickloader.cs
index a520475b..e5032b41 100644
--- a/encog-core-cs/ML/Data/Market/Loader/csvtickloader.cs
+++ b/encog-core-cs/ML/Data/Market/Loader/csvtickloader.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/MarketDataDescription.cs b/encog-core-cs/ML/Data/Market/MarketDataDescription.cs
index 5fba7b32..88d1b31d 100644
--- a/encog-core-cs/ML/Data/Market/MarketDataDescription.cs
+++ b/encog-core-cs/ML/Data/Market/MarketDataDescription.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/MarketDataType.cs b/encog-core-cs/ML/Data/Market/MarketDataType.cs
index f52fbd44..46b7eb8c 100644
--- a/encog-core-cs/ML/Data/Market/MarketDataType.cs
+++ b/encog-core-cs/ML/Data/Market/MarketDataType.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/MarketError.cs b/encog-core-cs/ML/Data/Market/MarketError.cs
index 2b489266..ac12407c 100644
--- a/encog-core-cs/ML/Data/Market/MarketError.cs
+++ b/encog-core-cs/ML/Data/Market/MarketError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/MarketMLDataSet.cs b/encog-core-cs/ML/Data/Market/MarketMLDataSet.cs
index fa5b24dc..8a43de1f 100644
--- a/encog-core-cs/ML/Data/Market/MarketMLDataSet.cs
+++ b/encog-core-cs/ML/Data/Market/MarketMLDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/MarketPoint.cs b/encog-core-cs/ML/Data/Market/MarketPoint.cs
index 56ab9603..ffaf8aa3 100644
--- a/encog-core-cs/ML/Data/Market/MarketPoint.cs
+++ b/encog-core-cs/ML/Data/Market/MarketPoint.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Market/TickerSymbol.cs b/encog-core-cs/ML/Data/Market/TickerSymbol.cs
index 090cbe46..1a1d1ce5 100644
--- a/encog-core-cs/ML/Data/Market/TickerSymbol.cs
+++ b/encog-core-cs/ML/Data/Market/TickerSymbol.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Specific/BiPolarMlData.cs b/encog-core-cs/ML/Data/Specific/BiPolarMlData.cs
index a471cfb3..49ec5a3e 100644
--- a/encog-core-cs/ML/Data/Specific/BiPolarMlData.cs
+++ b/encog-core-cs/ML/Data/Specific/BiPolarMlData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ namespace Encog.ML.Data.Specific
/// is stored as -1.
///
[Serializable]
- public class BiPolarMLData : IMLData
+ public class BiPolarMLData: IMLDataModifiable
{
///
/// The data held by this object.
@@ -44,9 +44,8 @@ public class BiPolarMLData : IMLData
/// Construct this object with the specified data.
///
/// The data to create this object with.
- public BiPolarMLData(bool[] d)
+ public BiPolarMLData(bool[] d) : this(d.Length)
{
- _data = new bool[d.Length];
for (int i = 0; i < d.Length; i++)
{
_data[i] = d[i];
@@ -79,7 +78,7 @@ public double this[int x]
public double[] Data
{
get { return BiPolarUtil.Bipolar2double(_data); }
- set { _data = BiPolarUtil.Double2bipolar(value); }
+ internal set { _data = BiPolarUtil.Double2bipolar(value); }
}
///
@@ -131,7 +130,7 @@ public void Clear()
}
///
- public String ToString()
+ public override String ToString()
{
var result = new StringBuilder();
result.Append('[');
@@ -156,5 +155,10 @@ public ICentroid CreateCentroid()
return null;
}
- }
+ public void CopyTo(double[] target, int targetIndex, int count)
+ {
+ for(int i = 0; i < count; i++)
+ target[i + targetIndex] = _data[i] ? 1.0 : -1.0;
+ }
+ }
}
diff --git a/encog-core-cs/ML/Data/Specific/CsvMlDataSet.cs b/encog-core-cs/ML/Data/Specific/CsvMlDataSet.cs
index efb8d513..769dac8c 100644
--- a/encog-core-cs/ML/Data/Specific/CsvMlDataSet.cs
+++ b/encog-core-cs/ML/Data/Specific/CsvMlDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Specific/SqlMlDataSet.cs b/encog-core-cs/ML/Data/Specific/SqlMlDataSet.cs
index 4f6d458c..d1eb3443 100644
--- a/encog-core-cs/ML/Data/Specific/SqlMlDataSet.cs
+++ b/encog-core-cs/ML/Data/Specific/SqlMlDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Temporal/TemporalDataDescription.cs b/encog-core-cs/ML/Data/Temporal/TemporalDataDescription.cs
index 6d1dfbd8..917f9b43 100644
--- a/encog-core-cs/ML/Data/Temporal/TemporalDataDescription.cs
+++ b/encog-core-cs/ML/Data/Temporal/TemporalDataDescription.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Temporal/TemporalError.cs b/encog-core-cs/ML/Data/Temporal/TemporalError.cs
index 5f079951..0a462939 100644
--- a/encog-core-cs/ML/Data/Temporal/TemporalError.cs
+++ b/encog-core-cs/ML/Data/Temporal/TemporalError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Data/Temporal/TemporalMLDataSet.cs b/encog-core-cs/ML/Data/Temporal/TemporalMLDataSet.cs
index 5a094885..01e0b58d 100644
--- a/encog-core-cs/ML/Data/Temporal/TemporalMLDataSet.cs
+++ b/encog-core-cs/ML/Data/Temporal/TemporalMLDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ public class TemporalMLDataSet : BasicMLDataSet
///
/// The temporal points at which we have data.
///
- private readonly List _points = new List();
+ private readonly List _points;
///
/// How big would we like the input size to be.
@@ -121,9 +121,11 @@ public class TemporalMLDataSet : BasicMLDataSet
/// Construct a dataset.
///
/// What is the input window size.
- /// What is the prediction window size.
+ /// What is the prediction window size.
+ /// Size of the dataset (if known ahead)
public TemporalMLDataSet(int inputWindowSize,
- int predictWindowSize)
+ int predictWindowSize,
+ int dataSetSize=0)
{
_inputWindowSize = inputWindowSize;
_predictWindowSize = predictWindowSize;
@@ -132,6 +134,7 @@ public TemporalMLDataSet(int inputWindowSize,
_desiredSetSize = int.MaxValue;
_startingPoint = DateTime.MinValue;
_sequenceGrandularity = TimeUnit.Days;
+ _points = new List(Math.Max(0, dataSetSize));
}
@@ -269,7 +272,7 @@ public virtual void Clear()
///
/// Not used
/// Not used
- public override void Add(IMLData inputData, IMLData idealData)
+ public sealed override void Add(IMLData inputData, IMLData idealData)
{
throw new TemporalError(AddNotSupported);
}
@@ -279,7 +282,7 @@ public override void Add(IMLData inputData, IMLData idealData)
/// generate the training data.
///
/// Not used.
- public override void Add(IMLDataPair inputData)
+ public sealed override void Add(IMLDataPair inputData)
{
throw new TemporalError(AddNotSupported);
}
@@ -289,7 +292,7 @@ public override void Add(IMLDataPair inputData)
/// generate the training data.
///
/// Not used.
- public override void Add(IMLData data)
+ public sealed override void Add(IMLData data)
{
throw new TemporalError(AddNotSupported);
}
@@ -442,7 +445,10 @@ public virtual BasicNeuralData GenerateInputNeuralData(int index)
private double GetDataRaw(TemporalDataDescription desc,
int index)
{
- TemporalPoint point = _points[index - 1];
+ // Note: The reason that we subtract 1 from the index is because we are always one ahead.
+ // This allows the DELTA change formatter to work. DELTA change requires two timeslices,
+ // so we have to be one ahead. RAW only requires one, so we shift backwards.
+ TemporalPoint point = _points[index-1];
return point[desc.Index];
}
diff --git a/encog-core-cs/ML/Data/Temporal/TemporalPoint.cs b/encog-core-cs/ML/Data/Temporal/TemporalPoint.cs
index 11ebf95d..af0eafd0 100644
--- a/encog-core-cs/ML/Data/Temporal/TemporalPoint.cs
+++ b/encog-core-cs/ML/Data/Temporal/TemporalPoint.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
namespace Encog.ML.Data.Temporal
{
///
- /// A point in tme for a temporal data set.
+ /// A point in time for a temporal data set.
///
public class TemporalPoint : IComparable
{
diff --git a/encog-core-cs/ML/Data/Versatile/Columns/ColumnDefinition.cs b/encog-core-cs/ML/Data/Versatile/Columns/ColumnDefinition.cs
new file mode 100644
index 00000000..ad757d8d
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Columns/ColumnDefinition.cs
@@ -0,0 +1,230 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Encog.Util;
+
+namespace Encog.ML.Data.Versatile.Columns
+{
+ ///
+ /// Defines a column definition.
+ ///
+ [Serializable]
+ public class ColumnDefinition
+ {
+ ///
+ /// The classes of a catagorical.
+ ///
+ private readonly IList _classes = new List();
+
+ ///
+ /// The normalization helper.
+ ///
+ public NormalizationHelper Owner { get; set; }
+
+ ///
+ /// The column definition.
+ ///
+ /// The name of the column.
+ /// The type of the column.
+ public ColumnDefinition(String theName, ColumnType theDataType)
+ {
+ Name = theName;
+ DataType = theDataType;
+ Count = -1;
+ Low = High = Mean = Sd = Double.NaN;
+ }
+
+ ///
+ /// The name of the column.
+ ///
+ public String Name { get; set; }
+
+ ///
+ /// The type of column.
+ ///
+ public ColumnType DataType { get; set; }
+
+ ///
+ /// The observed low in a dataset.
+ ///
+ public double Low { get; set; }
+
+ ///
+ /// The observed high in a dataset.
+ ///
+ public double High { get; set; }
+
+ ///
+ /// The observed mean in a dataset.
+ ///
+ public double Mean { get; set; }
+
+ ///
+ /// The observed standard deviation in a dataset.
+ ///
+ public double Sd { get; set; }
+
+ ///
+ /// The observed count for a catagorical column.
+ ///
+ public int Count { get; set; }
+
+ ///
+ /// The index of this column in the dataset.
+ ///
+ public int Index { get; set; }
+
+ ///
+ /// The classes for a catagorical type.
+ ///
+ public IList Classes
+ {
+ get { return _classes; }
+ }
+
+ ///
+ /// Analyze the specified value.
+ ///
+ /// The value to analyze.
+ public void Analyze(string value)
+ {
+ switch (DataType)
+ {
+ case ColumnType.Continuous:
+ AnalyzeContinuous(value);
+ break;
+ case ColumnType.Ordinal:
+ AnalyzeOrdinal(value);
+ break;
+ case ColumnType.Nominal:
+ AnalyzeNominal(value);
+ break;
+ }
+ }
+
+ /**
+ * Analyze a nominal value.
+ * @param value The value to analyze.
+ */
+
+ private void AnalyzeNominal(String value)
+ {
+ if (!_classes.Contains(value))
+ {
+ _classes.Add(value);
+ }
+ }
+
+ /**
+ * Analyze a nominal value.
+ * @param value The value to analyze.
+ */
+
+ private void AnalyzeOrdinal(String value)
+ {
+ if (!_classes.Contains(value))
+ {
+ throw (new EncogError("You must predefine any ordinal values (in order). Undefined ordinal value: " +
+ value));
+ }
+ }
+
+ ///
+ /// Analyze a nominal value.
+ ///
+ /// The value to analyze.
+ private void AnalyzeContinuous(String value)
+ {
+ double d = Owner.Format.Parse(value);
+ if (Count < 0)
+ {
+ Low = d;
+ High = d;
+ Mean = d;
+ Sd = 0;
+ Count = 1;
+ }
+ else
+ {
+ Mean = Mean + d;
+ Low = Math.Min(Low, d);
+ High = Math.Max(High, d);
+ Count++;
+ }
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[");
+ result.Append("ColumnDefinition:");
+ result.Append(Name);
+ result.Append("(");
+ result.Append(DataType);
+ result.Append(")");
+ if (DataType == ColumnType.Continuous)
+ {
+ result.Append(";low=");
+ result.Append(Format.FormatDouble(Low, 6));
+ result.Append(",high=");
+ result.Append(Format.FormatDouble(High, 6));
+ result.Append(",mean=");
+ result.Append(Format.FormatDouble(Mean, 6));
+ result.Append(",sd=");
+ result.Append(Format.FormatDouble(Sd, 6));
+ }
+ else
+ {
+ result.Append(";");
+ result.Append(_classes);
+ }
+ result.Append("]");
+ return result.ToString();
+ }
+
+ /**
+ * Define a class for a catagorical value.
+ * @param str The class to add.
+ */
+
+ public void DefineClass(string str)
+ {
+ _classes.Add(str);
+ }
+
+ ///
+ /// Define an array of classes for a catagorical value.
+ ///
+ /// The classes to add.
+ public void DefineClass(string[] str)
+ {
+ foreach (string s in str)
+ {
+ DefineClass(s);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Columns/ColumnType.cs b/encog-core-cs/ML/Data/Versatile/Columns/ColumnType.cs
new file mode 100644
index 00000000..57009833
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Columns/ColumnType.cs
@@ -0,0 +1,54 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.ML.Data.Versatile.Columns
+{
+ ///
+ /// The type of column, defined using level of measurement.
+ /// http://en.wikipedia.org/wiki/Level_of_measurement
+ ///
+ public enum ColumnType
+ {
+ ///
+ /// A discrete nominal, or categorical, value specifies class membership. For example, US states.
+ /// There is a fixed number, yet no obvious, meaningful ordering.
+ ///
+ Nominal,
+
+ ///
+ /// A discrete ordinal specifies a non-numeric value that has a specific ordering. For example,
+ /// the months of the year are inherently non-numerical, yet has a specific ordering.
+ ///
+ Ordinal,
+
+ ///
+ /// A continuous (non-discrete) value is simply floating point numeric. These values are
+ /// orderable and dense.
+ ///
+ Continuous,
+
+ ///
+ /// This field is ignored.
+ ///
+ Ignore
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Division/DataDivision.cs b/encog-core-cs/ML/Data/Versatile/Division/DataDivision.cs
new file mode 100644
index 00000000..cd1f43b3
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Division/DataDivision.cs
@@ -0,0 +1,69 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.ML.Data.Versatile.Division
+{
+ ///
+ /// A division of data inside of a versatile data set.
+ ///
+ public class DataDivision
+ {
+ ///
+ /// The count of items in this partition.
+ ///
+ public int Count { get; set; }
+
+ ///
+ /// The percent of items in this partition.
+ ///
+ public double Percent { get; private set; }
+
+ ///
+ /// The dataset that we are dividing.
+ ///
+ public MatrixMLDataSet Dataset { get; set; }
+
+ ///
+ /// The mask of items we are to use.
+ ///
+ public int[] Mask { get; private set; }
+
+ ///
+ /// Construct a division.
+ ///
+ /// The desired percentage in this division.
+ public DataDivision(double thePercent)
+ {
+ Percent = thePercent;
+ }
+
+ ///
+ /// Allocat space to hold the mask.
+ ///
+ /// The mask size.
+ public void AllocateMask(int theSize)
+ {
+ Mask = new int[theSize];
+ }
+
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Division/PerformDataDivision.cs b/encog-core-cs/ML/Data/Versatile/Division/PerformDataDivision.cs
new file mode 100644
index 00000000..8309ad75
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Division/PerformDataDivision.cs
@@ -0,0 +1,207 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.MathUtil.Randomize.Generate;
+
+namespace Encog.ML.Data.Versatile.Division
+{
+ ///
+ /// Perform a data division.
+ ///
+ public class PerformDataDivision
+ {
+ ///
+ /// A random number generator.
+ ///
+ private readonly IGenerateRandom _rnd;
+
+ ///
+ /// True, if we should shuffle during division.
+ ///
+ private readonly bool _shuffle;
+
+ ///
+ /// Construct the data division processor.
+ ///
+ /// Should we shuffle?
+ /// Random number generator, often seeded to be consistent.
+ public PerformDataDivision(bool theShuffle, IGenerateRandom theRandom)
+ {
+ _shuffle = theShuffle;
+ _rnd = theRandom;
+ }
+
+ ///
+ /// Should we shuffle.
+ ///
+ public bool Shuffle
+ {
+ get { return _shuffle; }
+ }
+
+ ///
+ /// Random number generator.
+ ///
+ public IGenerateRandom Random
+ {
+ get { return _rnd; }
+ }
+
+ ///
+ /// Perform the split.
+ ///
+ /// The list of data divisions.
+ /// The dataset to split.
+ /// The input count.
+ /// The ideal count.
+ public void Perform(IList dataDivisionList, MatrixMLDataSet dataset,
+ int inputCount, int idealCount)
+ {
+ GenerateCounts(dataDivisionList, dataset.Data.Length);
+ GenerateMasks(dataDivisionList);
+ if (_shuffle)
+ {
+ PerformShuffle(dataDivisionList, dataset.Data.Length);
+ }
+ CreateDividedDatasets(dataDivisionList, dataset, inputCount, idealCount);
+ }
+
+ ///
+ /// Create the datasets that we will divide into.
+ ///
+ /// The list of divisions.
+ /// The data set to divide.
+ /// The input count.
+ /// The ideal count.
+ private void CreateDividedDatasets(IEnumerable dataDivisionList,
+ MatrixMLDataSet parentDataset, int inputCount, int idealCount)
+ {
+ foreach (DataDivision division in dataDivisionList)
+ {
+ var dataset = new MatrixMLDataSet(parentDataset.Data, inputCount,
+ idealCount, division.Mask)
+ {
+ LagWindowSize = parentDataset.LagWindowSize,
+ LeadWindowSize = parentDataset.LeadWindowSize
+ };
+ division.Dataset = dataset;
+ }
+ }
+
+ ///
+ /// Perform a Fisher-Yates shuffle.
+ /// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
+ ///
+ /// The division list.
+ /// Total count across divisions.
+ private void PerformShuffle(IList dataDivisionList,
+ int totalCount)
+ {
+ for (int i = totalCount - 1; i > 0; i--)
+ {
+ int n = _rnd.NextInt(i + 1);
+ VirtualSwap(dataDivisionList, i, n);
+ }
+ }
+
+ ///
+ /// Swap two items, across all divisions.
+ ///
+ /// The division list.
+ /// The index of the first item to swap.
+ /// The index of the second item to swap.
+ private void VirtualSwap(IEnumerable dataDivisionList, int a, int b)
+ {
+ DataDivision divA = null;
+ DataDivision divB = null;
+ int offsetA = 0;
+ int offsetB = 0;
+
+ // Find points a and b in the collections.
+ int baseIndex = 0;
+ foreach (DataDivision division in dataDivisionList)
+ {
+ baseIndex += division.Count;
+
+ if (divA == null && a < baseIndex)
+ {
+ divA = division;
+ offsetA = a - (baseIndex - division.Count);
+ }
+ if (divB == null && b < baseIndex)
+ {
+ divB = division;
+ offsetB = b - (baseIndex - division.Count);
+ }
+ }
+
+ // Swap a and b.
+ int temp = divA.Mask[offsetA];
+ divA.Mask[offsetA] = divB.Mask[offsetB];
+ divB.Mask[offsetB] = temp;
+ }
+
+ ///
+ /// Generate the masks, for all divisions.
+ ///
+ /// The divisions.
+ private void GenerateMasks(IEnumerable dataDivisionList)
+ {
+ int idx = 0;
+ foreach (DataDivision division in dataDivisionList)
+ {
+ division.AllocateMask(division.Count);
+ for (int i = 0; i < division.Count; i++)
+ {
+ division.Mask[i] = idx++;
+ }
+ }
+ }
+
+ ///
+ /// Generate the counts for all divisions, give remaining items to final division.
+ ///
+ /// The division list.
+ /// The total count.
+ private void GenerateCounts(IList dataDivisionList,
+ int totalCount)
+ {
+ // First pass at division.
+ int countSofar = 0;
+ foreach (DataDivision division in dataDivisionList)
+ {
+ var count = (int) (division.Percent*totalCount);
+ division.Count = count;
+ countSofar += count;
+ }
+ // Adjust any remaining count
+ int remaining = totalCount - countSofar;
+ while (remaining-- > 0)
+ {
+ int idx = _rnd.NextInt(dataDivisionList.Count);
+ DataDivision div = dataDivisionList[idx];
+ div.Count = div.Count + 1;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/MatrixMLDataSet.cs b/encog-core-cs/ML/Data/Versatile/MatrixMLDataSet.cs
new file mode 100644
index 00000000..bfb13074
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/MatrixMLDataSet.cs
@@ -0,0 +1,417 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using Encog.ML.Data.Basic;
+using Encog.Util;
+
+namespace Encog.ML.Data.Versatile
+{
+ ///
+ /// The MatrixMLDataSet can use a large 2D matrix of doubles to internally hold
+ /// data. It supports several advanced features such as the ability to mask and
+ /// time-box. Masking allows several datasets to use the same backing array,
+ /// however use different parts.
+ /// Time boxing allows time-series data to be represented for prediction. The
+ /// following shows how data is laid out for different lag and lead settings.
+ /// Lag 0; Lead 0 [10 rows] 1->1 2->2 3->3 4->4 5->5 6->6 7->7 8->8 9->9 10->10
+ /// Lag 0; Lead 1 [9 rows] 1->2 2->3 3->4 4->5 5->6 6->7 7->8 8->9 9->10
+ /// Lag 1; Lead 0 [9 rows, not useful] 1,2->1 2,3->2 3,4->3 4,5->4 5,6->5 6,7->6
+ /// 7,8->7 8,9->8 9,10->9
+ /// Lag 1; Lead 1 [8 rows] 1,2->3 2,3->4 3,4->5 4,5->6 5,6->7 6,7->8 7,8->9
+ /// 8,9->10
+ /// Lag 1; Lead 2 [7 rows] 1,2->3,4 2,3->4,5 3,4->5,6 4,5->6,7 5,6->7,8 6,7->8,9
+ /// 7,8->9,10
+ /// Lag 2; Lead 1 [7 rows] 1,2,3->4 2,3,4->5 3,4,5->6 4,5,6->7 5,6,7->8 6,7,8->9
+ /// 7,8,9->10
+ ///
+ [Serializable]
+ public class MatrixMLDataSet : IMLDataSetAddable, IEnumerable
+ {
+ ///
+ /// The mask to the data.
+ ///
+ private readonly int[] _mask;
+
+ ///
+ /// The default constructor.
+ ///
+ public MatrixMLDataSet()
+ {
+ CalculatedInputSize = -1;
+ CalculatedIdealSize = -1;
+ }
+
+ ///
+ /// Construct the dataset with no mask.
+ ///
+ /// The backing array.
+ /// The input size.
+ /// The ideal size.
+ public MatrixMLDataSet(double[][] theData, int theCalculatedInputSize,
+ int theCalculatedIdealSize) : this()
+ {
+ Data = theData;
+ CalculatedInputSize = theCalculatedInputSize;
+ CalculatedIdealSize = theCalculatedIdealSize;
+ }
+
+ ///
+ /// Construct the dataset from a 2D double array.
+ ///
+ /// The data.
+ /// The input count.
+ /// The ideal count.
+ /// The mask.
+ public MatrixMLDataSet(double[][] theData, int inputCount, int idealCount,
+ int[] theMask) : this()
+ {
+ Data = theData;
+ CalculatedInputSize = inputCount;
+ CalculatedIdealSize = idealCount;
+ _mask = theMask;
+ }
+
+ ///
+ /// Construct the dataset from another matrix dataset.
+ ///
+ /// The data.
+ /// The mask.
+ public MatrixMLDataSet(MatrixMLDataSet data, int[] mask) : this()
+ {
+ Data = data.Data;
+ CalculatedInputSize = data.CalculatedInputSize;
+ CalculatedIdealSize = data.CalculatedIdealSize;
+ _mask = mask;
+ }
+
+ ///
+ /// The number of inputs.
+ ///
+ public int CalculatedInputSize { get; set; }
+
+ ///
+ /// The number of ideal values.
+ ///
+ public int CalculatedIdealSize { get; set; }
+
+ ///
+ /// The backing data.
+ ///
+ public double[][] Data { get; set; }
+
+ ///
+ /// The lag window size.
+ ///
+ public int LagWindowSize { get; set; }
+
+ ///
+ /// The lead window size.
+ ///
+ public int LeadWindowSize { get; set; }
+
+ ///
+ /// The mask.
+ ///
+ public int[] Mask
+ {
+ get { return _mask; }
+ }
+
+ ///
+ /// Create an enumerator.
+ ///
+ /// The enumerator.
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return new MatrixMLDataSetEnumerator(this);
+ }
+
+ ///
+ /// Create an enumerator.
+ ///
+ /// The enumerator.
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return new MatrixMLDataSetEnumerator(this);
+ }
+
+
+ ///
+ public int IdealSize
+ {
+ get { return CalculatedIdealSize*Math.Min(LeadWindowSize, 1); }
+ }
+
+ ///
+ public int InputSize
+ {
+ get { return CalculatedInputSize*LagWindowSize; }
+ }
+
+ ///
+ public bool Supervised
+ {
+ get { return IdealSize == 0; }
+ }
+
+ ///
+ public IEnumerator GetEnumerator()
+ {
+ return new MatrixMLDataSetEnumerator(this);
+ }
+
+ ///
+ public IMLDataSet OpenAdditional()
+ {
+ var result = new MatrixMLDataSet(Data,
+ CalculatedInputSize, CalculatedIdealSize, _mask)
+ {
+ LagWindowSize = LagWindowSize,
+ LeadWindowSize = LeadWindowSize
+ };
+ return result;
+ }
+
+ ///
+ public void Add(IMLData data1)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ ///
+ public void Add(IMLData inputData, IMLData idealData)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ ///
+ public void Add(IMLDataPair inputData)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ ///
+ public void Close()
+ {
+ // TODO Auto-generated method stub
+ }
+
+ ///
+ public int Count
+ {
+ get { return (int) GetRecordCount(); }
+ }
+
+ ///
+ public IMLDataPair this[int index]
+ {
+ get
+ {
+ if (index > Count)
+ {
+ return null;
+ }
+
+ var input = new BasicMLData(
+ CalculatedInputSize*CalculateLagCount());
+ var ideal = new BasicMLData(
+ CalculatedIdealSize*CalculateLeadCount());
+ IMLDataPair pair = new BasicMLDataPair(input, ideal);
+
+ GetRecord(index, pair);
+
+ return pair;
+ }
+ }
+
+ ///
+ public long GetRecordCount()
+ {
+ if (Data == null)
+ {
+ throw new EncogError(
+ "You must normalize the dataset before using it.");
+ }
+
+ if (_mask == null)
+ {
+ return Data.Length
+ - (LagWindowSize + LeadWindowSize);
+ }
+ return _mask.Length - (LagWindowSize + LeadWindowSize);
+ }
+
+ ///
+ /// Calculate the actual lead count.
+ ///
+ /// The actual lead count.
+ private int CalculateLagCount()
+ {
+ return (LagWindowSize <= 0)
+ ? 1
+ : (LagWindowSize + 1);
+ }
+
+ ///
+ /// Calculate the actual lead count.
+ ///
+ /// The actual lead count.
+ private int CalculateLeadCount()
+ {
+ return (LeadWindowSize <= 1) ? 1 : LeadWindowSize;
+ }
+
+ ///
+ public void GetRecord(long index, IMLDataPair pair)
+ {
+ if (Data == null)
+ {
+ throw new EncogError(
+ "You must normalize the dataset before using it.");
+ }
+
+ double[] inputArray = ((BasicMLData) pair.Input).Data;
+ double[] idealArray = ((BasicMLData) pair.Ideal).Data;
+
+ // Copy the input, account for time windows.
+ int inputSize = CalculateLagCount();
+ for (int i = 0; i < inputSize; i++)
+ {
+ double[] dataRow = LookupDataRow((int) (index + i));
+
+ EngineArray.ArrayCopy(dataRow, 0, inputArray, i
+ *CalculatedInputSize,
+ CalculatedInputSize);
+ }
+
+ // Copy the output, account for time windows.
+ int outputStart = (LeadWindowSize > 0) ? 1 : 0;
+ int outputSize = CalculateLeadCount();
+ for (int i = 0; i < outputSize; i++)
+ {
+ double[] dataRow = LookupDataRow((int) (index + i + outputStart));
+ EngineArray.ArrayCopy(dataRow, CalculatedInputSize,
+ idealArray, i
+ *CalculatedIdealSize,
+ CalculatedIdealSize);
+ }
+ }
+
+ ///
+ /// Find a row, using the mask.
+ ///
+ /// The index we seek.
+ /// The row.
+ private double[] LookupDataRow(int index)
+ {
+ if (_mask != null)
+ {
+ return Data[_mask[index]];
+ }
+ return Data[index];
+ }
+
+ ///
+ /// The enumerator for the matrix data set.
+ ///
+ [Serializable]
+ public class MatrixMLDataSetEnumerator : IEnumerator
+ {
+ ///
+ /// The owner.
+ ///
+ private readonly MatrixMLDataSet _owner;
+
+ ///
+ /// The current index.
+ ///
+ private int _current;
+
+ ///
+ /// Construct an enumerator.
+ ///
+ /// The owner of the enumerator.
+ public MatrixMLDataSetEnumerator(MatrixMLDataSet owner)
+ {
+ _current = -1;
+ _owner = owner;
+ }
+
+ ///
+ /// The current data item.
+ ///
+ public IMLDataPair Current
+ {
+ get { return InternalCurrent(); }
+ }
+
+ ///
+ /// Dispose of this object.
+ ///
+ public void Dispose()
+ {
+ // nothing needed
+ }
+
+ ///
+ /// The current item.
+ ///
+ object IEnumerator.Current
+ {
+ get { return InternalCurrent(); }
+ }
+
+ ///
+ /// Move to the next item.
+ ///
+ /// True if there is a next item.
+ public bool MoveNext()
+ {
+ _current++;
+ if (_current >= _owner.Count)
+ return false;
+ return true;
+ }
+
+ ///
+ /// Reset to the beginning.
+ ///
+ public void Reset()
+ {
+ _current = -1;
+ }
+
+ private IMLDataPair InternalCurrent()
+ {
+ if (_current < 0)
+ {
+ throw new InvalidOperationException("Must call MoveNext before reading Current.");
+ }
+
+ return _owner[_current];
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Missing/IMissingHandler.cs b/encog-core-cs/ML/Data/Versatile/Missing/IMissingHandler.cs
new file mode 100644
index 00000000..923acc1a
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Missing/IMissingHandler.cs
@@ -0,0 +1,56 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Missing
+{
+ ///
+ ///
+ ///
+ public interface IMissingHandler
+ {
+ ///
+ /// Called by the normalizer to setup this handler.
+ ///
+ /// The normalizer that is being used.
+ void Init(NormalizationHelper normalizationHelper);
+
+ ///
+ /// Process a column's missing data.
+ ///
+ /// The column that is missing.
+ /// The value to use.
+ String ProcessString(ColumnDefinition colDef);
+
+ ///
+ /// Process a column's missing data.
+ ///
+ /// The column that is missing.
+ /// The value to use.
+ double ProcessDouble(ColumnDefinition colDef);
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Missing/MeanMissingHandler.cs b/encog-core-cs/ML/Data/Versatile/Missing/MeanMissingHandler.cs
new file mode 100644
index 00000000..8fc934b9
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Missing/MeanMissingHandler.cs
@@ -0,0 +1,51 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Missing
+{
+ ///
+ /// Handle missing data by using the mean value of that column.
+ ///
+ public class MeanMissingHandler: IMissingHandler
+ {
+ ///
+ public void Init(NormalizationHelper normalizationHelper)
+ {
+
+ }
+
+ ///
+ public String ProcessString(ColumnDefinition colDef)
+ {
+ throw new EncogError("The mean missing handler only accepts continuous numeric values.");
+ }
+
+ ///
+ public double ProcessDouble(ColumnDefinition colDef)
+ {
+ return colDef.Mean;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/NormalizationHelper.cs b/encog-core-cs/ML/Data/Versatile/NormalizationHelper.cs
new file mode 100644
index 00000000..9194c81e
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/NormalizationHelper.cs
@@ -0,0 +1,353 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Data.Basic;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Missing;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.Util.CSV;
+
+namespace Encog.ML.Data.Versatile
+{
+ ///
+ /// This class is used to perform normalizations for methods trained with the
+ /// versatile dataset.
+ ///
+ [Serializable]
+ public class NormalizationHelper
+ {
+ ///
+ /// The columns, from the source columns, used for input to the model.
+ ///
+ private readonly IList _inputColumns = new List();
+
+ ///
+ /// The missing column handlers.
+ ///
+ private readonly IDictionary _missingHandlers =
+ new Dictionary();
+
+ ///
+ /// The columns, from the source columns, used for output from the model.
+ ///
+ private readonly IList _outputColumns = new List();
+
+ ///
+ /// The source columns from the original file. These are then added to the
+ /// input and output columns.
+ ///
+ private readonly IList _sourceColumns = new List();
+
+ ///
+ /// What to do with unknown values.
+ ///
+ private readonly IList _unknownValues = new List();
+
+ ///
+ /// Default constructor;
+ ///
+ public NormalizationHelper()
+ {
+ Format = CSVFormat.English;
+ }
+
+ ///
+ /// The normalizaton strategy to use.
+ ///
+ public INormalizationStrategy NormStrategy { get; set; }
+
+ ///
+ /// The CSV format to use.
+ ///
+ public CSVFormat Format { get; set; }
+
+ ///
+ /// The source columns.
+ ///
+ public IList SourceColumns
+ {
+ get { return _sourceColumns; }
+ }
+
+
+ ///
+ /// The input columns.
+ ///
+ public IList InputColumns
+ {
+ get { return _inputColumns; }
+ }
+
+
+ ///
+ /// The output columns.
+ ///
+ public IList OutputColumns
+ {
+ get { return _outputColumns; }
+ }
+
+ ///
+ /// The unknown values.
+ ///
+ public IList UnknownValues
+ {
+ get { return _unknownValues; }
+ }
+
+ ///
+ /// Add a source column. These define the raw input.
+ ///
+ /// The column definition.
+ public void AddSourceColumn(ColumnDefinition def)
+ {
+ _sourceColumns.Add(def);
+ def.Owner = this;
+ }
+
+ ///
+ /// Define a source column. These define the raw input. Use this function if
+ /// you know the index of the column in a non-header file.
+ ///
+ /// The name of the column.
+ /// The index of the column, needed for non-headered files.
+ /// The column type.
+ /// The column definition
+ public ColumnDefinition DefineSourceColumn(string name, int index,
+ ColumnType colType)
+ {
+ var result = new ColumnDefinition(name, colType) {Index = index};
+ AddSourceColumn(result);
+ return result;
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[NormalizationHelper:\n");
+ foreach (ColumnDefinition colDef in _sourceColumns)
+ {
+ result.Append(colDef);
+ result.Append("\n");
+ }
+ result.Append("]");
+ return result.ToString();
+ }
+
+ ///
+ /// Clear the input/output columns, but not the source columns.
+ ///
+ public void ClearInputOutput()
+ {
+ _inputColumns.Clear();
+ _outputColumns.Clear();
+ }
+
+ ///
+ /// Normalize a single input column.
+ ///
+ /// The column definition index (from the input columns).
+ /// The value to normalize.
+ /// The normalized result.
+ public double[] NormalizeInputColumn(int i, String value)
+ {
+ ColumnDefinition colDef = _inputColumns[i];
+ var result = new double[NormStrategy.NormalizedSize(colDef,
+ true)];
+ NormStrategy.NormalizeColumn(colDef, true, value, result, 0);
+ return result;
+ }
+
+ ///
+ /// Normalize a single output column.
+ ///
+ /// The column definition index (from the output columns).
+ /// The value to normalize.
+ /// The normalized result.
+ public double[] NormalizeOutputColumn(int i, string value)
+ {
+ ColumnDefinition colDef = _outputColumns[i];
+ var result = new double[NormStrategy.NormalizedSize(colDef,
+ false)];
+ NormStrategy.NormalizeColumn(colDef, false, value, result, 0);
+ return result;
+ }
+
+ ///
+ /// Calculate the number of elements the input will normalize to.
+ ///
+ /// The number of elements the input will normalize to.
+ public int CalculateNormalizedInputCount()
+ {
+ return _inputColumns.Sum(colDef => NormStrategy.NormalizedSize(colDef, true));
+ }
+
+ ///
+ /// Calculate the number of elements the output will normalize to.
+ ///
+ /// The number of elements the output will normalize to.
+ public int CalculateNormalizedOutputCount()
+ {
+ return _outputColumns.Sum(colDef => NormStrategy.NormalizedSize(colDef, false));
+ }
+
+ ///
+ /// Allocate a data item large enough to hold a single input vector.
+ ///
+ /// The data element.
+ public IMLData AllocateInputVector()
+ {
+ return AllocateInputVector(1);
+ }
+
+ ///
+ /// Allocate a data item large enough to hold several input vectors. This is
+ /// normally used for timeslices.
+ ///
+ /// How many input vectors.
+ /// The data element.
+ public IMLData AllocateInputVector(int multiplier)
+ {
+ return new BasicMLData(CalculateNormalizedInputCount()*multiplier);
+ }
+
+ ///
+ /// Denormalize a complete output vector to an array of strings.
+ ///
+ /// The data vector to denorm, the source.
+ /// The denormalized vector.
+ public String[] DenormalizeOutputVectorToString(IMLData output)
+ {
+ var result = new String[_outputColumns.Count];
+
+ int idx = 0;
+ for (int i = 0; i < _outputColumns.Count; i++)
+ {
+ ColumnDefinition colDef = _outputColumns[i];
+ result[i] = NormStrategy.DenormalizeColumn(colDef, false,
+ output, idx);
+ idx += NormStrategy.NormalizedSize(colDef, false);
+ }
+
+ return result;
+ }
+
+ ///
+ /// Define the string that signifies an unknown value (eg "?")
+ ///
+ /// The string for unknowns.
+ public void DefineUnknownValue(String str)
+ {
+ _unknownValues.Add(str);
+ }
+
+ ///
+ /// Normalize a single column to the input vector.
+ ///
+ /// The column to normalize.
+ /// The current position in the vector.
+ /// The vector to output to.
+ /// Is this an input column.
+ /// The value to normalize.
+ /// The new current position in the vector.
+ public int NormalizeToVector(ColumnDefinition colDef, int outputColumn,
+ double[] output, bool isInput, String value)
+ {
+ IMissingHandler handler = null;
+
+ if (_unknownValues.Contains(value))
+ {
+ if (!_missingHandlers.ContainsKey(colDef))
+ {
+ throw new EncogError(
+ "Do not know how to process missing value \"" + value
+ + "\" in field: " + colDef.Name);
+ }
+ handler = _missingHandlers[colDef];
+ }
+
+ if (colDef.DataType == ColumnType.Continuous)
+ {
+ double d = ParseDouble(value);
+ if (handler != null)
+ {
+ d = handler.ProcessDouble(colDef);
+ }
+ return NormStrategy.NormalizeColumn(colDef, isInput, d,
+ output, outputColumn);
+ }
+ if (handler != null)
+ {
+ value = handler.ProcessString(colDef);
+ }
+ return NormStrategy.NormalizeColumn(colDef, isInput, value,
+ output, outputColumn);
+ }
+
+ ///
+ /// Parse a double, using the correct formatter.
+ ///
+ /// The string.
+ /// The double.
+ public double ParseDouble(string str)
+ {
+ return Format.Parse(str);
+ }
+
+ ///
+ /// Define a missing value handler.
+ ///
+ /// The column this handler applies to.
+ /// The handler.
+ public void DefineMissingHandler(ColumnDefinition colDef,
+ IMissingHandler handler)
+ {
+ _missingHandlers[colDef] = handler;
+ handler.Init(this);
+ }
+
+ ///
+ /// Normalize a string array to an input vector.
+ ///
+ /// The unnormalized string array.
+ /// The output data.
+ /// Should the output be forced into the original column order?
+ public void NormalizeInputVector(String[] line, double[] data,
+ bool originalOrder)
+ {
+ int outputIndex = 0;
+ int i = 0;
+ foreach (ColumnDefinition colDef in _inputColumns)
+ {
+ int idx = originalOrder ? _sourceColumns.IndexOf(colDef) : i;
+ outputIndex = NormalizeToVector(colDef, outputIndex, data, false,
+ line[idx]);
+ i++;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/INormalizer.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/INormalizer.cs
new file mode 100644
index 00000000..e9643354
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/INormalizer.cs
@@ -0,0 +1,73 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// The normalizer interface defines how to normalize a column. The source of the
+ /// normalization can be either string or double.
+ ///
+ public interface INormalizer
+ {
+ ///
+ /// Determine the normalized size of the specified column.
+ ///
+ /// The column to check.
+ /// The size of the column normalized.
+ int OutputSize(ColumnDefinition colDef);
+
+ ///
+ /// Normalize a column from a string. The output will go to an array, starting at outputColumn.
+ ///
+ /// The column that is being normalized.
+ /// The value to normalize.
+ /// The array to output to.
+ /// The index to start at in outputData.
+ /// The new index (in outputData) that we've moved to.
+ int NormalizeColumn(ColumnDefinition colDef, String value,
+ double[] outputData, int outputIndex);
+
+ ///
+ /// Normalize a column from a double. The output will go to an array, starting at outputColumn.
+ ///
+ /// The column that is being normalized.
+ /// The value to normalize.
+ /// The array to output to.
+ /// The index to start at in outputData.
+ /// The new index (in outputData) that we've moved to.
+ int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputIndex);
+
+ ///
+ /// Denormalize a value.
+ ///
+ /// The column to denormalize.
+ /// The data to denormalize.
+ /// The starting location inside data.
+ /// The denormalized value.
+ String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataIndex);
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/IndexedNormalizer.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/IndexedNormalizer.cs
new file mode 100644
index 00000000..249145ee
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/IndexedNormalizer.cs
@@ -0,0 +1,70 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// Normalize ordinal/nominal values to a single value that is simply the index
+ /// of the class in the list. For example, "one", "two", "three" normalizes to
+ /// 0,1,2.
+ ///
+ public class IndexedNormalizer : INormalizer
+ {
+ ///
+ public int OutputSize(ColumnDefinition colDef)
+ {
+ return 1;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, String value,
+ double[] outputData, int outputColumn)
+ {
+ if (!colDef.Classes.Contains(value))
+ {
+ throw new EncogError("Undefined value: " + value);
+ }
+
+ outputData[outputColumn] = colDef.Classes.IndexOf(value);
+ return outputColumn + 1;
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataColumn)
+ {
+ return colDef.Classes[(int) data[dataColumn]];
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputColumn)
+ {
+ throw new EncogError(
+ "Can't use an indexed normalizer on a continuous value: "
+ + value);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/OneOfNNormalizer.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/OneOfNNormalizer.cs
new file mode 100644
index 00000000..51d59058
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/OneOfNNormalizer.cs
@@ -0,0 +1,109 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// Normalize to one-of-n for nominal values. For example, "one", "two", "three"
+ /// becomes 1,0,0 and 0,1,0 and 0,0,1 etc. Assuming 0 and 1 were the min/max.
+ ///
+ [Serializable]
+ public class OneOfNNormalizer : INormalizer
+ {
+ ///
+ /// The normalized high.
+ ///
+ private readonly double _normalizedHigh;
+
+ ///
+ /// The normalized low.
+ ///
+ private readonly double _normalizedLow;
+
+ ///
+ /// Construct the normalizer.
+ ///
+ /// The normalized low.
+ /// The normalized high.
+ public OneOfNNormalizer(double theNormalizedLow, double theNormalizedHigh)
+ {
+ _normalizedLow = theNormalizedLow;
+ _normalizedHigh = theNormalizedHigh;
+ }
+
+ ///
+ public int OutputSize(ColumnDefinition colDef)
+ {
+ return colDef.Classes.Count;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, String value,
+ double[] outputData, int outputColumn)
+ {
+ for (int i = 0; i < colDef.Classes.Count; i++)
+ {
+ double d = _normalizedLow;
+
+ if (colDef.Classes[i].Equals(value))
+ {
+ d = _normalizedHigh;
+ }
+
+ outputData[outputColumn + i] = d;
+ }
+ return outputColumn + colDef.Classes.Count;
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataColumn)
+ {
+ double bestValue = Double.NegativeInfinity;
+ int bestIndex = 0;
+
+ for (int i = 0; i < data.Count; i++)
+ {
+ double d = data[dataColumn + i];
+ if (d > bestValue)
+ {
+ bestValue = d;
+ bestIndex = i;
+ }
+ }
+
+ return colDef.Classes[bestIndex];
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputColumn)
+ {
+ throw new EncogError(
+ "Can't use a one-of-n normalizer on a continuous value: "
+ + value);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/PassThroughNormalizer.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/PassThroughNormalizer.cs
new file mode 100644
index 00000000..e2a66b80
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/PassThroughNormalizer.cs
@@ -0,0 +1,61 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// A normalizer that simply passes the value through unnormalized.
+ ///
+ public class PassThroughNormalizer : INormalizer
+ {
+ ///
+ public int OutputSize(ColumnDefinition colDef)
+ {
+ return 1;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, String value,
+ double[] outputData, int outputColumn)
+ {
+ throw new EncogError("Can't use a pass-through normalizer on a string value: " + value);
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataColumn)
+ {
+ return "" + data[dataColumn];
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputColumn)
+ {
+ outputData[outputColumn] = value;
+ return outputColumn + 1;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/RangeNormalizer.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/RangeNormalizer.cs
new file mode 100644
index 00000000..9cf03e82
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/RangeNormalizer.cs
@@ -0,0 +1,107 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// A a range normalizer forces a value to fall in a specific range.
+ ///
+ [Serializable]
+ public class RangeNormalizer : INormalizer
+ {
+ ///
+ /// The normalized high value.
+ ///
+ private readonly double _normalizedHigh;
+
+ ///
+ /// The normalized low value.
+ ///
+ private readonly double _normalizedLow;
+
+ ///
+ /// Construct the range normalizer.
+ ///
+ /// The normalized low value.
+ /// The normalized high value.
+ public RangeNormalizer(double theNormalizedLow, double theNormalizedHigh)
+ {
+ _normalizedLow = theNormalizedLow;
+ _normalizedHigh = theNormalizedHigh;
+ }
+
+ ///
+ public int OutputSize(ColumnDefinition colDef)
+ {
+ return 1;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, String value,
+ double[] outputData, int outputColumn)
+ {
+ throw new EncogError("Can't range-normalize a string value: " + value);
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputColumn)
+ {
+ double result = ((value - colDef.Low)/(colDef.High - colDef.Low))
+ *(_normalizedHigh - _normalizedLow)
+ + _normalizedLow;
+
+ // typically caused by a number that should not have been normalized
+ // (i.e. normalization or actual range is infinitely small.
+ if (Double.IsNaN(result))
+ {
+ result = ((_normalizedHigh - _normalizedLow)/2) + _normalizedLow;
+ }
+
+ outputData[outputColumn] = result;
+
+ return outputColumn + 1;
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataColumn)
+ {
+ double value = data[dataColumn];
+ double result = ((colDef.Low - colDef.High)*value
+ - _normalizedHigh*colDef.Low + colDef.High
+ *_normalizedLow)
+ /(_normalizedLow - _normalizedHigh);
+
+ // typically caused by a number that should not have been normalized
+ // (i.e. normalization or actual range is infinitely small.
+ if (Double.IsNaN(result))
+ {
+ return "" + (((_normalizedHigh - _normalizedLow)/2) + _normalizedLow);
+ }
+ return "" + result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/RangeOrdinal.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/RangeOrdinal.cs
new file mode 100644
index 00000000..d64903ec
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/RangeOrdinal.cs
@@ -0,0 +1,123 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers
+{
+ ///
+ /// Normalize an ordinal into a specific range. An ordinal is a string value that
+ /// has order. For example "first grade", "second grade", ... "freshman", ...,
+ /// "senior". These values are mapped to an increasing index.
+ ///
+ [Serializable]
+ public class RangeOrdinal : INormalizer
+ {
+ ///
+ /// The high range of the normalized data.
+ ///
+ private readonly double _normalizedHigh;
+
+ ///
+ /// The low range of the normalized data.
+ ///
+ private readonly double _normalizedLow;
+
+ ///
+ /// Construct with normalized high and low.
+ ///
+ /// The normalized low value.
+ /// The normalized high value.
+ public RangeOrdinal(double theNormalizedLow, double theNormalizedHigh)
+ {
+ _normalizedLow = theNormalizedLow;
+ _normalizedHigh = theNormalizedHigh;
+ }
+
+ ///
+ public int OutputSize(ColumnDefinition colDef)
+ {
+ return 1;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, String theValue,
+ double[] outputData, int outputColumn)
+ {
+ // Find the index of the ordinal
+ int v = colDef.Classes.IndexOf(theValue);
+ if (v == -1)
+ {
+ throw new EncogError("Unknown ordinal: " + theValue);
+ }
+
+ double high = colDef.Classes.Count;
+ double value = v;
+
+ double result = (value/high)
+ *(_normalizedHigh - _normalizedLow)
+ + _normalizedLow;
+
+ // typically caused by a number that should not have been normalized
+ // (i.e. normalization or actual range is infinitely small.
+ if (Double.IsNaN(result))
+ {
+ result = ((_normalizedHigh - _normalizedLow)/2)
+ + _normalizedLow;
+ }
+
+ outputData[outputColumn] = result;
+
+ return outputColumn + 1;
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, double value,
+ double[] outputData, int outputColumn)
+ {
+ throw new EncogError(
+ "Can't ordinal range-normalize a continuous value: " + value);
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, IMLData data,
+ int dataColumn)
+ {
+ double high = colDef.Classes.Count;
+ double low = 0;
+
+ double value = data[dataColumn];
+ double result = ((low - high)*value - _normalizedHigh*low + high
+ *_normalizedLow)
+ /(_normalizedLow - _normalizedHigh);
+
+ // typically caused by a number that should not have been normalized
+ // (i.e. normalization or actual range is infinitely small.
+ if (Double.IsNaN(result))
+ {
+ return colDef.Classes[0];
+ }
+ return colDef.Classes[(int) result];
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/BasicNormalizationStrategy.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/BasicNormalizationStrategy.cs
new file mode 100644
index 00000000..58208474
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/BasicNormalizationStrategy.cs
@@ -0,0 +1,174 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers.Strategy
+{
+ ///
+ /// Provides a basic normalization strategy that will work with most models built into Encog.
+ /// This is often used as a starting point for building more customized models, as this
+ /// normalizer works mainly by using maps to define which normalizer to use for what
+ /// data type.
+ ///
+ [Serializable]
+ public class BasicNormalizationStrategy : INormalizationStrategy
+ {
+ ///
+ /// Mapping to all of the input normalizers.
+ ///
+ private readonly IDictionary _inputNormalizers =
+ new Dictionary();
+
+ ///
+ /// Mapping to all of the output normalizers.
+ ///
+ private readonly IDictionary _outputNormalizers =
+ new Dictionary();
+
+ ///
+ /// Construct the basic normalization strategy.
+ ///
+ /// The desired low to normalize input into.
+ /// The desired high to normalize input into.
+ /// The desired low to normalize output into.
+ /// The desired high to normalize output into.
+ public BasicNormalizationStrategy(double inputLow, double inputHigh, double outputLow, double outputHigh)
+ {
+ AssignInputNormalizer(ColumnType.Continuous, new RangeNormalizer(inputLow, inputHigh));
+ AssignInputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(inputLow, inputHigh));
+ AssignInputNormalizer(ColumnType.Ordinal, new RangeOrdinal(inputLow, inputHigh));
+
+ AssignOutputNormalizer(ColumnType.Continuous, new RangeNormalizer(outputLow, outputHigh));
+ AssignOutputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(outputLow, outputHigh));
+ AssignOutputNormalizer(ColumnType.Ordinal, new RangeOrdinal(outputLow, outputHigh));
+ }
+
+ ///
+ /// Default constructor.
+ ///
+ public BasicNormalizationStrategy()
+ {
+ }
+
+ ///
+ /// The input normalizers.
+ ///
+ public IDictionary InputNormalizers
+ {
+ get { return _inputNormalizers; }
+ }
+
+ ///
+ /// The output normalizers.
+ ///
+ public IDictionary OutputNormalizers
+ {
+ get { return _outputNormalizers; }
+ }
+
+ ///
+ public int NormalizedSize(ColumnDefinition colDef, bool isInput)
+ {
+ INormalizer norm = FindNormalizer(colDef, isInput);
+ return norm.OutputSize(colDef);
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, bool isInput,
+ String value, double[] outputData, int outputColumn)
+ {
+ INormalizer norm = FindNormalizer(colDef, isInput);
+ return norm.NormalizeColumn(colDef, value, outputData, outputColumn);
+ }
+
+ ///
+ public int NormalizeColumn(ColumnDefinition colDef, bool isInput,
+ double value, double[] outputData, int outputColumn)
+ {
+ INormalizer norm = FindNormalizer(colDef, isInput);
+ return norm.NormalizeColumn(colDef, value, outputData, outputColumn);
+ }
+
+ ///
+ public String DenormalizeColumn(ColumnDefinition colDef, bool isInput,
+ IMLData data, int dataColumn)
+ {
+ INormalizer norm = FindNormalizer(colDef, isInput);
+ return norm.DenormalizeColumn(colDef, data, dataColumn);
+ }
+
+ ///
+ /// Assign a normalizer to the specified column type for output.
+ ///
+ /// The column type.
+ /// The normalizer.
+ public void AssignInputNormalizer(ColumnType colType, INormalizer norm)
+ {
+ _inputNormalizers[colType] = norm;
+ }
+
+ ///
+ /// Assign a normalizer to the specified column type for output.
+ ///
+ /// The column type.
+ /// The normalizer.
+ public void AssignOutputNormalizer(ColumnType colType, INormalizer norm)
+ {
+ _outputNormalizers[colType] = norm;
+ }
+
+ ///
+ /// Find a normalizer for the specified column definition, and if it is input or output.
+ ///
+ /// The column definition.
+ /// True if the column is input.
+ /// The normalizer to use.
+ private INormalizer FindNormalizer(ColumnDefinition colDef, bool isInput)
+ {
+ INormalizer norm = null;
+
+ if (isInput)
+ {
+ if (_inputNormalizers.ContainsKey(colDef.DataType))
+ {
+ norm = _inputNormalizers[colDef.DataType];
+ }
+ }
+ else
+ {
+ if (_outputNormalizers.ContainsKey(colDef.DataType))
+ {
+ norm = _outputNormalizers[colDef.DataType];
+ }
+ }
+
+ if (norm == null)
+ {
+ throw new EncogError("No normalizer defined for input=" + isInput + ", type=" + colDef.DataType);
+ }
+ return norm;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/INormalizationStrategy.cs b/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/INormalizationStrategy.cs
new file mode 100644
index 00000000..12c6787f
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Normalizers/Strategy/INormalizationStrategy.cs
@@ -0,0 +1,76 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.Data.Versatile.Columns;
+
+namespace Encog.ML.Data.Versatile.Normalizers.Strategy
+{
+ ///
+ /// Defines the interface to a normalization strategy.
+ ///
+ public interface INormalizationStrategy
+ {
+ ///
+ /// Calculate how many elements a column will normalize into.
+ ///
+ /// The column definition.
+ /// True, if this is an input column.
+ /// The number of elements needed to normalize this column.
+ int NormalizedSize(ColumnDefinition colDef, bool isInput);
+
+ ///
+ /// Normalize a column, with a string input.
+ ///
+ /// The column definition.
+ /// True, if this is an input column.
+ /// The value to normalize.
+ /// The output data.
+ /// The element to begin outputing to.
+ /// The new output element, advanced by the correct amount.
+ int NormalizeColumn(ColumnDefinition colDef, bool isInput, string value,
+ double[] outpuData, int outputColumn);
+
+
+ ///
+ /// Normalize a column, with a double input.
+ ///
+ /// The column definition.
+ /// True, if this is an input column.
+ /// The value to normalize.
+ /// The output data.
+ /// The new output element, advanced by the correct amount.
+ string DenormalizeColumn(ColumnDefinition colDef, bool isInput, IMLData output,
+ int idx);
+
+ ///
+ /// Normalize a column, with a double value.
+ ///
+ /// The column definition.
+ /// True, if this is an input column.
+ /// The value to normalize.
+ /// The output data.
+ /// The element to begin outputing to.
+ /// The new output element, advanced by the correct amount.
+ int NormalizeColumn(ColumnDefinition colDef, bool isInput, double value,
+ double[] outpuData, int outputColumn);
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Sources/CSVDataSource.cs b/encog-core-cs/ML/Data/Versatile/Sources/CSVDataSource.cs
new file mode 100644
index 00000000..26fe4a32
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Sources/CSVDataSource.cs
@@ -0,0 +1,145 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.Util.CSV;
+
+namespace Encog.ML.Data.Versatile.Sources
+{
+ ///
+ /// Allow a CSV file to serve as a source for the versatile data source.
+ ///
+ public class CSVDataSource : IVersatileDataSource
+ {
+ ///
+ /// The file to read.
+ ///
+ private readonly string _file;
+
+ ///
+ /// The CSV format of the file.
+ ///
+ private readonly CSVFormat _format;
+
+ ///
+ /// The index values for each header, if we have headers.
+ ///
+ private readonly IDictionary _headerIndex = new Dictionary();
+
+ ///
+ /// True, if the file has headers.
+ ///
+ private readonly bool _headers;
+
+ ///
+ /// The CSV reader.
+ ///
+ private ReadCSV _reader;
+
+ ///
+ /// Construct a CSV source from a filename. The format parameter specifies
+ /// the separator character to use, as well as the number format.
+ ///
+ /// The filename.
+ /// The headers.
+ /// The delimiter.
+ public CSVDataSource(string file, bool headers,
+ char delim)
+ {
+ _format = new CSVFormat(CSVFormat.DecimalCharacter,
+ delim);
+ _headers = headers;
+ _file = file;
+ }
+
+ ///
+ /// Construct a CSV source from a filename. Allows a delimiter character to
+ /// be specified.
+ ///
+ /// The filename.
+ /// The headers.
+ /// The format.
+ public CSVDataSource(string file, bool headers,
+ CSVFormat format)
+ {
+ _file = file;
+ _headers = headers;
+ _format = format;
+ }
+
+ ///
+ public string[] ReadLine()
+ {
+ if (_reader == null)
+ {
+ throw new EncogError("Please call rewind before reading the file.");
+ }
+
+ if (_reader.Next())
+ {
+ int len = _reader.ColumnCount;
+ var result = new string[len];
+ for (int i = 0; i < result.Length; i++)
+ {
+ result[i] = _reader.Get(i);
+ }
+ return result;
+ }
+ _reader.Close();
+ return null;
+ }
+
+ ///
+ public void Rewind()
+ {
+ if (_reader != null)
+ {
+ _reader.Close();
+ }
+ _reader = new ReadCSV(_file, _headers, _format);
+ if (_headerIndex.Count == 0)
+ {
+ for (int i = 0; i < _reader.ColumnNames.Count; i++)
+ {
+ _headerIndex[_reader.ColumnNames[i].ToLower()] = i;
+ }
+ }
+ }
+
+ ///
+ public int ColumnIndex(string name)
+ {
+ string name2 = name.ToLower();
+ if (!_headerIndex.ContainsKey(name2))
+ {
+ return -1;
+ }
+ return _headerIndex[name2];
+ }
+
+ public void Close()
+ {
+ _reader.Close();
+ _reader = null;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/Sources/IVersatileDataSource.cs b/encog-core-cs/ML/Data/Versatile/Sources/IVersatileDataSource.cs
new file mode 100644
index 00000000..737b6b25
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/Sources/IVersatileDataSource.cs
@@ -0,0 +1,55 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.Data.Versatile.Sources
+{
+ ///
+ /// Defines a data source for the versatile data set.
+ ///
+ public interface IVersatileDataSource
+ {
+ ///
+ /// Read a line from the source.
+ ///
+ /// The values read.
+ String[] ReadLine();
+
+ ///
+ /// Rewind the source back to the beginning.
+ ///
+ void Rewind();
+
+ ///
+ /// Obtain the column index for the specified name.
+ ///
+ /// The column name.
+ /// The column index, or -1 if not found.
+ int ColumnIndex(string name);
+
+ ///
+ /// Close the source.
+ ///
+ void Close();
+ }
+}
diff --git a/encog-core-cs/ML/Data/Versatile/VersatileMLDataSet.cs b/encog-core-cs/ML/Data/Versatile/VersatileMLDataSet.cs
new file mode 100644
index 00000000..7fe9bf09
--- /dev/null
+++ b/encog-core-cs/ML/Data/Versatile/VersatileMLDataSet.cs
@@ -0,0 +1,294 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.MathUtil.Randomize.Generate;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Division;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Data.Versatile.Sources;
+using Encog.Util;
+
+namespace Encog.ML.Data.Versatile
+{
+ ///
+ /// The versatile dataset supports several advanced features. 1. it can directly
+ /// read and normalize from a CSV file. 2. It supports virtual time-boxing for
+ /// time series data (the data is NOT expanded in memory). 3. It can easily be
+ /// segmented into smaller datasets.
+ ///
+ public class VersatileMLDataSet : MatrixMLDataSet
+ {
+ ///
+ /// The source that data is being pulled from.
+ ///
+ private readonly IVersatileDataSource _source;
+
+ ///
+ /// The number of rows that were analyzed.
+ ///
+ private int _analyzedRows;
+
+ ///
+ /// Construct the data source.
+ ///
+ /// The data source.
+ public VersatileMLDataSet(IVersatileDataSource theSource) : this()
+ {
+ _source = theSource;
+ }
+
+ ///
+ /// The normalization helper.
+ ///
+ public NormalizationHelper NormHelper { get; set; }
+
+ ///
+ /// Default constructor.
+ ///
+ public VersatileMLDataSet()
+ {
+ NormHelper = new NormalizationHelper();
+ }
+
+ ///
+ /// Find the index of a column.
+ ///
+ /// The column.
+ /// The column index.
+ private int FindIndex(ColumnDefinition colDef)
+ {
+ if (colDef.Index != -1)
+ {
+ return colDef.Index;
+ }
+
+ int index = _source.ColumnIndex(colDef.Name);
+ colDef.Index = index;
+
+ if (index == -1)
+ {
+ throw new EncogError("Can't find column");
+ }
+
+ return index;
+ }
+
+ ///
+ /// Analyze the input and determine max, min, mean, etc.
+ ///
+ public void Analyze()
+ {
+ String[] line;
+
+ // Collect initial stats: sums (for means), highs, lows.
+ _source.Rewind();
+ int c = 0;
+ while ((line = _source.ReadLine()) != null)
+ {
+ c++;
+ foreach (ColumnDefinition colDef in NormHelper.SourceColumns)
+ {
+ int index = FindIndex(colDef);
+ String value = line[index];
+ colDef.Analyze(value);
+ }
+ }
+ _analyzedRows = c;
+
+ // Calculate the means, and reset for sd calc.
+ foreach (ColumnDefinition colDef in NormHelper.SourceColumns)
+ {
+ // Only calculate mean/sd for continuous columns.
+ if (colDef.DataType == ColumnType.Continuous)
+ {
+ colDef.Mean = colDef.Mean/colDef.Count;
+ colDef.Sd = 0;
+ }
+ }
+
+ // Sum the standard deviation
+ _source.Rewind();
+ while ((line = _source.ReadLine()) != null)
+ {
+ for (int i = 0; i < NormHelper.SourceColumns.Count; i++)
+ {
+ ColumnDefinition colDef = NormHelper.SourceColumns[i];
+ String value = line[i];
+ if (colDef.DataType == ColumnType.Continuous)
+ {
+ double d = NormHelper.ParseDouble(value);
+ d = colDef.Mean - d;
+ d = d*d;
+ colDef.Sd = colDef.Sd + d;
+ }
+ }
+ }
+
+ // Calculate the standard deviations.
+ foreach (ColumnDefinition colDef in NormHelper.SourceColumns)
+ {
+ // Only calculate sd for continuous columns.
+ if (colDef.DataType == ColumnType.Continuous)
+ {
+ colDef.Sd = Math.Sqrt(colDef.Sd/colDef.Count);
+ }
+ }
+ }
+
+ ///
+ /// Normalize the data set, and allocate memory to hold it.
+ ///
+ public void Normalize()
+ {
+ INormalizationStrategy strat = NormHelper.NormStrategy;
+
+ if (strat == null)
+ {
+ throw new EncogError(
+ "Please choose a model type first, with selectMethod.");
+ }
+
+ int normalizedInputColumns = NormHelper
+ .CalculateNormalizedInputCount();
+ int normalizedOutputColumns = NormHelper
+ .CalculateNormalizedOutputCount();
+
+ int normalizedColumns = normalizedInputColumns
+ + normalizedOutputColumns;
+ CalculatedIdealSize = normalizedOutputColumns;
+ CalculatedInputSize = normalizedInputColumns;
+
+ Data = EngineArray.AllocateDouble2D(_analyzedRows, normalizedColumns);
+
+ _source.Rewind();
+ String[] line;
+ int row = 0;
+ while ((line = _source.ReadLine()) != null)
+ {
+ int column = 0;
+ foreach (ColumnDefinition colDef in NormHelper.InputColumns)
+ {
+ int index = FindIndex(colDef);
+ string value = line[index];
+
+ column = NormHelper.NormalizeToVector(colDef, column,
+ Data[row], true, value);
+ }
+
+ foreach (ColumnDefinition colDef in NormHelper.OutputColumns)
+ {
+ int index = FindIndex(colDef);
+ string value = line[index];
+
+ column = NormHelper.NormalizeToVector(colDef, column,
+ Data[row], false, value);
+ }
+ row++;
+ }
+ }
+
+ ///
+ /// Define a source column. Used when the file does not contain headings.
+ ///
+ /// The name of the column.
+ /// The index of the column.
+ /// The column type.
+ /// The column definition.
+ public ColumnDefinition DefineSourceColumn(String name, int index,
+ ColumnType colType)
+ {
+ return NormHelper.DefineSourceColumn(name, index, colType);
+ }
+
+ ///
+ /// Divide, and optionally shuffle, the dataset.
+ ///
+ /// The desired divisions.
+ /// True, if we should shuffle.
+ /// Random number generator, often with a specific seed.
+ public void Divide(IList dataDivisionList, bool shuffle,
+ IGenerateRandom rnd)
+ {
+ if (Data == null)
+ {
+ throw new EncogError(
+ "Can't divide, data has not yet been generated/normalized.");
+ }
+
+ var divide = new PerformDataDivision(shuffle, rnd);
+ divide.Perform(dataDivisionList, this, CalculatedInputSize,
+ CalculatedIdealSize);
+ }
+
+ ///
+ /// Define an output column.
+ ///
+ /// The output column.
+ public void DefineOutput(ColumnDefinition col)
+ {
+ NormHelper.OutputColumns.Add(col);
+ }
+
+ ///
+ /// Define an input column.
+ ///
+ /// The input column.
+ public void DefineInput(ColumnDefinition col)
+ {
+ NormHelper.InputColumns.Add(col);
+ }
+
+ ///
+ /// Define a single column as an output column, all others as inputs.
+ ///
+ /// The output column.
+ public void DefineSingleOutputOthersInput(ColumnDefinition outputColumn)
+ {
+ NormHelper.ClearInputOutput();
+
+ foreach (ColumnDefinition colDef in NormHelper.SourceColumns)
+ {
+ if (colDef == outputColumn)
+ {
+ DefineOutput(colDef);
+ }
+ else if (colDef.DataType != ColumnType.Ignore)
+ {
+ DefineInput(colDef);
+ }
+ }
+ }
+
+ ///
+ /// Define a source column.
+ ///
+ /// The name of the source column.
+ /// The column type.
+ /// The column definition.
+ public ColumnDefinition DefineSourceColumn(string name, ColumnType colType)
+ {
+ return NormHelper.DefineSourceColumn(name, -1, colType);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Codec/GenomeAsPhenomeCODEC.cs b/encog-core-cs/ML/EA/Codec/GenomeAsPhenomeCODEC.cs
new file mode 100644
index 00000000..094c6f8e
--- /dev/null
+++ b/encog-core-cs/ML/EA/Codec/GenomeAsPhenomeCODEC.cs
@@ -0,0 +1,45 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Codec
+{
+ ///
+ /// This is a simple pass-through CODEC. This CODEC is used when the genome and
+ /// phenome are the same class, and no decoding is necessary.
+ ///
+ public class GenomeAsPhenomeCODEC : IGeneticCODEC
+ {
+ ///
+ public IMLMethod Decode(IGenome genome)
+ {
+ return genome;
+ }
+
+ ///
+ public IGenome Encode(IMLMethod phenotype)
+ {
+ return (IGenome) phenotype;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Codec/IGeneticCODEC.cs b/encog-core-cs/ML/EA/Codec/IGeneticCODEC.cs
new file mode 100644
index 00000000..55799431
--- /dev/null
+++ b/encog-core-cs/ML/EA/Codec/IGeneticCODEC.cs
@@ -0,0 +1,50 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Codec
+{
+ ///
+ /// A CODEC defines how to transfer between a genome and phenome. Every CODEC
+ /// should support genome to phenome. However, not every code can transform a
+ /// phenome into a genome.
+ ///
+ public interface IGeneticCODEC
+ {
+ ///
+ /// Decode the specified genome into a phenome. A phenome is an actual
+ /// instance of a genome that you can query.
+ ///
+ /// The genome to decode.
+ /// The phenome.
+ IMLMethod Decode(IGenome genome);
+
+ ///
+ /// Attempt to build a genome from a phenome. Note: not all CODEC's support
+ /// this. If it is unsupported, an exception will be thrown.
+ ///
+ /// The phenotype.
+ /// The genome.
+ IGenome Encode(IMLMethod phenotype);
+ }
+}
diff --git a/encog-core-cs/App/Analyst/CSV/ClusterRow.cs b/encog-core-cs/ML/EA/Exceptions/EACompileError.cs
similarity index 53%
rename from encog-core-cs/App/Analyst/CSV/ClusterRow.cs
rename to encog-core-cs/ML/EA/Exceptions/EACompileError.cs
index 589bab1a..d55ee144 100644
--- a/encog-core-cs/App/Analyst/CSV/ClusterRow.cs
+++ b/encog-core-cs/ML/EA/Exceptions/EACompileError.cs
@@ -1,59 +1,60 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.App.Analyst.CSV.Basic;
-using Encog.ML.Data.Basic;
-
-namespace Encog.App.Analyst.CSV
-{
- ///
- /// Holds input data and the CSV row for a cluster item.
- ///
- ///
- public class ClusterRow : BasicMLDataPair
- {
-
- ///
- /// The loaded row of data.
- ///
- ///
- private readonly LoadedRow _row;
-
- ///
- /// Construct the cluster row.
- ///
- ///
- /// The input data.
- /// The CSV row.
- public ClusterRow(double[] input, LoadedRow theRow) : base(new BasicMLData(input))
- {
- _row = theRow;
- }
-
-
- /// the row
- public LoadedRow Row
- {
- get { return _row; }
- }
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.EA.Exceptions
+{
+ ///
+ /// The genome has generated a compile error and is invalid.
+ ///
+ public class EACompileError : EAError
+ {
+ ///
+ /// Construct an error.
+ ///
+ /// The error.
+ public EACompileError(string msg)
+ : base(msg)
+ {
+ }
+
+ ///
+ /// Construct the error.
+ ///
+ /// The message.
+ /// The error.
+ public EACompileError(String msg, Exception t)
+ : base(msg, t)
+ {
+ }
+
+ ///
+ /// Construct the error.
+ ///
+ /// An error
+ public EACompileError(Exception t)
+ : base(t)
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genes/IGene.cs b/encog-core-cs/ML/EA/Exceptions/EAError.cs
similarity index 50%
rename from encog-core-cs/ML/Genetic/Genes/IGene.cs
rename to encog-core-cs/ML/EA/Exceptions/EAError.cs
index ea5ee5ec..23986e9a 100644
--- a/encog-core-cs/ML/Genetic/Genes/IGene.cs
+++ b/encog-core-cs/ML/EA/Exceptions/EAError.cs
@@ -1,64 +1,60 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Genes
-{
- ///
- /// Describes a gene. A gene is the smallest piece of genetic information in
- /// Encog.
- ///
- ///
- public interface IGene : IComparable
- {
- ///
- /// Get the ID of this gene, -1 for undefined.
- ///
- ///
- /// The ID of this gene.
- long Id {
- get; }
-
-
- /// The innovation ID of this gene.
- long InnovationId {
- get; }
-
-
- ///
- /// Determine if this gene is enabled.
- ///
- ///
- /// True if this gene is enabled.
- bool Enabled {
- get;
- set; }
-
- ///
- /// Copy another gene to this one.
- ///
- ///
- /// The other gene to copy.
- void Copy(IGene gene);
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.EA.Exceptions
+{
+ ///
+ /// A general evolutionary algorithm error.
+ ///
+ public class EAError : EncogError
+ {
+ ///
+ /// Construct the exception. Errors can be further divided into runtime or compile.
+ ///
+ /// The message.
+ public EAError(string msg)
+ : base(msg)
+ {
+ }
+
+ ///
+ /// Construct the exception.
+ ///
+ /// The message.
+ /// A throwable error.
+ public EAError(string msg, Exception t)
+ : base(msg, t)
+ {
+ }
+
+ ///
+ /// Construct the exception.
+ ///
+ /// A throwable error.
+ public EAError(Exception t)
+ : base(t)
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Exceptions/EARuntimeError.cs b/encog-core-cs/ML/EA/Exceptions/EARuntimeError.cs
new file mode 100644
index 00000000..5b5707ea
--- /dev/null
+++ b/encog-core-cs/ML/EA/Exceptions/EARuntimeError.cs
@@ -0,0 +1,60 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.EA.Exceptions
+{
+ ///
+ /// An error has occurred while running a phenotype (or genome).
+ ///
+ public class EARuntimeError : EAError
+ {
+ ///
+ /// Construct an error.
+ ///
+ /// The error.
+ public EARuntimeError(string msg)
+ : base(msg)
+ {
+ }
+
+ ///
+ /// Construct the error.
+ ///
+ /// The message.
+ /// The error.
+ public EARuntimeError(String msg, Exception t)
+ : base(msg, t)
+ {
+ }
+
+ ///
+ /// Construct the error.
+ ///
+ /// An error
+ public EARuntimeError(Exception t)
+ : base(t)
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Genome/BasicGenome.cs b/encog-core-cs/ML/EA/Genome/BasicGenome.cs
new file mode 100644
index 00000000..42ab7508
--- /dev/null
+++ b/encog-core-cs/ML/EA/Genome/BasicGenome.cs
@@ -0,0 +1,86 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Species;
+
+namespace Encog.ML.EA.Genome
+{
+ ///
+ /// A basic abstract genome. Provides base functionality.
+ ///
+ [Serializable]
+ public abstract class BasicGenome : IGenome
+ {
+ ///
+ /// Construct a basic genome.
+ ///
+ public BasicGenome()
+ {
+ Score = double.NaN;
+ AdjustedScore = double.NaN;
+ }
+
+ ///
+ /// The adjusted score. If unknown, it is set to NaN.
+ ///
+ public double AdjustedScore { get; set; }
+
+ ///
+ /// The score of this genome.
+ ///
+ public double Score { get; set; }
+
+ ///
+ /// The population this genome belongs to.
+ ///
+ public IPopulation Population { get; set; }
+
+ ///
+ /// The birth generation for this genome.
+ ///
+ public int BirthGeneration { get; set; }
+
+ ///
+ /// The species of this genome.
+ ///
+ public ISpecies Species { get; set; }
+
+
+ public abstract void Copy(IGenome source);
+
+ public abstract int Size { get; }
+
+ ///
+ public override string ToString()
+ {
+ var builder = new StringBuilder();
+ builder.Append("[");
+ builder.Append(GetType().Name);
+ builder.Append(": score=");
+ builder.Append(Score);
+ return builder.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Genome/IGenome.cs b/encog-core-cs/ML/EA/Genome/IGenome.cs
new file mode 100644
index 00000000..28df8954
--- /dev/null
+++ b/encog-core-cs/ML/EA/Genome/IGenome.cs
@@ -0,0 +1,74 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Species;
+
+namespace Encog.ML.EA.Genome
+{
+ ///
+ /// A genome is the basic blueprint for creating an phenome (organism) in Encog.
+ /// Some genomes also function as phenomes.
+ ///
+ public interface IGenome : IMLMethod
+ {
+ ///
+ /// Get the adjusted score, this considers old-age penalties and youth
+ /// bonuses. If there are no such bonuses or penalties, this is the same as
+ /// the score.
+ ///
+ double AdjustedScore { get; set; }
+
+ ///
+ /// The birth generation (or iteration).
+ ///
+ int BirthGeneration { get; set; }
+
+ ///
+ /// The population that this genome belongs to.
+ ///
+ IPopulation Population { get; set; }
+
+ ///
+ /// The score for this genome.
+ ///
+ double Score { get; set; }
+
+ ///
+ /// The size of this genome. This size is a relative number
+ /// that indicates the complexity of the genome.
+ ///
+ int Size { get; }
+
+
+ ///
+ /// The species for this genome.
+ ///
+ ISpecies Species { get; set; }
+
+ ///
+ /// Copy from the specified genome into this one.
+ ///
+ /// The source genome.
+ void Copy(IGenome source);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Genome/IGenomeFactory.cs b/encog-core-cs/ML/EA/Genome/IGenomeFactory.cs
new file mode 100644
index 00000000..bcf3f136
--- /dev/null
+++ b/encog-core-cs/ML/EA/Genome/IGenomeFactory.cs
@@ -0,0 +1,43 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.ML.EA.Genome
+{
+ ///
+ /// Defines a factory that produces genomes.
+ ///
+ public interface IGenomeFactory
+ {
+ ///
+ /// Factor a new genome.
+ ///
+ /// The newly created genome.
+ IGenome Factor();
+
+ ///
+ /// Create a clone of the other genome.
+ ///
+ /// The other genome.
+ /// The newly created clone.
+ IGenome Factor(IGenome other);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/CompoundOperator.cs b/encog-core-cs/ML/EA/Opp/CompoundOperator.cs
new file mode 100644
index 00000000..43e170aa
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/CompoundOperator.cs
@@ -0,0 +1,94 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Train;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.EA.Opp
+{
+ ///
+ /// A compound operator randomly chooses sub-operators to perform the actual
+ /// operation. Each of the sub-operators can be provided with a weighting.
+ ///
+ public class CompoundOperator : IEvolutionaryOperator
+ {
+ ///
+ /// The sub-operators that make up this compound operator.
+ ///
+ private readonly OperationList _components = new OperationList();
+
+ ///
+ /// The owner of this operator.
+ ///
+ private IEvolutionaryAlgorithm _owner;
+
+ ///
+ /// The components.
+ ///
+ public OperationList Components
+ {
+ get { return _components; }
+ }
+
+ ///
+ /// The owner.
+ ///
+ public IEvolutionaryAlgorithm Owner
+ {
+ get { return _owner; }
+ }
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ _owner = theOwner;
+ foreach (var obj in _components.Contents)
+ {
+ obj.obj.Init(theOwner);
+ }
+ }
+
+ ///
+ public int OffspringProduced
+ {
+ get { return _components.MaxOffspring(); }
+ }
+
+ ///
+ public int ParentsNeeded
+ {
+ get { return _components.MaxOffspring(); }
+ }
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents,
+ int parentIndex, IGenome[] offspring,
+ int offspringIndex)
+ {
+ IEvolutionaryOperator opp = _components.Pick(rnd);
+ opp.PerformOperation(rnd, parents, parentIndex, offspring,
+ offspringIndex);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/IEvolutionaryOperator.cs b/encog-core-cs/ML/EA/Opp/IEvolutionaryOperator.cs
new file mode 100644
index 00000000..25de28a3
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/IEvolutionaryOperator.cs
@@ -0,0 +1,68 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Train;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.EA.Opp
+{
+ ///
+ /// An evolutionary operator is used to create new offspring genomes based on
+ /// parent genomes. There are a variety of means by which this can be done. The
+ /// number of parents required, as well as the number of offspring produced are
+ /// dependent on the operator. This interface defines key characteristics that
+ /// all operators must share.
+ /// Most operators do not modify the parents. However, some mutation operators do
+ /// require that the children and parent array be the same. If the children and
+ /// parent arrays are the same, then the parent will be mutated.
+ ///
+ public interface IEvolutionaryOperator
+ {
+ ///
+ /// The number of offspring produced by this type of crossover.
+ ///
+ int OffspringProduced { get; }
+
+ ///
+ /// The number of parents needed.
+ ///
+ int ParentsNeeded { get; }
+
+ ///
+ /// Called to setup the evolutionary operator.
+ ///
+ /// The evolutionary algorithm used with this operator.
+ void Init(IEvolutionaryAlgorithm theOwner);
+
+ ///
+ /// Perform the evolutionary operation.
+ ///
+ /// A random number generator.
+ /// The parents.
+ /// The index into the parents array.
+ /// The offspring.
+ /// An index into the offspring array.
+ void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
+ IGenome[] offspring, int offspringIndex);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/OperationList.cs b/encog-core-cs/ML/EA/Opp/OperationList.cs
new file mode 100644
index 00000000..54f00b1e
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/OperationList.cs
@@ -0,0 +1,87 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Linq;
+using Encog.Util.Obj;
+
+namespace Encog.ML.EA.Opp
+{
+ ///
+ /// This class holds a list of evolutionary operators. Each operator is given a
+ /// probability weight. Based on the number of parents available a random
+ /// selection of an operator can be made based on the probability given each of
+ /// the operators.
+ ///
+ public class OperationList : ChooseObject
+ {
+ ///
+ /// Determine the maximum number of offspring that might be produced by any
+ /// of the operators in this list.
+ ///
+ /// The maximum number of offspring.
+ public int MaxOffspring()
+ {
+ return Contents.Select(holder => holder.obj.OffspringProduced).Concat(new[] {0}).Max();
+ }
+
+ ///
+ /// Determine the maximum number of parents required by any of the operators
+ /// in the list.
+ ///
+ /// The maximum number of parents.
+ public int MaxParents()
+ {
+ return Contents.Select(holder => holder.obj.ParentsNeeded).Concat(new[] {int.MinValue}).Max();
+ }
+
+ ///
+ /// Pick a operator based on the number of parents available.
+ ///
+ /// A random number generator.
+ /// The maximum number of parents available.
+ /// The operator that was selected.
+ public IEvolutionaryOperator PickMaxParents(Random rnd,
+ int maxParents)
+ {
+ // determine the total probability of eligible operators
+ double total = Contents.Where(holder => holder.obj.ParentsNeeded <= maxParents).Sum(holder => holder.probability);
+
+ // choose an operator
+ double r = rnd.NextDouble()*total;
+ double current = 0;
+ foreach (var holder in Contents)
+ {
+ if (holder.obj.ParentsNeeded <= maxParents)
+ {
+ current += holder.probability;
+ if (r < current)
+ {
+ return holder.obj;
+ }
+ }
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/Selection/ISelectionOperator.cs b/encog-core-cs/ML/EA/Opp/Selection/ISelectionOperator.cs
new file mode 100644
index 00000000..fa4d0331
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/Selection/ISelectionOperator.cs
@@ -0,0 +1,56 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Species;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Opp.Selection
+{
+ ///
+ /// Provides the interface to a selection operator. This allows genomes to be
+ /// selected for offspring production or elimination.
+ ///
+ public interface ISelectionOperator
+ {
+ ///
+ /// The trainer being used.
+ ///
+ IEvolutionaryAlgorithm Trainer { get; }
+
+ ///
+ /// Selects an fit genome.
+ ///
+ /// A random number generator.
+ /// The species to select the genome from.
+ /// The selected genome.
+ int PerformSelection(Random rnd, ISpecies species);
+
+ ///
+ /// Selects an unfit genome.
+ ///
+ /// A random number generator.
+ /// The species to select the genome from.
+ /// The selected genome.
+ int PerformAntiSelection(Random rnd, ISpecies species);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/Selection/TournamentSelection.cs b/encog-core-cs/ML/EA/Opp/Selection/TournamentSelection.cs
new file mode 100644
index 00000000..103778e6
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/Selection/TournamentSelection.cs
@@ -0,0 +1,123 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Species;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Opp.Selection
+{
+ ///
+ /// Tournament select can be used to select a fit (or unfit) genome from a
+ /// species. The selection is run a set number of rounds. Each round two random
+ /// participants are chosen. The more fit participant continues to the next
+ /// round.
+ /// http://en.wikipedia.org/wiki/Tournament_selection
+ ///
+ [Serializable]
+ public class TournamentSelection : ISelectionOperator
+ {
+ ///
+ /// Construct a tournament selection.
+ ///
+ /// The trainer.
+ /// The number of rounds to use.
+ public TournamentSelection(IEvolutionaryAlgorithm theTrainer,
+ int theRounds)
+ {
+ Trainer = theTrainer;
+ Rounds = theRounds;
+ }
+
+ ///
+ /// The number of rounds.
+ ///
+ public int Rounds { get; set; }
+
+ ///
+ /// The trainer being used.
+ ///
+ public IEvolutionaryAlgorithm Trainer { get; set; }
+
+ ///
+ public int PerformAntiSelection(Random rnd, ISpecies species)
+ {
+ int worstIndex = rnd.Next(species.Members.Count);
+ IGenome worst = species.Members[worstIndex];
+ BasicEA.CalculateScoreAdjustment(worst,
+ Trainer.ScoreAdjusters);
+
+ for (int i = 0; i < Rounds; i++)
+ {
+ int competitorIndex = rnd.Next(species.Members.Count - 1);
+ IGenome competitor = species.Members[competitorIndex];
+
+ // force an invalid genome to lose
+ if (Double.IsInfinity(competitor.AdjustedScore)
+ || Double.IsNaN(competitor.AdjustedScore))
+ {
+ return competitorIndex;
+ }
+
+ BasicEA.CalculateScoreAdjustment(competitor,
+ Trainer.ScoreAdjusters);
+ if (!Trainer.SelectionComparer.IsBetterThan(competitor,
+ worst))
+ {
+ worst = competitor;
+ worstIndex = competitorIndex;
+ }
+ }
+ return worstIndex;
+ }
+
+ ///
+ public int PerformSelection(Random rnd, ISpecies species)
+ {
+ int bestIndex = rnd.Next(species.Members.Count);
+ IGenome best = species.Members[bestIndex];
+ BasicEA.CalculateScoreAdjustment(best, Trainer.ScoreAdjusters);
+
+ for (int i = 0; i < Rounds; i++)
+ {
+ int competitorIndex = rnd.Next(species.Members.Count - 1);
+ IGenome competitor = species.Members[competitorIndex];
+
+ // only evaluate valid genomes
+ if (!Double.IsInfinity(competitor.AdjustedScore)
+ && !Double.IsNaN(competitor.AdjustedScore))
+ {
+ BasicEA.CalculateScoreAdjustment(competitor,
+ Trainer.ScoreAdjusters);
+ if (Trainer.SelectionComparer.IsBetterThan(
+ competitor, best))
+ {
+ best = competitor;
+ bestIndex = competitorIndex;
+ }
+ }
+ }
+ return bestIndex;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Opp/Selection/TruncationSelection.cs b/encog-core-cs/ML/EA/Opp/Selection/TruncationSelection.cs
new file mode 100644
index 00000000..42950eb0
--- /dev/null
+++ b/encog-core-cs/ML/EA/Opp/Selection/TruncationSelection.cs
@@ -0,0 +1,81 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Species;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Opp.Selection
+{
+ ///
+ /// Perform a selection by truncation.
+ ///
+ public class TruncationSelection : ISelectionOperator
+ {
+ ///
+ /// The percent to select from.
+ ///
+ private readonly double _percent;
+
+ ///
+ /// The trainer.
+ ///
+ private IEvolutionaryAlgorithm _trainer;
+
+ ///
+ /// Construct the truncation selector.
+ ///
+ /// The trainer.
+ /// The top percent to select from.
+ public TruncationSelection(IEvolutionaryAlgorithm theTrainer,
+ double thePercent)
+ {
+ _trainer = theTrainer;
+ _percent = thePercent;
+ }
+
+ ///
+ /// The number of rounds.
+ ///
+ public int Rounds { get; set; }
+
+ ///
+ /// The trainer being used.
+ ///
+ public IEvolutionaryAlgorithm Trainer { get; set; }
+
+ ///
+ public int PerformSelection(Random rnd, ISpecies species)
+ {
+ int top = Math.Max((int) (species.Members.Count*_percent),
+ 1);
+ int result = rnd.Next(top);
+ return result;
+ }
+
+ ///
+ public int PerformAntiSelection(Random rnd, ISpecies species)
+ {
+ return species.Members.Count - PerformSelection(rnd, species);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Population/BasicPopulation.cs b/encog-core-cs/ML/EA/Population/BasicPopulation.cs
new file mode 100644
index 00000000..266cfde4
--- /dev/null
+++ b/encog-core-cs/ML/EA/Population/BasicPopulation.cs
@@ -0,0 +1,188 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Species;
+
+namespace Encog.ML.EA.Population
+{
+ ///
+ /// Defines the basic functionality for a population of genomes. The population
+ /// is made up of species. These species contain the individiual genomes that
+ /// make up the population. If you do not want to use species, then create one
+ /// species that holds every genome.
+ ///
+ [Serializable]
+ public class BasicPopulation : BasicML, IPopulation
+ {
+ ///
+ /// The species that make up the population.
+ ///
+ private readonly List _species = new List();
+
+ ///
+ /// The object name.
+ ///
+ private String _name;
+
+ ///
+ /// Construct an empty population.
+ ///
+ public BasicPopulation()
+ {
+ PopulationSize = 0;
+ }
+
+ ///
+ /// Construct a population.
+ ///
+ /// The population size.
+ /// The genome factory.
+ public BasicPopulation(int thePopulationSize,
+ IGenomeFactory theGenomeFactory)
+ {
+ PopulationSize = thePopulationSize;
+ GenomeFactory = theGenomeFactory;
+ }
+
+ ///
+ /// The name of this object.
+ ///
+ public string Name { get; set; }
+
+ ///
+ /// The best genome.
+ ///
+ public IGenome BestGenome { get; set; }
+
+ ///
+ /// A factory that can be used to store create genomes.
+ ///
+ public IGenomeFactory GenomeFactory { get; set; }
+
+ ///
+ /// How many genomes should be created.
+ ///
+ public int PopulationSize { get; set; }
+
+ ///
+ public void Clear()
+ {
+ _species.Clear();
+ }
+
+ ///
+ public ISpecies CreateSpecies()
+ {
+ ISpecies species = new BasicSpecies();
+ species.Population = this;
+ _species.Add(species);
+ return species;
+ }
+
+ ///
+ public ISpecies DetermineBestSpecies()
+ {
+ return _species.FirstOrDefault(species => species.Members.Contains(BestGenome));
+ }
+
+ ///
+ public IList Flatten()
+ {
+ IList result = new List();
+ return _species.Aggregate(result, (current, species) => current.Union(species.Members).ToList());
+ }
+
+
+ ///
+ public int MaxIndividualSize
+ {
+ get { return int.MaxValue; }
+ }
+
+ ///
+ public int Count
+ {
+ get { return Flatten().Count; }
+ }
+
+ ///
+ public List Species
+ {
+ get { return _species; }
+ }
+
+ ///
+ public void PurgeInvalidGenomes()
+ {
+ // remove any invalid genomes
+ int speciesNum = 0;
+ while (speciesNum < Species.Count)
+ {
+ ISpecies species = Species[speciesNum];
+
+ int genomeNum = 0;
+ while (genomeNum < species.Members.Count)
+ {
+ IGenome genome = species.Members[genomeNum];
+ if (double.IsInfinity(genome.Score)
+ || double.IsInfinity(genome.AdjustedScore)
+ || double.IsNaN(genome.Score)
+ || double.IsNaN(genome.AdjustedScore))
+ {
+ species.Members.Remove(genome);
+ }
+ else
+ {
+ genomeNum++;
+ }
+ }
+
+ // is the species now empty?
+ if (species.Members.Count == 0)
+ {
+ Species.Remove(species);
+ }
+ else
+ {
+ // new leader needed?
+ if (!species.Members.Contains(species.Leader))
+ {
+ species.Leader = species.Members[0];
+ species.BestScore = species.Leader.AdjustedScore;
+ }
+
+ // onto the next one!
+ speciesNum++;
+ }
+ }
+ }
+
+ ///
+ public override void UpdateProperties()
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Population/IPopulation.cs b/encog-core-cs/ML/EA/Population/IPopulation.cs
new file mode 100644
index 00000000..5fb4a253
--- /dev/null
+++ b/encog-core-cs/ML/EA/Population/IPopulation.cs
@@ -0,0 +1,92 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Species;
+
+namespace Encog.ML.EA.Population
+{
+ ///
+ /// Defines a population of genomes.
+ ///
+ public interface IPopulation : IMLMethod
+ {
+ ///
+ /// The best genome in the population.
+ ///
+ IGenome BestGenome { get; set; }
+
+ ///
+ /// A factory used to create genomes.
+ ///
+ IGenomeFactory GenomeFactory { get; set; }
+
+ ///
+ /// The max size that an individual can become.
+ ///
+ int MaxIndividualSize { get; }
+
+ ///
+ /// The max population size.
+ ///
+ int PopulationSize { get; set; }
+
+ ///
+ /// The size of the population.
+ ///
+ int Count { get; }
+
+ ///
+ /// The list of species.
+ ///
+ List Species { get; }
+
+ ///
+ /// Clear all genomes from this population.
+ ///
+ void Clear();
+
+ ///
+ /// Create a species.
+ ///
+ /// The newly created species.
+ ISpecies CreateSpecies();
+
+ ///
+ /// Determine which species has the top genome.
+ ///
+ /// The species with the top genome.
+ ISpecies DetermineBestSpecies();
+
+ ///
+ /// Flatten the species into a single list of genomes.
+ ///
+ /// The genomes that make up all species in the population.
+ IList Flatten();
+
+ ///
+ /// Purge invalid genomes.
+ ///
+ void PurgeInvalidGenomes();
+ }
+}
diff --git a/encog-core-cs/ML/EA/Population/IPopulationGenerator.cs b/encog-core-cs/ML/EA/Population/IPopulationGenerator.cs
new file mode 100644
index 00000000..a61efb19
--- /dev/null
+++ b/encog-core-cs/ML/EA/Population/IPopulationGenerator.cs
@@ -0,0 +1,47 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.EA.Population
+{
+ ///
+ /// Generate a random population.
+ ///
+ public interface IPopulationGenerator
+ {
+ ///
+ /// Generate a random genome.
+ ///
+ /// A random number generator.
+ /// A random genome.
+ IGenome Generate(EncogRandom rnd);
+
+ ///
+ /// Generate a random population.
+ ///
+ /// Random number generator.
+ /// The population to generate into.
+ void Generate(EncogRandom rnd, IPopulation pop);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Rules/BasicRuleHolder.cs b/encog-core-cs/ML/EA/Rules/BasicRuleHolder.cs
new file mode 100644
index 00000000..d195e5f6
--- /dev/null
+++ b/encog-core-cs/ML/EA/Rules/BasicRuleHolder.cs
@@ -0,0 +1,93 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Rules
+{
+ ///
+ /// Basic implementation of a rule holder.
+ ///
+ public class BasicRuleHolder : IRuleHolder
+ {
+ ///
+ /// Rewrite rules that can simplify genomes.
+ ///
+ private readonly IList _constraintRules = new List();
+
+ ///
+ /// Rewrite rules that can simplify genomes.
+ ///
+ private readonly IList _rewriteRules = new List();
+
+ ///
+ public void AddRewriteRule(IRewriteRule rule)
+ {
+ _rewriteRules.Add(rule);
+ }
+
+ ///
+ public void Rewrite(IGenome prg)
+ {
+ bool done = false;
+
+ while (!done)
+ {
+ done = true;
+
+ foreach (IRewriteRule rule in _rewriteRules)
+ {
+ if (rule.Rewrite(prg))
+ {
+ done = false;
+ }
+ }
+ }
+ }
+
+ ///
+ public void AddConstraintRule(IConstraintRule rule)
+ {
+ _constraintRules.Add(rule);
+ }
+
+ ///
+ public bool IsValid(IGenome genome)
+ {
+ return _constraintRules.All(rule => rule.IsValid(genome));
+ }
+
+ ///
+ public IList ConstraintRules
+ {
+ get { return _constraintRules; }
+ }
+
+ ///
+ public IList RewriteRules
+ {
+ get { return _rewriteRules; }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Innovation/IInnovation.cs b/encog-core-cs/ML/EA/Rules/IConstraintRule.cs
similarity index 65%
rename from encog-core-cs/ML/Genetic/Innovation/IInnovation.cs
rename to encog-core-cs/ML/EA/Rules/IConstraintRule.cs
index 2942f522..71b70e17 100644
--- a/encog-core-cs/ML/Genetic/Innovation/IInnovation.cs
+++ b/encog-core-cs/ML/EA/Rules/IConstraintRule.cs
@@ -1,39 +1,39 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-namespace Encog.ML.Genetic.Innovation
-{
- ///
- /// Provides the interface for an innovation. An innovation is a enhancement that
- /// was tried to the genome.
- ///
- ///
- public interface IInnovation
- {
- ///
- /// Set the innovation id.
- ///
- long InnovationID {
- get;
- set; }
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Rules
+{
+ ///
+ /// Defines a constraint. A constraint specifies if a genome is invalid.
+ ///
+ public interface IConstraintRule
+ {
+ ///
+ /// Is this genome valid?
+ ///
+ /// The genome.
+ /// True if this genome is valid.
+ bool IsValid(IGenome genome);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Rules/IRewriteRule.cs b/encog-core-cs/ML/EA/Rules/IRewriteRule.cs
new file mode 100644
index 00000000..a05c4eff
--- /dev/null
+++ b/encog-core-cs/ML/EA/Rules/IRewriteRule.cs
@@ -0,0 +1,40 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Rules
+{
+ ///
+ /// Defines a rewrite rule. Rewrite rules are used to rewrite a genome into a
+ /// more simple, yet equivalent, form.
+ ///
+ public interface IRewriteRule
+ {
+ ///
+ /// Rewrite the specified genome.
+ ///
+ /// The genome to rewrite.
+ /// True, if the genome was rewritten.
+ bool Rewrite(IGenome genome);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Rules/IRuleHolder.cs b/encog-core-cs/ML/EA/Rules/IRuleHolder.cs
new file mode 100644
index 00000000..007c4b6d
--- /dev/null
+++ b/encog-core-cs/ML/EA/Rules/IRuleHolder.cs
@@ -0,0 +1,69 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Rules
+{
+ ///
+ /// Holds a set of rules for an EA.
+ ///
+ public interface IRuleHolder
+ {
+ ///
+ /// The list of constraints.
+ ///
+ IList ConstraintRules { get; }
+
+ ///
+ /// The rewrite rules.
+ ///
+ IList RewriteRules { get; }
+
+ ///
+ /// Add a rewrite rule. Rewrite rules can be used to simplify genomes.
+ ///
+ /// The rule to add.
+ void AddRewriteRule(IRewriteRule rule);
+
+ ///
+ /// Add a constraint rule.
+ ///
+ /// The rule to add.
+ void AddConstraintRule(IConstraintRule rule);
+
+ ///
+ /// Rewrite the specified genome. The genome will still perform the same
+ /// function, but it may be shorter.
+ ///
+ /// The genome to rewrite.
+ void Rewrite(IGenome genome);
+
+ ///
+ /// Determine if the specified genome is valid according to the constraint rules.
+ ///
+ /// The gnome to check.
+ /// True, if the genome is valid.
+ bool IsValid(IGenome genome);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Score/Adjust/ComplexityAdjustedScore.cs b/encog-core-cs/ML/EA/Score/Adjust/ComplexityAdjustedScore.cs
new file mode 100644
index 00000000..ef4cabaf
--- /dev/null
+++ b/encog-core-cs/ML/EA/Score/Adjust/ComplexityAdjustedScore.cs
@@ -0,0 +1,103 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Score.Adjust
+{
+ ///
+ /// Adjust scores to penalize complexity.
+ ///
+ public class ComplexityAdjustedScore : IAdjustScore
+ {
+ ///
+ /// Construct a adjustor to penalize complexity.
+ ///
+ ///
+ /// The complexity level at which a penalty
+ /// begins to be applied.
+ ///
+ ///
+ /// The complexity level at which
+ /// a full (100%) penalty is applied.
+ ///
+ /// The starting complexity penalty.
+ /// The full complexity penalty.
+ public ComplexityAdjustedScore(int theComplexityPenaltyThreshold,
+ int theComplexityPentaltyFullThreshold,
+ double theComplexityPenalty, double theComplexityFullPenalty)
+ {
+ ComplexityPenaltyThreshold = theComplexityPenaltyThreshold;
+ ComplexityPentaltyFullThreshold = theComplexityPentaltyFullThreshold;
+ ComplexityPenalty = theComplexityPenalty;
+ ComplexityFullPenalty = theComplexityFullPenalty;
+ }
+
+ ///
+ /// Construct with defaults. 10 for ComplexityPenaltyThreshold, 50 for ComplexityPentaltyFullThreshold,
+ /// 0.2 for ComplexityPenalty, 2.0 for ComplexityFullPenalty.
+ ///
+ public ComplexityAdjustedScore()
+ : this(10, 50, 0.2, 2.0)
+ {
+ }
+
+ ///
+ /// The starting complexity penalty.
+ ///
+ public double ComplexityPenalty { get; set; }
+
+ ///
+ /// The full complexity penalty.
+ ///
+ public double ComplexityFullPenalty { get; set; }
+
+ ///
+ /// The complexity level at which a penalty begins to be applied.
+ ///
+ public int ComplexityPenaltyThreshold { get; set; }
+
+ ///
+ /// The complexity level at which a full (100%) penalty is applied.
+ ///
+ public int ComplexityPentaltyFullThreshold { get; set; }
+
+ ///
+ public double CalculateAdjustment(IGenome genome)
+ {
+ double score = genome.Score;
+ double result = 0;
+
+ if (genome.Size > ComplexityPenaltyThreshold)
+ {
+ int over = genome.Size - ComplexityPenaltyThreshold;
+ int range = ComplexityPentaltyFullThreshold
+ - ComplexityPenaltyThreshold;
+ double complexityPenalty = ((ComplexityFullPenalty - ComplexityPenalty)/range)
+ *over;
+ result = (score*complexityPenalty);
+ }
+
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genome/ICalculateGenomeScore.cs b/encog-core-cs/ML/EA/Score/IAdjustScore.cs
similarity index 62%
rename from encog-core-cs/ML/Genetic/Genome/ICalculateGenomeScore.cs
rename to encog-core-cs/ML/EA/Score/IAdjustScore.cs
index c778c29a..9d98dca3 100644
--- a/encog-core-cs/ML/Genetic/Genome/ICalculateGenomeScore.cs
+++ b/encog-core-cs/ML/EA/Score/IAdjustScore.cs
@@ -1,42 +1,40 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-namespace Encog.ML.Genetic.Genome
-{
- ///
- /// Genetic Algorithms need a class to calculate the score.
- ///
- ///
- public interface ICalculateGenomeScore
- {
- /// True if the goal is to minimize the score.
- bool ShouldMinimize { get; }
-
- ///
- /// Calculate this genome's score.
- ///
- ///
- /// The genome.
- /// The score.
- double CalculateScore(IGenome genome);
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Score
+{
+ ///
+ /// Score adjusters adjust the score according to some means. The resulting score
+ /// is stored in the genome's adjusted score.
+ ///
+ public interface IAdjustScore
+ {
+ ///
+ /// Calculate the score adjustment.
+ ///
+ /// The genome.
+ /// The adjusted score.
+ double CalculateAdjustment(IGenome genome);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Score/Multi/ParallelScore.cs b/encog-core-cs/ML/EA/Score/Multi/ParallelScore.cs
new file mode 100644
index 00000000..3cf95938
--- /dev/null
+++ b/encog-core-cs/ML/EA/Score/Multi/ParallelScore.cs
@@ -0,0 +1,138 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Encog.ML.EA.Codec;
+using Encog.ML.EA.Population;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.EA.Score.Multi
+{
+ ///
+ /// This class is used to calculate the scores for an entire population. This is
+ /// typically done when a new population must be scored for the first time.
+ ///
+ public class ParallelScore
+ {
+ ///
+ /// The score adjuster.
+ ///
+ private readonly IList _adjusters;
+
+ ///
+ /// The CODEC used to create genomes.
+ ///
+ private readonly IGeneticCODEC _codec;
+
+ ///
+ /// The population to score.
+ ///
+ private readonly IPopulation _population;
+
+ ///
+ /// The scoring function.
+ ///
+ private readonly ICalculateScore _scoreFunction;
+
+ ///
+ /// Construct the parallel score calculation object.
+ ///
+ /// The population to score.
+ /// The CODEC to use.
+ /// The score adjusters to use.
+ /// The score function.
+ /// The requested thread count.
+ public ParallelScore(IPopulation thePopulation, IGeneticCODEC theCODEC,
+ IList theAdjusters, ICalculateScore theScoreFunction,
+ int theThreadCount)
+ {
+ _codec = theCODEC;
+ _population = thePopulation;
+ _scoreFunction = theScoreFunction;
+ _adjusters = theAdjusters;
+ ThreadCount = theThreadCount;
+ }
+
+ ///
+ /// The thread count.
+ ///
+ public int ThreadCount { get; set; }
+
+ ///
+ /// The population.
+ ///
+ public IPopulation Population
+ {
+ get { return _population; }
+ }
+
+ ///
+ /// The score function.
+ ///
+ public ICalculateScore ScoreFunction
+ {
+ get { return _scoreFunction; }
+ }
+
+ ///
+ /// The CODEC.
+ ///
+ public IGeneticCODEC CODEC
+ {
+ get { return _codec; }
+ }
+
+ ///
+ /// The score adjusters.
+ ///
+ public IList Adjusters
+ {
+ get { return _adjusters; }
+ }
+
+ ///
+ /// Calculate the scores.
+ ///
+ public void Process()
+ {
+ // calculate workload
+ IList tasks = (from species in _population.Species from genome in species.Members select new ParallelScoreTask(genome, this)).ToList();
+
+ // determine thread usage
+ if (ScoreFunction.RequireSingleThreaded || ThreadCount == 1)
+ {
+ // single
+ foreach (ParallelScoreTask task in tasks)
+ {
+ task.PerformTask();
+ }
+ }
+ else
+ {
+ // parallel
+ Parallel.ForEach(tasks, currentTask => currentTask.PerformTask());
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Score/Multi/ParallelScoreTask.cs b/encog-core-cs/ML/EA/Score/Multi/ParallelScoreTask.cs
new file mode 100644
index 00000000..4cbe7799
--- /dev/null
+++ b/encog-core-cs/ML/EA/Score/Multi/ParallelScoreTask.cs
@@ -0,0 +1,90 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Train;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.EA.Score.Multi
+{
+ public class ParallelScoreTask
+ {
+ ///
+ /// The score adjusters.
+ ///
+ private readonly IList adjusters;
+
+ ///
+ /// The genome to calculate the score for.
+ ///
+ private readonly IGenome genome;
+
+ ///
+ /// The owners.
+ ///
+ private readonly ParallelScore owner;
+
+ ///
+ /// The score function.
+ ///
+ private readonly ICalculateScore scoreFunction;
+
+ ///
+ /// Construct the parallel task.
+ ///
+ /// The genome.
+ /// The owner.
+ public ParallelScoreTask(IGenome genome, ParallelScore theOwner)
+ {
+ owner = theOwner;
+ this.genome = genome;
+ scoreFunction = theOwner.ScoreFunction;
+ adjusters = theOwner.Adjusters;
+ }
+
+ ///
+ /// Perform the task.
+ ///
+ public void PerformTask()
+ {
+ IMLMethod phenotype = owner.CODEC.Decode(genome);
+ if (phenotype != null)
+ {
+ double score;
+ try
+ {
+ score = scoreFunction.CalculateScore(phenotype);
+ }
+ catch (EARuntimeError e)
+ {
+ score = Double.NaN;
+ }
+ genome.Score = score;
+ genome.AdjustedScore = score;
+ BasicEA.CalculateScoreAdjustment(genome, adjusters);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/AbstractGenomeComparer.cs b/encog-core-cs/ML/EA/Sort/AbstractGenomeComparer.cs
new file mode 100644
index 00000000..81f6a982
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/AbstractGenomeComparer.cs
@@ -0,0 +1,82 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Provides base functionality for comparing genomes. Specifically the ability
+ /// to add bonuses and penalties.
+ ///
+ [Serializable]
+ public abstract class AbstractGenomeComparer : IGenomeComparer
+ {
+ ///
+ public double ApplyBonus(double value, double bonus)
+ {
+ double amount = value*bonus;
+ if (ShouldMinimize)
+ {
+ return value - amount;
+ }
+ return value + amount;
+ }
+
+ ///
+ public double ApplyPenalty(double value, double bonus)
+ {
+ double amount = value*bonus;
+ if (!ShouldMinimize)
+ {
+ return value - amount;
+ }
+ return value + amount;
+ }
+
+ ///
+ public bool IsBetterThan(double d1, double d2)
+ {
+ if (ShouldMinimize)
+ {
+ return d1 < d2;
+ }
+ return d1 > d2;
+ }
+
+
+ public abstract bool IsBetterThan(IGenome genome1, IGenome genome2);
+
+ public abstract bool ShouldMinimize { get; }
+
+ ///
+ /// Compare two genomes.
+ ///
+ /// The first genome.
+ /// The second genome.
+ ///
+ /// 0 if equal, <0 if x is less,>0 if y is less.
+ ///
+ public abstract int Compare(IGenome x, IGenome y);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/IGenomeComparer.cs b/encog-core-cs/ML/EA/Sort/IGenomeComparer.cs
new file mode 100644
index 00000000..d32b92c9
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/IGenomeComparer.cs
@@ -0,0 +1,74 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Defines methods for comparing genomes. Also provides methods to apply bonuses
+ /// and penalties.
+ ///
+ public interface IGenomeComparer : IComparer
+ {
+ ///
+ /// Returns true if the score should be minimized.
+ ///
+ bool ShouldMinimize { get; }
+
+ ///
+ /// Apply a bonus, this is a simple percent that is applied in the direction
+ /// specified by the "should minimize" property of the score function.
+ ///
+ /// The current value.
+ /// The bonus.
+ /// The resulting value.
+ double ApplyBonus(double value, double bonus);
+
+ ///
+ /// Apply a penalty, this is a simple percent that is applied in the
+ /// direction specified by the "should minimize" property of the score
+ /// function.
+ ///
+ /// The current value.
+ /// The penalty.
+ /// The resulting value.
+ double ApplyPenalty(double value, double bonus);
+
+ ///
+ /// Determine if one score is better than the other.
+ ///
+ /// The first score to compare.
+ /// The second score to compare.
+ /// True if d1 is better than d2.
+ bool IsBetterThan(double d1, double d2);
+
+ ///
+ /// Determine if one genome is better than the other genome.
+ ///
+ /// The first genome.
+ /// The second genome.
+ /// True, if genome1 is better than genome2.
+ bool IsBetterThan(IGenome genome1, IGenome genome2);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/MaximizeAdjustedScoreComp.cs b/encog-core-cs/ML/EA/Sort/MaximizeAdjustedScoreComp.cs
new file mode 100644
index 00000000..eb84b193
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/MaximizeAdjustedScoreComp.cs
@@ -0,0 +1,53 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Use this comparator to maximize the adjusted score.
+ ///
+ [Serializable]
+ public class MaximizeAdjustedScoreComp : AbstractGenomeComparer
+ {
+ ///
+ public override bool ShouldMinimize
+ {
+ get { return false; }
+ }
+
+ ///
+ public override int Compare(IGenome p1, IGenome p2)
+ {
+ return p2.AdjustedScore.CompareTo(p1.AdjustedScore);
+ }
+
+ ///
+ public override bool IsBetterThan(IGenome prg, IGenome betterThan)
+ {
+ return IsBetterThan(prg.AdjustedScore,
+ betterThan.AdjustedScore);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/MaximizeScoreComp.cs b/encog-core-cs/ML/EA/Sort/MaximizeScoreComp.cs
new file mode 100644
index 00000000..db00a7b5
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/MaximizeScoreComp.cs
@@ -0,0 +1,53 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Use this comparator to maximize the score.
+ ///
+ [Serializable]
+ public class MaximizeScoreComp : AbstractGenomeComparer
+ {
+ ///
+ public override bool ShouldMinimize
+ {
+ get { return false; }
+ }
+
+ ///
+ public override int Compare(IGenome p1, IGenome p2)
+ {
+ return p2.Score.CompareTo(p1.Score);
+ }
+
+ ///
+ public override bool IsBetterThan(IGenome prg, IGenome betterThan)
+ {
+ return IsBetterThan(prg.AdjustedScore,
+ betterThan.AdjustedScore);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/MinimizeAdjustedScoreComp.cs b/encog-core-cs/ML/EA/Sort/MinimizeAdjustedScoreComp.cs
new file mode 100644
index 00000000..390cf4e5
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/MinimizeAdjustedScoreComp.cs
@@ -0,0 +1,53 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Use this comparator to minimize the adjusted score.
+ ///
+ [Serializable]
+ public class MinimizeAdjustedScoreComp : AbstractGenomeComparer
+ {
+ ///
+ public override bool ShouldMinimize
+ {
+ get { return true; }
+ }
+
+ ///
+ public override int Compare(IGenome p1, IGenome p2)
+ {
+ return p1.AdjustedScore.CompareTo(p2.AdjustedScore);
+ }
+
+ ///
+ public override bool IsBetterThan(IGenome prg, IGenome betterThan)
+ {
+ return IsBetterThan(prg.AdjustedScore,
+ betterThan.AdjustedScore);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/MinimizeScoreComp.cs b/encog-core-cs/ML/EA/Sort/MinimizeScoreComp.cs
new file mode 100644
index 00000000..04823d1e
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/MinimizeScoreComp.cs
@@ -0,0 +1,53 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Use this comparator to minimize the score.
+ ///
+ [Serializable]
+ public class MinimizeScoreComp : AbstractGenomeComparer
+ {
+ ///
+ public override bool ShouldMinimize
+ {
+ get { return true; }
+ }
+
+ ///
+ public override int Compare(IGenome p1, IGenome p2)
+ {
+ return p1.Score.CompareTo(p2.Score);
+ }
+
+ ///
+ public override bool IsBetterThan(IGenome prg, IGenome betterThan)
+ {
+ return IsBetterThan(prg.AdjustedScore,
+ betterThan.AdjustedScore);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/SortGenomesForSpecies.cs b/encog-core-cs/ML/EA/Sort/SortGenomesForSpecies.cs
new file mode 100644
index 00000000..a9d027a1
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/SortGenomesForSpecies.cs
@@ -0,0 +1,62 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// Sort the gnomes for species. Sort first by score, second by birth generation.
+ /// This favors younger genomes if scores are equal.
+ ///
+ public class SortGenomesForSpecies : IComparer
+ {
+ ///
+ /// The trainer.
+ ///
+ private readonly IEvolutionaryAlgorithm _train;
+
+ ///
+ /// Construct the comparator.
+ ///
+ /// The trainer.
+ public SortGenomesForSpecies(IEvolutionaryAlgorithm theTrain)
+ {
+ _train = theTrain;
+ }
+
+ ///
+ public int Compare(IGenome g1, IGenome g2)
+ {
+ int result = _train.SelectionComparer.Compare(g1, g2);
+
+ if (result != 0)
+ {
+ return result;
+ }
+
+ return g2.BirthGeneration - g1.BirthGeneration;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Sort/SpeciesComparer.cs b/encog-core-cs/ML/EA/Sort/SpeciesComparer.cs
new file mode 100644
index 00000000..20fbf157
--- /dev/null
+++ b/encog-core-cs/ML/EA/Sort/SpeciesComparer.cs
@@ -0,0 +1,56 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Species;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Sort
+{
+ ///
+ /// This comparator is used to compare two species. This is done by comparing the
+ /// scores of the two leaders.
+ ///
+ public class SpeciesComparer : Comparer
+ {
+ ///
+ /// The training method.
+ ///
+ private readonly IEvolutionaryAlgorithm _training;
+
+ ///
+ /// Create a species comparator.
+ ///
+ /// The trainer.
+ public SpeciesComparer(IEvolutionaryAlgorithm theTraining)
+ {
+ _training = theTraining;
+ }
+
+ ///
+ public override int Compare(ISpecies sp1, ISpecies sp2)
+ {
+ return _training.BestComparer.Compare(sp1.Leader,
+ sp2.Leader);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Species/BasicSpecies.cs b/encog-core-cs/ML/EA/Species/BasicSpecies.cs
new file mode 100644
index 00000000..ab90ce14
--- /dev/null
+++ b/encog-core-cs/ML/EA/Species/BasicSpecies.cs
@@ -0,0 +1,190 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+using Encog.Util;
+
+namespace Encog.ML.EA.Species
+{
+ ///
+ /// Provides basic functionality for a species.
+ ///
+ [Serializable]
+ public class BasicSpecies : ISpecies
+ {
+ ///
+ /// The list of genomes.
+ ///
+ private List _members = new List();
+
+ ///
+ /// Default constructor, used mainly for persistence.
+ ///
+ public BasicSpecies()
+ {
+ }
+
+ ///
+ /// Construct a species.
+ ///
+ /// The population the species belongs to.
+ /// The first genome in the species.
+ public BasicSpecies(IPopulation thePopulation, IGenome theFirst)
+ {
+ Population = thePopulation;
+ BestScore = theFirst.Score;
+ GensNoImprovement = 0;
+ Age = 0;
+ Leader = theFirst;
+ _members.Add(theFirst);
+ }
+
+ ///
+ /// The age of this species.
+ ///
+ public int Age { get; set; }
+
+ ///
+ /// The best score.
+ ///
+ public double BestScore { get; set; }
+
+ ///
+ /// The number of generations with no improvement.
+ ///
+ public int GensNoImprovement { get; set; }
+
+ ///
+ /// The leader.
+ ///
+ public IGenome Leader { get; set; }
+
+ ///
+ /// The owner class.
+ ///
+ public IPopulation Population { get; set; }
+
+ ///
+ /// The offspring count.
+ ///
+ public int OffspringCount { get; set; }
+
+ ///
+ /// The offpsring share (percent).
+ ///
+ public double OffspringShare { get; set; }
+
+ ///
+ public void Add(IGenome genome)
+ {
+ genome.Population = Population;
+ _members.Add(genome);
+ }
+
+ ///
+ public double CalculateShare(bool shouldMinimize,
+ double maxScore)
+ {
+ double total = 0;
+
+ int count = 0;
+ foreach (IGenome genome in _members)
+ {
+ if (!double.IsNaN(genome.AdjustedScore)
+ && !double.IsInfinity(genome.AdjustedScore))
+ {
+ double s;
+ if (shouldMinimize)
+ {
+ s = maxScore - genome.AdjustedScore;
+ }
+ else
+ {
+ s = genome.AdjustedScore;
+ }
+ total += s;
+ count++;
+ }
+ }
+
+ if (count == 0)
+ {
+ OffspringShare = 0;
+ }
+ else
+ {
+ OffspringShare = total/count;
+ }
+
+ return OffspringShare;
+ }
+
+ ///
+ public List Members
+ {
+ get { return _members; }
+ set { _members = value; }
+ }
+
+ ///
+ /// Purge all members, increase age by one and count the number of
+ /// generations with no improvement.
+ ///
+ public void Purge()
+ {
+ _members.Clear();
+ if (Leader != null)
+ {
+ _members.Add(Leader);
+ }
+ Age++;
+ GensNoImprovement++;
+ OffspringCount = 0;
+ OffspringShare = 0;
+ }
+
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[BasicSpecies: score=");
+ result.Append(Format.FormatDouble(BestScore, 2));
+ result.Append(", members=");
+ result.Append(_members.Count);
+ result.Append(", age=");
+ result.Append(Age);
+ result.Append(", no_improv=");
+ result.Append(GensNoImprovement);
+ result.Append(", share=");
+ result.Append(OffspringShare);
+ result.Append(", offspring count=");
+ result.Append(OffspringShare);
+ result.Append("]");
+ return result.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Innovation/IInnovationList.cs b/encog-core-cs/ML/EA/Species/ISpeciation.cs
similarity index 56%
rename from encog-core-cs/ML/Genetic/Innovation/IInnovationList.cs
rename to encog-core-cs/ML/EA/Species/ISpeciation.cs
index e1bacd32..b0334532 100644
--- a/encog-core-cs/ML/Genetic/Innovation/IInnovationList.cs
+++ b/encog-core-cs/ML/EA/Species/ISpeciation.cs
@@ -1,51 +1,46 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System.Collections.Generic;
-
-namespace Encog.ML.Genetic.Innovation
-{
- ///
- /// Defines a list of innovations.
- ///
- ///
- public interface IInnovationList
- {
- /// A list of innovations.
- IList Innovations { get; }
-
- ///
- /// Add an innovation.
- ///
- ///
- /// The innovation added.
- void Add(IInnovation innovation);
-
- ///
- /// Get the innovation specified by index.
- ///
- ///
- /// The index.
- /// The innovation.
- IInnovation Get(int id);
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Species
+{
+ ///
+ /// Defines a speciation strategy.
+ ///
+ public interface ISpeciation
+ {
+ ///
+ /// Setup the speciation strategy.
+ ///
+ /// The owner.
+ void Init(IEvolutionaryAlgorithm theOwner);
+
+ ///
+ /// Perform the speciation.
+ ///
+ /// The genomes to speciate.
+ void PerformSpeciation(IList genomeList);
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Species/Species.cs b/encog-core-cs/ML/EA/Species/ISpecies.cs
similarity index 50%
rename from encog-core-cs/ML/Genetic/Species/Species.cs
rename to encog-core-cs/ML/EA/Species/ISpecies.cs
index 9b7ce2b2..529f23ae 100644
--- a/encog-core-cs/ML/Genetic/Species/Species.cs
+++ b/encog-core-cs/ML/EA/Species/ISpecies.cs
@@ -1,96 +1,88 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System.Collections.Generic;
-using Encog.ML.Genetic.Genome;
-
-namespace Encog.ML.Genetic.Species
-{
- ///
- /// Defines the features used in a species. A species is a group of genomes.
- ///
- ///
- public interface ISpecies
- {
- ///
- /// Set the age of this species.
- ///
- int Age { get; set; }
-
-
- ///
- /// Set the best score.
- ///
- double BestScore { get; set; }
-
-
- ///
- /// Set the number of generations with no improvement.
- ///
- int GensNoImprovement { get; set; }
-
-
- ///
- /// Set the leader of this species.
- ///
- IGenome Leader { get; set; }
-
-
- /// The numbers of this species.
- IList Members { get; }
-
-
- /// The number of genomes this species will try to spawn into the
- /// next generation.
- double NumToSpawn { get; }
-
-
- ///
- /// Set the number of spawns required.
- ///
- double SpawnsRequired { get; set; }
-
-
- /// The species ID.
- long SpeciesID { get; }
-
- ///
- /// Calculate the amount that a species will spawn.
- ///
- ///
- void CalculateSpawnAmount();
-
- ///
- /// Choose a worthy parent for mating.
- ///
- ///
- /// The parent genome.
- IGenome ChooseParent();
-
-
- ///
- /// Purge old unsuccessful genomes.
- ///
- ///
- void Purge();
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+
+namespace Encog.ML.EA.Species
+{
+ ///
+ /// Defines a species.
+ ///
+ public interface ISpecies
+ {
+ ///
+ /// The age of this species.
+ ///
+ int Age { get; set; }
+
+ ///
+ /// The best score for this species.
+ ///
+ double BestScore { get; set; }
+
+ ///
+ /// The number of generations with no imrpvement.
+ ///
+ int GensNoImprovement { get; set; }
+
+ ///
+ /// The leader of this species.
+ ///
+ IGenome Leader { get; set; }
+
+ ///
+ /// The members of this species.
+ ///
+ List Members { get; set; }
+
+ ///
+ /// Get the offspring count.
+ ///
+ int OffspringCount { get; set; }
+
+ ///
+ /// The offspring share for the next iteration's population.
+ ///
+ double OffspringShare { get; }
+
+ ///
+ /// The population.
+ ///
+ IPopulation Population { get; set; }
+
+ ///
+ /// Add a genome to this species.
+ ///
+ /// The genome to add.
+ void Add(IGenome genome);
+
+ ///
+ /// Calculate this genome's share of the next population.
+ ///
+ /// True if we see to minimize the score.
+ /// The best score.
+ /// The share of this species, as a percent ratio.
+ double CalculateShare(bool shouldMinimize, double maxScore);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Species/SingleSpeciation.cs b/encog-core-cs/ML/EA/Species/SingleSpeciation.cs
new file mode 100644
index 00000000..b6cbcad7
--- /dev/null
+++ b/encog-core-cs/ML/EA/Species/SingleSpeciation.cs
@@ -0,0 +1,84 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Sort;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.EA.Species
+{
+ ///
+ /// This speciation strategy simply creates a single species that contains the
+ /// entire population. Use this speciation strategy if you do not wish to use
+ /// species.
+ ///
+ [Serializable]
+ public class SingleSpeciation : ISpeciation
+ {
+ ///
+ /// The trainer.
+ ///
+ private IEvolutionaryAlgorithm _owner;
+
+ ///
+ /// The method used to sort the genomes in the species. More desirable
+ /// genomes should come first for later selection.
+ ///
+ private SortGenomesForSpecies _sortGenomes;
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ _owner = theOwner;
+ _sortGenomes = new SortGenomesForSpecies(_owner);
+ }
+
+ ///
+ public void PerformSpeciation(IList genomeList)
+ {
+ UpdateShare();
+ ISpecies species = _owner.Population.Species[0];
+ species.Members.Clear();
+ species.Members = species.Members.Union(genomeList).ToList();
+ species.Members.Sort(_sortGenomes);
+ species.Leader = species.Members[0];
+ }
+
+ ///
+ private void UpdateShare()
+ {
+ int speciesCount = _owner.Population.Species.Count;
+ if (speciesCount != 1)
+ {
+ throw new EncogError(
+ "SingleSpeciation can only be used with a species count of 1, species count is "
+ + speciesCount);
+ }
+
+ ISpecies species = _owner.Population.Species[0];
+ species.OffspringCount = _owner.Population.PopulationSize;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Species/ThresholdSpeciation.cs b/encog-core-cs/ML/EA/Species/ThresholdSpeciation.cs
new file mode 100644
index 00000000..ba038b5b
--- /dev/null
+++ b/encog-core-cs/ML/EA/Species/ThresholdSpeciation.cs
@@ -0,0 +1,479 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Sort;
+using Encog.ML.EA.Train;
+using Encog.ML.Genetic;
+
+namespace Encog.ML.EA.Species
+{
+ ///
+ /// Speciate based on threshold. Any genomes with a compatibility score below a
+ /// level will be in the same species.
+ ///
+ [Serializable]
+ public abstract class ThresholdSpeciation : ISpeciation
+ {
+ ///
+ /// The minimum compatibility that two genes must have to be in the same
+ /// species.
+ ///
+ private double _compatibilityThreshold = 1.0;
+
+ ///
+ /// The maximum number of species. This is just a target. If the number of
+ /// species goes over this number then the compatibilityThreshold is
+ /// increased to decrease the number of species.
+ ///
+ private int _maxNumberOfSpecies = 40;
+
+ ///
+ /// The maximum number of generations allows with no improvement. After this
+ /// the genomes in this species are not allowed to reproduce or continue.
+ /// This does not apply to top species.
+ ///
+ private int _numGensAllowedNoImprovement = 15;
+
+ ///
+ /// The training being used.
+ ///
+ private IEvolutionaryAlgorithm _owner;
+
+ ///
+ /// The population.
+ ///
+ private IPopulation _population;
+
+ ///
+ /// The method used to sort the genomes in the species. More desirable
+ /// genomes should come first for later selection.
+ ///
+ private SortGenomesForSpecies _sortGenomes;
+
+ ///
+ /// The minimum compatibility that two genes must have to be in the same
+ /// species.
+ ///
+ public double CompatibilityThreshold
+ {
+ get { return _compatibilityThreshold; }
+ set { _compatibilityThreshold = value; }
+ }
+
+ ///
+ /// The maximum number of species. This is just a target. If the number of
+ /// species goes over this number then the compatibilityThreshold is
+ /// increased to decrease the number of species.
+ ///
+ public int MaxNumberOfSpecies
+ {
+ get { return _maxNumberOfSpecies; }
+ set { _maxNumberOfSpecies = value; }
+ }
+
+ ///
+ /// The maximum number of generations allows with no improvement. After this
+ /// the genomes in this species are not allowed to reproduce or continue.
+ /// This does not apply to top species.
+ ///
+ public int NumGensAllowedNoImprovement
+ {
+ get { return _numGensAllowedNoImprovement; }
+ set { _numGensAllowedNoImprovement = value; }
+ }
+
+ ///
+ /// The owner.
+ ///
+ public IEvolutionaryAlgorithm Owner
+ {
+ get { return _owner; }
+ }
+
+ ///
+ /// The method used to sort the genomes in the species. More desirable
+ /// genomes should come first for later selection.
+ ///
+ public SortGenomesForSpecies SortGenomes
+ {
+ get { return _sortGenomes; }
+ set { _sortGenomes = value; }
+ }
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ _owner = theOwner;
+ _population = theOwner.Population;
+ _sortGenomes = new SortGenomesForSpecies(_owner);
+ }
+
+ ///
+ public void PerformSpeciation(IList genomeList)
+ {
+ IList newGenomeList = ResetSpecies(genomeList);
+ SpeciateAndCalculateSpawnLevels(newGenomeList);
+ }
+
+ ///
+ /// Add a genome.
+ ///
+ /// The species to add to.
+ /// The genome to add.
+ public void AddSpeciesMember(ISpecies species, IGenome genome)
+ {
+ if (_owner.ValidationMode)
+ {
+ if (species.Members.Contains(genome))
+ {
+ throw new GeneticError("Species already contains genome: "
+ + genome);
+ }
+ }
+
+ if (_owner.SelectionComparer.Compare(genome,
+ species.Leader) < 0)
+ {
+ species.BestScore = genome.AdjustedScore;
+ species.GensNoImprovement = 0;
+ species.Leader = genome;
+ }
+
+ species.Add(genome);
+ }
+
+ ///
+ /// Adjust the species compatibility threshold. This prevents us from having
+ /// too many species. Dynamically increase or decrease the
+ /// compatibilityThreshold.
+ ///
+ private void AdjustCompatibilityThreshold()
+ {
+ // has this been disabled (unlimited species)
+ if (_maxNumberOfSpecies < 1)
+ {
+ return;
+ }
+
+ const double thresholdIncrement = 0.01;
+
+ if (_population.Species.Count > _maxNumberOfSpecies)
+ {
+ _compatibilityThreshold += thresholdIncrement;
+ }
+
+ else if (_population.Species.Count < 2)
+ {
+ _compatibilityThreshold -= thresholdIncrement;
+ }
+ }
+
+ ///
+ /// Divide up the potential offspring by the most fit species. To do this we
+ /// look at the total species score, vs each individual species percent
+ /// contribution to that score.
+ ///
+ /// The current species list.
+ /// The total score over all species.
+ private void DivideByFittestSpecies(IEnumerable speciesCollection,
+ double totalSpeciesScore)
+ {
+ ISpecies bestSpecies = FindBestSpecies();
+
+ // loop over all species and calculate its share
+ ISpecies[] speciesArray = speciesCollection.ToArray();
+ foreach (ISpecies element in speciesArray)
+ {
+ var species = element;
+ // calculate the species share based on the percent of the total
+ // species score
+ var share = (int) Math
+ .Round((species.OffspringShare/totalSpeciesScore)
+ *_owner.Population.PopulationSize);
+
+ // do not give the best species a zero-share
+ if ((species == bestSpecies) && (share == 0))
+ {
+ share = 1;
+ }
+
+ // if the share is zero, then remove the species
+ if ((species.Members.Count == 0) || (share == 0))
+ {
+ RemoveSpecies(species);
+ }
+ // if the species has not improved over the specified number of
+ // generations, then remove it.
+ else if ((species.GensNoImprovement > _numGensAllowedNoImprovement)
+ && (species != bestSpecies))
+ {
+ RemoveSpecies(species);
+ }
+ else
+ {
+ // otherwise assign a share and sort the members.
+ species.OffspringCount = share;
+ species.Members.Sort(_sortGenomes);
+ }
+ }
+ }
+
+ ///
+ /// Find the best species.
+ ///
+ /// The best species.
+ public ISpecies FindBestSpecies()
+ {
+ if (_owner.BestGenome != null)
+ {
+ return _owner.BestGenome.Species;
+ }
+ return null;
+ }
+
+ ///
+ /// Attempt to remove a removable species. If the species is the best
+ /// species, then do not remove it. If the species is the last species, don't
+ /// remove it.
+ ///
+ /// The species to attempt to remove.
+ public void RemoveSpecies(ISpecies species)
+ {
+ if (species != FindBestSpecies())
+ {
+ if (_population.Species.Count > 1)
+ {
+ _population.Species.Remove(species);
+ }
+ }
+ }
+
+ ///
+ /// If no species has a good score then divide the potential offspring amount
+ /// all species evenly.
+ ///
+ /// The current set of species.
+ private void DivideEven(IList speciesCollection)
+ {
+ double ratio = 1.0/speciesCollection.Count;
+ foreach (ISpecies species in speciesCollection)
+ {
+ var share = (int) Math.Round(ratio
+ *_owner.Population.PopulationSize);
+ species.OffspringCount = share;
+ }
+ }
+
+ ///
+ /// Level off all of the species shares so that they add up to the desired
+ /// population size. If they do not add up to the desired species size, this
+ /// was a result of rounding the floating point share amounts to integers.
+ ///
+ private void LevelOff()
+ {
+ List list = _population.Species;
+
+ if (list.Count == 0)
+ {
+ throw new EncogError(
+ "Can't speciate, next generation contains no species.");
+ }
+
+ list.Sort(new SpeciesComparer(_owner));
+
+ // best species gets at least one offspring
+ if (list[0].OffspringCount == 0)
+ {
+ list[0].OffspringCount = 1;
+ }
+
+ // total up offspring
+ int total = list.Sum(species => species.OffspringCount);
+
+ // how does the total offspring count match the target
+ int diff = _population.PopulationSize - total;
+
+ if (diff < 0)
+ {
+ // need less offspring
+ int index = list.Count - 1;
+ while ((diff != 0) && (index > 0))
+ {
+ ISpecies species = list[index];
+ int t = Math.Min(species.OffspringCount,
+ Math.Abs(diff));
+ species.OffspringCount = (species.OffspringCount - t);
+ if (species.OffspringCount == 0)
+ {
+ list.Remove(species);
+ }
+ diff += t;
+ index--;
+ }
+ }
+ else
+ {
+ // need more offspring
+ list[0].OffspringCount = (
+ list[0].OffspringCount + diff);
+ }
+ }
+
+ ///
+ /// Reset for an iteration.
+ ///
+ /// The genomes to speciate.
+ ///
+ private IList ResetSpecies(IList inputGenomes)
+ {
+ ISpecies[] speciesArray = _population.Species.ToArray();
+
+ // Add the genomes
+ IList result = inputGenomes.ToList();
+
+ foreach (ISpecies element in speciesArray)
+ {
+ var s = (BasicSpecies) element;
+ s.Purge();
+
+ // did the leader die? If so, disband the species. (but don't kill
+ // the genomes)
+ if (!inputGenomes.Contains(s.Leader))
+ {
+ RemoveSpecies(s);
+ }
+ else if (s.GensNoImprovement > _numGensAllowedNoImprovement)
+ {
+ RemoveSpecies(s);
+ }
+
+ // remove the leader from the list we return. the leader already has
+ // a species
+ result.Remove(s.Leader);
+ }
+
+ if (_population.Species.Count == 0)
+ {
+ throw new EncogError("Can't speciate, the population is empty.");
+ }
+
+ return result;
+ }
+
+ ///
+ /// Determine the species.
+ ///
+ /// The genomes to speciate.
+ private void SpeciateAndCalculateSpawnLevels(IList genomes)
+ {
+ double maxScore = 0;
+
+ if (genomes.Count == 0)
+ {
+ throw new EncogError("Can't speciate, the population is empty.");
+ }
+
+ IList speciesCollection = _population.Species;
+
+ if (speciesCollection.Count == 0)
+ {
+ throw new EncogError("Can't speciate, there are no species.1");
+ }
+
+ // calculate compatibility between genomes and species
+ AdjustCompatibilityThreshold();
+
+ // assign genomes to species (if any exist)
+ foreach (IGenome g in genomes)
+ {
+ ISpecies currentSpecies = null;
+ IGenome genome = g;
+
+ if (!Double.IsNaN(genome.AdjustedScore)
+ && !double.IsInfinity(genome.AdjustedScore))
+ {
+ maxScore = Math.Max(genome.AdjustedScore, maxScore);
+ }
+
+ foreach (ISpecies s in speciesCollection)
+ {
+ double compatibility = GetCompatibilityScore(genome,
+ s.Leader);
+
+ if (compatibility <= _compatibilityThreshold)
+ {
+ currentSpecies = s;
+ AddSpeciesMember(s, genome);
+ genome.Species = s;
+ break;
+ }
+ }
+
+ // if this genome did not fall into any existing species, create a
+ // new species
+ if (currentSpecies == null)
+ {
+ currentSpecies = new BasicSpecies(_population, genome);
+ _population.Species.Add(currentSpecies);
+ }
+ }
+
+ //
+ double totalSpeciesScore = speciesCollection.Sum(species => species.CalculateShare(_owner.ScoreFunction.ShouldMinimize, maxScore));
+
+ if (speciesCollection.Count == 0)
+ {
+ throw new EncogError("Can't speciate, there are no species.2");
+ }
+ if (totalSpeciesScore < EncogFramework.DefaultDoubleEqual)
+ {
+ // This should not happen much, or if it does, only in the
+ // beginning.
+ // All species scored zero. So they are all equally bad. Just divide
+ // up the right to produce offspring evenly.
+ DivideEven(speciesCollection);
+ }
+ else
+ {
+ // Divide up the number of offspring produced to the most fit
+ // species.
+ DivideByFittestSpecies(speciesCollection, totalSpeciesScore);
+ }
+
+ LevelOff();
+ }
+
+ ///
+ /// Determine how compatible two genomes are. More compatible genomes will be
+ /// placed into the same species. The lower the number, the more compatible.
+ ///
+ /// The first genome.
+ /// The second genome.
+ /// The compatability level.
+ public abstract double GetCompatibilityScore(IGenome genome1, IGenome genome2);
+ }
+}
diff --git a/encog-core-cs/ML/EA/Train/BasicEA.cs b/encog-core-cs/ML/EA/Train/BasicEA.cs
new file mode 100644
index 00000000..7e2ecba3
--- /dev/null
+++ b/encog-core-cs/ML/EA/Train/BasicEA.cs
@@ -0,0 +1,515 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Encog.ML.EA.Codec;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Rules;
+using Encog.ML.EA.Score;
+using Encog.ML.EA.Score.Multi;
+using Encog.ML.EA.Sort;
+using Encog.ML.EA.Species;
+using Encog.MathUtil.Randomize.Factory;
+using Encog.Neural.Networks.Training;
+using Encog.Util.Concurrency;
+
+namespace Encog.ML.EA.Train
+{
+ ///
+ /// Provides a basic implementation of a multi-threaded Evolutionary Algorithm.
+ /// The EA works from a score function.
+ ///
+ [Serializable]
+ public class BasicEA : IEvolutionaryAlgorithm, IMultiThreadable
+ {
+ ///
+ /// The score adjusters.
+ ///
+ private readonly IList _adjusters = new List();
+
+ ///
+ /// The population for the next iteration.
+ ///
+ private readonly IList _newPopulation = new List();
+
+ ///
+ /// The operators to use.
+ ///
+ private readonly OperationList _operators = new OperationList();
+
+ ///
+ /// Has the first iteration occured.
+ ///
+ private bool _initialized;
+
+ ///
+ /// The best genome from the last iteration.
+ ///
+ private IGenome _oldBestGenome;
+
+ ///
+ /// The speciation method.
+ ///
+ private ISpeciation _speciation = new SingleSpeciation();
+
+ ///
+ /// Construct an EA.
+ ///
+ /// The population.
+ /// The score function.
+ public BasicEA(IPopulation thePopulation,
+ ICalculateScore theScoreFunction)
+ {
+ RandomNumberFactory = EncogFramework.Instance.RandomFactory.FactorFactory();
+ EliteRate = 0.3;
+ MaxTries = 5;
+ MaxOperationErrors = 500;
+ CODEC = new GenomeAsPhenomeCODEC();
+
+
+ Population = thePopulation;
+ ScoreFunction = theScoreFunction;
+ Selection = new TournamentSelection(this, 4);
+ Rules = new BasicRuleHolder();
+
+ // set the score compare method
+ if (theScoreFunction.ShouldMinimize)
+ {
+ SelectionComparer = new MinimizeAdjustedScoreComp();
+ BestComparer = new MinimizeScoreComp();
+ }
+ else
+ {
+ SelectionComparer = new MaximizeAdjustedScoreComp();
+ BestComparer = new MaximizeScoreComp();
+ }
+
+ // set the iteration
+ foreach (ISpecies species in thePopulation.Species)
+ {
+ foreach (IGenome genome in species.Members)
+ {
+ IterationNumber = Math.Max(IterationNumber,
+ genome.BirthGeneration);
+ }
+ }
+
+
+ // Set a best genome, just so it is not null.
+ // We won't know the true best genome until the first iteration.
+ if (Population.Species.Count > 0 && Population.Species[0].Members.Count > 0)
+ {
+ BestGenome = Population.Species[0].Members[0];
+ }
+ }
+
+ ///
+ /// Should exceptions be ignored.
+ ///
+ public bool IgnoreExceptions { get; set; }
+
+ ///
+ /// Random number factory.
+ ///
+ public IRandomFactory RandomNumberFactory { get; set; }
+
+ ///
+ /// The mutation to be used on the top genome. We want to only modify its
+ /// weights.
+ ///
+ public IEvolutionaryOperator ChampMutation { get; set; }
+
+ ///
+ /// The percentage of a species that is "elite" and is passed on directly.
+ ///
+ public double EliteRate { get; set; }
+
+ ///
+ /// The maximum number of errors to tolerate for the operators before stopping.
+ /// Because this is a stocastic process some operators will generated errors sometimes.
+ ///
+ public int MaxOperationErrors { get; set; }
+
+ ///
+ /// The old best genome.
+ ///
+ public IGenome OldBestGenome
+ {
+ get { return _oldBestGenome; }
+ }
+
+ ///
+ /// The genome comparator.
+ ///
+ public IGenomeComparer BestComparer { get; set; }
+
+ ///
+ /// The genome comparator.
+ ///
+ public IGenomeComparer SelectionComparer { get; set; }
+
+ ///
+ /// The population.
+ ///
+ public IPopulation Population { get; set; }
+
+ ///
+ /// The score calculation function.
+ ///
+ public ICalculateScore ScoreFunction { get; set; }
+
+ ///
+ /// The selection operator.
+ ///
+ public ISelectionOperator Selection { get; set; }
+
+ ///
+ /// The CODEC to use to convert between genome and phenome.
+ ///
+ public IGeneticCODEC CODEC { get; set; }
+
+ ///
+ /// The current iteration.
+ ///
+ public int IterationNumber { get; set; }
+
+ ///
+ /// The validation mode.
+ ///
+ public bool ValidationMode { get; set; }
+
+ ///
+ /// The number of times to try certian operations, so an endless loop does
+ /// not occur.
+ ///
+ public int MaxTries { get; set; }
+
+ ///
+ /// The best ever genome.
+ ///
+ public IGenome BestGenome { get; set; }
+
+ ///
+ /// Holds rewrite and constraint rules.
+ ///
+ public IRuleHolder Rules { get; set; }
+
+ ///
+ public void AddOperation(double probability,
+ IEvolutionaryOperator opp)
+ {
+ Operators.Add(probability, opp);
+ opp.Init(this);
+ }
+
+ ///
+ public void AddScoreAdjuster(IAdjustScore scoreAdjust)
+ {
+ _adjusters.Add(scoreAdjust);
+ }
+
+ ///
+ public void CalculateScore(IGenome g)
+ {
+ // try rewrite
+ Rules.Rewrite(g);
+
+ // decode
+ IMLMethod phenotype = CODEC.Decode(g);
+ double score;
+
+ // deal with invalid decode
+ if (phenotype == null)
+ {
+ score = BestComparer.ShouldMinimize ? Double.PositiveInfinity : Double.NegativeInfinity;
+ }
+ else
+ {
+ var context = phenotype as IMLContext;
+ if (context != null)
+ {
+ context.ClearContext();
+ }
+ score = ScoreFunction.CalculateScore(phenotype);
+ }
+
+ // now set the scores
+ g.Score = score;
+ g.AdjustedScore = score;
+ }
+
+ ///
+ public virtual void FinishTraining()
+ {
+ }
+
+ ///
+ public double Error
+ {
+ get
+ {
+ // do we have a best genome, and does it have an error?
+ if (BestGenome != null)
+ {
+ double err = BestGenome.Score;
+ if (!Double.IsNaN(err))
+ {
+ return err;
+ }
+ }
+
+ // otherwise, assume the worst!
+ if (ScoreFunction.ShouldMinimize)
+ {
+ return Double.PositiveInfinity;
+ }
+ return Double.NegativeInfinity;
+ }
+ }
+
+
+ ///
+ public void Iteration()
+ {
+ if (!_initialized)
+ {
+ PreIteration();
+ }
+
+ if (Population.Species.Count == 0)
+ {
+ throw new EncogError("Population is empty, there are no species.");
+ }
+
+ IterationNumber++;
+
+ // Clear new population to just best genome.
+ _newPopulation.Clear();
+ _newPopulation.Add(BestGenome);
+ _oldBestGenome = BestGenome;
+
+
+ // execute species in parallel
+ IList threadList = new List();
+ foreach (ISpecies species in Population.Species)
+ {
+ int numToSpawn = species.OffspringCount;
+
+ // Add elite genomes directly
+ if (species.Members.Count > 5)
+ {
+ var idealEliteCount = (int) (species.Members.Count*EliteRate);
+ int eliteCount = Math.Min(numToSpawn, idealEliteCount);
+ for (int i = 0; i < eliteCount; i++)
+ {
+ IGenome eliteGenome = species.Members[i];
+ if (_oldBestGenome != eliteGenome)
+ {
+ numToSpawn--;
+ if (!AddChild(eliteGenome))
+ {
+ break;
+ }
+ }
+ }
+ }
+
+ // now add one task for each offspring that each species is allowed
+ while (numToSpawn-- > 0)
+ {
+ var worker = new EAWorker(this, species);
+ threadList.Add(worker);
+ }
+ }
+
+ // run all threads and wait for them to finish
+ Parallel.ForEach(threadList, currentTask => currentTask.PerformTask());
+
+ // validate, if requested
+ if (ValidationMode)
+ {
+ if (_oldBestGenome != null
+ && !_newPopulation.Contains(_oldBestGenome))
+ {
+ throw new EncogError(
+ "The top genome died, this should never happen!!");
+ }
+
+ if (BestGenome != null
+ && _oldBestGenome != null
+ && BestComparer.IsBetterThan(_oldBestGenome,
+ BestGenome))
+ {
+ throw new EncogError(
+ "The best genome's score got worse, this should never happen!! Went from "
+ + _oldBestGenome.Score + " to "
+ + BestGenome.Score);
+ }
+ }
+
+ _speciation.PerformSpeciation(_newPopulation);
+
+ // purge invalid genomes
+ Population.PurgeInvalidGenomes();
+
+ PostIteration();
+ }
+
+ ///
+ /// post iteration added to support strategies.
+ ///
+ public virtual void PostIteration()
+ {
+
+ }
+
+ ///
+ /// The operators.
+ ///
+ public OperationList Operators
+ {
+ get { return _operators; }
+ }
+
+
+ public int MaxIndividualSize { get; set; }
+
+
+ public IList ScoreAdjusters
+ {
+ get { return _adjusters; }
+ }
+
+ public bool ShouldIgnoreExceptions { get; set; }
+
+ public ISpeciation Speciation
+ {
+ get { return _speciation; }
+ set { _speciation = value; }
+ }
+
+ ///
+ /// The desired thread count.
+ ///
+ public int ThreadCount { get; set; }
+
+ ///
+ /// Calculate the score adjustment, based on adjusters.
+ ///
+ /// The genome to adjust.
+ /// The score adjusters.
+ public static void CalculateScoreAdjustment(IGenome genome,
+ IList adjusters)
+ {
+ double score = genome.Score;
+ double delta = adjusters.Sum(a => a.CalculateAdjustment(genome));
+
+ genome.AdjustedScore = (score + delta);
+ }
+
+ ///
+ /// Add a child to the next iteration.
+ ///
+ /// The child.
+ /// True, if the child was added successfully.
+ public bool AddChild(IGenome genome)
+ {
+ lock (_newPopulation)
+ {
+ if (_newPopulation.Count < Population.PopulationSize)
+ {
+ // don't readd the old best genome, it was already added
+ if (genome != _oldBestGenome)
+ {
+ if (ValidationMode)
+ {
+ if (_newPopulation.Contains(genome))
+ {
+ throw new EncogError(
+ "Genome already added to population: "
+ + genome);
+ }
+ }
+
+ _newPopulation.Add(genome);
+ }
+
+ if (!Double.IsInfinity(genome.Score)
+ && !Double.IsNaN(genome.Score)
+ && BestComparer.IsBetterThan(genome,
+ BestGenome))
+ {
+ BestGenome = genome;
+ Population.BestGenome = BestGenome;
+ }
+ return true;
+ }
+ return false;
+ }
+ }
+
+ ///
+ /// Called before the first iteration. Determine the number of threads to
+ /// use.
+ ///
+ private void PreIteration()
+ {
+ _initialized = true;
+ _speciation.Init(this);
+
+ // score the population
+ var pscore = new ParallelScore(Population,
+ CODEC, _adjusters, ScoreFunction, ThreadCount);
+ pscore.Process();
+
+ // just pick the first genome with a valid score as best, it will be
+ // updated later.
+ // also most populations are sorted this way after training finishes
+ // (for reload)
+ // if there is an empty population, the constructor would have blow
+ IList list = Population.Flatten();
+
+ int idx = 0;
+ do
+ {
+ BestGenome = list[idx++];
+ } while (idx < list.Count
+ && (Double.IsInfinity(BestGenome.Score) || Double
+ .IsNaN(BestGenome.Score)));
+
+ Population.BestGenome = BestGenome;
+
+ // speciate
+ IList genomes = Population.Flatten();
+ _speciation.PerformSpeciation(genomes);
+
+ // purge invalid genomes
+ Population.PurgeInvalidGenomes();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Train/EAWorker.cs b/encog-core-cs/ML/EA/Train/EAWorker.cs
new file mode 100644
index 00000000..a93ef9fe
--- /dev/null
+++ b/encog-core-cs/ML/EA/Train/EAWorker.cs
@@ -0,0 +1,185 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Species;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.EA.Train
+{
+ ///
+ /// A worker thread for an Evolutionary Algorithm.
+ ///
+ public class EAWorker
+ {
+ ///
+ /// The children genomes.
+ ///
+ private readonly IGenome[] _children;
+
+ ///
+ /// The parent genomes.
+ ///
+ private readonly IGenome[] _parents;
+
+ ///
+ /// Random number generator.
+ ///
+ private readonly EncogRandom _rnd;
+
+ ///
+ /// The species being processed.
+ ///
+ private readonly ISpecies _species;
+
+ ///
+ /// The parent object.
+ ///
+ private readonly BasicEA _train;
+
+ ///
+ /// Construct the EA worker.
+ ///
+ /// The trainer.
+ /// The species.
+ public EAWorker(BasicEA theTrain, ISpecies theSpecies)
+ {
+ _train = theTrain;
+ _species = theSpecies;
+ _rnd = _train.RandomNumberFactory.Factor();
+
+ _parents = new IGenome[_train.Operators.MaxParents()];
+ _children = new IGenome[_train.Operators.MaxOffspring()];
+ }
+
+ ///
+ /// Choose a parent.
+ ///
+ /// The chosen parent.
+ private IGenome ChooseParent()
+ {
+ int idx = _train.Selection.PerformSelection(_rnd,
+ _species);
+ return _species.Members[idx];
+ }
+
+ ///
+ /// Perform one operation.
+ ///
+ public void PerformTask()
+ {
+ bool success = false;
+ int tries = _train.MaxOperationErrors;
+ do
+ {
+ try
+ {
+ // choose an evolutionary operation (i.e. crossover or a type of
+ // mutation) to use
+ IEvolutionaryOperator opp = _train.Operators
+ .PickMaxParents(_rnd,
+ _species.Members.Count);
+
+ _children[0] = null;
+
+ // prepare for either sexual or asexual reproduction either way,
+ // we need at least one parent, which is the first parent.
+ //
+ // Chose the first parent, there must be at least one genome in
+ // this species
+ _parents[0] = ChooseParent();
+
+ // if the number of individuals in this species is only
+ // one then we can only clone and perhaps mutate, otherwise use
+ // the crossover probability to determine if we are to use
+ // sexual reproduction.
+ if (opp.ParentsNeeded > 1)
+ {
+ int numAttempts = 5;
+
+ _parents[1] = ChooseParent();
+ while (_parents[0] == _parents[1]
+ && numAttempts-- > 0)
+ {
+ _parents[1] = ChooseParent();
+ }
+
+ // success, perform crossover
+ if (_parents[0] != _parents[1])
+ {
+ opp.PerformOperation(_rnd, _parents, 0,
+ _children, 0);
+ }
+ }
+ else
+ {
+ // clone a child (asexual reproduction)
+ opp.PerformOperation(_rnd, _parents, 0,
+ _children, 0);
+ _children[0].Population = _parents[0].Population;
+ }
+
+ // process the new child
+ foreach (IGenome child in _children)
+ {
+ if (child != null)
+ {
+ child.Population = _parents[0].Population;
+ if (_train.Rules.IsValid(child))
+ {
+ child.BirthGeneration = _train.IterationNumber;
+
+ _train.CalculateScore(child);
+ if (!_train.AddChild(child))
+ {
+ return;
+ }
+ success = true;
+ }
+ }
+ }
+ }
+ catch (EARuntimeError)
+ {
+ tries--;
+ if (tries < 0)
+ {
+ throw new EncogError(
+ "Could not perform a successful genetic operaton after "
+ + _train.MaxOperationErrors
+ + " tries.");
+ }
+ }
+ catch (Exception t)
+ {
+ if (!_train.IgnoreExceptions)
+ {
+ throw;
+ }
+ }
+ } while (!success);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/EA/Train/IEvolutionaryAlgorithm.cs b/encog-core-cs/ML/EA/Train/IEvolutionaryAlgorithm.cs
new file mode 100644
index 00000000..495840e1
--- /dev/null
+++ b/encog-core-cs/ML/EA/Train/IEvolutionaryAlgorithm.cs
@@ -0,0 +1,173 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Codec;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Rules;
+using Encog.ML.EA.Score;
+using Encog.ML.EA.Sort;
+using Encog.ML.EA.Species;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.EA.Train
+{
+ ///
+ /// This interface defines the basic functionality of an Evolutionary Algorithm.
+ /// An evolutionary algorithm is one that applies operations to a population of
+ /// potential "solutions".
+ ///
+ public interface IEvolutionaryAlgorithm
+ {
+ ///
+ /// Get the comparator that is used to choose the "true best" genome. This
+ /// uses the real score, and not the adjusted score.
+ ///
+ IGenomeComparer BestComparer { get; set; }
+
+ ///
+ /// The current best genome. This genome is safe to use while the EA
+ /// is running. Genomes are not modified. They simply produce
+ /// "offspring".
+ ///
+ IGenome BestGenome { get; }
+
+ ///
+ /// The CODEC that is used to transform between genome and phenome.
+ ///
+ IGeneticCODEC CODEC { get; set; }
+
+ ///
+ /// The current score. This value should either be minimized or
+ /// maximized, depending on the score function.
+ ///
+ double Error { get; }
+
+ ///
+ /// The current iteration number. Also sometimes referred to as
+ /// generation or epoch.
+ ///
+ int IterationNumber { get; }
+
+ ///
+ /// The maximum size an individual genome can be. This is an
+ /// arbitrary number defined by the genome. Lower numbers are less
+ /// complex.
+ ///
+ int MaxIndividualSize { get; }
+
+ ///
+ /// The maximum number to try certain genetic operations. This
+ /// prevents endless loops.
+ ///
+ int MaxTries { get; }
+
+ ///
+ /// The operators.
+ ///
+ OperationList Operators { get; }
+
+ ///
+ /// The population.
+ ///
+ IPopulation Population { get; set; }
+
+ ///
+ /// The rules holder, contains rewrite and constraint rules.
+ ///
+ IRuleHolder Rules { get; set; }
+
+ ///
+ /// The score adjusters. This allows bonuses and penalties to be
+ /// applied for desirable or undesirable traits.
+ ///
+ IList ScoreAdjusters { get; }
+
+ ///
+ /// The score function.
+ ///
+ ICalculateScore ScoreFunction { get; }
+
+ ///
+ /// The selection operator. Used to choose genomes.
+ ///
+ ISelectionOperator Selection { get; set; }
+
+ ///
+ /// Get the comparator that is used to choose the "best" genome for
+ /// selection, as opposed to the "true best". This uses the adjusted score,
+ /// and not the score.
+ ///
+ IGenomeComparer SelectionComparer { get; set; }
+
+ ///
+ /// True if exceptions that occur during genetic operations should be
+ /// ignored.
+ ///
+ bool ShouldIgnoreExceptions { get; set; }
+
+ ///
+ /// The speciation method.
+ ///
+ ISpeciation Speciation { get; set; }
+
+ ///
+ /// True if any genome validators should be applied.
+ ///
+ bool ValidationMode { get; set; }
+
+ ///
+ /// Add an operation.
+ ///
+ /// The probability of using this operator.
+ /// The operator to add.
+ void AddOperation(double probability, IEvolutionaryOperator opp);
+
+ ///
+ /// Add a score adjuster. Score adjusters are used to adjust the adjusted
+ /// score of a genome. This allows bonuses and penalties to be applied for
+ /// desirable or undesirable traits.
+ ///
+ /// The score adjustor to add.
+ void AddScoreAdjuster(IAdjustScore scoreAdjust);
+
+ ///
+ /// Calculate the score for a genome.
+ ///
+ /// The genome to calculate the score for.
+ void CalculateScore(IGenome g);
+
+ ///
+ /// Called when training is finished. This allows the EA to properly shut
+ /// down.
+ ///
+ void FinishTraining();
+
+ ///
+ /// Perform a training iteration. Also called generations or epochs.
+ ///
+ void Iteration();
+ }
+}
diff --git a/encog-core-cs/ML/EA/Train/TrainEA.cs b/encog-core-cs/ML/EA/Train/TrainEA.cs
new file mode 100644
index 00000000..90afae6a
--- /dev/null
+++ b/encog-core-cs/ML/EA/Train/TrainEA.cs
@@ -0,0 +1,181 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.Data;
+using Encog.ML.EA.Population;
+using Encog.ML.Train;
+using Encog.ML.Train.Strategy;
+using Encog.Neural.Networks.Training;
+using Encog.Neural.Networks.Training.Propagation;
+using Encog.ML.Train.Strategy.End;
+using System.Linq;
+
+namespace Encog.ML.EA.Train
+{
+ ///
+ /// Provides a MLTrain compatible class that can be used to train genomes.
+ ///
+ public class TrainEA : BasicEA, IMLTrain
+ {
+ ///
+ /// The training strategies to use.
+ ///
+ ///
+ private readonly IList _strategies;
+
+ ///
+ /// Create a trainer for a score function.
+ ///
+ /// The population.
+ /// The score function.
+ public TrainEA(IPopulation thePopulation, ICalculateScore theScoreFunction)
+ : base(thePopulation, theScoreFunction)
+ {
+ _strategies = new List();
+ }
+
+ ///
+ /// Create a trainer for training data.
+ ///
+ /// The population.
+ /// The training data.
+ public TrainEA(IPopulation thePopulation, IMLDataSet trainingData)
+ : base(thePopulation, new TrainingSetScore(trainingData))
+ {
+ }
+
+ ///
+ /// Not used, the error.
+ ///
+ public new double Error
+ {
+ get { return base.Error; }
+ set
+ {
+ // not needed
+ }
+ }
+
+ ///
+ /// Call the strategies after an iteration.
+ ///
+ ///
+ public override void PostIteration()
+ {
+ foreach (IStrategy strategy in _strategies)
+ {
+ strategy.PostIteration();
+ }
+ }
+
+ /// True if training can progress no further.
+ public virtual bool TrainingDone
+ {
+ get { return _strategies.OfType().Any(end => end.ShouldStop()); }
+ }
+
+
+ ///
+ public TrainingImplementationType ImplementationType
+ {
+ get { return TrainingImplementationType.Iterative; }
+ }
+
+ ///
+ /// Perform the specified number of training iterations. This is a basic
+ /// implementation that just calls iteration the specified number of times.
+ /// However, some training methods, particularly with the GPU, benefit
+ /// greatly by calling with higher numbers than 1.
+ ///
+ /// The number of training iterations.
+ public void Iteration(int count)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ Iteration();
+ }
+ }
+
+
+
+ ///
+ public TrainingContinuation Pause()
+ {
+ return null;
+ }
+
+ ///
+ public void Resume(TrainingContinuation state)
+ {
+ }
+
+ ///
+ /// Not supported, will throw an error.
+ ///
+ /// Not used.
+ public void AddStrategy(IStrategy strategy)
+ {
+ strategy.Init(this);
+ _strategies.Add(strategy);
+ //throw new TrainingError(
+ // "Strategies are not supported by this training method.");
+ }
+
+ ///
+ public bool CanContinue
+ {
+ get { return false; }
+ }
+
+ ///
+ public override void FinishTraining()
+ {
+ base.FinishTraining();
+ Population.BestGenome = BestGenome;
+ }
+
+ ///
+ /// A network created for the best genome.
+ ///
+ public IMLMethod Method
+ {
+ get { return Population; }
+ }
+
+ ///
+ /// Returns null, does not use a training set, rather uses a score function.
+ ///
+ public IMLDataSet Training
+ {
+ get { return null; }
+ }
+
+ ///
+ /// Returns an empty list, strategies are not supported.
+ ///
+ public IList Strategies
+ {
+ get { return _strategies; }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Factory/MLActivationFactory.cs b/encog-core-cs/ML/Factory/MLActivationFactory.cs
index 42eb32b7..ffe5ba25 100644
--- a/encog-core-cs/ML/Factory/MLActivationFactory.cs
+++ b/encog-core-cs/ML/Factory/MLActivationFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -38,11 +38,13 @@ public class MLActivationFactory
public const String AF_LOG = "log";
public const String AF_RAMP = "ramp";
public const String AF_SIGMOID = "sigmoid";
+ public const String AF_SSIGMOID = "ssigmoid";
public const String AF_SIN = "sin";
public const String AF_SOFTMAX = "softmax";
public const String AF_STEP = "step";
public const String AF_TANH = "tanh";
+
public IActivationFunction Create(String fn)
{
diff --git a/encog-core-cs/ML/Factory/MLMethodFactory.cs b/encog-core-cs/ML/Factory/MLMethodFactory.cs
index d9311157..ffa54c84 100644
--- a/encog-core-cs/ML/Factory/MLMethodFactory.cs
+++ b/encog-core-cs/ML/Factory/MLMethodFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ public class MLMethodFactory
///
/// String constant for a bayesian neural network.
///
- public const String TypeBayesian = "bayesian";
+ public const String TypeBayesian = "bayesian";
///
/// String constant for feedforward neural networks.
@@ -67,6 +67,26 @@ public class MLMethodFactory
///
public const String TypePNN = "pnn";
+ ///
+ /// A NEAT neural network.
+ ///
+ public const String TypeNEAT = "neat";
+
+ ///
+ /// A Encog program.
+ ///
+ public const String TypeEPL = "epl";
+
+
+ public const String PropertyAF = "AF";
+
+ ///
+ /// Population size.
+ ///
+ public const String PropertyPopulationSize = "population";
+
+ public const String PropertyCycles = "cycles";
+
///
/// Create a new machine learning method.
///
diff --git a/encog-core-cs/ML/Factory/MLTrainFactory.cs b/encog-core-cs/ML/Factory/MLTrainFactory.cs
index b6043cd0..151525f3 100644
--- a/encog-core-cs/ML/Factory/MLTrainFactory.cs
+++ b/encog-core-cs/ML/Factory/MLTrainFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -114,6 +114,16 @@ public class MLTrainFactory
///
public const String TypeSOMCluster = "som-cluster";
+ ///
+ /// String constant for LMA training.
+ ///
+ public const String TypeNEATGA = "neat-ga";
+
+ ///
+ /// String constant for LMA training.
+ ///
+ public const String TypeEPLGA = "epl-ga";
+
///
/// Property for learning rate.
///
diff --git a/encog-core-cs/ML/Factory/Method/BayesianFactory.cs b/encog-core-cs/ML/Factory/Method/BayesianFactory.cs
index 7e90a553..b7d94c34 100644
--- a/encog-core-cs/ML/Factory/Method/BayesianFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/BayesianFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/EPLFactory.cs b/encog-core-cs/ML/Factory/Method/EPLFactory.cs
new file mode 100644
index 00000000..8876b32b
--- /dev/null
+++ b/encog-core-cs/ML/Factory/Method/EPLFactory.cs
@@ -0,0 +1,91 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.ML.Factory.Parse;
+using Encog.Util;
+using Encog.ML.Prg;
+using Encog.ML.Prg.Train;
+using Encog.ML.Prg.Generator;
+using Encog.MathUtil.Randomize;
+using Encog.ML.Prg.Ext;
+
+namespace Encog.ML.Factory.Method
+{
+ ///
+ /// Create an EPL method.
+ ///
+ public class EPLFactory
+ {
+ ///
+ /// Create a feed forward network.
+ ///
+ /// The architecture string to use.
+ /// The input count.
+ /// The output count.
+ /// The feedforward network.
+ public IMLMethod Create(String architecture, int input,
+ int output)
+ {
+
+ if (input <= 0)
+ {
+ throw new EncogError("Must have at least one input for EPL.");
+ }
+
+ if (output <= 0)
+ {
+ throw new EncogError("Must have at least one output for EPL.");
+ }
+
+
+ IDictionary args = ArchitectureParse.ParseParams(architecture);
+ var holder = new ParamsHolder(args);
+
+ int populationSize = holder.GetInt(
+ MLMethodFactory.PropertyPopulationSize, false, 1000);
+ String variables = holder.GetString("vars", false, "x");
+ String funct = holder.GetString("funct", false, null);
+
+ var context = new EncogProgramContext();
+ string[] tok = variables.Split(',');
+ foreach (string v in tok)
+ {
+ context.DefineVariable(v);
+ }
+
+ if (String.Compare("numeric", funct, StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ StandardExtensions.CreateNumericOperators(context);
+ }
+
+ var pop = new PrgPopulation(context, populationSize);
+
+ if (context.Functions.Count > 0)
+ {
+ (new RampedHalfAndHalf(context, 2, 6)).Generate(new EncogRandom(), pop);
+ }
+ return pop;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Factory/Method/FeedforwardFactory.cs b/encog-core-cs/ML/Factory/Method/FeedforwardFactory.cs
index dacd033d..846e6116 100644
--- a/encog-core-cs/ML/Factory/Method/FeedforwardFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/FeedforwardFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/NEATFactory.cs b/encog-core-cs/ML/Factory/Method/NEATFactory.cs
new file mode 100644
index 00000000..e8e12350
--- /dev/null
+++ b/encog-core-cs/ML/Factory/Method/NEATFactory.cs
@@ -0,0 +1,83 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Factory.Parse;
+using Encog.Util;
+using Encog.Neural.NEAT;
+using Encog.Engine.Network.Activation;
+
+namespace Encog.ML.Factory.Method
+{
+ public class NEATFactory
+ {
+ ///
+ /// The activation function factory to use.
+ ///
+ private MLActivationFactory factory = new MLActivationFactory();
+
+ ///
+ /// Create a NEAT population.
+ ///
+ /// The architecture string to use.
+ /// The input count.
+ /// The output count.
+ /// The population.
+ public IMLMethod Create(String architecture, int input,
+ int output)
+ {
+
+ if (input <= 0)
+ {
+ throw new EncogError("Must have at least one input for NEAT.");
+ }
+
+ if (output <= 0)
+ {
+ throw new EncogError("Must have at least one output for NEAT.");
+ }
+
+
+ IDictionary args = ArchitectureParse.ParseParams(architecture);
+ ParamsHolder holder = new ParamsHolder(args);
+
+ int populationSize = holder.GetInt(
+ MLMethodFactory.PropertyPopulationSize, false, 1000);
+
+ int cycles = holder.GetInt(
+ MLMethodFactory.PropertyCycles, false, NEATPopulation.DefaultCycles);
+
+ IActivationFunction af = this.factory.Create(
+ holder.GetString(MLMethodFactory.PropertyAF, false, MLActivationFactory.AF_SSIGMOID));
+
+ NEATPopulation pop = new NEATPopulation(input, output, populationSize);
+ pop.Reset();
+ pop.ActivationCycles = cycles;
+ pop.NEATActivationFunction = af;
+
+ return pop;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Factory/Method/PNNFactory.cs b/encog-core-cs/ML/Factory/Method/PNNFactory.cs
index 3d5d7092..bdb27417 100644
--- a/encog-core-cs/ML/Factory/Method/PNNFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/PNNFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/RBFNetworkFactory.cs b/encog-core-cs/ML/Factory/Method/RBFNetworkFactory.cs
index d7e629be..17f582cb 100644
--- a/encog-core-cs/ML/Factory/Method/RBFNetworkFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/RBFNetworkFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/SOMFactory.cs b/encog-core-cs/ML/Factory/Method/SOMFactory.cs
index eb17a490..5298da9e 100644
--- a/encog-core-cs/ML/Factory/Method/SOMFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/SOMFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/SRNFactory.cs b/encog-core-cs/ML/Factory/Method/SRNFactory.cs
index af51dfb2..dcc5f0ec 100644
--- a/encog-core-cs/ML/Factory/Method/SRNFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/SRNFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Method/SVMFactory.cs b/encog-core-cs/ML/Factory/Method/SVMFactory.cs
index c0a0a3f5..a9a6aa58 100644
--- a/encog-core-cs/ML/Factory/Method/SVMFactory.cs
+++ b/encog-core-cs/ML/Factory/Method/SVMFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Parse/ArchitectureLayer.cs b/encog-core-cs/ML/Factory/Parse/ArchitectureLayer.cs
index 251dace3..c05a5f56 100644
--- a/encog-core-cs/ML/Factory/Parse/ArchitectureLayer.cs
+++ b/encog-core-cs/ML/Factory/Parse/ArchitectureLayer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Parse/ArchitectureParse.cs b/encog-core-cs/ML/Factory/Parse/ArchitectureParse.cs
index 60134f88..c404720f 100644
--- a/encog-core-cs/ML/Factory/Parse/ArchitectureParse.cs
+++ b/encog-core-cs/ML/Factory/Parse/ArchitectureParse.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/AnnealFactory.cs b/encog-core-cs/ML/Factory/Train/AnnealFactory.cs
index 95bb05df..3d9683c8 100644
--- a/encog-core-cs/ML/Factory/Train/AnnealFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/AnnealFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/BackPropFactory.cs b/encog-core-cs/ML/Factory/Train/BackPropFactory.cs
index 8a92fd17..9971ec3a 100644
--- a/encog-core-cs/ML/Factory/Train/BackPropFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/BackPropFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/BayesSearch.cs b/encog-core-cs/ML/Factory/Train/BayesSearch.cs
index 157a5181..7748a0ae 100644
--- a/encog-core-cs/ML/Factory/Train/BayesSearch.cs
+++ b/encog-core-cs/ML/Factory/Train/BayesSearch.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/ClusterSOMFactory.cs b/encog-core-cs/ML/Factory/Train/ClusterSOMFactory.cs
index 2b9ce081..b682f741 100644
--- a/encog-core-cs/ML/Factory/Train/ClusterSOMFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/ClusterSOMFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/EPLGAFactory.cs b/encog-core-cs/ML/Factory/Train/EPLGAFactory.cs
new file mode 100644
index 00000000..45d8c411
--- /dev/null
+++ b/encog-core-cs/ML/Factory/Train/EPLGAFactory.cs
@@ -0,0 +1,68 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Train;
+using Encog.ML.Data;
+using Encog.ML.Prg.Train;
+using Encog.Neural.Networks.Training;
+using Encog.ML.EA.Train;
+using Encog.ML.Prg.Train.Rewrite;
+using Encog.ML.Prg;
+using Encog.ML.Prg.Species;
+using Encog.ML.Prg.Opp;
+using Encog.ML.EA.Score.Adjust;
+
+namespace Encog.ML.Factory.Train
+{
+ public class EPLGAFactory
+ {
+ ///
+ /// Create an EPL GA trainer.
+ ///
+ /// The method to use.
+ /// The training data to use.
+ /// The arguments to use.
+ /// The newly created trainer.
+ public IMLTrain Create(IMLMethod method,
+ IMLDataSet training, String argsStr)
+ {
+
+ PrgPopulation pop = (PrgPopulation)method;
+
+ ICalculateScore score = new TrainingSetScore(training);
+ TrainEA train = new TrainEA(pop, score);
+ train.Rules.AddRewriteRule(new RewriteConstants());
+ train.Rules.AddRewriteRule(new RewriteAlgebraic());
+ train.CODEC = new PrgCODEC();
+ train.AddOperation(0.8, new SubtreeCrossover());
+ train.AddOperation(0.1, new SubtreeMutation(pop.Context, 4));
+ train.AddOperation(0.1, new ConstMutation(pop.Context, 0.5, 1.0));
+ train.AddScoreAdjuster(new ComplexityAdjustedScore());
+ train.Speciation = new PrgSpeciation();
+ return train;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Factory/Train/GeneticFactory.cs b/encog-core-cs/ML/Factory/Train/GeneticFactory.cs
index 294d92a9..0ae9cef2 100644
--- a/encog-core-cs/ML/Factory/Train/GeneticFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/GeneticFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -28,8 +28,8 @@
using Encog.ML.Train;
using Encog.Neural.Networks;
using Encog.Neural.Networks.Training;
-using Encog.Neural.Networks.Training.Genetic;
using Encog.Util;
+using Encog.ML.Genetic;
namespace Encog.ML.Factory.Train
{
@@ -42,7 +42,6 @@ public class GeneticFactory
///
/// Create an annealing trainer.
///
- ///
/// The method to use.
/// The training data to use.
/// The arguments to use.
@@ -61,17 +60,18 @@ public IMLTrain Create(IMLMethod method,
IDictionary args = ArchitectureParse.ParseParams(argsStr);
var holder = new ParamsHolder(args);
int populationSize = holder.GetInt(
- MLTrainFactory.PropertyPopulationSize, false, 5000);
- double mutation = holder.GetDouble(
- MLTrainFactory.PropertyMutation, false, 0.1d);
- double mate = holder.GetDouble(MLTrainFactory.PropertyMate,
- false, 0.25d);
+ MLTrainFactory.PropertyPopulationSize, false, 5000);
+
+ IMLTrain train = new MLMethodGeneticAlgorithm( () => {
+
+ IMLMethod result = (IMLMethod) ObjectCloner.DeepCopy(method);
+ ((IMLResettable)result).Reset();
+ return result;
+ }, score, populationSize);
- IMLTrain train = new NeuralGeneticAlgorithm((BasicNetwork) method,
- new RangeRandomizer(-1, 1), score, populationSize, mutation,
- mate);
+ return train;
- return train;
+
}
}
}
diff --git a/encog-core-cs/ML/Factory/Train/LMAFactory.cs b/encog-core-cs/ML/Factory/Train/LMAFactory.cs
index b8ac500a..9e4dbba4 100644
--- a/encog-core-cs/ML/Factory/Train/LMAFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/LMAFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/ManhattanFactory.cs b/encog-core-cs/ML/Factory/Train/ManhattanFactory.cs
index 9fda22a3..fdfa7e49 100644
--- a/encog-core-cs/ML/Factory/Train/ManhattanFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/ManhattanFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/NEATGAFactory.cs b/encog-core-cs/ML/Factory/Train/NEATGAFactory.cs
new file mode 100644
index 00000000..b711377f
--- /dev/null
+++ b/encog-core-cs/ML/Factory/Train/NEATGAFactory.cs
@@ -0,0 +1,57 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Train;
+using Encog.ML.Data;
+using Encog.Neural.Networks.Training;
+using Encog.Neural.NEAT;
+using Encog.ML.EA.Train;
+
+namespace Encog.ML.Factory.Train
+{
+ ///
+ /// A factory to create genetic algorithm trainers.
+ ///
+ public class NEATGAFactory
+ {
+ ///
+ /// Create an NEAT GA trainer.
+ ///
+ /// The method to use.
+ /// The training data to use.
+ /// The arguments to use.
+ /// The newly created trainer.
+ public IMLTrain Create(IMLMethod method,
+ IMLDataSet training, String argsStr)
+ {
+
+ ICalculateScore score = new TrainingSetScore(training);
+ TrainEA train = NEATUtil.ConstructNEATTrainer((NEATPopulation)method, score);
+
+ return train;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Factory/Train/NeighborhoodSOMFactory.cs b/encog-core-cs/ML/Factory/Train/NeighborhoodSOMFactory.cs
index 4080f312..0cb37e9f 100644
--- a/encog-core-cs/ML/Factory/Train/NeighborhoodSOMFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/NeighborhoodSOMFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/NelderMeadFactory.cs b/encog-core-cs/ML/Factory/Train/NelderMeadFactory.cs
index fa6cf39f..f01ae062 100644
--- a/encog-core-cs/ML/Factory/Train/NelderMeadFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/NelderMeadFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/PNNTrainFactory.cs b/encog-core-cs/ML/Factory/Train/PNNTrainFactory.cs
index 5e3f180b..5ffaf353 100644
--- a/encog-core-cs/ML/Factory/Train/PNNTrainFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/PNNTrainFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/PSOFactory.cs b/encog-core-cs/ML/Factory/Train/PSOFactory.cs
index 26dc47e9..4721a273 100644
--- a/encog-core-cs/ML/Factory/Train/PSOFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/PSOFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/QuickPropFactory.cs b/encog-core-cs/ML/Factory/Train/QuickPropFactory.cs
index e929c034..1112dfe6 100644
--- a/encog-core-cs/ML/Factory/Train/QuickPropFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/QuickPropFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/RBFSVDFactory.cs b/encog-core-cs/ML/Factory/Train/RBFSVDFactory.cs
index 653c5101..0195804e 100644
--- a/encog-core-cs/ML/Factory/Train/RBFSVDFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/RBFSVDFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/RPROPFactory.cs b/encog-core-cs/ML/Factory/Train/RPROPFactory.cs
index d7884bcd..b945e902 100644
--- a/encog-core-cs/ML/Factory/Train/RPROPFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/RPROPFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/SCGFactory.cs b/encog-core-cs/ML/Factory/Train/SCGFactory.cs
index d402936d..3ecc9618 100644
--- a/encog-core-cs/ML/Factory/Train/SCGFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/SCGFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/SVMFactory.cs b/encog-core-cs/ML/Factory/Train/SVMFactory.cs
index 9f2c9858..856b3255 100644
--- a/encog-core-cs/ML/Factory/Train/SVMFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/SVMFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/SVMSearchFactory.cs b/encog-core-cs/ML/Factory/Train/SVMSearchFactory.cs
index 329f8a73..4934ce92 100644
--- a/encog-core-cs/ML/Factory/Train/SVMSearchFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/SVMSearchFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Factory/Train/TrainBayesianFactory.cs b/encog-core-cs/ML/Factory/Train/TrainBayesianFactory.cs
index f5f0a1e2..08eab4ad 100644
--- a/encog-core-cs/ML/Factory/Train/TrainBayesianFactory.cs
+++ b/encog-core-cs/ML/Factory/Train/TrainBayesianFactory.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Fitness/FitnessObjective.cs b/encog-core-cs/ML/Fitness/FitnessObjective.cs
new file mode 100644
index 00000000..f24e82d6
--- /dev/null
+++ b/encog-core-cs/ML/Fitness/FitnessObjective.cs
@@ -0,0 +1,79 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.Fitness
+{
+ ///
+ /// A fitness objective.
+ ///
+ public class FitnessObjective
+ {
+ ///
+ /// The weight.
+ ///
+ private double weight;
+
+ ///
+ /// The score function.
+ ///
+ private ICalculateScore score;
+
+ ///
+ /// Construct the fitness objective.
+ ///
+ /// The weight.
+ /// The score.
+ public FitnessObjective(double weight, ICalculateScore score)
+ {
+ this.weight = weight;
+ this.score = score;
+ }
+
+ ///
+ /// The weight.
+ ///
+ public double Weight
+ {
+ get
+ {
+ return weight;
+ }
+ }
+
+ ///
+ /// The score.
+ ///
+ public ICalculateScore Score
+ {
+ get
+ {
+ return score;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Fitness/MultiObjectiveFitness.cs b/encog-core-cs/ML/Fitness/MultiObjectiveFitness.cs
new file mode 100644
index 00000000..ec62123f
--- /dev/null
+++ b/encog-core-cs/ML/Fitness/MultiObjectiveFitness.cs
@@ -0,0 +1,108 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.Fitness
+{
+ ///
+ /// A multi-objective fitness function.
+ ///
+ [Serializable]
+ public class MultiObjectiveFitness : ICalculateScore
+ {
+ ///
+ /// The objectives.
+ ///
+ private IList objectives = new List();
+
+ ///
+ /// Is the goal to minimize the score?
+ ///
+ private bool min;
+
+ ///
+ /// Add an objective.
+ ///
+ /// The weight of this objective, 1.0 for full, 0.5 for half, etc.
+ /// The fitness function.
+ public void AddObjective(double weight, ICalculateScore fitnessFunction)
+ {
+ if (this.objectives.Count == 0)
+ {
+ this.min = fitnessFunction.ShouldMinimize;
+ }
+ else
+ {
+ if (fitnessFunction.ShouldMinimize != this.min)
+ {
+ throw new EncogError("Multi-objective mismatch, some objectives are min and some are max.");
+ }
+ }
+ this.objectives.Add(new FitnessObjective(weight, fitnessFunction));
+ }
+
+ ///
+ public double CalculateScore(IMLMethod method)
+ {
+ double result = 0;
+
+ foreach (FitnessObjective obj in this.objectives)
+ {
+ result += obj.Score.CalculateScore(method) * obj.Weight;
+ }
+
+ return result;
+ }
+
+ ///
+ public bool ShouldMinimize
+ {
+ get
+ {
+ return this.min;
+ }
+ }
+
+ ///
+ public bool RequireSingleThreaded
+ {
+ get
+ {
+ foreach (FitnessObjective obj in this.objectives)
+ {
+ if (obj.Score.RequireSingleThreaded)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/BasicGeneticAlgorithm.cs b/encog-core-cs/ML/Genetic/BasicGeneticAlgorithm.cs
deleted file mode 100644
index a807b433..00000000
--- a/encog-core-cs/ML/Genetic/BasicGeneticAlgorithm.cs
+++ /dev/null
@@ -1,88 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.ML.Genetic.Genome;
-using Encog.MathUtil;
-using System.Threading.Tasks;
-
-namespace Encog.ML.Genetic
-{
- ///
- /// Provides a basic implementation of a genetic algorithm.
- ///
- ///
- public class BasicGeneticAlgorithm : GeneticAlgorithm
- {
- ///
- /// Is this the first iteration.
- ///
- ///
- private bool _first;
-
- ///
- /// Construct the object.
- ///
- public BasicGeneticAlgorithm()
- {
- _first = true;
- }
-
- ///
- /// Modify the weight matrix and bias values based on the last call to
- /// calcError.
- ///
- public override sealed void Iteration()
- {
- if (_first)
- {
- Population.Claim(this);
- _first = false;
- }
-
- var countToMate = (int) (Population.PopulationSize*PercentToMate);
- int offspringCount = countToMate*2;
- int offspringIndex = Population.PopulationSize
- - offspringCount;
- var matingPopulationSize = (int) (Population.PopulationSize*MatingPopulation);
-
- // mate and form the next generation
- Parallel.For(0, countToMate, i =>
- {
- IGenome mother = Population.Genomes[i];
- var fatherInt = (int)(ThreadSafeRandom.NextDouble() * matingPopulationSize);
- IGenome father = Population.Genomes[fatherInt];
- IGenome child1 = Population.Genomes[offspringIndex];
- IGenome child2 = Population.Genomes[offspringIndex + 1];
-
- var worker = new MateWorker(mother, father, child1,
- child2);
-
- worker.Run();
-
- offspringIndex += 2;
- });
-
- // sort the next generation
- Population.Sort();
- }
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Crossover/Splice.cs b/encog-core-cs/ML/Genetic/Crossover/Splice.cs
index 54a0f543..cc824a60 100644
--- a/encog-core-cs/ML/Genetic/Crossover/Splice.cs
+++ b/encog-core-cs/ML/Genetic/Crossover/Splice.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,8 +20,16 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Train;
+using Encog.ML.EA.Genome;
using Encog.ML.Genetic.Genome;
-using Encog.MathUtil;
+using Encog.MathUtil.Randomize;
namespace Encog.ML.Genetic.Crossover
{
@@ -29,52 +37,53 @@ namespace Encog.ML.Genetic.Crossover
/// A simple cross over where genes are simply "spliced". Genes are allowed to
/// repeat.
///
- ///
- public class Splice : ICrossover
+ public class Splice : IEvolutionaryOperator
{
///
/// The cut length.
///
- ///
- private readonly int _cutLength;
+ private int cutLength;
///
- /// Create a slice crossover with the specified cut length.
+ /// The owner.
+ ///
+ private IEvolutionaryAlgorithm owner;
+
+ ///
+ /// Create a slice crossover with the specified cut length.
///
- ///
/// The cut length.
public Splice(int theCutLength)
{
- _cutLength = theCutLength;
+ this.cutLength = theCutLength;
}
- #region ICrossover Members
-
- ///
- /// Assuming this chromosome is the "mother" mate with the passed in
- /// "father".
- ///
- ///
- /// The mother.
- /// The father.
- /// Returns the first offspring
- /// Returns the second offspring.
- public void Mate(Chromosome mother, Chromosome father,
- Chromosome offspring1, Chromosome offspring2)
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
+ IGenome[] offspring, int offspringIndex)
{
- int geneLength = mother.Genes.Count;
+
+ IArrayGenome mother = (IArrayGenome)parents[parentIndex];
+ IArrayGenome father = (IArrayGenome)parents[parentIndex + 1];
+ IArrayGenome offspring1 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();
+ IArrayGenome offspring2 = (IArrayGenome)this.owner.Population.GenomeFactory.Factor();
+
+ offspring[offspringIndex] = offspring1;
+ offspring[offspringIndex + 1] = offspring2;
+
+ int geneLength = mother.Size;
// the chromosome must be cut at two positions, determine them
- var cutpoint1 = (int)(ThreadSafeRandom.NextDouble()*(geneLength - _cutLength));
- int cutpoint2 = cutpoint1 + _cutLength;
+ int cutpoint1 = (int)(rnd.Next(geneLength - this.cutLength));
+ int cutpoint2 = cutpoint1 + this.cutLength;
// handle cut section
for (int i = 0; i < geneLength; i++)
{
if (!((i < cutpoint1) || (i > cutpoint2)))
{
- offspring1.GetGene(i).Copy(father.GetGene(i));
- offspring2.GetGene(i).Copy(mother.GetGene(i));
+ offspring1.Copy(father, i, i);
+ offspring2.Copy(mother, i, i);
}
}
@@ -83,12 +92,35 @@ public void Mate(Chromosome mother, Chromosome father,
{
if ((i < cutpoint1) || (i > cutpoint2))
{
- offspring1.GetGene(i).Copy(mother.GetGene(i));
- offspring2.GetGene(i).Copy(father.GetGene(i));
+ offspring1.Copy(mother, i, i);
+ offspring2.Copy(father, i, i);
}
}
}
- #endregion
+ ///
+ public int OffspringProduced
+ {
+ get
+ {
+ return 2;
+ }
+ }
+
+ ///
+ public int ParentsNeeded
+ {
+ get
+ {
+ return 2;
+ }
+ }
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ this.owner = theOwner;
+
+ }
}
}
diff --git a/encog-core-cs/ML/Genetic/Crossover/SpliceNoRepeat.cs b/encog-core-cs/ML/Genetic/Crossover/SpliceNoRepeat.cs
index c7da4fba..ae1965bb 100644
--- a/encog-core-cs/ML/Genetic/Crossover/SpliceNoRepeat.cs
+++ b/encog-core-cs/ML/Genetic/Crossover/SpliceNoRepeat.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,67 +20,103 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
+using System;
using System.Collections.Generic;
-using Encog.MathUtil;
-using Encog.ML.Genetic.Genes;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Train;
using Encog.ML.Genetic.Genome;
-using Enumerable = System.Linq.Enumerable;
+using Encog.ML.EA.Genome;
+using Encog.MathUtil.Randomize;
namespace Encog.ML.Genetic.Crossover
{
///
- /// A simple cross over where genes are simply "spliced".
- /// Genes are not allowed to repeat.
+ /// A simple cross over where genes are simply "spliced". Genes are not allowed
+ /// to repeat. This method only works with IntegerArrayGenome.
///
- public class SpliceNoRepeat : ICrossover
+ public class SpliceNoRepeat : IEvolutionaryOperator
{
///
- /// The cut length.
+ /// The owner.
///
- private readonly int _cutLength;
+ private IEvolutionaryAlgorithm owner;
///
- /// Construct a splice crossover.
+ /// Get a list of the genes that have not been taken before. This is useful
+ /// if you do not wish the same gene to appear more than once in a
+ /// genome.
///
- /// The cut length.
- public SpliceNoRepeat(int cutLength)
+ /// The pool of genes to select from.
+ /// An array of the taken genes.
+ /// Those genes in source that are not taken.
+ private static int GetNotTaken(IntegerArrayGenome source,
+ HashSet taken)
{
- _cutLength = cutLength;
- }
- #region ICrossover Members
+ foreach (int trial in source.Data)
+ {
+ if (!taken.Contains(trial))
+ {
+ taken.Add(trial);
+ return trial;
+ }
+ }
+
+ throw new GeneticError("Ran out of integers to select.");
+ }
///
- /// Assuming this chromosome is the "mother" mate with the passed in
- /// "father".
+ /// The cut length.
///
- /// The mother.
- /// The father.
- /// The first offspring.
- /// The second offspring.
- public void Mate(Chromosome mother, Chromosome father,
- Chromosome offspring1, Chromosome offspring2)
+ private int cutLength;
+
+ /**
+ * Construct a splice crossover.
+ *
+ * @param theCutLength
+ * The cut length.
+ */
+ public SpliceNoRepeat(int theCutLength)
{
- int geneLength = father.Genes.Count;
+ this.cutLength = theCutLength;
+ }
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
+ IGenome[] offspring, int offspringIndex)
+ {
+
+ IntegerArrayGenome mother = (IntegerArrayGenome)parents[parentIndex];
+ IntegerArrayGenome father = (IntegerArrayGenome)parents[parentIndex + 1];
+ IntegerArrayGenome offspring1 = (IntegerArrayGenome)this.owner.Population.GenomeFactory.Factor();
+ IntegerArrayGenome offspring2 = (IntegerArrayGenome)this.owner.Population.GenomeFactory.Factor();
+
+ offspring[offspringIndex] = offspring1;
+ offspring[offspringIndex + 1] = offspring2;
+
+ int geneLength = mother.Size;
// the chromosome must be cut at two positions, determine them
- var cutpoint1 = (int) (ThreadSafeRandom.NextDouble()*(geneLength - _cutLength));
- int cutpoint2 = cutpoint1 + _cutLength;
+ int cutpoint1 = (int)(rnd.Next(geneLength - this.cutLength));
+ int cutpoint2 = cutpoint1 + this.cutLength;
// keep track of which genes have been taken in each of the two
// offspring, defaults to false.
- IList taken1 = new List();
- IList taken2 = new List();
+ HashSet taken1 = new HashSet();
+ HashSet taken2 = new HashSet();
// handle cut section
for (int i = 0; i < geneLength; i++)
{
if (!((i < cutpoint1) || (i > cutpoint2)))
{
- offspring1.Genes[i].Copy(father.Genes[i]);
- offspring2.Genes[i].Copy(mother.Genes[i]);
- taken1.Add(offspring1.Genes[i]);
- taken2.Add(offspring2.Genes[i]);
+ offspring1.Copy(father, i, i);
+ offspring2.Copy(mother, i, i);
+ taken1.Add(father.Data[i]);
+ taken2.Add(mother.Data[i]);
}
}
@@ -89,42 +125,38 @@ public void Mate(Chromosome mother, Chromosome father,
{
if ((i < cutpoint1) || (i > cutpoint2))
{
- offspring1.Genes[i].Copy(
- GetNotTaken(mother, taken1));
- offspring2.Genes[i].Copy(
- GetNotTaken(father, taken2));
+
+ offspring1.Data[i] = SpliceNoRepeat.GetNotTaken(mother, taken1);
+ offspring2.Data[i] = SpliceNoRepeat.GetNotTaken(father, taken2);
+
}
}
}
- #endregion
-
///
- /// Get a list of the genes that have not been taken before. This is useful
- /// if you do not wish the same gene to appear more than once in a
- /// chromosome.
+ /// The number of offspring produced, which is 2 for splice crossover.
///
- /// The pool of genes to select from.
- /// An array of the taken genes.
- /// Those genes in source that are not taken.
- private static IGene GetNotTaken(Chromosome source,
- IList taken)
+ public int OffspringProduced
{
- int geneLength = source.Genes.Count;
-
- for (int i = 0; i < geneLength; i++)
+ get
{
- IGene trial = source.Genes[i];
+ return 2;
+ }
+ }
- bool found = Enumerable.Contains(taken, trial);
- if (!found)
- {
- taken.Add(trial);
- return trial;
- }
+ ///
+ public int ParentsNeeded
+ {
+ get
+ {
+ return 2;
}
+ }
- return null;
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ this.owner = theOwner;
}
}
}
diff --git a/encog-core-cs/ML/Genetic/Genes/BasicGene.cs b/encog-core-cs/ML/Genetic/Genes/BasicGene.cs
deleted file mode 100644
index 07a58955..00000000
--- a/encog-core-cs/ML/Genetic/Genes/BasicGene.cs
+++ /dev/null
@@ -1,106 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Genes
-{
- ///
- /// Implements the basic functionality for a gene. This is an abstract class.
- ///
- ///
- [Serializable]
- public abstract class BasicGene : IGene
- {
- ///
- /// Is this gene enabled?
- ///
- ///
- private bool _enabled;
-
- ///
- /// ID of this gene, -1 for unassigned.
- ///
- ///
- private long _id;
-
- ///
- /// Innovation ID, -1 for unassigned.
- ///
- ///
- private long _innovationId;
-
- ///
- /// Construct the object.
- ///
- protected BasicGene()
- {
- _enabled = true;
- _id = -1;
- _innovationId = -1;
- }
-
- #region IGene Members
-
- ///
- public int CompareTo(IGene o)
- {
- return ((int) (InnovationId - o.InnovationId));
- }
-
- ///
- /// Set the id for this gene.
- ///
- public long Id
- {
- get { return _id; }
- set { _id = value; }
- }
-
-
- ///
- /// Set the innovation id for this gene.
- ///
- public long InnovationId
- {
- get { return _innovationId; }
- set { _innovationId = value; }
- }
-
-
- /// True, if this gene is enabled.
- public bool Enabled
- {
- get { return _enabled; }
- set { _enabled = value; }
- }
-
-
- ///
- /// from Encog.ml.genetic.genes.Gene
- ///
- ///
- public abstract void Copy(IGene gene);
-
- #endregion
- }
-}
diff --git a/encog-core-cs/ML/Genetic/GeneticAlgorithm.cs b/encog-core-cs/ML/Genetic/GeneticAlgorithm.cs
deleted file mode 100644
index b52b9842..00000000
--- a/encog-core-cs/ML/Genetic/GeneticAlgorithm.cs
+++ /dev/null
@@ -1,146 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.ML.Genetic.Crossover;
-using Encog.ML.Genetic.Genome;
-using Encog.ML.Genetic.Mutate;
-using Encog.ML.Genetic.Population;
-using Encog.ML.Genetic.Species;
-using Encog.Util.Concurrency;
-
-namespace Encog.ML.Genetic
-{
- ///
- /// Implements a genetic algorithm. This is an abstract class. Other classes are
- /// provided by Encog use this base class to train neural networks or provide an
- /// answer to the traveling salesman problem.
- /// The genetic algorithm is also capable of using a thread pool to speed
- /// execution.
- ///
- ///
- public abstract class GeneticAlgorithm: IMultiThreadable
- {
- ///
- /// The score calculation object.
- ///
- ///
- private ICalculateGenomeScore _calculateScore;
-
- ///
- /// Set the score calculation object.
- ///
- public ICalculateGenomeScore CalculateScore
- {
- get { return _calculateScore; }
- set { _calculateScore = value; }
- }
-
- ///
- /// The thread count.
- ///
- public int ThreadCount { get; set; }
-
-
- ///
- /// Set the comparator.
- ///
- public GenomeComparator Comparator { get; set; }
-
-
- ///
- /// Set the crossover object.
- ///
- public ICrossover Crossover { get; set; }
-
-
- ///
- /// Set the mating population percent.
- ///
- public double MatingPopulation { get; set; }
-
-
- ///
- /// Set the mutate object.
- ///
- public IMutate Mutate { get; set; }
-
-
- ///
- /// Set the mutation percent.
- ///
- public double MutationPercent { get; set; }
-
-
- ///
- /// Set the percent to mate.
- ///
- public double PercentToMate { get; set; }
-
-
- ///
- /// Set the population.
- ///
- public IPopulation Population { get; set; }
-
- ///
- /// Add a genome.
- ///
- ///
- /// The species to add.
- /// The genome to add.
- public void AddSpeciesMember(ISpecies species,
- IGenome genome)
- {
- if (Comparator.IsBetterThan(genome.Score,
- species.BestScore))
- {
- species.BestScore = genome.Score;
- species.GensNoImprovement = 0;
- species.Leader = genome;
- }
-
- species.Members.Add(genome);
- }
-
- ///
- /// Calculate the score for this genome. The genome's score will be set.
- ///
- ///
- /// The genome to calculate for.
- public void PerformCalculateScore(IGenome g)
- {
- if (g.Organism is IMLContext)
- {
- ((IMLContext) g.Organism).ClearContext();
- }
- double score = _calculateScore.CalculateScore(g);
- g.Score = score;
- }
-
-
- ///
- /// Perform one training iteration.
- ///
- ///
- public abstract void Iteration();
- }
-}
diff --git a/encog-core-cs/ML/Genetic/GeneticError.cs b/encog-core-cs/ML/Genetic/GeneticError.cs
index 04c02f1c..5633a064 100644
--- a/encog-core-cs/ML/Genetic/GeneticError.cs
+++ b/encog-core-cs/ML/Genetic/GeneticError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,41 +21,43 @@
// http://www.heatonresearch.com/copyright
//
using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
namespace Encog.ML.Genetic
{
///
/// An error raised by the genetic algorithm.
///
- ///
- [Serializable]
public class GeneticError : EncogError
{
+
///
/// Construct a message exception.
///
- ///
- /// The exception message.
- public GeneticError(String msg) : base(msg)
+ /// The message.
+ public GeneticError(String str)
+ : base(str)
{
}
///
- /// Construct an exception that holds another exception.
+ /// Pass on an exception.
///
- ///
- /// A message.
- /// The other exception.
- public GeneticError(String msg, Exception t) : base(msg, t)
+ /// The other exception.
+ public GeneticError(Exception e)
+ : base("Nested Exception", e)
{
}
///
- /// Construct an exception that holds another exception.
+ /// Pass on an exception.
///
- ///
- /// The other exception.
- public GeneticError(Exception t) : base(t)
+ /// The message.
+ /// The exception.
+ public GeneticError(String msg, Exception e)
+ : base(msg, e)
{
}
}
diff --git a/encog-core-cs/ML/Genetic/Genome/BasicGenome.cs b/encog-core-cs/ML/Genetic/Genome/BasicGenome.cs
deleted file mode 100644
index 9723d1bd..00000000
--- a/encog-core-cs/ML/Genetic/Genome/BasicGenome.cs
+++ /dev/null
@@ -1,308 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Encog.ML.Genetic.Population;
-using Encog.MathUtil;
-
-namespace Encog.ML.Genetic.Genome
-{
- ///
- /// A basic abstract genome. Provides base functionality.
- ///
- ///
- [Serializable]
- public abstract class BasicGenome : IGenome
- {
- ///
- /// The genetic algorithm.
- ///
- [NonSerialized]
- private GeneticAlgorithm _ga;
-
- ///
- /// The chromosomes for this gene.
- ///
- ///
- private readonly IList _chromosomes;
-
- ///
- /// The adjusted score.
- ///
- ///
- private double _adjustedScore;
-
- ///
- /// The amount to spawn.
- ///
- ///
- private double _amountToSpawn;
-
- ///
- /// The genome id.
- ///
- ///
- private long _genomeID;
-
- ///
- /// The organism generated by this gene.
- ///
- ///
- [NonSerialized]
- private Object _organism;
-
- ///
- /// The population this genome belongs to.
- ///
- ///
- private IPopulation _population;
-
- ///
- /// The score of this genome.
- ///
- ///
- private double _score;
-
- ///
- /// Construct the bo
- ///
- protected BasicGenome()
- {
- _chromosomes = new List();
- _score = 0;
- }
-
- #region IGenome Members
-
- /// The number of genes in this genome.
- public int CalculateGeneCount()
- {
- // sum the genes in the chromosomes.
- return _chromosomes.Sum(chromosome => chromosome.Genes.Count);
- }
-
- ///
- public bool Equals(IGenome other)
- {
- if (other == this)
- {
- return true;
- }
-
- return Math.Abs(other.Score - Score) < EncogFramework.DefaultDoubleEqual;
- }
-
- ///
- public int CompareTo(IGenome other)
- {
- // might be null when deserializing
- if( _ga==null )
- {
- return 0;
- }
-
- if( Equals(other))
- {
- return 0;
- }
-
- // compare
- if ( _ga.CalculateScore.ShouldMinimize)
- {
- if (Math.Abs(Score - other.Score) < EncogFramework.DefaultDoubleEqual)
- {
- return 0;
- }
- if (Score > other.Score)
- {
- return 1;
- }
- return -1;
- }
- if (Math.Abs(Score - other.Score) < EncogFramework.DefaultDoubleEqual)
- {
- return 0;
- }
- if (Score > other.Score)
- {
- return -1;
- }
- return 1;
- }
-
- ///
- /// Set the adjusted score.
- ///
- ///
- /// The score.
- public double AdjustedScore
- {
- get { return _adjustedScore; }
- set { _adjustedScore = value; }
- }
-
-
- ///
- /// Set the amount to spawn.
- ///
- public double AmountToSpawn
- {
- get { return _amountToSpawn; }
- set { _amountToSpawn = value; }
- }
-
-
- /// The number of chromosomes.
- public IList Chromosomes
- {
- get { return _chromosomes; }
- }
-
-
- ///
- /// Set the genetic algorithm to use.
- ///
- public GeneticAlgorithm GA
- {
- get { return _ga; }
- set { _ga = value; }
- }
-
-
- ///
- /// Set the genome id.
- ///
- public long GenomeID
- {
- get { return _genomeID; }
- set { _genomeID = value; }
- }
-
-
- ///
- /// Set the organism.
- ///
- public Object Organism
- {
- get { return _organism; }
- set { _organism = value; }
- }
-
-
- /// the population to set
- public IPopulation Population
- {
- get { return _population; }
- set { _population = value; }
- }
-
-
- ///
- /// Set the score.
- ///
- public double Score
- {
- get { return _score; }
- set { _score = value; }
- }
-
-
- ///
- /// Mate two genomes. Will loop over all chromosomes.
- ///
- ///
- /// The father.
- /// The first child.
- /// The second child.
- public void Mate(IGenome father, IGenome child1,
- IGenome child2)
- {
- int motherChromosomes = Chromosomes.Count;
- int fatherChromosomes = father.Chromosomes.Count;
-
- if (motherChromosomes != fatherChromosomes)
- {
- throw new GeneticError(
- "Mother and father must have same chromosome count, Mother:"
- + motherChromosomes + ",Father:"
- + fatherChromosomes);
- }
-
- for (int i = 0; i < fatherChromosomes; i++)
- {
- Chromosome motherChromosome = _chromosomes[i];
- Chromosome fatherChromosome = father.Chromosomes[i];
- Chromosome offspring1Chromosome = child1.Chromosomes[i];
- Chromosome offspring2Chromosome = child2.Chromosomes[i];
-
- _ga.Crossover.Mate(motherChromosome,
- fatherChromosome, offspring1Chromosome,
- offspring2Chromosome);
-
- if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
- {
- _ga.Mutate.PerformMutation(
- offspring1Chromosome);
- }
-
- if (ThreadSafeRandom.NextDouble() < _ga.MutationPercent)
- {
- _ga.Mutate.PerformMutation(
- offspring2Chromosome);
- }
- }
-
- child1.Decode();
- child2.Decode();
- _ga.PerformCalculateScore(child1);
- _ga.PerformCalculateScore(child2);
- }
-
- ///
- /// from Encog.ml.genetic.genome.Genome
- ///
- ///
- public abstract void Decode();
-
- ///
- /// from Encog.ml.genetic.genome.Genome
- ///
- ///
- public abstract void Encode();
-
- #endregion
-
- ///
- public override sealed String ToString()
- {
- var builder = new StringBuilder();
- builder.Append("[");
- builder.Append(GetType().Name);
- builder.Append(": score=");
- builder.Append(Score);
- return builder.ToString();
- }
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Genome/Chromosome.cs b/encog-core-cs/ML/Genetic/Genome/Chromosome.cs
deleted file mode 100644
index 00d8eece..00000000
--- a/encog-core-cs/ML/Genetic/Genome/Chromosome.cs
+++ /dev/null
@@ -1,103 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-using Encog.ML.Genetic.Genes;
-
-namespace Encog.ML.Genetic.Genome
-{
- ///
- /// Implements a chromosome to genetic algorithm. This is an abstract class.
- /// Other classes are provided in this book that use this base class to train
- /// neural networks or provide an answer to the traveling salesman problem.
- /// Chromosomes are made up of genes.
- /// Genomes in this genetic algorithm consist of one or more chromosomes.
- ///
- ///
- [Serializable]
- public class Chromosome
- {
- ///
- /// The individual elements of this chromosome.
- ///
- ///
- private readonly List _genes;
-
- ///
- /// Construct the object.
- ///
- public Chromosome()
- {
- _genes = new List();
- }
-
- ///
- /// Used the get the entire gene list.
- ///
- ///
- /// the genes
- public List Genes
- {
- get { return _genes; }
- }
-
- ///
- /// Add a gene.
- ///
- ///
- /// The gene to add.
- public void Add(IGene gene)
- {
- _genes.Add(gene);
- }
-
- ///
- /// Get an individual gene.
- ///
- ///
- /// The index of the gene.
- /// The gene.
- public IGene Get(int i)
- {
- return _genes[i];
- }
-
- ///
- /// Get the specified gene.
- ///
- ///
- /// The specified gene.
- /// The gene specified.
- public IGene GetGene(int gene)
- {
- return _genes[gene];
- }
-
-
- /// The number of genes in this chromosome.
- public int Size()
- {
- return _genes.Count;
- }
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Genome/DoubleArrayGenome.cs b/encog-core-cs/ML/Genetic/Genome/DoubleArrayGenome.cs
new file mode 100644
index 00000000..e8060bbe
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/Genome/DoubleArrayGenome.cs
@@ -0,0 +1,107 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+using Encog.Util;
+
+namespace Encog.ML.Genetic.Genome
+{
+ ///
+ /// A genome made up of continuous doubles.
+ ///
+ public class DoubleArrayGenome : BasicGenome, IArrayGenome
+ {
+ ///
+ /// The data.
+ ///
+ private double[] data;
+
+ ///
+ /// Construct a genome of a specific size.
+ ///
+ /// The size.
+ public DoubleArrayGenome(int size)
+ {
+ this.data = new double[size];
+ }
+
+ ///
+ /// Construct a genome based on another genome.
+ ///
+ /// The other genome.
+ public DoubleArrayGenome(DoubleArrayGenome other)
+ {
+ this.data = (double[])other.Data.Clone();
+ }
+
+ ///
+ public override int Size
+ {
+ get
+ {
+ return this.data.Length;
+ }
+ }
+
+ ///
+ public void Copy(IArrayGenome source, int sourceIndex, int targetIndex)
+ {
+ DoubleArrayGenome sourceInt = (DoubleArrayGenome)source;
+ this.data[targetIndex] = sourceInt.data[sourceIndex];
+
+ }
+
+ ///
+ /// The data.
+ ///
+ public double[] Data
+ {
+ get
+ {
+ return this.data;
+ }
+ }
+
+ ///
+ public override void Copy(IGenome source)
+ {
+ DoubleArrayGenome sourceDouble = (DoubleArrayGenome)source;
+ EngineArray.ArrayCopy(sourceDouble.data, this.data);
+ Score = source.Score;
+ AdjustedScore = source.AdjustedScore;
+
+ }
+
+ ///
+ public void Swap(int iswap1, int iswap2)
+ {
+ double temp = this.data[iswap1];
+ this.data[iswap1] = this.data[iswap2];
+ this.data[iswap2] = temp;
+
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genes/IntegerGene.cs b/encog-core-cs/ML/Genetic/Genome/DoubleArrayGenomeFactory.cs
similarity index 50%
rename from encog-core-cs/ML/Genetic/Genes/IntegerGene.cs
rename to encog-core-cs/ML/Genetic/Genome/DoubleArrayGenomeFactory.cs
index 2b080e1b..1f584d3e 100644
--- a/encog-core-cs/ML/Genetic/Genes/IntegerGene.cs
+++ b/encog-core-cs/ML/Genetic/Genome/DoubleArrayGenomeFactory.cs
@@ -1,72 +1,63 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Genes
-{
- ///
- /// A gene that contains an integer value.
- ///
- ///
- public class IntegerGene : BasicGene
- {
- ///
- /// Set the value of this gene.
- ///
- public int Value { get; set; }
-
- ///
- /// Copy another gene to this one.
- ///
- public override sealed void Copy(IGene gene)
- {
- Value = ((IntegerGene) gene).Value;
- }
-
- ///
- public override sealed bool Equals(Object obj)
- {
- if (obj is IntegerGene)
- {
- return (((IntegerGene) obj).Value == Value);
- }
- else
- {
- return false;
- }
- }
-
-
- /// a hash code.
- public override sealed int GetHashCode()
- {
- return Value;
- }
-
- ///
- public override sealed String ToString()
- {
- return "" + Value;
- }
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.Genetic.Genome
+{
+ ///
+ /// A factory that creates DoubleArrayGenome objects of a specific size.
+ ///
+ public class DoubleArrayGenomeFactory : IGenomeFactory
+ {
+ ///
+ /// The size to create.
+ ///
+ private int size;
+
+ ///
+ /// Construct the genome factory.
+ ///
+ /// The size to create genomes of.
+ public DoubleArrayGenomeFactory(int theSize)
+ {
+ this.size = theSize;
+ }
+
+ ///
+ public IGenome Factor()
+ {
+ return new DoubleArrayGenome(this.size);
+ }
+
+ ///
+ public IGenome Factor(IGenome other)
+ {
+ // TODO Auto-generated method stub
+ return new DoubleArrayGenome((DoubleArrayGenome)other);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genome/GenomeComparator.cs b/encog-core-cs/ML/Genetic/Genome/GenomeComparator.cs
deleted file mode 100644
index c0f1d84a..00000000
--- a/encog-core-cs/ML/Genetic/Genome/GenomeComparator.cs
+++ /dev/null
@@ -1,132 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-
-namespace Encog.ML.Genetic.Genome
-{
- ///
- /// Used to compare two genomes, a score object is used.
- ///
- ///
- public class GenomeComparator : IComparer
- {
- ///
- /// The method to calculate the score.
- ///
- ///
- private readonly ICalculateGenomeScore _calculateScore;
-
- ///
- /// Construct the genome comparator.
- ///
- ///
- /// The score calculation object to use.
- public GenomeComparator(ICalculateGenomeScore theCalculateScore)
- {
- _calculateScore = theCalculateScore;
- }
-
- /// The score calculation object.
- public ICalculateGenomeScore CalculateScore
- {
- get { return _calculateScore; }
- }
-
- #region IComparer Members
-
- ///
- /// Compare two genomes.
- ///
- ///
- /// The first genome.
- /// The second genome.
- /// Zero if equal, or less than or greater than zero to indicate
- /// order.
- public int Compare(IGenome genome1, IGenome genome2)
- {
- return genome1.Score.CompareTo(genome2.Score);
- }
-
- #endregion
-
- ///
- /// Apply a bonus, this is a simple percent that is applied in the direction
- /// specified by the "should minimize" property of the score function.
- ///
- ///
- /// The current value.
- /// The bonus.
- /// The resulting value.
- public double ApplyBonus(double v, double bonus)
- {
- double amount = v*bonus;
- if (_calculateScore.ShouldMinimize)
- {
- return v - amount;
- }
- return v + amount;
- }
-
- ///
- /// Apply a penalty, this is a simple percent that is applied in the
- /// direction specified by the "should minimize" property of the score
- /// function.
- ///
- ///
- /// The current value.
- /// The penalty.
- /// The resulting value.
- public double ApplyPenalty(double v, double bonus)
- {
- double amount = v*bonus;
- return _calculateScore.ShouldMinimize ? v - amount : v + amount;
- }
-
- ///
- /// Determine the best score from two scores, uses the "should minimize"
- /// property of the score function.
- ///
- ///
- /// The first score.
- /// The second score.
- /// The best score.
- public double BestScore(double d1, double d2)
- {
- return _calculateScore.ShouldMinimize ? Math.Min(d1, d2) : Math.Max(d1, d2);
- }
-
-
- ///
- /// Determine if one score is better than the other.
- ///
- ///
- /// The first score to compare.
- /// The second score to compare.
- /// True if d1 is better than d2.
- public bool IsBetterThan(double d1, double d2)
- {
- return _calculateScore.ShouldMinimize ? d1 < d2 : d1 > d2;
- }
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Genome/IArrayGenome.cs b/encog-core-cs/ML/Genetic/Genome/IArrayGenome.cs
new file mode 100644
index 00000000..dae26028
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/Genome/IArrayGenome.cs
@@ -0,0 +1,52 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.Genetic.Genome
+{
+ ///
+ /// An array genome represents an array of "something", this allows array
+ /// operators such as crossover and mutate to work on the genome.
+ ///
+ public interface IArrayGenome : IGenome
+ {
+ ///
+ /// Copy elements from another array genome into this one.
+ ///
+ /// The source genome.
+ /// The source index.
+ /// The target index.
+ void Copy(IArrayGenome source, int sourceIndex, int targetIndex);
+
+ ///
+ /// Swap two elements in this genome.
+ ///
+ /// The first element index to swap.
+ /// The second element index to swap.
+ void Swap(int iswap1, int iswap2);
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genome/IGenome.cs b/encog-core-cs/ML/Genetic/Genome/IGenome.cs
deleted file mode 100644
index 855d0b19..00000000
--- a/encog-core-cs/ML/Genetic/Genome/IGenome.cs
+++ /dev/null
@@ -1,127 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-using Encog.ML.Genetic.Population;
-
-namespace Encog.ML.Genetic.Genome
-{
- ///
- /// A genome is the basic blueprint for creating an organism in Encog. A genome
- /// is made up of one or more chromosomes, which are in turn made up of genes.
- ///
- ///
- public interface IGenome : IComparable
- {
- ///
- /// Set the adjusted score.
- ///
- double AdjustedScore { get; set; }
-
-
- ///
- /// Set the amount to spawn.
- ///
- double AmountToSpawn { get; set; }
-
-
- /// The chromosomes that make up this genome.
- IList Chromosomes
- {
- get;
- }
-
-
- ///
- /// Set the GA used by this genome. This is normally a transient field and
- /// only used during training.
- ///
- GeneticAlgorithm GA
- {
- get;
- set;
- }
-
-
- ///
- /// Set the genome ID.
- ///
- long GenomeID
- {
- get;
- set;
- }
-
-
- /// The organism produced by this genome.
- Object Organism
- {
- get;
- }
-
-
- ///
- /// Set the population that this genome belongs to.
- ///
- IPopulation Population
- {
- get;
- set;
- }
-
-
- ///
- /// Set the score.
- ///
- double Score
- {
- get;
- set;
- }
-
- /// The number of genes in this genome.
- int CalculateGeneCount();
-
- ///
- /// Use the genes to update the organism.
- ///
- ///
- void Decode();
-
- ///
- /// Use the organism to update the genes.
- ///
- ///
- void Encode();
-
-
- ///
- /// Mate with another genome and produce two children.
- ///
- ///
- /// The father genome.
- /// The first child.
- /// The second child.
- void Mate(IGenome father, IGenome child1, IGenome child2);
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenome.cs b/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenome.cs
new file mode 100644
index 00000000..13998d5b
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenome.cs
@@ -0,0 +1,107 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+using Encog.Util;
+
+namespace Encog.ML.Genetic.Genome
+{
+ ///
+ /// A genome that is an array of discrete integer values.
+ ///
+ public class IntegerArrayGenome : BasicGenome, IArrayGenome
+ {
+ ///
+ /// The genome data.
+ ///
+ private int[] data;
+
+ ///
+ /// Construct the genome.
+ ///
+ /// The size of the genome.
+ public IntegerArrayGenome(int size)
+ {
+ this.data = new int[size];
+ }
+
+ ///
+ /// Construct the genome by copying another.
+ ///
+ /// The other genome.
+ public IntegerArrayGenome(IntegerArrayGenome other)
+ {
+ this.data = (int[])other.Data.Clone();
+ }
+
+ ///
+ public override int Size
+ {
+ get
+ {
+ return this.data.Length;
+ }
+ }
+
+ ///
+ public void Copy(IArrayGenome source, int sourceIndex, int targetIndex)
+ {
+ IntegerArrayGenome sourceInt = (IntegerArrayGenome)source;
+ this.data[targetIndex] = sourceInt.data[sourceIndex];
+
+ }
+
+ ///
+ /// The data.
+ ///
+ public int[] Data
+ {
+ get
+ {
+ return this.data;
+ }
+ }
+
+ ///
+ public override void Copy(IGenome source)
+ {
+ IntegerArrayGenome sourceInt = (IntegerArrayGenome)source;
+ EngineArray.ArrayCopy(sourceInt.data, this.data);
+ this.Score = source.Score;
+ this.AdjustedScore = source.AdjustedScore;
+
+ }
+
+ ///
+ public void Swap(int iswap1, int iswap2)
+ {
+ int temp = this.data[iswap1];
+ this.data[iswap1] = this.data[iswap2];
+ this.data[iswap2] = temp;
+
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenomeFactory.cs b/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenomeFactory.cs
new file mode 100644
index 00000000..cdd29005
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/Genome/IntegerArrayGenomeFactory.cs
@@ -0,0 +1,62 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.Genetic.Genome
+{
+ ///
+ /// A factory to create integer genomes of a specific size.
+ ///
+ public class IntegerArrayGenomeFactory : IGenomeFactory
+ {
+ ///
+ /// The size of genome to create.
+ ///
+ private int size;
+
+ ///
+ /// Create the integer genome of a fixed size.
+ ///
+ /// The size to use.
+ public IntegerArrayGenomeFactory(int theSize)
+ {
+ this.size = theSize;
+ }
+
+ ///
+ public IGenome Factor()
+ {
+ return new IntegerArrayGenome(this.size);
+ }
+
+ ///
+ public IGenome Factor(IGenome other)
+ {
+ return new IntegerArrayGenome(((IntegerArrayGenome)other));
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Innovation/BasicInnovationList.cs b/encog-core-cs/ML/Genetic/Innovation/BasicInnovationList.cs
deleted file mode 100644
index cf015a6f..00000000
--- a/encog-core-cs/ML/Genetic/Innovation/BasicInnovationList.cs
+++ /dev/null
@@ -1,81 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-
-namespace Encog.ML.Genetic.Innovation
-{
- ///
- /// Provides basic functionality for a list of innovations.
- ///
- ///
- [Serializable]
- public class BasicInnovationList : IInnovationList
- {
- ///
- /// The list of innovations.
- ///
- ///
- private readonly IList _list;
-
- ///
- /// Construct the object.
- ///
- public BasicInnovationList()
- {
- _list = new List();
- }
-
- #region IInnovationList Members
-
- ///
- /// Add an innovation.
- ///
- ///
- /// The innovation to add.
- public void Add(IInnovation innovation)
- {
- _list.Add(innovation);
- }
-
- ///
- /// Get a specific innovation, by index.
- ///
- ///
- /// The innovation index id.
- /// The innovation.
- public IInnovation Get(int id)
- {
- return _list[id];
- }
-
-
- /// A list of innovations.
- public IList Innovations
- {
- get { return _list; }
- }
-
- #endregion
- }
-}
diff --git a/encog-core-cs/ML/Genetic/MLEncodableCODEC.cs b/encog-core-cs/ML/Genetic/MLEncodableCODEC.cs
new file mode 100644
index 00000000..338f8cd4
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/MLEncodableCODEC.cs
@@ -0,0 +1,54 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Codec;
+
+namespace Encog.ML.Genetic
+{
+ ///
+ /// A CODEC for IMLEncodable classes.
+ ///
+ [Serializable]
+ public class MLEncodableCODEC: IGeneticCODEC
+ {
+ ///
+ public IMLMethod Decode(IGenome genome)
+ {
+ MLMethodGenome genome2 = (MLMethodGenome)genome;
+ genome2.Decode();
+ return genome2.Phenotype;
+ }
+
+ ///
+ public IGenome Encode(IMLMethod phenotype)
+ {
+ IMLEncodable phenotype2 = (IMLEncodable)phenotype;
+ return new MLMethodGenome(phenotype2);
+ }
+
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/MLMethodGeneticAlgorithm.cs b/encog-core-cs/ML/Genetic/MLMethodGeneticAlgorithm.cs
new file mode 100644
index 00000000..7085f15c
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/MLMethodGeneticAlgorithm.cs
@@ -0,0 +1,207 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Train;
+using Encog.Util.Concurrency;
+using Encog.ML.EA.Train;
+using Encog.ML.EA.Population;
+using Encog.Neural.Networks.Training;
+using Encog.ML.EA.Sort;
+using Encog.ML.EA.Species;
+using Encog.ML.Genetic.Mutate;
+using Encog.ML.Genetic.Crossover;
+using Encog.ML.EA.Genome;
+using Encog.Util.Logging;
+using Encog.Neural.Networks.Training.Propagation;
+
+namespace Encog.ML.Genetic
+{
+ ///
+ /// Implements a genetic algorithm that allows an MLMethod that is encodable
+ /// (MLEncodable) to be trained. It works well with both BasicNetwork and
+ /// FreeformNetwork class, as well as any MLEncodable class.
+ ///
+ /// There are essentially two ways you can make use of this class.
+ ///
+ /// Either way, you will need a score object. The score object tells the genetic
+ /// algorithm how well suited a neural network is.
+ ///
+ /// If you would like to use genetic algorithms with a training set you should
+ /// make use TrainingSetScore class. This score object uses a training set to
+ /// score your neural network.
+ ///
+ /// If you would like to be more abstract, and not use a training set, you can
+ /// create your own implementation of the CalculateScore method. This class can
+ /// then score the networks any way that you like.
+ ///
+ public class MLMethodGeneticAlgorithm : BasicTraining, IMultiThreadable
+ {
+ ///
+ /// Very simple class that implements a genetic algorithm.
+ ///
+ public class MLMethodGeneticAlgorithmHelper : TrainEA
+ {
+
+ ///
+ /// Construct the helper.
+ ///
+ /// The population.
+ /// The score function.
+ public MLMethodGeneticAlgorithmHelper(IPopulation thePopulation,
+ ICalculateScore theScoreFunction)
+ : base(thePopulation, theScoreFunction)
+ {
+
+ }
+ }
+
+ ///
+ /// Simple helper class that implements the required methods to implement a
+ /// genetic algorithm.
+ ///
+ private MLMethodGeneticAlgorithmHelper genetic;
+
+ ///
+ /// Construct a method genetic algorithm.
+ ///
+ /// The phenotype factory.
+ /// The score calculation object.
+ /// The population size.
+ public MLMethodGeneticAlgorithm(MLMethodGenomeFactory.CreateMethod phenotypeFactory,
+ ICalculateScore calculateScore, int populationSize)
+ : base(TrainingImplementationType.Iterative)
+ {
+ // create the population
+ IPopulation population = new BasicPopulation(populationSize, null);
+ population.GenomeFactory = new MLMethodGenomeFactory(phenotypeFactory,
+ population);
+
+ ISpecies defaultSpecies = population.CreateSpecies();
+
+ for (int i = 0; i < population.PopulationSize; i++)
+ {
+ IMLEncodable chromosomeNetwork = (IMLEncodable)phenotypeFactory();
+ MLMethodGenome genome = new MLMethodGenome(chromosomeNetwork);
+ defaultSpecies.Add(genome);
+ }
+ defaultSpecies.Leader = defaultSpecies.Members[0];
+
+
+
+ // create the trainer
+ this.genetic = new MLMethodGeneticAlgorithmHelper(population,
+ calculateScore);
+ this.genetic.CODEC = new MLEncodableCODEC();
+
+ IGenomeComparer comp = null;
+ if (calculateScore.ShouldMinimize)
+ {
+ comp = new MinimizeScoreComp();
+ }
+ else
+ {
+ comp = new MaximizeScoreComp();
+ }
+ this.genetic.BestComparer = comp;
+ this.genetic.SelectionComparer = comp;
+
+ // create the operators
+ int s = Math
+ .Max(defaultSpecies.Members[0].Size / 5, 1);
+ Genetic.Population = population;
+
+ this.genetic.AddOperation(0.9, new Splice(s));
+ this.genetic.AddOperation(0.1, new MutatePerturb(1.0));
+ }
+
+ ///
+ public override bool CanContinue
+ {
+ get
+ {
+ return false;
+ }
+ }
+
+ ///
+ /// The genetic algorithm implementation.
+ ///
+ public MLMethodGeneticAlgorithmHelper Genetic
+ {
+ get
+ {
+ return this.genetic;
+ }
+ }
+
+ ///
+ public override IMLMethod Method
+ {
+ get
+ {
+ IGenome best = this.genetic.BestGenome;
+ return this.genetic.CODEC.Decode(best);
+ }
+ }
+ ///
+ public int ThreadCount
+ {
+ get
+ {
+ return this.genetic.ThreadCount;
+ }
+ set
+ {
+ this.genetic.ThreadCount = value;
+ }
+ }
+
+ ///
+ public override void Iteration()
+ {
+
+ EncogLogging.Log(EncogLogging.LevelInfo,
+ "Performing Genetic iteration.");
+ PreIteration();
+ Error = Genetic.Error;
+ Genetic.Iteration();
+ Error = Genetic.Error;
+ PostIteration();
+ }
+
+ ///
+ public override TrainingContinuation Pause()
+ {
+ return null;
+ }
+
+ ///
+ public override void Resume(TrainingContinuation state)
+ {
+
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/MLMethodGenome.cs b/encog-core-cs/ML/Genetic/MLMethodGenome.cs
new file mode 100644
index 00000000..9d9b4c4a
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/MLMethodGenome.cs
@@ -0,0 +1,64 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Genetic.Genome;
+
+namespace Encog.ML.Genetic
+{
+ ///
+ /// Implements a genome that allows a feedforward neural network to be trained
+ /// using a genetic algorithm. The chromosome for a feed forward neural network
+ /// is the weight and bias matrix.
+ ///
+ public class MLMethodGenome : DoubleArrayGenome
+ {
+ ///
+ /// The phenome.
+ ///
+ public IMLEncodable Phenotype { get; set; }
+
+ ///
+ /// Construct a neural genome.
+ ///
+ /// The phenotype to use.
+ public MLMethodGenome(IMLEncodable thePhenotype)
+ : base(thePhenotype.EncodedArrayLength())
+ {
+ Phenotype = thePhenotype;
+ Phenotype.EncodeToArray(Data);
+ }
+
+ ///
+ /// Decode the phenotype.
+ ///
+ public void Decode()
+ {
+ Phenotype.DecodeFromArray(Data);
+ }
+
+
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/MLMethodGenomeFactory.cs b/encog-core-cs/ML/Genetic/MLMethodGenomeFactory.cs
new file mode 100644
index 00000000..b61e0b42
--- /dev/null
+++ b/encog-core-cs/ML/Genetic/MLMethodGenomeFactory.cs
@@ -0,0 +1,79 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+
+namespace Encog.ML.Genetic
+{
+ ///
+ /// A factory to create MLMethod based genomes.
+ ///
+ public class MLMethodGenomeFactory : IGenomeFactory
+ {
+ public delegate IMLMethod CreateMethod();
+
+ ///
+ /// The MLMethod factory.
+ ///
+ private CreateMethod factory;
+
+ ///
+ /// The population.
+ ///
+ private IPopulation population;
+
+ ///
+ /// Construct the genome factory.
+ ///
+ /// The factory.
+ /// The population.
+ public MLMethodGenomeFactory(CreateMethod theFactory,
+ IPopulation thePopulation)
+ {
+ this.factory = theFactory;
+ this.population = thePopulation;
+ }
+
+ ///
+ public IGenome Factor()
+ {
+ IGenome result = new MLMethodGenome(
+ (IMLEncodable)this.factory());
+ result.Population = this.population;
+ return result;
+ }
+
+ ///
+ public IGenome Factor(IGenome other)
+ {
+ MLMethodGenome result = (MLMethodGenome)Factor();
+ result.Copy(other);
+ result.Population = this.population;
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/MateWorker.cs b/encog-core-cs/ML/Genetic/MateWorker.cs
deleted file mode 100644
index ad3ce6fe..00000000
--- a/encog-core-cs/ML/Genetic/MateWorker.cs
+++ /dev/null
@@ -1,86 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.ML.Genetic.Genome;
-using Encog.Util.Concurrency;
-
-namespace Encog.ML.Genetic
-{
- ///
- /// This class is used in conjunction with a thread pool. This allows the genetic
- /// algorithm to offload all of those calculations to a thread pool.
- ///
- ///
- public class MateWorker : IEngineTask
- {
- ///
- /// The first child.
- ///
- ///
- private readonly IGenome _child1;
-
- ///
- /// The second child.
- ///
- ///
- private readonly IGenome _child2;
-
- ///
- /// The father.
- ///
- ///
- private readonly IGenome _father;
-
- ///
- /// The mother.
- ///
- ///
- private readonly IGenome _mother;
-
-
- /// The mother.
- /// The father.
- /// The first child.
- /// The second child.
- public MateWorker(IGenome theMother, IGenome theFather,
- IGenome theChild1, IGenome theChild2)
- {
- _mother = theMother;
- _father = theFather;
- _child1 = theChild1;
- _child2 = theChild2;
- }
-
- #region IEngineTask Members
-
- ///
- /// Mate the two chromosomes.
- ///
- ///
- public void Run()
- {
- _mother.Mate(_father, _child1, _child2);
- }
-
- #endregion
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Mutate/MutatePerturb.cs b/encog-core-cs/ML/Genetic/Mutate/MutatePerturb.cs
index 910e843a..fd1f6226 100644
--- a/encog-core-cs/ML/Genetic/Mutate/MutatePerturb.cs
+++ b/encog-core-cs/ML/Genetic/Mutate/MutatePerturb.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,55 +20,78 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
-using Encog.ML.Genetic.Genes;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Train;
using Encog.ML.Genetic.Genome;
-using Encog.MathUtil;
+using Encog.ML.EA.Genome;
+using Encog.MathUtil.Randomize;
namespace Encog.ML.Genetic.Mutate
{
///
/// A simple mutation based on random numbers.
///
- ///
- public class MutatePerturb : IMutate
+ public class MutatePerturb : IEvolutionaryOperator
{
///
/// The amount to perturb by.
///
- ///
- private readonly double _perturbAmount;
+ private double perturbAmount;
///
/// Construct a perturb mutation.
///
- ///
/// The amount to mutate by(percent).
public MutatePerturb(double thePerturbAmount)
{
- _perturbAmount = thePerturbAmount;
+ this.perturbAmount = thePerturbAmount;
}
- #region IMutate Members
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
+ IGenome[] offspring, int offspringIndex)
+ {
+ DoubleArrayGenome parent = (DoubleArrayGenome)parents[parentIndex];
+ offspring[offspringIndex] = parent.Population.GenomeFactory.Factor();
+ DoubleArrayGenome child = (DoubleArrayGenome)offspring[offspringIndex];
+
+ for (int i = 0; i < parent.Size; i++)
+ {
+ double value = parent.Data[i];
+ value += (perturbAmount - (rnd.NextDouble() * perturbAmount * 2));
+ child.Data[i] = value;
+ }
+ }
///
- /// Perform a perturb mutation on the specified chromosome.
+ /// The number of offspring produced, which is 1 for this mutation.
///
- ///
- /// The chromosome to mutate.
- public void PerformMutation(Chromosome chromosome)
+ public int OffspringProduced
+ {
+ get
+ {
+ return 1;
+ }
+ }
+
+ ///
+ public int ParentsNeeded
{
- foreach (IGene gene in chromosome.Genes)
+ get
{
- if (gene is DoubleGene)
- {
- var doubleGene = (DoubleGene) gene;
- double v = doubleGene.Value;
- v += (_perturbAmount - (ThreadSafeRandom.NextDouble()*_perturbAmount*2));
- doubleGene.Value = v;
- }
+ return 1;
}
}
- #endregion
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ // not needed
+ }
}
}
diff --git a/encog-core-cs/ML/Genetic/Mutate/MutateShuffle.cs b/encog-core-cs/ML/Genetic/Mutate/MutateShuffle.cs
index 95dcf828..dcd7c32a 100644
--- a/encog-core-cs/ML/Genetic/Mutate/MutateShuffle.cs
+++ b/encog-core-cs/ML/Genetic/Mutate/MutateShuffle.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,9 +20,16 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
-using Encog.ML.Genetic.Genes;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Opp.Selection;
+using Encog.ML.EA.Train;
+using Encog.ML.EA.Genome;
using Encog.ML.Genetic.Genome;
-using Encog.MathUtil;
+using Encog.MathUtil.Randomize;
namespace Encog.ML.Genetic.Mutate
{
@@ -30,21 +37,26 @@ namespace Encog.ML.Genetic.Mutate
/// A simple mutation where genes are shuffled.
/// This mutation will not produce repeated genes.
///
- ///
- public class MutateShuffle : IMutate
+ public class MutateShuffle : IEvolutionaryOperator
{
- #region IMutate Members
-
///
- /// Perform a shuffle mutation.
+ /// The owner.
///
- ///
- /// The chromosome to mutate.
- public void PerformMutation(Chromosome chromosome)
+ private IEvolutionaryAlgorithm owner;
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents, int parentIndex,
+ IGenome[] offspring, int offspringIndex)
{
- int length = chromosome.Genes.Count;
- var iswap1 = (int)(ThreadSafeRandom.NextDouble()*length);
- var iswap2 = (int)(ThreadSafeRandom.NextDouble() * length);
+ IArrayGenome parent = (IArrayGenome)parents[parentIndex];
+ offspring[offspringIndex] = this.owner.Population.GenomeFactory.Factor();
+ IArrayGenome child = (IArrayGenome)offspring[offspringIndex];
+
+ child.Copy(parent);
+
+ int length = parent.Size;
+ int iswap1 = (int)(rnd.NextDouble() * length);
+ int iswap2 = (int)(rnd.NextDouble() * length);
// can't be equal
if (iswap1 == iswap2)
@@ -59,6 +71,7 @@ public void PerformMutation(Chromosome chromosome)
{
iswap1++;
}
+
}
// make sure they are in the right order
@@ -69,18 +82,33 @@ public void PerformMutation(Chromosome chromosome)
iswap2 = temp;
}
- IGene gene1 = chromosome.Genes[iswap1];
- IGene gene2 = chromosome.Genes[iswap2];
+ child.Swap(iswap1, iswap2);
+ }
- // remove the two genes
- chromosome.Genes.Remove(gene1);
- chromosome.Genes.Remove(gene2);
+ ///
+ /// The number of offspring produced, which is 1 for this mutation.
+ ///
+ public int OffspringProduced
+ {
+ get
+ {
+ return 1;
+ }
+ }
- // insert them back in, reverse order
- chromosome.Genes.Insert(iswap1, gene2);
- chromosome.Genes.Insert(iswap2, gene1);
+ ///
+ public int ParentsNeeded
+ {
+ get
+ {
+ return 1;
+ }
}
- #endregion
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ this.owner = theOwner;
+ }
}
}
diff --git a/encog-core-cs/ML/Genetic/Population/BasicPopulation.cs b/encog-core-cs/ML/Genetic/Population/BasicPopulation.cs
deleted file mode 100644
index e4ddbece..00000000
--- a/encog-core-cs/ML/Genetic/Population/BasicPopulation.cs
+++ /dev/null
@@ -1,300 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-using Encog.ML.Genetic.Genome;
-using Encog.ML.Genetic.Innovation;
-using Encog.ML.Genetic.Species;
-using Encog.Util.Identity;
-
-namespace Encog.ML.Genetic.Population
-{
- ///
- /// Defines the basic functionality for a population of genomes.
- ///
- [Serializable]
- public class BasicPopulation : IPopulation
- {
- ///
- /// Thed default old age penalty.
- ///
- ///
- public const double DefaultOldAgePenalty = 0.3d;
-
- ///
- /// The default old age threshold.
- ///
- ///
- public const int DefaultOldAgeThreshold = 50;
-
- ///
- /// The default survival rate.
- ///
- ///
- public const double DefaultSurvivalRate = 0.2d;
-
- ///
- /// The default youth penalty.
- ///
- ///
- public const double DefaultYouthBonus = 0.3d;
-
- ///
- /// The default youth threshold.
- ///
- ///
- public const int DefaultYouthThreshold = 10;
-
- ///
- /// Generate gene id's.
- ///
- ///
- private readonly IGenerateID _geneIDGenerate;
-
- ///
- /// Generate genome id's.
- ///
- ///
- private readonly IGenerateID _genomeIDGenerate;
-
- ///
- /// The population.
- ///
- ///
- private readonly List _genomes;
-
- ///
- /// Generate innovation id's.
- ///
- ///
- private readonly IGenerateID _innovationIDGenerate;
-
- ///
- /// Generate species id's.
- ///
- ///
- private readonly IGenerateID _speciesIDGenerate;
-
- ///
- /// The young threshold.
- ///
- ///
- private int _youngBonusAgeThreshold;
-
- ///
- /// Construct an empty population.
- ///
- ///
- public BasicPopulation()
- {
- _geneIDGenerate = new BasicGenerateID();
- _genomeIDGenerate = new BasicGenerateID();
- _genomes = new List();
- _innovationIDGenerate = new BasicGenerateID();
- OldAgePenalty = DefaultOldAgePenalty;
- OldAgeThreshold = DefaultOldAgeThreshold;
- Species = new List();
- _speciesIDGenerate = new BasicGenerateID();
- SurvivalRate = DefaultSurvivalRate;
- _youngBonusAgeThreshold = DefaultYouthThreshold;
- YoungScoreBonus = DefaultYouthBonus;
- PopulationSize = 0;
- }
-
- ///
- /// Construct a population.
- ///
- /// The population size.
- public BasicPopulation(int thePopulationSize)
- {
- _geneIDGenerate = new BasicGenerateID();
- _genomeIDGenerate = new BasicGenerateID();
- _genomes = new List();
- _innovationIDGenerate = new BasicGenerateID();
- OldAgePenalty = DefaultOldAgePenalty;
- OldAgeThreshold = DefaultOldAgeThreshold;
- Species = new List();
- _speciesIDGenerate = new BasicGenerateID();
- SurvivalRate = DefaultSurvivalRate;
- _youngBonusAgeThreshold = DefaultYouthThreshold;
- YoungScoreBonus = DefaultYouthBonus;
- PopulationSize = thePopulationSize;
- }
-
- /// the geneIDGenerate
- public IGenerateID GeneIDGenerate
- {
- get { return _geneIDGenerate; }
- }
-
-
- /// the genomeIDGenerate
- public IGenerateID GenomeIDGenerate
- {
- get { return _genomeIDGenerate; }
- }
-
- /// the innovationIDGenerate
- public IGenerateID InnovationIDGenerate
- {
- get { return _innovationIDGenerate; }
- }
-
- ///
- /// Set the name.
- ///
- public String Name { get; set; }
-
- /// the speciesIDGenerate
- public IGenerateID SpeciesIDGenerate
- {
- get { return _speciesIDGenerate; }
- }
-
- #region IPopulation Members
-
-
- ///
- public void Add(IGenome genome)
- {
- _genomes.Add(genome);
- genome.Population = this;
- }
-
- ///
- public long AssignGeneID()
- {
- return _geneIDGenerate.Generate();
- }
-
- ///
- public long AssignGenomeID()
- {
- return _genomeIDGenerate.Generate();
- }
-
- ///
- public long AssignInnovationID()
- {
- return _innovationIDGenerate.Generate();
- }
-
- ///
- public long AssignSpeciesID()
- {
- return _speciesIDGenerate.Generate();
- }
-
- ///
- public void Claim(GeneticAlgorithm ga)
- {
- foreach (IGenome genome in _genomes)
- {
- genome.GA = ga;
- }
- }
-
- ///
- public void Clear()
- {
- _genomes.Clear();
- }
-
- ///
- public IGenome Get(int i)
- {
- return _genomes[i];
- }
-
- ///
- public IGenome Best
- {
- get { return _genomes.Count == 0 ? null : _genomes[0]; }
- }
-
-
- ///
- public IList Genomes
- {
- get { return _genomes; }
- }
-
-
- ///
- public IInnovationList Innovations { get; set; }
-
-
- ///
- public double OldAgePenalty { get; set; }
-
-
- ///
- public int OldAgeThreshold { get; set; }
-
-
- ///
- public int PopulationSize { get; set; }
-
-
- ///
- public IList Species { get; set; }
-
-
- ///
- public double SurvivalRate { get; set; }
-
-
- /// the youngBonusAgeThreshold to set
- public int YoungBonusAgeThreshold
- {
- get { return _youngBonusAgeThreshold; }
- set { _youngBonusAgeThreshold = value; }
- }
-
-
- ///
- public double YoungScoreBonus { get; set; }
-
-
- ///
- public int YoungBonusAgeThreshhold
- {
- set { _youngBonusAgeThreshold = value; }
- }
-
-
- ///
- public int Size()
- {
- return _genomes.Count;
- }
-
- ///
- public void Sort()
- {
- _genomes.Sort();
- }
-
- #endregion
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Population/IPopulation.cs b/encog-core-cs/ML/Genetic/Population/IPopulation.cs
deleted file mode 100644
index a4335558..00000000
--- a/encog-core-cs/ML/Genetic/Population/IPopulation.cs
+++ /dev/null
@@ -1,150 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System.Collections.Generic;
-using Encog.ML.Genetic.Genome;
-using Encog.ML.Genetic.Innovation;
-using Encog.ML.Genetic.Species;
-
-namespace Encog.ML.Genetic.Population
-{
- ///
- /// Defines a population of genomes.
- ///
- ///
- public interface IPopulation
- {
- /// The best genome in the population.
- IGenome Best { get; }
-
-
- /// The genomes in the population.
- IList Genomes { get; }
-
-
- ///
- /// Set the innovations collection.
- ///
- IInnovationList Innovations { get; set; }
-
-
- ///
- /// Set the old age penalty.
- ///
- double OldAgePenalty { get; set; }
-
-
- ///
- /// Set the age at which a genome is considered "old".
- ///
- int OldAgeThreshold { get; set; }
-
-
- ///
- /// Set the max population size.
- ///
- int PopulationSize { get; set; }
-
-
- /// A list of species.
- IList Species { get; }
-
-
- ///
- /// Set the survival rate.
- ///
- ///
- /// The survival rate.
- double SurvivalRate { get; set; }
-
-
- /// The age, below which, a genome is considered "young".
- int YoungBonusAgeThreshold { get; }
-
-
- ///
- /// Set the youth score bonus.
- ///
- double YoungScoreBonus { get; set; }
-
-
- ///
- /// Set the age at which genoms are considered young.
- ///
- ///
- /// The age.
- int YoungBonusAgeThreshhold { set; }
-
- ///
- /// Add a genome to the population.
- ///
- ///
- /// The genome to add.
- void Add(IGenome genome);
-
- /// Assign a gene id.
- long AssignGeneID();
-
-
- /// Assign a genome id.
- long AssignGenomeID();
-
-
- /// Assign an innovation id.
- long AssignInnovationID();
-
-
- /// Assign a species id.
- long AssignSpeciesID();
-
- ///
- /// Clear all genomes from this population.
- ///
- ///
- void Clear();
-
- ///
- /// Get a genome by index. Index 0 is the best genome.
- ///
- ///
- /// The genome to get.
- /// The genome at the specified index.
- IGenome Get(int i);
-
-
- /// The size of the population.
- int Size();
-
- ///
- /// Sort the population by best score.
- ///
- ///
- void Sort();
-
- ///
- /// Claim the population, before training.
- ///
- ///
- /// The GA that is claiming.
- void Claim(GeneticAlgorithm ga);
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Population/PopulationConst.cs b/encog-core-cs/ML/Genetic/Population/PopulationConst.cs
deleted file mode 100644
index be1e4a66..00000000
--- a/encog-core-cs/ML/Genetic/Population/PopulationConst.cs
+++ /dev/null
@@ -1,110 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-
-namespace Encog.ML.Genetic.Population
-{
- ///
- /// Constants for the population.
- ///
- public class PopulationConst
- {
- ///
- /// Property tag for the next gene id.
- ///
- ///
- public const String PropertyNextGeneID = "nextGeneID";
-
- ///
- /// Property tag for the next genome id.
- ///
- ///
- public const String PropertyNextGenomeID = "nextGenomeID";
-
- ///
- /// Property tag for the next innovation id.
- ///
- ///
- public const String PropertyNextInnovationID = "nextInnovationID";
-
- ///
- /// Property tag for the next species id.
- ///
- ///
- public const String PropertyNextSpeciesID = "nextSpeciesID";
-
- ///
- /// Property tag for the old age penalty.
- ///
- ///
- public const String PropertyOldAgePenalty = "oldAgePenalty";
-
- ///
- /// Property tag for the old age threshold.
- ///
- ///
- public const String PropertyOldAgeThreshold = "oldAgeThreshold";
-
- ///
- /// Property tag for the population size.
- ///
- ///
- public const String PropertyPopulationSize = "populationSize";
-
- ///
- /// Property tag for the survival rate.
- ///
- ///
- public const String PropertySurvivalRate = "survivalRate";
-
- ///
- /// Property tag for the young age bonus.
- ///
- ///
- public const String PropertyYoungAgeBonus = "youngAgeBonus";
-
- ///
- /// Property tag for the young age threshold.
- ///
- ///
- public const String PropertyYoungAgeThreshold = "youngAgeThreshold";
-
- ///
- /// Property tag for the genomes collection.
- ///
- ///
- public const String PropertyGenomes = "genomes";
-
- ///
- /// Property tag for the innovations collection.
- ///
- ///
- public const String PropertyInnovations = "innovations";
-
- ///
- /// Property tag for the species collection.
- ///
- ///
- public const String PropertySpecies = "species";
- }
-}
diff --git a/encog-core-cs/ML/Genetic/Species/BasicSpecies.cs b/encog-core-cs/ML/Genetic/Species/BasicSpecies.cs
deleted file mode 100644
index dd02fede..00000000
--- a/encog-core-cs/ML/Genetic/Species/BasicSpecies.cs
+++ /dev/null
@@ -1,281 +0,0 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-using System.Collections.Generic;
-using Encog.MathUtil.Randomize;
-using Encog.ML.Genetic.Genome;
-using Encog.ML.Genetic.Population;
-
-namespace Encog.ML.Genetic.Species
-{
- ///
- /// Provides basic functionality for a species.
- ///
- ///
- [Serializable]
- public class BasicSpecies : ISpecies
- {
- ///
- /// The list of genomes.
- ///
- ///
- private readonly IList _members;
-
- ///
- /// The age of this species.
- ///
- ///
- private int _age;
-
- ///
- /// The best score.
- ///
- ///
- private double _bestScore;
-
- ///
- /// The number of generations with no improvement.
- ///
- ///
- private int _gensNoImprovement;
-
- ///
- /// The leader.
- ///
- ///
- private IGenome _leader;
-
- ///
- /// The id of the leader.
- ///
- [NonSerialized]
- private long _leaderID;
-
- ///
- /// The owner class.
- ///
- ///
- private IPopulation _population;
-
- ///
- /// The number of spawns required.
- ///
- ///
- private double _spawnsRequired;
-
- ///
- /// The species id.
- ///
- ///
- private long _speciesID;
-
- ///
- /// Default constructor, used mainly for persistence.
- ///
- ///
- public BasicSpecies()
- {
- _members = new List();
- }
-
- ///
- /// Construct a species.
- ///
- ///
- /// The population the species belongs to.
- /// The first genome in the species.
- /// The species id.
- public BasicSpecies(IPopulation thePopulation, IGenome theFirst,
- long theSpeciesID)
- {
- _members = new List();
- _population = thePopulation;
- _speciesID = theSpeciesID;
- _bestScore = theFirst.Score;
- _gensNoImprovement = 0;
- _age = 0;
- _leader = theFirst;
- _spawnsRequired = 0;
- _members.Add(theFirst);
- }
-
- /// the population to set
- public IPopulation Population
- {
- get { return _population; }
- set { _population = value; }
- }
-
- ///
- /// Set the leader id. This value is not persisted, it is used only for
- /// loading.
- ///
- ///
- /// the leaderID to set
- public long TempLeaderID
- {
- get { return _leaderID; }
- set { _leaderID = value; }
- }
-
- #region ISpecies Members
-
- ///
- /// Calculate the amount to spawn.
- ///
- ///
- public void CalculateSpawnAmount()
- {
- _spawnsRequired = 0;
-
- foreach (IGenome genome in _members)
- {
- _spawnsRequired += genome.AmountToSpawn;
- }
- }
-
- ///
- /// Choose a parent to mate. Choose from the population, determined by the
- /// survival rate. From this pool, a random parent is chosen.
- ///
- ///
- /// The parent.
- public IGenome ChooseParent()
- {
- IGenome baby;
-
- // If there is a single member, then choose that one.
- if (_members.Count == 1)
- {
- baby = _members[0];
- }
- else
- {
- // If there are many, then choose the population based on survival
- // rate
- // and select a random genome.
- int maxIndexSize = (int) (_population.SurvivalRate*_members.Count) + 1;
- var theOne = (int) RangeRandomizer.Randomize(0, maxIndexSize);
- baby = _members[theOne];
- }
-
- return baby;
- }
-
- ///
- /// Set the age of this species.
- ///
- ///
- /// The age of this species.
- public int Age
- {
- get { return _age; }
- set { _age = value; }
- }
-
-
- ///
- /// Set the best score.
- ///
- ///
- /// The best score.
- public double BestScore
- {
- get { return _bestScore; }
- set { _bestScore = value; }
- }
-
-
- ///
- /// Set the number of generations with no improvement.
- ///
- ///
- /// The number of generations.
- public int GensNoImprovement
- {
- get { return _gensNoImprovement; }
- set { _gensNoImprovement = value; }
- }
-
-
- ///
- /// Set the leader.
- ///
- ///
- /// The new leader.
- public IGenome Leader
- {
- get { return _leader; }
- set { _leader = value; }
- }
-
-
- /// The members of this species.
- public IList Members
- {
- get { return _members; }
- }
-
-
- /// The number to spawn.
- public double NumToSpawn
- {
- get { return _spawnsRequired; }
- }
-
-
- ///
- /// Set the number of spawns required.
- ///
- public double SpawnsRequired
- {
- get { return _spawnsRequired; }
- set { _spawnsRequired = value; }
- }
-
-
- ///
- /// Purge all members, increase age by one and count the number of
- /// generations with no improvement.
- ///
- ///
- public void Purge()
- {
- _members.Clear();
- _age++;
- _gensNoImprovement++;
- _spawnsRequired = 0;
- }
-
- ///
- /// Set the species id.
- ///
- public long SpeciesID
- {
- get { return _speciesID; }
- set { _speciesID = value; }
- }
-
- #endregion
- }
-}
diff --git a/encog-core-cs/ML/HMM/Alog/ForwardBackwardCalculator.cs b/encog-core-cs/ML/HMM/Alog/ForwardBackwardCalculator.cs
index e18f452e..18d26b34 100644
--- a/encog-core-cs/ML/HMM/Alog/ForwardBackwardCalculator.cs
+++ b/encog-core-cs/ML/HMM/Alog/ForwardBackwardCalculator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Alog/ForwardBackwardScaledCalculator.cs b/encog-core-cs/ML/HMM/Alog/ForwardBackwardScaledCalculator.cs
index 56c8c066..a699be62 100644
--- a/encog-core-cs/ML/HMM/Alog/ForwardBackwardScaledCalculator.cs
+++ b/encog-core-cs/ML/HMM/Alog/ForwardBackwardScaledCalculator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Alog/KullbackLeiblerDistanceCalculator.cs b/encog-core-cs/ML/HMM/Alog/KullbackLeiblerDistanceCalculator.cs
index 0fa26a42..3c54ef4c 100644
--- a/encog-core-cs/ML/HMM/Alog/KullbackLeiblerDistanceCalculator.cs
+++ b/encog-core-cs/ML/HMM/Alog/KullbackLeiblerDistanceCalculator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Alog/MarkovGenerator.cs b/encog-core-cs/ML/HMM/Alog/MarkovGenerator.cs
index 75713f15..65982d17 100644
--- a/encog-core-cs/ML/HMM/Alog/MarkovGenerator.cs
+++ b/encog-core-cs/ML/HMM/Alog/MarkovGenerator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -100,7 +100,7 @@ public IMLDataPair Observation()
public IMLDataSet ObservationSequence(int length)
{
- IMLDataSet sequence = new BasicMLDataSet();
+ var sequence = new BasicMLDataSet();
while (length-- > 0)
{
sequence.Add(Observation());
diff --git a/encog-core-cs/ML/HMM/Alog/ViterbiCalculator.cs b/encog-core-cs/ML/HMM/Alog/ViterbiCalculator.cs
index 2fbe309e..8423368c 100644
--- a/encog-core-cs/ML/HMM/Alog/ViterbiCalculator.cs
+++ b/encog-core-cs/ML/HMM/Alog/ViterbiCalculator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Distributions/ContinousDistribution.cs b/encog-core-cs/ML/HMM/Distributions/ContinousDistribution.cs
index 609053ab..47d4cb6f 100644
--- a/encog-core-cs/ML/HMM/Distributions/ContinousDistribution.cs
+++ b/encog-core-cs/ML/HMM/Distributions/ContinousDistribution.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -129,7 +129,7 @@ public Matrix Covariance
public void Fit(IMLDataSet co)
{
var weights = new double[co.Count];
- EngineArray.Fill(weights, 1.0/co.Count);
+ EngineArray.Fill(weights, 1.0 / co.Count);
Fit(co, weights);
}
@@ -152,7 +152,7 @@ public void Fit(IMLDataSet co, double[] weights)
foreach (IMLDataPair o in co)
{
- mean[r] += o.Input[r]*weights[i++];
+ mean[r] += o.Input[r] * weights[i++];
}
}
@@ -161,19 +161,20 @@ public void Fit(IMLDataSet co, double[] weights)
i = 0;
foreach (IMLDataPair o in co)
{
- double[] obs = o.Input.Data;
- var omm = new double[obs.Length];
+ //double[] obs = o.Input.CreateCentroid();
- for (int j = 0; j < obs.Length; j++)
+ var omm = new double[o.Input.Count];
+
+ for (int j = 0; j < omm.Length; j++)
{
- omm[j] = obs[j] - mean[j];
+ omm[j] = o.Input[j] - mean[j];
}
for (int r = 0; r < _dimension; r++)
{
for (int c = 0; c < _dimension; c++)
{
- covariance[r][c] += omm[r]*omm[c]*weights[i];
+ covariance[r][c] += omm[r] * omm[c] * weights[i];
}
}
@@ -194,28 +195,29 @@ public IMLDataPair Generate()
}
double[] d2 = MatrixMath.Multiply(_covarianceL, d);
- return new BasicMLDataPair(new BasicMLData(EngineArray.Add(d2,
- _mean)));
+ return new BasicMLDataPair(new BasicMLData(EngineArray.Add(d2, _mean)));
}
+
///
public double Probability(IMLDataPair o)
{
- double[] v = o.InputArray;
- Matrix vmm = Matrix.CreateColumnMatrix(EngineArray.Subtract(v,
+ // double[] v = o.InputArray;
+ // Matrix vmm = Matrix.CreateColumnMatrix(EngineArray.Subtract(v,
+ Matrix vmm = Matrix.CreateColumnMatrix(EngineArray.Subtract(o.Input,
_mean));
Matrix t = MatrixMath.Multiply(_covarianceInv, vmm);
double expArg = MatrixMath.Multiply(MatrixMath.Transpose(vmm), t)
- [0, 0]*-0.5;
+ [0, 0] * -0.5;
return Math.Exp(expArg)
- /(Math.Pow(2.0*Math.PI, _dimension/2.0)*Math.Pow(
+ / (Math.Pow(2.0 * Math.PI, _dimension / 2.0) * Math.Pow(
_covarianceDet, 0.5));
}
///
IStateDistribution IStateDistribution.Clone()
{
- return new ContinousDistribution((double[]) _mean.Clone(), (double[][]) _covariance.Data.Clone());
+ return new ContinousDistribution((double[])_mean.Clone(), (double[][])_covariance.Data.Clone());
}
#endregion
diff --git a/encog-core-cs/ML/HMM/Distributions/DiscreteDistribution.cs b/encog-core-cs/ML/HMM/Distributions/DiscreteDistribution.cs
index 67b0186e..9c8d19bc 100644
--- a/encog-core-cs/ML/HMM/Distributions/DiscreteDistribution.cs
+++ b/encog-core-cs/ML/HMM/Distributions/DiscreteDistribution.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -163,7 +163,7 @@ public void Fit(IMLDataSet co, double[] weights)
/// The random element.
public IMLDataPair Generate()
{
- IMLData result = new BasicMLData(_probabilities.Length);
+ var result = new BasicMLData(_probabilities.Length);
for (int i = 0; i < _probabilities.Length; i++)
{
diff --git a/encog-core-cs/ML/HMM/Distributions/IStateDistribution.cs b/encog-core-cs/ML/HMM/Distributions/IStateDistribution.cs
index fd2967eb..75ac12a1 100644
--- a/encog-core-cs/ML/HMM/Distributions/IStateDistribution.cs
+++ b/encog-core-cs/ML/HMM/Distributions/IStateDistribution.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/HiddenMarkovModel.cs b/encog-core-cs/ML/HMM/HiddenMarkovModel.cs
index 20351a4f..1a3b2dfe 100644
--- a/encog-core-cs/ML/HMM/HiddenMarkovModel.cs
+++ b/encog-core-cs/ML/HMM/HiddenMarkovModel.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/PersistHMM.cs b/encog-core-cs/ML/HMM/PersistHMM.cs
index 1a3b5a78..633b1286 100644
--- a/encog-core-cs/ML/HMM/PersistHMM.cs
+++ b/encog-core-cs/ML/HMM/PersistHMM.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Train/BW/BaseBaumWelch.cs b/encog-core-cs/ML/HMM/Train/BW/BaseBaumWelch.cs
index 8c5cb841..b0e4fb27 100644
--- a/encog-core-cs/ML/HMM/Train/BW/BaseBaumWelch.cs
+++ b/encog-core-cs/ML/HMM/Train/BW/BaseBaumWelch.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelch.cs b/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelch.cs
index e9d61685..7f40c42f 100644
--- a/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelch.cs
+++ b/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelch.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelchScaled.cs b/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelchScaled.cs
index 2f94e7a7..1594cbf9 100644
--- a/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelchScaled.cs
+++ b/encog-core-cs/ML/HMM/Train/BW/TrainBaumWelchScaled.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Train/KMeans/Clusters.cs b/encog-core-cs/ML/HMM/Train/KMeans/Clusters.cs
index 26e2cf17..a273232b 100644
--- a/encog-core-cs/ML/HMM/Train/KMeans/Clusters.cs
+++ b/encog-core-cs/ML/HMM/Train/KMeans/Clusters.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/HMM/Train/KMeans/TrainKMeans.cs b/encog-core-cs/ML/HMM/Train/KMeans/TrainKMeans.cs
index baf8d7c1..6bd23d6e 100644
--- a/encog-core-cs/ML/HMM/Train/KMeans/TrainKMeans.cs
+++ b/encog-core-cs/ML/HMM/Train/KMeans/TrainKMeans.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -205,7 +205,7 @@ private void LearnOpdf(HiddenMarkovModel hmm)
}
else
{
- IMLDataSet temp = new BasicMLDataSet();
+ var temp = new BasicMLDataSet();
foreach (IMLDataPair pair in clusterObservations)
{
temp.Add(pair);
diff --git a/encog-core-cs/ML/IMLAutoAssocation.cs b/encog-core-cs/ML/IMLAutoAssocation.cs
index 1ccb153d..f07caab5 100644
--- a/encog-core-cs/ML/IMLAutoAssocation.cs
+++ b/encog-core-cs/ML/IMLAutoAssocation.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLClassification.cs b/encog-core-cs/ML/IMLClassification.cs
index ce5890aa..c5d8d34b 100644
--- a/encog-core-cs/ML/IMLClassification.cs
+++ b/encog-core-cs/ML/IMLClassification.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLCluster.cs b/encog-core-cs/ML/IMLCluster.cs
index f644a9ad..f81d6620 100644
--- a/encog-core-cs/ML/IMLCluster.cs
+++ b/encog-core-cs/ML/IMLCluster.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLClustering.cs b/encog-core-cs/ML/IMLClustering.cs
index a1ba3130..e27ed293 100644
--- a/encog-core-cs/ML/IMLClustering.cs
+++ b/encog-core-cs/ML/IMLClustering.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLContext.cs b/encog-core-cs/ML/IMLContext.cs
index 4d76d810..69f7b165 100644
--- a/encog-core-cs/ML/IMLContext.cs
+++ b/encog-core-cs/ML/IMLContext.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLEncodable.cs b/encog-core-cs/ML/IMLEncodable.cs
index 7d5ba440..afd749f2 100644
--- a/encog-core-cs/ML/IMLEncodable.cs
+++ b/encog-core-cs/ML/IMLEncodable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLError.cs b/encog-core-cs/ML/IMLError.cs
index 13f62f67..68258f26 100644
--- a/encog-core-cs/ML/IMLError.cs
+++ b/encog-core-cs/ML/IMLError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLFactory.cs b/encog-core-cs/ML/IMLFactory.cs
new file mode 100644
index 00000000..aecd3ae6
--- /dev/null
+++ b/encog-core-cs/ML/IMLFactory.cs
@@ -0,0 +1,47 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML
+{
+ ///
+ /// This interface defines the fact that a class, or object, is having the
+ /// ability to generate an Encog factory code from the objects instanciated
+ /// state.
+ ///
+ public interface IMLFactory
+ {
+ ///
+ /// The Encog factory type code.
+ ///
+ string FactoryType { get; }
+
+ ///
+ /// The Encog architecture code.
+ ///
+ string FactoryArchitecture { get; }
+ }
+}
diff --git a/encog-core-cs/ML/IMLInput.cs b/encog-core-cs/ML/IMLInput.cs
index e185fa38..c2bb7e08 100644
--- a/encog-core-cs/ML/IMLInput.cs
+++ b/encog-core-cs/ML/IMLInput.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLInputOutput.cs b/encog-core-cs/ML/IMLInputOutput.cs
index 35da54c8..8752807a 100644
--- a/encog-core-cs/ML/IMLInputOutput.cs
+++ b/encog-core-cs/ML/IMLInputOutput.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLMethod.cs b/encog-core-cs/ML/IMLMethod.cs
index b1c2b706..5882c34a 100644
--- a/encog-core-cs/ML/IMLMethod.cs
+++ b/encog-core-cs/ML/IMLMethod.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLOutput.cs b/encog-core-cs/ML/IMLOutput.cs
index c72e8136..9f41f414 100644
--- a/encog-core-cs/ML/IMLOutput.cs
+++ b/encog-core-cs/ML/IMLOutput.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLProperties.cs b/encog-core-cs/ML/IMLProperties.cs
index 05d9bb11..ffee0586 100644
--- a/encog-core-cs/ML/IMLProperties.cs
+++ b/encog-core-cs/ML/IMLProperties.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLRegression.cs b/encog-core-cs/ML/IMLRegression.cs
index c03751e3..d09f8d6c 100644
--- a/encog-core-cs/ML/IMLRegression.cs
+++ b/encog-core-cs/ML/IMLRegression.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLResettable.cs b/encog-core-cs/ML/IMLResettable.cs
index 7044ea71..f5d0eb05 100644
--- a/encog-core-cs/ML/IMLResettable.cs
+++ b/encog-core-cs/ML/IMLResettable.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/IMLStateSequence.cs b/encog-core-cs/ML/IMLStateSequence.cs
index 986287ce..c848470a 100644
--- a/encog-core-cs/ML/IMLStateSequence.cs
+++ b/encog-core-cs/ML/IMLStateSequence.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/KMeans/BasicCluster.cs b/encog-core-cs/ML/KMeans/BasicCluster.cs
index 074e3121..b89198ac 100644
--- a/encog-core-cs/ML/KMeans/BasicCluster.cs
+++ b/encog-core-cs/ML/KMeans/BasicCluster.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ public void Add(IMLData pair)
/// The dataset.
public IMLDataSet CreateDataSet()
{
- IMLDataSet result = new BasicMLDataSet();
+ var result = new BasicMLDataSet();
foreach (IMLData dataItem in _data)
{
diff --git a/encog-core-cs/ML/KMeans/KMeansClustering.cs b/encog-core-cs/ML/KMeans/KMeansClustering.cs
index e9282cd7..05a23692 100644
--- a/encog-core-cs/ML/KMeans/KMeansClustering.cs
+++ b/encog-core-cs/ML/KMeans/KMeansClustering.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Genetic/Selection/ParentSelection.cs b/encog-core-cs/ML/MLDelegates.cs
similarity index 70%
rename from encog-core-cs/ML/Genetic/Selection/ParentSelection.cs
rename to encog-core-cs/ML/MLDelegates.cs
index 38a94fad..042192cf 100644
--- a/encog-core-cs/ML/Genetic/Selection/ParentSelection.cs
+++ b/encog-core-cs/ML/MLDelegates.cs
@@ -1,33 +1,35 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-namespace Encog.ML.Genetic.Selection
-{
- ///
- /// Will be expanded in the future. Will define how
- /// parents are selected.
- ///
- ///
- public interface IParentSelection
- {
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Tree;
+
+namespace Encog.ML
+{
+ public class MLDelegates
+ {
+ public delegate bool TreeTraversalTask(ITreeNode node);
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/FeedforwardConfig.cs b/encog-core-cs/ML/Model/Config/FeedforwardConfig.cs
new file mode 100644
index 00000000..6ae4ab21
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/FeedforwardConfig.cs
@@ -0,0 +1,112 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.Engine.Network.Activation;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Factory;
+using Encog.Neural.Networks;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Config class for EncogModel to use a feedforward neural network.
+ ///
+ public class FeedforwardConfig : IMethodConfig
+ {
+ ///
+ public String MethodName
+ {
+ get { return MLMethodFactory.TypeFeedforward; }
+ }
+
+ ///
+ public String SuggestModelArchitecture(VersatileMLDataSet dataset)
+ {
+ int inputColumns = dataset.NormHelper.InputColumns.Count;
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+ var hiddenCount = (int) ((inputColumns + outputColumns)*1.5);
+ var result = new StringBuilder();
+ result.Append("?:B->TANH->");
+ result.Append(hiddenCount);
+ result.Append(":B->TANH->?");
+ return result.ToString();
+ }
+
+ ///
+ public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, String architecture)
+ {
+ double inputLow = -1;
+ double inputHigh = 1;
+ double outputLow = -1;
+ double outputHigh = 1;
+
+ // Create a basic neural network, just to examine activation functions.
+ var methodFactory = new MLMethodFactory();
+ var network = (BasicNetwork) methodFactory.Create(MethodName, architecture, 1, 1);
+
+ if (network.LayerCount < 1)
+ {
+ throw new EncogError("Neural network does not have an output layer.");
+ }
+
+ IActivationFunction outputFunction = network.GetActivation(network.LayerCount - 1);
+
+ double[] d = {-1000, -100, -50};
+ outputFunction.ActivationFunction(d, 0, d.Length);
+
+ if (d[0] > 0 && d[1] > 0 && d[2] > 0)
+ {
+ inputLow = 0;
+ }
+
+ INormalizationStrategy result = new BasicNormalizationStrategy(
+ inputLow,
+ inputHigh,
+ outputLow,
+ outputHigh);
+ return result;
+ }
+
+
+ ///
+ public String SuggestTrainingType()
+ {
+ return "rprop";
+ }
+
+
+ ///
+ public String SuggestTrainingArgs(string trainingType)
+ {
+ return "";
+ }
+
+ ///
+ public int DetermineOutputCount(VersatileMLDataSet dataset)
+ {
+ return dataset.NormHelper.CalculateNormalizedOutputCount();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/IMethodConfig.cs b/encog-core-cs/ML/Model/Config/IMethodConfig.cs
new file mode 100644
index 00000000..5b4234a2
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/IMethodConfig.cs
@@ -0,0 +1,77 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Define normalization for a specific method.
+ ///
+ public interface IMethodConfig
+ {
+ ///
+ /// The method name.
+ ///
+ string MethodName { get; }
+
+ ///
+ /// Suggest a model architecture, based on a dataset.
+ ///
+ /// The dataset.
+ /// The model architecture.
+ string SuggestModelArchitecture(VersatileMLDataSet dataset);
+
+ ///
+ /// Suggest a normalization strategy based on a dataset.
+ ///
+ /// The dataset.
+ /// The architecture.
+ /// The strategy.
+ INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, string architecture);
+
+ ///
+ /// Suggest a training type.
+ ///
+ /// The training type.
+ string SuggestTrainingType();
+
+ ///
+ /// Suggest training arguments.
+ ///
+ /// The training type.
+ /// The training arguments.
+ string SuggestTrainingArgs(string trainingType);
+
+ ///
+ /// Determine the needed output count.
+ ///
+ /// The dataset.
+ /// The needed output count.
+ int DetermineOutputCount(VersatileMLDataSet dataset);
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/NEATConfig.cs b/encog-core-cs/ML/Model/Config/NEATConfig.cs
new file mode 100644
index 00000000..739fda27
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/NEATConfig.cs
@@ -0,0 +1,83 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Normalizers;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Factory;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Config class for EncogModel to use a NEAT neural network.
+ ///
+ public class NEATConfig : IMethodConfig
+ {
+ ///
+ public String MethodName
+ {
+ get { return MLMethodFactory.TypeNEAT; }
+ }
+
+ ///
+ public String SuggestModelArchitecture(VersatileMLDataSet dataset)
+ {
+ return ("cycles=4");
+ }
+
+ ///
+ public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, String architecture)
+ {
+ var result = new BasicNormalizationStrategy();
+ result.AssignInputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+
+ result.AssignOutputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+ return result;
+ }
+
+
+ ///
+ public String SuggestTrainingType()
+ {
+ return "neat-ga";
+ }
+
+
+ ///
+ public String SuggestTrainingArgs(String trainingType)
+ {
+ return "";
+ }
+
+ ///
+ public int DetermineOutputCount(VersatileMLDataSet dataset)
+ {
+ return dataset.NormHelper.CalculateNormalizedOutputCount();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/PNNConfig.cs b/encog-core-cs/ML/Model/Config/PNNConfig.cs
new file mode 100644
index 00000000..9b9611a6
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/PNNConfig.cs
@@ -0,0 +1,92 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Normalizers;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Factory;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Config class for EncogModel to use a PNN neural network.
+ ///
+ public class PNNConfig : IMethodConfig
+ {
+ ///
+ public String MethodName
+ {
+ get { return MLMethodFactory.TypePNN; }
+ }
+
+ ///
+ public String SuggestModelArchitecture(VersatileMLDataSet dataset)
+ {
+ return ("?->C(kernel=gaussian)->?");
+ }
+
+ ///
+ public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, String architecture)
+ {
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+
+ if (outputColumns > 1)
+ {
+ throw new EncogError("PNN does not support multiple output columns.");
+ }
+
+ ColumnType ct = dataset.NormHelper.OutputColumns[0].DataType;
+
+ var result = new BasicNormalizationStrategy();
+ result.AssignInputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+
+ result.AssignOutputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Nominal, new IndexedNormalizer());
+ result.AssignOutputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+ return result;
+ }
+
+
+ ///
+ public String SuggestTrainingType()
+ {
+ return MLTrainFactory.TypePNN;
+ }
+
+
+ ///
+ public String SuggestTrainingArgs(String trainingType)
+ {
+ return "";
+ }
+
+ ///
+ public int DetermineOutputCount(VersatileMLDataSet dataset)
+ {
+ return dataset.NormHelper.OutputColumns[0].Classes.Count;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/RBFNetworkConfig.cs b/encog-core-cs/ML/Model/Config/RBFNetworkConfig.cs
new file mode 100644
index 00000000..7168918c
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/RBFNetworkConfig.cs
@@ -0,0 +1,96 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Normalizers;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Factory;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Config class for EncogModel to use a RBF neural network.
+ ///
+ public class RBFNetworkConfig : IMethodConfig
+ {
+ ///
+ public String MethodName
+ {
+ get { return MLMethodFactory.TypeRbfnetwork; }
+ }
+
+ ///
+ public String SuggestModelArchitecture(VersatileMLDataSet dataset)
+ {
+ int inputColumns = dataset.NormHelper.InputColumns.Count;
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+ var hiddenCount = (int) ((inputColumns + outputColumns)*1.5);
+ var result = new StringBuilder();
+
+ result.Append("?->gaussian(c=");
+ result.Append(hiddenCount);
+ result.Append(")->?");
+ return result.ToString();
+ }
+
+ ///
+ public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, string architecture)
+ {
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+
+ ColumnType ct = dataset.NormHelper.OutputColumns[0].DataType;
+
+ var result = new BasicNormalizationStrategy();
+ result.AssignInputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+
+ result.AssignOutputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+ return result;
+ }
+
+
+ ///
+ public String SuggestTrainingType()
+ {
+ return "rprop";
+ }
+
+
+ ///
+ public String SuggestTrainingArgs(String trainingType)
+ {
+ return "";
+ }
+
+ ///
+ public int DetermineOutputCount(VersatileMLDataSet dataset)
+ {
+ return dataset.NormHelper.CalculateNormalizedOutputCount();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Model/Config/SVMConfig.cs b/encog-core-cs/ML/Model/Config/SVMConfig.cs
new file mode 100644
index 00000000..f8d84a68
--- /dev/null
+++ b/encog-core-cs/ML/Model/Config/SVMConfig.cs
@@ -0,0 +1,106 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Normalizers;
+using Encog.ML.Data.Versatile.Normalizers.Strategy;
+using Encog.ML.Factory;
+
+namespace Encog.ML.Model.Config
+{
+ ///
+ /// Config class for EncogModel to use an SVM.
+ ///
+ public class SVMConfig: IMethodConfig
+ {
+ ///
+ public String MethodName
+ {
+ get { return MLMethodFactory.TypeSVM; }
+ }
+
+
+ ///
+ public String SuggestModelArchitecture(VersatileMLDataSet dataset)
+ {
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+
+ if (outputColumns > 1)
+ {
+ throw new EncogError("SVM does not support multiple output columns.");
+ }
+
+ ColumnType ct = dataset.NormHelper.OutputColumns[0].DataType;
+ var result = new StringBuilder();
+ result.Append("?->");
+ result.Append(ct == ColumnType.Nominal ? "C" : "R");
+ result.Append("->?");
+ return result.ToString();
+ }
+
+ ///
+ public INormalizationStrategy SuggestNormalizationStrategy(VersatileMLDataSet dataset, string architecture)
+ {
+ int outputColumns = dataset.NormHelper.OutputColumns.Count;
+
+ if (outputColumns > 1)
+ {
+ throw new EncogError("SVM does not support multiple output columns.");
+ }
+
+ ColumnType ct = dataset.NormHelper.OutputColumns[0].DataType;
+
+ var result = new BasicNormalizationStrategy();
+ result.AssignInputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Nominal, new OneOfNNormalizer(0, 1));
+ result.AssignInputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+
+ result.AssignOutputNormalizer(ColumnType.Continuous, new RangeNormalizer(0, 1));
+ result.AssignOutputNormalizer(ColumnType.Nominal, new IndexedNormalizer());
+ result.AssignOutputNormalizer(ColumnType.Ordinal, new OneOfNNormalizer(0, 1));
+ return result;
+ }
+
+
+ ///
+ public string SuggestTrainingType()
+ {
+ return MLTrainFactory.TypeSVM;
+ }
+
+
+ ///
+ public String SuggestTrainingArgs(string trainingType)
+ {
+ return "";
+ }
+
+ ///
+ public int DetermineOutputCount(VersatileMLDataSet dataset)
+ {
+ return dataset.NormHelper.CalculateNormalizedOutputCount();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Model/EncogModel.cs b/encog-core-cs/ML/Model/EncogModel.cs
new file mode 100644
index 00000000..952c8cf7
--- /dev/null
+++ b/encog-core-cs/ML/Model/EncogModel.cs
@@ -0,0 +1,423 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Encog.MathUtil.Randomize.Generate;
+using Encog.ML.Data;
+using Encog.ML.Data.Cross;
+using Encog.ML.Data.Versatile;
+using Encog.ML.Data.Versatile.Columns;
+using Encog.ML.Data.Versatile.Division;
+using Encog.ML.Factory;
+using Encog.ML.Model.Config;
+using Encog.ML.Train;
+using Encog.ML.Train.Strategy.End;
+using Encog.Util;
+using Encog.Util.Simple;
+
+namespace Encog.ML.Model
+{
+ ///
+ /// Encog model is designed to allow you to easily swap between different model
+ /// types and automatically normalize data. It is designed to work with a
+ /// VersatileMLDataSet only.
+ ///
+ public class EncogModel
+ {
+ ///
+ /// The dataset to use.
+ ///
+ private readonly VersatileMLDataSet _dataset;
+
+ ///
+ /// The input features.
+ ///
+ private readonly IList _inputFeatures = new List();
+
+ ///
+ /// The standard configrations for each method type.
+ ///
+ private readonly IDictionary _methodConfigurations =
+ new Dictionary();
+
+ ///
+ /// The predicted features.
+ ///
+ private readonly IList _predictedFeatures = new List();
+
+ ///
+ /// The current method configuration, determined by the selected model.
+ ///
+ private IMethodConfig _config;
+
+ ///
+ /// The method arguments for the selected method.
+ ///
+ private string _methodArgs;
+
+ ///
+ /// The selected method type.
+ ///
+ private string _methodType;
+
+ ///
+ /// The training arguments for the selected training type.
+ ///
+ private string _trainingArgs;
+
+ ///
+ /// The selected training type.
+ ///
+ private string _trainingType;
+
+ ///
+ /// The cross validated score.
+ ///
+ public double CrossValidatedScore { get; set; }
+
+ ///
+ /// Default constructor.
+ ///
+ public EncogModel()
+ {
+ Report = new NullStatusReportable();
+ }
+
+ ///
+ /// Construct a model for the specified dataset.
+ ///
+ /// The dataset.
+ public EncogModel(VersatileMLDataSet theDataset) : this()
+ {
+ _dataset = theDataset;
+ _methodConfigurations[MLMethodFactory.TypeFeedforward] = new FeedforwardConfig();
+ _methodConfigurations[MLMethodFactory.TypeSVM] = new SVMConfig();
+ _methodConfigurations[MLMethodFactory.TypeRbfnetwork] = new RBFNetworkConfig();
+ _methodConfigurations[MLMethodFactory.TypeNEAT] = new NEATConfig();
+ _methodConfigurations[MLMethodFactory.TypePNN] = new PNNConfig();
+ }
+
+ ///
+ /// The training dataset.
+ ///
+ public MatrixMLDataSet TrainingDataset { get; set; }
+
+ ///
+ /// The validation dataset.
+ ///
+ public MatrixMLDataSet ValidationDataset { get; set; }
+
+ ///
+ /// The report.
+ ///
+ public IStatusReportable Report { get; set; }
+
+ ///
+ /// The data set.
+ ///
+ public VersatileMLDataSet Dataset
+ {
+ get { return _dataset; }
+ }
+
+ ///
+ /// The input features.
+ ///
+ public IList InputFeatures
+ {
+ get { return _inputFeatures; }
+ }
+
+ ///
+ /// The predicted features.
+ ///
+ public IList PredictedFeatures
+ {
+ get { return _predictedFeatures; }
+ }
+
+ ///
+ /// The method configurations.
+ ///
+ public IDictionary MethodConfigurations
+ {
+ get { return _methodConfigurations; }
+ }
+
+ ///
+ /// Specify a validation set to hold back.
+ ///
+ /// The percent to use for validation.
+ /// True to shuffle.
+ /// The seed for random generation.
+ public void HoldBackValidation(double validationPercent, bool shuffle,
+ int seed)
+ {
+ IList dataDivisionList = new List();
+ dataDivisionList.Add(new DataDivision(1.0 - validationPercent)); // Training
+ dataDivisionList.Add(new DataDivision(validationPercent)); // Validation
+ _dataset.Divide(dataDivisionList, shuffle,
+ new MersenneTwisterGenerateRandom((uint) seed));
+ TrainingDataset = dataDivisionList[0].Dataset;
+ ValidationDataset = dataDivisionList[1].Dataset;
+ }
+
+ ///
+ /// Fit the model using cross validation.
+ ///
+ /// The number of folds total.
+ /// The current fold.
+ /// The current fold.
+ private void FitFold(int k, int foldNum, DataFold fold)
+ {
+ IMLMethod method = CreateMethod();
+ IMLTrain train = CreateTrainer(method, fold.Training);
+
+ if (train.ImplementationType == TrainingImplementationType.Iterative)
+ {
+ var earlyStop = new SimpleEarlyStoppingStrategy(
+ fold.Validation);
+ train.AddStrategy(earlyStop);
+
+ var line = new StringBuilder();
+ while (!train.TrainingDone)
+ {
+ train.Iteration();
+ line.Length = 0;
+ line.Append("Fold #");
+ line.Append(foldNum);
+ line.Append("/");
+ line.Append(k);
+ line.Append(": Iteration #");
+ line.Append(train.IterationNumber);
+ line.Append(", Training Error: ");
+ line.Append(Format.FormatDouble(train.Error, 8));
+ line.Append(", Validation Error: ");
+ line.Append(Format.FormatDouble(earlyStop.ValidationError,
+ 8));
+ Report.Report(k, foldNum, line.ToString());
+ }
+ fold.Score = earlyStop.ValidationError;
+ fold.Method = method;
+ }
+ else if (train.ImplementationType == TrainingImplementationType.OnePass)
+ {
+ train.Iteration();
+ double validationError = CalculateError(method,
+ fold.Validation);
+ Report.Report(k, k,
+ "Trained, Training Error: " + train.Error
+ + ", Validatoin Error: " + validationError);
+ fold.Score = validationError;
+ fold.Method = method;
+ }
+ else
+ {
+ throw new EncogError("Unsupported training type for EncogModel: "
+ + train.ImplementationType);
+ }
+ }
+
+ ///
+ /// Calculate the error for the given method and dataset.
+ ///
+ /// The method to use.
+ /// The data to use.
+ /// The error.
+ public double CalculateError(IMLMethod method, IMLDataSet data)
+ {
+ if (_dataset.NormHelper.OutputColumns.Count == 1)
+ {
+ ColumnDefinition cd = _dataset.NormHelper
+ .OutputColumns[0];
+ if (cd.DataType == ColumnType.Nominal)
+ {
+ return EncogUtility.CalculateClassificationError(
+ (IMLClassification) method, data);
+ }
+ }
+
+ return EncogUtility.CalculateRegressionError((IMLRegression) method,
+ data);
+ }
+
+ ///
+ /// Create a trainer.
+ ///
+ /// The method to train.
+ /// The dataset.
+ /// The trainer.
+ private IMLTrain CreateTrainer(IMLMethod method, IMLDataSet dataset)
+ {
+ if (_trainingType == null)
+ {
+ throw new EncogError(
+ "Please call selectTraining first to choose how to train.");
+ }
+ var trainFactory = new MLTrainFactory();
+ IMLTrain train = trainFactory.Create(method, dataset, _trainingType,
+ _trainingArgs);
+ return train;
+ }
+
+ ///
+ /// Crossvalidate and fit.
+ ///
+ /// The number of folds.
+ /// True if we should shuffle.
+ /// The trained method.
+ public IMLMethod Crossvalidate(int k, bool shuffle)
+ {
+ var cross = new KFoldCrossvalidation(
+ TrainingDataset, k);
+ cross.Process(shuffle);
+
+ int foldNumber = 0;
+ foreach (DataFold fold in cross.Folds)
+ {
+ foldNumber++;
+ Report.Report(k, foldNumber, "Fold #" + foldNumber);
+ FitFold(k, foldNumber, fold);
+ }
+
+ double sum = 0;
+ double bestScore = Double.PositiveInfinity;
+ IMLMethod bestMethod = null;
+ foreach (DataFold fold in cross.Folds)
+ {
+ sum += fold.Score;
+ if (fold.Score < bestScore)
+ {
+ bestScore = fold.Score;
+ bestMethod = fold.Method;
+ }
+ }
+ sum = sum/cross.Folds.Count;
+ Report.Report(k, k, "Cross-validated score:" + sum);
+ CrossValidatedScore = sum;
+ return bestMethod;
+ }
+
+
+
+ ///
+ /// Create the selected method.
+ ///
+ /// The created method.
+ public IMLMethod CreateMethod()
+ {
+ if (_methodType == null)
+ {
+ throw new EncogError(
+ "Please call selectMethod first to choose what type of method you wish to use.");
+ }
+ var methodFactory = new MLMethodFactory();
+ IMLMethod method = methodFactory.Create(_methodType, _methodArgs, _dataset
+ .NormHelper.CalculateNormalizedInputCount(), _config
+ .DetermineOutputCount(_dataset));
+ return method;
+ }
+
+ ///
+ /// Select the method to create.
+ ///
+ /// The dataset.
+ /// The method type.
+ public void SelectMethod(VersatileMLDataSet dataset, String methodType)
+ {
+ if (!_methodConfigurations.ContainsKey(methodType))
+ {
+ throw new EncogError("Don't know how to autoconfig method: "
+ + methodType);
+ }
+
+ _config = _methodConfigurations[methodType];
+ _methodType = methodType;
+ _methodArgs = _config.SuggestModelArchitecture(dataset);
+ dataset.NormHelper.NormStrategy =
+ _config.SuggestNormalizationStrategy(dataset, _methodArgs);
+ }
+
+ ///
+ /// Select the method to use.
+ ///
+ /// The dataset.
+ /// The type of method.
+ /// The method arguments.
+ /// The training type.
+ /// The training arguments.
+ public void SelectMethod(VersatileMLDataSet dataset, String methodType,
+ String methodArgs, String trainingType, String trainingArgs)
+ {
+ if (!_methodConfigurations.ContainsKey(methodType))
+ {
+ throw new EncogError("Don't know how to autoconfig method: "
+ + methodType);
+ }
+
+
+ _config = _methodConfigurations[methodType];
+ _methodType = methodType;
+ _methodArgs = methodArgs;
+ dataset.NormHelper.NormStrategy =
+ _methodConfigurations[methodType]
+ .SuggestNormalizationStrategy(dataset, methodArgs);
+ }
+
+ ///
+ /// Select the training type.
+ ///
+ /// The dataset.
+ public void SelectTrainingType(VersatileMLDataSet dataset)
+ {
+ if (_methodType == null)
+ {
+ throw new EncogError(
+ "Please select your training method, before your training type.");
+ }
+ IMethodConfig config = _methodConfigurations[_methodType];
+ SelectTraining(dataset, config.SuggestTrainingType(),
+ config.SuggestTrainingArgs(_trainingType));
+ }
+
+ ///
+ /// Select the training to use.
+ ///
+ /// The dataset.
+ /// The type of training.
+ /// The training arguments.
+ public void SelectTraining(VersatileMLDataSet dataset, String trainingType,
+ String trainingArgs)
+ {
+ if (_methodType == null)
+ {
+ throw new EncogError(
+ "Please select your training method, before your training type.");
+ }
+
+ _trainingType = trainingType;
+ _trainingArgs = trainingArgs;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/EncogProgram.cs b/encog-core-cs/ML/Prg/EncogProgram.cs
new file mode 100644
index 00000000..51c99a00
--- /dev/null
+++ b/encog-core-cs/ML/Prg/EncogProgram.cs
@@ -0,0 +1,476 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Encog.ML.Data;
+using Encog.ML.Data.Basic;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.EA.Genome;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.ML.Prg.Train;
+using Encog.ML.Tree.Traverse.Tasks;
+using Encog.MathUtil.Randomize;
+using Encog.Parse.Expression.Common;
+using Encog.Parse.Expression.EPL;
+using Encog.Parse.Expression.RPN;
+using Encog.Util.Simple;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// Holds an Encog Programming Language (EPL) program. A Encog program is
+ /// internally represented as a tree structure. It can be rendered to a variety
+ /// of forms, such as RPN, common infix expressions, or Latex. The Encog
+ /// Workbench also allows display as a graphical tree. An Encog Program is both a
+ /// genome and phenome. No decoding is necessary.
+ /// Every Encog Program has a context. The context is the same for every Encog
+ /// Program in a population. The context defines which opcodes should be used, as
+ /// well as the defined variables.
+ /// The actual values for the variables are not stored in the context. Rather
+ /// they are stored in a variable holder. Each program usually has its own
+ /// variable holder, though it is possible to share.
+ ///
+ [Serializable]
+ public class EncogProgram : BasicGenome, IMLRegression, IMLError
+ {
+ ///
+ /// The context that this Encog program executes in, the context is typically
+ /// shared with other Encog programs.
+ ///
+ private readonly EncogProgramContext _context = new EncogProgramContext();
+
+ ///
+ /// Holds extra data that might be needed by user extended opcodes.
+ ///
+ private readonly IDictionary _extraData = new Dictionary();
+
+ ///
+ /// The variables that will be used by this Encog program.
+ ///
+ private readonly EncogProgramVariables _variables = new EncogProgramVariables();
+
+ ///
+ /// Construct the Encog program and create a default context and variable
+ /// holder. Use all available opcodes.
+ ///
+ public EncogProgram()
+ : this(new EncogProgramContext(), new EncogProgramVariables())
+ {
+ StandardExtensions.CreateAll(_context);
+ }
+
+ ///
+ /// Construct the Encog program with the specified context, but create a new
+ /// variable holder.
+ ///
+ /// The context.
+ public EncogProgram(EncogProgramContext theContext)
+ : this(theContext, new EncogProgramVariables())
+ {
+ }
+
+ ///
+ /// Construct an Encog program using the specified context and variable
+ /// holder.
+ ///
+ /// The context.
+ /// The variable holder.
+ public EncogProgram(EncogProgramContext theContext,
+ EncogProgramVariables theVariables)
+ {
+ _context = theContext;
+ _variables = theVariables;
+
+ // define variables
+ foreach (VariableMapping v in _context.DefinedVariables)
+ {
+ _variables.DefineVariable(v);
+ }
+ }
+
+ ///
+ /// Construct an Encog program using the specified expression, but create an
+ /// empty context and variable holder.
+ ///
+ /// The expression.
+ public EncogProgram(String expression)
+ : this()
+ {
+ CompileExpression(expression);
+ }
+
+ ///
+ /// The root node of the program.
+ ///
+ public ProgramNode RootNode { get; set; }
+
+ ///
+ /// The program context. The program context may be shared over
+ /// multiple programs.
+ ///
+ public EncogProgramContext Context
+ {
+ get { return _context; }
+ }
+
+ ///
+ /// The function factory from the context.
+ ///
+ public FunctionFactory Functions
+ {
+ get { return _context.Functions; }
+ }
+
+ ///
+ /// The variable mapping for the result type. This is obtained from
+ /// the context.
+ ///
+ private VariableMapping ResultType
+ {
+ get { return ((PrgPopulation) Population).Context.Result; }
+ }
+
+ ///
+ /// The return type, from the context.
+ ///
+ public EPLValueType ReturnType
+ {
+ get { return _context.Result.VariableType; }
+ }
+
+ ///
+ /// The variable holder.
+ ///
+ public EncogProgramVariables Variables
+ {
+ get { return _variables; }
+ }
+
+ ///
+ public override int Size
+ {
+ get { return RootNode.Count; }
+ }
+
+ ///
+ public double CalculateError(IMLDataSet data)
+ {
+ return EncogUtility.CalculateRegressionError(this, data);
+ }
+
+ ///
+ /// Compute the output from the input MLData. The individual values of the
+ /// input will be mapped to the variables defined in the context. The order
+ /// is the same between the input and the defined variables. The input will
+ /// be mapped to the appropriate types. Enums will use their ordinal number.
+ /// The result will be a single number MLData.
+ ///
+ /// The input to the program.
+ /// A single numer MLData.
+ public IMLData Compute(IMLData input)
+ {
+ if (input.Count != InputCount)
+ {
+ throw new EACompileError("Invalid input count.");
+ }
+
+ for (int i = 0; i < input.Count; i++)
+ {
+ _variables.SetVariable(i, input[i]);
+ }
+
+ ExpressionValue v = RootNode.Evaluate();
+ VariableMapping resultMapping = ResultType;
+
+ var result = new BasicMLData(1);
+ bool success = false;
+
+ switch (resultMapping.VariableType)
+ {
+ case EPLValueType.FloatingType:
+ if (v.IsNumeric)
+ {
+ result.Data[0] = v.ToFloatValue();
+ success = true;
+ }
+ break;
+ case EPLValueType.StringType:
+ result.Data[0] = v.ToFloatValue();
+ success = true;
+ break;
+ case EPLValueType.BooleanType:
+ if (v.IsBoolean)
+ {
+ result.Data[0] = v.ToBooleanValue() ? 1.0 : 0.0;
+ success = true;
+ }
+ break;
+ case EPLValueType.IntType:
+ if (v.IsNumeric)
+ {
+ result[0] = v.ToIntValue();
+ success = true;
+ }
+ break;
+ case EPLValueType.EnumType:
+ if (v.IsEnum)
+ {
+ result.Data[0] = v.ToIntValue();
+ success = true;
+ }
+ break;
+ }
+
+ if (!success)
+ {
+ throw new EARuntimeError("EncogProgram produced "
+ + v.ExprType.ToString() + " but "
+ + resultMapping.VariableType.ToString()
+ + " was expected.");
+ }
+
+ return result;
+ }
+
+ ///
+ public int InputCount
+ {
+ get { return _variables.Count; }
+ }
+
+ ///
+ public int OutputCount
+ {
+ get { return 1; }
+ }
+
+ ///
+ /// Parse the specified program, or expression, and return the result. No
+ /// variables can be defined for this as a default context is used. The
+ /// result is returned as a boolean.
+ ///
+ /// The program expression.
+ /// The value the expression was evaluated to.
+ public static bool ParseBoolean(String str)
+ {
+ var holder = new EncogProgram(str);
+ return holder.Evaluate().ToBooleanValue();
+ }
+
+ ///
+ /// Parse the specified program, or expression, and return the result. No
+ /// variables can be defined for this as a default context is used. The
+ /// result is returned as a boolean.
+ ///
+ /// The program expression.
+ /// The value the expression was evaluated to.
+ public static ExpressionValue ParseExpression(String str)
+ {
+ var holder = new EncogProgram(str);
+ return holder.Evaluate();
+ }
+
+ ///
+ /// Parse the specified program, or expression, and return the result. No
+ /// variables can be defined for this as a default context is used. The
+ /// result is returned as a float.
+ ///
+ /// The program expression value.
+ /// The value the expression was evaluated to.
+ public static double ParseFloat(String str)
+ {
+ var holder = new EncogProgram(str);
+ return holder.Evaluate().ToFloatValue();
+ }
+
+ ///
+ /// Parse the specified program, or expression, and return the result. No
+ /// variables can be defined for this as a default context is used. The
+ /// result is returned as a string.
+ ///
+ /// The program expression value.
+ /// The value the expression was evaluated to.
+ public static String ParseString(String str)
+ {
+ var holder = new EncogProgram(str);
+ return holder.Evaluate().ToStringValue();
+ }
+
+ ///
+ /// Compile the specified EPL into an actual program node structure, for
+ /// later execution.
+ ///
+ /// The code to compile.
+ /// The root node.
+ public ProgramNode CompileEPL(String code)
+ {
+ var parser = new ParseEPL(this);
+ RootNode = parser.Parse(code);
+ return RootNode;
+ }
+
+ ///
+ /// Compile the specified expression.
+ ///
+ /// The expression.
+ /// The program node that this was compiled into.
+ public ProgramNode CompileExpression(String expression)
+ {
+ var parser = new ParseCommonExpression(this);
+ RootNode = parser.Parse(expression);
+ return RootNode;
+ }
+
+ ///
+ public override void Copy(IGenome source)
+ {
+ // not needed, already a genome
+ }
+
+ ///
+ /// The string as a common "infix" expression.
+ ///
+ /// The string as a common "infix" expression.
+ public String DumpAsCommonExpression()
+ {
+ var render = new RenderCommonExpression();
+ return render.Render(this);
+ }
+
+ ///
+ /// Execute the program and return the result.
+ ///
+ /// The result of running the program.
+ public ExpressionValue Evaluate()
+ {
+ return RootNode.Evaluate();
+ }
+
+ ///
+ /// Find the specified node by index. The tree is traversed to do this. This
+ /// is typically used to select a random node.
+ ///
+ /// The index being sought.
+ /// The program node found.
+ public ProgramNode FindNode(int index)
+ {
+ return (ProgramNode) TaskGetNodeIndex.process(index, RootNode);
+ }
+
+ ///
+ /// The string as an EPL expression. EPL is the format that
+ /// EncogPrograms are usually persisted as.
+ ///
+ /// EPL code.
+ public String GenerateEPL()
+ {
+ var render = new RenderEPL();
+ return render.Render(this);
+ }
+
+ ///
+ /// Replace the specified node with another.
+ ///
+ /// The node to replace.
+ /// The node that is replacing that node.
+ public void ReplaceNode(ProgramNode replaceThisNode,
+ ProgramNode replaceWith)
+ {
+ if (replaceThisNode == RootNode)
+ {
+ RootNode = replaceWith;
+ }
+ else
+ {
+ TaskReplaceNode
+ .Process(RootNode, replaceThisNode, replaceWith);
+ }
+ }
+
+ ///
+ /// Select a random variable from the defined variables.
+ ///
+ /// A random number generator.
+ /// The desired types that the variable can be.
+ /// The index of the defined variable, or -1 if unable to define.
+ public int SelectRandomVariable(EncogRandom rnd,
+ IList desiredTypes)
+ {
+ IList selectionSet = _context
+ .FindVariablesByTypes(desiredTypes);
+ if (selectionSet.Count == 0
+ && desiredTypes.Contains(EPLValueType.IntType))
+ {
+ IList floatList = new List();
+ floatList.Add(EPLValueType.FloatingType);
+ selectionSet = _context.FindVariablesByTypes(floatList);
+ }
+
+ if (selectionSet.Count == 0)
+ {
+ return -1;
+ }
+
+ VariableMapping selected = selectionSet[rnd.Next(selectionSet.Count)];
+ return Context.DefinedVariables.IndexOf(selected);
+ }
+
+ ///
+ public override string ToString()
+ {
+ var render = new RenderRPN();
+ String code = render.Render(this);
+ var result = new StringBuilder();
+ result.Append("[EncogProgram: size=");
+ result.Append(Size);
+ result.Append(", score=");
+ result.Append(Score);
+ result.Append(",code=");
+ result.Append(code);
+ result.Append("]");
+ return result.ToString();
+ }
+
+ ///
+ /// Get extra data that might be needed by user extended opcodes.
+ ///
+ /// The name the data was stored under.
+ /// The extra data.
+ public Object GetExtraData(String name)
+ {
+ return _extraData[name];
+ }
+
+ ///
+ /// Set extra data that might be needed by extensions.
+ ///
+ /// The name of the data stored.
+ /// The data.
+ public void SetExtraData(String name, Object value)
+ {
+ _extraData[name] = value;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/EncogProgramContext.cs b/encog-core-cs/ML/Prg/EncogProgramContext.cs
new file mode 100644
index 00000000..d28f2e28
--- /dev/null
+++ b/encog-core-cs/ML/Prg/EncogProgramContext.cs
@@ -0,0 +1,367 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.Util.CSV;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// Every EncogProgram must belong to a context. When programs are in a
+ /// population, they must all share a common context. The context defines
+ /// attributes that are common to all programs. The following information is
+ /// stored in a context.
+ /// The number formatting used. Namely, what type of radix point should strings
+ /// be parsed/rendered to.
+ /// The functions, or opcodes, that are available to the program. This defines
+ /// the set of functions & operators that a program might use. For an Encog
+ /// Program all operators are treated as functions internally. A operator is
+ /// essentially a shortcut notation for common functions.
+ /// The defined variables. These variables are constant for the run of the
+ /// program, but typically change for each run. They are essentially the
+ /// variables that make up an algebraic expression.
+ /// ly, the return value mapping for the programs.
+ ///
+ [Serializable]
+ public class EncogProgramContext
+ {
+ ///
+ /// The defined variables. These variables are constant for the run of the
+ /// program, but typically change for each run. They are essentially the
+ /// variables that make up an algebraic expression.
+ ///
+ private readonly IList _definedVariables = new List();
+
+ ///
+ /// The number formatting used. Namely, what type of radix point should
+ /// strings be parsed/rendered to.
+ ///
+ private readonly CSVFormat _format;
+
+ ///
+ /// The functions, or opcodes, that are available to the program. This
+ /// defines the set of functions & operators that a program might use. For an
+ /// Encog Program all operators are treated as functions internally. A
+ /// operator is essentially a shortcut notation for common functions.
+ ///
+ private readonly FunctionFactory _functions;
+
+ ///
+ /// Lookup map for the defined variables.
+ ///
+ private readonly IDictionary _map = new Dictionary();
+
+ ///
+ /// The return value mapping for the programs.
+ ///
+ private VariableMapping _result = new VariableMapping(null,
+ EPLValueType.FloatingType);
+
+ ///
+ /// Construct the context with an English number format and an empty function
+ /// factory.
+ ///
+ public EncogProgramContext()
+ : this(CSVFormat.English, new FunctionFactory())
+ {
+ }
+
+ ///
+ /// Construct a context with the specified number format and an empty
+ /// function factory.
+ ///
+ /// The format.
+ public EncogProgramContext(CSVFormat format)
+ : this(format, new FunctionFactory())
+ {
+ }
+
+ ///
+ /// Construct the context with the specified format and function factory.
+ ///
+ /// The format.
+ /// The function factory.
+ public EncogProgramContext(CSVFormat theFormat,
+ FunctionFactory theFunctions)
+ {
+ _format = theFormat;
+ _functions = theFunctions;
+ }
+
+ ///
+ /// The defined variables.
+ ///
+ public IList DefinedVariables
+ {
+ get { return _definedVariables; }
+ }
+
+ ///
+ /// The number formatting used. Namely, what type of radix point
+ /// should strings be parsed/rendered to.
+ ///
+ public CSVFormat Format
+ {
+ get { return _format; }
+ }
+
+ ///
+ /// The functions, or opcodes, that are available to the program.
+ /// This defines the set of functions & operators that a program
+ /// might use. For an Encog Program all operators are treated as
+ /// functions internally. A operator is essentially a shortcut
+ /// notation for common functions.
+ ///
+ public FunctionFactory Functions
+ {
+ get { return _functions; }
+ }
+
+ ///
+ /// The result of the program.
+ ///
+ public VariableMapping Result
+ {
+ get { return _result; }
+ set { _result = value; }
+ }
+
+ ///
+ /// True, if enums are defined.
+ ///
+ public bool HasEnum
+ {
+ get
+ {
+ if (_result.VariableType == EPLValueType.EnumType)
+ {
+ return true;
+ }
+
+ return _definedVariables.Any(mapping => mapping.VariableType == EPLValueType.EnumType);
+ }
+ }
+
+ ///
+ /// Clear the defined variables.
+ ///
+ public void ClearDefinedVariables()
+ {
+ _definedVariables.Clear();
+ _map.Clear();
+ }
+
+ ///
+ /// Clone a branch of the program from the specified node.
+ ///
+ /// The program that this branch will be "grafted" into.
+ /// The branch to clone, from the source program.
+ /// The cloned branch.
+ public ProgramNode CloneBranch(EncogProgram targetProgram,
+ ProgramNode sourceBranch)
+ {
+ if (sourceBranch == null)
+ {
+ throw new EncogError("Can't clone null branch.");
+ }
+
+ String name = sourceBranch.Name;
+
+ // create any subnodes
+ var args = new ProgramNode[sourceBranch.ChildNodes.Count];
+ for (int i = 0; i < args.Length; i++)
+ {
+ args[i] = CloneBranch(targetProgram, (ProgramNode) sourceBranch
+ .ChildNodes[i]);
+ }
+
+ ProgramNode result = targetProgram.Context.Functions
+ .FactorProgramNode(name, targetProgram, args);
+
+ // now copy the expression data for the node
+ for (int i = 0; i < sourceBranch.Data.Length; i++)
+ {
+ result.Data[i] = new ExpressionValue(sourceBranch.Data[i]);
+ }
+
+ // return the new node
+ return result;
+ }
+
+ ///
+ /// Clone an entire program, keep the same context.
+ ///
+ /// The source program.
+ /// The cloned program.
+ public EncogProgram CloneProgram(EncogProgram sourceProgram)
+ {
+ ProgramNode rootNode = sourceProgram.RootNode;
+ var result = new EncogProgram(this);
+ result.RootNode = CloneBranch(result, rootNode);
+ return result;
+ }
+
+ ///
+ /// Create a new program, using this context.
+ ///
+ /// The common expression to compile.
+ /// The resulting program.
+ public EncogProgram CreateProgram(String expression)
+ {
+ var result = new EncogProgram(this);
+ result.CompileExpression(expression);
+ return result;
+ }
+
+ ///
+ /// Define the specified variable as floating point.
+ ///
+ /// The variable name to define.
+ public void DefineVariable(String theName)
+ {
+ DefineVariable(theName, EPLValueType.FloatingType, 0, 0);
+ }
+
+ ///
+ /// Define the specified variable as the specified type. Don't use this for
+ /// enums.
+ ///
+ /// The name of the variable.
+ /// The variable type.
+ public void DefineVariable(String theName,
+ EPLValueType theVariableType)
+ {
+ DefineVariable(theName, theVariableType, 0, 0);
+ }
+
+ ///
+ /// Define a variable.
+ ///
+ /// The name of the variable.
+ /// The type of variable.
+ /// The enum type, not used if not an enum type.
+ ///
+ /// The number of values for the enum, not used if not an enum
+ /// type.
+ ///
+ public void DefineVariable(String theName,
+ EPLValueType theVariableType, int theEnumType,
+ int theEnumValueCount)
+ {
+ var mapping = new VariableMapping(theName,
+ theVariableType, theEnumType, theEnumValueCount);
+ DefineVariable(mapping);
+ }
+
+ ///
+ /// Define a variable, based on a mapping.
+ ///
+ /// The variable mapping.
+ public void DefineVariable(VariableMapping mapping)
+ {
+ if (_map.ContainsKey(mapping.Name))
+ {
+ throw new EACompileError("Variable " + mapping.Name
+ + " already defined.");
+ }
+ _map[mapping.Name] = mapping;
+ _definedVariables.Add(mapping);
+ }
+
+ ///
+ /// Find all of the variables of the specified types.
+ ///
+ /// The types to look for.
+ /// The variables that matched the specified types.
+ public IList FindVariablesByTypes(
+ IList desiredTypes)
+ {
+ return _definedVariables.Where(mapping => desiredTypes.Contains(mapping.VariableType)).ToList();
+ }
+
+ ///
+ /// Get the enum ordinal count for the specified enumeration type.
+ ///
+ /// The enumeration type.
+ /// The ordinal count for the specified enumeration type.
+ public int GetEnumCount(int enumType)
+ {
+ // make sure we consider the result
+ if (_result.VariableType == EPLValueType.EnumType
+ && _result.EnumType == enumType)
+ {
+ return _result.EnumValueCount;
+ }
+
+ foreach (VariableMapping mapping in _definedVariables)
+ {
+ if (mapping.VariableType == EPLValueType.EnumType)
+ {
+ if (mapping.EnumType == enumType)
+ {
+ return mapping.EnumValueCount;
+ }
+ }
+ }
+ throw new EACompileError("Undefined enum type: " + enumType);
+ }
+
+ ///
+ /// Get the max enum type for all defined variables.
+ ///
+ /// The max enumeration type.
+ public int GetMaxEnumType()
+ {
+ int r = -1;
+
+ // make sure we consider the result
+ if (_result.VariableType == EPLValueType.EnumType)
+ {
+ r = _result.EnumType;
+ }
+
+ // loop over all mappings and find the max enum type
+ r = (from mapping in _definedVariables where mapping.VariableType == EPLValueType.EnumType select mapping.EnumType).Concat(new[] {r}).Max();
+
+ // if we did not find one then there are no enum types
+ if (r == -1)
+ {
+ throw new EACompileError("No enum types defined in context.");
+ }
+
+ return r;
+ }
+
+ ///
+ /// Load all known functions as opcodes.
+ ///
+ public void LoadAllFunctions()
+ {
+ StandardExtensions.CreateAll(this);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/EncogProgramVariables.cs b/encog-core-cs/ML/Prg/EncogProgramVariables.cs
new file mode 100644
index 00000000..acc7fd9b
--- /dev/null
+++ b/encog-core-cs/ML/Prg/EncogProgramVariables.cs
@@ -0,0 +1,192 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// This class stores the actual variable values for an Encog Program. The
+ /// definitions for the variables are stored in the context.
+ ///
+ [Serializable]
+ public class EncogProgramVariables
+ {
+ ///
+ /// A map to the index values of each variable name.
+ ///
+ private readonly IDictionary _varMap = new Dictionary();
+
+ ///
+ /// The values of each variable. The order lines up to the order defined in
+ /// the context.
+ ///
+ private readonly IList _variables = new List();
+
+ ///
+ /// The number of variables.
+ ///
+ public int Count
+ {
+ get { return _varMap.Count; }
+ }
+
+ ///
+ /// Define the specified variable mapping. This is to be used by the context
+ /// to setup the variable definitions. Do not call it directly. You will have
+ /// unexpected results if you have a variable defined in this class, but not
+ /// in the context.
+ ///
+ /// The variable mapping.
+ public void DefineVariable(VariableMapping mapping)
+ {
+ if (_varMap.ContainsKey(mapping.Name))
+ {
+ throw new EARuntimeError(
+ "Variable "
+ + mapping.Name
+ + " already defined, simply set its value, do not redefine.");
+ }
+ _varMap[mapping.Name] = _variables.Count;
+ _variables.Add(new ExpressionValue(mapping.VariableType));
+ }
+
+ ///
+ /// Get a variable value by index.
+ ///
+ /// The index of the variable we are using.
+ /// The variable at the specified index.
+ public ExpressionValue GetVariable(int i)
+ {
+ return _variables[i];
+ }
+
+ ///
+ /// Get a variable value by name.
+ ///
+ /// The name of the variable we are using.
+ /// The variable at the specified index.
+ public ExpressionValue GetVariable(String name)
+ {
+ if (_varMap.ContainsKey(name))
+ {
+ int index = _varMap[name];
+ return _variables[index];
+ }
+ return null;
+ }
+
+ ///
+ /// Get a variable index by name.
+ ///
+ /// The variable name.
+ /// The index of the specified variable.
+ public int GetVariableIndex(String varName)
+ {
+ if (!VariableExists(varName))
+ {
+ throw new EARuntimeError("Undefined variable: " + varName);
+ }
+
+ return _varMap[varName];
+ }
+
+ ///
+ /// Get a variable name by index.
+ ///
+ /// The variable index.
+ /// The variable name.
+ public String GetVariableName(int idx)
+ {
+ foreach (string key in _varMap.Keys)
+ {
+ int value = _varMap[key];
+ if (value == idx)
+ {
+ return key;
+ }
+ }
+
+ throw new EARuntimeError("No variable defined for index " + idx);
+ }
+
+ ///
+ /// Set a variable floating point value by index.
+ ///
+ /// The index.
+ /// The value.
+ public void SetVariable(int index, double value)
+ {
+ _variables[index] = new ExpressionValue(value);
+ }
+
+ ///
+ /// Set a floating point variable value by name.
+ ///
+ /// The name.
+ /// The value.
+ public void SetVariable(String name, double d)
+ {
+ SetVariable(name, new ExpressionValue(d));
+ }
+
+ ///
+ /// Set a variable value by name.
+ ///
+ /// The variable name.
+ /// The value.
+ public void SetVariable(String name,
+ ExpressionValue value)
+ {
+ lock (this)
+ {
+ if (_varMap.ContainsKey(name))
+ {
+ int index = _varMap[name];
+ _variables[index] = value;
+ }
+ else
+ {
+ _varMap[name] = _variables.Count;
+ _variables.Add(value);
+ }
+ }
+ }
+
+ /**
+ * @return Get the number of variables defined.
+ */
+
+ ///
+ /// Determine if the specified variable name exists.
+ ///
+ /// The name of the variable.
+ /// True if the name already exists.
+ public bool VariableExists(String name)
+ {
+ return _varMap.ContainsKey(name);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/ExpValue/DivisionByZeroError.cs b/encog-core-cs/ML/Prg/ExpValue/DivisionByZeroError.cs
new file mode 100644
index 00000000..11a2562c
--- /dev/null
+++ b/encog-core-cs/ML/Prg/ExpValue/DivisionByZeroError.cs
@@ -0,0 +1,67 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Exceptions;
+
+namespace Encog.ML.Prg.ExpValue
+{
+ ///
+ /// A division by zero.
+ ///
+ public class DivisionByZeroError : EARuntimeError
+ {
+ ///
+ /// Construct a message exception.
+ ///
+ /// The message.
+ public DivisionByZeroError(String str)
+ : base(str)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The other exception.
+ public DivisionByZeroError(Exception e)
+ : base("Nested Exception", e)
+ {
+ }
+
+ ///
+ /// Pass on an exception.
+ ///
+ /// The message.
+ /// The exception.
+ public DivisionByZeroError(String msg, Exception e)
+ : base(msg, e)
+ {
+ }
+
+ ///
+ ///
+ public DivisionByZeroError() : base("")
+ {
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/ExpValue/EPLValueType.cs b/encog-core-cs/ML/Prg/ExpValue/EPLValueType.cs
new file mode 100644
index 00000000..60fdd56a
--- /dev/null
+++ b/encog-core-cs/ML/Prg/ExpValue/EPLValueType.cs
@@ -0,0 +1,60 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.ML.Prg.ExpValue
+{
+ ///
+ /// The type of value.
+ ///
+ public enum EPLValueType
+ {
+ ///
+ /// Floating point.
+ ///
+ FloatingType,
+
+ ///
+ /// String
+ ///
+ StringType,
+
+ ///
+ /// boolean
+ ///
+ BooleanType,
+
+ ///
+ /// Integer.
+ ///
+ IntType,
+
+ ///
+ /// Enumeration
+ ///
+ EnumType,
+
+ ///
+ /// Unknown.
+ ///
+ Unknown
+ }
+}
diff --git a/encog-core-cs/ML/Prg/ExpValue/EvaluateExpr.cs b/encog-core-cs/ML/Prg/ExpValue/EvaluateExpr.cs
new file mode 100644
index 00000000..6814abc9
--- /dev/null
+++ b/encog-core-cs/ML/Prg/ExpValue/EvaluateExpr.cs
@@ -0,0 +1,228 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.Prg.ExpValue
+{
+ ///
+ /// Simple utility class that performs some basic operations on ExpressionValue
+ /// objects.
+ ///
+ public static class EvaluateExpr
+ {
+ ///
+ /// Perform an add on two expression values. a+b
+ ///
+ /// The first argument.
+ /// The second argument.
+ ///
+ /// The result of adding two numbers. Concat for strings. If one is a
+ /// string, the other is converted to string. If no string, then if
+ /// one is float, both are converted to int.
+ ///
+ public static ExpressionValue Add(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsString || b.IsString)
+ {
+ return new ExpressionValue(a.ToStringValue() + b.ToStringValue());
+ }
+ if (a.IsInt && b.IsInt)
+ {
+ return new ExpressionValue(a.ToIntValue() + b.ToIntValue());
+ }
+ return new ExpressionValue(a.ToFloatValue() + b.ToFloatValue());
+ }
+
+ ///
+ /// Perform a division on two expression values. a/b An Encog division by
+ /// zero exception can occur. If one param is a float, the other is converted
+ /// to a float.
+ ///
+ /// The first argument, must be numeric.
+ /// The second argument, must be numeric.
+ /// The result of the operation.
+ public static ExpressionValue Div(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsInt && b.IsInt)
+ {
+ long i = b.ToIntValue();
+ if (i == 0)
+ {
+ throw new DivisionByZeroError();
+ }
+ return new ExpressionValue(a.ToIntValue()/i);
+ }
+
+ double denom = b.ToFloatValue();
+
+ if (Math.Abs(denom) < EncogFramework.DefaultDoubleEqual)
+ {
+ throw new DivisionByZeroError();
+ }
+
+ return new ExpressionValue(a.ToFloatValue()/denom);
+ }
+
+ ///
+ /// Perform an equal on two expressions. Booleans, ints and strings must
+ /// exactly equal. Floating point must be equal within the default Encog
+ /// tolerance.
+ ///
+ /// The first parameter to check.
+ /// The second parameter to check.
+ /// True/false.
+ public static ExpressionValue Equ(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.ExprType == EPLValueType.BooleanType)
+ {
+ return new ExpressionValue(a.ToBooleanValue() == b.ToBooleanValue());
+ }
+ if (a.ExprType == EPLValueType.EnumType)
+ {
+ return new ExpressionValue(a.ToIntValue() == b.ToIntValue()
+ && a.EnumType == b.EnumType);
+ }
+ if (a.ExprType == EPLValueType.StringType)
+ {
+ return new ExpressionValue(a.ToStringValue().Equals(
+ b.ToStringValue()));
+ }
+ var diff = Math.Abs(a.ToFloatValue() - b.ToFloatValue());
+ return new ExpressionValue(diff < EncogFramework.DefaultDoubleEqual);
+ }
+
+ ///
+ /// Perform a multiply on two expression values. a*b If one param is a float,
+ /// the other is converted to a float.
+ ///
+ /// The first argument, must be numeric.
+ /// The second argument, must be numeric.
+ /// The result of the operation.
+ public static ExpressionValue Mul(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsInt && b.IsInt)
+ {
+ return new ExpressionValue(a.ToIntValue()*b.ToIntValue());
+ }
+ return new ExpressionValue(a.ToFloatValue()*b.ToFloatValue());
+ }
+
+
+ ///
+ /// Perform a non-equal on two expressions. Booleans, ints and strings must
+ /// exactly non-equal. Floating point must be non-equal within the default
+ /// Encog tolerance.
+ ///
+ /// The first parameter to check.
+ /// The second parameter to check.
+ /// True/false.
+ public static ExpressionValue Notequ(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.ExprType == EPLValueType.BooleanType)
+ {
+ return new ExpressionValue(a.ToBooleanValue() != b.ToBooleanValue());
+ }
+ if (a.ExprType == EPLValueType.EnumType)
+ {
+ return new ExpressionValue(a.ToIntValue() != b.ToIntValue()
+ && a.EnumType == b.EnumType);
+ }
+ if (a.ExprType == EPLValueType.StringType)
+ {
+ return new ExpressionValue(!a.ToStringValue().Equals(
+ b.ToStringValue()));
+ }
+ double diff = Math.Abs(a.ToFloatValue() - b.ToFloatValue());
+ return new ExpressionValue(diff > EncogFramework.DefaultDoubleEqual);
+ }
+
+ ///
+ /// Perform a protected div on two expression values. a/b If one param is a
+ /// float, the other is converted to a float.
+ ///
+ /// The first argument, must be numeric.
+ /// The second argument, must be numeric.
+ /// The result of the operation.
+ public static ExpressionValue Pow(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsInt && b.IsInt)
+ {
+ return new ExpressionValue(Math.Pow(a.ToIntValue(), b.ToIntValue()));
+ }
+ return new ExpressionValue(Math.Pow(a.ToFloatValue(), b.ToFloatValue()));
+ }
+
+ ///
+ /// Perform a protected div on two expression values. a/b Division by zero
+ /// results in 1.
+ ///
+ /// The first argument, must be numeric.
+ /// The second argument, must be numeric.
+ /// The result of the operation.
+ public static ExpressionValue ProtectedDiv(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsInt && b.IsInt)
+ {
+ long i = b.ToIntValue();
+ if (i == 0)
+ {
+ return new ExpressionValue(1);
+ }
+ return new ExpressionValue(a.ToIntValue()/i);
+ }
+
+ double denom = b.ToFloatValue();
+
+ if (Math.Abs(denom) < EncogFramework.DefaultDoubleEqual)
+ {
+ return new ExpressionValue(1);
+ }
+
+ return new ExpressionValue(a.ToFloatValue()/denom);
+ }
+
+ ///
+ /// Perform a subtract on two expression values. a-b If one param is a float,
+ /// the other is converted to a float.
+ ///
+ /// The first argument, must be numeric.
+ /// The second argument, must be numeric.
+ /// The result of the operation.
+ public static ExpressionValue Sub(ExpressionValue a,
+ ExpressionValue b)
+ {
+ if (a.IsInt && b.IsInt)
+ {
+ return new ExpressionValue(a.ToIntValue() - b.ToIntValue());
+ }
+ return new ExpressionValue(a.ToFloatValue() - b.ToFloatValue());
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/ExpValue/ExpressionValue.cs b/encog-core-cs/ML/Prg/ExpValue/ExpressionValue.cs
new file mode 100644
index 00000000..1b1b54c3
--- /dev/null
+++ b/encog-core-cs/ML/Prg/ExpValue/ExpressionValue.cs
@@ -0,0 +1,395 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.ML.EA.Exceptions;
+
+namespace Encog.ML.Prg.ExpValue
+{
+ ///
+ /// An EncogProgram expression value. These is how Encog stores variables and
+ /// calculates values.
+ ///
+ [Serializable]
+ public class ExpressionValue
+ {
+ ///
+ /// If the value is a boolean, this contains the value.
+ ///
+ private readonly bool _boolValue;
+
+ ///
+ /// If the value is an enum, this contains the value.
+ ///
+ private readonly int _enumType;
+
+ ///
+ /// The type of this expression.
+ ///
+ private readonly EPLValueType _expressionType;
+
+ ///
+ /// If the value is a float, this contains the value.
+ ///
+ private readonly double _floatValue;
+
+ ///
+ /// If the value is an int, this contains the value.
+ ///
+ private readonly long _intValue;
+
+ ///
+ /// If the value is a string, this contains the value.
+ ///
+ private readonly String _stringValue;
+
+ ///
+ /// Construct a boolean expression.
+ ///
+ /// The value to construct.
+ public ExpressionValue(bool theValue)
+ {
+ _boolValue = theValue;
+ _expressionType = EPLValueType.BooleanType;
+ _floatValue = 0;
+ _stringValue = null;
+ _intValue = 0;
+ _enumType = -1;
+ }
+
+ ///
+ /// Construct a boolean expression.
+ ///
+ /// The value to construct.
+ public ExpressionValue(double theValue)
+ {
+ _floatValue = theValue;
+ _expressionType = EPLValueType.FloatingType;
+ _boolValue = false;
+ _stringValue = null;
+ _intValue = 0;
+ _enumType = -1;
+ }
+
+ ///
+ /// Construct a expression based on an expression.
+ ///
+ /// The value to construct.
+ public ExpressionValue(ExpressionValue other)
+ {
+ switch (_expressionType = other._expressionType)
+ {
+ case EPLValueType.BooleanType:
+ _boolValue = other._boolValue;
+ _floatValue = 0;
+ _stringValue = null;
+ _intValue = 0;
+ _enumType = -1;
+ break;
+ case EPLValueType.FloatingType:
+ _floatValue = other._floatValue;
+ _boolValue = false;
+ _stringValue = null;
+ _intValue = 0;
+ _enumType = -1;
+ break;
+ case EPLValueType.IntType:
+ _intValue = other._intValue;
+ _boolValue = false;
+ _floatValue = 0;
+ _stringValue = null;
+ _enumType = -1;
+ break;
+ case EPLValueType.StringType:
+ _stringValue = other._stringValue;
+ _boolValue = false;
+ _floatValue = 0;
+ _intValue = 0;
+ _enumType = -1;
+ break;
+ case EPLValueType.EnumType:
+ _intValue = other._intValue;
+ _boolValue = false;
+ _floatValue = 0;
+ _stringValue = null;
+ _enumType = other._enumType;
+ break;
+ default:
+ throw new EARuntimeError("Unsupported type.");
+ }
+ }
+
+ ///
+ /// Construct an enum expression.
+ ///
+ /// The enum type.
+ /// The value to construct.
+ public ExpressionValue(int enumType, long theValue)
+ {
+ _intValue = theValue;
+ _expressionType = EPLValueType.EnumType;
+ _boolValue = false;
+ _floatValue = 0;
+ _stringValue = null;
+ _enumType = enumType;
+ }
+
+ ///
+ /// Construct an integer expression.
+ ///
+ /// The value to construct.
+ public ExpressionValue(long theValue)
+ {
+ _intValue = theValue;
+ _expressionType = EPLValueType.IntType;
+ _boolValue = false;
+ _floatValue = 0;
+ _stringValue = null;
+ _enumType = -1;
+ }
+
+ ///
+ /// Construct a string expression.
+ ///
+ /// The value to construct.
+ public ExpressionValue(String theValue)
+ {
+ _stringValue = theValue;
+ _expressionType = EPLValueType.StringType;
+ _boolValue = false;
+ _floatValue = 0;
+ _intValue = 0;
+ _enumType = -1;
+ }
+
+ ///
+ /// Construct a value of the specified type.
+ ///
+ /// The value to construct.
+ public ExpressionValue(EPLValueType theType)
+ {
+ _expressionType = theType;
+ _intValue = 0;
+ _boolValue = false;
+ _floatValue = 0;
+ _stringValue = null;
+ _enumType = -1;
+ }
+
+ ///
+ /// The enum type.
+ ///
+ public int EnumType
+ {
+ get { return _enumType; }
+ }
+
+ ///
+ /// The expression type.
+ ///
+ public EPLValueType ExprType
+ {
+ get { return _expressionType; }
+ }
+
+ ///
+ /// True, if this is a boolean.
+ ///
+ public bool IsBoolean
+ {
+ get { return _expressionType == EPLValueType.BooleanType; }
+ }
+
+ ///
+ /// True, if this is an enum.
+ ///
+ public bool IsEnum
+ {
+ get { return _expressionType == EPLValueType.EnumType; }
+ }
+
+ ///
+ /// True, if this is a float.
+ ///
+ public bool IsFloat
+ {
+ get { return _expressionType == EPLValueType.FloatingType; }
+ }
+
+ ///
+ /// True, if this is an int.
+ ///
+ public bool IsInt
+ {
+ get { return _expressionType == EPLValueType.IntType; }
+ }
+
+ ///
+ /// True, if the value is either int or float.
+ ///
+ public bool IsNumeric
+ {
+ get { return IsFloat || IsInt; }
+ }
+
+ ///
+ /// True, if this is a string.
+ ///
+ public bool IsString
+ {
+ get { return _expressionType == EPLValueType.StringType; }
+ }
+
+ ///
+ /// The value as a boolean, or type mismatch if conversion is not
+ /// possible.
+ ///
+ /// The value.
+ public bool ToBooleanValue()
+ {
+ switch (_expressionType)
+ {
+ case EPLValueType.IntType:
+ throw new EARuntimeError("Type Mismatch: can't convert "
+ + _intValue + " to boolean.");
+ case EPLValueType.FloatingType:
+ throw new EARuntimeError("Type Mismatch: can't convert "
+ + _floatValue + " to boolean.");
+ case EPLValueType.BooleanType:
+ return _boolValue;
+ case EPLValueType.StringType:
+ throw new EARuntimeError("Type Mismatch: can't convert "
+ + _stringValue + " to boolean.");
+ case EPLValueType.EnumType:
+ throw new EARuntimeError(
+ "Type Mismatch: can't convert enum to boolean.");
+ default:
+ throw new EARuntimeError("Unknown type: " + _expressionType);
+ }
+ }
+
+ ///
+ /// The value as a float, or type mismatch if conversion is not
+ /// possible.
+ ///
+ /// The value.
+ public double ToFloatValue()
+ {
+ switch (_expressionType)
+ {
+ case EPLValueType.IntType:
+ return _intValue;
+ case EPLValueType.FloatingType:
+ return _floatValue;
+ case EPLValueType.BooleanType:
+ throw new EARuntimeError(
+ "Type Mismatch: can't convert float to boolean.");
+ case EPLValueType.StringType:
+ try
+ {
+ return Double.Parse(_stringValue);
+ }
+ catch (FormatException)
+ {
+ throw new EARuntimeError("Type Mismatch: can't convert "
+ + _stringValue + " to floating point.");
+ }
+ case EPLValueType.EnumType:
+ throw new EARuntimeError(
+ "Type Mismatch: can't convert enum to float.");
+ default:
+ throw new EARuntimeError("Unknown type: " + _expressionType);
+ }
+ }
+
+ ///
+ /// The value as a int, or type mismatch if conversion is not
+ /// possible.
+ ///
+ /// The value.
+ public long ToIntValue()
+ {
+ switch (_expressionType)
+ {
+ case EPLValueType.IntType:
+ return _intValue;
+ case EPLValueType.FloatingType:
+ return (int) _floatValue;
+ case EPLValueType.BooleanType:
+ throw new EARuntimeError(
+ "Type Mismatch: can't convert int to boolean.");
+ case EPLValueType.StringType:
+ try
+ {
+ return long.Parse(_stringValue);
+ }
+ catch (FormatException)
+ {
+ throw new EARuntimeError("Type Mismatch: can't convert "
+ + _stringValue + " to int.");
+ }
+ case EPLValueType.EnumType:
+ return _intValue;
+ default:
+ throw new EARuntimeError("Unknown type: " + _expressionType);
+ }
+ }
+
+ ///
+ public override string ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[ExpressionValue: ");
+ result.Append("type: ");
+ result.Append(ExprType.ToString());
+ result.Append(", String Value: ");
+ result.Append(ToStringValue());
+ result.Append("]");
+ return result.ToString();
+ }
+
+ ///
+ /// The value as a string, or type mismatch if conversion is not
+ /// possible.
+ ///
+ /// The value.
+ public String ToStringValue()
+ {
+ switch (_expressionType)
+ {
+ case EPLValueType.IntType:
+ return "" + _intValue;
+ case EPLValueType.FloatingType:
+ return "" + _floatValue;
+ case EPLValueType.BooleanType:
+ return "" + _boolValue;
+ case EPLValueType.StringType:
+ return _stringValue;
+ case EPLValueType.EnumType:
+ return "[" + _enumType + ":" + _intValue + "]";
+ default:
+ throw new EARuntimeError("Unknown type: " + _expressionType);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/BasicTemplate.cs b/encog-core-cs/ML/Prg/Ext/BasicTemplate.cs
new file mode 100644
index 00000000..f64f5905
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/BasicTemplate.cs
@@ -0,0 +1,380 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+using Encog.Util;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// A basic template.
+ ///
+ [Serializable]
+ public class BasicTemplate : IProgramExtensionTemplate
+ {
+ ///
+ /// Delegate to evaluate the node.
+ ///
+ /// Actual node.
+ /// Evaluated value.
+ public delegate ExpressionValue DelEvaluate(ProgramNode actual);
+
+ ///
+ /// Delegate to determine return types.
+ ///
+ /// The context.
+ /// The return type to check.
+ /// True, if this is a return type.
+ public delegate bool DelIsPossibleReturnType(EncogProgramContext context, EPLValueType rtn);
+
+ ///
+ /// Delegate to randomize.
+ ///
+ /// Random number generator.
+ /// The desired type.
+ /// The actual node.
+ /// The min value.
+ /// The max value.
+ public delegate void DelRandomize(
+ EncogRandom rnd, IList desiredType, ProgramNode actual, double minValue, double maxValue);
+
+ ///
+ /// Defines a very low precidence.
+ ///
+ public const int NoPrec = 100;
+
+
+ ///
+ /// The amount of data that is stored with this node.
+ ///
+ private readonly int _dataSize;
+
+ ///
+ /// The delegate to evaluate.
+ ///
+ private readonly DelEvaluate _delEvaluate;
+
+ ///
+ /// The delegate for return types.
+ ///
+ private readonly DelIsPossibleReturnType _delIsPossibleReturnType;
+
+ ///
+ /// The delegate for randomize.
+ ///
+ private readonly DelRandomize _delRandomize;
+
+ ///
+ /// The name of this opcode.
+ ///
+ private readonly String _name;
+
+ ///
+ /// The node type.
+ ///
+ private readonly NodeType _nodeType;
+
+ ///
+ /// The parameters.
+ ///
+ private readonly IList _param = new List();
+
+ ///
+ /// The precedence.
+ ///
+ private readonly int _precedence;
+
+ ///
+ /// The return value.
+ ///
+ private readonly ParamTemplate _returnValue;
+
+ ///
+ /// The opcode signature.
+ ///
+ private readonly String _signature;
+
+ ///
+ /// True if this opcode has a variable value, other than variance of its
+ /// child nodes.
+ ///
+ private readonly bool _varValue;
+
+ ///
+ /// Construct a basic template object.
+ ///
+ /// The precedence.
+ /// The opcode signature.
+ /// The opcode type.
+ /// True, if this opcode is a variable.
+ /// The data size kept for this opcode.
+ /// The evaluator delegate.
+ /// The return type delegate.
+ /// The randomizer delegate.
+ public BasicTemplate(int thePrecedence, String theSignature,
+ NodeType theType, bool isVariable,
+ int theDataSize,
+ DelEvaluate theEvaluate,
+ DelIsPossibleReturnType theIsPossibleReturnType,
+ DelRandomize theRandomize)
+ {
+ _precedence = thePrecedence;
+ _signature = theSignature;
+ _varValue = isVariable;
+ _dataSize = theDataSize;
+ _nodeType = theType;
+
+ _delEvaluate = theEvaluate;
+ _delIsPossibleReturnType = theIsPossibleReturnType;
+ _delRandomize = theRandomize;
+
+ if (theSignature.Trim().Equals("("))
+ {
+ // special case, we add a left-paren for the shunting yard alg.
+ _name = theSignature;
+ _returnValue = null;
+ }
+ else
+ {
+ // non-special case, find the name of the function/operator
+ var parser = new SimpleParser(theSignature);
+ bool pass = false;
+
+ parser.EatWhiteSpace();
+ _name = parser.ReadToChars("(").Trim();
+ parser.Advance();
+
+ bool done = false;
+ while (!done)
+ {
+ if (parser.Peek() == ')')
+ {
+ parser.Advance();
+ done = true;
+ }
+ else if (parser.Peek() == ':')
+ {
+ parser.Advance();
+ pass = true;
+ }
+ else if (parser.Peek() == '{')
+ {
+ ParamTemplate temp = ReadParam(parser);
+ temp.PassThrough = pass;
+ pass = false;
+ _param.Add(temp);
+ }
+ else
+ {
+ parser.Advance();
+ if (parser.EOL())
+ {
+ throw new EncogError("Invalid opcode template.");
+ }
+ }
+ }
+
+ // get the return type
+ parser.EatWhiteSpace();
+ if (!parser.LookAhead(":", true))
+ {
+ throw new EACompileError("Return type not specified.");
+ }
+ parser.Advance();
+ parser.EatWhiteSpace();
+ _returnValue = ReadParam(parser);
+ }
+ }
+
+ ///
+ /// Construct a function based on the provided signature.
+ ///
+ /// The signature.
+ /// The evaluate delegate.
+ public BasicTemplate(String theSignature, DelEvaluate theEvaluate)
+ : this(0, theSignature, NodeType.Function, false, 0, theEvaluate, null, null)
+ {
+ }
+
+ ///
+ /// The signature.
+ ///
+ public String Signature
+ {
+ get { return _signature; }
+ }
+
+ ///
+ public int ChildNodeCount
+ {
+ get { return _param.Count; }
+ }
+
+ ///
+ public int DataSize
+ {
+ get { return _dataSize; }
+ }
+
+ ///
+ public String Name
+ {
+ get { return _name; }
+ }
+
+ ///
+ public NodeType NodeType
+ {
+ get { return _nodeType; }
+ }
+
+ ///
+ public IList Params
+ {
+ get { return _param; }
+ }
+
+ ///
+ public int Precedence
+ {
+ get { return _precedence; }
+ }
+
+ ///
+ public ParamTemplate ReturnValue
+ {
+ get { return _returnValue; }
+ }
+
+ ///
+ public bool IsVariable
+ {
+ get { return _varValue; }
+ }
+
+
+ public ExpressionValue Evaluate(ProgramNode actual)
+ {
+ return _delEvaluate(actual);
+ }
+
+ ///
+ public bool IsPossibleReturnType(EncogProgramContext context, EPLValueType rtn)
+ {
+ if (_delIsPossibleReturnType == null)
+ {
+ return _returnValue.PossibleTypes.Contains(rtn);
+ }
+ if (!_returnValue.PossibleTypes.Contains(rtn))
+ {
+ return false;
+ }
+
+ return _delIsPossibleReturnType(context, rtn);
+ }
+
+
+ ///
+ public void Randomize(EncogRandom rnd, IList desiredType, ProgramNode actual, double minValue,
+ double maxValue)
+ {
+ if (_delRandomize != null)
+ {
+ _delRandomize(rnd, desiredType, actual, minValue, maxValue);
+ }
+ }
+
+ ///
+ /// Read the specified parameter.
+ ///
+ /// The parser to use.
+ /// The parsed parameter.
+ private ParamTemplate ReadParam(SimpleParser parser)
+ {
+ var result = new ParamTemplate();
+
+ if (!parser.LookAhead("{", true))
+ {
+ throw new EACompileError("Expected {");
+ }
+ parser.Advance();
+
+ bool done = false;
+ var buffer = new StringBuilder();
+
+ while (!done)
+ {
+ if (parser.Peek() == '}')
+ {
+ done = true;
+ parser.Advance();
+ }
+ else if (parser.Peek() == '{')
+ {
+ throw new EACompileError("Unexpected {");
+ }
+ else if (parser.Peek() == '{')
+ {
+ done = true;
+ parser.Advance();
+ }
+ else if (parser.Peek() == ',')
+ {
+ result.AddType(buffer.ToString().Trim().ToLower());
+ parser.Advance();
+ buffer.Length = 0;
+ }
+ else
+ {
+ buffer.Append(parser.ReadChar());
+ }
+ }
+
+ String s = buffer.ToString().Trim();
+ if (s.Length > 0)
+ {
+ result.AddType(s);
+ }
+
+ return result;
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[BasicTemplate:");
+ result.Append(_signature);
+ result.Append(",type=");
+ result.Append(_nodeType.ToString());
+ result.Append(",argCount=");
+ result.Append(ChildNodeCount);
+ result.Append("]");
+ return result.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/EncogOpcodeRegistry.cs b/encog-core-cs/ML/Prg/Ext/EncogOpcodeRegistry.cs
new file mode 100644
index 00000000..a2b43520
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/EncogOpcodeRegistry.cs
@@ -0,0 +1,173 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// Holds all known EPL opcodes. Extension programs should add new opcodes here.
+ /// The FunctionFactory selects a subset of opcodes from here that will be run.
+ /// An opcode is identified by its name, and the number of parameters it accepts.
+ /// It is okay to add an opcode multiple times, the new opcode replaces the
+ /// previous.
+ ///
+ public class EncogOpcodeRegistry
+ {
+ ///
+ /// The instance.
+ ///
+ private static EncogOpcodeRegistry _instance;
+
+ ///
+ /// A lookup for all of the opcodes.
+ ///
+ private readonly IDictionary _registry =
+ new Dictionary();
+
+ ///
+ /// Construct the opcode registry with all known opcodes. User programs can
+ /// always add additional opcodes later.
+ ///
+ private EncogOpcodeRegistry()
+ {
+ Add(StandardExtensions.EXTENSION_NOT_EQUAL);
+ Add(StandardExtensions.EXTENSION_NOT);
+ Add(StandardExtensions.EXTENSION_VAR_SUPPORT);
+ Add(StandardExtensions.EXTENSION_CONST_SUPPORT);
+ Add(StandardExtensions.EXTENSION_NEG);
+ Add(StandardExtensions.EXTENSION_ADD);
+ Add(StandardExtensions.EXTENSION_SUB);
+ Add(StandardExtensions.EXTENSION_MUL);
+ Add(StandardExtensions.EXTENSION_DIV);
+ Add(StandardExtensions.EXTENSION_POWER);
+ Add(StandardExtensions.EXTENSION_AND);
+ Add(StandardExtensions.EXTENSION_OR);
+ Add(StandardExtensions.EXTENSION_EQUAL);
+ Add(StandardExtensions.EXTENSION_GT);
+ Add(StandardExtensions.EXTENSION_LT);
+ Add(StandardExtensions.EXTENSION_GTE);
+ Add(StandardExtensions.EXTENSION_LTE);
+ Add(StandardExtensions.EXTENSION_ABS);
+ Add(StandardExtensions.EXTENSION_ACOS);
+ Add(StandardExtensions.EXTENSION_ASIN);
+ Add(StandardExtensions.EXTENSION_ATAN);
+ Add(StandardExtensions.EXTENSION_ATAN2);
+ Add(StandardExtensions.EXTENSION_CEIL);
+ Add(StandardExtensions.EXTENSION_COS);
+ Add(StandardExtensions.EXTENSION_COSH);
+ Add(StandardExtensions.EXTENSION_EXP);
+ Add(StandardExtensions.EXTENSION_FLOOR);
+ Add(StandardExtensions.EXTENSION_LOG);
+ Add(StandardExtensions.EXTENSION_LOG10);
+ Add(StandardExtensions.EXTENSION_MAX);
+ Add(StandardExtensions.EXTENSION_MIN);
+ Add(StandardExtensions.EXTENSION_POWFN);
+ Add(StandardExtensions.EXTENSION_RANDOM);
+ Add(StandardExtensions.EXTENSION_ROUND);
+ Add(StandardExtensions.EXTENSION_SIN);
+ Add(StandardExtensions.EXTENSION_SINH);
+ Add(StandardExtensions.EXTENSION_SQRT);
+ Add(StandardExtensions.EXTENSION_TAN);
+ Add(StandardExtensions.EXTENSION_TANH);
+ Add(StandardExtensions.EXTENSION_TODEG);
+ Add(StandardExtensions.EXTENSION_TORAD);
+ Add(StandardExtensions.EXTENSION_LENGTH);
+ Add(StandardExtensions.EXTENSION_FORMAT);
+ Add(StandardExtensions.EXTENSION_LEFT);
+ Add(StandardExtensions.EXTENSION_RIGHT);
+ Add(StandardExtensions.EXTENSION_CINT);
+ Add(StandardExtensions.EXTENSION_CFLOAT);
+ Add(StandardExtensions.EXTENSION_CSTR);
+ Add(StandardExtensions.EXTENSION_CBOOL);
+ Add(StandardExtensions.EXTENSION_IFF);
+ Add(StandardExtensions.EXTENSION_CLAMP);
+ }
+
+ ///
+ /// Get the instance.
+ ///
+ public static EncogOpcodeRegistry Instance
+ {
+ get { return _instance ?? (_instance = new EncogOpcodeRegistry()); }
+ }
+
+ ///
+ /// Construct a lookup key for the hash map.
+ ///
+ /// The name of the opcode.
+ /// The number of parameters this opcode accepts.
+ /// Return the string key.
+ public static String CreateKey(String functionName, int argCount)
+ {
+ return functionName + '`' + argCount;
+ }
+
+ ///
+ /// Add an opcode. User programs should add opcodes here.
+ ///
+ /// The opcode to add.
+ public void Add(IProgramExtensionTemplate ext)
+ {
+ _registry[
+ CreateKey(ext.Name,
+ ext.ChildNodeCount)] = ext;
+ }
+
+ ///
+ /// Find all opcodes.
+ ///
+ /// The opcodes.
+ public ICollection FindAllOpcodes()
+ {
+ return _registry.Values;
+ }
+
+ ///
+ /// Find the specified opcode.
+ ///
+ /// The name of the opcode.
+ /// The number of arguments.
+ /// The opcode if found, null otherwise.
+ public IProgramExtensionTemplate FindOpcode(String name, int args)
+ {
+ String key = CreateKey(name, args);
+ if (_registry.ContainsKey(key))
+ {
+ return _registry[key];
+ }
+ return null;
+ }
+
+
+ ///
+ /// True, if this is an operator.
+ ///
+ /// Node type to check.
+ /// True, if this is an operator.
+ public static bool IsOperator(NodeType t)
+ {
+ return t == NodeType.OperatorLeft || t == NodeType.OperatorRight || t == NodeType.Unary;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/FunctionFactory.cs b/encog-core-cs/ML/Prg/Ext/FunctionFactory.cs
new file mode 100644
index 00000000..afb50198
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/FunctionFactory.cs
@@ -0,0 +1,245 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// A function factory maps the opcodes contained in the EncogOpcodeRegistry into
+ /// an EncogProgram. You rarely want to train with every available opcode. For
+ /// example, if you are only looking to produce an equation, you should not make
+ /// use of the if-statement and logical operators.
+ ///
+ [Serializable]
+ public class FunctionFactory
+ {
+ ///
+ /// The opcodes.
+ ///
+ private readonly IList _opcodes = new List();
+
+ ///
+ /// A map for quick lookup.
+ ///
+ private readonly IDictionary _templateMap =
+ new Dictionary();
+
+ ///
+ /// The opcode list.
+ ///
+ public IList OpCodes
+ {
+ get { return _opcodes; }
+ }
+
+ ///
+ /// The template map.
+ ///
+ public IDictionary TemplateMap
+ {
+ get { return _templateMap; }
+ }
+
+ ///
+ /// The number of opcodes.
+ ///
+ public int Count
+ {
+ get { return _opcodes.Count; }
+ }
+
+ ///
+ /// Add an opcode to the function factory. The opcode must exist in the
+ /// opcode registry.
+ ///
+ /// The opcode to add.
+ public void AddExtension(IProgramExtensionTemplate ext)
+ {
+ AddExtension(ext.Name, ext.ChildNodeCount);
+ }
+
+ ///
+ /// Add an opcode to the function factory from the opcode registry.
+ ///
+ /// The name of the opcode.
+ /// The number of arguments.
+ public void AddExtension(String name, int args)
+ {
+ String key = EncogOpcodeRegistry.CreateKey(name, args);
+ if (!_templateMap.ContainsKey(key))
+ {
+ IProgramExtensionTemplate temp = EncogOpcodeRegistry.Instance
+ .FindOpcode(name, args);
+ if (temp == null)
+ {
+ throw new EACompileError("Unknown extension " + name + " with "
+ + args + " arguments.");
+ }
+ _opcodes.Add(temp);
+ _templateMap[key] = temp;
+ }
+ }
+
+ ///
+ /// Factor a new program node, based in a template object.
+ ///
+ /// The opcode.
+ /// The program.
+ /// The arguments for this node.
+ /// The newly created ProgramNode.
+ public ProgramNode FactorProgramNode(IProgramExtensionTemplate temp,
+ EncogProgram program, ProgramNode[] args)
+ {
+ return new ProgramNode(program, temp, args);
+ }
+
+ ///
+ /// Factor a new program node, based on an opcode name and arguments.
+ ///
+ /// The name of the opcode.
+ /// The program to factor for.
+ /// The arguements.
+ /// The newly created ProgramNode.
+ public ProgramNode FactorProgramNode(String name,
+ EncogProgram program, ProgramNode[] args)
+ {
+ String key = EncogOpcodeRegistry.CreateKey(name, args.Length);
+
+ if (!_templateMap.ContainsKey(key))
+ {
+ throw new EACompileError("Undefined function/operator: " + name
+ + " with " + args.Length + " args.");
+ }
+
+ IProgramExtensionTemplate temp = _templateMap[key];
+ return new ProgramNode(program, temp, args);
+ }
+
+ ///
+ /// Find a function with the specified name.
+ ///
+ /// The name of the function.
+ /// The function opcode.
+ public IProgramExtensionTemplate FindFunction(String name)
+ {
+ return _opcodes.Where(opcode => opcode.NodeType == NodeType.Function).FirstOrDefault(opcode => opcode.Name.Equals(name));
+ }
+
+ ///
+ /// Find all opcodes that match the search criteria.
+ ///
+ /// The return types to consider.
+ /// The program context.
+ /// True, to include the terminals.
+ /// True, to include the functions.
+ /// The opcodes found.
+ public IList FindOpcodes(
+ IList types, EncogProgramContext context,
+ bool includeTerminal, bool includeFunction)
+ {
+ IList result = new List();
+
+ foreach (IProgramExtensionTemplate temp in _opcodes)
+ {
+ foreach (EPLValueType rtn in types)
+ {
+ // it is a possible return type, but given our variables, is it
+ // possible
+ if (temp.IsPossibleReturnType(context, rtn))
+ {
+ if (temp.ChildNodeCount == 0 && includeTerminal)
+ {
+ result.Add(temp);
+ }
+ else if (includeFunction)
+ {
+ result.Add(temp);
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ ///
+ /// This method is used when parsing an expression. Consider x>=2. The parser
+ /// first sees the > symbol. But it must also consider the =. So we first
+ /// look for a 2-char operator, in this case there is one.
+ ///
+ /// The first character of the potential operator.
+ /// The second character of the potential operator. Zero if none.
+ /// The expression template for the operator found.
+ public IProgramExtensionTemplate FindOperator(char ch1, char ch2)
+ {
+ // if ch2==0 then we are only looking for a single char operator.
+ // this is rare, but supported.
+ if (ch2 == 0)
+ {
+ return FindOperatorExact("" + ch1);
+ }
+
+ // first, see if we can match an operator with both ch1 and ch2
+ IProgramExtensionTemplate result = FindOperatorExact("" + ch1 + ch2) ?? FindOperatorExact("" + ch1);
+
+ // return the operator if we have one.
+ return result;
+ }
+
+ ///
+ /// Find an exact match on opcode.
+ ///
+ /// The string to match.
+ /// The opcode found.
+ private IProgramExtensionTemplate FindOperatorExact(String str)
+ {
+ return _opcodes.Where(opcode => opcode.NodeType == NodeType.OperatorLeft || opcode.NodeType == NodeType.OperatorRight).FirstOrDefault(opcode => opcode.Name.Equals(str));
+ }
+
+ ///
+ /// Get the specified opcode.
+ ///
+ /// The opcode index.
+ /// The opcode.
+ public IProgramExtensionTemplate GetOpCode(int theOpCode)
+ {
+ return _opcodes[theOpCode];
+ }
+
+ ///
+ /// Determine if an opcode is in the function factory.
+ ///
+ /// The name of the opcode.
+ /// The argument count for the opcode.
+ /// True if the opcode exists.
+ public bool IsDefined(String name, int l)
+ {
+ String key = EncogOpcodeRegistry.CreateKey(name, l);
+ return _templateMap.ContainsKey(key);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/IProgramExtensionTemplate.cs b/encog-core-cs/ML/Prg/Ext/IProgramExtensionTemplate.cs
new file mode 100644
index 00000000..4b229e25
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/IProgramExtensionTemplate.cs
@@ -0,0 +1,101 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// Defines an opcode. Opcodes are used to extend Encog programs.
+ ///
+ public interface IProgramExtensionTemplate
+ {
+ ///
+ /// Get the number of child nodes that this opcode requires.
+ ///
+ int ChildNodeCount { get; }
+
+ ///
+ /// The size of extra data that is stored by this node.
+ ///
+ int DataSize { get; }
+
+ ///
+ /// Get the name of this opcode.
+ ///
+ String Name { get; }
+
+ ///
+ /// The node type.
+ ///
+ NodeType NodeType { get; }
+
+ ///
+ /// The parameters (child nodes) required by this node.
+ ///
+ IList Params { get; }
+
+ ///
+ /// The operator precedence.
+ ///
+ int Precedence { get; }
+
+ ///
+ /// The return value for this opcode.
+ ///
+ ParamTemplate ReturnValue { get; }
+
+ ///
+ /// Returns true if this node is variable.
+ ///
+ bool IsVariable { get; }
+
+ ///
+ /// Evaluate the specified actual program node, using this opcode template.
+ ///
+ /// The tree node in the actual program.
+ /// The result of the evaluation.
+ ExpressionValue Evaluate(ProgramNode actual);
+
+ ///
+ /// Determines if the specified return type is a possible return type.
+ ///
+ /// The program context.
+ /// The potential return type to check.
+ /// True, if the specified type is a possible return type.
+ bool IsPossibleReturnType(EncogProgramContext context, EPLValueType rtn);
+
+ ///
+ /// Randomize this actual tree node.
+ ///
+ /// Random number generator.
+ /// The desired type of the randomization, if allowed.
+ /// The actual program node to randomize.
+ /// The minimum value to use for randomization.
+ /// The maximum value to use for randomization.
+ void Randomize(EncogRandom rnd, IList desiredType, ProgramNode actual,
+ double minValue, double maxValue);
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/NodeType.cs b/encog-core-cs/ML/Prg/Ext/NodeType.cs
new file mode 100644
index 00000000..c33daef4
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/NodeType.cs
@@ -0,0 +1,64 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// The node type. This mostly determines how opcodes are parsed and rendered.
+ /// Node types do not generally affect execution.
+ ///
+ public enum NodeType
+ {
+ ///
+ /// A left associated operator. Operators are actually functions, however,
+ /// setting to this type does affect how the opcode is rendered.
+ /// http://en.wikipedia.org/wiki/Operator_associativity
+ ///
+ OperatorLeft,
+
+ ///
+ /// A right associated operator. Operators are actually functions, however,
+ /// setting to this type does affect how the opcode is rendered.
+ /// http://en.wikipedia.org/wiki/Operator_associativity
+ ///
+ OperatorRight,
+
+ ///
+ /// A leaf, or terminal node. No children.
+ ///
+ Leaf,
+
+ ///
+ /// A function.
+ ///
+ Function,
+
+ ///
+ /// An unary operator.
+ ///
+ Unary,
+ ///
+ /// Unknown.
+ ///
+ None
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/ParamTemplate.cs b/encog-core-cs/ML/Prg/Ext/ParamTemplate.cs
new file mode 100644
index 00000000..fa97ea67
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/ParamTemplate.cs
@@ -0,0 +1,131 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// Provides a template for parameters to the opcodes. This defines the accepted
+ /// types and if type of a given parameter passes through to the return type.
+ ///
+ [Serializable]
+ public class ParamTemplate
+ {
+ ///
+ /// Possible types for this parameter.
+ ///
+ private readonly HashSet _possibleTypes = new HashSet();
+
+ ///
+ /// Is this a pass through argument. If so, then the return type of the
+ /// parent opcode will depend on the actual type of this parameter.
+ ///
+ public bool PassThrough { get; set; }
+
+ ///
+ /// All possible types.
+ ///
+ public HashSet PossibleTypes
+ {
+ get { return _possibleTypes; }
+ }
+
+ ///
+ /// Add all known types.
+ ///
+ public void AddAllTypes()
+ {
+ foreach (EPLValueType t in Enum.GetValues(typeof (EPLValueType)))
+ {
+ AddType(t);
+ }
+ }
+
+ ///
+ /// Add the specified type.
+ ///
+ /// The type to add.
+ public void AddType(String theType)
+ {
+ if (theType.Equals("b"))
+ {
+ AddType(EPLValueType.BooleanType);
+ }
+ else if (theType.Equals("e"))
+ {
+ AddType(EPLValueType.EnumType);
+ }
+ else if (theType.Equals("f"))
+ {
+ AddType(EPLValueType.FloatingType);
+ }
+ else if (theType.Equals("i"))
+ {
+ AddType(EPLValueType.IntType);
+ }
+ else if (theType.Equals("s"))
+ {
+ AddType(EPLValueType.StringType);
+ }
+ else if (theType.Equals("*"))
+ {
+ AddAllTypes();
+ }
+ else
+ {
+ throw new EACompileError("Unknown type: " + theType);
+ }
+ }
+
+ ///
+ /// Add a type using a type enum.
+ ///
+ /// The type to add.
+ public void AddType(EPLValueType theType)
+ {
+ _possibleTypes.Add(theType);
+ }
+
+ ///
+ /// Determine the possable argument types, given the parent types.
+ ///
+ /// The parent types.
+ /// The possable types.
+ public IList DetermineArgumentTypes(
+ IList parentTypes)
+ {
+ if (PassThrough)
+ {
+ return parentTypes;
+ }
+
+ IList result = new List();
+ result = result.Union(PossibleTypes).ToList();
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Ext/StandardExtensions.cs b/encog-core-cs/ML/Prg/Ext/StandardExtensions.cs
new file mode 100644
index 00000000..bc73d117
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Ext/StandardExtensions.cs
@@ -0,0 +1,923 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Ext
+{
+ ///
+ /// Standard operators and functions.
+ ///
+ public class StandardExtensions
+ {
+ ///
+ /// Variable support.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_VAR_SUPPORT = new BasicTemplate(
+ BasicTemplate.NoPrec, "#var():{*}", NodeType.Leaf,
+ true, 1, (actual) =>
+ {
+ var idx = (int) actual.Data[0].ToIntValue();
+ ExpressionValue result = actual.Owner.Variables.GetVariable(idx);
+ if (result == null)
+ {
+ throw new EARuntimeError("Variable has no value: "
+ + actual.Owner.Variables.GetVariableName(idx));
+ }
+ return result;
+ }, (context, rtn) =>
+ {
+ foreach (VariableMapping mapping in context.DefinedVariables)
+ {
+ if (mapping.VariableType == rtn)
+ {
+ return true;
+ }
+ }
+ return false;
+ }, (rnd, desiredTypes, actual, minValue, maxValue) =>
+ {
+ int variableIndex = actual.Owner.SelectRandomVariable(rnd, desiredTypes);
+ if (variableIndex == -1)
+ {
+ throw new EncogError("Can't find any variables of type "
+ + desiredTypes.ToString() + " to generate.");
+ }
+ actual.Data[0] = new ExpressionValue(variableIndex);
+ });
+
+
+ ///
+ /// Numeric const.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CONST_SUPPORT = new BasicTemplate(
+ BasicTemplate.NoPrec, "#const():{*}", NodeType.Leaf,
+ false, 1, (actual) => { return actual.Data[0]; }, null,
+ (rnd, desiredTypes, actual, minValue, maxValue) =>
+ {
+ EPLValueType pickedType = desiredTypes[rnd
+ .Next(desiredTypes.Count)];
+ EncogProgramContext context = actual.Owner.Context;
+ switch (pickedType)
+ {
+ case EPLValueType.FloatingType:
+ actual.Data[0] = new ExpressionValue(
+ RangeRandomizer.Randomize(rnd, minValue, maxValue));
+ break;
+ case EPLValueType.StringType:
+ // this will be added later
+ break;
+ case EPLValueType.BooleanType:
+ actual.Data[0] = new ExpressionValue(rnd.NextDouble() > 0.5);
+ break;
+ case EPLValueType.IntType:
+ actual.Data[0] = new ExpressionValue(
+ (int) RangeRandomizer
+ .Randomize(rnd, minValue, maxValue));
+ break;
+ case EPLValueType.EnumType:
+ int enumType = rnd.Next(context.GetMaxEnumType() + 1);
+ int enumCount = context.GetEnumCount(enumType);
+ int enumIndex = rnd.Next(enumCount);
+ actual.Data[0] = new ExpressionValue(enumType, enumIndex);
+ break;
+ }
+ });
+
+ ///
+ /// Standard unary minus operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_NEG = new BasicTemplate(3,
+ "-({f,i}):{f,i}", NodeType.Unary,
+ false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(
+ -actual.GetChildNode(0)
+ .Evaluate()
+ .ToFloatValue());
+ }, null, null);
+
+ ///
+ /// Standard binary add operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ADD = new BasicTemplate(6,
+ "+({f,i,s}{f,i,s}):{f,i,s}",
+ NodeType.OperatorLeft, false, 0,
+ (actual) =>
+ {
+ return
+ EvaluateExpr.Add(
+ actual.GetChildNode(0)
+ .Evaluate(),
+ actual
+ .GetChildNode(1)
+ .Evaluate());
+ }, null, null);
+
+ ///
+ /// Standard binary sub operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_SUB = new BasicTemplate(6,
+ "-({f,i}{f,i}):{f,i}",
+ NodeType.OperatorLeft, false, 0,
+ (actual) =>
+ {
+ return
+ EvaluateExpr.Sub(
+ actual.GetChildNode(0)
+ .Evaluate(),
+ actual
+ .GetChildNode(1)
+ .Evaluate());
+ }, null, null);
+
+ ///
+ /// Standard binary multiply operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_MUL = new BasicTemplate(5,
+ "*({f,i}{f,i}):{f,i}",
+ NodeType.OperatorLeft, false, 0,
+ (actual) =>
+ {
+ return
+ EvaluateExpr.Mul(
+ actual.GetChildNode(0)
+ .Evaluate(),
+ actual
+ .GetChildNode(1)
+ .Evaluate());
+ }, null, null);
+
+ ///
+ /// Standard binary div operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_DIV = new BasicTemplate(5,
+ "/({f,i}{f,i}):{f,i}",
+ NodeType.OperatorLeft, false, 0,
+ (actual) =>
+ {
+ return
+ EvaluateExpr.Div(
+ actual.GetChildNode(0)
+ .Evaluate(),
+ actual
+ .GetChildNode(1)
+ .Evaluate());
+ }, null, null);
+
+
+ ///
+ /// Standard binary protected div operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_PDIV = new BasicTemplate(
+ 5, "%({f,i}{f,i}):{f,i}", NodeType.OperatorLeft, false, 0,
+ (actual) => EvaluateExpr.ProtectedDiv(actual.GetChildNode(0).Evaluate(),
+ actual.GetChildNode(1).Evaluate())
+ , null, null);
+
+
+ ///
+ /// Standard binary power operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_POWER = new BasicTemplate(
+ 1, "^({f,i}{f,i}):{f,i}", NodeType.OperatorRight, false, 0,
+ (actual) => EvaluateExpr.Pow(actual.GetChildNode(0).Evaluate(), actual.GetChildNode(1).Evaluate()), null,
+ null);
+
+
+ ///
+ /// Standard boolean binary and operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_AND = new BasicTemplate(
+ 10, "&({b}{b}):{b}", NodeType.OperatorLeft, false, 0,
+ (actual) =>
+ {
+ return new ExpressionValue(actual.GetChildNode(0).Evaluate()
+ .ToBooleanValue()
+ && actual.GetChildNode(1).Evaluate().ToBooleanValue());
+ }, null, null);
+
+
+ ///
+ /// Standard boolean binary and operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_NOT = new BasicTemplate(3,
+ "!({b}):{b}", NodeType.Unary, false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(
+ !actual.GetChildNode(0)
+ .Evaluate()
+ .ToBooleanValue
+ ());
+ }, null, null);
+
+ ///
+ /// Standard boolean binary or operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_OR = new BasicTemplate(12,
+ "|({b}{b}):{b}", NodeType.OperatorLeft,
+ false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(actual
+ .GetChildNode
+ (0)
+ .Evaluate
+ ()
+ .ToBooleanValue
+ ()
+ ||
+ actual
+ .GetChildNode
+ (1)
+ .Evaluate
+ ()
+ .ToBooleanValue
+ ());
+ }, null, null);
+
+
+ ///
+ /// Standard boolean binary equal operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_EQUAL = new BasicTemplate(
+ 9, "=({*}{*}):{b}", NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return EvaluateExpr.Equ(actual.GetChildNode(0).Evaluate(), actual
+ .GetChildNode(1).Evaluate());
+ }, null, null);
+
+
+ ///
+ /// Standard boolean not equal operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_NOT_EQUAL = new BasicTemplate(
+ 9, "<>({*}{*}):{b}", NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return EvaluateExpr.Notequ(actual.GetChildNode(0).Evaluate(),
+ actual.GetChildNode(1).Evaluate());
+ }, null, null);
+
+ ///
+ /// Standard boolean binary greater than operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_GT = new BasicTemplate(8,
+ ">({i,f}{i,f}):{b}",
+ NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(actual
+ .GetChildNode
+ (0)
+ .Evaluate
+ ()
+ .ToFloatValue
+ () >
+ actual
+ .GetChildNode
+ (1)
+ .Evaluate
+ ()
+ .ToFloatValue
+ ());
+ }, null, null);
+
+ ///
+ /// Standard boolean binary less than operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LT = new BasicTemplate(8,
+ "<({i,f}{i,f}):{b}",
+ NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(actual
+ .GetChildNode
+ (0)
+ .Evaluate
+ ()
+ .ToFloatValue
+ () <
+ actual
+ .GetChildNode
+ (1)
+ .Evaluate
+ ()
+ .ToFloatValue
+ ());
+ }, null, null);
+
+
+ ///
+ /// Standard boolean binary greater than operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_GTE = new BasicTemplate(8,
+ ">=({i,f}{i,f}):{b}",
+ NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(actual
+ .GetChildNode
+ (0)
+ .Evaluate
+ ()
+ .ToFloatValue
+ () >=
+ actual
+ .GetChildNode
+ (1)
+ .Evaluate
+ ()
+ .ToFloatValue
+ ());
+ }, null, null);
+
+ ///
+ /// Standard boolean binary less than operator.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LTE = new BasicTemplate(8,
+ "<=({i,f}{i,f}):{b}",
+ NodeType.OperatorRight, false, 0,
+ (actual) =>
+ {
+ return
+ new ExpressionValue(actual
+ .GetChildNode
+ (0)
+ .Evaluate
+ ()
+ .ToFloatValue
+ () <=
+ actual
+ .GetChildNode
+ (1)
+ .Evaluate
+ ()
+ .ToFloatValue
+ ());
+ }, null, null);
+
+
+ ///
+ /// Standard numeric absolute value function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ABS = new BasicTemplate(
+ "abs({f,i}):{f,i}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Abs(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric acos function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ACOS = new BasicTemplate(
+ "acos({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Acos(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric asin function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ASIN = new BasicTemplate(
+ "asin({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Asin(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric atan function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ATAN = new BasicTemplate(
+ "atan({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Atan(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric atan2 function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ATAN2 = new BasicTemplate(
+ "atan2({f}{f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Atan2(
+ actual.GetChildNode(0).Evaluate().ToFloatValue(),
+ actual.GetChildNode(1).Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric ceil function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CEIL = new BasicTemplate(
+ "ceil({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Ceiling(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric cos function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_COS = new BasicTemplate(
+ "cos({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Cos(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric cosh function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_COSH = new BasicTemplate(
+ "cosh({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Cosh(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric exp function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_EXP = new BasicTemplate(
+ "exp({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Exp(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric floor function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_FLOOR = new BasicTemplate(
+ "floor({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Floor(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric log function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LOG = new BasicTemplate(
+ "log({f}):{f}", (actual) =>
+ {
+ return new ExpressionValue(Math.Log(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric log10 function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LOG10 = new BasicTemplate(
+ "log10({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Log10(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric max function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_MAX = new BasicTemplate(
+ "max({f,s,i}({f,s,i}):{f,s,i}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Max(
+ actual.GetChildNode(0).Evaluate().ToFloatValue(),
+ actual.GetChildNode(1).Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric max function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_MIN = new BasicTemplate(
+ "min({f,s,i}({f,s,i}):{f,s,i}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Min(
+ actual.GetChildNode(0).Evaluate().ToFloatValue(),
+ actual.GetChildNode(1).Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric pow function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_POWFN = new BasicTemplate(
+ "pow({f}{f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Pow(
+ actual.GetChildNode(0).Evaluate().ToFloatValue(),
+ actual.GetChildNode(1).Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric random function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_RANDOM = new BasicTemplate(
+ "rand():{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Log(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric log10 function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_ROUND = new BasicTemplate(
+ "round({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Round(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric sin function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_SIN = new BasicTemplate(
+ "sin({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Sin(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric sinh function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_SINH = new BasicTemplate(
+ "sinh({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Sinh(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric sqrt function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_SQRT = new BasicTemplate(
+ "sqrt({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Sqrt(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric tan function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_TAN = new BasicTemplate(
+ "tan({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Tan(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric tanh function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_TANH = new BasicTemplate(
+ "tanh({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Tanh(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard numeric toDegrees function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_TODEG = new BasicTemplate(
+ "todeg({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Sin(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+
+ ///
+ /// Standard numeric toRadians function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_TORAD = new BasicTemplate(
+ "torad({f}):{f}",
+ (actual) =>
+ {
+ return new ExpressionValue(Math.Log(actual.GetChildNode(0)
+ .Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// Standard string length function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LENGTH = new BasicTemplate(
+ "length({s}):{i}",
+ (actual) => { return new ExpressionValue(actual.GetChildNode(0).Evaluate().ToStringValue().Length); });
+
+ ///
+ /// Numeric formatting function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_FORMAT = new BasicTemplate(
+ "format({f}{i}):{s}",
+ (actual) =>
+ {
+ return new ExpressionValue(actual.Owner.Context.Format
+ .Format(actual.GetChildNode(0).Evaluate().ToFloatValue(),
+ (int) actual.GetChildNode(1).Evaluate().ToFloatValue()));
+ });
+
+ ///
+ /// String left function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_LEFT = new BasicTemplate(
+ "left({s}{i}):{s}",
+ (actual) =>
+ {
+ String str = actual.GetChildNode(0).Evaluate().ToStringValue();
+ var idx = (int) actual.GetChildNode(1).Evaluate().ToFloatValue();
+ String result = str.Substring(0, idx);
+ return new ExpressionValue(result);
+ });
+
+
+ ///
+ /// String right function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_RIGHT = new BasicTemplate(
+ "right({s}{i}):{s}",
+ (actual) =>
+ {
+ String str = actual.GetChildNode(0).Evaluate().ToStringValue();
+ var idx = (int) actual.GetChildNode(1).Evaluate().ToFloatValue();
+ String result = str.Substring(idx);
+ return new ExpressionValue(result);
+ });
+
+
+ ///
+ /// Standard string cint function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CINT = new BasicTemplate(
+ "cint({f}):{i}",
+ (actual) => { return new ExpressionValue(actual.GetChildNode(0).Evaluate().ToIntValue()); });
+
+
+ ///
+ /// Standard string cfloat function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CFLOAT = new BasicTemplate(
+ "cfloat({i}):{f}",
+ (actual) => { return new ExpressionValue(actual.GetChildNode(0).Evaluate().ToFloatValue()); });
+
+ ///
+ /// Standard string cstr function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CSTR = new BasicTemplate(
+ "cstr({*}):{s}",
+ (actual) => { return new ExpressionValue(actual.GetChildNode(0).Evaluate().ToStringValue()); });
+
+ ///
+ /// Standard string cbool function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CBOOL = new BasicTemplate(
+ "cbool({i,f}):{b}",
+ (actual) => { return new ExpressionValue(actual.GetChildNode(0).Evaluate().ToBooleanValue()); });
+
+
+ ///
+ /// Standard string iff function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_IFF = new BasicTemplate(
+ "iff({b}:{*}:{*}):{*}",
+ (actual) =>
+ {
+ bool a = actual.GetChildNode(0).Evaluate().ToBooleanValue();
+ if (a)
+ {
+ return actual.GetChildNode(1).Evaluate();
+ }
+ else
+ {
+ return actual.GetChildNode(2).Evaluate();
+ }
+ });
+
+
+ ///
+ /// Standard string clamp function.
+ ///
+ public static IProgramExtensionTemplate EXTENSION_CLAMP = new BasicTemplate(
+ "clamp({f}{f}{f}):{f}",
+ (actual) =>
+ {
+ bool a = actual.GetChildNode(0).Evaluate().ToBooleanValue();
+ if (a)
+ {
+ return actual.GetChildNode(1).Evaluate();
+ }
+ else
+ {
+ return actual.GetChildNode(2).Evaluate();
+ }
+ });
+
+ ///
+ /// Add all known opcodes to a context.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateAll(EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ foreach (IProgramExtensionTemplate temp in EncogOpcodeRegistry.Instance
+ .FindAllOpcodes())
+ {
+ factory.AddExtension(temp);
+ }
+ }
+
+ ///
+ /// Add the opcodes for basic operations to a context.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateBasicFunctions(EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_ABS);
+ factory.AddExtension(EXTENSION_CEIL);
+ factory.AddExtension(EXTENSION_EXP);
+ factory.AddExtension(EXTENSION_FLOOR);
+ factory.AddExtension(EXTENSION_LOG);
+ factory.AddExtension(EXTENSION_LOG10);
+ factory.AddExtension(EXTENSION_MAX);
+ factory.AddExtension(EXTENSION_MIN);
+ factory.AddExtension(EXTENSION_POWFN);
+ factory.AddExtension(EXTENSION_RANDOM);
+ factory.AddExtension(EXTENSION_ROUND);
+ factory.AddExtension(EXTENSION_SQRT);
+ factory.AddExtension(EXTENSION_CLAMP);
+ }
+
+ ///
+ /// Add the opcodes for boolean operations to a context.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateBooleanOperators(EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_AND);
+ factory.AddExtension(EXTENSION_OR);
+ factory.AddExtension(EXTENSION_EQUAL);
+ factory.AddExtension(EXTENSION_LT);
+ factory.AddExtension(EXTENSION_GT);
+ factory.AddExtension(EXTENSION_LTE);
+ factory.AddExtension(EXTENSION_GTE);
+ factory.AddExtension(EXTENSION_IFF);
+ factory.AddExtension(EXTENSION_NOT_EQUAL);
+ factory.AddExtension(EXTENSION_NOT);
+ }
+
+ ///
+ /// Add the opcodes for type conversion operations to a context.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateConversionFunctions(
+ EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_CINT);
+ factory.AddExtension(EXTENSION_CFLOAT);
+ factory.AddExtension(EXTENSION_CSTR);
+ factory.AddExtension(EXTENSION_CBOOL);
+ }
+
+ ///
+ /// Add the opcodes for numeric operations to a context, do not use protected
+ /// division.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateNumericOperators(EncogProgramContext context)
+ {
+ CreateNumericOperators(context, false);
+ }
+
+ ///
+ /// Add the opcodes for numeric operations to a context.
+ ///
+ /// The context to add the opcodes to.
+ /// Should protected division be used.
+ public static void CreateNumericOperators(
+ EncogProgramContext context, bool protectedDiv)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_VAR_SUPPORT);
+ factory.AddExtension(EXTENSION_CONST_SUPPORT);
+ factory.AddExtension(EXTENSION_NEG);
+ factory.AddExtension(EXTENSION_ADD);
+ factory.AddExtension(EXTENSION_SUB);
+ factory.AddExtension(EXTENSION_MUL);
+ if (protectedDiv)
+ {
+ factory.AddExtension(EXTENSION_PDIV);
+ }
+ else
+ {
+ factory.AddExtension(EXTENSION_DIV);
+ }
+ factory.AddExtension(EXTENSION_POWER);
+ }
+
+ ///
+ /// Add the opcodes for string operations to a context.
+ ///
+ ///
+ ///
+ public static void CreateStringFunctions(EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_LENGTH);
+ factory.AddExtension(EXTENSION_FORMAT);
+ factory.AddExtension(EXTENSION_LEFT);
+ factory.AddExtension(EXTENSION_RIGHT);
+ }
+
+ ///
+ /// Add the opcodes for trig functions operations to a context.
+ ///
+ /// The context to add the opcodes to.
+ public static void CreateTrigFunctions(EncogProgramContext context)
+ {
+ FunctionFactory factory = context.Functions;
+ factory.AddExtension(EXTENSION_ACOS);
+ factory.AddExtension(EXTENSION_ASIN);
+ factory.AddExtension(EXTENSION_ATAN);
+ factory.AddExtension(EXTENSION_ATAN2);
+ factory.AddExtension(EXTENSION_COS);
+ factory.AddExtension(EXTENSION_COSH);
+ factory.AddExtension(EXTENSION_SIN);
+ factory.AddExtension(EXTENSION_SINH);
+ factory.AddExtension(EXTENSION_TAN);
+ factory.AddExtension(EXTENSION_TANH);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Generator/AbstractPrgGenerator.cs b/encog-core-cs/ML/Prg/Generator/AbstractPrgGenerator.cs
new file mode 100644
index 00000000..d44fc127
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Generator/AbstractPrgGenerator.cs
@@ -0,0 +1,394 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Encog.ML.EA.Exceptions;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Species;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.ML.Prg.Train;
+using Encog.MathUtil.Randomize;
+using Encog.MathUtil.Randomize.Factory;
+using Encog.Neural.Networks.Training;
+using Encog.Util.Concurrency;
+
+namespace Encog.ML.Prg.Generator
+{
+ ///
+ /// The abstract base for Full and Grow program generation.
+ ///
+ public abstract class AbstractPrgGenerator : IPrgGenerator, IMultiThreadable
+ {
+ ///
+ /// The contents of this population, stored in rendered form. This prevents
+ /// duplicates.
+ ///
+ private readonly HashSet _contents = new HashSet();
+
+ ///
+ /// The program context to use.
+ ///
+ private readonly EncogProgramContext _context;
+
+ ///
+ /// True, if the program has enums.
+ ///
+ private readonly bool _hasEnum;
+
+ ///
+ /// The maximum depth to generate to.
+ ///
+ private readonly int _maxDepth;
+
+ ///
+ /// Construct the generator.
+ ///
+ /// The context that is to be used for generation.
+ /// The maximum depth to generate to.
+ protected AbstractPrgGenerator(EncogProgramContext theContext,
+ int theMaxDepth)
+ {
+ if (theContext.Functions.Count == 0)
+ {
+ throw new EncogError("There are no opcodes defined");
+ }
+
+ _context = theContext;
+ _maxDepth = theMaxDepth;
+ _hasEnum = _context.HasEnum;
+ Score = new ZeroEvalScoreFunction();
+ MinConst = -10;
+ MaxConst = 10;
+ RandomFactory = EncogFramework.Instance.RandomFactory.FactorFactory();
+ MaxGenerationErrors = 500;
+ }
+
+ ///
+ /// An optional scoring function.
+ ///
+ public ICalculateScore Score { get; set; }
+
+ ///
+ /// The minimum const to generate.
+ ///
+ public double MinConst { get; set; }
+
+ ///
+ /// The maximum const to generate.
+ ///
+ public double MaxConst { get; set; }
+
+ ///
+ /// A random number generator factory.
+ ///
+ public IRandomFactory RandomFactory { get; set; }
+
+ ///
+ /// The context.
+ ///
+ public EncogProgramContext Context
+ {
+ get { return _context; }
+ }
+
+ ///
+ /// The max depth.
+ ///
+ public int MaxDepth
+ {
+ get { return _maxDepth; }
+ }
+
+ ///
+ /// Do we have enums?
+ ///
+ public bool HasEnum
+ {
+ get { return _hasEnum; }
+ }
+
+ ///
+ /// The number of threads to use.
+ ///
+ public int ThreadCount { get; set; }
+
+ ///
+ /// The maximum number of allowed generation errors.
+ ///
+ public int MaxGenerationErrors { get; set; }
+
+ ///
+ public IGenome Generate(EncogRandom rnd)
+ {
+ var program = new EncogProgram(_context);
+ IList types = new List();
+ types.Add(_context.Result.VariableType);
+ program.RootNode = CreateNode(rnd, program, DetermineMaxDepth(rnd),
+ types);
+ return program;
+ }
+
+ ///
+ public void Generate(EncogRandom rnd, IPopulation pop)
+ {
+ // prepare population
+ _contents.Clear();
+ pop.Species.Clear();
+ ISpecies defaultSpecies = pop.CreateSpecies();
+
+ if (Score.RequireSingleThreaded || ThreadCount == 1)
+ {
+ for (int i = 0; i < pop.PopulationSize; i++)
+ {
+ EncogProgram prg = AttemptCreateGenome(rnd, pop);
+ AddPopulationMember(pop, prg);
+ }
+ }
+ else
+ {
+ Parallel.For(0, pop.PopulationSize, i =>
+ {
+ EncogProgram prg = AttemptCreateGenome(rnd, pop);
+ AddPopulationMember(pop, prg);
+ });
+ }
+
+ // just pick a leader, for the default species.
+ defaultSpecies.Leader = defaultSpecies.Members[0];
+ }
+
+ ///
+ public abstract ProgramNode CreateNode(EncogRandom rnd, EncogProgram program, int depthRemaining,
+ IList types);
+
+ ///
+ /// Add a population member from one of the threads.
+ ///
+ /// The population to add to.
+ /// The program to add.
+ public void AddPopulationMember(IPopulation population,
+ EncogProgram prg)
+ {
+ lock (this)
+ {
+ ISpecies defaultSpecies = population.Species[0];
+ prg.Species = defaultSpecies;
+ defaultSpecies.Add(prg);
+ _contents.Add(prg.DumpAsCommonExpression());
+ }
+ }
+
+ ///
+ /// Attempt to create a genome. Cycle the specified number of times if an
+ /// error occurs.
+ ///
+ /// The random number generator.
+ /// The population.
+ /// The generated genome.
+ public EncogProgram AttemptCreateGenome(EncogRandom rnd,
+ IPopulation pop)
+ {
+ bool done = false;
+ EncogProgram result = null;
+ int tries = MaxGenerationErrors;
+
+ while (!done)
+ {
+ result = (EncogProgram) Generate(rnd);
+ result.Population = pop;
+
+ double s;
+ try
+ {
+ tries--;
+ s = Score.CalculateScore(result);
+ }
+ catch (EARuntimeError)
+ {
+ s = double.NaN;
+ }
+
+ if (tries < 0)
+ {
+ throw new EncogError("Could not generate a valid genome after "
+ + MaxGenerationErrors + " tries.");
+ }
+ if (!Double.IsNaN(s) && !Double.IsInfinity(s)
+ && !_contents.Contains(result.DumpAsCommonExpression()))
+ {
+ done = true;
+ }
+ }
+
+ return result;
+ }
+
+ ///
+ /// Create a random note according to the specified paramaters.
+ ///
+ /// A random number generator.
+ /// The program to generate for.
+ /// The depth remaining to generate.
+ /// The types to generate.
+ /// Should we include terminal nodes.
+ /// Should we include function nodes.
+ /// The generated program node.
+ public ProgramNode CreateRandomNode(EncogRandom rnd,
+ EncogProgram program, int depthRemaining,
+ IList types, bool includeTerminal,
+ bool includeFunction)
+ {
+ // if we've hit the max depth, then create a terminal nodes, so it stops
+ // here
+ if (depthRemaining == 0)
+ {
+ return CreateTerminalNode(rnd, program, types);
+ }
+
+ // choose which opcode set we might create the node from
+ IList opcodeSet = Context.Functions.FindOpcodes(types, Context,
+ includeTerminal, includeFunction);
+
+ // choose a random opcode
+ IProgramExtensionTemplate temp = GenerateRandomOpcode(rnd,
+ opcodeSet);
+ if (temp == null)
+ {
+ throw new EACompileError(
+ "Trying to generate a random opcode when no opcodes exist.");
+ }
+
+ // create the child nodes
+ int childNodeCount = temp.ChildNodeCount;
+ var children = new ProgramNode[childNodeCount];
+
+ if (EncogOpcodeRegistry.IsOperator(temp.NodeType) && children.Length >= 2)
+ {
+ // for an operator of size 2 or greater make sure all children are
+ // the same time
+ IList childTypes = temp.Params[0]
+ .DetermineArgumentTypes(types);
+ EPLValueType selectedType = childTypes[rnd
+ .Next(childTypes.Count)];
+ childTypes.Clear();
+ childTypes.Add(selectedType);
+
+ // now create the children of a common type
+ for (int i = 0; i < children.Length; i++)
+ {
+ children[i] = CreateNode(rnd, program, depthRemaining - 1,
+ childTypes);
+ }
+ }
+ else
+ {
+ // otherwise, let the children have their own types
+ for (int i = 0; i < children.Length; i++)
+ {
+ IList childTypes = temp.Params[i]
+ .DetermineArgumentTypes(types);
+ children[i] = CreateNode(rnd, program, depthRemaining - 1,
+ childTypes);
+ }
+ }
+
+ // now actually create the node
+ var result = new ProgramNode(program, temp, children);
+ temp.Randomize(rnd, types, result, MinConst, MaxConst);
+ return result;
+ }
+
+ ///
+ /// Create a terminal node.
+ ///
+ /// A random number generator.
+ /// The program to generate for.
+ /// The types that we might generate.
+ /// The terminal program node.
+ public ProgramNode CreateTerminalNode(EncogRandom rnd,
+ EncogProgram program, IList types)
+ {
+ IProgramExtensionTemplate temp = GenerateRandomOpcode(
+ rnd,
+ Context.Functions.FindOpcodes(types, _context,
+ true, false));
+ if (temp == null)
+ {
+ throw new EACompileError("No opcodes exist for the type: "
+ + types);
+ }
+ var result = new ProgramNode(program, temp,
+ new ProgramNode[] {});
+
+ temp.Randomize(rnd, types, result, MinConst, MaxConst);
+ return result;
+ }
+
+ ///
+ /// Determine the max depth.
+ ///
+ /// Random number generator.
+ /// The max depth.
+ public virtual int DetermineMaxDepth(EncogRandom rnd)
+ {
+ return _maxDepth;
+ }
+
+ ///
+ /// Generate a random opcode.
+ ///
+ /// Random number generator.
+ /// The opcodes to choose from.
+ /// The selected opcode.
+ public IProgramExtensionTemplate GenerateRandomOpcode(EncogRandom rnd,
+ IList opcodes)
+ {
+ int maxOpCode = opcodes.Count;
+
+ if (maxOpCode == 0)
+ {
+ return null;
+ }
+
+ int tries = 10000;
+
+ IProgramExtensionTemplate result = null;
+
+ while (result == null)
+ {
+ int opcode = rnd.Next(maxOpCode);
+ result = opcodes[opcode];
+ tries--;
+ if (tries < 0)
+ {
+ throw new EACompileError(
+ "Could not generate an opcode. Make sure you have valid opcodes defined.");
+ }
+ }
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Generator/IPrgGenerator.cs b/encog-core-cs/ML/Prg/Generator/IPrgGenerator.cs
new file mode 100644
index 00000000..80fcc378
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Generator/IPrgGenerator.cs
@@ -0,0 +1,51 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Population;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Generator
+{
+ ///
+ /// Generate a random Encog Program.
+ ///
+ public interface IPrgGenerator : IPopulationGenerator
+ {
+ ///
+ /// The maximum number of errors to allow during generation.
+ ///
+ int MaxGenerationErrors { get; set; }
+
+ ///
+ /// Create a random node for an Encog Program.
+ ///
+ /// Random number generator.
+ /// The program that the node should be generated for.
+ /// The depth remaining to generate.
+ /// The types to generate.
+ /// The newly created node.
+ ProgramNode CreateNode(EncogRandom rnd, EncogProgram program, int depthRemaining,
+ IList types);
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Generator/PrgFullGenerator.cs b/encog-core-cs/ML/Prg/Generator/PrgFullGenerator.cs
new file mode 100644
index 00000000..cf456175
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Generator/PrgFullGenerator.cs
@@ -0,0 +1,59 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Generator
+{
+ ///
+ /// The full generator works by creating program trees that do not stop
+ /// prematurely. To do this a node is randomly selected from the "function set"
+ /// until the tree reaches the maximum depth. Once the tree reaches the maximum
+ /// depth only nodes from the "terminal set" are chosen.
+ /// This algorithm was implemented as described in the following publication:
+ /// Genetic programming: on the programming of computers by means of natural
+ /// selection MIT Press Cambridge, MA, USA (c)1992 ISBN:0-262-11170-5
+ ///
+ public class PrgFullGenerator : AbstractPrgGenerator
+ {
+ ///
+ /// Construct the full generator.
+ ///
+ /// The context.
+ /// The max depth.
+ public PrgFullGenerator(EncogProgramContext theContext,
+ int theMaxDepth)
+ : base(theContext, theMaxDepth)
+ {
+ }
+
+ ///
+ public override ProgramNode CreateNode(EncogRandom rnd, EncogProgram program,
+ int depthRemaining, IList types)
+ {
+ return CreateRandomNode(rnd, program, depthRemaining, types, false,
+ true);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Generator/PrgGrowGenerator.cs b/encog-core-cs/ML/Prg/Generator/PrgGrowGenerator.cs
new file mode 100644
index 00000000..3952e2c1
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Generator/PrgGrowGenerator.cs
@@ -0,0 +1,57 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Generator
+{
+ ///
+ /// The grow generator creates a random program by choosing a random node from
+ /// both the "function and terminal" sets until the maximum depth is reached.
+ /// Once the maximum depth is reached only nodes from terminal set are chosen.
+ /// This algorithm was implemented as described in the following publication:
+ /// Genetic programming: on the programming of computers by means of natural
+ /// selection MIT Press Cambridge, MA, USA (c)1992 ISBN:0-262-11170-5
+ ///
+ public class PrgGrowGenerator : AbstractPrgGenerator
+ {
+ ///
+ /// Construct the grow generator.
+ ///
+ /// The program context.
+ /// The max depth.
+ public PrgGrowGenerator(EncogProgramContext theContext,
+ int theMaxDepth)
+ : base(theContext, theMaxDepth)
+ {
+ }
+
+ ///
+ public override ProgramNode CreateNode(EncogRandom rnd, EncogProgram program,
+ int depthRemaining, IList types)
+ {
+ return CreateRandomNode(rnd, program, depthRemaining, types, true, true);
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Generator/RampedHalfAndHalf.cs b/encog-core-cs/ML/Prg/Generator/RampedHalfAndHalf.cs
new file mode 100644
index 00000000..86ed9b6c
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Generator/RampedHalfAndHalf.cs
@@ -0,0 +1,103 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Generator
+{
+ ///
+ /// Because neither the grow or full method provide a very wide array of sizes or
+ /// shapes on their own, Koza (1992) proposed a combination called ramped
+ /// half-and-half. Half the initial population is constructed using full and half
+ /// is constructed using grow. This is done using a range of depth limits (hence
+ /// the term "ramped") to help ensure that we generate trees having a variety of
+ /// sizes and shapes. (from: A field guide to genetic programming)
+ /// This algorithm was implemented as described in the following publication:
+ /// Genetic programming: on the programming of computers by means of natural
+ /// selection MIT Press Cambridge, MA, USA (c)1992 ISBN:0-262-11170-5
+ ///
+ public class RampedHalfAndHalf : AbstractPrgGenerator
+ {
+ ///
+ /// The full generator.
+ ///
+ private readonly PrgFullGenerator _fullGenerator;
+
+ ///
+ /// The grow generator.
+ ///
+ private readonly PrgGrowGenerator _growGenerator;
+
+ ///
+ /// The minimum depth.
+ ///
+ private readonly int _minDepth;
+
+ ///
+ /// Construct the ramped half-and-half generator.
+ ///
+ /// The context.
+ /// The minimum depth.
+ /// The maximum depth.
+ public RampedHalfAndHalf(EncogProgramContext theContext,
+ int theMinDepth, int theMaxDepth)
+ : base(theContext, theMaxDepth)
+ {
+ _minDepth = theMinDepth;
+
+ _fullGenerator = new PrgFullGenerator(theContext, theMaxDepth);
+ _growGenerator = new PrgGrowGenerator(theContext, theMaxDepth);
+ }
+
+ ///
+ /// The minimum depth.
+ ///
+ public int MinDepth
+ {
+ get { return _minDepth; }
+ }
+
+ ///
+ public override ProgramNode CreateNode(EncogRandom rnd, EncogProgram program,
+ int depthRemaining, IList types)
+ {
+ int actualDepthRemaining = depthRemaining;
+
+ if (rnd.NextDouble() > 0.5)
+ {
+ return _fullGenerator.CreateNode(rnd, program,
+ actualDepthRemaining, types);
+ }
+ return _growGenerator.CreateNode(rnd, program,
+ actualDepthRemaining, types);
+ }
+
+ ///
+ public override int DetermineMaxDepth(EncogRandom rnd)
+ {
+ int range = MaxDepth - _minDepth;
+ return rnd.Next(range) + _minDepth;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Opp/ConstMutation.cs b/encog-core-cs/ML/Prg/Opp/ConstMutation.cs
new file mode 100644
index 00000000..6741523a
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Opp/ConstMutation.cs
@@ -0,0 +1,120 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Train;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.ML.Tree;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Opp
+{
+ ///
+ /// Mutate the constant nodes of an Encog program. This mutation only changes
+ /// values and does not alter the structure.
+ ///
+ public class ConstMutation : IEvolutionaryOperator
+ {
+ ///
+ /// The frequency that constant nodes are mutated with.
+ ///
+ private readonly double _frequency;
+
+ ///
+ /// The sigma value used to generate gaussian random numbers.
+ ///
+ private readonly double _sigma;
+
+ ///
+ /// Construct a const mutator.
+ ///
+ /// The program context.
+ /// The frequency of mutation.
+ /// The sigma to use for mutation.
+ public ConstMutation(EncogProgramContext theContext,
+ double theFrequency, double theSigma)
+ {
+ _frequency = theFrequency;
+ _sigma = theSigma;
+ }
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ }
+
+ ///
+ public int OffspringProduced
+ {
+ get { return 1; }
+ }
+
+ ///
+ public int ParentsNeeded
+ {
+ get { return 1; }
+ }
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents,
+ int parentIndex, IGenome[] offspring,
+ int offspringIndex)
+ {
+ var program = (EncogProgram) parents[0];
+ EncogProgramContext context = program.Context;
+ EncogProgram result = context.CloneProgram(program);
+ MutateNode(rnd, result.RootNode);
+ offspring[0] = result;
+ }
+
+ ///
+ /// Called for each node in the progrmam. If this is a const node, then
+ /// mutate it according to the frequency and sigma specified.
+ ///
+ /// Random number generator.
+ /// The node to mutate.
+ private void MutateNode(EncogRandom rnd, ProgramNode node)
+ {
+ if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ if (rnd.NextDouble() < _frequency)
+ {
+ ExpressionValue v = node.Data[0];
+ if (v.IsFloat)
+ {
+ double adj = rnd.NextGaussian()*_sigma;
+ node.Data[0] = new ExpressionValue(v.ToFloatValue()
+ + adj);
+ }
+ }
+ }
+
+ foreach (ITreeNode n in node.ChildNodes)
+ {
+ var childNode = (ProgramNode) n;
+ MutateNode(rnd, childNode);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Opp/LevelHolder.cs b/encog-core-cs/ML/Prg/Opp/LevelHolder.cs
new file mode 100644
index 00000000..849e43c2
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Opp/LevelHolder.cs
@@ -0,0 +1,81 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using System.Linq;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg.Opp
+{
+ ///
+ /// The level holder class is passed down as a tree is mutated. The level holder
+ /// class is initially given the desired output of the program and tracks the
+ /// desired output for each of the nodes. This allows for type-safe crossovers
+ /// and mutations.
+ ///
+ public class LevelHolder
+ {
+ ///
+ /// Construct the level holder.
+ ///
+ /// The level to construct the holder for.
+ public LevelHolder(int theCurrentLevel)
+ {
+ CurrentLevel = theCurrentLevel;
+ }
+
+ ///
+ /// The current level in the tree.
+ ///
+ public int CurrentLevel { get; set; }
+
+ ///
+ /// The current node, or node found. This will be the mutation or crossover point.
+ ///
+ public ProgramNode NodeFound { get; set; }
+
+ ///
+ /// The types we are expecting at this level.
+ ///
+ public IList Types { get; set; }
+
+ ///
+ /// Determine if the specified child types are compatible with the parent types.
+ ///
+ /// The parent types.
+ /// The child types.
+ /// True, if compatible.
+ public static bool CompatibleTypes(IList parentTypes,
+ IList childTypes)
+ {
+ return childTypes.All(parentTypes.Contains);
+ }
+
+ ///
+ /// Decrease the level.
+ ///
+ public void DecreaseLevel()
+ {
+ CurrentLevel--;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Opp/SubtreeCrossover.cs b/encog-core-cs/ML/Prg/Opp/SubtreeCrossover.cs
new file mode 100644
index 00000000..98945600
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Opp/SubtreeCrossover.cs
@@ -0,0 +1,146 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Train;
+using Encog.ML.Prg.ExpValue;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Opp
+{
+ ///
+ /// Perform a type-safe subtree crossover. The crossover points will be chosen
+ /// randomly but must be type-safe. The first parent will be cloned to produce
+ /// the child. The tree formed from the crossover point of the second child will
+ /// be copied and grafted into the parent's clone and its crossover point.
+ ///
+ public class SubtreeCrossover : IEvolutionaryOperator
+ {
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+
+ }
+
+ ///
+ /// Returns the number of offspring produced. In this case, one.
+ ///
+ public int OffspringProduced
+ {
+ get { return 1; }
+ }
+
+ ///
+ /// Returns the number of parents needed. In this case, two.
+ ///
+ public int ParentsNeeded
+ {
+ get { return 2; }
+ }
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents,
+ int parentIndex, IGenome[] offspring,
+ int offspringIndex)
+ {
+ var parent1 = (EncogProgram) parents[0];
+ var parent2 = (EncogProgram) parents[1];
+ offspring[0] = null;
+
+ EncogProgramContext context = parent1.Context;
+ int size1 = parent1.RootNode.Count;
+ int size2 = parent2.RootNode.Count;
+
+ bool done = false;
+ int tries = 100;
+
+ while (!done)
+ {
+ int p1Index = rnd.Next(size1);
+ int p2Index = rnd.Next(size2);
+
+ var holder1 = new LevelHolder(p1Index);
+ var holder2 = new LevelHolder(p2Index);
+
+ IList types = new List();
+ types.Add(context.Result.VariableType);
+
+ FindNode(rnd, parent1.RootNode, types, holder1);
+ FindNode(rnd, parent2.RootNode, types, holder2);
+
+ if (LevelHolder.CompatibleTypes(holder1.Types,
+ holder2.Types))
+ {
+ EncogProgram result = context.CloneProgram(parent1);
+ ProgramNode resultNode = parent1.FindNode(p1Index);
+ ProgramNode p2Node = parent2.FindNode(p2Index);
+ ProgramNode newInsert = context.CloneBranch(result,
+ p2Node);
+ result.ReplaceNode(resultNode, newInsert);
+ offspring[0] = result;
+ done = true;
+ }
+ else
+ {
+ tries--;
+ if (tries < 0)
+ {
+ done = true;
+ }
+ }
+ }
+ }
+
+ ///
+ /// This method is called reflexivly as we iterate downward. Once we reach
+ /// the desired point (when current level drops to zero), the operation is
+ /// performed.
+ ///
+ /// A random number generator.
+ /// The parent node.
+ /// The desired node.
+ /// The level holder.
+ private void FindNode(EncogRandom rnd, ProgramNode parentNode,
+ IList types, LevelHolder holder)
+ {
+ if (holder.CurrentLevel == 0)
+ {
+ holder.DecreaseLevel();
+ holder.Types = types;
+ holder.NodeFound = parentNode;
+ }
+ else
+ {
+ holder.DecreaseLevel();
+ for (int i = 0; i < parentNode.Template.ChildNodeCount; i++)
+ {
+ ProgramNode childNode = parentNode.GetChildNode(i);
+ IList childTypes = parentNode.Template
+ .Params[i].DetermineArgumentTypes(types);
+ FindNode(rnd, childNode, childTypes, holder);
+ }
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Opp/SubtreeMutation.cs b/encog-core-cs/ML/Prg/Opp/SubtreeMutation.cs
new file mode 100644
index 00000000..aa3406fa
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Opp/SubtreeMutation.cs
@@ -0,0 +1,136 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System.Collections.Generic;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Opp;
+using Encog.ML.EA.Train;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Generator;
+using Encog.MathUtil.Randomize;
+
+namespace Encog.ML.Prg.Opp
+{
+ ///
+ /// Perform a type-safe subtree mutation. The mutation point is chosen randomly,
+ /// but the new tree will be generated with compatible types to the parent.
+ ///
+ public class SubtreeMutation : IEvolutionaryOperator
+ {
+ ///
+ /// The maximum depth.
+ ///
+ private readonly int _maxDepth;
+
+ ///
+ /// Construct the subtree mutation object.
+ ///
+ /// The program context.
+ /// The maximum depth.
+ public SubtreeMutation(EncogProgramContext theContext,
+ int theMaxDepth)
+ {
+ Generator = new PrgGrowGenerator(theContext, theMaxDepth);
+ _maxDepth = theMaxDepth;
+ }
+
+ ///
+ /// A random generator.
+ ///
+ public IPrgGenerator Generator { get; set; }
+
+
+ ///
+ public void Init(IEvolutionaryAlgorithm theOwner)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ ///
+ /// Returns the number of offspring produced. In this case, one.
+ ///
+ public int OffspringProduced
+ {
+ get { return 1; }
+ }
+
+ ///
+ /// Returns the number of parents needed. In this case, one.
+ ///
+ public int ParentsNeeded
+ {
+ get { return 1; }
+ }
+
+ ///
+ public void PerformOperation(EncogRandom rnd, IGenome[] parents,
+ int parentIndex, IGenome[] offspring,
+ int offspringIndex)
+ {
+ var program = (EncogProgram) parents[0];
+ EncogProgramContext context = program.Context;
+ EncogProgram result = context.CloneProgram(program);
+
+ IList types = new List();
+ types.Add(context.Result.VariableType);
+ var globalIndex = new int[1];
+ globalIndex[0] = rnd.Next(result.RootNode.Count);
+ FindNode(rnd, result, result.RootNode, types, globalIndex);
+
+ offspring[0] = result;
+ }
+
+ ///
+ /// This method is called reflexivly as we iterate downward. Once we reach
+ /// the desired point (when current level drops to zero), the operation is
+ /// performed.
+ ///
+ /// A random number generator.
+ /// The parent node.
+ ///
+ /// The desired node
+ /// The level holder.
+ private void FindNode(EncogRandom rnd, EncogProgram result,
+ ProgramNode parentNode, IList types,
+ int[] globalIndex)
+ {
+ if (globalIndex[0] == 0)
+ {
+ globalIndex[0]--;
+
+ ProgramNode newInsert = Generator.CreateNode(rnd,
+ result, _maxDepth, types);
+ result.ReplaceNode(parentNode, newInsert);
+ }
+ else
+ {
+ globalIndex[0]--;
+ for (int i = 0; i < parentNode.Template.ChildNodeCount; i++)
+ {
+ ProgramNode childNode = parentNode.GetChildNode(i);
+ IList childTypes = parentNode.Template.Params[i].DetermineArgumentTypes(types);
+ FindNode(rnd, result, childNode, childTypes, globalIndex);
+ }
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/PersistPrgPopulation.cs b/encog-core-cs/ML/Prg/PersistPrgPopulation.cs
new file mode 100644
index 00000000..a9f4c6ea
--- /dev/null
+++ b/encog-core-cs/ML/Prg/PersistPrgPopulation.cs
@@ -0,0 +1,319 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Species;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.ML.Prg.Train;
+using Encog.Persist;
+using Encog.Util;
+using Encog.Util.CSV;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// Persist a population of Encog programs.
+ ///
+ public class PersistPrgPopulation : IEncogPersistor
+ {
+ ///
+ public int FileVersion
+ {
+ get { return 1; }
+ }
+
+ ///
+ public String PersistClassString
+ {
+ get { return "PrgPopulation"; }
+ }
+
+ ///
+ public Object Read(Stream istream)
+ {
+ var context = new EncogProgramContext();
+
+ var result = new PrgPopulation(context, 0);
+
+ var reader = new EncogReadHelper(istream);
+ EncogFileSection section;
+
+ int count = 0;
+ ISpecies lastSpecies = null;
+ while ((section = reader.ReadNextSection()) != null)
+ {
+ if (section.SectionName.Equals("BASIC")
+ && section.SubSectionName.Equals("PARAMS"))
+ {
+ IDictionary prms = section.ParseParams();
+ EngineArray.PutAll(prms, result.Properties);
+ }
+ else if (section.SectionName.Equals("BASIC")
+ && section.SubSectionName.Equals("EPL-POPULATION"))
+ {
+ foreach (string line in section.Lines)
+ {
+ IList cols = EncogFileSection.SplitColumns(line);
+
+ if (String.Compare(cols[0], "s", StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ lastSpecies = new BasicSpecies
+ {
+ Age = int.Parse(cols[1]),
+ BestScore = CSVFormat.EgFormat.Parse(cols[2]),
+ Population = result,
+ GensNoImprovement = int.Parse(cols[3])
+ };
+ result.Species.Add(lastSpecies);
+ }
+ else if (cols[0].Equals("p"))
+ {
+ double score;
+ double adjustedScore;
+
+ if (String.Compare(cols[1], "nan", StringComparison.OrdinalIgnoreCase) == 0
+ || String.Compare(cols[2], "nan", StringComparison.OrdinalIgnoreCase) == 0)
+ {
+ score = Double.NaN;
+ adjustedScore = Double.NaN;
+ }
+ else
+ {
+ score = CSVFormat.EgFormat.Parse(cols[1]);
+ adjustedScore = CSVFormat.EgFormat.Parse(cols[2]);
+ }
+
+ String code = cols[3];
+ var prg = new EncogProgram(context);
+ prg.CompileEPL(code);
+ prg.Score = score;
+ prg.Species = lastSpecies;
+ prg.AdjustedScore = adjustedScore;
+ if (lastSpecies == null)
+ {
+ throw new EncogError(
+ "Have not defined a species yet");
+ }
+ lastSpecies.Add(prg);
+ count++;
+ }
+ }
+ }
+ else if (section.SectionName.Equals("BASIC")
+ && section.SubSectionName.Equals("EPL-OPCODES"))
+ {
+ foreach (String line in section.Lines)
+ {
+ IList cols = EncogFileSection.SplitColumns(line);
+ String name = cols[0];
+ int args = int.Parse(cols[1]);
+ result.Context.Functions.AddExtension(name, args);
+ }
+ }
+ else if (section.SectionName.Equals("BASIC")
+ && section.SubSectionName.Equals("EPL-SYMBOLIC"))
+ {
+ bool first = true;
+ foreach (string line in section.Lines)
+ {
+ if (!first)
+ {
+ IList cols = EncogFileSection.SplitColumns(line);
+ String name = cols[0];
+ String t = cols[1];
+ var vt = EPLValueType.Unknown;
+
+ if (string.Compare(t, "f", true) == 0)
+ {
+ vt = EPLValueType.FloatingType;
+ }
+ else if (string.Compare(t, "b", true) == 0)
+ {
+ vt = EPLValueType.BooleanType;
+ }
+ else if (string.Compare(t, "i", true) == 0)
+ {
+ vt = EPLValueType.IntType;
+ }
+ else if (string.Compare(t, "s", true) == 0)
+ {
+ vt = EPLValueType.StringType;
+ }
+ else if (string.Compare(t, "e", true) == 0)
+ {
+ vt = EPLValueType.EnumType;
+ }
+
+ int enumType = int.Parse(cols[2]);
+ int enumCount = int.Parse(cols[3]);
+ var mapping = new VariableMapping(
+ name, vt, enumType, enumCount);
+ if (mapping.Name.Length > 0)
+ {
+ result.Context.DefineVariable(mapping);
+ }
+ else
+ {
+ result.Context.Result = mapping;
+ }
+ }
+ else
+ {
+ first = false;
+ }
+ }
+ }
+ }
+ result.PopulationSize = count;
+
+ // set the best genome, should be the first genome in the first species
+ if (result.Species.Count > 0)
+ {
+ ISpecies species = result.Species[0];
+ if (species.Members.Count > 0)
+ {
+ result.BestGenome = species.Members[0];
+ }
+
+ // set the leaders
+ foreach (ISpecies sp in result.Species)
+ {
+ if (sp.Members.Count > 0)
+ {
+ sp.Leader = sp.Members[0];
+ }
+ }
+ }
+ return result;
+ }
+
+ ///
+ public void Save(Stream ostream, Object obj)
+ {
+ var writer = new EncogWriteHelper(ostream);
+ var pop = (PrgPopulation) obj;
+
+ writer.AddSection("BASIC");
+ writer.AddSubSection("PARAMS");
+ writer.AddProperties(pop.Properties);
+ writer.AddSubSection("EPL-OPCODES");
+ foreach (IProgramExtensionTemplate temp in pop.Context
+ .Functions.OpCodes)
+ {
+ writer.AddColumn(temp.Name);
+ writer.AddColumn(temp.ChildNodeCount);
+ writer.WriteLine();
+ }
+ writer.AddSubSection("EPL-SYMBOLIC");
+ writer.AddColumn("name");
+ writer.AddColumn("type");
+ writer.AddColumn("enum");
+ writer.AddColumn("enum_type");
+ writer.AddColumn("enum_count");
+ writer.WriteLine();
+
+ // write the first line, the result
+ writer.AddColumn("");
+ writer.AddColumn(GetType(pop.Context.Result));
+ writer.AddColumn(pop.Context.Result.EnumType);
+ writer.AddColumn(pop.Context.Result.EnumValueCount);
+ writer.WriteLine();
+
+ // write the next lines, the variables
+ foreach (VariableMapping mapping in pop.Context.DefinedVariables)
+ {
+ writer.AddColumn(mapping.Name);
+ writer.AddColumn(GetType(mapping));
+ writer.AddColumn(mapping.EnumType);
+ writer.AddColumn(mapping.EnumValueCount);
+ writer.WriteLine();
+ }
+ writer.AddSubSection("EPL-POPULATION");
+ foreach (ISpecies species in pop.Species)
+ {
+ if (species.Members.Count > 0)
+ {
+ writer.AddColumn("s");
+ writer.AddColumn(species.Age);
+ writer.AddColumn(species.BestScore);
+ writer.AddColumn(species.GensNoImprovement);
+ writer.WriteLine();
+ foreach (IGenome genome in species.Members)
+ {
+ var prg = (EncogProgram) genome;
+ writer.AddColumn("p");
+ if (Double.IsInfinity(prg.Score)
+ || Double.IsNaN(prg.Score))
+ {
+ writer.AddColumn("NaN");
+ writer.AddColumn("NaN");
+ }
+ else
+ {
+ writer.AddColumn(prg.Score);
+ writer.AddColumn(prg.AdjustedScore);
+ }
+
+ writer.AddColumn(prg.GenerateEPL());
+ writer.WriteLine();
+ }
+ }
+ }
+
+ writer.Flush();
+ }
+
+ ///
+ public Type NativeType
+ {
+ get { return typeof (PrgPopulation); }
+ }
+
+ ///
+ /// Get the type string for the specified variable mapping.
+ ///
+ /// The mapping.
+ /// The value.
+ private string GetType(VariableMapping mapping)
+ {
+ switch (mapping.VariableType)
+ {
+ case EPLValueType.FloatingType:
+ return "f";
+ case EPLValueType.StringType:
+ return "s";
+ case EPLValueType.BooleanType:
+ return "b";
+ case EPLValueType.IntType:
+ return "i";
+ case EPLValueType.EnumType:
+ return "e";
+ }
+ throw new EncogError("Unknown type: "
+ + mapping.VariableType.ToString());
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/PrgCODEC.cs b/encog-core-cs/ML/Prg/PrgCODEC.cs
new file mode 100644
index 00000000..53f67a96
--- /dev/null
+++ b/encog-core-cs/ML/Prg/PrgCODEC.cs
@@ -0,0 +1,46 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Codec;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// Encode and decode an Encog program between genome and phenotypes. This is a
+ /// passthrough, as the Encog geneome and phenome are identical.
+ ///
+ public class PrgCODEC : IGeneticCODEC
+ {
+ ///
+ public IMLMethod Decode(IGenome genome)
+ {
+ return genome;
+ }
+
+ ///
+ public IGenome Encode(IMLMethod phenotype)
+ {
+ return (IGenome) phenotype;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/ProgramNode.cs b/encog-core-cs/ML/Prg/ProgramNode.cs
new file mode 100644
index 00000000..d1ef6a3e
--- /dev/null
+++ b/encog-core-cs/ML/Prg/ProgramNode.cs
@@ -0,0 +1,185 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Linq;
+using System.Text;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+using Encog.ML.Tree;
+using Encog.ML.Tree.Basic;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// Represents a program node in an EPL program.
+ ///
+ [Serializable]
+ public class ProgramNode : BasicTreeNode
+ {
+ ///
+ /// Any data associated with this node. For example, const nodes will store
+ /// their value here.
+ ///
+ private readonly ExpressionValue[] _data;
+
+ ///
+ /// The Encog program that this node belongs to.
+ ///
+ private readonly EncogProgram _owner;
+
+ ///
+ /// The opcode that this node implements.
+ ///
+ private readonly IProgramExtensionTemplate _template;
+
+ ///
+ /// Construct the program node.
+ ///
+ /// The owner of the node.
+ /// The opcode that this node is based on.
+ /// The child nodes to this node.
+ public ProgramNode(EncogProgram theOwner,
+ IProgramExtensionTemplate theTemplate,
+ ITreeNode[] theArgs)
+ {
+ _owner = theOwner;
+ _data = new ExpressionValue[theTemplate.DataSize];
+ _template = theTemplate;
+ AddChildNodes(theArgs);
+
+ for (int i = 0; i < _data.Length; i++)
+ {
+ _data[i] = new ExpressionValue((long) 0);
+ }
+ }
+
+ ///
+ /// The node data.
+ ///
+ public ExpressionValue[] Data
+ {
+ get { return _data; }
+ }
+
+ ///
+ /// The name of this node (from the opcode template).
+ ///
+ public String Name
+ {
+ get { return _template.Name; }
+ }
+
+ ///
+ /// The EncogProgram that owns this node.
+ ///
+ public EncogProgram Owner
+ {
+ get { return _owner; }
+ }
+
+ ///
+ /// The template, or opcode.
+ ///
+ public IProgramExtensionTemplate Template
+ {
+ get { return _template; }
+ }
+
+ ///
+ /// Returns true if this node's value is variable.
+ ///
+ public bool IsVariable
+ {
+ get { return _template.IsVariable; }
+ }
+
+ ///
+ /// True if all children are constant.
+ ///
+ /// True if all children are constant.
+ public bool AllConstChildren()
+ {
+ return ChildNodes.Cast().All(node => !node.IsVariable);
+ }
+
+ ///
+ /// Determine if all descendants are constant.
+ ///
+ /// True if all descendants are constant.
+ public bool AllConstDescendants()
+ {
+ if (IsVariable)
+ {
+ return false;
+ }
+
+ if (IsLeaf)
+ {
+ return true;
+ }
+
+ return ChildNodes.Cast().All(childNode => childNode.AllConstDescendants());
+ }
+
+ ///
+ /// The evaluated value of this node.
+ ///
+ /// The evaluated value of this node.
+ public ExpressionValue Evaluate()
+ {
+ return _template.Evaluate(this);
+ }
+
+ ///
+ /// Get the specified child node.
+ ///
+ /// The index of this node.
+ /// The child node requested.
+ public ProgramNode GetChildNode(int index)
+ {
+ return (ProgramNode) ChildNodes[index];
+ }
+
+ ///
+ /// The string form of this node.
+ ///
+ /// A string form of this node.
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[ProgramNode: name=");
+ result.Append(_template.Name);
+ result.Append(", childCount=");
+ result.Append(ChildNodes.Count);
+ result.Append(", childNodes=");
+ foreach (ITreeNode tn in ChildNodes)
+ {
+ var node = (ProgramNode) tn;
+ result.Append(" ");
+ result.Append(node.Template.Name);
+ }
+ result.Append("]");
+ return result.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Species/CompareEncogProgram.cs b/encog-core-cs/ML/Prg/Species/CompareEncogProgram.cs
new file mode 100644
index 00000000..e65e05cb
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Species/CompareEncogProgram.cs
@@ -0,0 +1,85 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.ML.Prg.Species
+{
+ ///
+ /// Compare two Encog programs for speciation. Count the nodes that are the
+ /// different, the higher the compare value, the more different two genomes are.
+ /// Only the opcodes are compared, the actual values are not. This causes the
+ /// comparison to be more about structure than actual values. Two genomes with
+ /// the same structure, and different values, can be identical.
+ ///
+ public class CompareEncogProgram
+ {
+ ///
+ /// Compare program 1 and 2 node for node. Lower values mean more similar genomes.
+ ///
+ /// The first program.
+ /// The second program.
+ /// The result of the compare.
+ public double Compare(EncogProgram prg1, EncogProgram prg2)
+ {
+ return CompareNode(0, prg1.RootNode, prg2.RootNode);
+ }
+
+ ///
+ /// Compare two nodes.
+ ///
+ /// The result of previous comparisons.
+ /// The first node to compare.
+ /// The second node to compare.
+ /// The result.
+ private double CompareNode(double result, ProgramNode node1,
+ ProgramNode node2)
+ {
+ double newResult = result;
+
+ if (node1.Template != node2.Template)
+ {
+ newResult++;
+ }
+
+ int node1Size = node1.ChildNodes.Count;
+ int node2Size = node2.ChildNodes.Count;
+ int childNodeCount = Math.Max(node1Size, node2Size);
+
+ for (int i = 0; i < childNodeCount; i++)
+ {
+ if (i < node1Size && i < node2Size)
+ {
+ ProgramNode childNode1 = node1.GetChildNode(i);
+ ProgramNode childNode2 = node2.GetChildNode(i);
+ newResult = CompareNode(newResult, childNode1, childNode2);
+ }
+ else
+ {
+ newResult++;
+ }
+ }
+
+ return newResult;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Species/PrgSpeciation.cs b/encog-core-cs/ML/Prg/Species/PrgSpeciation.cs
new file mode 100644
index 00000000..833b376f
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Species/PrgSpeciation.cs
@@ -0,0 +1,55 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Species;
+
+namespace Encog.ML.Prg.Species
+{
+ ///
+ /// Perform speciation for two Encog programs. This is a threshold based
+ /// speciation, similar to that used for NEAT. Any genomes with a compatibility
+ /// score below a specified threshold will be in the same species.
+ ///
+ public class PrgSpeciation : ThresholdSpeciation
+ {
+ ///
+ /// Construct the object.
+ ///
+ public PrgSpeciation()
+ {
+ CompatibilityThreshold = 15;
+ MaxNumberOfSpecies = 30;
+ NumGensAllowedNoImprovement = 15;
+ }
+
+ ///
+ public override double GetCompatibilityScore(IGenome genome1,
+ IGenome genome2)
+ {
+ var comp = new CompareEncogProgram();
+ double d = comp.Compare((EncogProgram) genome1,
+ (EncogProgram) genome2);
+ return d;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/PrgGenomeFactory.cs b/encog-core-cs/ML/Prg/Train/PrgGenomeFactory.cs
new file mode 100644
index 00000000..59c52116
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/PrgGenomeFactory.cs
@@ -0,0 +1,65 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+
+namespace Encog.ML.Prg.Train
+{
+ ///
+ /// A GenomeFactory that creates EncogProgram genomes.
+ ///
+ [Serializable]
+ public class PrgGenomeFactory : IGenomeFactory
+ {
+ ///
+ /// The context.
+ ///
+ private readonly EncogProgramContext _context;
+
+ ///
+ /// Construct a factory.
+ ///
+ /// The context to use.
+ public PrgGenomeFactory(EncogProgramContext theContext)
+ {
+ _context = theContext;
+ }
+
+ ///
+ public IGenome Factor()
+ {
+ var result = new EncogProgram(_context,
+ new EncogProgramVariables());
+ return result;
+ }
+
+ ///
+ public IGenome Factor(IGenome other)
+ {
+ var result = new EncogProgram(_context,
+ new EncogProgramVariables());
+ result.Copy(other);
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/PrgPopulation.cs b/encog-core-cs/ML/Prg/Train/PrgPopulation.cs
new file mode 100644
index 00000000..25d49bba
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/PrgPopulation.cs
@@ -0,0 +1,117 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Population;
+using Encog.ML.EA.Species;
+using Encog.Parse.Expression.Common;
+
+namespace Encog.ML.Prg.Train
+{
+ ///
+ /// A population that contains EncogProgram's. The primary difference between
+ /// this class and BasicPopulation is that a "compute" method is provided that
+ /// automatically uses the "best" genome to provide a MLRegression compute
+ /// method. This population type also holds the common context that all of the
+ /// EncogProgram genomes make use of.
+ ///
+ [Serializable]
+ public class PrgPopulation : BasicPopulation, IMLRegression
+ {
+ ///
+ /// The context.
+ ///
+ private readonly EncogProgramContext _context;
+
+ ///
+ /// Construct the population.
+ ///
+ /// The context.
+ /// The population size.
+ public PrgPopulation(EncogProgramContext theContext,
+ int thePopulationSize)
+ : base(thePopulationSize, new PrgGenomeFactory(theContext))
+ {
+ _context = theContext;
+ }
+
+ ///
+ /// The context for the programs.
+ ///
+ public EncogProgramContext Context
+ {
+ get { return _context; }
+ }
+
+ ///
+ /// Compute the output from the best Genome. Note: it is not safe to call
+ /// this method while training is progressing.
+ ///
+ /// The input to the program.
+ /// The output.
+ public IMLData Compute(IMLData input)
+ {
+ var best = (EncogProgram) BestGenome;
+ return best.Compute(input);
+ }
+
+ ///
+ public int InputCount
+ {
+ get { return Context.DefinedVariables.Count; }
+ }
+
+ ///
+ public int OutputCount
+ {
+ get { return 1; }
+ }
+
+ ///
+ /// Dump the specified number of genomes.
+ ///
+ /// The specified number of genomes.
+ public void DumpMembers(int i)
+ {
+ var render = new RenderCommonExpression();
+
+ int index = 0;
+ foreach (ISpecies species in Species)
+ {
+ Console.Out.WriteLine("** Species: " + species);
+ foreach (IGenome obj in species.Members)
+ {
+ var prg = (EncogProgram) obj;
+ Console.WriteLine(index + ": Score " + prg.Score + " : "
+ + render.Render(prg));
+ index++;
+ if (index > i)
+ {
+ break;
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/Rewrite/RewriteAlgebraic.cs b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteAlgebraic.cs
new file mode 100644
index 00000000..0ff0295e
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteAlgebraic.cs
@@ -0,0 +1,453 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Rules;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+
+namespace Encog.ML.Prg.Train.Rewrite
+{
+ ///
+ /// This class is used to rewrite algebraic expressions into more simple forms.
+ /// This is by no means a complete set of rewrite rules, and will likely be
+ /// extended in the future.
+ ///
+ public class RewriteAlgebraic : IRewriteRule
+ {
+ ///
+ /// Has the expression been rewritten.
+ ///
+ private bool _rewritten;
+
+ ///
+ public bool Rewrite(IGenome g)
+ {
+ _rewritten = false;
+ var program = (EncogProgram) g;
+ ProgramNode node = program.RootNode;
+ ProgramNode rewrittenRoot = InternalRewrite(node);
+ if (rewrittenRoot != null)
+ {
+ program.RootNode = rewrittenRoot;
+ }
+ return _rewritten;
+ }
+
+ ///
+ /// Create an floating point numeric constant.
+ ///
+ /// The program to create the constant for.
+ /// The value that the constant represents.
+ /// The newly created node.
+ private ProgramNode CreateNumericConst(EncogProgram prg, double v)
+ {
+ ProgramNode result = prg.Functions.FactorProgramNode("#const",
+ prg, new ProgramNode[] {});
+ result.Data[0] = new ExpressionValue(v);
+ return result;
+ }
+
+ ///
+ /// Create an integer numeric constant.
+ ///
+ /// The program to create the constant for.
+ /// The value that the constant represents.
+ /// The newly created node.
+ private ProgramNode CreateNumericConst(EncogProgram prg, int v)
+ {
+ ProgramNode result = prg.Functions.FactorProgramNode("#const",
+ prg, new ProgramNode[] {});
+ result.Data[0] = new ExpressionValue(v);
+ return result;
+ }
+
+ ///
+ /// Attempt to rewrite the specified node.
+ ///
+ /// The parent node to start from.
+ /// The rewritten node, or the same node if no rewrite occurs.
+ private ProgramNode InternalRewrite(ProgramNode parent)
+ {
+ ProgramNode rewrittenParent = parent;
+
+ rewrittenParent = TryDoubleNegative(rewrittenParent);
+ rewrittenParent = TryMinusMinus(rewrittenParent);
+ rewrittenParent = TryPlusNeg(rewrittenParent);
+ rewrittenParent = TryVarOpVar(rewrittenParent);
+ rewrittenParent = TryPowerZero(rewrittenParent);
+ rewrittenParent = TryOnePower(rewrittenParent);
+ rewrittenParent = TryZeroPlus(rewrittenParent);
+ rewrittenParent = TryZeroDiv(rewrittenParent);
+ rewrittenParent = TryZeroMul(rewrittenParent);
+ rewrittenParent = TryMinusZero(rewrittenParent);
+
+ // try children
+ for (int i = 0; i < rewrittenParent.ChildNodes.Count; i++)
+ {
+ var childNode = (ProgramNode) rewrittenParent.ChildNodes[i];
+ ProgramNode rewriteChild = InternalRewrite(childNode);
+ if (childNode != rewriteChild)
+ {
+ rewrittenParent.ChildNodes.RemoveAt(i);
+ rewrittenParent.ChildNodes.Insert(i, rewriteChild);
+ _rewritten = true;
+ }
+ }
+
+ return rewrittenParent;
+ }
+
+ ///
+ /// Determine if the specified node is constant.
+ ///
+ /// The node to check.
+ /// The constant to compare against.
+ /// True if the specified node matches the specified constant.
+ private bool IsConstValue(ProgramNode node, double v)
+ {
+ if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ if (Math.Abs(node.Data[0].ToFloatValue() - v) < EncogFramework.DefaultDoubleEqual)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// Try to rewrite --x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryDoubleNegative(ProgramNode parent)
+ {
+ if (parent.Name.Equals("-"))
+ {
+ ProgramNode child = parent.GetChildNode(0);
+ if (child.Name.Equals("-"))
+ {
+ ProgramNode grandChild = child.GetChildNode(0);
+ _rewritten = true;
+ return grandChild;
+ }
+ }
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite --x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryMinusMinus(ProgramNode parent)
+ {
+ if (parent.Name.Equals("-") && parent.ChildNodes.Count == 2)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (child2.Name.Equals("#const"))
+ {
+ ExpressionValue v = child2.Data[0];
+ if (v.IsFloat)
+ {
+ double v2 = v.ToFloatValue();
+ if (v2 < 0)
+ {
+ child2.Data[0] = new ExpressionValue(-v2);
+ parent = parent.Owner.Context.Functions.FactorProgramNode(
+ "+", parent.Owner, new[] {child1, child2});
+ }
+ }
+ else if (v.IsInt)
+ {
+ long v2 = v.ToIntValue();
+ if (v2 < 0)
+ {
+ child2.Data[0] = new ExpressionValue(-v2);
+ parent = parent.Owner.Context.Functions
+ .FactorProgramNode("+", parent.Owner,
+ new[] {child1, child2});
+ }
+ }
+ }
+ }
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite x-0.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryMinusZero(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_SUB)
+ {
+ ProgramNode child2 = parent.GetChildNode(1);
+ if (IsConstValue(child2, 0))
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ return child1;
+ }
+ }
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite x^1.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryOnePower(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_POWER
+ || parent.Template == StandardExtensions.EXTENSION_POWFN)
+ {
+ ProgramNode child = parent.GetChildNode(0);
+ if (child.Template == StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ if (Math.Abs(child.Data[0].ToFloatValue() - 1) < EncogFramework.DefaultDoubleEqual)
+ {
+ _rewritten = true;
+ return CreateNumericConst(parent.Owner, 1);
+ }
+ }
+ }
+
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite x+-c.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryPlusNeg(ProgramNode parent)
+ {
+ if (parent.Name.Equals("+") && parent.ChildNodes.Count == 2)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (child2.Name.Equals("-")
+ && child2.ChildNodes.Count == 1)
+ {
+ parent = parent.Owner.Context.Functions.FactorProgramNode(
+ "-", parent.Owner, new[]
+ {
+ child1,
+ child2.GetChildNode(0)
+ });
+ }
+ else if (child2.Name.Equals("#const"))
+ {
+ ExpressionValue v = child2.Data[0];
+ if (v.IsFloat)
+ {
+ double v2 = v.ToFloatValue();
+ if (v2 < 0)
+ {
+ child2.Data[0] = new ExpressionValue(-v2);
+ parent = parent.Owner.Context.Functions.FactorProgramNode("-",
+ parent.Owner,
+ new[] {child1, child2});
+ }
+ }
+ else if (v.IsInt)
+ {
+ long v2 = v.ToIntValue();
+ if (v2 < 0)
+ {
+ child2.Data[0] = new ExpressionValue(-v2);
+ parent = parent.Owner.Context.Functions
+ .FactorProgramNode("-", parent.Owner,
+ new[] {child1, child2});
+ }
+ }
+ }
+ }
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite x^0.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryPowerZero(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_POWER
+ || parent.Template == StandardExtensions.EXTENSION_POWFN)
+ {
+ ProgramNode child0 = parent.GetChildNode(0);
+ ProgramNode child1 = parent.GetChildNode(1);
+ if (IsConstValue(child1, 0))
+ {
+ return CreateNumericConst(parent.Owner, 1);
+ }
+ if (IsConstValue(child0, 0))
+ {
+ return CreateNumericConst(parent.Owner, 0);
+ }
+ }
+
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite x+x, x-x, x*x, x/x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryVarOpVar(ProgramNode parent)
+ {
+ if (parent.ChildNodes.Count == 2
+ && parent.Name.Length == 1
+ && "+-*/".IndexOf(parent.Name[0]) != -1)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (child1.Name.Equals("#var")
+ && child2.Name.Equals("#var"))
+ {
+ if (child1.Data[0].ToIntValue() == child2.Data[0]
+ .ToIntValue())
+ {
+ switch (parent.Name[0])
+ {
+ case '-':
+ parent = CreateNumericConst(parent.Owner, 0);
+ break;
+ case '+':
+ parent = parent.Owner.Functions.FactorProgramNode("*", parent.Owner,
+ new[]
+ {
+ CreateNumericConst(
+ parent.Owner, 2),
+ child1
+ });
+ break;
+ case '*':
+ parent = parent
+ .Owner
+ .Functions
+ .FactorProgramNode(
+ "^",
+ parent.Owner,
+ new[]
+ {
+ child1,
+ CreateNumericConst(
+ parent.Owner, 2)
+ });
+ break;
+ case '/':
+ parent = CreateNumericConst(parent.Owner, 1);
+ break;
+ }
+ }
+ }
+ }
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite 0/x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryZeroDiv(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_DIV)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (!IsConstValue(child2, 0))
+ {
+ if (IsConstValue(child1, 0))
+ {
+ _rewritten = true;
+ return CreateNumericConst(parent.Owner, 0);
+ }
+ }
+ }
+
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite 0*x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryZeroMul(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_MUL)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (IsConstValue(child1, 0) || IsConstValue(child2, 0))
+ {
+ _rewritten = true;
+ return CreateNumericConst(parent.Owner, 0);
+ }
+ }
+
+ return parent;
+ }
+
+ ///
+ /// Try to rewrite 0+x.
+ ///
+ /// The parent node to attempt to rewrite.
+ /// The rewritten node, if it was rewritten.
+ private ProgramNode TryZeroPlus(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_ADD)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (IsConstValue(child1, 0))
+ {
+ _rewritten = true;
+ return child2;
+ }
+
+ if (IsConstValue(child2, 0))
+ {
+ _rewritten = true;
+ return child1;
+ }
+ }
+
+ return parent;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/Rewrite/RewriteBoolean.cs b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteBoolean.cs
new file mode 100644
index 00000000..e0eb1668
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteBoolean.cs
@@ -0,0 +1,154 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Rules;
+using Encog.ML.Prg.ExpValue;
+using Encog.ML.Prg.Ext;
+
+namespace Encog.ML.Prg.Train.Rewrite
+{
+ ///
+ /// Basic rewrite rules for boolean expressions.
+ ///
+ public class RewriteBoolean : IRewriteRule
+ {
+ ///
+ /// True, if the value has been rewritten.
+ ///
+ private bool _rewritten;
+
+ ///
+ public bool Rewrite(IGenome g)
+ {
+ _rewritten = false;
+ var program = (EncogProgram) g;
+ ProgramNode node = program.RootNode;
+ ProgramNode rewrittenRoot = InternalRewrite(node);
+ if (rewrittenRoot != null)
+ {
+ program.RootNode = rewrittenRoot;
+ }
+ return _rewritten;
+ }
+
+ ///
+ /// Returns true, if the specified constant value is a true const. Returns
+ /// false in any other case.
+ ///
+ /// The node to check.
+ /// True if the value is a true const.
+ private bool IsTrue(ProgramNode node)
+ {
+ if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ ExpressionValue v = node.Evaluate();
+ if (v.IsBoolean)
+ {
+ if (v.ToBooleanValue())
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// Returns true, if the specified constant value is a false const. Returns
+ /// false in any other case.
+ ///
+ /// The node to check.
+ /// True if the value is a false const.
+ private bool IsFalse(ProgramNode node)
+ {
+ if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ ExpressionValue v = node.Evaluate();
+ if (v.IsBoolean)
+ {
+ if (!v.ToBooleanValue())
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// Attempt to rewrite the specified node.
+ ///
+ /// The node to attempt to rewrite.
+ /// The rewritten node, or the original node, if no change was made.
+ private ProgramNode InternalRewrite(ProgramNode parent)
+ {
+ ProgramNode rewrittenParent = parent;
+
+ rewrittenParent = TryAnd(rewrittenParent);
+
+ // try children
+ for (int i = 0; i < rewrittenParent.ChildNodes.Count; i++)
+ {
+ var childNode = (ProgramNode) rewrittenParent.ChildNodes[i];
+ ProgramNode rewriteChild = InternalRewrite(childNode);
+ if (childNode != rewriteChild)
+ {
+ rewrittenParent.ChildNodes.RemoveAt(i);
+ rewrittenParent.ChildNodes.Insert(i, rewriteChild);
+ _rewritten = true;
+ }
+ }
+
+ return rewrittenParent;
+ }
+
+ ///
+ /// Try to rewrite true and true, false and false.
+ ///
+ /// The node to attempt to rewrite.
+ /// The rewritten node, or the original node if not rewritten.
+ private ProgramNode TryAnd(ProgramNode parent)
+ {
+ if (parent.Template == StandardExtensions.EXTENSION_AND)
+ {
+ ProgramNode child1 = parent.GetChildNode(0);
+ ProgramNode child2 = parent.GetChildNode(1);
+
+ if (IsTrue(child1)
+ && child2.Template != StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ _rewritten = true;
+ return child2;
+ }
+
+ if (IsTrue(child2)
+ && child1.Template != StandardExtensions.EXTENSION_CONST_SUPPORT)
+ {
+ _rewritten = true;
+ return child1;
+ }
+ }
+ return parent;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/Rewrite/RewriteConstants.cs b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteConstants.cs
new file mode 100644
index 00000000..479ccbfb
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/Rewrite/RewriteConstants.cs
@@ -0,0 +1,129 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.EA.Genome;
+using Encog.ML.EA.Rules;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg.Train.Rewrite
+{
+ ///
+ /// Rewrite any parts of the tree that are constant with a simple constant value.
+ ///
+ public class RewriteConstants : IRewriteRule
+ {
+ ///
+ /// True if the expression was rewritten.
+ ///
+ private bool _rewritten;
+
+ ///
+ public bool Rewrite(IGenome g)
+ {
+ var program = ((EncogProgram) g);
+ _rewritten = false;
+ ProgramNode rootNode = program.RootNode;
+ ProgramNode rewrite = RewriteNode(rootNode);
+ if (rewrite != null)
+ {
+ program.RootNode = rewrite;
+ }
+ return _rewritten;
+ }
+
+ ///
+ /// Attempt to rewrite the specified node.
+ ///
+ /// The node to attempt to rewrite.
+ /// The rewritten node, the original node, if no rewrite occured.
+ private ProgramNode RewriteNode(ProgramNode node)
+ {
+ // first try to rewrite the child node
+ ProgramNode rewrite = TryNodeRewrite(node);
+ if (rewrite != null)
+ {
+ return rewrite;
+ }
+
+ // if we could not rewrite the entire node, rewrite as many children as
+ // we can
+ for (int i = 0; i < node.ChildNodes.Count; i++)
+ {
+ var childNode = (ProgramNode) node.ChildNodes[i];
+ rewrite = RewriteNode(childNode);
+ if (rewrite != null)
+ {
+ node.ChildNodes.RemoveAt(i);
+ node.ChildNodes.Insert(i, rewrite);
+ _rewritten = true;
+ }
+ }
+
+ // we may have rewritten some children, but the parent was not
+ // rewritten, so return null.
+ return null;
+ }
+
+ ///
+ /// Try to rewrite the specified node.
+ ///
+ /// The node to attempt rewrite.
+ /// The rewritten node, or original node, if no rewrite could happen.
+ private ProgramNode TryNodeRewrite(ProgramNode parentNode)
+ {
+ ProgramNode result = null;
+
+ if (parentNode.IsLeaf)
+ {
+ return null;
+ }
+
+ if (parentNode.AllConstDescendants())
+ {
+ ExpressionValue v = parentNode.Evaluate();
+ double ck = v.ToFloatValue();
+
+ // do not rewrite if it produces a div by 0 or other bad result.
+ if (Double.IsNaN(ck) || Double.IsInfinity(ck))
+ {
+ return null;
+ }
+
+ result = parentNode.Owner.Context.Functions
+ .FactorProgramNode("#const", parentNode.Owner,
+ new ProgramNode[] {});
+
+ // is it an integer?
+ if (Math.Abs(ck - ((int) ck)) < EncogFramework.DefaultDoubleEqual)
+ {
+ result.Data[0] = new ExpressionValue((int) ck);
+ }
+ else
+ {
+ result.Data[0] = v;
+ }
+ }
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/Train/ZeroEvalScoreFunction.cs b/encog-core-cs/ML/Prg/Train/ZeroEvalScoreFunction.cs
new file mode 100644
index 00000000..d526e23c
--- /dev/null
+++ b/encog-core-cs/ML/Prg/Train/ZeroEvalScoreFunction.cs
@@ -0,0 +1,63 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data;
+using Encog.ML.Data.Basic;
+using Encog.Neural.Networks.Training;
+
+namespace Encog.ML.Prg.Train
+{
+ ///
+ /// This is a very simple evaluation function that simply passes in all zeros for
+ /// the input arguments. Make sure that the genome you are testing supports
+ /// floating point numbers for inputs.
+ /// This evaluation function is useful to test to very quickly verify that a
+ /// genome is valid and does not generate any obvious division by zero issues.
+ /// This allows a population generator to quickly eliminate some invalid genomes.
+ ///
+ [Serializable]
+ public class ZeroEvalScoreFunction : ICalculateScore
+ {
+ ///
+ public double CalculateScore(IMLMethod genome)
+ {
+ var prg = (EncogProgram) genome;
+ var pop = (PrgPopulation) prg.Population;
+ IMLData inputData = new BasicMLData(pop.Context.DefinedVariables.Count);
+ prg.Compute(inputData);
+ return 0;
+ }
+
+ ///
+ public bool RequireSingleThreaded
+ {
+ get { return false; }
+ }
+
+ ///
+ public bool ShouldMinimize
+ {
+ get { return true; }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Prg/VariableMapping.cs b/encog-core-cs/ML/Prg/VariableMapping.cs
new file mode 100644
index 00000000..0c626d44
--- /dev/null
+++ b/encog-core-cs/ML/Prg/VariableMapping.cs
@@ -0,0 +1,132 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.ML.Prg.ExpValue;
+
+namespace Encog.ML.Prg
+{
+ ///
+ /// A variable mapping defines the type for each of the variables in an Encog
+ /// program.
+ ///
+ [Serializable]
+ public class VariableMapping
+ {
+ ///
+ /// If this is an enum, what is the type.
+ ///
+ private readonly int _enumType;
+
+ ///
+ /// The count for this given enum. If this is not an enum, then value is not
+ /// used.
+ ///
+ private readonly int _enumValueCount;
+
+ ///
+ /// The name of the variable.
+ ///
+ private readonly String _name;
+
+ ///
+ /// The variable type.
+ ///
+ private readonly EPLValueType _variableType;
+
+ ///
+ /// Construct a variable mapping for a non-enum type.
+ ///
+ /// The variable name.
+ /// The variable type.
+ public VariableMapping(String theName, EPLValueType theVariableType)
+ : this(theName, theVariableType, 0, 0)
+ {
+ }
+
+ ///
+ /// Construct a variable mapping.
+ ///
+ /// The name of the variable.
+ /// The type of the variable.
+ /// The enum type.
+ /// The number of values for an enum.
+ public VariableMapping(String theName,
+ EPLValueType theVariableType, int theEnumType,
+ int theEnumValueCount)
+ {
+ _name = theName;
+ _variableType = theVariableType;
+ _enumType = theEnumType;
+ _enumValueCount = theEnumValueCount;
+ }
+
+ ///
+ /// The enum type.
+ ///
+ public int EnumType
+ {
+ get { return _enumType; }
+ }
+
+ ///
+ /// The enum value count.
+ ///
+ public int EnumValueCount
+ {
+ get { return _enumValueCount; }
+ }
+
+ ///
+ /// The name.
+ ///
+ public String Name
+ {
+ get { return _name; }
+ }
+
+ ///
+ /// The type.
+ ///
+ public EPLValueType VariableType
+ {
+ get { return _variableType; }
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[VariableMapping: name=");
+ result.Append(_name);
+ result.Append(",type=");
+ result.Append(_variableType.ToString());
+ result.Append(",enumType=");
+ result.Append(_enumType);
+ result.Append(",enumCount=");
+ result.Append(_enumValueCount);
+ result.Append("]");
+ return result.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/ML/SVM/KernelType.cs b/encog-core-cs/ML/SVM/KernelType.cs
index 434ff87d..8579930d 100644
--- a/encog-core-cs/ML/SVM/KernelType.cs
+++ b/encog-core-cs/ML/SVM/KernelType.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/SVM/PersistSVM.cs b/encog-core-cs/ML/SVM/PersistSVM.cs
index 54540b1e..3f56ef33 100644
--- a/encog-core-cs/ML/SVM/PersistSVM.cs
+++ b/encog-core-cs/ML/SVM/PersistSVM.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/SVM/SVMType.cs b/encog-core-cs/ML/SVM/SVMType.cs
index b4623b19..3596ad22 100644
--- a/encog-core-cs/ML/SVM/SVMType.cs
+++ b/encog-core-cs/ML/SVM/SVMType.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/SVM/SupportVectorMachine.cs b/encog-core-cs/ML/SVM/SupportVectorMachine.cs
index de5743f6..ce5b39b1 100644
--- a/encog-core-cs/ML/SVM/SupportVectorMachine.cs
+++ b/encog-core-cs/ML/SVM/SupportVectorMachine.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -354,7 +354,7 @@ public IMLData Compute(IMLData input)
+ "and no model exists.");
}
- IMLData result = new BasicMLData(1);
+ var result = new BasicMLData(1);
svm_node[] formattedInput = MakeSparse(input);
diff --git a/encog-core-cs/ML/SVM/Training/EncodeSVMProblem.cs b/encog-core-cs/ML/SVM/Training/EncodeSVMProblem.cs
index 2980cfe8..c2307ebb 100644
--- a/encog-core-cs/ML/SVM/Training/EncodeSVMProblem.cs
+++ b/encog-core-cs/ML/SVM/Training/EncodeSVMProblem.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/SVM/Training/SVMSearchTrain.cs b/encog-core-cs/ML/SVM/Training/SVMSearchTrain.cs
index a6bb83d6..b83fc462 100644
--- a/encog-core-cs/ML/SVM/Training/SVMSearchTrain.cs
+++ b/encog-core-cs/ML/SVM/Training/SVMSearchTrain.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -81,24 +81,12 @@ public class SVMSearchTrain : BasicTraining
///
private readonly SupportVectorMachine _network;
- ///
- /// The best values found for C.
- ///
- ///
- private double _bestConst;
-
///
/// The best error.
///
///
private double _bestError;
- ///
- /// The best values found for gamma.
- ///
- ///
- private double _bestGamma;
-
///
/// The beginning value for C.
///
@@ -165,6 +153,18 @@ public class SVMSearchTrain : BasicTraining
///
private bool _trainingDone;
+ ///
+ /// The best values found for C.
+ ///
+ ///
+ public double BestConst { get; set; }
+
+ ///
+ /// The best values found for gamma.
+ ///
+ ///
+ private double BestGamma { get; set; }
+
///
/// Construct a trainer for an SVM network.
///
@@ -268,8 +268,8 @@ public override bool TrainingDone
///
public override sealed void FinishTraining()
{
- _internalTrain.Gamma = _bestGamma;
- _internalTrain.C = _bestConst;
+ _internalTrain.Gamma = BestGamma;
+ _internalTrain.C = BestConst;
_internalTrain.Iteration();
}
@@ -305,8 +305,8 @@ public override sealed void Iteration()
{
if (e < _bestError)
{
- _bestConst = _currentConst;
- _bestGamma = _currentGamma;
+ BestConst = _currentConst;
+ BestGamma = _currentGamma;
_bestError = e;
}
}
diff --git a/encog-core-cs/ML/SVM/Training/SVMTrain.cs b/encog-core-cs/ML/SVM/Training/SVMTrain.cs
index 9996bd6a..a73d80db 100644
--- a/encog-core-cs/ML/SVM/Training/SVMTrain.cs
+++ b/encog-core-cs/ML/SVM/Training/SVMTrain.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/BasicTraining.cs b/encog-core-cs/ML/Train/BasicTraining.cs
index c99eda98..f6c00648 100644
--- a/encog-core-cs/ML/Train/BasicTraining.cs
+++ b/encog-core-cs/ML/Train/BasicTraining.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -181,7 +181,7 @@ public abstract void Resume(
/// Call the strategies after an iteration.
///
///
- public void PostIteration()
+ public virtual void PostIteration()
{
foreach (IStrategy strategy in _strategies)
{
@@ -193,7 +193,7 @@ public void PostIteration()
/// Call the strategies before an iteration.
///
///
- public void PreIteration()
+ public virtual void PreIteration()
{
_iteration++;
diff --git a/encog-core-cs/ML/Train/IMLTrain.cs b/encog-core-cs/ML/Train/IMLTrain.cs
index 1b9b05bd..00438417 100644
--- a/encog-core-cs/ML/Train/IMLTrain.cs
+++ b/encog-core-cs/ML/Train/IMLTrain.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/EarlyStoppingStrategy.cs b/encog-core-cs/ML/Train/Strategy/EarlyStoppingStrategy.cs
index 3c433ff7..b4d7feec 100644
--- a/encog-core-cs/ML/Train/Strategy/EarlyStoppingStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/EarlyStoppingStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/End/EndIterationsStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/EndIterationsStrategy.cs
index a1e3006d..46e3a574 100644
--- a/encog-core-cs/ML/Train/Strategy/End/EndIterationsStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/End/EndIterationsStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/End/EndMaxErrorStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/EndMaxErrorStrategy.cs
index f5a3a631..d6a4be90 100644
--- a/encog-core-cs/ML/Train/Strategy/End/EndMaxErrorStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/End/EndMaxErrorStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/End/EndMinutesStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/EndMinutesStrategy.cs
index c1e5a6c8..6e14b44b 100644
--- a/encog-core-cs/ML/Train/Strategy/End/EndMinutesStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/End/EndMinutesStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -86,7 +86,7 @@ public virtual bool ShouldStop()
{
lock (this)
{
- return _started && _minutesLeft >= 0;
+ return _started && _minutesLeft <= 0;
}
}
@@ -94,7 +94,7 @@ public virtual bool ShouldStop()
public virtual void Init(IMLTrain train)
{
_started = true;
- _startedTime = DateTime.Now.Millisecond;
+ _startedTime = DateTime.Now.Ticks;
}
///
@@ -102,8 +102,10 @@ public virtual void PostIteration()
{
lock (this)
{
- long now = DateTime.Now.Millisecond;
- _minutesLeft = ((int) ((now - _startedTime)/60000));
+ long now = DateTime.Now.Ticks;
+ long elapsedTicks = now - _startedTime;
+ int elapsedMinutes = (int)(elapsedTicks / TimeSpan.TicksPerMinute);
+ _minutesLeft = _minutes - elapsedMinutes;
}
}
diff --git a/encog-core-cs/ML/Train/Strategy/End/EndTimeSpanStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/EndTimeSpanStrategy.cs
new file mode 100644
index 00000000..cd3f1eb9
--- /dev/null
+++ b/encog-core-cs/ML/Train/Strategy/End/EndTimeSpanStrategy.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Train.Strategy.End
+{
+ ///
+ /// This End time span strategy gives greater specificity than EndMinutesStrategy.
+ /// You can specify Days, Hours, Minutes, Seconds etc...
+ ///
+ public class EndTimeSpanStrategy : IEndTrainingStrategy
+ {
+ private TimeSpan _duration;
+ private bool _started;
+ private DateTime _startedTime;
+
+ ///
+ public EndTimeSpanStrategy(TimeSpan duration)
+ {
+ _duration = duration;
+ }
+ public void Init(IMLTrain train)
+ {
+ _started = true;
+ _startedTime = DateTime.Now;
+ }
+
+ ///
+ public void PostIteration()
+ {
+
+ }
+
+ ///
+ public void PreIteration()
+ {
+
+ }
+
+ ///
+ public virtual bool ShouldStop()
+ {
+ lock (this)
+ {
+ return (DateTime.Now.Subtract(_startedTime) > _duration);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Train/Strategy/End/IEndTrainingStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/IEndTrainingStrategy.cs
index 3b7ea2f2..52906f30 100644
--- a/encog-core-cs/ML/Train/Strategy/End/IEndTrainingStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/End/IEndTrainingStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/End/SimpleEarlyStoppingStrategy.cs b/encog-core-cs/ML/Train/Strategy/End/SimpleEarlyStoppingStrategy.cs
new file mode 100644
index 00000000..72d9b70d
--- /dev/null
+++ b/encog-core-cs/ML/Train/Strategy/End/SimpleEarlyStoppingStrategy.cs
@@ -0,0 +1,142 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.ML.Data;
+
+namespace Encog.ML.Train.Strategy.End
+{
+ ///
+ /// A simple early stopping strategy that halts training when the validation set no longer improves.
+ ///
+ public class SimpleEarlyStoppingStrategy : IEndTrainingStrategy
+ {
+ private readonly int _checkFrequency;
+
+ ///
+ /// The validation set.
+ ///
+ private readonly IMLDataSet _validationSet;
+
+ ///
+ /// Current validation error.
+ ///
+ private IMLError _calc;
+
+ private int _lastCheck;
+
+ private double _lastError;
+
+ ///
+ /// Current validation error.
+ ///
+ private double _lastValidationError;
+
+ ///
+ /// Has training stopped.
+ ///
+ private bool _stop;
+
+ ///
+ /// The trainer.
+ ///
+ private IMLTrain _train;
+
+ ///
+ /// Current training error.
+ ///
+ private double _trainingError;
+
+
+ public SimpleEarlyStoppingStrategy(IMLDataSet theValidationSet) :
+ this(theValidationSet, 5)
+ {
+ }
+
+
+ public SimpleEarlyStoppingStrategy(IMLDataSet theValidationSet,
+ int theCheckFrequency)
+ {
+ _validationSet = theValidationSet;
+ _checkFrequency = theCheckFrequency;
+ }
+
+ ///
+ /// Thetraining error.
+ ///
+ public double TrainingError
+ {
+ get { return _trainingError; }
+ }
+
+
+ ///
+ /// The validation error.
+ ///
+ public double ValidationError
+ {
+ get { return _lastValidationError; }
+ }
+
+ ///
+ public void Init(IMLTrain theTrain)
+ {
+ _train = theTrain;
+ _calc = (IMLError) _train.Method;
+ _stop = false;
+ _lastCheck = 0;
+ _lastValidationError = _calc.CalculateError(_validationSet);
+ }
+
+ ///
+ public void PreIteration()
+ {
+ }
+
+ ///
+ public void PostIteration()
+ {
+ _lastCheck++;
+ _trainingError = _train.Error;
+
+ if (_lastCheck > _checkFrequency || Double.IsInfinity(_lastValidationError))
+ {
+ _lastCheck = 0;
+
+ double currentValidationError = _calc.CalculateError(_validationSet);
+
+ if (currentValidationError >= _lastValidationError)
+ {
+ _stop = true;
+ }
+
+ _lastValidationError = currentValidationError;
+ }
+ }
+
+ ///
+ public bool ShouldStop()
+ {
+ return _stop;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Train/Strategy/Greedy.cs b/encog-core-cs/ML/Train/Strategy/Greedy.cs
index ae7365bc..7730137f 100644
--- a/encog-core-cs/ML/Train/Strategy/Greedy.cs
+++ b/encog-core-cs/ML/Train/Strategy/Greedy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/HybridStrategy.cs b/encog-core-cs/ML/Train/Strategy/HybridStrategy.cs
index e59fa6c2..7dcace55 100644
--- a/encog-core-cs/ML/Train/Strategy/HybridStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/HybridStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/IStrategy.cs b/encog-core-cs/ML/Train/Strategy/IStrategy.cs
index bfb478f6..bbc76b02 100644
--- a/encog-core-cs/ML/Train/Strategy/IStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/IStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/RequiredImprovementStrategy.cs b/encog-core-cs/ML/Train/Strategy/RequiredImprovementStrategy.cs
index fa2cfbd3..1da377c8 100644
--- a/encog-core-cs/ML/Train/Strategy/RequiredImprovementStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/RequiredImprovementStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/ResetStrategy.cs b/encog-core-cs/ML/Train/Strategy/ResetStrategy.cs
index 5fb79218..f529c417 100644
--- a/encog-core-cs/ML/Train/Strategy/ResetStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/ResetStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Train/Strategy/StopTrainingStrategy.cs b/encog-core-cs/ML/Train/Strategy/StopTrainingStrategy.cs
index ed520af1..36a01050 100644
--- a/encog-core-cs/ML/Train/Strategy/StopTrainingStrategy.cs
+++ b/encog-core-cs/ML/Train/Strategy/StopTrainingStrategy.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/TrainingImplementationType.cs b/encog-core-cs/ML/TrainingImplementationType.cs
index 19338d74..69319eab 100644
--- a/encog-core-cs/ML/TrainingImplementationType.cs
+++ b/encog-core-cs/ML/TrainingImplementationType.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/ML/Tree/Basic/BasicTreeNode.cs b/encog-core-cs/ML/Tree/Basic/BasicTreeNode.cs
new file mode 100644
index 00000000..830a9149
--- /dev/null
+++ b/encog-core-cs/ML/Tree/Basic/BasicTreeNode.cs
@@ -0,0 +1,96 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Tree.Traverse.Tasks;
+
+namespace Encog.ML.Tree.Basic
+{
+ ///
+ /// A basic tree.
+ ///
+ [Serializable]
+ public class BasicTreeNode : ITreeNode
+ {
+ ///
+ /// The child nodes.
+ ///
+ private IList childNodes = new List();
+
+ ///
+ public IList ChildNodes
+ {
+ get
+ {
+ return this.childNodes;
+ }
+ }
+
+ ///
+ public void AddChildNodes(ITreeNode[] args)
+ {
+ foreach (ITreeNode pn in args)
+ {
+ this.childNodes.Add(pn);
+ }
+ }
+
+ ///
+ public bool AllLeafChildren()
+ {
+ bool result = true;
+
+ foreach (ITreeNode node in this.childNodes)
+ {
+ if (!node.IsLeaf)
+ {
+ result = false;
+ break;
+ }
+ }
+
+ return result;
+ }
+
+ ///
+ public bool IsLeaf
+ {
+ get
+ {
+ return this.childNodes.Count == 0;
+ }
+ }
+
+ ///
+ public int Count
+ {
+ get
+ {
+ return TaskCountNodes.Process(this);
+ }
+ }
+
+ }
+}
diff --git a/encog-core-cs/ML/Tree/ITreeNode.cs b/encog-core-cs/ML/Tree/ITreeNode.cs
new file mode 100644
index 00000000..4087d368
--- /dev/null
+++ b/encog-core-cs/ML/Tree/ITreeNode.cs
@@ -0,0 +1,62 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree
+{
+ ///
+ /// A node for a tree.
+ ///
+ public interface ITreeNode
+ {
+ ///
+ /// Add child nodes.
+ ///
+ /// The child nodes to add.
+ void AddChildNodes(ITreeNode[] args);
+
+ ///
+ /// True, if all children are leaves.
+ ///
+ /// True, if all children are leaves.
+ bool AllLeafChildren();
+
+ ///
+ /// The child nodes.
+ ///
+ IList ChildNodes { get; }
+
+ ///
+ /// True, if this is a leaf.
+ ///
+ bool IsLeaf { get; }
+
+ ///
+ /// The number of nodes from this point. Do not call on cyclic tree.
+ ///
+ int Count { get; }
+ }
+}
diff --git a/encog-core-cs/ML/Tree/Traverse/DepthFirstTraversal.cs b/encog-core-cs/ML/Tree/Traverse/DepthFirstTraversal.cs
new file mode 100644
index 00000000..32c99558
--- /dev/null
+++ b/encog-core-cs/ML/Tree/Traverse/DepthFirstTraversal.cs
@@ -0,0 +1,47 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree.Traverse
+{
+ ///
+ /// A depth first tree traversal.
+ ///
+ public class DepthFirstTraversal : ITreeTraversal
+ {
+ ///
+ public void Traverse(ITreeNode treeNode, MLDelegates.TreeTraversalTask task)
+ {
+ if (!task(treeNode))
+ return;
+
+ foreach (ITreeNode childNode in treeNode.ChildNodes)
+ {
+ Traverse(childNode, task);
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Innovation/BasicInnovation.cs b/encog-core-cs/ML/Tree/Traverse/ITreeTraversal.cs
similarity index 63%
rename from encog-core-cs/ML/Genetic/Innovation/BasicInnovation.cs
rename to encog-core-cs/ML/Tree/Traverse/ITreeTraversal.cs
index 9cffe0dc..75e7aaab 100644
--- a/encog-core-cs/ML/Genetic/Innovation/BasicInnovation.cs
+++ b/encog-core-cs/ML/Tree/Traverse/ITreeTraversal.cs
@@ -1,43 +1,42 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using System;
-namespace Encog.ML.Genetic.Innovation
-{
- ///
- /// Provides basic functionality for an innovation.
- ///
- [Serializable]
- public class BasicInnovation : IInnovation
- {
- #region IInnovation Members
-
- ///
- /// Set the innovation id.
- ///
- public long InnovationID {
- get;
- set; }
-
- #endregion
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree.Traverse
+{
+ ///
+ /// Traverse a tree.
+ ///
+ public interface ITreeTraversal
+ {
+ ///
+ /// Traverse the tree.
+ ///
+ /// The tree to traverse.
+ /// The task to execute on each tree node.
+ void Traverse(ITreeNode tree, MLDelegates.TreeTraversalTask task);
+ }
+}
diff --git a/encog-core-cs/ML/Genetic/Crossover/ICrossover.cs b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskCountNodes.cs
similarity index 53%
rename from encog-core-cs/ML/Genetic/Crossover/ICrossover.cs
rename to encog-core-cs/ML/Tree/Traverse/Tasks/TaskCountNodes.cs
index a802afb5..fe0cbaf7 100644
--- a/encog-core-cs/ML/Genetic/Crossover/ICrossover.cs
+++ b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskCountNodes.cs
@@ -1,44 +1,55 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-using Encog.ML.Genetic.Genome;
-
-namespace Encog.ML.Genetic.Crossover
-{
- ///
- /// Specifies how "crossover" or mating happens.
- ///
- ///
- public interface ICrossover
- {
- ///
- /// Mate two chromosomes.
- ///
- ///
- /// The mother.
- /// The father.
- /// The first offspring.
- /// The second offspring.
- void Mate(Chromosome mother, Chromosome father,
- Chromosome offspring1, Chromosome offspring2);
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree.Traverse.Tasks
+{
+ ///
+ /// Count the nodes in a tree.
+ ///
+ public class TaskCountNodes
+ {
+ ///
+ /// Count the nodes from this tree node.
+ ///
+ /// The tree node.
+ /// The node count.
+ public static int Process(ITreeNode node)
+ {
+ int result = 0;
+
+ DepthFirstTraversal trav = new DepthFirstTraversal();
+ trav.Traverse( node, (n) =>
+ {
+ result++;
+ return true;
+ });
+
+ return result;
+
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Tree/Traverse/Tasks/TaskGetNodeIndex.cs b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskGetNodeIndex.cs
new file mode 100644
index 00000000..55ed3e8c
--- /dev/null
+++ b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskGetNodeIndex.cs
@@ -0,0 +1,65 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree.Traverse.Tasks
+{
+ ///
+ /// Get a node by index.
+ ///
+ public class TaskGetNodeIndex
+ {
+ ///
+ /// Obtain the specified tree node for the specified index.
+ ///
+ /// The index.
+ /// The tree node to search from.
+ /// The tree node for the specified index.
+ public static ITreeNode process(int index, ITreeNode node)
+ {
+ ITreeNode result = null;
+ int nodeCount = 0;
+
+ DepthFirstTraversal trav = new DepthFirstTraversal();
+ trav.Traverse(node, (n) =>
+ {
+ if ( nodeCount >= index)
+ {
+ if (result == null)
+ {
+ result = n;
+ }
+ return false;
+ }
+
+ nodeCount++;
+ return true;
+ });
+
+ return result;
+ }
+ }
+}
diff --git a/encog-core-cs/ML/Tree/Traverse/Tasks/TaskReplaceNode.cs b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskReplaceNode.cs
new file mode 100644
index 00000000..8ddd2c0c
--- /dev/null
+++ b/encog-core-cs/ML/Tree/Traverse/Tasks/TaskReplaceNode.cs
@@ -0,0 +1,67 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.ML.Tree.Traverse.Tasks
+{
+ ///
+ /// Replace one node with another.
+ ///
+ public class TaskReplaceNode
+ {
+ ///
+ /// Replace one node with another.
+ ///
+ /// The root node.
+ /// The node to replace.
+ /// What to replace with.
+ public static void Process(ITreeNode rootNode, ITreeNode replaceThisNode, ITreeNode replaceWith)
+ {
+ bool done = false;
+
+ DepthFirstTraversal trav = new DepthFirstTraversal();
+ trav.Traverse(rootNode, (n) =>
+ {
+ if (done)
+ {
+ return false;
+ }
+
+ for (int i = 0; i < n.ChildNodes.Count; i++)
+ {
+ ITreeNode childNode = n.ChildNodes[i];
+ if (childNode == replaceThisNode)
+ {
+ n.ChildNodes[i] = replaceWith;
+ done = true;
+ return false;
+ }
+ }
+ return true;
+ });
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/BoundMath.cs b/encog-core-cs/MathUtil/BoundMath.cs
index 838259ff..9848df56 100644
--- a/encog-core-cs/MathUtil/BoundMath.cs
+++ b/encog-core-cs/MathUtil/BoundMath.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/BoundNumbers.cs b/encog-core-cs/MathUtil/BoundNumbers.cs
index c8c36e62..86265849 100644
--- a/encog-core-cs/MathUtil/BoundNumbers.cs
+++ b/encog-core-cs/MathUtil/BoundNumbers.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/ComplexNumber.cs b/encog-core-cs/MathUtil/ComplexNumber.cs
index 421b0c84..e7370e04 100644
--- a/encog-core-cs/MathUtil/ComplexNumber.cs
+++ b/encog-core-cs/MathUtil/ComplexNumber.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -174,7 +174,8 @@ public ComplexNumber Conj()
/// The result of the addition.
public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
{
- double den = Math.Pow(c2.Mod(), 2);
+ var mod = c2.Mod();
+ double den = mod * mod;
return new ComplexNumber((c1.Real*c2.Real + c1.Imaginary
*c2.Imaginary)/den, (c1.Imaginary
*c2.Real - c1.Real*c2.Imaginary)/den);
diff --git a/encog-core-cs/MathUtil/Convert.cs b/encog-core-cs/MathUtil/Convert.cs
index b4bce6da..dbce4b7d 100644
--- a/encog-core-cs/MathUtil/Convert.cs
+++ b/encog-core-cs/MathUtil/Convert.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/EncogMath.cs b/encog-core-cs/MathUtil/EncogMath.cs
index 90802514..5410bc26 100644
--- a/encog-core-cs/MathUtil/EncogMath.cs
+++ b/encog-core-cs/MathUtil/EncogMath.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -93,5 +93,18 @@ public static double Factorial(int p)
return result;
}
+
+ internal static int Sign(double p)
+ {
+ if (Math.Abs(p) < EncogFramework.DefaultDoubleEqual)
+ {
+ return 0;
+ }
+ if (p > 0)
+ {
+ return 1;
+ }
+ return -1;
+ }
}
}
diff --git a/encog-core-cs/MathUtil/Equilateral.cs b/encog-core-cs/MathUtil/Equilateral.cs
index 0de94083..c2f03d33 100644
--- a/encog-core-cs/MathUtil/Equilateral.cs
+++ b/encog-core-cs/MathUtil/Equilateral.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
// http://www.heatonresearch.com/copyright
//
using System;
+using Encog.ML.Data;
namespace Encog.MathUtil
{
@@ -77,7 +78,30 @@ public int Decode(double[] activations)
return minSet;
}
- ///
+ ///
+ /// Decode a set of activations and see which set it has the lowest Euclidean
+ /// distance from.
+ ///
+ /// The output from the neural network.
+ /// The set that these activations were closest too.
+ public int Decode(IMLData activations)
+ {
+ double minValue = double.PositiveInfinity;
+ int minSet = -1;
+
+ for(int i = 0; i < _matrix.GetLength(0); i++)
+ {
+ double dist = GetDistance(activations, i);
+ if(dist < minValue)
+ {
+ minValue = dist;
+ minSet = i;
+ }
+ }
+ return minSet;
+ }
+
+ ///
/// Get the activations for the specified set.
///
/// The set to determine the activations for.
@@ -162,12 +186,30 @@ public double GetDistance(double[] data, int set)
double result = 0;
for (int i = 0; i < data.GetLength(0); i++)
{
- result += Math.Pow(data[i] - _matrix[set][i], 2);
+ var val = data[i] - _matrix[set][i];
+ result += val * val;
}
return Math.Sqrt(result);
}
- ///
+ ///
+ /// Get the Euclidean distance between the specified data and the set number.
+ ///
+ /// The data to check.
+ /// The set to check.
+ /// The distance.
+ public double GetDistance(IMLData data, int set)
+ {
+ double result = 0;
+ for(int i = 0; i < data.Count; i++)
+ {
+ var val = data[i] - _matrix[set][i];
+ result += val * val;
+ }
+ return Math.Sqrt(result);
+ }
+
+ ///
/// Get the smallest distance.
///
/// The data to check.
diff --git a/encog-core-cs/MathUtil/Error/ErrorCalculation.cs b/encog-core-cs/MathUtil/Error/ErrorCalculation.cs
index 50bfc6bb..9a8e8650 100644
--- a/encog-core-cs/MathUtil/Error/ErrorCalculation.cs
+++ b/encog-core-cs/MathUtil/Error/ErrorCalculation.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
// http://www.heatonresearch.com/copyright
//
using System;
+using Encog.ML.Data;
namespace Encog.MathUtil.Error
{
@@ -142,7 +143,52 @@ public void UpdateError(double[] actual, double[] ideal, double significance)
_setSize += ideal.Length;
}
- ///
+ ///
+ /// Called to update for each number that should be checked.
+ ///
+ public void UpdateError(IMLData actual, IMLData ideal, double significance)
+ {
+ for(int i = 0; i < actual.Count; i++)
+ {
+ double delta = ideal[i] - actual[i];
+
+ _globalError += delta * delta;
+ }
+
+ _setSize += ideal.Count;
+ }
+
+ ///
+ /// Called to update for each number that should be checked.
+ ///
+ public void UpdateError(double[] actual, IMLData ideal, double significance)
+ {
+ for(int i = 0; i < actual.Length; i++)
+ {
+ double delta = ideal[i] - actual[i];
+
+ _globalError += delta * delta;
+ }
+
+ _setSize += ideal.Count;
+ }
+
+ ///
+ /// Called to update for each number that should be checked.
+ ///
+ public void UpdateError(IMLData actual, double[] ideal, double significance)
+ {
+ for(int i = 0; i < actual.Count; i++)
+ {
+ double delta = ideal[i] - actual[i];
+
+ _globalError += delta * delta;
+ }
+
+ _setSize += ideal.Length;
+ }
+
+ ///
/// Update the error with single values.
///
/// The actual value.
diff --git a/encog-core-cs/MathUtil/Error/ErrorCalculationMode.cs b/encog-core-cs/MathUtil/Error/ErrorCalculationMode.cs
index b12c667c..4a9ae28a 100644
--- a/encog-core-cs/MathUtil/Error/ErrorCalculationMode.cs
+++ b/encog-core-cs/MathUtil/Error/ErrorCalculationMode.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/IntRange.cs b/encog-core-cs/MathUtil/IntRange.cs
index b4959041..b0dd4a35 100644
--- a/encog-core-cs/MathUtil/IntRange.cs
+++ b/encog-core-cs/MathUtil/IntRange.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/SupportClass.cs b/encog-core-cs/MathUtil/LIBSVM/SupportClass.cs
index 144a57a7..b5298636 100644
--- a/encog-core-cs/MathUtil/LIBSVM/SupportClass.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/SupportClass.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/Utility.cs b/encog-core-cs/MathUtil/LIBSVM/Utility.cs
index 12d2f030..6b48ad5a 100644
--- a/encog-core-cs/MathUtil/LIBSVM/Utility.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/Utility.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm.cs b/encog-core-cs/MathUtil/LIBSVM/svm.cs
index fb931063..2ad4d777 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm_model.cs b/encog-core-cs/MathUtil/LIBSVM/svm_model.cs
index 7014847e..9e60272d 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm_model.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm_model.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm_node.cs b/encog-core-cs/MathUtil/LIBSVM/svm_node.cs
index 18a3d357..2c47e976 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm_node.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm_node.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm_parameter.cs b/encog-core-cs/MathUtil/LIBSVM/svm_parameter.cs
index 2e4c9f18..316621b3 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm_parameter.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm_parameter.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm_print_interface.cs b/encog-core-cs/MathUtil/LIBSVM/svm_print_interface.cs
index 1b2ef759..e62d5da9 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm_print_interface.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm_print_interface.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LIBSVM/svm_problem.cs b/encog-core-cs/MathUtil/LIBSVM/svm_problem.cs
index c1599ccb..cc1ceb2f 100644
--- a/encog-core-cs/MathUtil/LIBSVM/svm_problem.cs
+++ b/encog-core-cs/MathUtil/LIBSVM/svm_problem.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/LinearCongruentialGenerator.cs b/encog-core-cs/MathUtil/LinearCongruentialGenerator.cs
index 8a0e87f7..f158b883 100644
--- a/encog-core-cs/MathUtil/LinearCongruentialGenerator.cs
+++ b/encog-core-cs/MathUtil/LinearCongruentialGenerator.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/MathConst.cs b/encog-core-cs/MathUtil/MathConst.cs
index f3dba2a1..ca0a6548 100644
--- a/encog-core-cs/MathUtil/MathConst.cs
+++ b/encog-core-cs/MathUtil/MathConst.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/BiPolarUtil.cs b/encog-core-cs/MathUtil/Matrices/BiPolarUtil.cs
index 4134c1de..13bbf8d9 100644
--- a/encog-core-cs/MathUtil/Matrices/BiPolarUtil.cs
+++ b/encog-core-cs/MathUtil/Matrices/BiPolarUtil.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Decomposition/CholeskyDecomposition.cs b/encog-core-cs/MathUtil/Matrices/Decomposition/CholeskyDecomposition.cs
index 49727ea9..372f88fc 100644
--- a/encog-core-cs/MathUtil/Matrices/Decomposition/CholeskyDecomposition.cs
+++ b/encog-core-cs/MathUtil/Matrices/Decomposition/CholeskyDecomposition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Decomposition/EigenvalueDecomposition.cs b/encog-core-cs/MathUtil/Matrices/Decomposition/EigenvalueDecomposition.cs
index 973d3649..3ad99d02 100644
--- a/encog-core-cs/MathUtil/Matrices/Decomposition/EigenvalueDecomposition.cs
+++ b/encog-core-cs/MathUtil/Matrices/Decomposition/EigenvalueDecomposition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Decomposition/LUDecomposition.cs b/encog-core-cs/MathUtil/Matrices/Decomposition/LUDecomposition.cs
index 5e9609fa..6006da3e 100644
--- a/encog-core-cs/MathUtil/Matrices/Decomposition/LUDecomposition.cs
+++ b/encog-core-cs/MathUtil/Matrices/Decomposition/LUDecomposition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Decomposition/QRDecomposition.cs b/encog-core-cs/MathUtil/Matrices/Decomposition/QRDecomposition.cs
index f63f937b..e60152d1 100644
--- a/encog-core-cs/MathUtil/Matrices/Decomposition/QRDecomposition.cs
+++ b/encog-core-cs/MathUtil/Matrices/Decomposition/QRDecomposition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Decomposition/SingularValueDecomposition.cs b/encog-core-cs/MathUtil/Matrices/Decomposition/SingularValueDecomposition.cs
index 532d8263..9346a244 100644
--- a/encog-core-cs/MathUtil/Matrices/Decomposition/SingularValueDecomposition.cs
+++ b/encog-core-cs/MathUtil/Matrices/Decomposition/SingularValueDecomposition.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Hessian/BasicHessian.cs b/encog-core-cs/MathUtil/Matrices/Hessian/BasicHessian.cs
index de583cf3..244e69cd 100644
--- a/encog-core-cs/MathUtil/Matrices/Hessian/BasicHessian.cs
+++ b/encog-core-cs/MathUtil/Matrices/Hessian/BasicHessian.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -33,111 +33,114 @@ namespace Encog.MathUtil.Matrices.Hessian
public abstract class BasicHessian : IComputeHessian
{
///
- /// The derivatives.
+ /// The training data that provides the ideal values.
///
- protected double[] derivative;
+ protected IMLDataSet _training;
///
- /// The flat network.
+ /// The neural network that we would like to train.
///
- protected FlatNetwork flat;
+ protected BasicNetwork _network;
- ///
- /// The gradients of the Hessian.
- ///
- protected double[] gradients;
///
- /// The Hessian 2d array.
+ /// The sum of square error.
///
- protected double[][] hessian;
+ protected double _sse;
///
- /// The Hessian matrix.
+ /// The gradients of the Hessian.
///
- protected Matrix hessianMatrix;
+ protected double[] _gradients;
///
- /// The neural network that we would like to train.
+ /// The Hessian matrix.
///
- protected BasicNetwork network;
-
+ protected Matrix _hessianMatrix;
///
- /// The sum of square error.
+ /// The Hessian 2d array.
///
- protected double sse;
+ protected double[][] _hessian;
///
- /// The training data that provides the ideal values.
+ /// The flat network.
///
- protected IMLDataSet training;
-
- #region IComputeHessian Members
+ protected FlatNetwork _flat;
///
public virtual void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
{
+
int weightCount = theNetwork.Structure.Flat.Weights.Length;
- flat = theNetwork.Flat;
- training = theTraining;
- network = theNetwork;
- gradients = new double[weightCount];
- hessianMatrix = new Matrix(weightCount, weightCount);
- hessian = hessianMatrix.Data;
- derivative = new double[weightCount];
+ _flat = theNetwork.Flat;
+ _training = theTraining;
+ _network = theNetwork;
+ _gradients = new double[weightCount];
+ _hessianMatrix = new Matrix(weightCount, weightCount);
+ _hessian = _hessianMatrix.Data;
}
+ ///
+ public abstract void Compute();
+
///
public double[] Gradients
{
- get { return gradients; }
+ get
+ {
+ return _gradients;
+ }
}
///
public Matrix HessianMatrix
{
- get { return hessianMatrix; }
+ get
+ {
+ return _hessianMatrix;
+ }
}
///
public double[][] Hessian
{
- get { return hessian; }
+ get
+ {
+ return _hessian;
+ }
}
///
public void Clear()
{
- EngineArray.Fill(gradients, 0);
- hessianMatrix.Clear();
+ EngineArray.Fill(_gradients, 0);
+ _hessianMatrix.Clear();
}
///
public double SSE
{
- get { return sse; }
+ get
+ {
+ return _sse;
+ }
}
-
- ///
- public abstract void Compute();
-
- #endregion
-
///
- /// Update the Hessian, sum's with what is in the Hessian already. Call clear to clear out old Hessian.
+ /// Update the Hessian, sum's with what is in the Hessian already.
+ /// Call clear to clear out old Hessian.
///
- /// The first derivatives to update with.
+ /// The values to sum into the current Hessian
public void UpdateHessian(double[] d)
{
// update the hessian
- int weightCount = network.Flat.Weights.Length;
+ int weightCount = _network.Flat.Weights.Length;
for (int i = 0; i < weightCount; i++)
{
for (int j = 0; j < weightCount; j++)
{
- hessian[i][j] += 2*d[i]*d[j];
+ _hessian[i][j] += d[i] * d[j];
}
}
}
diff --git a/encog-core-cs/MathUtil/Matrices/Hessian/ChainRuleWorker.cs b/encog-core-cs/MathUtil/Matrices/Hessian/ChainRuleWorker.cs
index 6dfca760..1c278d5d 100644
--- a/encog-core-cs/MathUtil/Matrices/Hessian/ChainRuleWorker.cs
+++ b/encog-core-cs/MathUtil/Matrices/Hessian/ChainRuleWorker.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -22,117 +22,123 @@
//
using Encog.Engine.Network.Activation;
using Encog.ML.Data;
-using Encog.ML.Data.Basic;
using Encog.Neural.Flat;
using Encog.Util;
-using Encog.Util.Concurrency;
namespace Encog.MathUtil.Matrices.Hessian
{
///
- /// A threaded worker that is used to calculate the first derivatives of the
- /// output of the neural network. These values are ultimatly used to calculate
- /// the Hessian.
+ /// A threaded worker that is used to calculate the first derivatives of the
+ /// output of the neural network. These values are ultimatly used to calculate
+ /// the Hessian.
///
- public class ChainRuleWorker : IEngineTask
+ public class ChainRuleWorker
{
///
- /// The actual values from the neural network.
+ /// The actual values from the neural network.
///
private readonly double[] _actual;
///
- /// The current first derivatives.
- ///
- private readonly double[] _derivative;
-
- ///
- /// The flat network.
+ /// The flat network.
///
private readonly FlatNetwork _flat;
///
- /// The gradients.
+ /// The gradients.
///
private readonly double[] _gradients;
///
- /// The high range.
+ /// The high range.
///
private readonly int _high;
///
- /// The neuron counts, per layer.
+ /// The neuron counts, per layer.
///
private readonly int[] _layerCounts;
///
- /// The deltas for each layer.
+ /// The deltas for each layer.
///
private readonly double[] _layerDelta;
///
- /// The feed counts, per layer.
+ /// The feed counts, per layer.
///
private readonly int[] _layerFeedCounts;
///
- /// The layer indexes.
+ /// The layer indexes.
///
private readonly int[] _layerIndex;
///
- /// The output from each layer.
+ /// The output from each layer.
///
private readonly double[] _layerOutput;
///
- /// The sums.
+ /// The sums.
///
private readonly double[] _layerSums;
///
- /// The low range.
+ /// The low range.
///
private readonly int _low;
///
- /// The pair to use for training.
+ /// The total first derivatives.
///
- private readonly IMLDataPair _pair;
+ private readonly double[] _totDeriv;
///
- /// The total first derivatives.
+ /// The training data.
///
- private readonly double[] _totDeriv;
+ private readonly IMLDataSet _training;
///
- /// The training data.
+ /// The weight count.
///
- private readonly IMLDataSet _training;
+ private readonly int _weightCount;
///
- /// The index to each layer's weights and thresholds.
+ /// The index to each layer's weights and thresholds.
///
private readonly int[] _weightIndex;
///
- /// The weights and thresholds.
+ /// The weights and thresholds.
///
private readonly double[] _weights;
///
- /// The error.
+ /// The error.
///
private double _error;
///
- /// The output neuron to calculate for.
+ /// The hessian for this worker.
+ ///
+ private readonly double[][] _hessian;
+
+ ///
+ /// The output neuron to calculate for.
///
private int _outputNeuron;
///
- /// Construct the chain rule worker.
+ /// The Hessian matrix.
+ ///
+ public double[][] Hessian
+ {
+ get { return _hessian; }
+ }
+
+ ///
+ /// Construct the chain rule worker.
///
/// The network to calculate a Hessian for.
/// The training data.
@@ -140,16 +146,16 @@ public class ChainRuleWorker : IEngineTask
/// The high range.
public ChainRuleWorker(FlatNetwork theNetwork, IMLDataSet theTraining, int theLow, int theHigh)
{
- int weightCount = theNetwork.Weights.Length;
+ _weightCount = theNetwork.Weights.Length;
+ _hessian = EngineArray.AllocateDouble2D(_weightCount,_weightCount);
_training = theTraining;
_flat = theNetwork;
_layerDelta = new double[_flat.LayerOutput.Length];
_actual = new double[_flat.OutputCount];
- _derivative = new double[weightCount];
- _totDeriv = new double[weightCount];
- _gradients = new double[weightCount];
+ _totDeriv = new double[_weightCount];
+ _gradients = new double[_weightCount];
_weights = _flat.Weights;
_layerIndex = _flat.LayerIndex;
@@ -160,12 +166,11 @@ public ChainRuleWorker(FlatNetwork theNetwork, IMLDataSet theTraining, int theLo
_layerFeedCounts = _flat.LayerFeedCounts;
_low = theLow;
_high = theHigh;
- _pair = BasicMLDataPair.CreatePair(_flat.InputCount, _flat.OutputCount);
}
///
- /// The output neuron we are processing.
+ /// The output neuron we are processing.
///
public int OutputNeuron
{
@@ -174,7 +179,7 @@ public int OutputNeuron
}
///
- /// The first derivatives, used to calculate the Hessian.
+ /// The first derivatives, used to calculate the Hessian.
///
public double[] Derivative
{
@@ -183,7 +188,7 @@ public double[] Derivative
///
- /// The gradients.
+ /// The gradients.
///
public double[] Gradients
{
@@ -191,7 +196,7 @@ public double[] Gradients
}
///
- /// The SSE error.
+ /// The SSE error.
///
public double Error
{
@@ -199,7 +204,7 @@ public double Error
}
///
- /// The flat network.
+ /// The flat network.
///
public FlatNetwork Network
{
@@ -208,21 +213,23 @@ public FlatNetwork Network
#region IEngineTask Members
- ///
+ ///
public void Run()
{
_error = 0;
+ EngineArray.Fill(_hessian, 0);
EngineArray.Fill(_totDeriv, 0);
EngineArray.Fill(_gradients, 0);
+ var derivative = new double[_weightCount];
// Loop over every training element
for (int i = _low; i <= _high; i++)
{
- _training.GetRecord(i, _pair);
+ IMLDataPair pair = _training[i];
- EngineArray.Fill(_derivative, 0);
- Process(_outputNeuron, _pair.InputArray, _pair.IdealArray);
+ EngineArray.Fill(derivative, 0);
+ Process(_outputNeuron, derivative, pair);
}
}
@@ -232,13 +239,13 @@ public void Run()
/// Process one training set element.
///
/// The output neuron.
- /// The network input.
- /// The ideal values.
- private void Process(int outputNeuron, double[] input, double[] ideal)
+ /// The derivatives.
+ /// The training pair.
+ private void Process(int outputNeuron, double[] derivative, IMLDataPair pair)
{
- _flat.Compute(input, _actual);
+ _flat.Compute(pair.Input, _actual);
- double e = ideal[outputNeuron] - _actual[outputNeuron];
+ double e = pair.Ideal[outputNeuron] - _actual[outputNeuron];
_error += e*e;
for (int i = 0; i < _actual.Length; i++)
@@ -257,22 +264,32 @@ private void Process(int outputNeuron, double[] input, double[] ideal)
for (int i = _flat.BeginTraining; i < _flat.EndTraining; i++)
{
- ProcessLevel(i);
+ ProcessLevel(i, derivative);
}
// calculate gradients
for (int j = 0; j < _weights.Length; j++)
{
- _gradients[j] += e*_derivative[j];
- _totDeriv[j] += _derivative[j];
+ _gradients[j] += e*derivative[j];
+ _totDeriv[j] += derivative[j];
+ }
+
+ // update hessian
+ for (int i = 0; i < _weightCount; i++)
+ {
+ for (int j = 0; j < _weightCount; j++)
+ {
+ _hessian[i][j] += derivative[i] * derivative[j];
+ }
}
}
///
- /// Process one level.
+ /// Process one level.
///
/// The level.
- private void ProcessLevel(int currentLevel)
+ /// The derivatives.
+ private void ProcessLevel(int currentLevel, double[] derivative)
{
int fromLayerIndex = _layerIndex[currentLevel + 1];
int toLayerIndex = _layerIndex[currentLevel];
@@ -293,14 +310,14 @@ private void ProcessLevel(int currentLevel)
int wi = index + y;
for (int x = 0; x < toLayerSize; x++)
{
- _derivative[wi] += output*_layerDelta[xi];
+ derivative[wi] += output*_layerDelta[xi];
sum += _weights[wi]*_layerDelta[xi];
wi += fromLayerSize;
xi++;
}
_layerDelta[yi] = sum
- *(activation.DerivativeFunction(_layerSums[yi], _layerOutput[yi]));
+ *(activation.DerivativeFunction(_layerSums[yi], _layerOutput[yi]));
yi++;
}
}
diff --git a/encog-core-cs/MathUtil/Matrices/Hessian/HessianCR.cs b/encog-core-cs/MathUtil/Matrices/Hessian/HessianCR.cs
index f1eede47..3cd38fff 100644
--- a/encog-core-cs/MathUtil/Matrices/Hessian/HessianCR.cs
+++ b/encog-core-cs/MathUtil/Matrices/Hessian/HessianCR.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,58 +20,46 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
+using System.Threading.Tasks;
using Encog.ML.Data;
using Encog.Neural.Flat;
using Encog.Neural.Networks;
+using Encog.Util;
using Encog.Util.Concurrency;
namespace Encog.MathUtil.Matrices.Hessian
{
///
- /// Calculate the Hessian matrix using the chain rule method.
+ /// Calculate the Hessian matrix using the chain rule method.
///
public class HessianCR : BasicHessian, IMultiThreadable
{
///
- /// The number of threads to use.
- ///
- private int _numThreads;
-
- ///
- /// The workers.
+ /// The workers.
///
private ChainRuleWorker[] _workers;
- #region IMultiThreadable Members
-
///
- /// Set the number of threads. Specify zero to tell Encog to automatically
- /// determine the best number of threads for the processor. If OpenCL is used
- /// as the target device, then this value is not used.
+ /// The number of threads to use.
///
- public int ThreadCount
- {
- get { return _numThreads; }
- set { _numThreads = value; }
- }
+ public int ThreadCount { get; set; }
- #endregion
- ///
+ ///
public override void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
{
base.Init(theNetwork, theTraining);
int weightCount = theNetwork.Structure.Flat.Weights.Length;
- training = theTraining;
- network = theNetwork;
+ _training = theTraining;
+ _network = theNetwork;
- hessianMatrix = new Matrix(weightCount, weightCount);
- hessian = hessianMatrix.Data;
+ _hessianMatrix = new Matrix(weightCount, weightCount);
+ _hessian = _hessianMatrix.Data;
// create worker(s)
var determine = new DetermineWorkload(
- _numThreads, (int) training.Count);
+ ThreadCount, _training.Count);
_workers = new ChainRuleWorker[determine.ThreadCount];
@@ -80,44 +68,34 @@ public override void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
// handle CPU
foreach (IntRange r in determine.CalculateWorkers())
{
- _workers[index++] = new ChainRuleWorker((FlatNetwork) flat.Clone(),
- training.OpenAdditional(), r.Low,
- r.High);
+ _workers[index++] = new ChainRuleWorker((FlatNetwork) _flat.Clone(),
+ _training.OpenAdditional(), r.Low,
+ r.High);
}
}
- ///
+ ///
public override void Compute()
{
Clear();
double e = 0;
- int weightCount = network.Flat.Weights.Length;
+ int weightCount = _network.Flat.Weights.Length;
- for (int outputNeuron = 0; outputNeuron < network.OutputCount; outputNeuron++)
+ for (int outputNeuron = 0; outputNeuron < _network.OutputCount; outputNeuron++)
{
// handle context
- if (flat.HasContext)
+ if (_flat.HasContext)
{
_workers[0].Network.ClearContext();
}
- if (_workers.Length > 1)
+ int neuron = outputNeuron;
+ Parallel.ForEach(_workers, worker =>
{
- TaskGroup group = EngineConcurrency.Instance.CreateTaskGroup();
+ worker.OutputNeuron = neuron;
+ worker.Run();
+ });
- foreach (ChainRuleWorker worker in _workers)
- {
- worker.OutputNeuron = outputNeuron;
- EngineConcurrency.Instance.ProcessTask(worker, group);
- }
-
- group.WaitForComplete();
- }
- else
- {
- _workers[0].OutputNeuron = outputNeuron;
- _workers[0].Run();
- }
// aggregate workers
@@ -126,13 +104,13 @@ public override void Compute()
e += worker.Error;
for (int i = 0; i < weightCount; i++)
{
- gradients[i] += worker.Gradients[i];
+ _gradients[i] += worker.Gradients[i];
}
- UpdateHessian(worker.Derivative);
+ EngineArray.ArrayAdd(Hessian, worker.Hessian);
}
}
- sse = e/2;
+ _sse = e/2;
}
}
}
diff --git a/encog-core-cs/MathUtil/Matrices/Hessian/HessianFD.cs b/encog-core-cs/MathUtil/Matrices/Hessian/HessianFD.cs
index 7234e10b..aa3ccec5 100644
--- a/encog-core-cs/MathUtil/Matrices/Hessian/HessianFD.cs
+++ b/encog-core-cs/MathUtil/Matrices/Hessian/HessianFD.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -30,108 +30,109 @@
namespace Encog.MathUtil.Matrices.Hessian
{
///
- /// Calculate the Hessian matrix using the finite difference method. This is a
- /// very simple method of calculating the Hessian. The algorithm does not vary
- /// greatly by number layers. This makes it very useful as a tool to check the
- /// accuracy of other methods of determining the Hessian.
- ///
- /// For more information on the Finite Difference Method see the following article.
- ///
- /// http://en.wikipedia.org/wiki/Finite_difference_method
+ /// Calculate the Hessian matrix using the finite difference method. This is a
+ /// very simple method of calculating the Hessian. The algorithm does not vary
+ /// greatly by number layers. This makes it very useful as a tool to check the
+ /// accuracy of other methods of determining the Hessian.
+ /// For more information on the Finite Difference Method see the following article.
+ /// http://en.wikipedia.org/wiki/Finite_difference_method
///
public class HessianFD : BasicHessian
{
///
- /// The initial step size for dStep.
+ /// The initial step size for dStep.
///
public const double InitialStep = 0.001;
///
- /// The center of the point array.
+ /// The center of the point array.
///
private int _center;
///
- /// The derivative coefficient, used for the finite difference method.
+ /// The derivative coefficient, used for the finite difference method.
///
private double[] _dCoeff;
///
- /// The derivative step size, used for the finite difference method.
+ /// The derivative step size, used for the finite difference method.
///
private double[] _dStep;
///
- /// The number of points actually used, which is (pointsPerSide*2)+1.
+ /// The number of points actually used, which is (pointsPerSide*2)+1.
///
private int _pointCount;
///
- /// The number of points requested per side. This determines the accuracy of the calculation.
+ /// The weight count.
///
- private int _pointsPerSide = 5;
+ private int _weightCount;
+
///
- /// The number of points per side.
+ /// Construct the HessianFD.
///
- public int PointsPerSide
+ public HessianFD()
{
- get { return _pointsPerSide; }
- set { _pointsPerSide = value; }
+ PointsPerSide = 5;
}
- ///
- public new void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
+ ///
+ /// The number of points requested per side. This determines the accuracy of the calculation.
+ ///
+ private int PointsPerSide { get; set; }
+
+ ///
+ public override void Init(BasicNetwork theNetwork, IMLDataSet theTraining)
{
base.Init(theNetwork, theTraining);
- int weightCount = theNetwork.Structure.Flat.Weights.Length;
+ _weightCount = theNetwork.Structure.Flat.Weights.Length;
- _center = _pointsPerSide + 1;
- _pointCount = (_pointsPerSide*2) + 1;
+ _center = PointsPerSide + 1;
+ _pointCount = (PointsPerSide*2) + 1;
_dCoeff = CreateCoefficients();
- _dStep = new double[weightCount];
+ _dStep = new double[_weightCount];
- for (int i = 0; i < weightCount; i++)
+ for (int i = 0; i < _weightCount; i++)
{
_dStep[i] = InitialStep;
}
}
- ///
+ ///
public override void Compute()
{
- sse = 0;
+ _sse = 0;
- for (int i = 0; i < network.OutputCount; i++)
+ for (int i = 0; i < _network.OutputCount; i++)
{
InternalCompute(i);
}
}
- ///
- /// Called internally to compute each output neuron.
- ///
- /// The output neuron to compute.
+ ///
private void InternalCompute(int outputNeuron)
{
int row = 0;
var error = new ErrorCalculation();
- EngineArray.Fill(derivative, 0);
+
+ var derivative = new double[_weightCount];
// Loop over every training element
- foreach (var pair in training)
+ foreach (IMLDataPair pair in _training)
{
- var networkOutput = network.Compute(pair.Input);
-
- double e = pair.Ideal.Data[outputNeuron] - networkOutput[outputNeuron];
+ EngineArray.Fill(derivative, 0);
+ IMLData networkOutput = _network.Compute(pair.Input);
+ double e = pair.Ideal[outputNeuron] - networkOutput[outputNeuron];
error.UpdateError(networkOutput[outputNeuron], pair.Ideal[outputNeuron]);
int currentWeight = 0;
// loop over the output weights
- int outputFeedCount = network.GetLayerTotalNeuronCount(network.LayerCount - 2);
- for (int i = 0; i < network.OutputCount; i++)
+ int outputFeedCount = _network.GetLayerTotalNeuronCount(_network.LayerCount - 2);
+ for (int i = 0; i < _network.OutputCount; i++)
{
for (int j = 0; j < outputFeedCount; j++)
{
@@ -140,55 +141,54 @@ private void InternalCompute(int outputNeuron)
if (i == outputNeuron)
{
jc = ComputeDerivative(pair.Input, outputNeuron,
- currentWeight, _dStep,
- networkOutput[outputNeuron], row);
+ currentWeight, _dStep,
+ networkOutput[outputNeuron], row);
}
else
{
jc = 0;
}
- gradients[currentWeight] += jc*e;
- derivative[currentWeight] += jc;
+ _gradients[currentWeight] += jc*e;
+ derivative[currentWeight] = jc;
currentWeight++;
}
}
// Loop over every weight in the neural network
- while (currentWeight < network.Flat.Weights.Length)
+ while (currentWeight < _network.Flat.Weights.Length)
{
double jc = ComputeDerivative(
pair.Input, outputNeuron, currentWeight,
_dStep,
networkOutput[outputNeuron], row);
- derivative[currentWeight] += jc;
- gradients[currentWeight] += jc*e;
+ derivative[currentWeight] = jc;
+ _gradients[currentWeight] += jc*e;
currentWeight++;
}
row++;
+ UpdateHessian(derivative);
}
- UpdateHessian(derivative);
- sse += error.CalculateSSE();
+ _sse += error.CalculateSSE();
}
///
- /// Computes the derivative of the output of the neural network with respect to a weight.
+ /// Computes the derivative of the output of the neural network with respect to a weight.
///
/// The input data to the neural network.
- /// The output neuron to calculate for.
- /// The weight.
- /// The step size.
- /// The output from the neural network.
- /// The training row currently being processed.
+ /// The weight.
+ /// The step size.
+ /// The output from the neural network.
+ /// The training row currently being processed.
+ ///
/// The derivative output.
private double ComputeDerivative(IMLData inputData, int outputNeuron, int weight, double[] stepSize,
- double networkOutput, int row)
+ double networkOutput, int row)
{
- double temp = network.Flat.Weights[weight];
-
+ double temp = _network.Flat.Weights[weight];
var points = new double[_dCoeff.Length];
stepSize[row] = Math.Max(InitialStep*Math.Abs(temp), InitialStep);
@@ -203,26 +203,24 @@ private double ComputeDerivative(IMLData inputData, int outputNeuron, int weight
double newWeight = temp + ((i - _center))
*stepSize[row];
- network.Flat.Weights[weight] = newWeight;
+ _network.Flat.Weights[weight] = newWeight;
- IMLData output = network.Compute(inputData);
- points[i] = output.Data[outputNeuron];
+ IMLData output = _network.Compute(inputData);
+ points[i] = output[outputNeuron];
}
double result = _dCoeff.Select((t, i) => t*points[i]).Sum();
result /= Math.Pow(stepSize[row], 1);
- network.Flat.Weights[weight] = temp;
+ _network.Flat.Weights[weight] = temp;
return result;
}
///
- /// Compute finite difference coefficients according to the method provided here:
- ///
- /// http://en.wikipedia.org/wiki/Finite_difference_coefficients
- ///
+ /// Compute finite difference coefficients according to the method provided here:
+ /// http://en.wikipedia.org/wiki/Finite_difference_coefficients
///
/// An array of the coefficients for FD.
public double[] CreateCoefficients()
@@ -250,7 +248,8 @@ public double[] CreateCoefficients()
for (int k = 0; k < _pointCount; k++)
{
- result[k] = (Math.Round(invMatrix.Data[1][k]*f))/f;
+ result[k] = (Math
+ .Round(invMatrix.Data[1][k]*f))/f;
}
diff --git a/encog-core-cs/MathUtil/Matrices/Hessian/IComputeHessian.cs b/encog-core-cs/MathUtil/Matrices/Hessian/IComputeHessian.cs
index 5ae5c1bc..cbb35042 100644
--- a/encog-core-cs/MathUtil/Matrices/Hessian/IComputeHessian.cs
+++ b/encog-core-cs/MathUtil/Matrices/Hessian/IComputeHessian.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/Matrix.cs b/encog-core-cs/MathUtil/Matrices/Matrix.cs
index 3798dd0d..f1193f36 100644
--- a/encog-core-cs/MathUtil/Matrices/Matrix.cs
+++ b/encog-core-cs/MathUtil/Matrices/Matrix.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
using System;
using Encog.MathUtil.Matrices.Decomposition;
using Encog.MathUtil.Randomize;
+using Encog.ML.Data;
#if logging
#endif
@@ -101,7 +102,26 @@ public static Matrix CreateRowMatrix(double[] input)
return new Matrix(d);
}
- ///
+ ///
+ /// Create a matrix that is a single row.
+ ///
+ /// A 1D array to make the matrix from.
+ /// A matrix that contans a single row.
+ public static Matrix CreateRowMatrix(IMLData input)
+ {
+ var d = new double[1][];
+
+ d[0] = new double[input.Count];
+
+ for(int i = 0; i < input.Count; i++)
+ {
+ d[0][i] = input[i];
+ }
+
+ return new Matrix(d);
+ }
+
+ ///
/// The matrix data, stored as a 2D array.
///
private readonly double[][] matrix;
diff --git a/encog-core-cs/MathUtil/Matrices/MatrixError.cs b/encog-core-cs/MathUtil/Matrices/MatrixError.cs
index 639e2db6..5b12506e 100644
--- a/encog-core-cs/MathUtil/Matrices/MatrixError.cs
+++ b/encog-core-cs/MathUtil/Matrices/MatrixError.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Matrices/MatrixMath.cs b/encog-core-cs/MathUtil/Matrices/MatrixMath.cs
index 877d0865..819ca6d9 100644
--- a/encog-core-cs/MathUtil/Matrices/MatrixMath.cs
+++ b/encog-core-cs/MathUtil/Matrices/MatrixMath.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/NumericRange.cs b/encog-core-cs/MathUtil/NumericRange.cs
index dec625cf..0659cf06 100644
--- a/encog-core-cs/MathUtil/NumericRange.cs
+++ b/encog-core-cs/MathUtil/NumericRange.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -92,7 +92,7 @@ public NumericRange(IList values)
_rms = Math.Sqrt(rmsTotal/_samples);
// now get the standard deviation
- double devTotal = values.Sum(d => Math.Pow(d - _mean, 2));
+ double devTotal = values.Sum(d => (d - _mean) * (d - _mean));
_standardDeviation = Math.Sqrt(devTotal/_samples);
}
diff --git a/encog-core-cs/MathUtil/RBF/BasicRBF.cs b/encog-core-cs/MathUtil/RBF/BasicRBF.cs
index 0c9ad338..9df3350c 100644
--- a/encog-core-cs/MathUtil/RBF/BasicRBF.cs
+++ b/encog-core-cs/MathUtil/RBF/BasicRBF.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/RBF/GaussianFunction.cs b/encog-core-cs/MathUtil/RBF/GaussianFunction.cs
index e62de360..2cb586e6 100644
--- a/encog-core-cs/MathUtil/RBF/GaussianFunction.cs
+++ b/encog-core-cs/MathUtil/RBF/GaussianFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/RBF/IRadialBasisFunction.cs b/encog-core-cs/MathUtil/RBF/IRadialBasisFunction.cs
index 0977d7f6..426eef7d 100644
--- a/encog-core-cs/MathUtil/RBF/IRadialBasisFunction.cs
+++ b/encog-core-cs/MathUtil/RBF/IRadialBasisFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/RBF/InverseMultiquadricFunction.cs b/encog-core-cs/MathUtil/RBF/InverseMultiquadricFunction.cs
index 3001d949..a8b3ffda 100644
--- a/encog-core-cs/MathUtil/RBF/InverseMultiquadricFunction.cs
+++ b/encog-core-cs/MathUtil/RBF/InverseMultiquadricFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -91,7 +91,8 @@ public override double Calculate(double[] x)
for (int i = 0; i < center.Length; i++)
{
- value += Math.Pow(x[i] - center[i], 2) + (width*width);
+ var inner = x[i] - center[i];
+ value += inner * inner + (width*width);
}
return Peak/BoundMath.Sqrt(value);
}
diff --git a/encog-core-cs/MathUtil/RBF/MexicanHatFunction.cs b/encog-core-cs/MathUtil/RBF/MexicanHatFunction.cs
index 757a41a0..5cf1221c 100644
--- a/encog-core-cs/MathUtil/RBF/MexicanHatFunction.cs
+++ b/encog-core-cs/MathUtil/RBF/MexicanHatFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -89,13 +89,14 @@ public MexicanHatFunction(double center, double peak,
public override double Calculate(double[] x)
{
double[] center = Centers;
+ double width = Width;
// calculate the "norm", but don't take square root
// don't square because we are just going to square it
double norm = 0;
for (int i = 0; i < center.Length; i++)
{
- norm += Math.Pow(x[i] - center[i], 2);
+ norm += Math.Pow(x[i] - center[i], 2) / (2.0 * width * width);
}
// calculate the value
diff --git a/encog-core-cs/MathUtil/RBF/MultiquadricFunction.cs b/encog-core-cs/MathUtil/RBF/MultiquadricFunction.cs
index e4089150..46cbbda4 100644
--- a/encog-core-cs/MathUtil/RBF/MultiquadricFunction.cs
+++ b/encog-core-cs/MathUtil/RBF/MultiquadricFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -93,8 +93,8 @@ public override double Calculate(double[] x)
for (int i = 0; i < center.Length; i++)
{
- value += Math.Pow(x[i] - center[i], 2)
- + (width*width);
+ var inner = x[i] - center[i];
+ value += inner * inner + width * width;
}
return Peak*BoundMath.Sqrt(value);
}
diff --git a/encog-core-cs/MathUtil/RBF/RBFEnum.cs b/encog-core-cs/MathUtil/RBF/RBFEnum.cs
index 0b38c07b..00fccbfd 100644
--- a/encog-core-cs/MathUtil/RBF/RBFEnum.cs
+++ b/encog-core-cs/MathUtil/RBF/RBFEnum.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/BasicRandomizer.cs b/encog-core-cs/MathUtil/Randomize/BasicRandomizer.cs
index c5f5ec3c..3922dd70 100644
--- a/encog-core-cs/MathUtil/Randomize/BasicRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/BasicRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/ConsistentRandomizer.cs b/encog-core-cs/MathUtil/Randomize/ConsistentRandomizer.cs
index b13781be..8ef8cb6b 100644
--- a/encog-core-cs/MathUtil/Randomize/ConsistentRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/ConsistentRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/ConstRandomizer.cs b/encog-core-cs/MathUtil/Randomize/ConstRandomizer.cs
index f4dd9cbc..a1214f7d 100644
--- a/encog-core-cs/MathUtil/Randomize/ConstRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/ConstRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/Distort.cs b/encog-core-cs/MathUtil/Randomize/Distort.cs
index 76c3bee9..cf8bcc25 100644
--- a/encog-core-cs/MathUtil/Randomize/Distort.cs
+++ b/encog-core-cs/MathUtil/Randomize/Distort.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/EncogRandom.cs b/encog-core-cs/MathUtil/Randomize/EncogRandom.cs
new file mode 100644
index 00000000..84d4753c
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/EncogRandom.cs
@@ -0,0 +1,96 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.MathUtil.Randomize
+{
+ ///
+ /// This class is a thin layer on top of the existing random class. The main difference is
+ /// that it provides the NextGaussian function that is based on Java's Random class. All of Encog's
+ /// random number generation is based on this class. To create other random number generators,
+ /// that Encog can use, extend this class.
+ ///
+ ///
+ /// This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described
+ /// by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms,
+ /// section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the
+ /// cost of only one call to Math.log and one call to Math.sqrt.
+ ///
+ [Serializable]
+ public class EncogRandom : Random
+ {
+ ///
+ /// Do we have a next Gaussian.
+ ///
+ private bool haveNextNextGaussian = false;
+ private double nextNextGaussian = 0;
+
+ ///
+ /// Initializes a new instance of the Random class, using a time-dependent default seed value.
+ ///
+ public EncogRandom()
+ : base()
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the Random class, using the specified seed value.
+ ///
+ /// The seed.
+ public EncogRandom(Int32 seed)
+ : base(seed)
+ {
+ }
+
+ ///
+ /// Returns the next pseudorandom, Gaussian ("normally") distributed double value with
+ /// mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
+ ///
+ /// The random number.
+ public double NextGaussian()
+ {
+ if (haveNextNextGaussian)
+ {
+ haveNextNextGaussian = false;
+ return nextNextGaussian;
+ }
+ else
+ {
+ double v1, v2, s;
+ do
+ {
+ v1 = 2 * NextDouble() - 1; // between -1.0 and 1.0
+ v2 = 2 * NextDouble() - 1; // between -1.0 and 1.0
+ s = v1 * v1 + v2 * v2;
+ } while (s >= 1 || s == 0);
+ double multiplier = Math.Sqrt(-2 * Math.Log(s) / s);
+ nextNextGaussian = v2 * multiplier;
+ haveNextNextGaussian = true;
+ return v1 * multiplier;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Factory/BasicRandomFactory.cs b/encog-core-cs/MathUtil/Randomize/Factory/BasicRandomFactory.cs
new file mode 100644
index 00000000..18abccc3
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Factory/BasicRandomFactory.cs
@@ -0,0 +1,80 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.MathUtil.Randomize.Factory
+{
+ ///
+ /// Basic random number generator factory. Simply returns the Random class.
+ ///
+ [Serializable]
+ public class BasicRandomFactory : IRandomFactory
+ {
+ ///
+ /// A random generator to generate random seeds.
+ ///
+ private EncogRandom seedProducer;
+
+ ///
+ /// Construct a random generator factory. No assigned seed.
+ ///
+ public BasicRandomFactory()
+ {
+ this.seedProducer = new EncogRandom();
+ }
+
+ ///
+ /// Construct a random generator factory with the specified seed.
+ ///
+ /// The seed.
+ public BasicRandomFactory(int theSeed)
+ {
+ this.seedProducer = new EncogRandom(theSeed);
+ }
+
+ ///
+ /// Factor a new random generator.
+ ///
+ /// The random number generator.
+ public EncogRandom Factor()
+ {
+ lock (this)
+ {
+ int seed = this.seedProducer.Next();
+ return new EncogRandom(seed);
+ }
+ }
+
+ ///
+ /// Factor a new random generator factory.
+ ///
+ /// The random number generator generator.
+ public IRandomFactory FactorFactory()
+ {
+ return new BasicRandomFactory(this.seedProducer.Next());
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Factory/IRandomFactory.cs b/encog-core-cs/MathUtil/Randomize/Factory/IRandomFactory.cs
new file mode 100644
index 00000000..849f0e4d
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Factory/IRandomFactory.cs
@@ -0,0 +1,35 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.MathUtil.Randomize.Factory
+{
+ public interface IRandomFactory
+ {
+ EncogRandom Factor();
+ IRandomFactory FactorFactory();
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/FanInRandomizer.cs b/encog-core-cs/MathUtil/Randomize/FanInRandomizer.cs
index 6a4496f7..3e80d5fa 100644
--- a/encog-core-cs/MathUtil/Randomize/FanInRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/FanInRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
using Encog.MathUtil.Randomize;
using Encog.Neural.Networks;
-namespace Encog.Mathutil.Randomize
+namespace Encog.MathUtil.Randomize
{
///
/// A randomizer that attempts to create starting weight values that are
diff --git a/encog-core-cs/MathUtil/Randomize/GaussianRandomizer.cs b/encog-core-cs/MathUtil/Randomize/GaussianRandomizer.cs
index 40c7ae22..c099305a 100644
--- a/encog-core-cs/MathUtil/Randomize/GaussianRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/GaussianRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/AbstractBoxMuller.cs b/encog-core-cs/MathUtil/Randomize/Generate/AbstractBoxMuller.cs
new file mode 100644
index 00000000..f6de8372
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/AbstractBoxMuller.cs
@@ -0,0 +1,101 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// Provides the ability for subclasses to generate normally distributed random numbers.
+ ///
+ public abstract class AbstractBoxMuller : AbstractGenerateRandom
+ {
+ ///
+ /// The y2 value.
+ ///
+ private double _y2;
+
+ ///
+ /// Should we use the last value.
+ ///
+ private bool _useLast;
+
+ ///
+ /// The mean.
+ ///
+ public const double Mu = 0;
+
+ ///
+ /// The standard deviation.
+ ///
+ private const double Sigma = 1;
+
+
+ ///
+ public override double NextGaussian()
+ {
+ double y1;
+
+ // use value from previous call
+ if (_useLast)
+ {
+ y1 = _y2;
+ _useLast = false;
+ }
+ else
+ {
+ double x1;
+ double x2;
+ double w;
+ do
+ {
+ x1 = 2.0 * NextDouble() - 1.0;
+ x2 = 2.0 * NextDouble() - 1.0;
+ w = x1 * x1 + x2 * x2;
+ } while (w >= 1.0);
+
+ w = Math.Sqrt((-2.0 * Math.Log(w)) / w);
+ y1 = x1 * w;
+ _y2 = x2 * w;
+ _useLast = true;
+ }
+
+ return (Mu + y1 * Sigma);
+ }
+
+ ///
+ public abstract override double NextDouble();
+
+ ///
+ public abstract override bool NextBoolean();
+
+ ///
+ public abstract override float NextFloat();
+
+ ///
+ public abstract override long NextLong();
+
+ ///
+ public abstract override int NextInt();
+
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/AbstractGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/AbstractGenerateRandom.cs
new file mode 100644
index 00000000..5213bba2
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/AbstractGenerateRandom.cs
@@ -0,0 +1,75 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.MathUtil.Randomize.Generate;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// Provides a foundation for most random number generation. This allows the nextDouble to generate
+ /// the other types.
+ ///
+ public abstract class AbstractGenerateRandom : IGenerateRandom
+ {
+ ///
+ public int NextInt(int low, int high)
+ {
+ return (low + (int)(NextDouble() * ((high - low))));
+ }
+
+ ///
+ public double NextDouble(double high)
+ {
+ return NextDouble(0, high);
+ }
+
+ ///
+ public double NextDouble(double low, double high)
+ {
+ return (low + (NextDouble() * ((high - low))));
+ }
+
+ ///
+ public int NextInt(int range)
+ {
+ return NextInt(0, range);
+ }
+
+ ///
+ public abstract double NextDouble();
+
+ ///
+ public abstract bool NextBoolean();
+
+ ///
+ public abstract float NextFloat();
+
+ ///
+ public abstract double NextGaussian();
+
+ ///
+ public abstract long NextLong();
+
+ ///
+ public abstract int NextInt();
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/BasicGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/BasicGenerateRandom.cs
new file mode 100644
index 00000000..f81a1ebd
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/BasicGenerateRandom.cs
@@ -0,0 +1,84 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// A wrapper over C#'s built in random number generator.
+ ///
+ public class BasicGenerateRandom : AbstractBoxMuller
+ {
+ ///
+ /// The random number generator.
+ ///
+ protected readonly Random _rnd;
+
+ ///
+ /// Construct a random number generator with the specified seed.
+ ///
+ /// The seed.
+ public BasicGenerateRandom(int seed)
+ {
+ _rnd = new Random(seed);
+ }
+
+ ///
+ /// Construct a random number generator with a time-based seed.
+ ///
+ public BasicGenerateRandom()
+ {
+ _rnd = new Random();
+ }
+
+ ///
+ public override double NextDouble()
+ {
+ return _rnd.NextDouble();
+ }
+
+ ///
+ public override bool NextBoolean()
+ {
+ return _rnd.NextDouble() > 0.5;
+ }
+
+ ///
+ public override float NextFloat()
+ {
+ return (float)_rnd.NextDouble();
+ }
+
+ ///
+ public override long NextLong()
+ {
+ return _rnd.Next();
+ }
+
+ ///
+ public override int NextInt()
+ {
+ return _rnd.Next();
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/IGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/IGenerateRandom.cs
new file mode 100644
index 00000000..9d155347
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/IGenerateRandom.cs
@@ -0,0 +1,97 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// Interface that defines how random numbers are generated. Provides the means to generate both uniform
+ /// and normal (gaussian) distributed random numbers.
+ ///
+ public interface IGenerateRandom
+ {
+ ///
+ /// Generate the next normally distributed random number.
+ ///
+ /// The next normally distributed random number.
+ double NextGaussian();
+
+ ///
+ /// Generate the next random boolean.
+ ///
+ /// The next random boolean.
+ bool NextBoolean();
+
+ ///
+ /// Generate the next random long.
+ ///
+ /// The next random long.
+ long NextLong();
+
+ ///
+ /// Generate the next random floating point.
+ ///
+ /// The next random floating point.
+ float NextFloat();
+
+ ///
+ /// Generate the next random double.
+ ///
+ /// The next random double.
+ double NextDouble();
+
+ ///
+ /// The next random double up to a non-inclusive range.
+ ///
+ /// The highest desired value.
+ /// The result.
+ double NextDouble(double high);
+
+ ///
+ /// The next double between low (inclusive) and high (exclusive).
+ ///
+ /// The inclusive low value.
+ /// The exclusive high value.
+ /// The result.
+ double NextDouble(double low, double high);
+
+ ///
+ /// Generate the next random integer.
+ ///
+ /// The next random integer.
+ int NextInt();
+
+ ///
+ /// The next random int up to a non-inclusive range.
+ ///
+ /// The highest desired value.
+ /// The result.
+ int NextInt(int high);
+
+ ///
+ /// The next int between low (inclusive) and high (exclusive).
+ ///
+ /// The inclusive low value.
+ /// The exclusive high value.
+ /// The result.
+ int NextInt(int low, int high);
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/LinearCongruentialRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/LinearCongruentialRandom.cs
new file mode 100644
index 00000000..7185bc27
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/LinearCongruentialRandom.cs
@@ -0,0 +1,207 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// A Linear Congruential random number generator. A Linear Congruential Generator (LCG) yields a sequence of
+ /// randomized numbers calculated with a linear equation. The method represents one of the oldest and best-known
+ /// pseudorandom number generator algorithms. Most programming languages use this technique.
+ ///
+ /// http://en.wikipedia.org/wiki/Linear_congruential_generator
+ /// Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1
+ ///
+ public class LinearCongruentialRandom : AbstractBoxMuller
+ {
+ ///
+ /// First part of default mod.
+ ///
+ public const long DefaultMod1 = 2L;
+
+ ///
+ /// Second part of default mod.
+ ///
+ public const long DefaultMod2 = 32L;
+
+ ///
+ /// Default mult.
+ ///
+ public static long DefaultMult = 1103515245L;
+
+ ///
+ /// Default inc.
+ ///
+ public static long DefaultInc = 12345L;
+
+ ///
+ /// The modulus.
+ ///
+ private readonly long _modulus;
+
+ ///
+ /// The multiplier.
+ ///
+ private readonly long _multiplier;
+
+ ///
+ /// The amount to increment.
+ ///
+ private readonly long _increment;
+
+ ///
+ /// The current seed, set to an initial value and always holds the value of
+ /// the last random number generated.
+ ///
+ private long _seed;
+
+ ///
+ /// The maximum rand number that the standard GCC based LCG will generate.
+ ///
+ public const long MaxRand = 4294967295L;
+
+ ///
+ /// Construct the default LCG. You need only specify a seed.
+ ///
+ /// The seed to use.
+ public LinearCongruentialRandom(long theSeed)
+ : this((long)Math.Pow(DefaultMod1, DefaultMod2),
+ DefaultMult, DefaultInc, theSeed)
+ {
+ }
+
+ ///
+ /// Constructor to use a seed equal to system time.
+ ///
+ public LinearCongruentialRandom()
+ : this(Environment.TickCount)
+ {
+ }
+
+ ///
+ /// Create a LCG with the specified modulus, multiplier and increment. Unless
+ /// you REALLY KNOW WHAT YOU ARE DOING, just use the constructor that just
+ /// takes a seed. It will set these values to the same as set by the GCC C
+ /// compiler. Setting these values wrong can create fairly useless random
+ /// numbers.
+ ///
+ /// The modulus for the LCG algorithm.
+ /// The multiplier for the LCG algorithm.
+ /// The increment for the LCG algorithm.
+ /// The seed for the LCG algorithm. Using the same seed will give
+ /// the same random number sequence each time, whether in Java or DotNet.
+ public LinearCongruentialRandom(long theModulus,
+ long theMultiplier, long theIncrement,
+ long theSeed)
+ {
+ _modulus = theModulus;
+ _multiplier = theMultiplier;
+ _increment = theIncrement;
+ _seed = theSeed % MaxRand;
+ }
+
+ ///
+ /// The LCG increment.
+ ///
+ public long Increment
+ {
+ get
+ {
+ return _increment;
+ }
+ }
+
+ ///
+ /// The LCG modulus.
+ ///
+ public long Modulus
+ {
+ get
+ {
+ return _modulus;
+ }
+ }
+
+ ///
+ /// The LCG multiplier.
+ ///
+ public long Multiplier
+ {
+ get
+ {
+ return _multiplier;
+ }
+ }
+
+ ///
+ /// The current seed. Set to a constant to start, thereafter the
+ /// previously generated random number.
+ ///
+ public long Seed
+ {
+ get
+ {
+ return _seed;
+ }
+ }
+
+ ///
+ /// The next random number as a double between 0 and 1.
+ ///
+ /// The next random number as a double between 0 and 1.
+ public override double NextDouble()
+ {
+ return (double)NextLong() / MaxRand;
+ }
+
+ ///
+ /// The next random number as a long between 0 and MAX_RAND.
+ ///
+ /// The next random number as a long between 0 and MAX_RAND.
+ public override long NextLong()
+ {
+ _seed = (_multiplier * _seed + _increment)
+ % _modulus;
+ return _seed;
+ }
+
+ ///
+ public override bool NextBoolean()
+ {
+ return NextDouble() > 0.5;
+ }
+
+ ///
+ public override float NextFloat()
+ {
+ return (float)NextDouble();
+ }
+
+
+ ///
+ public override int NextInt()
+ {
+ return (int)NextLong();
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/MersenneTwisterGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/MersenneTwisterGenerateRandom.cs
new file mode 100644
index 00000000..fcdddfaa
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/MersenneTwisterGenerateRandom.cs
@@ -0,0 +1,182 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// The Mersenne twister is a pseudo random number generator developed in 1997 by Makoto Matsumoto and
+ /// Takuji Nishimura that is based on a matrix linear recurrence over a finite binary field F2.
+ ///
+ /// References:
+ ///
+ /// http://www.cs.gmu.edu/~sean/research/
+ /// http://en.wikipedia.org/wiki/Mersenne_twister
+ ///
+ /// Makato Matsumoto and Takuji Nishimura, "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
+ /// Pseudo-Random Number Generator", ACM Transactions on Modeling and. Computer Simulation,
+ /// Vol. 8, No. 1, January 1998, pp 3--30.
+ ///
+ public class MersenneTwisterGenerateRandom : AbstractBoxMuller
+ {
+ private const Int32 N = 624;
+ private const Int32 M = 397;
+ private const UInt32 MatrixA = 0x9908b0df;
+ private const UInt32 UpperMask = 0x80000000;
+ private const UInt32 LowerMask = 0x7fffffff;
+ private const UInt32 TemperingMaskB = 0x9d2c5680;
+ private const UInt32 TemperingMaskC = 0xefc60000;
+
+ private UInt32[] _stateVector;
+ private UInt16 _mti;
+ private UInt32[] _mag01;
+
+ public MersenneTwisterGenerateRandom()
+ : this((uint)Environment.TickCount)
+ {
+ }
+
+ public MersenneTwisterGenerateRandom(UInt32 seed)
+ {
+ SetSeed(seed);
+ }
+
+ public MersenneTwisterGenerateRandom(UInt32[] array)
+ {
+ SetSeed(array);
+ }
+
+ public void SetSeed(UInt32 seed)
+ {
+ _stateVector = new UInt32[N];
+
+ _mag01 = new UInt32[2];
+ _mag01[0] = 0x0;
+ _mag01[1] = MatrixA;
+
+ _stateVector[0] = seed;
+ for (_mti = 1; _mti < N; _mti++)
+ {
+ _stateVector[_mti] =
+ (1812433253 * (_stateVector[_mti - 1] ^ (_stateVector[_mti - 1] >> 30)) + _mti);
+ }
+ }
+
+ public void SetSeed(UInt32[] array)
+ {
+ Int32 i, j, k;
+ SetSeed(19650218);
+ i = 1;
+ j = 0;
+ k = (N > array.Length ? N : array.Length);
+ for (; k != 0; k--)
+ {
+
+ _stateVector[i] = (uint)((_stateVector[i] ^ ((_stateVector[i - 1] ^ (_stateVector[i - 1] >> 30)) * 1664525U)) + array[j] + j);
+ i++;
+ j++;
+ if (i >= N)
+ {
+ _stateVector[0] = _stateVector[N - 1];
+ i = 1;
+ }
+ if (j >= array.Length) j = 0;
+ }
+ for (k = N - 1; k != 0; k--)
+ {
+ _stateVector[i] = (uint)((_stateVector[i] ^ ((_stateVector[i - 1] ^ (_stateVector[i - 1] >> 30)) * 1566083941U)) - i);
+ i++;
+ if (i >= N)
+ {
+ _stateVector[0] = _stateVector[N - 1];
+ i = 1;
+ }
+ }
+ _stateVector[0] = 0x80000000;
+ }
+
+ protected UInt32 Next(int bits)
+ {
+ UInt32 y;
+
+ if (_mti >= N)
+ {
+ Int16 kk;
+
+ for (kk = 0; kk < N - M; kk++)
+ {
+ y = (_stateVector[kk] & UpperMask) | (_stateVector[kk + 1] & LowerMask);
+ _stateVector[kk] = _stateVector[kk + M] ^ (y >> 1) ^ _mag01[y & 0x1];
+ }
+ for (; kk < N - 1; kk++)
+ {
+ y = (_stateVector[kk] & UpperMask) | (_stateVector[kk + 1] & LowerMask);
+ _stateVector[kk] = _stateVector[kk + (M - N)] ^ (y >> 1) ^ _mag01[y & 0x1];
+ }
+ y = (_stateVector[N - 1] & UpperMask) | (_stateVector[0] & LowerMask);
+ _stateVector[N - 1] = _stateVector[M - 1] ^ (y >> 1) ^ _mag01[y & 0x1];
+
+ _mti = 0;
+ }
+
+ y = _stateVector[_mti++];
+ y ^= y >> 11;
+ y ^= (y << 7) & TemperingMaskB;
+ y ^= (y << 15) & TemperingMaskC;
+ y ^= (y >> 18);
+
+ return y >> (32 - bits);
+ }
+
+ public override double NextDouble()
+ {
+ return (((long)Next(26) << 27) + Next(27))
+ / (double)(1L << 53);
+ }
+
+ public override long NextLong()
+ {
+ uint u1 = Next(32);
+ uint u2 = Next(32);
+ return ((long)u1 << 32) + u2;
+ }
+
+ ///
+ public override bool NextBoolean()
+ {
+ return NextDouble() > 0.5;
+ }
+
+ ///
+ public override float NextFloat()
+ {
+ return (float)NextDouble();
+ }
+
+ ///
+ public override int NextInt()
+ {
+ return (int)NextLong();
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/MultiplyWithCarryGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/MultiplyWithCarryGenerateRandom.cs
new file mode 100644
index 00000000..7b7f69a2
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/MultiplyWithCarryGenerateRandom.cs
@@ -0,0 +1,168 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ ///
+ /// Multiply with Carry (MWC) is a pseudo random number generator computer science, multiply-with-carry (MWC)
+ /// is a method invented by George Marsaglia for generating sequences of random integers based on an initial set
+ /// from two to many thousands of randomly chosen seed values. The main advantages of the MWC method are that it
+ /// invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers
+ /// with immense periods.
+ ///
+ ///
+ /// This class was implemented using information from the following sources:
+ ///
+ /// http://www.javaprogrammingforums.com/blogs/helloworld922/11-complimentary-multiply-carry-better-way-generate-pseudo-random-numbers.html
+ /// http://en.wikipedia.org/wiki/Multiply-with-carry
+ ///
+ public class MultiplyWithCarryGenerateRandom : AbstractBoxMuller
+ {
+ private long _c;
+ private long _multiplier;
+ private int _n;
+ private int _r;
+ private readonly long[] _seed;
+
+ public MultiplyWithCarryGenerateRandom(long seed)
+ : this(new[] { seed }, seed / 2, 64, 987657110L)
+ {
+ }
+
+ public MultiplyWithCarryGenerateRandom()
+ : this(new long[] { Environment.TickCount }, Environment.TickCount * 64, 64, 987657110L)
+ {
+
+ }
+
+ public MultiplyWithCarryGenerateRandom(long[] seeds, long carry, int r, long multiplier)
+ {
+ SetR(r);
+ SetMultiplier(multiplier);
+ _seed = new long[r];
+ if (seeds == null || seeds.Length == 0)
+ {
+ seeds = new long[] { Environment.TickCount };
+ }
+
+ var rnd = new LinearCongruentialRandom(seeds[0]);
+ _c = (carry & 0xFFFFFFFFL) % multiplier;
+ for (int i = 0; i < r; ++i)
+ {
+ if (i < seeds.Length)
+ {
+ _seed[i] = seeds[i] & 0xFFFFFFFFL;
+ }
+ else
+ {
+ _seed[i] = rnd.NextInt() & 0xFFFFFFFFL;
+ }
+ if (_seed[i] == 0xFFFFFFFFL)
+ {
+ _seed[i] = 1L;
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public override double NextDouble()
+ {
+ return (((long)Next(26) << 27) + Next(27))
+ / (double)(1L << 53);
+ }
+
+ private int Next(int bits)
+ {
+ long t = _multiplier * _seed[_n] + _c;
+ long d32 = t >> 32;
+ _c = d32 + ((t & 0xFFFFFFFFL) >= 0xFFFFFFFFL - d32 ? 1L : 0L);
+ _seed[_n] = 0xFFFFFFFEL - (t & 0xFFFFFFFFL) - (_c - d32 << 32) - _c & 0xFFFFFFFFL;
+ long result = _seed[_n];
+ _n = _n + 1 & _r - 1;
+ return (int)(result >> 32 - bits);
+ }
+
+ private void SetMultiplier(long theMultiplier)
+ {
+ _multiplier = theMultiplier;
+ }
+
+ private void SetR(int theR)
+ {
+ if (theR <= 0)
+ {
+ theR = 256;
+ }
+ else
+ {
+ bool validR = true;
+ long a = theR;
+ while (a != 1 && validR)
+ {
+ if (a % 2 != 0)
+ {
+ theR = 256;
+ validR = false;
+ }
+ a >>= 1;
+ }
+ }
+ _r = theR;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public override long NextLong()
+ {
+ return ((long)Next(32) << 32) + Next(32);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public override bool NextBoolean()
+ {
+ return NextDouble() > 0.5;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public override float NextFloat()
+ {
+ return (float)NextDouble();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public override int NextInt()
+ {
+ return (int)NextLong();
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/Generate/SecureGenerateRandom.cs b/encog-core-cs/MathUtil/Randomize/Generate/SecureGenerateRandom.cs
new file mode 100644
index 00000000..761366bc
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/Generate/SecureGenerateRandom.cs
@@ -0,0 +1,81 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Security.Cryptography;
+
+namespace Encog.MathUtil.Randomize.Generate
+{
+ public class SecureGenerateRandom : AbstractBoxMuller
+ {
+ ///
+ /// The random number generator.
+ ///
+ private readonly RandomNumberGenerator _rnd;
+
+ ///
+ /// Construct a random number generator with a time-based seed.
+ ///
+ public SecureGenerateRandom()
+ {
+ _rnd = RandomNumberGenerator.Create();
+ }
+
+ ///
+ public override double NextDouble()
+ {
+ var result = new byte[8];
+ _rnd.GetBytes(result);
+ return (double)BitConverter.ToUInt64(result, 0) / ulong.MaxValue;
+ }
+
+ ///
+ public override bool NextBoolean()
+ {
+ return NextDouble() > 0.5;
+ }
+
+ ///
+ public override float NextFloat()
+ {
+ var result = new byte[4];
+ _rnd.GetBytes(result);
+ return (float)BitConverter.ToUInt32(result, 0) / ulong.MaxValue;
+ }
+
+ ///
+ public override long NextLong()
+ {
+ var result = new byte[8];
+ _rnd.GetBytes(result);
+ return (long)BitConverter.ToUInt64(result, 0);
+ }
+
+ ///
+ public override int NextInt()
+ {
+ var result = new byte[4];
+ _rnd.GetBytes(result);
+ return (int)BitConverter.ToUInt32(result, 0);
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/IRandomizer.cs b/encog-core-cs/MathUtil/Randomize/IRandomizer.cs
index dd0069b5..59e4aa90 100644
--- a/encog-core-cs/MathUtil/Randomize/IRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/IRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/NguyenWidrowRandomizer.cs b/encog-core-cs/MathUtil/Randomize/NguyenWidrowRandomizer.cs
index 3c386d91..15cff709 100644
--- a/encog-core-cs/MathUtil/Randomize/NguyenWidrowRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/NguyenWidrowRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/Randomize/RandomChoice.cs b/encog-core-cs/MathUtil/Randomize/RandomChoice.cs
new file mode 100644
index 00000000..705c322e
--- /dev/null
+++ b/encog-core-cs/MathUtil/Randomize/RandomChoice.cs
@@ -0,0 +1,173 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.Util;
+
+namespace Encog.MathUtil.Randomize
+{
+ ///
+ /// Generate random choices unevenly. This class is used to select random
+ /// choices from a list, with a probability weight places on each item
+ /// in the list.
+ ///
+ /// This is often called a Roulette Wheel in Machine Learning texts. How it differs from
+ /// a Roulette Wheel that you might find in Las Vegas or Monte Carlo is that the
+ /// areas that can be selected are not of uniform size. However, you can be sure
+ /// that one will be picked.
+ ///
+ /// http://en.wikipedia.org/wiki/Fitness_proportionate_selection
+ ///
+ [Serializable]
+ public class RandomChoice
+ {
+ ///
+ /// The probabilities of each item in the list.
+ ///
+ private double[] probabilities;
+
+ ///
+ /// Construct a list of probabilities.
+ ///
+ /// The probability of each item in the list.
+ public RandomChoice(double[] theProbabilities)
+ {
+ this.probabilities = EngineArray.ArrayCopy(theProbabilities);
+
+ double total = 0;
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ total += probabilities[i];
+ }
+
+ if (total == 0.0)
+ {
+ double prob = 1.0 / probabilities.Length;
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ probabilities[i] = prob;
+ }
+ }
+ else
+ {
+ double total2 = 0;
+ double factor = 1.0 / total;
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ probabilities[i] = probabilities[i] * factor;
+ total2 += probabilities[i];
+ }
+
+ if (Math.Abs(1.0 - total2) > 0.02)
+ {
+ double prob = 1.0 / probabilities.Length;
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ probabilities[i] = prob;
+ }
+ }
+ }
+ }
+
+ ///
+ /// Generate a random choice, based on the probabilities provided to the constructor.
+ ///
+ ///
+ /// The random choice.
+ public int Generate(Random theGenerator)
+ {
+ double r = theGenerator.NextDouble();
+ double sum = 0.0;
+
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ sum += probabilities[i];
+ if (r < sum)
+ {
+ return i;
+ }
+ }
+
+ for (int i = 0; i < probabilities.Length; i++)
+ {
+ if (probabilities[i] != 0.0)
+ {
+ return i;
+ }
+ }
+
+ throw new EncogError("Invalid probabilities.");
+ }
+
+ ///
+ /// Generate a random choice, but skip one of the choices.
+ ///
+ /// The choice to skip.
+ /// The random choice.
+ ///
+ public int Generate(Random theGenerator, int skip)
+ {
+ double totalProb = 1.0 - probabilities[skip];
+
+ double throwValue = theGenerator.NextDouble() * totalProb;
+ double accumulator = 0.0;
+
+ for (int i = 0; i < skip; i++)
+ {
+ accumulator += probabilities[i];
+ if (accumulator > throwValue)
+ {
+ return i;
+ }
+ }
+
+ for (int i = skip + 1; i < probabilities.Length; i++)
+ {
+ accumulator += probabilities[i];
+ if (accumulator > throwValue)
+ {
+ return i;
+ }
+ }
+
+ for (int i = 0; i < skip; i++)
+ {
+ if (probabilities[i] != 0.0)
+ {
+ return i;
+ }
+ }
+ for (int i = skip + 1; i < probabilities.Length; i++)
+ {
+ if (probabilities[i] != 0.0)
+ {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+ }
+}
diff --git a/encog-core-cs/MathUtil/Randomize/RangeRandomizer.cs b/encog-core-cs/MathUtil/Randomize/RangeRandomizer.cs
index c8728414..7c31eff6 100644
--- a/encog-core-cs/MathUtil/Randomize/RangeRandomizer.cs
+++ b/encog-core-cs/MathUtil/Randomize/RangeRandomizer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -91,6 +91,19 @@ public static double Randomize(double min, double max)
return (range*ThreadSafeRandom.NextDouble()) + min;
}
+ ///
+ /// Generate a random number in the specified range.
+ ///
+ /// Random number generator.
+ /// The minimum value.
+ /// The maximum value.
+ /// A random number.
+ public static double Randomize(EncogRandom rnd, double min, double max)
+ {
+ double range = max - min;
+ return (range * rnd.NextDouble()) + min;
+ }
+
///
/// Generate a random number based on the range specified in the constructor.
///
diff --git a/encog-core-cs/MathUtil/ThreadSafeRandom.cs b/encog-core-cs/MathUtil/ThreadSafeRandom.cs
index 3ebc3ebe..595a8cf8 100644
--- a/encog-core-cs/MathUtil/ThreadSafeRandom.cs
+++ b/encog-core-cs/MathUtil/ThreadSafeRandom.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/VectorAlgebra.cs b/encog-core-cs/MathUtil/VectorAlgebra.cs
index fb9201e5..d94e12db 100644
--- a/encog-core-cs/MathUtil/VectorAlgebra.cs
+++ b/encog-core-cs/MathUtil/VectorAlgebra.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/MathUtil/mathfunctions/mathstatistics.cs b/encog-core-cs/MathUtil/mathfunctions/mathstatistics.cs
index c0f4d9f6..7e82f087 100644
--- a/encog-core-cs/MathUtil/mathfunctions/mathstatistics.cs
+++ b/encog-core-cs/MathUtil/mathfunctions/mathstatistics.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/ART/ART1.cs b/encog-core-cs/Neural/ART/ART1.cs
index 26a212bb..83a86166 100644
--- a/encog-core-cs/Neural/ART/ART1.cs
+++ b/encog-core-cs/Neural/ART/ART1.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/ART/BasicART.cs b/encog-core-cs/Neural/ART/BasicART.cs
index e00aae45..a3f9aa8c 100644
--- a/encog-core-cs/Neural/ART/BasicART.cs
+++ b/encog-core-cs/Neural/ART/BasicART.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/ART/PersistART1.cs b/encog-core-cs/Neural/ART/PersistART1.cs
index f030ec7f..448b013a 100644
--- a/encog-core-cs/Neural/ART/PersistART1.cs
+++ b/encog-core-cs/Neural/ART/PersistART1.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/BAM/BAMNetwork.cs b/encog-core-cs/Neural/BAM/BAMNetwork.cs
index 55172032..c93efbeb 100644
--- a/encog-core-cs/Neural/BAM/BAMNetwork.cs
+++ b/encog-core-cs/Neural/BAM/BAMNetwork.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -223,7 +223,7 @@ private static double GetWeight(Matrix matrix, IMLData input,
/// The output pattern.
/// True if the network has become stable.
private static bool PropagateLayer(Matrix matrix, IMLData input,
- IMLData output)
+ IMLDataModifiable output)
{
int i;
diff --git a/encog-core-cs/Neural/BAM/PersistBAM.cs b/encog-core-cs/Neural/BAM/PersistBAM.cs
index ea842384..ccb942b4 100644
--- a/encog-core-cs/Neural/BAM/PersistBAM.cs
+++ b/encog-core-cs/Neural/BAM/PersistBAM.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/CPN/CPN.cs b/encog-core-cs/Neural/CPN/CPN.cs
index 9355797d..59becca6 100644
--- a/encog-core-cs/Neural/CPN/CPN.cs
+++ b/encog-core-cs/Neural/CPN/CPN.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -208,7 +208,7 @@ public void Reset(int seed)
/// The output.
public IMLData ComputeInstar(IMLData input)
{
- IMLData result = new BasicMLData(_instarCount);
+ var result = new BasicMLData(_instarCount);
int w, i;
int winner = 0;
var winners = new bool[_instarCount];
@@ -244,11 +244,11 @@ public IMLData ComputeInstar(IMLData input)
if (winners[i]
&& (Math.Abs(sumWinners) > EncogFramework.DefaultDoubleEqual))
{
- result.Data[i] /= sumWinners;
+ result[i] /= sumWinners;
}
else
{
- result.Data[i] = 0;
+ result[i] = 0;
}
}
@@ -263,7 +263,7 @@ public IMLData ComputeInstar(IMLData input)
/// The output.
public IMLData ComputeOutstar(IMLData input)
{
- IMLData result = new BasicMLData(_outstarCount);
+ var result = new BasicMLData(_outstarCount);
for (int i = 0; i < _outstarCount; i++)
{
diff --git a/encog-core-cs/Neural/CPN/PersistCPN.cs b/encog-core-cs/Neural/CPN/PersistCPN.cs
index c6526e40..a9b9a434 100644
--- a/encog-core-cs/Neural/CPN/PersistCPN.cs
+++ b/encog-core-cs/Neural/CPN/PersistCPN.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/CPN/Training/TrainInstar.cs b/encog-core-cs/Neural/CPN/Training/TrainInstar.cs
index 7d378769..a41ee8cd 100644
--- a/encog-core-cs/Neural/CPN/Training/TrainInstar.cs
+++ b/encog-core-cs/Neural/CPN/Training/TrainInstar.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -150,7 +150,7 @@ public override sealed void Iteration()
IMLData xout = _network.ComputeInstar(pair.Input);
// determine winner
- int winner = EngineArray.IndexOfLargest(xout.Data);
+ int winner = EngineArray.IndexOfLargest(xout);
// calculate the distance
double distance = 0;
diff --git a/encog-core-cs/Neural/CPN/Training/TrainOutstar.cs b/encog-core-cs/Neural/CPN/Training/TrainOutstar.cs
index a0ea4f64..d19bfbd8 100644
--- a/encog-core-cs/Neural/CPN/Training/TrainOutstar.cs
+++ b/encog-core-cs/Neural/CPN/Training/TrainOutstar.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -138,7 +138,7 @@ public override sealed void Iteration()
{
IMLData xout = _network.ComputeInstar(pair.Input);
- int j = EngineArray.IndexOfLargest(xout.Data);
+ int j = EngineArray.IndexOfLargest(xout);
for (int i = 0; i < _network.OutstarCount; i++)
{
double delta = _learningRate
@@ -147,7 +147,7 @@ public override sealed void Iteration()
}
IMLData out2 = _network.ComputeOutstar(xout);
- error.UpdateError(out2.Data, pair.Ideal.Data, pair.Significance);
+ error.UpdateError(out2, pair.Ideal, pair.Significance);
}
Error = error.Calculate();
diff --git a/encog-core-cs/Neural/Data/Basic/BasicNeuralData.cs b/encog-core-cs/Neural/Data/Basic/BasicNeuralData.cs
index d660f486..659497dc 100644
--- a/encog-core-cs/Neural/Data/Basic/BasicNeuralData.cs
+++ b/encog-core-cs/Neural/Data/Basic/BasicNeuralData.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/Data/Basic/BasicNeuralDataPair.cs b/encog-core-cs/Neural/Data/Basic/BasicNeuralDataPair.cs
index dd1d4244..1e65657f 100644
--- a/encog-core-cs/Neural/Data/Basic/BasicNeuralDataPair.cs
+++ b/encog-core-cs/Neural/Data/Basic/BasicNeuralDataPair.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/Data/Basic/BasicNeuralDataSet.cs b/encog-core-cs/Neural/Data/Basic/BasicNeuralDataSet.cs
index 01bf5819..a1e10be1 100644
--- a/encog-core-cs/Neural/Data/Basic/BasicNeuralDataSet.cs
+++ b/encog-core-cs/Neural/Data/Basic/BasicNeuralDataSet.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/Error/ATanErrorFunction.cs b/encog-core-cs/Neural/Error/ATanErrorFunction.cs
index 27bdf921..1b9b679d 100644
--- a/encog-core-cs/Neural/Error/ATanErrorFunction.cs
+++ b/encog-core-cs/Neural/Error/ATanErrorFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
// http://www.heatonresearch.com/copyright
//
using System;
+using Encog.ML.Data;
+using Encog.Engine.Network.Activation;
namespace Encog.Neural.Error
{
@@ -31,18 +33,16 @@ namespace Encog.Neural.Error
///
public class ATanErrorFunction : IErrorFunction
{
- #region IErrorFunction Members
-
- ///
- public void CalculateError(double[] ideal, double[] actual,
- double[] error)
+ public void CalculateError(IActivationFunction af, double[] b, double[] a,
+ IMLData ideal, double[] actual, double[] error, double derivShift,
+ double significance)
{
+
for (int i = 0; i < actual.Length; i++)
{
- error[i] = Math.Atan(ideal[i] - actual[i]);
+ double deriv = af.DerivativeFunction(b[i], a[i]);
+ error[i] = (Math.Atan(ideal[i] - actual[i]) * significance) * deriv;
}
}
-
- #endregion
}
}
diff --git a/encog-core-cs/Neural/Error/CrossEntropyErrorFunction.cs b/encog-core-cs/Neural/Error/CrossEntropyErrorFunction.cs
new file mode 100644
index 00000000..700d0f6c
--- /dev/null
+++ b/encog-core-cs/Neural/Error/CrossEntropyErrorFunction.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Encog.ML.Data;
+using Encog.Engine.Network.Activation;
+
+namespace Encog.Neural.Error
+{
+ ///
+ /// Implements a cross entropy error function. This can be used with backpropagation to
+ /// sometimes provide better performance than the standard linear error function.
+ ///
+ public class CrossEntropyErrorFunction : IErrorFunction
+ {
+ public void CalculateError(IActivationFunction af, double[] b, double[] a,
+ IMLData ideal, double[] actual, double[] error, double derivShift,
+ double significance)
+ {
+
+ for (int i = 0; i < actual.Length; i++)
+ {
+ error[i] = (ideal[i] - actual[i]) * significance;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Error/IErrorFunction.cs b/encog-core-cs/Neural/Error/IErrorFunction.cs
index 36c1dec3..cddfc336 100644
--- a/encog-core-cs/Neural/Error/IErrorFunction.cs
+++ b/encog-core-cs/Neural/Error/IErrorFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
// and trademarks visit:
// http://www.heatonresearch.com/copyright
//
+using Encog.Engine.Network.Activation;
+using Encog.ML.Data;
namespace Encog.Neural.Error
{
///
@@ -31,9 +33,16 @@ public interface IErrorFunction
///
/// Calculate the error.
///
+ /// The activation function used at the output layer.
+ /// The number to calculate the derivative of, the number "before" the activation function was applied.
+ /// The number "after" an activation function has been applied.
/// The ideal values.
/// The actual values.
- /// The rror output.
- void CalculateError(double[] ideal, double[] actual, double[] error);
+ /// The resulting error values.
+ /// The amount to shift af derivativeFunction by
+ /// Weighting to apply to ideal[i] - actual[i]
+ void CalculateError(IActivationFunction af, double[] b, double[] a,
+ IMLData ideal, double[] actual, double[] error, double derivShift,
+ double significance);
}
}
diff --git a/encog-core-cs/Neural/Error/LinearErrorFunction.cs b/encog-core-cs/Neural/Error/LinearErrorFunction.cs
index 0f7c68c2..0473c2a3 100644
--- a/encog-core-cs/Neural/Error/LinearErrorFunction.cs
+++ b/encog-core-cs/Neural/Error/LinearErrorFunction.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using Encog.ML.Data;
+using Encog.Engine.Network.Activation;
namespace Encog.Neural.Error
{
@@ -34,13 +36,16 @@ namespace Encog.Neural.Error
public class LinearErrorFunction : IErrorFunction
{
///
- public void CalculateError(double[] ideal, double[] actual, double[] error)
+ public void CalculateError(IActivationFunction af, double[] b, double[] a,
+ IMLData ideal, double[] actual, double[] error, double derivShift,
+ double significance)
{
+
for (int i = 0; i < actual.Length; i++)
{
- error[i] = ideal[i] - actual[i];
+ double deriv = af.DerivativeFunction(b[i], a[i]);// + derivShift;
+ error[i] = ((ideal[i] - actual[i]) * significance) * deriv;
}
-
}
}
}
diff --git a/encog-core-cs/Neural/Error/OutputErrorFunction.cs b/encog-core-cs/Neural/Error/OutputErrorFunction.cs
new file mode 100644
index 00000000..b454cf12
--- /dev/null
+++ b/encog-core-cs/Neural/Error/OutputErrorFunction.cs
@@ -0,0 +1,24 @@
+using Encog.Engine.Network.Activation;
+using Encog.ML.Data;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Encog.Neural.Error
+{
+ public class OutputErrorFunction: IErrorFunction
+ {
+ public void CalculateError(IActivationFunction af, double[] b, double[] a,
+ IMLData ideal, double[] actual, double[] error, double derivShift,
+ double significance)
+ {
+
+ for (int i = 0; i < actual.Length; i++)
+ {
+ double deriv = af.DerivativeFunction(b[i], a[i]) + derivShift;
+ error[i] = ((ideal[i] - actual[i]) * significance) * deriv;
+ }
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Flat/FlatLayer.cs b/encog-core-cs/Neural/Flat/FlatLayer.cs
index 3b525bf5..c250a19c 100644
--- a/encog-core-cs/Neural/Flat/FlatLayer.cs
+++ b/encog-core-cs/Neural/Flat/FlatLayer.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/encog-core-cs/Neural/Flat/FlatNetwork.cs b/encog-core-cs/Neural/Flat/FlatNetwork.cs
index 0f19feae..66219466 100644
--- a/encog-core-cs/Neural/Flat/FlatNetwork.cs
+++ b/encog-core-cs/Neural/Flat/FlatNetwork.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -463,14 +463,13 @@ public double CalculateError(IMLDataSet data)
var errorCalculation = new ErrorCalculation();
var actual = new double[_outputCount];
- IMLDataPair pair = BasicMLDataPair.CreatePair(data.InputSize,
- data.IdealSize);
+ IMLDataPair pair;
for (int i = 0; i < data.Count; i++)
{
- data.GetRecord(i, pair);
- Compute(pair.InputArray, actual);
- errorCalculation.UpdateError(actual, pair.IdealArray,pair.Significance);
+ pair = data[i];
+ Compute(pair.Input, actual);
+ errorCalculation.UpdateError(actual, pair.Ideal, pair.Significance);
}
return errorCalculation.Calculate();
}
@@ -563,6 +562,16 @@ public void CloneFlatNetwork(FlatNetwork result)
result._endTraining = _endTraining;
}
+ public virtual void Compute(IMLData input, double[] output)
+ {
+ int sourceIndex = _layerOutput.Length
+ - _layerCounts[_layerCounts.Length - 1];
+
+ input.CopyTo(_layerOutput, sourceIndex, _inputCount);
+
+ InnerCompute(output);
+ }
+
///
/// Calculate the output for the given input.
///
@@ -577,23 +586,28 @@ public virtual void Compute(double[] input, double[] output)
EngineArray.ArrayCopy(input, 0, _layerOutput, sourceIndex,
_inputCount);
- for (int i = _layerIndex.Length - 1; i > 0; i--)
- {
- ComputeLayer(i);
- }
+ InnerCompute(output);
+ }
- // update context values
- int offset = _contextTargetOffset[0];
+ private void InnerCompute(double[] output)
+ {
+ for(int i = _layerIndex.Length - 1; i > 0; i--)
+ {
+ ComputeLayer(i);
+ }
- for (int x = 0; x < _contextTargetSize[0]; x++)
- {
- _layerOutput[offset + x] = _layerOutput[x];
- }
+ // update context values
+ int offset = _contextTargetOffset[0];
- EngineArray.ArrayCopy(_layerOutput, 0, output, 0, _outputCount);
- }
+ for(int x = 0; x < _contextTargetSize[0]; x++)
+ {
+ _layerOutput[offset + x] = _layerOutput[x];
+ }
- ///
+ EngineArray.ArrayCopy(_layerOutput, 0, output, 0, _outputCount);
+ }
+
+ ///
/// Calculate a layer.
///
///
@@ -782,16 +796,6 @@ public void Init(FlatLayer[] layers)
}
- ///
- /// Perform a simple randomization of the weights of the neural network
- /// between -1 and 1.
- ///
- ///
- public void Randomize()
- {
- Randomize(1, -1);
- }
-
///
/// Perform a simple randomization of the weights of the neural network
/// between the specified hi and lo.
@@ -799,11 +803,13 @@ public void Randomize()
///
/// The network high.
/// The network low.
- public void Randomize(double hi, double lo)
+ /// Pass in an integer here to seed the random number generator or leave null for default behavior (seed from system clock).
+ public void Randomize(double hi = 1.0, double lo = -1.0, int? seed = null)
{
+ var random = seed == null ? new Random() : new Random(seed.Value);
for (int i = 0; i < _weights.Length; i++)
{
- _weights[i] = (ThreadSafeRandom.NextDouble()*(hi - lo)) + lo;
+ _weights[i] = (random.NextDouble()*(hi - lo)) + lo;
}
}
@@ -815,5 +821,78 @@ public double[] LayerSums
get { return _layerSums; }
set { _layerSums = value; }
}
+
+ ///
+ /// Get the weight between the two layers.
+ ///
+ /// The from layer.
+ /// The from neuron.
+ /// The to neuron.
+ /// The weight value.
+ public double GetWeight(int fromLayer,
+ int fromNeuron,
+ int toNeuron)
+ {
+ ValidateNeuron(fromLayer, fromNeuron);
+ ValidateNeuron(fromLayer + 1, toNeuron);
+ int fromLayerNumber = _layerContextCount.Length - fromLayer - 1;
+ int toLayerNumber = fromLayerNumber - 1;
+
+ if (toLayerNumber < 0)
+ {
+ throw new NeuralNetworkError(
+ "The specified layer is not connected to another layer: "
+ + fromLayer);
+ }
+
+ int weightBaseIndex
+ = _weightIndex[toLayerNumber];
+ int count
+ = _layerCounts[fromLayerNumber];
+ int weightIndex = weightBaseIndex + fromNeuron
+ + (toNeuron * count);
+
+ return _weights[weightIndex];
+ }
+
+ ///
+ /// Validate the the specified targetLayer and neuron are valid.
+ ///
+ /// The target layer.
+ /// The target neuron.
+ public void ValidateNeuron(int targetLayer, int neuron)
+ {
+ if ((targetLayer < 0) || (targetLayer >= _layerCounts.Length))
+ {
+ throw new NeuralNetworkError("Invalid layer count: " + targetLayer);
+ }
+
+ if ((neuron < 0) || (neuron >= GetLayerTotalNeuronCount(targetLayer)))
+ {
+ throw new NeuralNetworkError("Invalid neuron number: " + neuron);
+ }
+ }
+
+ ///
+ /// Get the total (including bias and context) neuron cont for a layer.
+ ///
+ /// The layer.
+ /// The count.
+ public int GetLayerTotalNeuronCount(int l)
+ {
+ int layerNumber = _layerCounts.Length - l - 1;
+ return _layerCounts[layerNumber];
+ }
+
+ ///
+ /// Get the neuron count.
+ ///
+ /// The layer.
+ /// The neuron count.
+ public int GetLayerNeuronCount(int l)
+ {
+ int layerNumber = _layerCounts.Length - l - 1;
+ return _layerFeedCounts[layerNumber];
+ }
}
}
diff --git a/encog-core-cs/Neural/Flat/FlatNetworkRBF.cs b/encog-core-cs/Neural/Flat/FlatNetworkRBF.cs
index 21260458..f8261628 100644
--- a/encog-core-cs/Neural/Flat/FlatNetworkRBF.cs
+++ b/encog-core-cs/Neural/Flat/FlatNetworkRBF.cs
@@ -1,8 +1,8 @@
//
-// Encog(tm) Core v3.1 - .Net Version
+// Encog(tm) Core v3.3 - .Net Version
// http://www.heatonresearch.com/encog/
//
-// Copyright 2008-2012 Heaton Research, Inc.
+// Copyright 2008-2014 Heaton Research, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -112,5 +112,18 @@ public override sealed void Compute(double[] x, double[] output)
EngineArray.ArrayCopy(LayerOutput, 0, output, 0,
OutputCount);
}
+
+ public override void Compute(ML.Data.IMLData input, double[] output)
+ {
+ if(input is ML.Data.Basic.BasicMLData)
+ Compute(((ML.Data.Basic.BasicMLData)input).Data, output);
+ else
+ {
+ // TODO: make this more efficient
+ var tmp = new double[input.Count];
+ input.CopyTo(tmp, 0, input.Count);
+ Compute(tmp, output);
+ }
+ }
}
}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummation.cs b/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummation.cs
new file mode 100644
index 00000000..5a079149
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummation.cs
@@ -0,0 +1,107 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.Engine.Network.Activation;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// Provides a basic implementation of an input summation. The inputs are summed
+ /// and applied to the activation function.
+ ///
+ [Serializable]
+ public class BasicActivationSummation : IInputSummation
+ {
+ ///
+ /// The inputs.
+ ///
+ private readonly IList _inputs = new List();
+
+ ///
+ /// The pre-activation summation.
+ ///
+ private double _sum;
+
+ ///
+ /// Construct the activation summation.
+ ///
+ /// The activation function.
+ public BasicActivationSummation(
+ IActivationFunction theActivationFunction)
+ {
+ ActivationFunction = theActivationFunction;
+ }
+
+ ///
+ public void Add(IFreeformConnection connection)
+ {
+ _inputs.Add(connection);
+ }
+
+ ///
+ public double Calculate()
+ {
+ var sumArray = new double[1];
+ _sum = 0;
+
+ // sum the input connections
+ foreach (IFreeformConnection connection in _inputs)
+ {
+ connection.Source.PerformCalculation();
+ _sum += connection.Weight
+ * connection.Source.Activation;
+ }
+
+ // perform the activation function
+ sumArray[0] = _sum;
+ ActivationFunction
+ .ActivationFunction(sumArray, 0, sumArray.Count());
+
+ return sumArray[0];
+ }
+
+ ///
+ public IActivationFunction ActivationFunction { get; set; }
+
+ ///
+ public double Sum
+ {
+ get
+ {
+ return _sum;
+ }
+ }
+
+ ///
+ public IList List
+ {
+ get
+ {
+ return _inputs;
+ }
+ }
+
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummationFactory.cs b/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummationFactory.cs
new file mode 100644
index 00000000..b5f39b4b
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicActivationSummationFactory.cs
@@ -0,0 +1,40 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.Neural.Freeform.Factory;
+using Encog.Engine.Network.Activation;
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// A factory to create BasicFactivationSUmmation objects.
+ ///
+ [Serializable]
+ public class BasicActivationSummationFactory : IInputSummationFactory
+ {
+ ///
+ public IInputSummation Factor(IActivationFunction theActivationFunction)
+ {
+ return new BasicActivationSummation(theActivationFunction);
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnection.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnection.cs
new file mode 100644
index 00000000..c34fc304
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnection.cs
@@ -0,0 +1,111 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// A basic freeform connection.
+ ///
+ [Serializable]
+ public class BasicFreeformConnection: IFreeformConnection
+ {
+ ///
+ /// The connection weight.
+ ///
+ public double Weight { get; set; }
+
+ ///
+ /// The source neuron.
+ ///
+ public IFreeformNeuron Source { get; set; }
+
+ ///
+ /// The target neuron.
+ ///
+ public IFreeformNeuron Target { get; set; }
+
+ ///
+ /// Temp training data.
+ ///
+ private double[] _tempTraining;
+
+ ///
+ /// Construct a basic freeform connection.
+ ///
+ /// The source neuron.
+ /// The target neuron.
+ public BasicFreeformConnection(IFreeformNeuron theSource,
+ IFreeformNeuron theTarget) {
+ Weight = 0.0;
+ Source = theSource;
+ Target = theTarget;
+ }
+
+ ///
+ public void AddTempTraining(int i, double value) {
+ _tempTraining[i] += value;
+
+ }
+
+ ///
+ public void AllocateTempTraining(int l) {
+ _tempTraining = new double[l];
+
+ }
+
+ ///
+ public void ClearTempTraining() {
+ _tempTraining = null;
+
+ }
+
+
+ ///
+ public double GetTempTraining(int index) {
+ return _tempTraining[index];
+ }
+
+ ///
+ public void SetTempTraining(int index, double value) {
+ _tempTraining[index] = value;
+
+ }
+
+
+ ///
+ public new String ToString() {
+ var result = new StringBuilder();
+ result.Append("[BasicFreeformConnection: ");
+ result.Append("source=");
+ result.Append(Source);
+ result.Append(",target=");
+ result.Append(Target);
+ result.Append(",weight=");
+ result.Append(Weight);
+ result.Append("]");
+ return result.ToString();
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnectionFactory.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnectionFactory.cs
new file mode 100644
index 00000000..839de52e
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformConnectionFactory.cs
@@ -0,0 +1,41 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.Neural.Freeform.Factory;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// A factory that creates basic freeform connection objects.
+ ///
+ [Serializable]
+ public class BasicFreeformConnectionFactory : IFreeformConnectionFactory
+ {
+ ///
+ public IFreeformConnection Factor(IFreeformNeuron theSourceNeuron,
+ IFreeformNeuron theTargetNeuron)
+ {
+ return new BasicFreeformConnection(theSourceNeuron, theTargetNeuron);
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayer.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayer.cs
new file mode 100644
index 00000000..92f3e476
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayer.cs
@@ -0,0 +1,87 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// Implements a basic freeform layer.
+ ///
+ [Serializable]
+ public class BasicFreeformLayer : IFreeformLayer
+ {
+ ///
+ /// The neurons in this layer.
+ ///
+ private readonly IList _neurons = new List();
+
+ ///
+ public void Add(IFreeformNeuron neuron)
+ {
+ _neurons.Add(neuron);
+ }
+
+ ///
+ public IList Neurons
+ {
+ get
+ {
+ return _neurons;
+ }
+ }
+
+ ///
+ public void SetActivation(int i, double activation)
+ {
+ _neurons[i].Activation = activation;
+ }
+
+ ///
+ public int Count
+ {
+ get
+ {
+ return _neurons.Count;
+ }
+ }
+
+ ///
+ public int CountNonBias
+ {
+ get
+ {
+ return _neurons.Count(neuron => !neuron.IsBias);
+ }
+ }
+
+
+ ///
+ public bool HasBias
+ {
+ get
+ { return Neurons.Any(n => n.IsBias); }
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayerFactory.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayerFactory.cs
new file mode 100644
index 00000000..28ec9d67
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformLayerFactory.cs
@@ -0,0 +1,40 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.Neural.Freeform.Factory;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// A factory that creates BasicFreeformLayer objects.
+ ///
+ [Serializable]
+ public class BasicFreeformLayerFactory : IFreeformLayerFactory
+ {
+ ///
+ public IFreeformLayer Factor()
+ {
+ return new BasicFreeformLayer();
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuron.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuron.cs
new file mode 100644
index 00000000..7000008e
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuron.cs
@@ -0,0 +1,166 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// This class provides a basic implementation of a freeform neuron.
+ ///
+ [Serializable]
+ public class BasicFreeformNeuron : IFreeformNeuron
+ {
+ ///
+ /// The input summation.
+ ///
+ public IInputSummation InputSummation { get; set; }
+
+ ///
+ /// The output connections.
+ ///
+ private readonly IList _outputConnections = new List();
+
+ ///
+ /// The activation.
+ ///
+ public double Activation { get; set; }
+
+ ///
+ /// True if this neuron is a bias neuron.
+ ///
+ public bool IsBias { get; set; }
+
+ ///
+ /// Temp training values.
+ ///
+ private double[] _tempTraining;
+
+ ///
+ /// Construct a basic freeform network.
+ ///
+ /// The input summation to use.
+ public BasicFreeformNeuron(IInputSummation theInputSummation)
+ {
+ InputSummation = theInputSummation;
+ }
+
+ ///
+ public void AddInput(IFreeformConnection connection)
+ {
+ InputSummation.Add(connection);
+
+ }
+
+ ///
+ public void AddOutput(IFreeformConnection connection)
+ {
+ _outputConnections.Add(connection);
+ }
+
+ ///
+ public void AddTempTraining(int i, double value)
+ {
+ _tempTraining[i] += value;
+ }
+
+ ///
+ public void AllocateTempTraining(int l)
+ {
+ _tempTraining = new double[l];
+
+ }
+
+ ///
+ public void ClearTempTraining()
+ {
+ _tempTraining = null;
+
+ }
+
+ ///
+ public double Sum
+ {
+ get
+ {
+ return InputSummation == null ? Activation : InputSummation.Sum;
+ }
+ }
+
+ ///
+ public double GetTempTraining(int index)
+ {
+ return _tempTraining[index];
+ }
+
+ ///
+ public void PerformCalculation()
+ {
+ // no inputs? Just keep activation as is, probably a bias neuron.
+ if (InputSummation == null)
+ {
+ return;
+ }
+
+ Activation = InputSummation.Calculate();
+ }
+
+ ///
+ public void SetTempTraining(int index, double value)
+ {
+ _tempTraining[index] = value;
+
+ }
+
+ ///
+ public virtual void UpdateContext()
+ {
+ // nothing to do for a non-context neuron
+
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[BasicFreeformNeuron: ");
+ result.Append("inputCount=");
+ if (InputSummation == null)
+ {
+ result.Append("null");
+ }
+ else
+ {
+ result.Append(InputSummation.List.Count);
+ }
+ result.Append(",outputCount=");
+ result.Append(_outputConnections.Count);
+ result.Append("]");
+ return result.ToString();
+ }
+
+ ///
+ public IList Outputs { get { return _outputConnections; } }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuronFactory.cs b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuronFactory.cs
new file mode 100644
index 00000000..8a9d783a
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Basic/BasicFreeformNeuronFactory.cs
@@ -0,0 +1,47 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using Encog.Neural.Freeform.Factory;
+
+namespace Encog.Neural.Freeform.Basic
+{
+ ///
+ /// A factory to create BasicFreeformNeuron objects.
+ ///
+ [Serializable]
+ public class BasicFreeformNeuronFactory : IFreeformNeuronFactory
+ {
+ ///
+ public IFreeformNeuron FactorContext(IFreeformNeuron neuron)
+ {
+ IFreeformNeuron result = new FreeformContextNeuron(neuron);
+ return result;
+ }
+
+ ///
+ public IFreeformNeuron FactorRegular(IInputSummation o)
+ {
+ return new BasicFreeformNeuron(o);
+ }
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Factory/IFreeformConnectionFactory.cs b/encog-core-cs/Neural/Freeform/Factory/IFreeformConnectionFactory.cs
new file mode 100644
index 00000000..9afeb2f6
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Factory/IFreeformConnectionFactory.cs
@@ -0,0 +1,39 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.Neural.Freeform.Factory
+{
+ ///
+ /// A factory that creates connections.
+ ///
+ public interface IFreeformConnectionFactory
+ {
+ ///
+ /// Create a connection.
+ ///
+ /// The source neuron.
+ /// The target neuron.
+ /// The newly created connection.
+ IFreeformConnection Factor(IFreeformNeuron sourceNeuron,
+ IFreeformNeuron targetNeuron);
+ }
+}
diff --git a/encog-core-cs/Util/Concurrency/IEngineTask.cs b/encog-core-cs/Neural/Freeform/Factory/IFreeformLayerFactory.cs
similarity index 72%
rename from encog-core-cs/Util/Concurrency/IEngineTask.cs
rename to encog-core-cs/Neural/Freeform/Factory/IFreeformLayerFactory.cs
index 04351a15..a0ca8e05 100644
--- a/encog-core-cs/Util/Concurrency/IEngineTask.cs
+++ b/encog-core-cs/Neural/Freeform/Factory/IFreeformLayerFactory.cs
@@ -1,35 +1,36 @@
-//
-// Encog(tm) Core v3.1 - .Net Version
-// http://www.heatonresearch.com/encog/
-//
-// Copyright 2008-2012 Heaton Research, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// For more information on Heaton Research copyrights, licenses
-// and trademarks visit:
-// http://www.heatonresearch.com/copyright
-//
-namespace Encog.Util.Concurrency
-{
- ///
- /// A task for Encog concurrency.
- ///
- public interface IEngineTask
- {
- ///
- /// Run the specified task.
- ///
- void Run();
- }
-}
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.Neural.Freeform.Factory
+{
+ ///
+ /// A factory that creates layers.
+ ///
+ public interface IFreeformLayerFactory
+ {
+ ///
+ /// Create a layer.
+ ///
+ /// The newly created layer.
+ IFreeformLayer Factor();
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Factory/IFreeformNeuronFactory.cs b/encog-core-cs/Neural/Freeform/Factory/IFreeformNeuronFactory.cs
new file mode 100644
index 00000000..be1caf3f
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Factory/IFreeformNeuronFactory.cs
@@ -0,0 +1,44 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+namespace Encog.Neural.Freeform.Factory
+{
+ ///
+ /// A factory that creates neurons.
+ ///
+ interface IFreeformNeuronFactory
+ {
+ ///
+ /// Create a context neuron.
+ ///
+ /// The source neuron.
+ /// The newly created neuron.
+ IFreeformNeuron FactorContext(IFreeformNeuron neuron);
+
+ ///
+ /// Create a regular neuron.
+ ///
+ /// The summation function.
+ /// The newly created neuron.
+ IFreeformNeuron FactorRegular(IInputSummation obj);
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/Factory/IInputSummationFactory.cs b/encog-core-cs/Neural/Freeform/Factory/IInputSummationFactory.cs
new file mode 100644
index 00000000..95c55bf5
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/Factory/IInputSummationFactory.cs
@@ -0,0 +1,39 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using Encog.Engine.Network.Activation;
+
+namespace Encog.Neural.Freeform.Factory
+{
+ ///
+ /// A factory that creates input summations.
+ ///
+ public interface IInputSummationFactory
+ {
+ ///
+ /// Create a new input summation.
+ ///
+ /// The activation function to use.
+ /// The input summation.
+ IInputSummation Factor(IActivationFunction activationFunction);
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/FreeformContextNeuron.cs b/encog-core-cs/Neural/Freeform/FreeformContextNeuron.cs
new file mode 100644
index 00000000..25553b73
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/FreeformContextNeuron.cs
@@ -0,0 +1,68 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Text;
+using Encog.Neural.Freeform.Basic;
+
+namespace Encog.Neural.Freeform
+{
+ ///
+ /// Defines a freeform context neuron.
+ ///
+ [Serializable]
+ public class FreeformContextNeuron : BasicFreeformNeuron
+ {
+ ///
+ /// The context source.
+ ///
+ public IFreeformNeuron ContextSource { get; set; }
+
+ ///
+ /// Construct the context neuron.
+ ///
+ /// The context source.
+ public FreeformContextNeuron(IFreeformNeuron theContextSource)
+ : base(null)
+ {
+ ContextSource = theContextSource;
+ }
+
+ ///
+ public override void UpdateContext()
+ {
+ Activation = ContextSource.Activation;
+ }
+
+ ///
+ public override String ToString()
+ {
+ var result = new StringBuilder();
+ result.Append("[FreeformContextNeuron: ");
+ result.Append("outputCount=");
+ result.Append(Outputs.Count);
+ result.Append("]");
+ return result.ToString();
+ }
+
+ }
+}
diff --git a/encog-core-cs/Neural/Freeform/FreeformNetwork.cs b/encog-core-cs/Neural/Freeform/FreeformNetwork.cs
new file mode 100644
index 00000000..c1d1d17f
--- /dev/null
+++ b/encog-core-cs/Neural/Freeform/FreeformNetwork.cs
@@ -0,0 +1,794 @@
+//
+// Encog(tm) Core v3.3 - .Net Version
+// http://www.heatonresearch.com/encog/
+//
+// Copyright 2008-2014 Heaton Research, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// For more information on Heaton Research copyrights, licenses
+// and trademarks visit:
+// http://www.heatonresearch.com/copyright
+//
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Encog.Engine.Network.Activation;
+using Encog.MathUtil.Randomize;
+using Encog.ML;
+using Encog.ML.Data;
+using Encog.ML.Data.Basic;
+using Encog.Neural.Freeform.Basic;
+using Encog.Neural.Freeform.Factory;
+using Encog.Neural.Networks;
+using Encog.Util;
+using Encog.Util.Simple;
+
+namespace Encog.Neural.Freeform
+{
+ ///
+ /// Implements a freefrom neural network. A freeform neural network can represent
+ /// much more advanced structures than the flat networks that the Encog
+ /// BasicNetwork implements. However, while freeform networks are more advanced
+ /// than the BasicNetwork, they are also much slower.
+ /// Freeform networks allow just about any neuron to be connected to another
+ /// neuron. You can have neuron layers if you want, but they are not required.
+ ///
+ [Serializable]
+ public class FreeformNetwork : BasicML, IMLContext,
+ IMLRegression, IMLEncodable, IMLResettable, IMLClassification, IMLError
+ {
+ ///
+ /// Perform a task for each connection.
+ ///
+ /// The connection.
+ public delegate void FreeformConnectionTask(IFreeformConnection connection);
+
+ ///
+ /// Perofmr a task for each neuron.
+ ///
+ /// The neuron.
+ public delegate void FreeformNeuronTask(IFreeformNeuron neuron);
+
+ ///
+ /// The connection factory.
+ ///
+ private readonly IFreeformConnectionFactory _connectionFactory = new BasicFreeformConnectionFactory();
+
+ ///
+ /// The layer factory.
+ ///
+ private readonly IFreeformLayerFactory _layerFactory = new BasicFreeformLayerFactory();
+
+ ///
+ /// The neuron factory.
+ ///
+ private readonly IFreeformNeuronFactory _neuronFactory = new BasicFreeformNeuronFactory();
+
+ ///
+ /// The input summation factory.
+ ///
+ private readonly IInputSummationFactory _summationFactory = new BasicActivationSummationFactory();
+
+ ///
+ /// The input layer.
+ ///
+ private IFreeformLayer _inputLayer;
+
+ ///
+ /// The output layer.
+ ///
+ private IFreeformLayer _outputLayer;
+
+ ///
+ /// Default constructor. Typically should not be directly used.
+ ///
+ public FreeformNetwork()
+ {
+ }
+
+ ///
+ /// Craete a freeform network from a basic network.
+ ///
+ /// The basic network to use.
+ public FreeformNetwork(BasicNetwork network)
+ {
+ if (network.LayerCount < 2)
+ {
+ throw new FreeformNetworkError(
+ "The BasicNetwork must have at least two layers to be converted.");
+ }
+
+ // handle each layer
+ IFreeformLayer previousLayer = null;
+
+ for (int currentLayerIndex = 0;
+ currentLayerIndex < network
+ .LayerCount;
+ currentLayerIndex++)
+ {
+ // create the layer
+ IFreeformLayer currentLayer = _layerFactory.Factor();
+
+ // Is this the input layer?
+ if (_inputLayer == null)
+ {
+ _inputLayer = currentLayer;
+ }
+
+ // Add the neurons for this layer
+ for (int i = 0; i < network.GetLayerNeuronCount(currentLayerIndex); i++)
+ {
+ // obtain the summation object.
+ IInputSummation summation = null;
+
+ if (previousLayer != null)
+ {
+ summation = _summationFactory.Factor(network
+ .GetActivation(currentLayerIndex));
+ }
+
+ // add the new neuron
+ currentLayer.Add(_neuronFactory.FactorRegular(summation));
+ }
+
+ // Fully connect this layer to previous
+ if (previousLayer != null)
+ {
+ ConnectLayersFromBasic(network, currentLayerIndex - 1,
+ previousLayer, currentLayer);
+ }
+
+ // Add the bias neuron
+ // The bias is added after connections so it has no inputs
+ if (network.IsLayerBiased(currentLayerIndex))
+ {
+ IFreeformNeuron biasNeuron = _neuronFactory
+ .FactorRegular(null);
+ biasNeuron.IsBias = true;
+ biasNeuron.Activation = network
+ .GetLayerBiasActivation(currentLayerIndex);
+ currentLayer.Add(biasNeuron);
+ }
+
+ // update previous layer
+ previousLayer = currentLayer;
+ }
+
+ // finally, set the output layer.
+ _outputLayer = previousLayer;
+ }
+
+ ///
+ /// The output layer.
+ ///
+ public IFreeformLayer OutputLayer
+ {
+ get { return _outputLayer; }
+ }
+
+ ///
+ public int Classify(IMLData input)
+ {
+ IMLData output = Compute(input);
+ return EngineArray.MaxIndex(output);
+ }
+
+ ///
+ public void ClearContext()
+ {
+ PerformNeuronTask(
+ neuron =>
+ {
+ if (neuron is FreeformContextNeuron)
+ {
+ neuron.Activation = 0;
+ }
+ });
+ }
+
+ ///
+ public void DecodeFromArray(double[] encoded)
+ {
+ int index = 0;
+ var visited = new HashSet();
+ IList queue = _outputLayer.Neurons.ToList();
+
+ // first copy outputs to queue
+
+ while (queue.Count > 0)
+ {
+ // pop a neuron off the queue
+ IFreeformNeuron neuron = queue[0];
+ queue.RemoveAt(0);
+ visited.Add(neuron);
+
+ // find anymore neurons and add them to the queue.
+ if (neuron.InputSummation != null)
+ {
+ foreach (IFreeformConnection connection in neuron
+ .InputSummation.List)
+ {
+ connection.Weight = encoded[index++];
+ IFreeformNeuron nextNeuron = connection.Source;
+ if (!visited.Contains(nextNeuron))
+ {
+ queue.Add(nextNeuron);
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ public int EncodedArrayLength()
+ {
+ int result = 0;
+ var visited = new HashSet();
+ IList queue = _outputLayer.Neurons.ToList();
+
+ // first copy outputs to queue
+
+ while (queue.Count > 0)
+ {
+ // pop a neuron off the queue
+ IFreeformNeuron neuron = queue[0];
+ queue.RemoveAt(0);
+ visited.Add(neuron);
+
+ // find anymore neurons and add them to the queue.
+ if (neuron.InputSummation != null)
+ {
+ foreach (IFreeformConnection connection in neuron
+ .InputSummation.List)
+ {
+ result++;
+ IFreeformNeuron nextNeuron = connection.Source;
+ if (!visited.Contains(nextNeuron))
+ {
+ queue.Add(nextNeuron);
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ ///
+ public void EncodeToArray(double[] encoded)
+ {
+ int index = 0;
+ var visited = new HashSet();
+ IList queue = _outputLayer.Neurons.ToList();
+
+ // first copy outputs to queue
+
+ while (queue.Count > 0)
+ {
+ // pop a neuron off the queue
+ IFreeformNeuron neuron = queue[0];
+ queue.RemoveAt(0);
+ visited.Add(neuron);
+
+ // find anymore neurons and add them to the queue.
+ if (neuron.InputSummation != null)
+ {
+ foreach (IFreeformConnection connection in neuron
+ .InputSummation.List)
+ {
+ encoded[index++] = connection.Weight;
+ IFreeformNeuron nextNeuron = connection.Source;
+ if (!visited.Contains(nextNeuron))
+ {
+ queue.Add(nextNeuron);
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ public double CalculateError(IMLDataSet data)
+ {
+ return EncogUtility.CalculateRegressionError(this, data);
+ }
+
+ ///
+ public IMLData Compute(IMLData input)
+ {
+ // Allocate result
+ var result = new BasicMLData(_outputLayer.Count);
+
+ // Copy the input
+ for (int i = 0; i < input.Count; i++)
+ {
+ _inputLayer.SetActivation(i, input[i]);
+ }
+
+ // Request calculation of outputs
+ for (int i = 0; i < _outputLayer.Count; i++)
+ {
+ IFreeformNeuron outputNeuron = _outputLayer.Neurons[i];
+ outputNeuron.PerformCalculation();
+ result[i] = outputNeuron.Activation;
+ }
+
+ UpdateContext();
+
+ return result;
+ }
+
+ ///
+ public int InputCount
+ {
+ get { return _inputLayer.CountNonBias; }
+ }
+
+ ///
+ public int OutputCount
+ {
+ get { return _outputLayer.CountNonBias; }
+ }
+
+ ///
+ public void Reset()
+ {
+ Reset((int) (DateTime.Now.Ticks%int.MaxValue));
+ }
+
+ ///
+ public void Reset(int seed)
+ {
+ var randomizer = new ConsistentRandomizer(-1, 1,
+ seed);
+
+ /**
+ * {@inheritDoc}
+ */
+ PerformConnectionTask(connection => { connection.Weight = randomizer.NextDouble(); });
+ }
+
+ ///
+ /// Construct an Elmann recurrent neural network.
+ ///
+ /// The input count.
+ /// The hidden count.
+ /// The output count.
+ /// The activation function.
+ /// The newly created network.
+ public static FreeformNetwork CreateElman(int input,
+ int hidden1, int output, IActivationFunction af)
+ {
+ var network = new FreeformNetwork();
+ IFreeformLayer inputLayer = network.CreateInputLayer(input);
+ IFreeformLayer hiddenLayer1 = network.CreateLayer(hidden1);
+ IFreeformLayer outputLayer = network.CreateOutputLayer(output);
+
+ network.ConnectLayers(inputLayer, hiddenLayer1, af, 1.0, false);
+ network.ConnectLayers(hiddenLayer1, outputLayer, af, 1.0, false);
+ network.CreateContext(hiddenLayer1, hiddenLayer1);
+ network.Reset();
+
+ return network;
+ }
+
+ ///
+ /// Create a feedforward freeform neural network.
+ ///
+ /// The input count.
+ /// The first hidden layer count, zero if none.
+ /// The second hidden layer count, zero if none.
+ /// The output count.
+ /// The activation function.
+ /// The newly crated network.
+ public static FreeformNetwork CreateFeedforward(int input,
+ int hidden1, int hidden2, int output,
+ IActivationFunction af)
+ {
+ var network = new FreeformNetwork();
+ IFreeformLayer lastLayer = network.CreateInputLayer(input);
+ IFreeformLayer currentLayer;
+
+ if (hidden1 > 0)
+ {
+ currentLayer = network.CreateLayer(hidden1);
+ network.ConnectLayers(lastLayer, currentLayer, af, 1.0, false);
+ lastLayer = currentLayer;
+ }
+
+ if (hidden2 > 0)
+ {
+ currentLayer = network.CreateLayer(hidden2);
+ network.ConnectLayers(lastLayer, currentLayer, af, 1.0, false);
+ lastLayer = currentLayer;
+ }
+
+ currentLayer = network.CreateOutputLayer(output);
+ network.ConnectLayers(lastLayer, currentLayer, af, 1.0, false);
+
+ network.Reset();
+
+ return network;
+ }
+
+ ///
+ public object Clone()
+ {
+ var result = (BasicNetwork) ObjectCloner.DeepCopy(this);
+ return result;
+ }
+
+ ///
+ /// Connect two layers. These layers will be connected with a TANH activation
+ /// function in a non-recurrent way. A bias activation of 1.0 will be used,
+ /// if needed.
+ ///
+ /// The source layer.
+ /// The target layer.
+ public void ConnectLayers(IFreeformLayer source,
+ IFreeformLayer target)
+ {
+ ConnectLayers(source, target, new ActivationTANH(), 1.0, false);
+ }
+
+ ///
+ /// Connect two layers.
+ ///
+ /// The source layer.
+ /// The target layer.
+ /// The activation function to use.
+ /// The bias activation to use.
+ /// True, if this is a recurrent connection.
+ public void ConnectLayers(IFreeformLayer source,
+ IFreeformLayer target,
+ IActivationFunction theActivationFunction,
+ double biasActivation, bool isRecurrent)
+ {
+ // create bias, if requested
+ if (biasActivation > EncogFramework.DefaultDoubleEqual)
+ {
+ // does the source already have a bias?
+ if (source.HasBias)
+ {
+ throw new FreeformNetworkError(
+ "The source layer already has a bias neuron, you cannot create a second.");
+ }
+ IFreeformNeuron biasNeuron = _neuronFactory
+ .FactorRegular(null);
+ biasNeuron.Activation = biasActivation;
+ biasNeuron.IsBias = true;
+ source.Add(biasNeuron);
+ }
+
+ // create connections
+ foreach (IFreeformNeuron targetNeuron in target.Neurons)
+ {
+ // create the summation for the target
+ IInputSummation summation = targetNeuron.InputSummation;
+
+ // do not create a second input summation
+ if (summation == null)
+ {
+ summation = _summationFactory.Factor(theActivationFunction);
+ targetNeuron.InputSummation = summation;
+ }
+
+ // connect the source neurons to the target neuron
+ foreach (IFreeformNeuron sourceNeuron in source.Neurons)
+ {
+ IFreeformConnection connection = _connectionFactory
+ .Factor(sourceNeuron, targetNeuron);
+ sourceNeuron.AddOutput(connection);
+ targetNeuron.AddInput(connection);
+ }
+ }
+ }
+
+ ///
+ /// Connect two layers, assume bias activation of 1.0 and non-recurrent
+ /// connection.
+ ///
+ /// The source layer.
+ /// The target layer.
+ /// The activation function.
+ public void ConnectLayers(IFreeformLayer source,
+ IFreeformLayer target,
+ IActivationFunction theActivationFunction)
+ {
+ ConnectLayers(source, target, theActivationFunction, 1.0, false);
+ }
+
+ ///
+ /// Connect layers from a BasicNetwork. Used internally only.
+ ///
+ /// The BasicNetwork.
+ /// The from layer index.
+ /// The from layer.
+ /// The target.
+ private void ConnectLayersFromBasic(BasicNetwork network,
+ int fromLayerIdx, IFreeformLayer source, IFreeformLayer target)
+ {
+ for (int targetNeuronIdx = 0; targetNeuronIdx < target.Count; targetNeuronIdx++)
+ {
+ for (int sourceNeuronIdx = 0; sourceNeuronIdx < source.Count; sourceNeuronIdx++)
+ {
+ IFreeformNeuron sourceNeuron = source.Neurons[sourceNeuronIdx];
+ IFreeformNeuron targetNeuron = target.Neurons[targetNeuronIdx];
+
+ // neurons with no input (i.e. bias neurons)
+ if (targetNeuron.InputSummation == null)
+ {
+ continue;
+ }
+
+ IFreeformConnection connection = _connectionFactory
+ .Factor(sourceNeuron, targetNeuron);
+ sourceNeuron.AddOutput(connection);
+ targetNeuron.AddInput(connection);
+ double weight = network.GetWeight(fromLayerIdx,
+ sourceNeuronIdx, targetNeuronIdx);
+ connection.Weight = weight;
+ }
+ }
+ }
+
+
+ ///
+ /// Create a context connection, such as those used by Jordan/Elmann.
+ ///
+ /// The source layer.
+ /// The target layer.
+ /// The newly created context layer.
+ public IFreeformLayer CreateContext(IFreeformLayer source,
+ IFreeformLayer target)
+ {
+ const double biasActivation = 0.0;
+
+ if (source.Neurons[0].Outputs.Count < 1)
+ {
+ throw new FreeformNetworkError(
+ "A layer cannot have a context layer connected if there are no other outbound connections from the source layer. Please connect the source layer somewhere else first.");
+ }
+
+ IActivationFunction activatonFunction = source.Neurons[0].InputSummation
+ .ActivationFunction;
+
+ // first create the context layer
+ IFreeformLayer result = _layerFactory.Factor();
+
+ for (int i = 0; i < source.Count; i++)
+ {
+ IFreeformNeuron neuron = source.Neurons[i];
+ if (neuron.IsBias)
+ {
+ IFreeformNeuron biasNeuron = _neuronFactory
+ .FactorRegular(null);
+ biasNeuron.IsBias = true;
+ biasNeuron.Activation = neuron.Activation;
+ result.Add(biasNeuron);
+ }
+ else
+ {
+ IFreeformNeuron contextNeuron = _neuronFactory
+ .FactorContext(neuron);
+ result.Add(contextNeuron);
+ }
+ }
+
+ // now connect the context layer to the target layer
+
+ ConnectLayers(result, target, activatonFunction, biasActivation, false);
+
+ return result;
+ }
+
+ ///
+ /// Create the input layer.
+ ///
+ /// The input neuron count.
+ /// The newly created layer.
+ public IFreeformLayer CreateInputLayer(int neuronCount)
+ {
+ if (neuronCount < 1)
+ {
+ throw new FreeformNetworkError(
+ "Input layer must have at least one neuron.");
+ }
+ _inputLayer = CreateLayer(neuronCount);
+ return _inputLayer;
+ }
+
+ ///
+ /// Create a hidden layer.
+ ///
+ /// The neuron count.
+ /// The newly created layer.
+ public IFreeformLayer CreateLayer(int neuronCount)
+ {
+ if (neuronCount < 1)
+ {
+ throw new FreeformNetworkError(
+ "Layer must have at least one neuron.");
+ }
+
+ IFreeformLayer result = _layerFactory.Factor();
+
+ // Add the neurons for this layer
+ for (int i = 0; i < neuronCount; i++)
+ {
+ result.Add(_neuronFactory.FactorRegular(null));
+ }
+
+ return result;
+ }
+
+ ///
+ /// Create the output layer.
+ ///
+ /// The neuron count.
+ /// The newly created output layer.
+ public IFreeformLayer CreateOutputLayer(int neuronCount)
+ {
+ if (neuronCount < 1)
+ {
+ throw new FreeformNetworkError(
+ "Output layer must have at least one neuron.");
+ }
+ _outputLayer = CreateLayer(neuronCount);
+ return _outputLayer;
+ }
+
+ ///
+ /// Perform the specified connection task. This task will be performed over
+ /// all connections.
+ ///
+ /// The connection task.
+ public void PerformConnectionTask(FreeformConnectionTask task)
+ {
+ var visited = new HashSet();
+
+ foreach (IFreeformNeuron neuron in _outputLayer.Neurons)
+ {
+ PerformConnectionTask(visited, neuron, task);
+ }
+ }
+
+
+ ///