/// <summary>
        /// Gets solution and record ideal score
        /// </summary>
        /// <param name="outputs"></param>
        /// <returns></returns>
        public GetSolutionResult GetSolutionFromOutputs(IEnumerable <RlmIOWithValue> outputs)
        {
            GetSolutionResult retVal   = new GetSolutionResult();
            Solution          solution = null;

            // generate key based on output values
            long solutionId = Util.GenerateHashKey(outputs.Select(a => a.Value).ToArray());

            // create new solution if not exists
            if (!Solutions.TryGetValue(solutionId, out solution))
            {
                solution = new Solution()
                {
                    ID = solutionId
                };

                foreach (var o in outputs)
                {
                    // create OVS instance
                    var ovs = new Output_Values_Solution()
                    {
                        ID          = Util.GenerateHashKey(solutionId, o.ID),
                        Value       = o.Value,
                        Output_ID   = o.ID,
                        Solution_ID = solutionId
                    };
                    solution.Output_Values_Solutions.Add(ovs);

                    // insert into dynamic output collection
                    HashSet <SolutionOutputSet> outputSet = DynamicOutputs[o.ID];
                    outputSet.Add(new SolutionOutputSet()
                    {
                        SolutionId = solutionId,
                        Value      = o.Value
                    });
                }

                Solutions.TryAdd(solution.ID, solution);

                //Solutions2.Enqueue(retVal);

                //solution_queue.Add(retVal);

                retVal.Solution      = solution;
                retVal.ExistsInCache = false;
            }
            else
            {
                retVal.Solution      = solution;
                retVal.ExistsInCache = true;
            }

            return(retVal);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a random solution from outputs or randomize
        /// </summary>
        /// <param name="randomnessCurrVal"></param>
        /// <param name="outputs"></param>
        /// <param name="bestSolutionId"></param>
        /// <returns></returns>
        public GetSolutionResult GetRandomSolutionFromOutput(double randomnessCurrVal, IEnumerable <RlmIO> outputs, long?bestSolutionId = null, IEnumerable <RlmIdea> ideas = null)
        {
            GetSolutionResult retVal = null;

            if (outputs.Count() == 1)
            {
                IEnumerable <RlmIOWithValue> outputWithValues = GetRandomOutputValues(outputs, ideas);
                retVal = GetSolutionFromOutputs(outputWithValues);
            }
            else
            {
                var outputsWithVal = new List <RlmIOWithValue>();

                // check if best solution was passed in as parameter or not
                if (bestSolutionId.HasValue)
                {
                    IEnumerable <Output_Values_Solution> bestOutputs = null;
                    int cntRandomValues = 0;

                    foreach (var item in outputs)
                    {
                        // use best solution value if randomness outside threshold
                        int randomnessValue = Util.Randomizer.Next(1, 101);
                        if (randomnessValue > randomnessCurrVal)
                        {
                            if (bestOutputs == null)
                            {
                                //bestOutputs = db.Output_Values_Solutions.Where(a => a.Solution_ID == bestSolutionId.Value);
                                Solution solution = Solutions[bestSolutionId.Value];
                                bestOutputs = solution.Output_Values_Solutions;
                            }

                            var bestOutput = bestOutputs.FirstOrDefault(a => a.Output_ID == item.ID);
                            outputsWithVal.Add(new RlmIOWithValue(item, bestOutput.Value));
                        }
                        else // get random value
                        {
                            RlmIdea idea = null;
                            if (ideas != null)
                            {
                                idea = ideas.FirstOrDefault(a => a.RlmIOId == item.ID);
                            }

                            string value = GetRandomValue(item, idea);
                            outputsWithVal.Add(new RlmIOWithValue(item, value));
                            cntRandomValues++;
                        }
                    }

                    // if no random values were assigned then we randomly select one to randomize
                    // this is to ensure we have at least one random output value
                    if (cntRandomValues == 0)
                    {
                        var index  = Util.Randomizer.Next(0, outputsWithVal.Count);
                        var output = outputsWithVal.ElementAt(index);

                        RlmIdea idea = null;
                        if (ideas != null)
                        {
                            idea = ideas.FirstOrDefault(a => a.RlmIOId == output.ID);
                        }

                        string value = GetRandomValue(output, idea);
                        output.Value = value;
                    }
                }
                else // no best solution, so we give out all random values
                {
                    outputsWithVal.AddRange(GetRandomOutputValues(outputs, ideas));
                }

                retVal = GetSolutionFromOutputs(outputsWithVal);
            }

            return(retVal);
        }
Exemplo n.º 4
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);
        }