Example #1
0
        public Action ChooseAction()
        {
            var processedActions = 0;

            this.ActionCombinationsThisFrame = 0;

            var startTime = Time.realtimeSinceStartup;

            //TODO: Implement

            //def planAction(worldModel, maxDepth)
            float currentValue;

            while (this.CurrentDepth >= 0 && this.ActionCombinationsThisFrame < this.ActionCombinationsProcessedPerFrame)
            {
                if (this.CurrentDepth >= MAX_DEPTH)
                {
                    currentValue = this.Models[this.CurrentDepth].CalculateDiscontentment(Goals);
                    if (currentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        this.BestAction = this.ActionPerLevel[0];
                        for (int i = 0; i < CurrentDepth; i++)
                        {
                            this.BestActionSequence[i] = this.ActionPerLevel[i];
                        }
                    }
                    this.CurrentDepth -= 1;
                    this.ActionCombinationsThisFrame++;
                    continue;
                }
                Action nextAction = this.Models[this.CurrentDepth].GetNextAction();
                if (nextAction != null)
                {
                    this.Models[this.CurrentDepth + 1] = this.Models[this.CurrentDepth].GenerateChildWorldModel();
                    nextAction.ApplyActionEffects(this.Models[this.CurrentDepth + 1]);
                    this.ActionPerLevel[this.CurrentDepth] = nextAction;
                    this.CurrentDepth += 1;
                    processedActions++;
                }
                else
                {
                    if (this.CurrentDepth == 0 && BestAction == null)
                    {
                        this.BestAction            = this.ActionPerLevel[0];
                        this.BestActionSequence[0] = this.BestAction;
                        break;
                    }
                    this.CurrentDepth -= 1;
                    this.ActionCombinationsThisFrame++;
                }
            }
            this.TotalActionCombinationsProcessed += processedActions;
            this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
            this.InProgress           = false;
            return(this.BestAction);
        }
        public Action ChooseAction()
        {
            var processedActions = 0;

            var startTime = Time.realtimeSinceStartup;

            while (this.CurrentDepth >= 0)
            {
                if (processedActions > this.ActionCombinationsProcessedPerFrame)
                {
                    this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
                    return(null);
                }

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

                    if (currentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        this.BestAction = this.ActionPerLevel[0];

                        for (int i = 0; i < this.ActionPerLevel.Length; i++)
                        {
                            this.BestActionSequence[i] = this.ActionPerLevel[i];
                        }
                    }
                    CurrentDepth     -= 1;
                    processedActions += 1;
                    this.TotalActionCombinationsProcessed += processedActions;
                    continue;
                }

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

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

            this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
            this.InProgress           = false;
            return(this.BestAction);
        }
Example #3
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);
        }
Example #4
0
 //Simulates one or many playouts of an action in a state. Only applies to swordattack as all other actions are not stochastic.
 //Afterwards merges them in MergeStates to average out the results.
 protected IWorldModel StochasticPlayout(Action action, IWorldModel prevState, int n)
 {
     if (action.Name.Contains("SwordAttack") && n > 0)
     {
         IWorldModel[] testStates = new WorldModelFEAR[n];
         //IWorldModel[] testStates = new WorldModel[n];
         for (int i = 0; i < n; i++)
         {
             TotalPlayouts++;
             testStates[i] = prevState.GenerateChildWorldModel();
             action.ApplyActionEffects(testStates[i]);
         }
         prevState = MergeStates(testStates, (SwordAttack)action);
     }
     else
     {
         TotalPlayouts++;
         prevState = prevState.GenerateChildWorldModel();
         action.ApplyActionEffects(prevState);
     }
     return(prevState);
 }
Example #5
0
        public Action ChooseAction()
        {
            var processedActions = 0;

            var startTime = Time.realtimeSinceStartup;

            while (this.CurrentDepth >= 0)
            {
                if (processedActions > this.ActionCombinationsProcessedPerFrame)
                {
                    this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
                    return(null);
                }

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

                    if (currentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        this.BestAction = this.ActionPerLevel[0];
                        this.ActionPerLevel.CopyTo(this.BestActionSequence, 0);
                    }

                    this.CurrentDepth -= 1;
                    continue;
                }

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

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

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

            var startTime = Time.realtimeSinceStartup;

            //TODO: Implement
            //throw new NotImplementedException();
            float  currentValue = 0.0f;
            Action nextAction   = null;

            while (this.CurrentDepth >= 0)
            {
                if (this.CurrentDepth >= MAX_DEPTH)
                {
                    currentValue = this.Models[this.CurrentDepth].CalculateDiscontentment(this.Goals);

                    if (currentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        this.BestAction = this.Actions[this.CurrentDepth];
                        this.BestActionSequence[this.CurrentDepth] = this.Actions[this.CurrentDepth];
                    }
                    this.CurrentDepth -= 1;
                    continue;
                }
                if (processedActions < 3)
                {
                    nextAction = this.Models[this.CurrentDepth].GetNextAction();

                    if (nextAction != null)
                    {
                        this.Models[this.CurrentDepth + 1] = this.Models[this.CurrentDepth].GenerateChildWorldModel();
                        nextAction.ApplyActionEffects(this.Models[this.CurrentDepth + 1]);
                        processedActions++;
                        this.Actions[this.CurrentDepth] = nextAction;
                        this.CurrentDepth += 1;
                    }
                    else
                    {
                        this.CurrentDepth -= 1;
                    }
                }
            }
            this.TotalActionCombinationsProcessed = processedActions;
            this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
            this.InProgress           = false;
            return(this.BestAction);
        }
        public Action ChooseAction()
        {
            this.TotalActionCombinationsProcessed = 0;
            var startTime = Time.realtimeSinceStartup;

            while (this.CurrentDepth >= 0)
            {
                if (this.TotalActionCombinationsProcessed >= this.ActionCombinationsProcessedPerFrame)
                {
                    break;
                }
                this.TotalActionCombinationsProcessed++;

                if (this.CurrentDepth >= MAX_DEPTH)
                {
                    float CurrentValue = this.Models[this.CurrentDepth].CalculateDiscontentment(this.Goals);
                    if (CurrentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = CurrentValue;
                        this.BestAction         = this.ActionPerLevel[0];
                        this.BestActionSequence = (Action[])this.ActionPerLevel.Clone();
                    }
                    this.CurrentDepth--;
                    continue;
                }

                Action nextAction = this.Models[this.CurrentDepth].GetNextAction();
                if (nextAction != null)
                {
                    this.Models[this.CurrentDepth + 1] = this.Models[this.CurrentDepth].GenerateChildWorldModel();
                    nextAction.ApplyActionEffects(this.Models[this.CurrentDepth + 1]);
                    this.ActionPerLevel[this.CurrentDepth] = nextAction;
                    this.TotalActionCombinationsProcessed++;
                    this.CurrentDepth++;
                }
                else
                {
                    this.CurrentDepth--;
                }
            }

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

            var startTime = Time.realtimeSinceStartup;

            while (this.CurrentDepth >= 0)
            {
                if (this.CurrentDepth >= MAX_DEPTH)
                {
                    var currentValue = Models [CurrentDepth].CalculateDiscontentment(this.Goals);

                    if (currentValue < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        this.BestAction         = this.ActionPerLevel [0];
                        this.BestActionSequence = this.ActionPerLevel.ToArray();
                    }

                    this.CurrentDepth--;
                    this.TotalActionCombinationsProcessed++;
                    continue;
                }

                Action next = Models [CurrentDepth].GetNextAction();
                if (next != null)
                {
                    this.Models [this.CurrentDepth + 1] = Models [CurrentDepth].GenerateChildWorldModel();
                    next.ApplyActionEffects(this.Models [this.CurrentDepth + 1]);
                    this.Models [this.CurrentDepth + 1].CalculateNextPlayer();
                    this.ActionPerLevel [CurrentDepth] = next;
                    CurrentDepth++;
                }
                else
                {
                    CurrentDepth--;
                }
            }


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

            BestAction = null;

            while (CurrentDepth >= 0 && TotalActionCombinationsProcessed <= ActionCombinationsProcessedPerFrame)
            {
                processedActions++;
                float currentValue = Models[CurrentDepth].CalculateDiscontentment(Goals);

                if (CurrentDepth >= MAX_DEPTH)
                {
                    if (currentValue < BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = currentValue;
                        BestAction = ActionSequence[0];
                        this.ActionSequence.CopyTo(BestActionSequence, 0);
                    }

                    CurrentDepth--;
                    continue;
                }

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

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

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

            var startTime = Time.realtimeSinceStartup;

            //TODO: Implement
            //throw new NotImplementedException();
            //(slide: 99)

            float  value;
            Action nextAction = null;

            while (this.CurrentDepth >= 0)
            {
                ShuffleActions();

                if (this.InProgress == false || processedActions > this.ActionCombinationsProcessedPerFrame)
                {
                    return(null);
                }

                //quando ja tivermos 3 accoes, vamos escolher a melhor (permite a personagem anticipar os efeitos e planear as suas accoes)
                if (this.CurrentDepth >= MAX_DEPTH)
                {
                    value = this.Models[this.CurrentDepth].CalculateDiscontentment(this.Goals);

                    //diminuimos o descontentamento
                    if (value < this.BestDiscontentmentValue)
                    {
                        this.BestDiscontentmentValue = value;
                        this.BestAction         = this.ActionPerLevel[0];
                        this.BestActionSequence = this.ActionPerLevel.ToArray();   //substituimos a nova accao (que e melhor), na sequencia das melhores accoes
                    }
                    this.CurrentDepth -= 1;
                    continue;
                }
                //so processo 3 accoes: olho no maximo um futuro de 3 accoes

                //ve qual a proxima accao que pode ser executada ou da null se nao existirem mais accoes executaveis
                nextAction = this.Models[this.CurrentDepth].GetNextAction();

                if (nextAction != null)
                {
                    this.Models[this.CurrentDepth + 1] = this.Models[this.CurrentDepth].GenerateChildWorldModel(); //simula o novo estado do mundo consoante esta accao,
                    nextAction.ApplyActionEffects(this.Models[this.CurrentDepth + 1]);                             //para vermos que accoes vao estar disponiveis
                    processedActions++;
                    this.ActionPerLevel[this.CurrentDepth] = nextAction;                                           //adicionamos às accoes para depois vermos qual e melhor
                    this.CurrentDepth += 1;
                }
                //se nao houver mais accoes, diminuimos CurrentDepth para sairmos do ciclo
                else
                {
                    this.CurrentDepth -= 1;
                }
            }
            this.TotalActionCombinationsProcessed = processedActions;

            this.TotalProcessingTime += Time.realtimeSinceStartup - startTime;
            this.InProgress           = false;
            return(this.BestAction);
        }