private Int64 Init(RlmNetwork rnn_net, Int64 sessionID, List <RlmIOWithValue> inputs_values, Boolean learn, List <RlmIOWithValue> output_values = null)
        {
            if (this.CaseReference != null)
            {
                throw new Exception("You may only envoke Runxx once per cycle object.");
            }

            // set type of learning based on Inputs and Outputs
            RlmType = RlmNetworkType.Predict;
            if (learn)
            {
                if ((inputs_values != null && inputs_values.Count > 0) &&
                    (output_values != null && output_values.Count > 0)) // Inputs & Outputs
                {
                    RlmType = RlmNetworkType.Supervised;
                }
                else if ((inputs_values != null && inputs_values.Count > 0) &&
                         (output_values == null || (output_values != null && output_values.Count == 0))) // Inputs without Outputs
                {
                    RlmType = RlmNetworkType.Unsupervised;
                }
            }

            Case cyclecase = new Case()
            {
                Session_ID = sessionID /*db.Sessions.Where(item=>item.ID == sessionID).First()*/, CycleStartTime = DateTime.Now, CycleEndTime = DateTime.Now
            };

            this.CaseReference = cyclecase;
            return(cyclecase.ID);
        }
        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));
        }
        /// <summary>
        /// starts training
        /// </summary>
        /// <param name="rnnNet">current network being used</param>
        /// <param name="sessionID">unique identifier for the session being started</param>
        /// <param name="inputsValues">Inputs with stored values</param>
        /// <param name="learn">Indicator that if true, will start training, if false, will run prediction</param>
        /// <param name="outputValues">Outputs with stored values</param>
        /// <param name="cyclescore">Score of the current cycle</param>
        /// <param name="Parallel"></param>
        /// <param name="ideas">Gives bias to the RLM on what to output</param>
        /// <returns></returns>
        public RlmCyclecompleteArgs RunCycle(RlmNetwork rnnNet, Int64 sessionID, List <RlmIOWithValue> inputsValues, Boolean learn, List <RlmIOWithValue> outputValues = null, double cyclescore = 0.000, Boolean Parallel = false, IEnumerable <RlmIdea> ideas = null)
        {
            //Run Validity Checks -- includes local var assignment
            Int64 cyclecaseid = Init(rnnNet, sessionID, inputsValues, learn, outputValues);

            //if parallel, it is executed on another thread and returns null since the cycle complete arguments can only be taken via the Rnetwork cycle complete event
            if (Parallel)
            {
                cycletask = Task.Run(() => RunCycle(rnnNet, this, inputsValues, RlmType, outputValues, cyclescore, ideas));
                return(null);
            }
            else //if not parallel, we run it on same thread and return the cycle complete arguments **did it this way for a better windowless training
            {
                return(RunCycle(rnnNet, this, inputsValues, RlmType, outputValues, cyclescore, ideas));
            }

            #region Old code commented
            //cycletask = Task.Run(() =>
            //{
            //    rnn_net._AddCycleToCurrentCycles(this);
            //    rlm_cycle_output caseOutput = rnn_cerebralcortex.CoreCycleProcess(rnn_net, this, inputs_values, RlmType, output_values, cyclescore);
            //    rnn_net._EndCycle(caseOutput, RlmType);
            //});

            //Non-Parallel Waits for Thread to Complete
            //if (!Parallel) cycletask.Wait();
            #endregion
        }
 private void ClearNetwork()
 {
     if (network != null)
     {
         network.DataPersistenceComplete -= Network_DataPersistenceComplete;
         network.Dispose();
         network = null;
     }
 }
示例#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);
        }
        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);
        }
        public IDictionary <int, RlmCyclecompleteArgs> StartTraining(RlmSettings settings, CancellationToken?token = null)
        {
            dataPersistEvent.Reset();
            //stopTraining = false;
            //settings = this.Settings;
            this.Settings = settings;

            //Get RLM Settings from user
            int startRandomness       = settings.StartRandomness;
            int endRandomness         = settings.EndRandomness;
            int maxBracket            = settings.MaxLinearBracket;
            int minBracket            = settings.MinLinearBracket;
            RlmSimulationType simType = settings.SimulationType;
            TimeSpan          time    = settings.Time;
            double            target  = settings.SimulationTarget;

            // clear previous network, if any
            ClearNetwork();

            IDictionary <int, RlmCyclecompleteArgs> cycleOutputDic = null;

            network = (string.IsNullOrEmpty(DatabaseName) ? new RlmNetwork() : new RlmNetwork(RlmDbData, true));

            network.DataPersistenceComplete += Network_DataPersistenceComplete;

            network.StartRandomness  = startRandomness;
            network.EndRandomness    = endRandomness;
            network.MaxLinearBracket = maxBracket;
            network.MinLinearBracket = minBracket;

            List <RlmIO> inputs  = new List <RlmIO>();
            List <RlmIO> outputs = new List <RlmIO>();

            //get inputs/outputs from resources
            var resourceInputs  = Resources.Where(a => a.Value.RLMObject == RLMObject.Input);
            var resourceOutputs = Resources.Where(a => a.Value.RLMObject == RLMObject.Output);

            //set rlm inputs/outputs
            inputs  = GetRlmIOFromResource(resourceInputs);
            outputs = GetRlmIOFromResource(resourceOutputs);

            //create network
            if (!network.LoadNetwork())
            {
                network.NewNetwork("RlmOptimizer", inputs, outputs);
            }

            //start training
            int      sessions = settings.SimulationType == RlmSimulationType.Sessions ? Convert.ToInt32(settings.SimulationTarget) : settings.NumOfSessionsReset;
            double   hours    = settings.SimulationType == RlmSimulationType.Time ? settings.Time.TotalHours : 0;
            DateTime endsOn   = DateTime.Now.AddHours(hours);

            TraceLog($"RLM Settings: \n\n" +
                     $"Randomness: Max = {startRandomness}, End = {endRandomness}\nLinear Bracket: Max = {maxBracket}, Min = {minBracket}\n" +
                     $"Simulation Type: {simType}\nTarget: {target}\nTime: {hours}\n" +
                     $"Cycle Score Formula: {string.Join(" | ", CyclePhase.Formula)}\n" +
                     $"Session Score Formula: {string.Join(" | ", SessionPhase.Formula)}");
            TraceLog();

            int  scoreHits = 0;
            bool timesUp   = false;

            //for (var trial = 1; trial <= 5; trial++)
            //{
            //TrainingLog($"\nTrial #{trial}");

            do
            {
                //settings
                network.NumSessions      = sessions;
                network.StartRandomness  = startRandomness;
                network.EndRandomness    = endRandomness;
                network.MaxLinearBracket = maxBracket;
                network.MinLinearBracket = minBracket;

                network.ResetRandomizationCounter();

                if ((simType == RlmSimulationType.Time && DateTime.Compare(DateTime.Now, endsOn) == 1) || token?.IsCancellationRequested == true)
                {
                    timesUp = true;
                }

                if (!timesUp)
                {
                    TraceLog("\nTraining...\n");
                    for (int i = 1; i <= sessions; i++)
                    {
                        cycleOutputDic = RunOneSession(network, resourceInputs, true);
                        if (settings.SimulationType == RlmSimulationType.Score)
                        {
                            if (UpdateScoreHit(ref scoreHits, settings) || token?.IsCancellationRequested == true)
                            {
                                break;
                            }
                        }

                        if ((simType == RlmSimulationType.Time && DateTime.Compare(DateTime.Now, endsOn) == 1) || token?.IsCancellationRequested == true)
                        {
                            timesUp = true;
                            break;
                        }

                        if (stopTraining || token?.IsCancellationRequested == true)
                        {
                            break;
                        }
                    }
                }

                //predict
                TraceLog("\nPredicting...\n");
                if (settings.SimulationType == RlmSimulationType.Score)
                {
                    for (int i = 1; i <= settings.NumScoreHits; i++)
                    {
                        cycleOutputDic = RunOneSession(network, resourceInputs, false, (i == settings.NumScoreHits));
                        if (UpdateScoreHit(ref scoreHits, settings) || token?.IsCancellationRequested == true)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    cycleOutputDic = RunOneSession(network, resourceInputs, false, true);
                }

                if (stopTraining || token?.IsCancellationRequested == true)
                {
                    break;
                }
            } while ((!timesUp && simType == RlmSimulationType.Time) ||
                     (simType == RlmSimulationType.Score && settings.NumScoreHits > scoreHits));
            //}

            //end training
            network.TrainingDone();

            if (simType == RlmSimulationType.Score)
            {
                TraceLog($"Score Hits: {scoreHits}");
            }

            TraceLog("Optimization Done");

            return(cycleOutputDic);
        }