Beispiel #1
0
        /// <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;
        }
Beispiel #3
0
        //save rneuron
        public async void saveRneuron(Rneuron rneuron)
        {
            using (RlmDbEntities db = new RlmDbEntities(databaseName))
            {
                try
                {
                    if (networkLoaded)
                    {
                        Task.Delay(5000).Wait();

                        Rneuron _rneuron = db.Rneurons.Add(rneuron);

                        _rneuron.Rnetwork_ID = networkID;

                        foreach (var rn in _rneuron.Input_Values_Reneurons)
                        {
                            db.Input_Values_Reneurons.Add(rn);
                        }

                        await db.SaveChangesAsync();
                    }
                }
                catch (Exception ex)
                {
                    RlmDbLogger.Error(ex, databaseName, "saveRneuron");
                }
            }
        }
Beispiel #4
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)
        {
            // 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);
        }
Beispiel #5
0
        internal static RlmCycleOutput CoreCycleProcess(RlmNetwork rnn_net, RlmCycle rnn_cyc, IEnumerable <Models.RlmIOWithValue> rnn_ins, RlmNetworkType rnnType, IEnumerable <Models.RlmIOWithValue> rnn_outs, double cyclescore, IEnumerable <RlmIdea> ideas = null, IEnumerable <long> excludeSolutions = null)
        {
            var memoryMgr = rnn_net.MemoryManager;

            // temp benchmark only
            //rnn_net.CurrentCycleCount++;
            // temp benhcmark only

            // Determine if any inputs are of Linear type
            bool hasLinearInputs = rnn_ins.Any(a => a.Type == RlmInputType.Linear);

            // update input momentums
            if (hasLinearInputs)
            {
                foreach (var item in rnn_ins)
                {
                    if (item.Type == RlmInputType.Linear)
                    {
                        var inputMomentumObj = rnn_net.InputMomentums[item.ID];
                        inputMomentumObj.SetInputValue(Convert.ToDouble(item.Value));
                        item.InputMomentum = inputMomentumObj;
                    }
                }
            }

            //Get rneuron
            GetRneuronResult rneuronFound = memoryMgr.GetRneuronFromInputs(rnn_ins, rnn_net.CurrentNetworkID);
            Rneuron          neuron       = rneuronFound.Rneuron;

            //Holds the solution instance
            GetSolutionResult solutionFound = new GetSolutionResult();
            Solution          solution      = null;

            IEnumerable <Models.RlmIO> outputs = rnn_net.Outputs;

            bool   completelyRandom = false;
            double randomnessValue  = rnn_net.RandomnessCurrentValue;

            if (rnnType == RlmNetworkType.Supervised)
            {
                //Supervised, get solution and record ideal score
                solutionFound = memoryMgr.GetSolutionFromOutputs(rnn_outs);
                solution      = solutionFound.Solution;
                cyclescore    = IDEAL_SCORE;
            }
            else if (rnnType == RlmNetworkType.Unsupervised && randomnessValue > 0)
            {
                //TODO:  This should be based upon the randomization factor
                double randomProbability = Util.GetRandomDoubleNumber(0, 100);
                bool   random            = randomProbability <= randomnessValue;

                //Idea
                //ToDo: Implement Ideas
                //The idea implementation will not be added until core functionality works.  It is an "extra" and the network can learn without it.  In fact, since it reduces load, we need
                //to test without it in place first.  Otherwise networks that don't have an applicable "idea" may crash

                //System.Diagnostics.Debug.WriteLine("Threshold: " + randomnThreshold);

                long?bestSolutionId = null;
                if (!random)
                {
                    // get best solution
                    solution       = memoryMgr.GetBestSolution(rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0, excludeSolutions: excludeSolutions); //db.GetBestSolution(rnn_net.CurrentNetworkID, rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0);
                    bestSolutionId = solution?.ID;
                    if (solution == null)
                    {
                        completelyRandom = true;
                        solutionFound    = memoryMgr.GetRandomSolutionFromOutput(randomnessValue, outputs, bestSolutionId, ideas);
                    }
                    else
                    {
                        solutionFound.Solution      = solution;
                        solutionFound.ExistsInCache = true;
                    }
                }
                else if (random && outputs.Count() > 1)
                {
                    solution         = memoryMgr.GetBestSolution(rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0, excludeSolutions: excludeSolutions); //db.GetBestSolution(rnn_net.CurrentNetworkID, rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0);
                    bestSolutionId   = solution?.ID;
                    completelyRandom = true;
                    solutionFound    = memoryMgr.GetRandomSolutionFromOutput(randomnessValue, outputs, bestSolutionId, ideas);
                }
                else
                {
                    completelyRandom = true;
                    solutionFound    = memoryMgr.GetRandomSolutionFromOutput(randomnessValue, outputs, ideas: ideas);
                }

                solution = solutionFound.Solution;
            }
            else // Predict
            {
                solution = memoryMgr.GetBestSolution(rnn_ins, predict: true, predictLinearTolerance: rnn_net.PredictLinear, excludeSolutions: excludeSolutions); //db.GetBestSolution(rnn_net.CurrentNetworkID, new List<long>() { neuron.ID }, true);

                if (solution == null)
                {
                    completelyRandom = true;
                    solutionFound    = memoryMgr.GetRandomSolutionFromOutput(randomnessValue, outputs, ideas: ideas);
                    solution         = solutionFound.Solution;
                    #region TODO cousin node search
                    //// no solution found AND all inputs are Distinct
                    //if (!hasLinearInputs)
                    //{
                    //    completelyRandom = true;
                    //    //solution = GetRandomSolutionFromOutput(db, rnn_net.CurrentNetworkID, outputs, false);
                    //    solutionFound = memoryMgr.GetRandomSolutionFromOutput(randomnessValue, outputs); //GetRandomSolutionFromOutput(db, rnn_net, outputs, rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0);
                    //}
                    //else // has linear
                    //{
                    //    // TODO need to change the methods used below to MemoryManager
                    //    //// gets all the known inputs
                    //    //var knownInputs = DetermineKnownInputs(db, rnn_ins, rnn_net.CousinNodeSearchToleranceIncrement);
                    //    //if (knownInputs.Count > 0)
                    //    //{
                    //    //    // holds the top cases for each known input
                    //    //    var topCases = new List<Case>();
                    //    //    foreach (var item in knownInputs)
                    //    //    {
                    //    //        // gets the top solution for the current input with incremental checks based on the linear bracket
                    //    //        var topCase = GetBestKnownCase(db, item, rnn_net.CousinNodeSearchToleranceIncrement);
                    //    //        if (topCase != null)
                    //    //        {
                    //    //            topCases.Add(topCase);
                    //    //        }
                    //    //    }

                    //    //    // determine which Case has the highest score and get it's corresponding solution
                    //    //    solution = topCases.OrderByDescending(a => a.Session.DateTimeStop)
                    //    //        .ThenByDescending(a => a.CycleEndTime)
                    //    //        .ThenByDescending(a => a.CycleScore)
                    //    //        .ThenByDescending(a => a.Session.SessionScore)
                    //    //        .Take(1)
                    //    //        .Select(a => a.Solution)
                    //    //        .FirstOrDefault();
                    //    //}
                    //    //else // if no known inputs then we get solution randomly
                    //    //{
                    //    //    completelyRandom = true;
                    //    //    //solution = GetRandomSolutionFromOutput(db, rnn_net.CurrentNetworkID, outputs, false);
                    //    //    solution = GetRandomSolutionFromOutput(db, rnn_net, outputs, rnn_ins, (hasLinearInputs) ? rnn_net.LinearToleranceCurrentValue : 0);
                    //    //}
                    //}
                    #endregion

                    //solutionFound.Solution = solution;
                    //solutionFound.ExistsInCache = false;
                }
                else
                {
                    solutionFound.Solution      = solution;
                    solutionFound.ExistsInCache = true;
                }
            }

            //Document score, solution in Case
            var newCase = RecordCase(rnn_cyc
                                     , rneuronFound
                                     , rnn_ins
                                     , rnn_outs
                                     , cyclescore
                                     , solutionFound
                                     , 0                //ToDo: Pass the current maturity factor setting
                                     , completelyRandom //ToDo: pass whether or not the result was completely randomly generated
                                     , 0                //ToDo: pass sequential count
                                     );

            // set Current case reference
            rnn_net.CurrentCase = newCase;

            var cycleOutput = new RlmCycleOutput(newCase.ID, newCase.Rneuron_ID, newCase.Solution_ID, rnn_net.Outputs, solution.Output_Values_Solutions);
            cycleOutput.CompletelyRandom = completelyRandom;
            return(cycleOutput);
        }
        /// <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);
        }