コード例 #1
0
        /// <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;
        }
コード例 #2
0
        /// <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);
        }