/// <summary> /// Creates the instance and initializes it from given xml element. /// </summary> /// <param name="readoutUnitElem"> /// Xml data containing the settings. /// </param> public ReadoutUnitSettings(XElement readoutUnitElem) { Name = readoutUnitElem.Attribute("name").Value; TaskType = CommonEnums.ParseTaskType(readoutUnitElem.Attribute("task").Value); //Net settings List <XElement> netSettingsElems = new List <XElement>(); netSettingsElems.AddRange(readoutUnitElem.Descendants("ff")); netSettingsElems.AddRange(readoutUnitElem.Descendants("pp")); if (netSettingsElems.Count != 1) { throw new Exception("Only one network configuration can be specified in readout unit settings."); } if (netSettingsElems.Count == 0) { throw new Exception("Network configuration is not specified in readout unit settings."); } XElement netSettingsElem = netSettingsElems[0]; //FF? if (netSettingsElem.Name.LocalName == "ff") { NetType = ReadoutUnitNetworkType.FF; NetSettings = new FeedForwardNetworkSettings(netSettingsElem); OutputRange = ((FeedForwardNetworkSettings)NetSettings).OutputRange.DeepClone(); } else { //PP NetType = ReadoutUnitNetworkType.PP; NetSettings = new ParallelPerceptronSettings(netSettingsElem); OutputRange = ((ParallelPerceptronSettings)NetSettings).OutputRange.DeepClone(); } return; }
/// <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; }