//Constructor /// <summary> /// Constructs an instance of State Machine /// </summary> /// <param name="settings">State Machine settings</param> /// <param name="inputRange">Range of input values</param> public StateMachine(StateMachineSettings settings, Interval inputRange) { _settings = settings.DeepClone(); //Random object if (_settings.RandomizerSeek < 0) { _rand = new System.Random(); } else { _rand = new System.Random(_settings.RandomizerSeek); } //Build structure //Reservoir instance(s) _numOfPredictors = 0; _reservoirInstanceCollection = new List <ReservoirInstance>(_settings.ReservoirInstanceDefinitionCollection.Count); foreach (StateMachineSettings.ReservoirInstanceDefinition instanceDefinition in _settings.ReservoirInstanceDefinitionCollection) { ReservoirInstance reservoirInstance = new ReservoirInstance(instanceDefinition, _settings.RandomizerSeek, inputRange); _reservoirInstanceCollection.Add(reservoirInstance); _numOfPredictors += reservoirInstance.ReservoirObj.NumOfOutputPredictors; } if (_settings.RouteInputToReadout) { _numOfPredictors += _settings.InputFieldNameCollection.Count; } //Readout layer _readoutLayer = new ReadoutLayer(_settings.TaskType, _settings.ReadoutLayerConfig, _rand); return; }
//Properties //Methods /// <summary> /// Sets State Machine internal state to its initial state /// </summary> public void Reset() { //Neural preprocessor reset NP.Reset(true); //Get rid the ReadoutLayer instance RL = null; return; }
//Constructor /// <summary> /// Constructs an instance of State Machine /// </summary> /// <param name="settings">State Machine settings</param> public StateMachine(StateMachineSettings settings) { _settings = settings.DeepClone(); //Neural preprocessor instance NP = new NeuralPreprocessor(settings.NeuralPreprocessorConfig, settings.RandomizerSeek); //Readout layer RL = null; return; }
//Constructors /// <summary> /// Creates an initialized instance. /// </summary> /// <param name="cfg">The state machine configuration.</param> public StateMachine(StateMachineSettings cfg) { Config = (StateMachineSettings)cfg.DeepClone(); //Neural preprocessor instance NP = Config.NeuralPreprocessorCfg == null ? null : new NeuralPreprocessor(Config.NeuralPreprocessorCfg, Config.RandomizerSeek); //Readout layer instance RL = new ReadoutLayer(Config.ReadoutLayerCfg); return; }
/// <summary> /// Creates and trains the State Machine readout layer. /// </summary> /// <param name="regressionInput"> /// RegressionInput object prepared by PrepareRegressionData function /// </param> /// <param name="regressionController"> /// Optional. see Regression.RegressionCallbackDelegate /// </param> /// <param name="regressionControllerData"> /// Optional custom object to be passed to regressionController together with other standard information /// </param> public ResultComparativeBundle BuildReadoutLayer(RegressionInput regressionInput, ReadoutUnit.RegressionCallbackDelegate regressionController = null, Object regressionControllerData = null ) { //Readout layer instance RL = new ReadoutLayer(_settings.ReadoutLayerConfig); //Optional mapper of predictors to readout units ReadoutLayer.PredictorsMapper mapper = null; if (_settings.MapperConfig != null) { //Create empty instance of the mapper mapper = new ReadoutLayer.PredictorsMapper(NP.NumOfPredictors); //Expand list of predicting neurons to array of predictor origin StateMachineSettings.MapperSettings.PoolRef[] neuronPoolRefCollection = new StateMachineSettings.MapperSettings.PoolRef[NP.NumOfPredictors]; int idx = 0; foreach (Reservoir.PredictorNeuron pn in NP.PredictorNeuronCollection) { neuronPoolRefCollection[idx] = new StateMachineSettings.MapperSettings.PoolRef { _reservoirInstanceIdx = pn.Neuron.Placement.ReservoirID, _poolIdx = pn.Neuron.Placement.PoolID }; ++idx; if (pn.UseSecondaryPredictor) { neuronPoolRefCollection[idx] = neuronPoolRefCollection[idx - 1]; ++idx; } } //Iterate readout units having specific predictors mapping foreach (string readoutUnitName in _settings.MapperConfig.Map.Keys) { bool[] switches = new bool[NP.NumOfPredictors]; switches.Populate(false); foreach (StateMachineSettings.MapperSettings.PoolRef allowedPool in _settings.MapperConfig.Map[readoutUnitName]) { //Enable specific predictors from allowed pool (origin) for (int i = 0; i < neuronPoolRefCollection.Length; i++) { if (neuronPoolRefCollection[i]._reservoirInstanceIdx == allowedPool._reservoirInstanceIdx && neuronPoolRefCollection[i]._poolIdx == allowedPool._poolIdx) { switches[i] = true; } } } //Add mapping to mapper mapper.Add(readoutUnitName, switches); } } //Training return(RL.Build(regressionInput.PreprocessedData, regressionController, regressionControllerData, mapper )); }
/// <summary> /// Creates an initialized instance. /// </summary> /// <param name="xmlFileName">The name of xml file where the root element matches the state machine configuration.</param> public StateMachine(string xmlFileName) { XDocument xmlDoc = XDocument.Load(xmlFileName); Config = new StateMachineSettings(xmlDoc.Root); //Neural preprocessor instance NP = Config.NeuralPreprocessorCfg == null ? null : new NeuralPreprocessor(Config.NeuralPreprocessorCfg, Config.RandomizerSeek); //Readout layer instance RL = new ReadoutLayer(Config.ReadoutLayerCfg); return; }
//Constructor /// <summary> /// Constructs an instance of State Machine /// </summary> /// <param name="settings">State Machine settings</param> public StateMachine(StateMachineSettings settings) { _settings = settings.DeepClone(); //Neural preprocessor instance NP = new NeuralPreprocessor(settings.NeuralPreprocessorConfig, settings.RandomizerSeek); NumOfValidPredictors = 0; PredictorGeneralSwitchCollection = null; //Readout layer RL = null; return; }
/// <summary> /// Trains the State Machine readout layer. /// </summary> /// <param name="rsi"> /// RegressionStageInput object prepared by PrepareRegressionStageInput function /// </param> /// <param name="regressionController"> /// Optional. see Regression.RegressionCallbackDelegate /// </param> /// <param name="regressionControllerData"> /// Optional custom object to be passed to regressionController together with other standard information /// </param> public ValidationBundle RegressionStage(RegressionStageInput rsi, ReadoutUnit.RegressionCallbackDelegate regressionController = null, Object regressionControllerData = null ) { //Readout layer instance _readoutLayer = new ReadoutLayer(_settings.ReadoutLayerConfig, DataRange); //Training return(_readoutLayer.Build(rsi.PredictorsCollection, rsi.IdealOutputsCollection, regressionController, regressionControllerData )); }
//Constructor /// <summary> /// Constructs an instance of State Machine /// </summary> /// <param name="settings">State Machine settings</param> public StateMachine(StateMachineSettings settings) { _settings = settings.DeepClone(); //Internal input generators _internalInputGeneratorCollection = new List <IGenerator>(); foreach (StateMachineSettings.InputSettings.InternalField field in _settings.InputConfig.InternalFieldCollection) { if (field.GeneratorSettings.GetType() == typeof(ConstGeneratorSettings)) { _internalInputGeneratorCollection.Add(new ConstGenerator((ConstGeneratorSettings)field.GeneratorSettings)); } else if (field.GeneratorSettings.GetType() == typeof(RandomValueSettings)) { _internalInputGeneratorCollection.Add(new RandomGenerator((RandomValueSettings)field.GeneratorSettings)); } else if (field.GeneratorSettings.GetType() == typeof(SinusoidalGeneratorSettings)) { _internalInputGeneratorCollection.Add(new SinusoidalGenerator((SinusoidalGeneratorSettings)field.GeneratorSettings)); } else if (field.GeneratorSettings.GetType() == typeof(MackeyGlassGeneratorSettings)) { _internalInputGeneratorCollection.Add(new MackeyGlassGenerator((MackeyGlassGeneratorSettings)field.GeneratorSettings)); } else { throw new Exception($"Unsupported internal signal generator for field {field.Name}"); } } //Reservoir instance(s) //Random generator used for reservoir structure initialization Random rand = (_settings.RandomizerSeek < 0 ? new Random() : new Random(_settings.RandomizerSeek)); _numOfPredictors = 0; _reservoirCollection = new List <Reservoir>(_settings.ReservoirInstanceDefinitionCollection.Count); foreach (StateMachineSettings.ReservoirInstanceDefinition instanceDefinition in _settings.ReservoirInstanceDefinitionCollection) { Reservoir reservoir = new Reservoir(instanceDefinition, DataRange, rand); _reservoirCollection.Add(reservoir); _numOfPredictors += reservoir.NumOfOutputPredictors; } if (_settings.InputConfig.RouteExternalInputToReadout) { _numOfPredictors += _settings.InputConfig.ExternalFieldCollection.Count; } //Readout layer _readoutLayer = null; return; }
/// <summary> /// Creates and trains the State Machine readout layer. /// Function uses specific mapping of predictors to readout units, if available. /// Function also rejects unusable predictors having no reasonable fluctuation of values. /// </summary> /// <param name="regressionInput"> /// RegressionInput object prepared by PrepareRegressionData function /// </param> /// <param name="regressionController"> /// Optional. see Regression.RegressionCallbackDelegate /// </param> /// <param name="regressionControllerData"> /// Optional custom object to be passed to regressionController together with other standard information /// </param> public ResultBundle BuildReadoutLayer(RegressionInput regressionInput, ReadoutUnit.RegressionCallbackDelegate regressionController = null, Object regressionControllerData = null ) { //Readout layer instance RL = new ReadoutLayer(_settings.ReadoutLayerConfig); //Create empty instance of the mapper ReadoutLayer.PredictorsMapper mapper = new ReadoutLayer.PredictorsMapper(PredictorGeneralSwitchCollection); if (_settings.MapperCfg != null) { //Expand list of predicting neurons to array of predictor origin StateMachineSettings.MapperSettings.AllowedPool[] neuronPoolRefCollection = new StateMachineSettings.MapperSettings.AllowedPool[NP.NumOfPredictors]; int idx = 0; foreach (HiddenNeuron neuron in NP.PredictorNeuronCollection) { StateMachineSettings.MapperSettings.AllowedPool poolRef = new StateMachineSettings.MapperSettings.AllowedPool { _reservoirInstanceIdx = neuron.Placement.ReservoirID, _poolIdx = neuron.Placement.PoolID }; for (int i = 0; i < neuron.PredictorsCfg.NumOfEnabledPredictors; i++) { neuronPoolRefCollection[idx] = poolRef; ++idx; } } //Iterate all readout units foreach (string readoutUnitName in _settings.ReadoutLayerConfig.OutputFieldNameCollection) { bool[] switches = new bool[NP.NumOfPredictors]; //Initially allow all valid predictors PredictorGeneralSwitchCollection.CopyTo(switches, 0); //Exists specific mapping? if (_settings.MapperCfg != null && (_settings.MapperCfg.PoolsMap.ContainsKey(readoutUnitName) || _settings.MapperCfg.RoutedInputFieldsMap.ContainsKey(readoutUnitName))) { //Routed input fields if (_settings.MapperCfg.RoutedInputFieldsMap.ContainsKey(readoutUnitName)) { //Initially disable all routed input fields for (int i = NP.PredictorNeuronCollection.Count; i < NP.NumOfPredictors; i++) { switches[i] = false; } //Enable enabled routed input fields List <int> enabledRoutedFieldsIdxs = _settings.MapperCfg.RoutedInputFieldsMap[readoutUnitName]; for (int i = 0; i < enabledRoutedFieldsIdxs.Count; i++) { switches[NP.PredictorNeuronCollection.Count + enabledRoutedFieldsIdxs[i]] = PredictorGeneralSwitchCollection[NP.PredictorNeuronCollection.Count + enabledRoutedFieldsIdxs[i]]; } } //Neuron predictors if (_settings.MapperCfg.PoolsMap.ContainsKey(readoutUnitName)) { //Initially disable all neuron predictors for (int i = 0; i < NP.PredictorNeuronCollection.Count; i++) { switches[i] = false; } //Enable allowed neuron predictors foreach (StateMachineSettings.MapperSettings.AllowedPool allowedPool in _settings.MapperCfg.PoolsMap[readoutUnitName]) { //Enable specific predictors from allowed pool (origin) for (int i = 0; i < NP.PredictorNeuronCollection.Count; i++) { if (neuronPoolRefCollection[i]._reservoirInstanceIdx == allowedPool._reservoirInstanceIdx && neuronPoolRefCollection[i]._poolIdx == allowedPool._poolIdx) { //Enable predictor if it is valid switches[i] = PredictorGeneralSwitchCollection[i]; } } } } } //Add mapping to mapper mapper.Add(readoutUnitName, switches); } } //Training return(RL.Build(regressionInput.PreprocessedData, regressionController, regressionControllerData, mapper )); }