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));
        }
Beispiel #2
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 #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);
        }
        private IDictionary <int, RlmCyclecompleteArgs> RunOneSession(RlmNetwork network, IEnumerable <KeyValuePair <string, Resource> > resourceInputs, bool learn = true, bool showCycleOutput = false)
        {
            CycleOutputs.Clear();
            SessionOutputs.Clear();

            IDictionary <int, RlmCyclecompleteArgs> cycleOutputDic = new Dictionary <int, RlmCyclecompleteArgs>();

            TrainingVariables["SessionScore"].Value = 0;

            long          sessId     = network.SessionStart();
            List <object> outputList = new List <object>();

            //TODO: check later for multiple inputs
            int      min   = 0;
            int      max   = 0;
            var      resIn = resourceInputs.First();
            Resource res   = resIn.Value;
            Dictionary <string, int> inputRange = GetInputRange(res);

            min = inputRange["Min"];
            max = inputRange["Max"];

            bool usedExcludeSolutions = false;

            for (int j = min; j <= max; j++)
            {
                RlmCyclecompleteArgs result;
                List <long>          excludeSolutions = null;

                while (true)
                {
                    //Populate input values
                    var invs = new List <RlmIOWithValue>();
                    foreach (var a in network.Inputs)
                    {
                        invs.Add(new RlmIOWithValue(a, j.ToString()));
                        CycleInputs[a.Name] = j;
                    }

                    //Build and run a new RlmCycle
                    var Cycle = new RlmCycle();
                    result = Cycle.RunCycle(network, sessId, invs, learn, excludeSolutions: excludeSolutions);

                    //TODO: check later for multiple outputs
                    var rlmOut = network.Outputs.First();
                    var value  = result.CycleOutput.Outputs.First(b => b.Name == rlmOut.Name).Value;

                    if (!AllowDuplicates && !learn)
                    {
                        bool hasDup = false;
                        foreach (var rlmOutputs in SessionOutputs)
                        {
                            if (rlmOutputs.Value.Any(a => a.ToString() == value))
                            {
                                if (excludeSolutions == null)
                                {
                                    excludeSolutions = new List <long>();
                                }

                                excludeSolutions.Add(result.CycleOutput.SolutionID);
                                hasDup = true;
                                System.Diagnostics.Debug.WriteLine("Duplicate found!");
                                break;
                            }
                        }

                        if (hasDup)
                        {
                            usedExcludeSolutions = true;
                            continue;
                        }
                    }

                    //set current rn and sl
                    cycleOutputDic[j] = result;

                    CycleOutputs[rlmOut.Name] = value;

                    outputList.Add(value);
                    SessionOutputs[rlmOut.Name] = outputList;

                    break;
                }

                double cycleScore = ScoreCycle();


                if (showCycleOutput)
                {
                    //if (j == min && PredictData.Count() > min)
                    //{
                    //    PredictData.Clear();
                    //}
                    //// exposed cyclescore on predict
                    //PredictData.Add(cycleScore.ToString());
                }

                network.ScoreCycle(result.CycleOutput.CycleID, cycleScore);

                TrainingVariables["CycleScore"].Value = 0;
            }

            double sessionScore = ScoreSession();

            LastScore = sessionScore;
            if (LastScore > HighScore)
            {
                HighScore = LastScore;
            }
            network.SessionEnd(sessionScore);

            TraceLogObjectsIf(!learn && !usedExcludeSolutions && sessionScore < HighScore, $"{network.DatabaseName}_BestSolutions_InMemory", network.MemoryManager.BestSolutions.SelectMany(a => a.Value.Values));
            TraceLog($"Score: {string.Format("{0:n}", sessionScore)}");

            // exposed session data
            //SessionData.Add(sessionScore.ToString());

            return(cycleOutputDic);
        }