/// <summary> /// Algorithm that will execute with open minded commitment to the current selected intention /// </summary> /// <param name="obj">The cancellation token for the task</param> private async Task RunWithOpenMindedCommitment(object obj) { var token = (CancellationToken)obj; //(checked token from time to time for cancelation to gracefully stop thread) while (!token.IsCancellationRequested) { BeliefRevision(); GenerateOptions(); Deliberate(); if (CurrentIntentions.Any()) { var plan = MakePlan(); while (plan != null && plan.HasSteps() && !IntentionSucceeded() && !IntentionImpossible() && !token.IsCancellationRequested) { var step = plan.GetFirstStep(); await Execute(step); plan.FinishStep(step); BeliefRevision(); if (NeedsReconsideration()) { Deliberate(); } if (plan.HasSteps() && !Sound(plan)) { plan = MakePlan(); } } } //before looping again, sleep 100ms Thread.Sleep(100); } }
/// <summary> /// Filter the desires to select a new current intention /// </summary> /// <param name="potentialDesires">Potential desires to filter out and use for intentions</param> private void Filter(IEnumerable <Desire <TAction, TAgent, TEnvironment> > potentialDesires) { //choose strongest achievable desire and add it as a base intention CurrentIntentions.Clear(); potentialDesires = potentialDesires.OrderByDescending(y => y.Strength); //now add intentions that do not conflict foreach (var potentialDesire in potentialDesires) { if (CurrentIntentions.All(x => x.IsCompatibleWith(potentialDesire))) { CurrentIntentions.Add(new Intention <TAction, TAgent, TEnvironment>(potentialDesire)); } } }
/// <summary> /// Initializes the agent /// </summary> /// <param name="desires">The initial list of desires of the agent</param> /// <param name="intentions">The initial current intentions of the agent</param> /// <returns>A task in which the agent loop is run</returns> public virtual Task Init(List <Desire <TAction, TAgent, TEnvironment> > desires, List <Intention <TAction, TAgent, TEnvironment> > intentions) { if (desires != null) { Desires.AddRange(desires); foreach (var desire in desires) { desire.Agent = this; } } if (intentions != null) { CurrentIntentions.AddRange(intentions); } return(StartRun()); }
/// <summary> /// Does our current intention needs reconsideration after we updated our beliefs /// </summary> /// <returns>True if we need to reconsider the current intention, false otherwise</returns> protected override bool NeedsReconsideration() { var currentStrength = CurrentIntentions.First().GetStrength(); return(Desires.Any(x => x.Strength >= currentStrength * 2)); }
/// <summary> /// Check to see if our current intention impossible? /// </summary> /// <returns>True if our current intention impossible to achieve, false otherwise</returns> private bool IntentionImpossible() => !CurrentIntentions.All(x => x.IsAchievable());
/// <summary> /// Check to see if our current intention has succeeded already /// </summary> /// <returns>True if we succeeded already in our current intention, false otherwise</returns> private bool IntentionSucceeded() => CurrentIntentions.All(x => x.IsFulfilled());