/// <summary>
 /// Write a link.
 /// </summary>
 /// <param name="xout">The output file.</param>
 /// <param name="link">The link.</param>
 private static void WriteLink(EncogWriteHelper xout, NEATLink link)
 {
     xout.AddColumn((int)link.FromNeuron.NeuronID);
     xout.AddColumn((int)link.ToNeuron.NeuronID);
     xout.AddColumn(link.Recurrent);
     xout.AddColumn(link.Weight);
     xout.WriteLine();
 }
Esempio n. 2
0
        /// <inheritdoc/>
        public void Save(Stream os, Object obj)
        {
            var output = new EncogWriteHelper(os);
            var pop    = (NEATPopulation)obj;

            output.AddSection("NEAT-POPULATION");
            output.AddSubSection("CONFIG");
            output.WriteProperty(PersistConst.ActivationCycles, pop.ActivationCycles);

            if (pop.IsHyperNEAT)
            {
                output.WriteProperty(NEATPopulation.PropertyNEATActivation, TypeCppn);
            }
            else
            {
                IActivationFunction af = pop.ActivationFunctions.Contents[0].obj;
                output.WriteProperty(NEATPopulation.PropertyNEATActivation, af);
            }

            output.WriteProperty(PersistConst.InputCount, pop.InputCount);
            output.WriteProperty(PersistConst.OutputCount, pop.OutputCount);
            output.WriteProperty(NEATPopulation.PropertyCycles, pop.ActivationCycles);
            output.WriteProperty(NEATPopulation.PropertyPopulationSize, pop.PopulationSize);
            output.WriteProperty(NEATPopulation.PropertySurvivalRate, pop.SurvivalRate);
            output.AddSubSection("INNOVATIONS");
            if (pop.Innovations != null)
            {
                foreach (string key in pop.Innovations.Innovations.Keys)
                {
                    NEATInnovation innovation = pop.Innovations.Innovations[key];
                    output.AddColumn(key);
                    output.AddColumn(innovation.InnovationId);
                    output.AddColumn(innovation.NeuronId);
                    output.WriteLine();
                }
            }

            output.AddSubSection("SPECIES");

            // make sure the best species goes first
            ISpecies bestSpecies = pop.DetermineBestSpecies();

            if (bestSpecies != null)
            {
                SaveSpecies(output, bestSpecies);
            }

            // now write the other species, other than the best one
            foreach (ISpecies species in pop.Species)
            {
                if (species != bestSpecies)
                {
                    SaveSpecies(output, species);
                }
            }
            output.Flush();
        }
Esempio n. 3
0
        /// <summary>
        ///     Save segregate info.
        /// </summary>
        /// <param name="xout">The output file.</param>
        private void SaveSegregate(EncogWriteHelper xout)
        {
            SaveSubSection(xout, "SEGREGATE", "CONFIG");
            xout.AddSubSection("FILES");
            xout.AddColumn("file");
            xout.AddColumn("percent");
            xout.WriteLine();

            foreach (AnalystSegregateTarget target in _script.Segregate.SegregateTargets)
            {
                xout.AddColumn(target.File);
                xout.AddColumn(target.Percent);
                xout.WriteLine();
            }
        }
        /// <inheritdoc/>
        public void Save(Stream os, Object obj)
        {
            var         xout = new EncogWriteHelper(os);
            var         net  = (BasicNetwork)obj;
            FlatNetwork flat = net.Structure.Flat;

            xout.AddSection("BASIC");
            xout.AddSubSection("PARAMS");
            xout.AddProperties(net.Properties);
            xout.AddSubSection("NETWORK");

            xout.WriteProperty(BasicNetwork.TagBeginTraining,
                               flat.BeginTraining);
            xout.WriteProperty(BasicNetwork.TagConnectionLimit,
                               flat.ConnectionLimit);
            xout.WriteProperty(BasicNetwork.TagContextTargetOffset,
                               flat.ContextTargetOffset);
            xout.WriteProperty(BasicNetwork.TagContextTargetSize,
                               flat.ContextTargetSize);
            xout.WriteProperty(BasicNetwork.TagEndTraining, flat.EndTraining);
            xout.WriteProperty(BasicNetwork.TagHasContext, flat.HasContext);
            xout.WriteProperty(PersistConst.InputCount, flat.InputCount);
            xout.WriteProperty(BasicNetwork.TagLayerCounts, flat.LayerCounts);
            xout.WriteProperty(BasicNetwork.TagLayerFeedCounts,
                               flat.LayerFeedCounts);
            xout.WriteProperty(BasicNetwork.TagLayerContextCount,
                               flat.LayerContextCount);
            xout.WriteProperty(BasicNetwork.TagLayerIndex, flat.LayerIndex);
            xout.WriteProperty(PersistConst.Output, flat.LayerOutput);
            xout.WriteProperty(PersistConst.OutputCount, flat.OutputCount);
            xout.WriteProperty(BasicNetwork.TagWeightIndex, flat.WeightIndex);
            xout.WriteProperty(PersistConst.Weights, flat.Weights);
            xout.WriteProperty(BasicNetwork.TagBiasActivation,
                               flat.BiasActivation);
            xout.AddSubSection("ACTIVATION");

            foreach (IActivationFunction af  in  flat.ActivationFunctions)
            {
                xout.AddColumn(af.GetType().Name);
                for (int i = 0; i < af.Params.Length; i++)
                {
                    xout.AddColumn(af.Params[i]);
                }
                xout.WriteLine();
            }

            xout.Flush();
        }
        /// <summary>
        /// Save the object.
        /// </summary>
        /// <param name="os">The output stream.</param>
        /// <param name="obj">The object to save.</param>
        public virtual void Save(Stream os, Object obj)
        {
            var xout = new EncogWriteHelper(os);
            var neat = (NEATNetwork)obj;

            xout.AddSection("NEAT");
            xout.AddSubSection("PARAMS");
            xout.AddProperties(neat.Properties);
            xout.AddSubSection("NETWORK");

            xout.WriteProperty(PersistConst.InputCount, neat.InputCount);
            xout.WriteProperty(PersistConst.OutputCount, neat.OutputCount);
            xout.WriteProperty(PersistConst.ActivationFunction,
                               neat.ActivationFunction);
            xout.WriteProperty(NEATPopulation.PropertyOutputActivation,
                               neat.OutputActivationFunction);
            xout.WriteProperty(PersistConst.Depth, neat.NetworkDepth);
            xout.WriteProperty(PersistConst.Snapshot, neat.Snapshot);

            xout.AddSubSection("NEURONS");

            foreach (NEATNeuron neatNeuron  in  neat.Neurons)
            {
                xout.AddColumn((int)neatNeuron.NeuronID);
                xout.AddColumn(PersistNEATPopulation.NeuronTypeToString(neatNeuron.NeuronType));
                xout.AddColumn(neatNeuron.ActivationResponse);
                xout.AddColumn(neatNeuron.SplitX);
                xout.AddColumn(neatNeuron.SplitY);
                xout.WriteLine();
            }

            xout.AddSubSection("LINKS");

            foreach (NEATNeuron neatNeuron  in  neat.Neurons)
            {
                foreach (NEATLink link  in  neatNeuron.OutputboundLinks)
                {
                    WriteLink(xout, link);
                }
            }

            xout.Flush();
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public void Save(Stream os, Object obj)
        {
            var xout = new EncogWriteHelper(os);
            var net  = (RBFNetwork)obj;
            var flat = (FlatNetworkRBF)net.Flat;

            xout.AddSection("RBF-NETWORK");
            xout.AddSubSection("PARAMS");
            xout.AddProperties(net.Properties);
            xout.AddSubSection("NETWORK");
            xout.WriteProperty(BasicNetwork.TagBeginTraining,
                               flat.BeginTraining);
            xout.WriteProperty(BasicNetwork.TagConnectionLimit,
                               flat.ConnectionLimit);
            xout.WriteProperty(BasicNetwork.TagContextTargetOffset,
                               flat.ContextTargetOffset);
            xout.WriteProperty(BasicNetwork.TagContextTargetSize,
                               flat.ContextTargetSize);
            xout.WriteProperty(BasicNetwork.TagEndTraining, flat.EndTraining);
            xout.WriteProperty(BasicNetwork.TagHasContext, flat.HasContext);
            xout.WriteProperty(PersistConst.InputCount, flat.InputCount);
            xout.WriteProperty(BasicNetwork.TagLayerCounts, flat.LayerCounts);
            xout.WriteProperty(BasicNetwork.TagLayerFeedCounts,
                               flat.LayerFeedCounts);
            xout.WriteProperty(BasicNetwork.TagLayerContextCount,
                               flat.LayerContextCount);
            xout.WriteProperty(BasicNetwork.TagLayerIndex, flat.LayerIndex);
            xout.WriteProperty(PersistConst.Output, flat.LayerOutput);
            xout.WriteProperty(PersistConst.OutputCount, flat.OutputCount);
            xout.WriteProperty(BasicNetwork.TagWeightIndex, flat.WeightIndex);
            xout.WriteProperty(PersistConst.Weights, flat.Weights);
            xout.WriteProperty(BasicNetwork.TagBiasActivation,
                               flat.BiasActivation);
            xout.AddSubSection("ACTIVATION");

            foreach (IActivationFunction af  in  flat.ActivationFunctions)
            {
                xout.AddColumn(af.GetType().Name);
                foreach (double t in af.Params)
                {
                    xout.AddColumn(t);
                }
                xout.WriteLine();
            }
            xout.AddSubSection("RBF");

            foreach (IRadialBasisFunction rbf  in  flat.RBF)
            {
                xout.AddColumn(rbf.GetType().Name);
                xout.AddColumn(rbf.Width);
                xout.AddColumn(rbf.Peak);
                foreach (double t in rbf.Centers)
                {
                    xout.AddColumn(t);
                }
                xout.WriteLine();
            }

            xout.Flush();
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        ///
        public void Save(Stream os, Object obj)
        {
            var xout = new EncogWriteHelper(os);
            var pnn  = (BasicPNN)obj;

            xout.AddSection("PNN");
            xout.AddSubSection("PARAMS");
            xout.AddProperties(pnn.Properties);
            xout.AddSubSection("NETWORK");

            xout.WriteProperty(PersistConst.Error, pnn.Error);
            xout.WriteProperty(PersistConst.InputCount, pnn.InputCount);
            xout.WriteProperty(PersistConst.Kernel,
                               KernelToString(pnn.Kernel));
            xout.WriteProperty(PersistConst.OutputCount, pnn.OutputCount);
            xout.WriteProperty(PropertyOutputMode,
                               OutputModeToString(pnn.OutputMode));
            xout.WriteProperty(PersistConst.Sigma, pnn.Sigma);

            xout.AddSubSection("SAMPLES");

            if (pnn.Samples != null)
            {
                foreach (IMLDataPair pair in pnn.Samples)
                {
                    for (int i = 0; i < pair.Input.Count; i++)
                    {
                        xout.AddColumn(pair.Input[i]);
                    }

                    for (int i = 0; i < pair.Ideal.Count; i++)
                    {
                        xout.AddColumn(pair.Ideal[i]);
                    }
                    xout.WriteLine();
                }
            }
            xout.Flush();
        }
Esempio n. 8
0
        /// <inheritdoc />
        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();
        }
Esempio n. 9
0
        /// <summary>
        /// Save the normalization data.
        /// </summary>
        ///
        /// <param name="xout">The output file.</param>
        private void SaveNormalize(EncogWriteHelper xout)
        {
            SaveSubSection(xout, "NORMALIZE", "CONFIG");

            xout.AddSubSection("RANGE");
            xout.AddColumn("name");
            xout.AddColumn("io");
            xout.AddColumn("timeSlice");
            xout.AddColumn("action");
            xout.AddColumn("high");
            xout.AddColumn("low");
            xout.WriteLine();

            foreach (AnalystField field  in  _script.Normalize.NormalizedFields)
            {
                xout.AddColumn(field.Name);
                xout.AddColumn(field.Input ? "input" : "output");
                xout.AddColumn(field.TimeSlice);
                switch (field.Action)
                {
                case NormalizationAction.Ignore:
                    xout.AddColumn("ignore");
                    break;

                case NormalizationAction.Normalize:
                    xout.AddColumn("range");
                    break;

                case NormalizationAction.PassThrough:
                    xout.AddColumn("pass");
                    break;

                case NormalizationAction.OneOf:
                    xout.AddColumn("oneof");
                    break;

                case NormalizationAction.Equilateral:
                    xout.AddColumn("equilateral");
                    break;

                case NormalizationAction.SingleField:
                    xout.AddColumn("single");
                    break;

                default:
                    throw new AnalystError("Unknown action: " + field.Action);
                }

                xout.AddColumn(field.NormalizedHigh);
                xout.AddColumn(field.NormalizedLow);
                xout.WriteLine();
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Save the data fields.
        /// </summary>
        ///
        /// <param name="xout">The output file.</param>
        private void SaveData(EncogWriteHelper xout)
        {
            SaveSubSection(xout, "DATA", "CONFIG");
            xout.AddSubSection("STATS");
            xout.AddColumn("name");
            xout.AddColumn("isclass");
            xout.AddColumn("iscomplete");
            xout.AddColumn("isint");
            xout.AddColumn("isreal");
            xout.AddColumn("amax");
            xout.AddColumn("amin");
            xout.AddColumn("mean");
            xout.AddColumn("sdev");
            xout.WriteLine();


            foreach (DataField field  in  _script.Fields)
            {
                xout.AddColumn(field.Name);
                xout.AddColumn(field.Class);
                xout.AddColumn(field.Complete);
                xout.AddColumn(field.Integer);
                xout.AddColumn(field.Real);
                xout.AddColumn(field.Max);
                xout.AddColumn(field.Min);
                xout.AddColumn(field.Mean);
                xout.AddColumn(field.StandardDeviation);
                xout.WriteLine();
            }
            xout.Flush();

            xout.AddSubSection("CLASSES");
            xout.AddColumn("field");
            xout.AddColumn("code");
            xout.AddColumn("name");
            xout.WriteLine();


            foreach (DataField field  in  _script.Fields)
            {
                if (field.Class)
                {
                    foreach (AnalystClassItem col  in  field.ClassMembers)
                    {
                        xout.AddColumn(field.Name);
                        xout.AddColumn(col.Code);
                        xout.AddColumn(col.Name);
                        xout.AddColumn(col.Count);
                        xout.WriteLine();
                    }
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Save the object.
        /// </summary>
        /// <param name="os">The stream to write to.</param>
        /// <param name="obj">The object to save.</param>
        public virtual void Save(Stream os, Object obj)
        {
            var xout = new EncogWriteHelper(os);
            var pop  = (NEATPopulation)obj;

            xout.AddSection("NEAT-POPULATION");
            xout.AddSubSection("CONFIG");
            xout.WriteProperty(PersistConst.Snapshot, pop.Snapshot);
            xout.WriteProperty(NEATPopulation.PropertyOutputActivation,
                               pop.OutputActivationFunction);
            xout.WriteProperty(NEATPopulation.PropertyNEATActivation,
                               pop.NeatActivationFunction);
            xout.WriteProperty(PersistConst.InputCount, pop.InputCount);
            xout.WriteProperty(PersistConst.OutputCount, pop.OutputCount);
            xout.WriteProperty(PopulationConst.PropertyOldAgePenalty,
                               pop.OldAgePenalty);
            xout.WriteProperty(PopulationConst.PropertyOldAgeThreshold,
                               pop.OldAgeThreshold);
            xout.WriteProperty(PopulationConst.PropertyPopulationSize,
                               pop.PopulationSize);
            xout.WriteProperty(PopulationConst.PropertySurvivalRate,
                               pop.SurvivalRate);
            xout.WriteProperty(PopulationConst.PropertyYoungAgeThreshold,
                               pop.YoungBonusAgeThreshold);
            xout.WriteProperty(PopulationConst.PropertyYoungAgeBonus,
                               pop.YoungScoreBonus);
            xout.WriteProperty(PopulationConst.PropertyNextGenomeID, pop.GenomeIDGenerate.CurrentID);
            xout.WriteProperty(PopulationConst.PropertyNextInnovationID, pop.InnovationIDGenerate.CurrentID);
            xout.WriteProperty(PopulationConst.PropertyNextGeneID, pop.GeneIDGenerate.CurrentID);
            xout.WriteProperty(PopulationConst.PropertyNextSpeciesID, pop.SpeciesIDGenerate.CurrentID);
            xout.AddSubSection("INNOVATIONS");
            if (pop.Innovations != null)
            {
                foreach (IInnovation innovation  in  pop.Innovations.Innovations)
                {
                    var neatInnovation = (NEATInnovation)innovation;
                    xout.AddColumn(neatInnovation.InnovationID);
                    xout.AddColumn(InnovationTypeToString(neatInnovation.InnovationType));
                    xout.AddColumn(NeuronTypeToString(neatInnovation.NeuronType));
                    xout.AddColumn(neatInnovation.SplitX);
                    xout.AddColumn(neatInnovation.SplitY);
                    xout.AddColumn(neatInnovation.NeuronID);
                    xout.AddColumn(neatInnovation.FromNeuronID);
                    xout.AddColumn(neatInnovation.ToNeuronID);
                    xout.WriteLine();
                }
            }
            xout.AddSubSection("GENOMES");

            foreach (IGenome genome  in  pop.Genomes)
            {
                var neatGenome = (NEATGenome)genome;
                xout.AddColumn("g");
                xout.AddColumn(neatGenome.GenomeID);
                xout.AddColumn(neatGenome.SpeciesID);
                xout.AddColumn(neatGenome.AdjustedScore);
                xout.AddColumn(neatGenome.AmountToSpawn);
                xout.AddColumn(neatGenome.NetworkDepth);
                xout.AddColumn(neatGenome.Score);
                xout.WriteLine();


                foreach (IGene neuronGene  in  neatGenome.Neurons.Genes)
                {
                    var neatNeuronGene = (NEATNeuronGene)neuronGene;
                    xout.AddColumn("n");
                    xout.AddColumn(neatNeuronGene.Id);
                    xout.AddColumn(NeuronTypeToString(neatNeuronGene.NeuronType));
                    xout.AddColumn(neatNeuronGene.Enabled);
                    xout.AddColumn(neatNeuronGene.InnovationId);
                    xout.AddColumn(neatNeuronGene.ActivationResponse);
                    xout.AddColumn(neatNeuronGene.SplitX);
                    xout.AddColumn(neatNeuronGene.SplitY);
                    xout.WriteLine();
                }

                foreach (IGene linkGene  in  neatGenome.Links.Genes)
                {
                    var neatLinkGene = (NEATLinkGene)linkGene;
                    xout.AddColumn("l");
                    xout.AddColumn(neatLinkGene.Id);
                    xout.AddColumn(neatLinkGene.Enabled);
                    xout.AddColumn(neatLinkGene.Recurrent);
                    xout.AddColumn(neatLinkGene.FromNeuronID);
                    xout.AddColumn(neatLinkGene.ToNeuronID);
                    xout.AddColumn(neatLinkGene.Weight);
                    xout.AddColumn(neatLinkGene.InnovationId);
                    xout.WriteLine();
                }
            }
            xout.AddSubSection("SPECIES");

            foreach (ISpecies species  in  pop.Species)
            {
                xout.AddColumn(species.SpeciesID);
                xout.AddColumn(species.Age);
                xout.AddColumn(species.BestScore);
                xout.AddColumn(species.GensNoImprovement);
                xout.AddColumn(species.NumToSpawn);
                xout.AddColumn(species.SpawnsRequired);
                xout.AddColumn(species.Leader.GenomeID);
                xout.WriteLine();
            }
            xout.Flush();
        }
Esempio n. 12
0
        private void SaveSpecies(EncogWriteHelper output, ISpecies species)
        {
            output.AddColumn("s");
            output.AddColumn(species.Age);
            output.AddColumn(species.BestScore);
            output.AddColumn(species.GensNoImprovement);
            output.WriteLine();

            foreach (IGenome genome in species.Members)
            {
                var neatGenome = (NEATGenome)genome;
                output.AddColumn("g");
                output.AddColumn(neatGenome.AdjustedScore);
                output.AddColumn(neatGenome.Score);
                output.AddColumn(neatGenome.BirthGeneration);
                output.WriteLine();

                foreach (NEATNeuronGene neatNeuronGene in neatGenome.NeuronsChromosome)
                {
                    output.AddColumn("n");
                    output.AddColumn(neatNeuronGene.Id);
                    output.AddColumn(neatNeuronGene.ActivationFunction);
                    output.AddColumn(NeuronTypeToString(neatNeuronGene.NeuronType));
                    output.AddColumn(neatNeuronGene.InnovationId);
                    output.WriteLine();
                }
                foreach (NEATLinkGene neatLinkGene in neatGenome.LinksChromosome)
                {
                    output.AddColumn("l");
                    output.AddColumn(neatLinkGene.Id);
                    output.AddColumn(neatLinkGene.Enabled);
                    output.AddColumn(neatLinkGene.FromNeuronId);
                    output.AddColumn(neatLinkGene.ToNeuronId);
                    output.AddColumn(neatLinkGene.Weight);
                    output.AddColumn(neatLinkGene.InnovationId);
                    output.WriteLine();
                }
            }
        }