private RlmCyclecompleteArgs RunCycle(RlmNetwork rnnNet, RlmCycle cycle, List <RlmIOWithValue> inputValues, RlmNetworkType rnnType, List <RlmIOWithValue> outputValues, double cycleScore, IEnumerable <RlmIdea> ideas = null)
        {
            //rnn_net._AddCycleToCurrentCycles(this);
            RlmCycleOutput caseOutput = RlmCerebralCortex.CoreCycleProcess(rnnNet, this, inputValues, RlmType, outputValues, cycleScore, ideas);

            return(rnnNet.EndCycle(caseOutput, RlmType));
        }
        //End Cycle
        internal RlmCyclecompleteArgs EndCycle(RlmCycleOutput cycleOutput, RlmNetworkType rnnType)
        {
            var retVal = new RlmCyclecompleteArgs(cycleOutput, this, rnnType);

            CurrentCase.Order = ++CaseOrder;

            //Fire CycleComplete Event
            if (CycleComplete != null)
            {
                CycleComplete(retVal);
            }

            return(retVal);
        }
Example #3
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);
        }