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

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BAM") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("BAM") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();

                    result.F1Count = EncogFileSection.ParseInt(p,
                                                               PersistConst.PropertyF1Count);
                    result.F2Count = EncogFileSection.ParseInt(p,
                                                               PersistConst.PropertyF2Count);
                    result.WeightsF1ToF2 = EncogFileSection.ParseMatrix(p, PersistConst.PropertyWeightsF1F2);
                    result.WeightsF2ToF1 = EncogFileSection.ParseMatrix(p, PersistConst.PropertyWeightsF2F1);
                }
            }

            return(result);
        }
Beispiel #2
0
        /**
         * {@inheritDoc}
         */

        public Object Read(Stream istream)
        {
            var result = new SOMNetwork();
            var reader = new EncogReadHelper(istream);
            EncogFileSection section;

            while ((section = reader.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("SOM") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    EngineArray.PutAll(p, result.Properties);
                }
                if (section.SectionName.Equals("SOM") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    result.Weights = EncogFileSection.ParseMatrix(p,
                                                                  PersistConst.Weights)
                    ;
                }
            }

            return(result);
        }
        /// <summary>
        ///     Prepare a revert.
        /// </summary>
        /// <returns>Data that can be used to revert properties.</returns>
        public IDictionary <String, String> PrepareRevert()
        {
            IDictionary <String, String> result = new Dictionary <String, String>();

            EngineArray.PutAll(_data, result);
            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);
        }
Beispiel #5
0
        /// <inheritdoc/>
        public Object Read(Stream mask0)
        {
            IDictionary <String, String> networkParams = null;
            var ins0 = new EncogReadHelper(mask0);
            EncogFileSection section;
            int    inputCount  = 0;
            int    instarCount = 0;
            int    outputCount = 0;
            int    winnerCount = 0;
            Matrix m1          = null;
            Matrix m2          = null;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("CPN") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    networkParams = section.ParseParams();
                }
                if (section.SectionName.Equals("CPN") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> paras = section.ParseParams();

                    inputCount = EncogFileSection.ParseInt(paras,
                                                           PersistConst.InputCount);
                    instarCount = EncogFileSection.ParseInt(paras,
                                                            PersistConst.Instar);
                    outputCount = EncogFileSection.ParseInt(paras,
                                                            PersistConst.OutputCount);
                    winnerCount = EncogFileSection.ParseInt(paras,
                                                            PropertyWinnerCount);
                    m1 = EncogFileSection.ParseMatrix(paras,
                                                      PropertyInputToInstar);
                    m2 = EncogFileSection.ParseMatrix(paras,
                                                      PropertyInstarToInput);
                }
            }

            var result = new CPNNetwork(inputCount, instarCount, outputCount,
                                        winnerCount);

            EngineArray.PutAll(networkParams, result.Properties);
            result.WeightsInputToInstar.Set(m1);
            result.WeightsInstarToOutstar.Set(m2);
            return(result);
        }
        /// <inheritdoc/>
        public Object Read(Stream mask0)
        {
            var result = new ART1();
            var ins0   = new EncogReadHelper(mask0);
            EncogFileSection section;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("ART1") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("ART1") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();

                    result.A1 = EncogFileSection.ParseDouble(p,
                                                             BasicART.PropertyA1);
                    result.B1 = EncogFileSection.ParseDouble(p,
                                                             BasicART.PropertyB1);
                    result.C1 = EncogFileSection.ParseDouble(p,
                                                             BasicART.PropertyC1);
                    result.D1 = EncogFileSection.ParseDouble(p,
                                                             BasicART.PropertyD1);
                    result.F1Count = EncogFileSection.ParseInt(p,
                                                               PersistConst.PropertyF1Count);
                    result.F2Count = EncogFileSection.ParseInt(p,
                                                               PersistConst.PropertyF2Count);
                    result.NoWinner = EncogFileSection.ParseInt(p,
                                                                BasicART.PropertyNoWinner);
                    result.L = EncogFileSection
                               .ParseDouble(p, BasicART.PropertyL);
                    result.Vigilance = EncogFileSection.ParseDouble(p,
                                                                    BasicART.PropertyVigilance);
                    result.WeightsF1ToF2 = EncogFileSection.ParseMatrix(p,
                                                                        PersistConst.PropertyWeightsF1F2);
                    result.WeightsF2ToF1 = EncogFileSection.ParseMatrix(p,
                                                                        PersistConst.PropertyWeightsF2F1);
                }
            }

            return(result);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        /// <inheritdoc/>
        public Object Read(Stream mask0)
        {
            var result = new RBFNetwork();
            var flat   = (FlatNetworkRBF)result.Flat;

            var ins0 = new EncogReadHelper(mask0);
            EncogFileSection section;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("RBF-NETWORK") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("RBF-NETWORK") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> p = section.ParseParams();

                    flat.BeginTraining = EncogFileSection.ParseInt(p,
                                                                   BasicNetwork.TagBeginTraining);
                    flat.ConnectionLimit = EncogFileSection.ParseDouble(p,
                                                                        BasicNetwork.TagConnectionLimit);
                    flat.ContextTargetOffset = EncogFileSection.ParseIntArray(
                        p, BasicNetwork.TagContextTargetOffset);
                    flat.ContextTargetSize = EncogFileSection.ParseIntArray(
                        p, BasicNetwork.TagContextTargetSize);
                    flat.EndTraining = EncogFileSection.ParseInt(p,
                                                                 BasicNetwork.TagEndTraining);
                    flat.HasContext = EncogFileSection.ParseBoolean(p,
                                                                    BasicNetwork.TagHasContext);
                    flat.InputCount = EncogFileSection.ParseInt(p,
                                                                PersistConst.InputCount);
                    flat.LayerCounts = EncogFileSection.ParseIntArray(p,
                                                                      BasicNetwork.TagLayerCounts);
                    flat.LayerFeedCounts = EncogFileSection.ParseIntArray(p,
                                                                          BasicNetwork.TagLayerFeedCounts);
                    flat.LayerContextCount = EncogFileSection.ParseIntArray(p, BasicNetwork.TagLayerContextCount);
                    flat.LayerIndex        = EncogFileSection.ParseIntArray(p,
                                                                            BasicNetwork.TagLayerIndex);
                    flat.LayerOutput = section.ParseDoubleArray(p,
                                                                PersistConst.Output);
                    flat.LayerSums   = new double[flat.LayerOutput.Length];
                    flat.OutputCount = EncogFileSection.ParseInt(p, PersistConst.OutputCount);
                    flat.WeightIndex = EncogFileSection.ParseIntArray(p,
                                                                      BasicNetwork.TagWeightIndex);
                    flat.Weights = section.ParseDoubleArray(p,
                                                            PersistConst.Weights);
                    flat.BiasActivation = section.ParseDoubleArray(p, BasicNetwork.TagBiasActivation);
                }
                else if (section.SectionName.Equals("RBF-NETWORK") &&
                         section.SubSectionName.Equals("ACTIVATION"))
                {
                    int index = 0;

                    flat.ActivationFunctions = new IActivationFunction[flat.LayerCounts.Length];


                    foreach (String line  in  section.Lines)
                    {
                        IActivationFunction af;
                        IList <String>      cols = EncogFileSection
                                                   .SplitColumns(line);
                        String name = ReflectionUtil.AfPath
                                      + cols[0];
                        try
                        {
                            af = (IActivationFunction)ReflectionUtil.LoadObject(name);
                        }
                        catch (Exception e)
                        {
                            throw new PersistError(e);
                        }
                        for (int i = 0; i < af.ParamNames.Length; i++)
                        {
                            af.Params[i] = CSVFormat.EgFormat.Parse(cols[i + 1]);
                        }

                        flat.ActivationFunctions[index++] = af;
                    }
                }
                else if (section.SectionName.Equals("RBF-NETWORK") &&
                         section.SubSectionName.Equals("RBF"))
                {
                    int index = 0;

                    int hiddenCount = flat.LayerCounts[1];
                    int inputCount  = flat.LayerCounts[2];

                    flat.RBF = new IRadialBasisFunction[hiddenCount];


                    foreach (String line  in  section.Lines)
                    {
                        IRadialBasisFunction rbf;
                        IList <String>       cols = EncogFileSection
                                                    .SplitColumns(line);
                        String name = ReflectionUtil.RBFPath + cols[0];
                        try
                        {
                            rbf = (IRadialBasisFunction)ReflectionUtil.LoadObject(name);
                        }
                        catch (TypeLoadException ex)
                        {
                            throw new PersistError(ex);
                        }
                        catch (TargetException ex)
                        {
                            throw new PersistError(ex);
                        }
                        catch (MemberAccessException ex)
                        {
                            throw new PersistError(ex);
                        }

                        rbf.Width   = CSVFormat.EgFormat.Parse(cols[1]);
                        rbf.Peak    = CSVFormat.EgFormat.Parse(cols[2]);
                        rbf.Centers = new double[inputCount];

                        for (int i = 0; i < inputCount; i++)
                        {
                            rbf.Centers[i] = CSVFormat.EgFormat.Parse(cols[i + 3]);
                        }

                        flat.RBF[index++] = rbf;
                    }
                }
            }

            return(result);
        }
        /// <inheritdoc/>
        public Object Read(Stream istream)
        {
            int states = 0;

            int[]    items;
            double[] pi = null;
            Matrix   transitionProbability             = null;
            IDictionary <String, String> properties    = null;
            IList <IStateDistribution>   distributions = new List <IStateDistribution>();

            EncogReadHelper  reader = new EncogReadHelper(istream);
            EncogFileSection section;

            while ((section = reader.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("HMM") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    properties = section.ParseParams();
                }
                if (section.SectionName.Equals("HMM") &&
                    section.SubSectionName.Equals("CONFIG"))
                {
                    IDictionary <String, String> p = section.ParseParams();

                    states = EncogFileSection.ParseInt(p, HiddenMarkovModel.TAG_STATES);

                    if (p.ContainsKey(HiddenMarkovModel.TAG_ITEMS))
                    {
                        items = EncogFileSection.ParseIntArray(p, HiddenMarkovModel.TAG_ITEMS);
                    }
                    pi = section.ParseDoubleArray(p, HiddenMarkovModel.TAG_PI);
                    transitionProbability = EncogFileSection.ParseMatrix(p, HiddenMarkovModel.TAG_TRANSITION);
                }
                else if (section.SectionName.Equals("HMM") &&
                         section.SubSectionName.StartsWith("DISTRIBUTION-"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    String t = p[HiddenMarkovModel.TAG_DIST_TYPE];
                    if ("ContinousDistribution".Equals(t))
                    {
                        double[] mean = section.ParseDoubleArray(p, HiddenMarkovModel.TAG_MEAN);
                        Matrix   cova = EncogFileSection.ParseMatrix(p, HiddenMarkovModel.TAG_COVARIANCE);
                        ContinousDistribution dist = new ContinousDistribution(mean, cova.Data);
                        distributions.Add(dist);
                    }
                    else if ("DiscreteDistribution".Equals(t))
                    {
                        Matrix prob = EncogFileSection.ParseMatrix(p, HiddenMarkovModel.TAG_PROBABILITIES);
                        DiscreteDistribution dist = new DiscreteDistribution(prob.Data);
                        distributions.Add(dist);
                    }
                }
            }

            HiddenMarkovModel result = new HiddenMarkovModel(states);

            EngineArray.PutAll(properties, result.Properties);
            result.TransitionProbability = transitionProbability.Data;
            result.Pi = pi;
            int index = 0;

            foreach (IStateDistribution dist in distributions)
            {
                result.StateDistributions[index++] = dist;
            }


            return(result);
        }
Beispiel #10
0
        /// <inheritdoc />
        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 <string, string> 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 <String> 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 <string> 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 <String> 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);
        }
 /// <summary>
 ///     Perform a revert.
 /// </summary>
 /// <param name="revertedData">The source data to revert from.</param>
 public void PerformRevert(IDictionary <String, String> revertedData)
 {
     _data.Clear();
     EngineArray.PutAll(revertedData, _data);
 }
        /// <summary>
        /// parse a layer.
        /// </summary>
        ///
        /// <param name="line">The line to parse.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The parsed ArchitectureLayer.</returns>
        public static ArchitectureLayer ParseLayer(String line,
                                                   int defaultValue)
        {
            var layer = new ArchitectureLayer();

            String check = line.Trim().ToUpper();

            // first check for bias
            if (check.EndsWith(":B"))
            {
                check      = check.Substring(0, (check.Length - 2) - (0));
                layer.Bias = true;
            }

            // see if simple number
            try
            {
                layer.Count = Int32.Parse(check);
                if (layer.Count < 0)
                {
                    throw new EncogError("Count cannot be less than zero.");
                }
            }
            catch (FormatException f)
            {
                EncogLogging.Log(f);
            }

            // see if it is a default
            if ("?".Equals(check))
            {
                if (defaultValue < 0)
                {
                    throw new EncogError("Default (?) in an invalid location.");
                }
                layer.Count       = defaultValue;
                layer.UsedDefault = true;
                return(layer);
            }

            // single item, no function
            int startIndex = check.IndexOf('(');
            int endIndex   = check.LastIndexOf(')');

            if (startIndex == -1)
            {
                layer.Name = check;
                return(layer);
            }

            // function
            if (endIndex == -1)
            {
                throw new EncogError("Illegal parentheses.");
            }

            layer.Name = check.Substring(0, (startIndex) - (0)).Trim();

            String paramStr = check.Substring(startIndex + 1, (endIndex) - (startIndex + 1));
            IDictionary <String, String> paras = ParseParams(paramStr);

            EngineArray.PutAll(paras, layer.Params);
            return(layer);
        }
Beispiel #13
0
        /// <summary>
        /// Read an object.
        /// </summary>
        public Object Read(Stream mask0)
        {
            var ins0 = new EncogReadHelper(mask0);
            EncogFileSection section;
            var samples = new BasicMLDataSet();
            IDictionary <String, String> networkParams = null;
            PNNKernelType kernel      = default(PNNKernelType) /* was: null */;
            PNNOutputMode outmodel    = default(PNNOutputMode) /* was: null */;
            int           inputCount  = 0;
            int           outputCount = 0;
            double        error       = 0;

            double[] sigma = null;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    networkParams = section.ParseParams();
                }
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("NETWORK"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    inputCount = EncogFileSection.ParseInt(paras,
                                                           PersistConst.InputCount);
                    outputCount = EncogFileSection.ParseInt(paras,
                                                            PersistConst.OutputCount);
                    kernel   = StringToKernel(paras[PersistConst.Kernel]);
                    outmodel = StringToOutputMode(paras[PropertyOutputMode]);
                    error    = EncogFileSection
                               .ParseDouble(paras, PersistConst.Error);
                    sigma = section.ParseDoubleArray(paras, PersistConst.Sigma);
                }
                if (section.SectionName.Equals("PNN") &&
                    section.SubSectionName.Equals("SAMPLES"))
                {
                    foreach (String line in section.Lines)
                    {
                        IList <String> cols = EncogFileSection
                                              .SplitColumns(line);
                        int index     = 0;
                        var inputData = new BasicMLData(inputCount);
                        for (int i = 0; i < inputCount; i++)
                        {
                            inputData[i] =
                                CSVFormat.EgFormat.Parse(cols[index++]);
                        }
                        var idealData = new BasicMLData(inputCount);

                        idealData[0] = CSVFormat.EgFormat.Parse(cols[index++]);

                        IMLDataPair pair = new BasicMLDataPair(inputData,
                                                               idealData);
                        samples.Add(pair);
                    }
                }
            }

            var result = new BasicPNN(kernel, outmodel, inputCount,
                                      outputCount);

            if (networkParams != null)
            {
                EngineArray.PutAll(networkParams, result.Properties);
            }
            result.Samples = samples;
            result.Error   = error;
            if (sigma != null)
            {
                EngineArray.ArrayCopy(sigma, result.Sigma);
            }

            return(result);
        }
        /// <inheritdoc/>
        public Object Read(Stream mask0)
        {
            var result = new SupportVectorMachine();
            var ins0   = new EncogReadHelper(mask0);
            EncogFileSection section;

            while ((section = ins0.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("SVM") &&
                    section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
                if (section.SectionName.Equals("SVM") &&
                    section.SubSectionName.Equals("SVM-PARAM"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    result.InputCount = EncogFileSection.ParseInt(p,
                                                                  PersistConst.InputCount);
                    result.Params.C = EncogFileSection.ParseDouble(p,
                                                                   ParamC);
                    result.Params.cache_size = EncogFileSection.ParseDouble(
                        p, ParamCacheSize);
                    result.Params.coef0 = EncogFileSection.ParseDouble(p,
                                                                       ParamCoef0);
                    result.Params.degree = EncogFileSection.ParseDouble(p,
                                                                        ParamDegree);
                    result.Params.eps = EncogFileSection.ParseDouble(p,
                                                                     ParamEps);
                    result.Params.gamma = EncogFileSection.ParseDouble(p,
                                                                       ParamGamma);
                    result.Params.kernel_type = EncogFileSection.ParseInt(
                        p, ParamKernelType);
                    result.Params.nr_weight = EncogFileSection.ParseInt(
                        p, ParamNumWeight);
                    result.Params.nu = EncogFileSection.ParseDouble(p,
                                                                    ParamNu);
                    result.Params.p = EncogFileSection.ParseDouble(p,
                                                                   ParamP);
                    result.Params.probability = EncogFileSection.ParseInt(
                        p, ParamProbability);
                    result.Params.shrinking = EncogFileSection.ParseInt(
                        p, ParamShrinking);

                    /*result.Params.statIterations = Encog.Persist.EncogFileSection.ParseInt(
                     *                                  params_0, PersistSVM.PARAM_START_ITERATIONS);*/
                    result.Params.svm_type = EncogFileSection.ParseInt(p,
                                                                       ParamSVMType);
                    result.Params.weight = EncogFileSection.ParseDoubleArray(
                        p, ParamWeight);
                    result.Params.weight_label = EncogFileSection
                                                 .ParseIntArray(p, ParamWeightLabel);
                }
                else if (section.SectionName.Equals("SVM") &&
                         section.SubSectionName.Equals("SVM-MODEL"))
                {
                    try
                    {
#if !SILVERLIGHT
                        var rdr = new StringReader(
                            section.LinesAsString);
                        TextReader br    = rdr;
                        svm_model  model = svm.svm_load_model(rdr);
                        result.Model = model;
                        br.Close();
                        rdr.Close();
#endif
                    }
                    catch (IOException ex)
                    {
                        throw new PersistError(ex);
                    }
                }
            }

            return(result);
        }
Beispiel #15
0
        /// <inheritdoc/>
        public Object Read(Stream istream)
        {
            BayesianNetwork  result = new BayesianNetwork();
            EncogReadHelper  input  = new EncogReadHelper(istream);
            EncogFileSection section;
            String           queryType   = "";
            String           queryStr    = "";
            String           contentsStr = "";

            while ((section = input.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-PARAM"))
                {
                    IDictionary <String, String> p = section.ParseParams();
                    queryType   = p["queryType"];
                    queryStr    = p["query"];
                    contentsStr = p["contents"];
                }
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-TABLE"))
                {
                    result.Contents = contentsStr;

                    // first, define relationships (1st pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineRelationship(line);
                    }

                    result.FinalizeStructure();

                    // now define the probabilities (2nd pass)
                    foreach (String line in section.Lines)
                    {
                        result.DefineProbability(line);
                    }
                }
                if (section.SectionName.Equals("BAYES-NETWORK") &&
                    section.SubSectionName.Equals("BAYES-PROPERTIES"))
                {
                    IDictionary <String, String> paras = section.ParseParams();
                    EngineArray.PutAll(paras, result.Properties);
                }
            }

            // define query, if it exists
            if (queryType.Length > 0)
            {
                IBayesianQuery query = null;
                if (queryType.Equals("EnumerationQuery"))
                {
                    query = new EnumerationQuery(result);
                }
                else
                {
                    query = new SamplingQuery(result);
                }

                if (query != null && queryStr.Length > 0)
                {
                    result.Query = query;
                    result.DefineClassificationStructure(queryStr);
                }
            }

            return(result);
        }