Skip to content

Commit

Permalink
Update BlockerCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Jun 2, 2020
1 parent 981c77d commit 4268ac0
Show file tree
Hide file tree
Showing 26 changed files with 552 additions and 354 deletions.
1 change: 0 additions & 1 deletion Symu examples/SymuMessageAndTaskTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ public void CostOfMessageTest()
_symu.Process();
Assert.IsTrue(_environment.IterationResult.Capacity > _environment.IterationResult.Tasks.Total);
Assert.AreEqual(0, _environment.IterationResult.Tasks.AverageDone);
Assert.AreEqual(_environment.IterationResult.Tasks.Weight, _environment.IterationResult.Tasks.Total);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions Symu examples/SymuMurphiesAndBlockers/Home.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Symu examples/SymuMurphiesAndBlockers/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void LoadSettings()
case "Changing Information":
//item.Checked = murphies.ChangingInformation.On;
break;
case "Incorrect information":
case "Incorrectness information":
//item.Checked = murphies.IncorrectInformation.On;
break;
case "Communication breakdowns":
Expand Down Expand Up @@ -634,7 +634,7 @@ private void lvMurphies_ItemChecked(object sender, ItemCheckedEventArgs e)
break;
case "Changing Information":
break;
case "Incorrect information":
case "Incorrectness information":
break;
case "Communication breakdowns":
break;
Expand Down
10 changes: 10 additions & 0 deletions Symu source code/Symu/Classes/Agents/Agent.Act.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Symu.Classes.Task;
using Symu.Common;
Expand Down Expand Up @@ -41,6 +42,15 @@ public void Act(Message message)
throw new ArgumentNullException(nameof(message));
}

// agent ask Environment to be in a SplitStep mode
// message is managed directly
if (message.Subject == SymuYellowPages.SplitStep)
{
var splitStep = message.Attachments.First as SplitStep;
splitStep?.Step();
return;
}

if (Cognitive.TasksAndPerformance.CanPerformTask && message.Medium != CommunicationMediums.System)
{
// Switch message into a task in the task manager
Expand Down
26 changes: 13 additions & 13 deletions Symu source code/Symu/Classes/Agents/Agent.TaskManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void ReplyHelpIncomplete(SymuTask task, Blocker blocker, CommunicationMed

// Take some time to learn, allocate this time on KeyActivity
ImpactOfTheCommunicationMediumOnTimeSpent(medium, false, task.KeyActivity);
task.Blockers.Recover(blocker, internalHelp ? BlockerResolution.Internal : BlockerResolution.External);
task.Recover(blocker, internalHelp ? BlockerResolution.Internal : BlockerResolution.External);
}

/// <summary>
Expand Down Expand Up @@ -550,23 +550,23 @@ protected virtual void TryRecoverBlocker(SymuTask task, Blocker blocker)
}

var impact = murphy.NextGuess();
if (impact > task.Incorrect)
if (impact > task.Incorrectness)
{
task.Incorrect = impact;
task.Incorrectness = impact;
}

if (task.Incorrect == ImpactLevel.Blocked)
if (task.Incorrectness == ImpactLevel.Blocked)
{
//Agent decide to cancel the task
TaskProcessor.Cancel(task);
task.Blockers.Cancel(blocker);
task.CancelBlocker(blocker);
}
else
{
task.Weight *= murphy.NextImpactOnTimeSpent();
if (blocker != null)
{
task.Blockers.Recover(blocker, resolution);
task.Recover(blocker, resolution);
}
}
}
Expand Down Expand Up @@ -689,7 +689,7 @@ protected virtual void CheckBlockerIncompleteKnowledge(SymuTask task, ushort kno
if (!mandatoryOk)
{
// mandatoryCheck is false => Task is blocked
var blocker = task.Blockers.Add(Murphy.IncompleteKnowledge, Schedule.Step, knowledgeId, mandatoryIndex);
var blocker = task.Add(Murphy.IncompleteKnowledge, Schedule.Step, knowledgeId, mandatoryIndex);
TryRecoverBlockerIncompleteKnowledge(task, blocker);
}
else if (!requiredOk)
Expand Down Expand Up @@ -719,7 +719,7 @@ protected virtual void CheckBlockerIncompleteKnowledge(SymuTask task, ushort kno

RecoverBlockerIncompleteByGuessing(task, blocker, Environment.Organization.Murphies.IncompleteKnowledge,
resolution);
if (task.Incorrect == ImpactLevel.Blocked)
if (task.Incorrectness == ImpactLevel.Blocked)
{
return;
}
Expand Down Expand Up @@ -941,7 +941,7 @@ public void CheckBlockerIncompleteBelief(SymuTask task, ushort knowledgeId)
}

// mandatoryScore is not enough => agent don't want to do the task, the task is blocked
var blocker1 = task.Blockers.Add(Murphy.IncompleteBelief, Schedule.Step, knowledgeId, mandatoryIndex);
var blocker1 = task.Add(Murphy.IncompleteBelief, Schedule.Step, knowledgeId, mandatoryIndex);
TryRecoverBlockerIncompleteBelief(task, blocker1);
}

Expand Down Expand Up @@ -976,9 +976,9 @@ public void CheckRiskAversion(SymuTask task, ushort knowledgeId)
}

// Prevent the agent from acting on a particular belief
var blocker = task.Blockers.Add(Murphy.IncompleteBelief, Schedule.Step, knowledgeId, mandatoryIndex);
var blocker = task.Add(Murphy.IncompleteBelief, Schedule.Step, knowledgeId, mandatoryIndex);
TaskProcessor.Cancel(task);
task.Blockers.Cancel(blocker);
task.CancelBlocker(blocker);
}

/// <summary>
Expand Down Expand Up @@ -1072,7 +1072,7 @@ public void RecoverBlockerIncompleteBeliefByGuessing(SymuTask task, Blocker bloc

RecoverBlockerIncompleteByGuessing(task, blocker, Environment.Organization.Murphies.IncompleteBelief,
BlockerResolution.Guessing);
if (task.Incorrect == ImpactLevel.Blocked)
if (task.Incorrectness == ImpactLevel.Blocked)
{
return;
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ public bool CheckBlockerIncompleteInformation(SymuTask task)
return false;
}

var blocker = task.Blockers.Add(Murphy.IncompleteInformation, Schedule.Step);
var blocker = task.Add(Murphy.IncompleteInformation, Schedule.Step);
TryRecoverBlocker(task, blocker);

return true;
Expand Down
2 changes: 1 addition & 1 deletion Symu source code/Symu/Classes/Agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void Start()
return;
}

TaskProcessor = new TaskProcessor(Cognitive.TasksAndPerformance.TasksLimit);
TaskProcessor = new TaskProcessor(Cognitive.TasksAndPerformance.TasksLimit, Environment.Debug);
OnAfterTaskProcessorStart();
}

Expand Down
67 changes: 67 additions & 0 deletions Symu source code/Symu/Classes/Agents/SplitStep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#region Licence

// Description: Symu - Symu
// Website: https://symu.org
// Copyright: (c) 2020 laurent morisseau
// License : the program is distributed under the terms of the GNU General Public License

#endregion

#region using directives

using System;
using Symu.Environment;
using Symu.Messaging.Messages;
using Symu.Repository;

#endregion

namespace Symu.Classes.Agents
{
/// <summary>
/// SplitStep is a special class which allow an agent to split a step into a number of steps
/// using messages between agent and its environment
/// </summary>
public class SplitStep
{
private readonly AgentId _agentId;
private readonly SymuEnvironment _environment;
private const byte NumberOfSplits = 10;
public float ActualRatio => (float)_actualSplit / NumberOfSplits;
private byte _actualSplit;
/// <summary>
/// EventHandler triggered after the message is received by agent to act during the actual split and call the next split
/// This event is triggered in the Agent.Act() method
/// </summary>
public event EventHandler OnStep;


public SplitStep(SymuEnvironment environment, AgentId agentId)
{
_environment = environment;
_agentId = agentId;
}

/// <summary>
/// If NumberOfSplits is reached,
/// </summary>
/// <returns>false if NumberOfSplits is reached</returns>
/// <returns>true if message is send to agent</returns>
public bool NextSplit()
{
if (_actualSplit >= NumberOfSplits)
{
return false;
}
_actualSplit++;
var message = new Message(_agentId, _agentId, MessageAction.Handle, SymuYellowPages.SplitStep, this, CommunicationMediums.System);
_environment.SendAgent(message);
return true;
}

public void Step()
{
OnStep?.Invoke(this, null);
}
}
}
97 changes: 14 additions & 83 deletions Symu source code/Symu/Classes/Blockers/BlockerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Symu.Results.Blocker;

#endregion

Expand All @@ -26,51 +25,24 @@ public class BlockerCollection
/// <summary>
/// Number of blockers In Progress
/// </summary>
public BlockerResult Result { get; set; } = new BlockerResult();
//public BlockerResult Result { get; set; } = new BlockerResult();

public List<Blocker> List { get; } = new List<Blocker>();
public bool IsBlocked => List.Any();

/// <summary>
/// Add a blocker with two parameters
/// And follow it in the IterationResult if FollowBlocker is true
/// </summary>
/// <param name="type">type of the blocker</param>
/// <param name="step">step of creation of the blocker</param>
/// <param name="parameter1"></param>
/// <param name="parameter2"></param>
public Blocker Add(int type, ushort step, object parameter1, object parameter2)
public Blocker Add(Blocker blocker)
{
var blocker = new Blocker(type, step, parameter1, parameter2);
return Add(blocker);
List.Add(blocker);
return blocker;
}

/// <summary>
/// Add a blocker with one parameter
/// And follow it in the IterationResult if FollowBlocker is true
/// </summary>
/// <param name="type">type of the blocker</param>
/// <param name="step">step of creation of the blocker</param>
/// <param name="parameter"></param>
[Obsolete]
public Blocker Add(int type, ushort step, object parameter)
{
var blocker = new Blocker(type, step, parameter);
return Add(blocker);
}

private Blocker Add(Blocker blocker)
{
SetBlockerInProgress();
List.Add(blocker);
return blocker;
}

/// <summary>
/// Add a blocker without parameter
/// And follow it in the IterationResult if FollowBlocker is true
/// </summary>
/// <param name="type">type of the blocker</param>
/// <param name="step">step of creation of the blocker</param>
[Obsolete]
public Blocker Add(int type, ushort step)
{
var blocker = new Blocker(type, step);
Expand All @@ -82,8 +54,7 @@ public Blocker Add(int type, ushort step)
/// And update IterationResult if FollowBlocker is true
/// </summary>
/// <param name="blocker"></param>
/// <param name="resolution"></param>
public void Recover(Blocker blocker, BlockerResolution resolution)
public void Recover(Blocker blocker)
{
if (!Contains(blocker))
{
Expand All @@ -95,29 +66,24 @@ public void Recover(Blocker blocker, BlockerResolution resolution)
{
Remove(blocker);
}

SetBlockerDone(resolution);
}

/// <summary>
/// Cancel an existing blocker from a task
/// CancelBlocker an existing blocker from a task
/// And update IterationResult if FollowBlocker is true
/// </summary>
/// <param name="blocker"></param>
public void Cancel(Blocker blocker)
/// <returns>true if blocker has been removed</returns>
public bool Cancel(Blocker blocker)
{
if (!Contains(blocker))
if (blocker == null || !Contains(blocker))
{
// Blocker may have been already cancelled
return;
return false;
}

if (blocker != null)
{
Remove(blocker);
}

SetBlockerCancelled();
Remove(blocker);
return true;
}

/// <summary>
Expand Down Expand Up @@ -192,40 +158,5 @@ public bool Contains(Blocker blocker)
{
return List.Contains(blocker);
}

public void SetBlockerInProgress()
{
Result.InProgress++;
}

public void SetBlockerDone(BlockerResolution resolution)
{
switch (resolution)
{
case BlockerResolution.Internal:
Result.InternalHelp++;
break;
case BlockerResolution.External:
Result.ExternalHelp++;
break;
case BlockerResolution.Guessing:
Result.Guess++;
break;
case BlockerResolution.Searching:
Result.Search++;
break;
default:
throw new ArgumentOutOfRangeException(nameof(resolution), resolution, null);
}

Result.Done++;
Result.InProgress--;
}

public void SetBlockerCancelled()
{
Result.Cancelled++;
Result.InProgress--;
}
}
}

0 comments on commit 4268ac0

Please sign in to comment.