/// <summary> /// Sets the Rneuron /// </summary> /// <param name="rneuron"></param> public void SetRneuronWithInputs(Rneuron rneuron) { var rneuronId = rneuron.ID; int cnt = 0; IComparer <RlmInputKey> distinctComparer = new RlmInputKeyDistinctComparer(); IComparer <RlmInputKey> linearComparer = new RlmInputKeyLinearComparer(); // TODO must implement repopulation of data arrays for loading network // build dynamic inputs foreach (var i in rneuron.Input_Values_Rneurons) { double inputDoubleValue = 0; if (i.DotNetType == typeof(bool).ToString()) { bool boolVal = Convert.ToBoolean(i.Value); inputDoubleValue = (boolVal) ? 1 : 0;; } else { inputDoubleValue = Convert.ToDouble(i.Value); } doubleInputs[cnt].Add(inputDoubleValue); results[cnt].Resize(doubleInputs[cnt].DataArray.Length); cnt++; } Rneurons.TryAdd(rneuronId, rneuron); rneuronIds.Add(rneuronId); }
/// <summary> /// Sets the Rneuron /// </summary> /// <param name="rneuron"></param> public void SetRneuronWithInputs(Rneuron rneuron) { var rneuronId = rneuron.ID; // add rneuron to cache Rneurons.TryAdd(rneuronId, rneuron); bool isFirstInput = true; RlmInputValue lastInputValue = null; int cnt = 0; IComparer <RlmInputKey> distinctComparer = new RlmInputKeyDistinctComparer(); IComparer <RlmInputKey> linearComparer = new RlmInputKeyLinearComparer(); // build dynamic inputs foreach (var i in rneuron.Input_Values_Reneurons) { RlmInputKey inputKey = new RlmInputKey() { Value = i.Value, InputNum = cnt, Type = i.InputType }; inputKey.DoubleValue = (i.InputType == Enums.RlmInputType.Linear) ? Convert.ToDouble(i.Value) : 0D; RlmInputValue inputVal = null; if (!isFirstInput) { if (lastInputValue.RelatedInputs == null) { lastInputValue.RelatedInputs = new SortedList <RlmInputKey, RlmInputValue>(i.InputType == Enums.RlmInputType.Linear ? linearComparer : distinctComparer); } if (!lastInputValue.RelatedInputs.TryGetValue(inputKey, out inputVal)) { inputVal = new RlmInputValue(); lastInputValue.RelatedInputs.Add(inputKey, inputVal); } lastInputValue = inputVal; } else { if (DynamicInputs == null) { DynamicInputs = new SortedList <RlmInputKey, RlmInputValue>(i.InputType == Enums.RlmInputType.Linear ? linearComparer : distinctComparer); } isFirstInput = false; if (!DynamicInputs.TryGetValue(inputKey, out inputVal)) { inputVal = new RlmInputValue(); DynamicInputs.Add(inputKey, inputVal); } lastInputValue = inputVal; } cnt++; } lastInputValue.RneuronId = rneuronId; }
/// <summary> /// Gets existing Rneuron and creates a new one if not existing /// </summary> /// <param name="inputs">Inputs with value</param> /// <param name="rnetworkID">Current NetworkId</param> /// <returns></returns> public GetRneuronResult GetRneuronFromInputs(IEnumerable <RlmIOWithValue> inputs, long rnetworkID) { // generate key based on input values long rneuronId = Util.GenerateHashKey(inputs.Select(a => a.Value).ToArray()); // create new rneuron if not exists if (!Rneurons.ContainsKey(rneuronId)) { var rneuron = new Rneuron() { ID = rneuronId, Rnetwork_ID = rnetworkID }; int cnt = 0; foreach (var i in inputs) { // create IVR instance var ivr = new Input_Values_Rneuron() { ID = Util.GenerateHashKey(rneuronId, i.ID), Value = i.Value, Input_ID = i.ID, Rneuron_ID = rneuronId, DotNetType = i.DotNetType, InputType = i.Type }; rneuron.Input_Values_Rneurons.Add(ivr); double value; if (i.DotNetType == typeof(bool).ToString()) { bool boolVal = Convert.ToBoolean(i.Value); value = (boolVal) ? 1D : 0D; } else { value = Convert.ToDouble(ivr.Value); } doubleInputs[cnt].Add(value); results[cnt].Resize(doubleInputs[cnt].DataArray.Length); cnt++; } Rneurons.TryAdd(rneuronId, rneuron); rneuronIds.Add(rneuronId); retValGetRneuronFromInputs.Rneuron = rneuron; retValGetRneuronFromInputs.ExistsInCache = false; } else { retValGetRneuronFromInputs.Rneuron = Rneurons[rneuronId]; retValGetRneuronFromInputs.ExistsInCache = true; } return(retValGetRneuronFromInputs); }
/// <summary> /// Gets existing Rneuron and creates a new one if not existing /// </summary> /// <param name="inputs">Inputs with value</param> /// <param name="rnetworkID">Current NetworkId</param> /// <returns></returns> public GetRneuronResult GetRneuronFromInputs(IEnumerable <RlmIOWithValue> inputs, long rnetworkID) { GetRneuronResult retVal = new GetRneuronResult(); Rneuron rneuron = null; // generate key based on input values long rneuronId = Util.GenerateHashKey(inputs.Select(a => a.Value).ToArray()); // create new rneuron if not exists if (!Rneurons.TryGetValue(rneuronId, out rneuron)) { rneuron = new Rneuron() { ID = rneuronId, Rnetwork_ID = rnetworkID }; bool isFirstInput = true; RlmInputValue lastInputValue = null; int cnt = 0; IComparer <RlmInputKey> distinctComparer = new RlmInputKeyDistinctComparer(); IComparer <RlmInputKey> linearComparer = new RlmInputKeyLinearComparer(); foreach (var i in inputs) { // create IVR instance var ivr = new Input_Values_Rneuron() { ID = Util.GenerateHashKey(rneuronId, i.ID), Value = i.Value, Input_ID = i.ID, Rneuron_ID = rneuronId, DotNetType = i.DotNetType, InputType = i.Type }; rneuron.Input_Values_Reneurons.Add(ivr); RlmInputKey inputKey = new RlmInputKey() { Value = ivr.Value, InputNum = cnt, Type = i.Type }; inputKey.DoubleValue = (i.Type == Enums.RlmInputType.Linear) ? Convert.ToDouble(ivr.Value) : 0D; RlmInputValue inputVal = null; if (!isFirstInput) { if (lastInputValue.RelatedInputs == null) { lastInputValue.RelatedInputs = new SortedList <RlmInputKey, RlmInputValue>(i.Type == Enums.RlmInputType.Linear ? linearComparer : distinctComparer); } if (!lastInputValue.RelatedInputs.TryGetValue(inputKey, out inputVal)) { inputVal = new RlmInputValue(); lastInputValue.RelatedInputs.Add(inputKey, inputVal); } lastInputValue = inputVal; } else { if (DynamicInputs == null) { DynamicInputs = new SortedList <RlmInputKey, RlmInputValue>(i.Type == Enums.RlmInputType.Linear ? linearComparer : distinctComparer); } isFirstInput = false; if (!DynamicInputs.TryGetValue(inputKey, out inputVal)) { inputVal = new RlmInputValue(); DynamicInputs.Add(inputKey, inputVal); } lastInputValue = inputVal; } cnt++; } lastInputValue.RneuronId = rneuronId; Rneurons.TryAdd(rneuronId, rneuron); //rneuron_queue.Add(retVal); //if (Rneurons.TryAdd(rneuronId, retVal)) //{ //} //Rneurons2.Enqueue(retVal); //rneuron_queue.Add(retVal); retVal.Rneuron = rneuron; retVal.ExistsInCache = false; } else { retVal.Rneuron = rneuron; retVal.ExistsInCache = true; } return(retVal); }