Beispiel #1
0
        //Record Case
        private static Case RecordCase(RlmCycle cycle, GetRneuronResult rneuronFound, IEnumerable <RlmIOWithValue> rnn_ins, IEnumerable <RlmIOWithValue> runn_outs,
                                       double cyclescore, GetSolutionResult solutionFound, Int16 currentmfactor, bool resultcompletelyrandom, short sequentialmfactorsuccessescount)
        {
            Case casefromdb = cycle.CaseReference; //db.Cases.Find(cycle.CycleCaseID);

            //db.Sessions.Attach(casefromdb.Session);

            //db.Entry(casefromdb.Session).State = EntityState.Unchanged;

            //casefromdb.Session = db.Sessions.Find(casefromdb.Session.ID);

            //Check for none found
            if (casefromdb == null)
            {
                throw new Exception("An error occurred, the current case could not be located in the database.");
            }

            //Check for none found
            if (rneuronFound.Rneuron == null)
            {
                throw new Exception("An error occurred, the current Rnueron could not be located in the database.");
            }

            //Check for none found
            if (solutionFound.Solution == null)
            {
                throw new Exception("An error occurred, the current Solution could not be located in the database.");
            }

            //Assign Values
            //casefromdb.Rneuron = rneuron;
            casefromdb.Rneuron_ID = rneuronFound.Rneuron.ID;
            casefromdb.Rneuron    = (rneuronFound.ExistsInCache) ? null : rneuronFound.Rneuron;
            //casefromdb.Idea_Implementations --> Later
            casefromdb.Solution_ID                     = solutionFound.Solution.ID;
            casefromdb.Solution                        = (solutionFound.ExistsInCache) ? null : solutionFound.Solution;
            casefromdb.CycleEndTime                    = DateTime.Now;
            casefromdb.CycleScore                      = cyclescore;
            casefromdb.CurrentRFactor                  = 0;// rneuron.RandomizationFactor;
            casefromdb.CurrentMFactor                  = currentmfactor;
            casefromdb.ResultCompletelyRandom          = resultcompletelyrandom;
            casefromdb.SequentialMFactorSuccessesCount = sequentialmfactorsuccessescount;


            return(casefromdb);
        }
Beispiel #2
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);
        }