Ejemplo n.º 1
0
            /// <summary>
            /// See the base.
            /// </summary>
            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return(false);
                }
                ReservoirInstanceDefinition cmpSettings = obj as ReservoirInstanceDefinition;

                if (InstanceID != cmpSettings.InstanceID ||
                    InstanceName != cmpSettings.InstanceName ||
                    !Equals(Settings, cmpSettings.Settings) ||
                    NPInputFieldIdxCollection.Count != cmpSettings.NPInputFieldIdxCollection.Count ||
                    !NPInputFieldIdxCollection.ToArray().ContainsEqualValues(cmpSettings.NPInputFieldIdxCollection.ToArray()) ||
                    InputConnectionCollection.Count != cmpSettings.InputConnectionCollection.Count ||
                    !Equals(PredictorsCfg, cmpSettings.PredictorsCfg)
                    )
                {
                    return(false);
                }
                for (int i = 0; i < InputConnectionCollection.Count; i++)
                {
                    if (!Equals(InputConnectionCollection[i], cmpSettings.InputConnectionCollection[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// See the base.
            /// </summary>
            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return(false);
                }
                ReservoirInstanceDefinition cmpSettings = obj as ReservoirInstanceDefinition;

                if (InstanceName != cmpSettings.InstanceName ||
                    !Equals(Settings, cmpSettings.Settings) ||
                    AugmentedStates != cmpSettings.AugmentedStates ||
                    SMInputFieldIdxCollection.Count != cmpSettings.SMInputFieldIdxCollection.Count ||
                    !SMInputFieldIdxCollection.ToArray().ContainsEqualValues(cmpSettings.SMInputFieldIdxCollection.ToArray()) ||
                    InputConnectionCollection.Count != cmpSettings.InputConnectionCollection.Count
                    )
                {
                    return(false);
                }
                for (int i = 0; i < InputConnectionCollection.Count; i++)
                {
                    if (!Equals(InputConnectionCollection[i], cmpSettings.InputConnectionCollection[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
Ejemplo n.º 3
0
 /// <summary>
 /// The deep copy constructor.
 /// </summary>
 /// <param name="source">Source instance</param>
 public ReservoirInstanceDefinition(ReservoirInstanceDefinition source)
 {
     InputFieldMappingCollection = new List <int>(source.InputFieldMappingCollection);
     InstanceName      = source.InstanceName;
     ReservoirSettings = source.ReservoirSettings.DeepClone();
     AugmentedStates   = source.AugmentedStates;
     return;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// The deep copy constructor.
 /// </summary>
 /// <param name="source">Source instance</param>
 public ReservoirInstanceDefinition(ReservoirInstanceDefinition source)
 {
     InstanceName              = source.InstanceName;
     Settings                  = source.Settings.DeepClone();
     AugmentedStates           = source.AugmentedStates;
     SMInputFieldIdxCollection = new List <int>(source.SMInputFieldIdxCollection);
     InputConnectionCollection = new List <InputConnection>(source.InputConnectionCollection.Count);
     foreach (InputConnection ifa in source.InputConnectionCollection)
     {
         InputConnectionCollection.Add(ifa.DeepClone());
     }
     return;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// The deep copy constructor.
 /// </summary>
 /// <param name="source">Source instance</param>
 public ReservoirInstanceDefinition(ReservoirInstanceDefinition source)
 {
     InstanceID   = source.InstanceID;
     InstanceName = source.InstanceName;
     Settings     = source.Settings.DeepClone();
     NPInputFieldIdxCollection = new List <int>(source.NPInputFieldIdxCollection);
     InputConnectionCollection = new List <InputConnection>(source.InputConnectionCollection.Count);
     foreach (InputConnection ifa in source.InputConnectionCollection)
     {
         InputConnectionCollection.Add(ifa.DeepClone());
     }
     PredictorsCfg = source.PredictorsCfg?.DeepClone();
     return;
 }
Ejemplo n.º 6
0
            /// <summary>
            /// See the base.
            /// </summary>
            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return(false);
                }
                ReservoirInstanceDefinition cmpSettings = obj as ReservoirInstanceDefinition;

                if (InputFieldMappingCollection.Count != cmpSettings.InputFieldMappingCollection.Count ||
                    InstanceName != cmpSettings.InstanceName ||
                    !Equals(ReservoirSettings, cmpSettings.ReservoirSettings) ||
                    AugmentedStates != cmpSettings.AugmentedStates ||
                    !InputFieldMappingCollection.ToArray().ContainsEqualValues(cmpSettings.InputFieldMappingCollection.ToArray())
                    )
                {
                    return(false);
                }
                return(true);
            }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates the instance and initializes it from given xml element.
        /// This is the preferred way to instantiate Neural Preprocessor settings.
        /// </summary>
        /// <param name="elem">
        /// Xml data containing the settings.
        /// Content of xml element is always validated against the xml schema.
        /// </param>
        public NeuralPreprocessorSettings(XElement elem)
        {
            //Validation
            ElemValidator validator     = new ElemValidator();
            Assembly      assemblyRCNet = Assembly.GetExecutingAssembly();

            validator.AddXsdFromResources(assemblyRCNet, "RCNet.Neural.Network.SM.Preprocessing.NeuralPreprocessorSettings.xsd");
            validator.AddXsdFromResources(assemblyRCNet, "RCNet.RCNetTypes.xsd");
            XElement neuralPreprocessorSettingsElem = validator.Validate(elem, "rootElem");

            //Parsing
            //Input
            InputConfig = new InputSettings(neuralPreprocessorSettingsElem.Descendants("input").First());
            //Collect available reservoir settings
            List <ReservoirSettings> availableResSettings = new List <ReservoirSettings>();
            XElement reservoirSettingsContainerElem       = neuralPreprocessorSettingsElem.Descendants("reservoirCfgContainer").First();

            foreach (XElement reservoirSettingsElem in reservoirSettingsContainerElem.Descendants("reservoirCfg"))
            {
                availableResSettings.Add(new ReservoirSettings(reservoirSettingsElem));
            }
            //Mapping of input fields to reservoir settings (future reservoir instance)
            ReservoirInstanceDefinitionCollection = new List <ReservoirInstanceDefinition>();
            XElement reservoirInstancesContainerElem = neuralPreprocessorSettingsElem.Descendants("reservoirInstanceContainer").First();
            int      reservoirInstanceID             = 0;
            int      numOfNeuronsInLargestReservoir  = 0;

            foreach (XElement reservoirInstanceElem in reservoirInstancesContainerElem.Descendants("reservoirInstance"))
            {
                ReservoirInstanceDefinition reservoirInstanceDefinition = new ReservoirInstanceDefinition
                {
                    InstanceID   = reservoirInstanceID,
                    InstanceName = reservoirInstanceElem.Attribute("name").Value,
                    //Select reservoir settings
                    Settings = (from settings in availableResSettings
                                where settings.SettingsName == reservoirInstanceElem.Attribute("cfg").Value
                                select settings).FirstOrDefault(),
                    PredictorsCfg = new HiddenNeuronPredictorsSettings(reservoirInstanceElem.Descendants("predictors").First())
                };
                if (reservoirInstanceDefinition.Settings == null)
                {
                    throw new Exception($"Reservoir settings '{reservoirInstanceElem.Attribute("cfg").Value}' was not found among available settings.");
                }
                //Update number of neurons in the largest reservoir
                int numOfReservoirNeurons = 0;
                foreach (PoolSettings ps in reservoirInstanceDefinition.Settings.PoolSettingsCollection)
                {
                    numOfReservoirNeurons += ps.Dim.Size;
                }
                if (numOfNeuronsInLargestReservoir < numOfReservoirNeurons)
                {
                    numOfNeuronsInLargestReservoir = numOfReservoirNeurons;
                }

                //Distinct input field names and corresponding indexes using by the reservoir instance
                List <string> resInpFieldNameCollection = new List <string>();
                foreach (XElement inputFieldConnectionElem in reservoirInstanceElem.Descendants("inputConnections").First().Descendants("inputConnection"))
                {
                    //Input field name
                    string inputFieldName = inputFieldConnectionElem.Attribute("fieldName").Value;
                    //Index in InputConfig
                    int inputFieldIdx = InputConfig.IndexOf(inputFieldName);
                    //Found?
                    if (inputFieldIdx < 0)
                    {
                        //Not found
                        throw new Exception($"Reservoir instance {reservoirInstanceDefinition.InstanceName}: field name {inputFieldName} is not defined among input fields.");
                    }
                    //Add distinct name to the collection
                    if (resInpFieldNameCollection.IndexOf(inputFieldName) < 0)
                    {
                        reservoirInstanceDefinition.NPInputFieldIdxCollection.Add(inputFieldIdx);
                        resInpFieldNameCollection.Add(inputFieldName);
                    }
                }

                //Connections of the reservoir's input fields to the pools
                foreach (XElement inputConnectionElem in reservoirInstanceElem.Descendants("inputConnections").First().Descendants("inputConnection"))
                {
                    //Input field
                    string inputFieldName = inputConnectionElem.Attribute("fieldName").Value;
                    //Index in resInpFieldNameCollection
                    int resInputFieldIdx = resInpFieldNameCollection.IndexOf(inputFieldName);
                    //Found?
                    if (resInputFieldIdx < 0)
                    {
                        //Not found
                        throw new Exception($"Reservoir instance {reservoirInstanceDefinition.InstanceName}: input field {inputFieldName} is not defined among Reservoir's input fields.");
                    }
                    //Target pool
                    string targetPoolName = inputConnectionElem.Attribute("poolName").Value;
                    int    targetPoolID   = -1;
                    //Find target pool ID (index)
                    for (int idx = 0; idx < reservoirInstanceDefinition.Settings.PoolSettingsCollection.Count; idx++)
                    {
                        if (reservoirInstanceDefinition.Settings.PoolSettingsCollection[idx].Name == targetPoolName)
                        {
                            targetPoolID = idx;
                            break;
                        }
                    }
                    //Found?
                    if (targetPoolID < 0)
                    {
                        //Not found
                        throw new Exception($"Reservoir instance {reservoirInstanceDefinition.InstanceName}: pool {targetPoolName} is not defined among Reservoir pools.");
                    }
                    //Density
                    double density = double.Parse(inputConnectionElem.Attribute("density").Value, CultureInfo.InvariantCulture);
                    //Input synapse settings
                    InputSynapseSettings synapseCfg = new InputSynapseSettings(inputConnectionElem.Descendants("synapse").First());
                    //Add new assignment
                    reservoirInstanceDefinition.InputConnectionCollection.Add(new ReservoirInstanceDefinition.InputConnection(resInputFieldIdx, targetPoolID, density, synapseCfg));
                }
                ReservoirInstanceDefinitionCollection.Add(reservoirInstanceDefinition);
                ++reservoirInstanceID;
            }
            //Finalize boot cycles if necessary
            if (InputConfig.BootCycles == AutomaticBootCycles && InputConfig.FeedingType == NeuralPreprocessor.InputFeedingType.Continuous)
            {
                InputConfig.BootCycles = numOfNeuronsInLargestReservoir;
            }

            return;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates the instance and initializes it from given xml element.
        /// This is the preferred way to instantiate State Machine settings.
        /// </summary>
        /// <param name="elem">
        /// Xml data containing State Machine settings.
        /// Content of xml element is always validated against the xml schema.
        /// </param>
        public StateMachineSettings(XElement elem)
        {
            //Validation
            ElemValidator validator     = new ElemValidator();
            Assembly      assemblyRCNet = Assembly.GetExecutingAssembly();

            validator.AddXsdFromResources(assemblyRCNet, "RCNet.Neural.Network.SM.StateMachineSettings.xsd");
            validator.AddXsdFromResources(assemblyRCNet, "RCNet.RCNetTypes.xsd");
            XElement stateMachineSettingsElem = validator.Validate(elem, "rootElem");

            //Parsing
            //Task type
            TaskType = CommonEnums.ParseTaskType(stateMachineSettingsElem.Attribute("taskType").Value);
            //Randomizer seek
            RandomizerSeek = int.Parse(stateMachineSettingsElem.Attribute("randomizerSeek").Value);
            //Input fields
            XElement inputFieldsElem = stateMachineSettingsElem.Descendants("inputFields").First();

            RouteInputToReadout = (inputFieldsElem.Attribute("routeToReadout") == null) ? false : bool.Parse(inputFieldsElem.Attribute("routeToReadout").Value);
            if (TaskType != CommonEnums.TaskType.Prediction && RouteInputToReadout)
            {
                throw new Exception("Routing input to readout is allowed for prediction task only.");
            }
            InputFieldNameCollection = new List <string>();
            foreach (XElement inputFieldElem in inputFieldsElem.Descendants("field"))
            {
                InputFieldNameCollection.Add(inputFieldElem.Attribute("name").Value);
            }
            //Collect available reservoir settings
            List <ReservoirSettings> availableResSettings = new List <ReservoirSettings>();
            XElement reservoirSettingsContainerElem       = stateMachineSettingsElem.Descendants("reservoirCfgContainer").First();

            foreach (XElement reservoirSettingsElem in reservoirSettingsContainerElem.Descendants("reservoirCfg"))
            {
                availableResSettings.Add(new ReservoirSettings(reservoirSettingsElem));
            }
            //Readout layer
            XElement readoutLayerElem = stateMachineSettingsElem.Descendants("readoutLayer").First();

            ReadoutLayerConfig = new ReadoutLayerSettings(readoutLayerElem);
            //Mapping of input fields to reservoir settings (future reservoir instance)
            ReservoirInstanceDefinitionCollection = new List <ReservoirInstanceDefinition>();
            XElement reservoirInstancesContainerElem = stateMachineSettingsElem.Descendants("reservoirInstanceContainer").First();

            foreach (XElement reservoirInstanceElem in reservoirInstancesContainerElem.Descendants("reservoirInstance"))
            {
                ReservoirInstanceDefinition newMap = new ReservoirInstanceDefinition();
                newMap.InstanceName    = reservoirInstanceElem.Attribute("name").Value;
                newMap.AugmentedStates = bool.Parse(reservoirInstanceElem.Attribute("augmentedStates").Value);
                //Select reservoir settings
                newMap.ReservoirSettings = (from settings in availableResSettings
                                            where settings.SettingsName == reservoirInstanceElem.Attribute("cfg").Value
                                            select settings).FirstOrDefault();
                if (newMap.ReservoirSettings == null)
                {
                    throw new Exception($"Reservoir settings '{reservoirInstanceElem.Attribute("cfg").Value}' was not found among available settings.");
                }
                //Associated State Machine input fields
                foreach (XElement inputFieldElem in reservoirInstanceElem.Descendants("inputFields").First().Descendants("field"))
                {
                    string inputFieldName = inputFieldElem.Attribute("name").Value;
                    //Index in InputFieldsNames
                    int inputFieldIdx = InputFieldNameCollection.IndexOf(inputFieldName);
                    //Found?
                    if (inputFieldIdx < 0)
                    {
                        //Not found
                        throw new Exception($"Reservoir instance {newMap.InstanceName}: input field {inputFieldName} is not defined among State Machine input fields.");
                    }
                    newMap.InputFieldMappingCollection.Add(inputFieldIdx);
                }
                ReservoirInstanceDefinitionCollection.Add(newMap);
            }

            return;
        }