/// <summary>
        /// Parse a matrix from a name-value collection of params.
        /// </summary>
        ///
        /// <param name="paras">The name-value pairs.</param>
        /// <param name="name">The name to parse.</param>
        /// <returns>The parsed matrix value.</returns>
        public static Matrix ParseMatrix(IDictionary <String, String> paras,
                                         String name)
        {
            if (!paras.ContainsKey(name))
            {
                throw new PersistError("Missing property: " + name);
            }

            String line = paras[name];

            double[] d    = NumberList.FromList(CSVFormat.EgFormat, line);
            var      rows = (int)d[0];
            var      cols = (int)d[1];

            var result = new Matrix(rows, cols);

            int index = 2;

            for (int r = 0; r < rows; r++)
            {
                for (int c = 0; c < cols; c++)
                {
                    result[r, c] = d[index++];
                }
            }

            return(result);
        }
        /// <summary>
        /// Read a an object.
        /// </summary>
        public Object Read(Stream mask0)
        {
            var result = new HopfieldNetwork();
            var ins0   = new EncogReadHelper(mask0);
            EncogFileSection section;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("HOPFIELD") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("HOPFIELD") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    result.Weights = NumberList.FromList(CSVFormat.EgFormat,
                                                         (p[PersistConst.Weights]));
                    result.SetCurrentState(NumberList.FromList(CSVFormat.EgFormat,
                                                               (p[PersistConst.Output])));
                    result.NeuronCount = EncogFileSection.ParseInt(p,
                                                                   PersistConst.NeuronCount);
                }
            }

            return(result);
        }
        /// <summary>
        /// Parse a double array from a name-value collection of params.
        /// </summary>
        ///
        /// <param name="paras">The name-value pairs.</param>
        /// <param name="name">The name to parse.</param>
        /// <returns>The parsed double array value.</returns>
        public double[] ParseDoubleArray(IDictionary <String, String> paras,
                                         String name)
        {
            String v = null;

            try
            {
                if (!paras.ContainsKey(name))
                {
                    throw new PersistError("Missing property: " + name);
                }

                v = paras[name];

                if (v.StartsWith("##"))
                {
                    int i = int.Parse(v.Substring(2));
                    return(_largeArrays[i]);
                }
                else
                {
                    return(NumberList.FromList(CSVFormat.EgFormat, v));
                }
            }
            catch (FormatException)
            {
                throw new PersistError("Field: " + name + ", "
                                       + "invalid integer: " + v);
            }
        }
        /// <summary>
        /// Called internally to read a large array.
        /// </summary>
        /// <param name="line">The line containing the beginning of a large array.</param>
        /// <returns>The array read.</returns>
        private double[] ReadLargeArray(String line)
        {
            String str = line.Substring(9);
            int    l   = int.Parse(str);

            double[] result = new double[l];

            int index = 0;

            while ((line = this.reader.ReadLine()) != null)
            {
                line = line.Trim();

                // is it a comment
                if (line.StartsWith("//"))
                {
                    continue;
                }
                else if (line.StartsWith("##end"))
                {
                    break;
                }

                double[] t = NumberList.FromList(CSVFormat.EgFormat, line);
                EngineArray.ArrayCopy(t, 0, result, index, t.Length);
                index += t.Length;
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Load a matrix from the reader.
        /// </summary>
        /// <param name="xmlIn">The XML reader.</param>
        /// <returns>The loaded matrix.</returns>
        public static Matrix LoadMatrix(ReadXML xmlIn)
        {
            int rows = xmlIn.LastTag.GetAttributeInt(
                PersistorUtil.ATTRIBUTE_MATRIX_ROWS);
            int cols = xmlIn.LastTag.GetAttributeInt(
                PersistorUtil.ATTRIBUTE_MATRIX_COLS);
            Matrix matrix = new Matrix(rows, cols);

            int row = 0;

            String end = xmlIn.LastTag.Name;

            while (xmlIn.ReadToTag())
            {
                if (xmlIn.IsIt(end, false))
                {
                    break;
                }
                if (xmlIn.IsIt(PersistorUtil.ROW, true))
                {
                    String   str = xmlIn.ReadTextToTag();
                    double[] d   = NumberList.FromList(CSVFormat.EG_FORMAT, str);
                    for (int col = 0; col < d.Length; col++)
                    {
                        matrix[row, col] = d[col];
                    }
                    row++;
                }
            }

            return(matrix);
        }
        /// <summary>
        /// Handle an item.
        /// </summary>
        /// <param name="xmlin">The XML input object.</param>
        public void HandleItem(ReadXML xmlin)
        {
            String name = xmlin.LastTag.GetAttributeValue(
                TrainingContinuationPersistor.ATTRIBUTE_NAME);
            String str = xmlin.ReadTextToTag();

            double[] list = NumberList.FromList(CSVFormat.EG_FORMAT, str);
            this.current[name] = list;
        }
        /// <inheritdoc/>
        public IActivationFunction CreateActivationFunction(String fn)
        {
            String name;

            double[] p;

            int index = fn.IndexOf('[');

            if (index != -1)
            {
                name = fn.Substring(0, index).ToLower();
                int index2 = fn.IndexOf(']');
                if (index2 == -1)
                {
                    throw new EncogError(
                              "Unbounded [ while parsing activation function.");
                }
                String a = fn.Substring(index + 1, index2);
                p = NumberList.FromList(CSVFormat.EgFormat, a);
            }
            else
            {
                name = fn.ToLower();
                p    = new double[0];
            }

            IActivationFunction af = AllocateAF(name);

            if (af == null)
            {
                return(null);
            }

            if (af.ParamNames.Length != p.Length)
            {
                throw new EncogError(name + " expected "
                                     + af.ParamNames.Length + ", but " + p.Length
                                     + " were provided.");
            }

            for (int i = 0; i < af.ParamNames.Length; i++)
            {
                af.Params[i] = p[i];
            }

            return(af);
        }
Beispiel #8
0
        /// <summary>
        /// Parse a double array from a name-value collection of params.
        /// </summary>
        ///
        /// <param name="paras">The name-value pairs.</param>
        /// <param name="name">The name to parse.</param>
        /// <returns>The parsed double array value.</returns>
        public static double[] ParseDoubleArray(IDictionary <String, String> paras,
                                                String name)
        {
            String v = null;

            try
            {
                v = paras[name];
                if (v == null)
                {
                    throw new PersistError("Missing property: " + name);
                }

                return(NumberList.FromList(CSVFormat.EgFormat, v));
            }
            catch (FormatException)
            {
                throw new PersistError("Field: " + name + ", "
                                       + "invalid integer: " + v);
            }
        }
Beispiel #9
0
        private IRadialBasisFunction LoadRBF(String name, ReadXML xmlin)
        {
            String clazz = ReflectionUtil.ResolveEncogClass(name);

            if (clazz == null)
            {
                throw new NeuralNetworkError("Unknown RBF function type: " + name);
            }

            IRadialBasisFunction result = (IRadialBasisFunction)ReflectionUtil.LoadObject(clazz);

            while (xmlin.ReadToTag())
            {
                if (xmlin.IsIt(name, false))
                {
                    break;
                }
                else if (xmlin.IsIt(PROPERTY_CENTERS, true))
                {
                    String   str     = xmlin.ReadTextToTag();
                    double[] centers = NumberList.FromList(CSVFormat.EG_FORMAT, str);
                    result.Centers = centers;
                }
                else if (xmlin.IsIt(PROPERTY_PEAK, true))
                {
                    String str = xmlin.ReadTextToTag();
                    double d   = Double.Parse(str);
                    result.Peak = d;
                }
                else if (xmlin.IsIt(PROPERTY_WIDTH, true))
                {
                    String str = xmlin.ReadTextToTag();
                    double d   = Double.Parse(str);
                    result.Width = d;
                }
            }

            return(result);
        }
Beispiel #10
0
        /// <inheritdoc/>
        public Object Read(Stream mask0)
        {
            var result = new BoltzmannMachine();
            var ins0   = new EncogReadHelper(mask0);
            EncogFileSection section;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BOLTZMANN") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("BOLTZMANN") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    result.Weights = NumberList.FromList(CSVFormat.EgFormat,
                                                         (p[PersistConst.Weights]));
                    result.SetCurrentState(NumberList.FromList(CSVFormat.EgFormat,
                                                               (p[PersistConst.Output])));
                    result.NeuronCount = EncogFileSection.ParseInt(p,
                                                                   PersistConst.NeuronCount);

                    result.Threshold = NumberList.FromList(CSVFormat.EgFormat,
                                                           (p[PersistConst.Thresholds]));
                    result.AnnealCycles = EncogFileSection.ParseInt(p,
                                                                    BoltzmannMachine.ParamAnnealCycles);
                    result.RunCycles = EncogFileSection.ParseInt(p,
                                                                 BoltzmannMachine.ParamRunCycles);
                    result.Temperature = EncogFileSection.ParseDouble(p,
                                                                      PersistConst.Temperature);
                }
            }

            return(result);
        }
        /// <summary>
        /// Handle reading an item tag.
        /// </summary>
        /// <param name="xmlIn">The XML reader.</param>
        private void HandleItem(ReadXML xmlIn)
        {
            IDictionary <String, String> properties = xmlIn.ReadPropertyBlock();
            INeuralDataPair pair  = null;
            INeuralData     input = new BasicNeuralData(NumberList
                                                        .FromList(CSVFormat.EG_FORMAT, properties
                                                                  [BasicNeuralDataSetPersistor.TAG_INPUT]));

            if (properties.ContainsKey(BasicNeuralDataSetPersistor.TAG_IDEAL))
            {
                // supervised
                INeuralData ideal = new BasicNeuralData(NumberList
                                                        .FromList(CSVFormat.EG_FORMAT, properties
                                                                  [BasicNeuralDataSetPersistor.TAG_IDEAL]));
                pair = new BasicNeuralDataPair(input, ideal);
            }
            else
            {
                // unsupervised
                pair = new BasicNeuralDataPair(input);
            }

            this.currentDataSet.Add(pair);
        }
        /// <summary>
        /// Load the specified Encog object from an XML reader.
        /// </summary>
        /// <param name="xmlIn">The XML reader to use.</param>
        /// <returns>The loaded object.</returns>
        public IEncogPersistedObject Load(ReadXML xmlIn)
        {
            int    neuronCount             = 0;
            int    x                       = 0;
            int    y                       = 0;
            double biasActivation          = 1;
            String threshold               = null;
            IActivationFunction activation = null;
            String end                     = xmlIn.LastTag.Name;
            String context                 = null;

            while (xmlIn.ReadToTag())
            {
                if (xmlIn.IsIt(BasicLayerPersistor.TAG_ACTIVATION, true))
                {
                    xmlIn.ReadToTag();
                    String type = xmlIn.LastTag.Name;
                    activation = BasicLayerPersistor.LoadActivation(type, xmlIn);
                }
                else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_NEURONS, true))
                {
                    neuronCount = xmlIn.ReadIntToTag();
                }
                else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_X, true))
                {
                    x = xmlIn.ReadIntToTag();
                }
                else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_Y, true))
                {
                    y = xmlIn.ReadIntToTag();
                }
                else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_THRESHOLD, true))
                {
                    threshold = xmlIn.ReadTextToTag();
                }
                else if (xmlIn.IsIt(PROPERTY_CONTEXT, true))
                {
                    context = xmlIn.ReadTextToTag();
                }
                else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_BIAS_ACTIVATION, true))
                {
                    biasActivation = double.Parse(xmlIn.ReadTextToTag());
                }
                else if (xmlIn.IsIt(end, false))
                {
                    break;
                }
            }

            if (neuronCount > 0)
            {
                ContextLayer layer;

                if (threshold == null)
                {
                    layer = new ContextLayer(activation, false, neuronCount);
                }
                else
                {
                    double[] t = NumberList.FromList(CSVFormat.EG_FORMAT, threshold);
                    layer = new ContextLayer(activation, true, neuronCount);
                    for (int i = 0; i < t.Length; i++)
                    {
                        layer.BiasWeights[i] = t[i];
                    }
                }

                if (context != null)
                {
                    double[] c = NumberList.FromList(CSVFormat.EG_FORMAT, context);

                    for (int i = 0; i < c.Length; i++)
                    {
                        layer.Context[i] = c[i];
                    }
                }

                layer.X = x;
                layer.Y = y;
                layer.BiasActivation = biasActivation;

                return(layer);
            }
            return(null);
        }