This class contains some utilities for persisting objects.
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="xmlOut"></param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlOut)
        {
            PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_TRAINING,
                                           xmlOut, obj, true);
            INeuralDataSet set     = (INeuralDataSet)obj;
            StringBuilder  builder = new StringBuilder();

            foreach (INeuralDataPair pair in set)
            {
                xmlOut.BeginTag(BasicNeuralDataSetPersistor.TAG_ITEM);

                NumberList.ToList(CSVFormat.EG_FORMAT, builder, pair.Input.Data);
                xmlOut.AddProperty(BasicNeuralDataSetPersistor.TAG_INPUT, builder
                                   .ToString());

                if (pair.Ideal != null)
                {
                    NumberList.ToList(CSVFormat.EG_FORMAT, builder, pair.Ideal.Data);
                    xmlOut.AddProperty(BasicNeuralDataSetPersistor.TAG_IDEAL, builder
                                       .ToString());
                }
                xmlOut.EndTag();
            }
            xmlOut.EndTag();
        }
Esempio n. 2
0
 /// <summary>
 /// Save the specified Encog object to an XML writer.
 /// </summary>
 /// <param name="obj">The object to save.</param>
 /// <param name="xmlOut">The XML writer to save to.</param>
 public void Save(IEncogPersistedObject obj, WriteXML xmlOut)
 {
     PersistorUtil.BeginEncogObject(
         EncogPersistedCollection.TYPE_WEIGHTLESS_SYNAPSE, xmlOut, obj,
         false);
     xmlOut.EndTag();
 }
        /// <summary>
        /// Save the specified Encog object to an XML writer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlOut">The XML writer to save to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlOut)
        {
            PersistorUtil.BeginEncogObject(
                EncogPersistedCollection.TYPE_CONTEXT_LAYER, xmlOut, obj, false);
            ContextLayer layer = (ContextLayer)obj;

            xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_NEURONS, layer
                               .NeuronCount);
            xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_X, layer.X);
            xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_Y, layer.Y);

            if (layer.HasBias)
            {
                StringBuilder result = new StringBuilder();
                NumberList.ToList(CSVFormat.EG_FORMAT, result, layer.BiasWeights);
                xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_THRESHOLD, result
                                   .ToString());
            }

            StringBuilder ctx = new StringBuilder();

            NumberList.ToList(CSVFormat.EG_FORMAT, ctx, layer.Context.Data);
            xmlOut.AddProperty(PROPERTY_CONTEXT, ctx.ToString());


            xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_BIAS_ACTIVATION, layer.BiasActivation);
            BasicLayerPersistor.SaveActivationFunction(layer.ActivationFunction, xmlOut);

            xmlOut.EndTag();
        }
        /// <summary>
        /// Process any synapses that should be loaded.
        /// </summary>
        /// <param name="xmlIn">The XML reader.</param>
        private void HandleSynapses(ReadXML xmlIn)
        {
            String end = xmlIn.LastTag.Name;

            while (xmlIn.ReadToTag())
            {
                if (xmlIn.IsIt(BasicNetworkPersistor.TAG_SYNAPSE, true))
                {
                    int from = xmlIn.LastTag.GetAttributeInt(
                        BasicNetworkPersistor.ATTRIBUTE_FROM);
                    int to = xmlIn.LastTag.GetAttributeInt(
                        BasicNetworkPersistor.ATTRIBUTE_TO);
                    xmlIn.ReadToTag();
                    IPersistor persistor = PersistorUtil.CreatePersistor(xmlIn
                                                                         .LastTag.Name);
                    ISynapse synapse = (ISynapse)persistor.Load(xmlIn);
                    synapse.FromLayer = this.index2layer[from];
                    synapse.ToLayer   = this.index2layer[to];
                    synapse.FromLayer.AddSynapse(synapse);
                }
                if (xmlIn.IsIt(end, false))
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Save the specified Encog object to an XML writer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlout">The XML writer to save to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlout)
        {
            PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_TEXT, xmlout,
                                           obj, true);
            TextData text = (TextData)obj;

            xmlout.AddCDATA(text.Text);
            xmlout.EndTag();
        }
        /// <summary>
        /// Save the object.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlout">The XML output object.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlout)
        {
            PersistorUtil.BeginEncogObject(
                EncogPersistedCollection.TYPE_TRAINING_CONTINUATION, xmlout, obj,
                true);
            this.current = (TrainingContinuation)obj;

            xmlout.BeginTag(TrainingContinuationPersistor.TAG_ITEMS);
            SaveItems(xmlout);
            xmlout.EndTag();

            xmlout.EndTag();
        }
        /// <summary>
        /// Save the specified Encog object to an XML writer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlOut">The XML writer to save to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlOut)
        {
            PersistorUtil
            .BeginEncogObject(
                EncogPersistedCollection.TYPE_WEIGHTED_SYNAPSE, xmlOut,
                obj, false);
            WeightedSynapse synapse = (WeightedSynapse)obj;

            xmlOut.BeginTag(WeightedSynapsePersistor.TAG_WEIGHTS);
            PersistorUtil.SaveMatrix(synapse.WeightMatrix, xmlOut);
            xmlOut.EndTag();

            xmlOut.EndTag();
        }
        /// <summary>
        /// Handle any layers that should be loaded.
        /// </summary>
        /// <param name="xmlIn">The XML reader.</param>
        private void HandleLayers(ReadXML xmlIn)
        {
            String end = xmlIn.LastTag.Name;

            while (xmlIn.ReadToTag())
            {
                if (xmlIn.IsIt(BasicNetworkPersistor.TAG_LAYER, true))
                {
                    int num = xmlIn.LastTag.GetAttributeInt(
                        BasicNetworkPersistor.ATTRIBUTE_ID);
                    String type = xmlIn.LastTag.GetAttributeValue(
                        BasicNetworkPersistor.ATTRIBUTE_TYPE);
                    xmlIn.ReadToTag();
                    IPersistor persistor = PersistorUtil.CreatePersistor(xmlIn
                                                                         .LastTag.Name);
                    ILayer layer = (ILayer)persistor.Load(xmlIn);
                    this.index2layer[num] = layer;

                    // the type attribute is actually "legacy", but if its there
                    // then use it!
                    if (type != null)
                    {
                        if (type.Equals(BasicNetworkPersistor.ATTRIBUTE_TYPE_INPUT))
                        {
                            this.currentNetwork.TagLayer(BasicNetwork.TAG_INPUT,
                                                         layer);
                        }
                        else if (type
                                 .Equals(BasicNetworkPersistor.ATTRIBUTE_TYPE_OUTPUT))
                        {
                            this.currentNetwork.TagLayer(BasicNetwork.TAG_OUTPUT,
                                                         layer);
                        }
                        else if (type
                                 .Equals(BasicNetworkPersistor.ATTRIBUTE_TYPE_BOTH))
                        {
                            this.currentNetwork.TagLayer(BasicNetwork.TAG_INPUT,
                                                         layer);
                            this.currentNetwork.TagLayer(BasicNetwork.TAG_OUTPUT,
                                                         layer);
                        }
                    }
                    // end of legacy processing
                }
                if (xmlIn.IsIt(end, false))
                {
                    break;
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Save a RBF layer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlout">XML stream to write to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlout)
        {
            PersistorUtil.BeginEncogObject(
                EncogPersistedCollection.TYPE_RADIAL_BASIS_LAYER, xmlout, obj,
                false);
            RadialBasisFunctionLayer layer = (RadialBasisFunctionLayer)obj;

            xmlout.AddProperty(BasicLayerPersistor.PROPERTY_NEURONS, layer.NeuronCount);
            xmlout.AddProperty(BasicLayerPersistor.PROPERTY_X, layer.X);
            xmlout.AddProperty(BasicLayerPersistor.PROPERTY_Y, layer.Y);

            SaveRBF(xmlout, layer);

            xmlout.EndTag();
        }
Esempio n. 10
0
        /// <summary>
        /// Save a SVMNetwork.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlout">Where to save it to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlout)
        {
            PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_SVM, xmlout,
                                           obj, true);
            SVMNetwork net = (SVMNetwork)obj;

            xmlout.AddProperty(SVMNetworkPersistor.TAG_INPUT, net.InputCount);
            xmlout.AddProperty(SVMNetworkPersistor.TAG_OUTPUT, net.OutputCount);
            xmlout.BeginTag(SVMNetworkPersistor.TAG_MODELS);
            for (int i = 0; i < net.Models.Length; i++)
            {
                SaveModel(xmlout, net.Models[i]);
            }
            xmlout.EndTag();
            xmlout.EndTag();
        }
Esempio n. 11
0
        /// <summary>
        /// Save the specified Encog object to an XML writer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlout">The XML writer to save to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlout)
        {
            PropertyData pData = (PropertyData)obj;

            PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_PROPERTY,
                                           xmlout, obj, true);
            xmlout.BeginTag(PropertyDataPersistor.TAG_PROPERTIES);
            foreach (String key in pData.Data.Keys)
            {
                xmlout.AddAttribute(PropertyDataPersistor.ATTRIBUTE_NAME, key);
                xmlout.AddAttribute(PropertyDataPersistor.ATTRIBUTE_VALUE, pData
                                    [key]);
                xmlout.BeginTag(PropertyDataPersistor.TAG_PROPERTY);
                xmlout.EndTag();
            }
            xmlout.EndTag();
            xmlout.EndTag();
        }
        /// <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)
        {
            WeightedSynapse synapse = new WeightedSynapse();

            String end = xmlIn.LastTag.Name;

            while (xmlIn.ReadToTag())
            {
                if (xmlIn.IsIt(WeightedSynapsePersistor.TAG_WEIGHTS, true))
                {
                    xmlIn.ReadToTag();
                    synapse.WeightMatrix = PersistorUtil.LoadMatrix(xmlIn);
                }
                if (xmlIn.IsIt(end, false))
                {
                    break;
                }
            }

            return(synapse);
        }
        /// <summary>
        /// Save the specified Encog object to an XML writer.
        /// </summary>
        /// <param name="obj">The object to save.</param>
        /// <param name="xmlOut">The XML writer to save to.</param>
        public void Save(IEncogPersistedObject obj, WriteXML xmlOut)
        {
            PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_BASIC_NET,
                                           xmlOut, obj, true);
            this.currentNetwork = (BasicNetwork)obj;

            this.currentNetwork.Structure.FinalizeStructure();

            // save the layers
            xmlOut.BeginTag(BasicNetworkPersistor.TAG_LAYERS);
            SaveLayers(xmlOut);
            xmlOut.EndTag();

            // save the structure of these layers
            xmlOut.BeginTag(BasicNetworkPersistor.TAG_SYNAPSES);
            SaveSynapses(xmlOut);
            xmlOut.EndTag();

            SaveProperties(xmlOut);
            SaveTags(xmlOut);
            SaveLogic(xmlOut);

            xmlOut.EndTag();
        }