public Action ChooseAction()
        {
            var    processedActions = 0;
            float  currentValue     = 0.0f;
            int    CurrentDepth     = 0;
            Action action;
            float  bestActionDiscontentment = float.MaxValue;

            while (CurrentDepth >= 0)
            {
                if (processedActions > ActionCombinationsProcessedPerFrame)
                {
                    this.InProgress = false;
                    break;
                }

                if (CurrentDepth >= MAX_DEPTH)
                {
                    processedActions++;
                    currentValue = Models[CurrentDepth].CalculateDiscontentment(Goals);

                    if (currentValue < BestDiscontentmentValue)
                    {
                        BestDiscontentmentValue = currentValue;

                        ActionPerLevel           = ActionPerLevel.OrderBy(a => a.GetDiscontentment(Goals)).ToArray();
                        BestAction               = ActionPerLevel[0];
                        bestActionDiscontentment = ActionPerLevel[0].GetDiscontentment(Goals);

                        for (int i = 0; i < ActionPerLevel.Length; i++)
                        {
                            this.BestActionSequence[i] = ActionPerLevel[i];
                        }
                    }

                    CurrentDepth -= 1;
                    continue;
                }

                action = Models[CurrentDepth].GetNextAction();

                if (action != null && action.CanExecute())
                {
                    Models[CurrentDepth + 1] = Models[CurrentDepth].GenerateChildWorldModel();
                    action.ApplyActionEffects(Models[CurrentDepth + 1]);
                    ActionPerLevel[CurrentDepth] = action;
                    CurrentDepth += 1;
                }
                else
                {
                    CurrentDepth -= 1;
                }
            }

            this.TotalProcessingTime         += Time.deltaTime;
            TotalActionCombinationsProcessed += processedActions;
            return(this.BestAction);
        }
Beispiel #2
0
        public Action ChooseAction()
        {
            var processedActions = 0;

            var startTime = Time.realtimeSinceStartup;

            BestAction = null;
            var returnAction = BestAction;

            BestDiscontentmentValue = float.MaxValue;

            while (CurrentDepth >= 0)   // processedActions < ActionCombinationsProcessedPerFrame
            {
                if (processedActions >= ActionCombinationsProcessedPerFrame)
                {
                    return(null);
                }
                if (CurrentDepth >= MAX_DEPTH)
                {
                    processedActions++;
                    CurrentValue = Models[CurrentDepth].CalculateDiscontentment(Goals);

                    if (CurrentValue < BestDiscontentmentValue)
                    {
                        BestDiscontentmentValue = CurrentValue;
                        BestAction         = ActionPerLevel[0];
                        BestActionSequence = ActionPerLevel.ToArray();
                    }
                    CurrentDepth -= 1;
                    continue;
                }

                Action nextAction = Models[CurrentDepth].GetNextAction();

                if (nextAction != null)
                {
                    var child = Models[CurrentDepth].GenerateChildWorldModel();
                    nextAction.ApplyActionEffects(child);
                    Models[CurrentDepth + 1]     = child;
                    ActionPerLevel[CurrentDepth] = nextAction;
                    CurrentDepth++;
                }
                else
                {
                    CurrentDepth--;
                }
            }

            TotalActionCombinationsProcessed += processedActions;
            this.TotalProcessingTime         += Time.realtimeSinceStartup - startTime;
            this.InProgress = false;
            return(this.BestAction);
        }
        public Action ChooseAction()
        {
            var processedActions = 0;
            var combinations     = 0;

            var startTime = Time.realtimeSinceStartup;

            float bestValue = float.MaxValue;

            while (CurrentDepth >= 0)
            {
                if (CurrentDepth >= MAX_DEPTH)
                {
                    var currentValue = Models[CurrentDepth].CalculateDiscontentment(Goals);
                    if (currentValue < bestValue)
                    {
                        bestValue  = currentValue;
                        BestAction = ActionPerLevel[0];
                        BestDiscontentmentValue = currentValue;
                        ActionPerLevel.CopyTo(BestActionSequence, 0);
                    }
                    CurrentDepth -= 1;
                    combinations += 1;
                    TotalActionCombinationsProcessed += 1;
                    continue;
                }

                //if (combinations >= ActionCombinationsProcessedPerFrame) return this.BestAction;

                var NextAction = Models[CurrentDepth].GetNextAction();
                if (NextAction != null)
                {
                    Models[CurrentDepth + 1] = Models[CurrentDepth].GenerateChildWorldModel();
                    NextAction.ApplyActionEffects(Models[CurrentDepth + 1]);
                    ActionPerLevel[CurrentDepth] = NextAction;
                    CurrentDepth     += 1;
                    processedActions += 1;
                }
                else
                {
                    CurrentDepth -= 1;
                }
            }

            this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
            this.InProgress           = false;

            return(this.BestAction);
        }