/// <summary>
 ///     Create a new class.
 /// </summary>
 /// <param name="className">The class name.</param>
 /// <returns>The newly created class.</returns>
 public EncogProgramNode CreateClass(String className)
 {
     var node = new EncogProgramNode(this, this,
                                     NodeType.Class, className);
     Children.Add(node);
     return node;
 }
        /// <summary>
        ///     Create a new main function.
        /// </summary>
        /// <returns>The newly created main function.</returns>
        public EncogProgramNode CreateMainFunction()
        {
            var node = new EncogProgramNode(Program, this,
                                            NodeType.MainFunction, null);

            Children.Add(node);
            return(node);
        }
        /// <summary>
        ///     Create a function.
        /// </summary>
        /// <param name="theName">The name of the function.</param>
        /// <returns>The newly created function.</returns>
        public EncogProgramNode CreateFunction(string theName)
        {
            var node = new EncogProgramNode(Program, this,
                                            NodeType.StaticFunction, theName);

            Children.Add(node);
            return(node);
        }
Esempio n. 4
0
        /// <summary>
        ///     Create a new class.
        /// </summary>
        /// <param name="className">The class name.</param>
        /// <returns>The newly created class.</returns>
        public EncogProgramNode CreateClass(String className)
        {
            var node = new EncogProgramNode(this, this,
                                            NodeType.Class, className);

            Children.Add(node);
            return(node);
        }
Esempio n. 5
0
        /// <summary>
        ///     Load the training data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>The newly created data load.</returns>
        public EncogProgramNode GenerateLoadTraining(FileInfo data)
        {
            var node = new EncogProgramNode(Program, this,
                                            NodeType.LoadTraining, "");

            node.AddArg(data);
            Children.Add(node);
            return(node);
        }
Esempio n. 6
0
        /// <summary>
        ///     Create an array.
        /// </summary>
        /// <param name="name">THe name of the array.</param>
        /// <param name="a">The value to init the array to.</param>
        /// <returns>The newly creatred array.</returns>
        public EncogProgramNode CreateArray(string name, double[] a)
        {
            var node = new EncogProgramNode(Program, this,
                                            NodeType.InitArray, name);

            node.AddArg(a);
            Children.Add(node);
            return(node);
        }
Esempio n. 7
0
        /// <summary>
        ///     Create a new network function.
        /// </summary>
        /// <param name="name">The name of the network function.</param>
        /// <param name="method">The method to call.</param>
        /// <returns>The newly created network function.</returns>
        public EncogProgramNode CreateNetworkFunction(string name,
                                                      FileInfo method)
        {
            var node = new EncogProgramNode(Program, this,
                                            NodeType.CreateNetwork, name);

            node.AddArg(method);
            Children.Add(node);
            return(node);
        }
Esempio n. 8
0
        /// <summary>
        ///     Define a const.
        /// </summary>
        /// <param name="type">The type of const.</param>
        /// <param name="name">The name of the const.</param>
        /// <param name="value">The value of the const.</param>
        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);
        }
Esempio n. 9
0
        /// <summary>
        ///     Create a function call.
        /// </summary>
        /// <param name="name">The name of the function to call.</param>
        /// <param name="returnType">The return type.</param>
        /// <param name="returnVariable">The variable to assign the function to.</param>
        /// <returns>The newly created function call.</returns>
        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);
        }
        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 GenerateClass(EncogProgramNode node)
        {
            AddBreak();

            AddLine("<!DOCTYPE html>");
            AddLine("<html>");
            AddLine("<head>");
            AddLine("<title>Encog Generated Javascript</title>");
            AddLine("</head>");
            AddLine("<body>");
            AddLine("<script src=\"../encog.js\"></script>");
            AddLine("<script src=\"../encog-widget.js\"></script>");
            AddLine("<pre>");
            AddLine("<script type=\"text/javascript\">");

            GenerateForChildren(node);

            AddLine("</script>");
            AddLine(
                "<noscript>Your browser does not support JavaScript! Note: if you are trying to view this in Encog Workbench, right-click file and choose \"Open as Text\".</noscript>");
            AddLine("</pre>");
            AddLine("</body>");
            AddLine("</html>");
        }
 private void GenerateComment(EncogProgramNode commentNode)
 {
     AddLine("// " + commentNode.Name);
 }
Esempio n. 13
0
 private void GenerateMainFunction(EncogProgramNode node)
 {
     AddBreak();
     AddLine("static void Main(string[] args)");
     IndentLine("{");
     GenerateForChildren(node);
     UnIndentLine("}");
 }
 /// <summary>
 ///     Load the training data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <returns>The newly created data load.</returns>
 public EncogProgramNode GenerateLoadTraining(FileInfo data)
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.LoadTraining, "");
     node.AddArg(data);
     Children.Add(node);
     return node;
 }
 private void GenerateMainFunction(EncogProgramNode node)
 {
     AddBreak();
     GenerateForChildren(node);
 }
        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);
            }

            FlatNetwork flat = ((IContainsFlat) method).Flat;

            // header
            var line = new StringBuilder();
            line.Append("public static MLMethod ");
            line.Append(node.Name);
            line.Append("() {");
            IndentLine(line.ToString());

            // create factory
            line.Length = 0;

            AddLine("var network = ENCOG.BasicNetwork.create( null );");
            AddLine("network.inputCount = " + flat.InputCount + ";");
            AddLine("network.outputCount = " + flat.OutputCount + ";");
            AddLine("network.layerCounts = "
                    + ToSingleLineArray(flat.LayerCounts) + ";");
            AddLine("network.layerContextCount = "
                    + ToSingleLineArray(flat.LayerContextCount) + ";");
            AddLine("network.weightIndex = "
                    + ToSingleLineArray(flat.WeightIndex) + ";");
            AddLine("network.layerIndex = "
                    + ToSingleLineArray(flat.LayerIndex) + ";");
            AddLine("network.activationFunctions = "
                    + ToSingleLineArray(flat.ActivationFunctions) + ";");
            AddLine("network.layerFeedCounts = "
                    + ToSingleLineArray(flat.LayerFeedCounts) + ";");
            AddLine("network.contextTargetOffset = "
                    + ToSingleLineArray(flat.ContextTargetOffset) + ";");
            AddLine("network.contextTargetSize = "
                    + ToSingleLineArray(flat.ContextTargetSize) + ";");
            AddLine("network.biasActivation = "
                    + ToSingleLineArray(flat.BiasActivation) + ";");
            AddLine("network.beginTraining = " + flat.BeginTraining + ";");
            AddLine("network.endTraining=" + flat.EndTraining + ";");
            AddLine("network.weights = WEIGHTS;");
            AddLine("network.layerOutput = "
                    + ToSingleLineArray(flat.LayerOutput) + ";");
            AddLine("network.layerSums = " + ToSingleLineArray(flat.LayerSums)
                    + ";");

            // return
            AddLine("return network;");

            UnIndentLine("}");
        }
        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("}");
        }
        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 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 GenerateEmbedTraining(EncogProgramNode node)
 {
     if (embed)
     {
         EmbedTraining(node);
     }
 }
 private void GenerateCreateNetwork(EncogProgramNode node)
 {
     if (embed)
     {
         EmbedNetwork(node);
     }
     else
     {
         LinkNetwork(node);
     }
 }
 private void GenerateClass(EncogProgramNode node)
 {
     AddBreak();
     IndentLine("public class " + node.Name + " {");
     GenerateForChildren(node);
     UnIndentLine("}");
 }
        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("}");
        }
 /// <summary>
 ///     Create a function.
 /// </summary>
 /// <param name="theName">The name of the function.</param>
 /// <returns>The newly created function.</returns>
 public EncogProgramNode CreateFunction(string theName)
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.StaticFunction, theName);
     Children.Add(node);
     return node;
 }
        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());
        }
 /// <summary>
 ///     Create a function call.
 /// </summary>
 /// <param name="name">The name of the function to call.</param>
 /// <param name="returnType">The return type.</param>
 /// <param name="returnVariable">The variable to assign the function to.</param>
 /// <returns>The newly created function call.</returns>
 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;
 }
 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;
     }
 }
 /// <summary>
 ///     Create a new main function.
 /// </summary>
 /// <returns>The newly created main function.</returns>
 public EncogProgramNode CreateMainFunction()
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.MainFunction, null);
     Children.Add(node);
     return node;
 }
        /**
         * GEnerate from a machine learning method.
         *
         * @param mainClass
         *            The main class.
         * @param method
         *            The filename of the method.
         * @return The newly created node.
         */
        private EncogProgramNode GenerateForMethod(
            EncogProgramNode mainClass, FileInfo method)
        {
            if (EmbedData)
            {
                var encodable = (IMLEncodable) EncogDirectoryPersistence
                                                   .LoadObject(method);
                var weights = new double[encodable.EncodedArrayLength()];
                encodable.EncodeToArray(weights);
                mainClass.CreateArray("WEIGHTS", weights);
            }

            return mainClass.CreateNetworkFunction("createNetwork", method);
        }
Esempio n. 31
0
        private void GenerateFunctionCall(EncogProgramNode node)
        {
            AddBreak();
            var line = new StringBuilder();
            if (node.Args[0].Value.ToString().Length > 0)
            {
                String objType = node.Args[0].Value.ToString();

                if (objType.Equals("MLMethod"))
                {
                    objType = "IMLMethod";
                }
                else if (objType.Equals("MLDataSet"))
                {
                    objType = "IMLDataSet";
                }

                line.Append(objType);
                line.Append(" ");
                line.Append(node.Args[1].Value);
                line.Append(" = ");
            }

            line.Append(node.Name);
            line.Append("();");
            AddLine(line.ToString());
        }
 /// <summary>
 ///     Add a comment.
 /// </summary>
 /// <param name="str">The comment.</param>
 public void AddComment(string str)
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.Comment, str);
     children.Add(node);
 }
 /// <summary>
 ///     Create a new network function.
 /// </summary>
 /// <param name="name">The name of the network function.</param>
 /// <param name="method">The method to call.</param>
 /// <returns>The newly created network function.</returns>
 public EncogProgramNode CreateNetworkFunction(string name,
                                               FileInfo method)
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.CreateNetwork, name);
     node.AddArg(method);
     Children.Add(node);
     return node;
 }
        private void EmbedTraining(EncogProgramNode node)
        {
            var dataFile = (FileInfo) node.Args[0].Value;
            IMLDataSet data = EncogUtility.LoadEGB2Memory(dataFile);

            // generate the input data

            IndentLine("var 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("var 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("];");
        }
        private void GenerateArrayInit(EncogProgramNode node)
        {
            var line = new StringBuilder();
            line.Append("var ");
            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("];");
        }
 /// <summary>
 ///     Create an array.
 /// </summary>
 /// <param name="name">THe name of the array.</param>
 /// <param name="a">The value to init the array to.</param>
 /// <returns>The newly creatred array.</returns>
 public EncogProgramNode CreateArray(string name, double[] a)
 {
     var node = new EncogProgramNode(Program, this,
                                     NodeType.InitArray, name);
     node.AddArg(a);
     Children.Add(node);
     return node;
 }
 /// <summary>
 ///     Define a const.
 /// </summary>
 /// <param name="type">The type of const.</param>
 /// <param name="name">The name of the const.</param>
 /// <param name="value">The value of the const.</param>
 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);
 }