Skip to content

Commit

Permalink
Update SymuBeliefAndInfluence
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed May 17, 2020
1 parent d576782 commit 5c86911
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ namespace SymuBeliefsAndInfluence.Classes
{
public class ExampleEnvironment : SymuEnvironment
{
public byte KnowledgeCount { get; set; } = 2;
public byte WorkersCount { get; set; } = 5;
public byte InfluencersCount { get; set; } = 2;
public byte Knowledge { get; set; } = 0;
public byte KnowledgeCount { get; set; } = 2;
public List<Knowledge> Knowledges { get; private set; }
public List<InfluencerAgent> Influencers { get; } = new List<InfluencerAgent>();
public SimpleHumanTemplate InfluencerTemplate { get; } = new SimpleHumanTemplate();
Expand All @@ -54,15 +53,15 @@ public override void SetModelForAgents()
Organization.Models.InteractionSphere.RelativeActivityWeight = 0;
Organization.Models.InteractionSphere.RelativeKnowledgeWeight = 0.25F;
Organization.Models.InteractionSphere.SocialDemographicWeight = 0.25F;
// Knowledge
// KnowledgeCount are added for tasks initialization
// Adn Beliefs are created based on knowledge
Knowledges = new List<Knowledge>();
for (var i = 0; i < KnowledgeCount; i++)
{
// knowledge length of 10 is arbitrary in this example
var knowledge = new Knowledge((ushort)i, i.ToString(), 10);
WhitePages.Network.AddKnowledge(knowledge);
Knowledges.Add(knowledge);
//Beliefs are created based on knowledge
}
#endregion

Expand All @@ -77,12 +76,12 @@ public override void SetModelForAgents()
CommunicationMediums.Email;
InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.HasInitialBelief = true;
InfluencerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMin = 0;
InfluencerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMax = 0.1F;
InfluencerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMax = 0;

for (var j = 0; j < InfluencersCount; j++)
{
var actor = new InfluencerAgent(Organization.NextEntityIndex(), this);
//Beliefs are added with knowledge
//Beliefs are added with knowledge based on DefaultBeliefLevel of the influencer cognitive template
SetKnowledge(actor, Knowledges);
Influencers.Add(actor);
agentIds.Add(actor.Id);
Expand All @@ -97,11 +96,12 @@ public override void SetModelForAgents()
WorkerTemplate.Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
WorkerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMin = 0;
WorkerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 0.1F;
WorkerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 0F;
WorkerTemplate.Cognitive.TasksAndPerformance.CanPerformTaskOnWeekEnds = true;
for (var j = 0; j < WorkersCount; j++)
{
var actor = new PersonAgent(Organization.NextEntityIndex(), this);
//Beliefs are added with knowledge
//Beliefs are added with knowledge based on DefaultBeliefLevel of the worker cognitive template
SetKnowledge(actor, Knowledges);
agentIds.Add(actor.Id);
}
Expand Down
5 changes: 3 additions & 2 deletions Symu examples/SymuBeliefsAndInfluence/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private void LoadSettings()

InfluencerBeliefLevel.Items.AddRange(BeliefLevelService.GetNames());
InfluencerBeliefLevel.SelectedItem = BeliefLevelService.GetName(OrganizationEntity.Templates.Human.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel);
MandatoryRatio.Text = _environment.Model.MandatoryRatio.ToString();
}

protected override void SetUpOrganization()
Expand All @@ -89,7 +90,7 @@ private void Button1_Click(object sender, EventArgs e)
#region influencer
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.HasBelief = HasBeliefs.Checked;
_environment.InfluencerTemplate.Cognitive.MessageContent.CanSendBeliefs = CanSendBeliefs.Checked;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel= BeliefLevelService.GetValue(InfluencerBeliefLevel.SelectedText);
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel= BeliefLevelService.GetValue(InfluencerBeliefLevel.SelectedItem.ToString());

#endregion

Expand Down Expand Up @@ -235,7 +236,7 @@ private void tbKnowledge_TextChanged(object sender, EventArgs e)
{
try
{
_environment.Knowledge = byte.Parse(tbKnowledge.Text);
_environment.KnowledgeCount = byte.Parse(tbKnowledge.Text);
tbKnowledge.BackColor = SystemColors.Window;
}
catch (FormatException)
Expand Down
192 changes: 188 additions & 4 deletions Symu examples/SymuBeliefsAndInfluenceTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
using SymuEngine.Common;
using SymuEngine.Engine;
using SymuEngine.Environment;
using SymuEngine.Repository.Networks.Beliefs;
using SymuEngine.Repository.Networks.Knowledges;
using SymuTools;

#endregion

Expand All @@ -31,7 +33,7 @@ namespace SymuBeliefsAndInfluenceTests
[TestClass]
public class IntegrationTests
{
private const int NumberOfSteps = 31; // 2 organizationFlexibility computations
private const int NumberOfSteps = 61; // 3 IterationResult computations
private readonly ExampleEnvironment _environment = new ExampleEnvironment();
private readonly OrganizationEntity _organization = new OrganizationEntity("1");
private readonly SimulationEngine _simulation = new SimulationEngine();
Expand All @@ -46,10 +48,192 @@ public void Initialize()
NumberOfSteps = NumberOfSteps
};
_simulation.AddScenario(scenario);
_environment.TimeStep.Type = TimeStepType.Daily;
_organization.Models.Generator = RandomGenerator.RandomUniform;


}
/// <summary>
/// No belief
/// </summary>
[TestMethod]
public void NoBeliefTest()
{
_environment.KnowledgeCount= 0;
_simulation.Process();
Assert.AreEqual(0, _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Sum);
Assert.AreEqual(100, _environment.IterationResult.OrganizationFlexibility.Triads.First().Density);
}
/// <summary>
/// Agents don't have beliefs
/// </summary>
[TestMethod]
public void HasBeliefsTest()
{
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.HasBelief = false;
_environment.WorkerTemplate.Cognitive.KnowledgeAndBeliefs.HasBelief = false;
_simulation.Process();
Assert.AreEqual(0, _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Sum);
Assert.AreEqual(100, _environment.IterationResult.OrganizationFlexibility.Triads.First().Density);
}
/// <summary>
/// No Influencers
/// </summary>
[TestMethod]
public void NoInfluencerTest()
{
_environment.InfluencersCount = 0;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Influencers can't send beliefs
/// Belief should not change
/// </summary>
[TestMethod]
public void CantSendBeliefsTest()
{
_environment.InfluencerTemplate.Cognitive.MessageContent.CanSendBeliefs= false;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Influencers don't have the minimum belief to send
/// Belief should not change
/// Strongly Disagree
/// </summary>
[TestMethod]
public void MinimumBeliefsToSendTest()
{
_environment.InfluencerTemplate.Cognitive.MessageContent.MinimumBeliefToSendPerBit= 1;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel = BeliefLevel.StronglyDisagree;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Influencers have the minimum belief to send, but can't send any belief
/// Belief should not change
/// </summary>
[TestMethod]
public void BeliefBitsToSendTest()
{
_environment.InfluencerTemplate.Cognitive.MessageContent.MinimumNumberOfBitsOfBeliefToSend = 0;
_environment.InfluencerTemplate.Cognitive.MessageContent.MaximumNumberOfBitsOfBeliefToSend= 0;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel = BeliefLevel.StronglyDisagree;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Influencers don't have the minimum belief to send
/// Belief should not change
/// Strongly agree
/// </summary>
[TestMethod]
public void MinimumBeliefsToSendTest2()
{
_environment.InfluencerTemplate.Cognitive.MessageContent.MinimumBeliefToSendPerBit = 1;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel = BeliefLevel.StronglyAgree;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Worker can't receive beliefs
/// Belief should not change
/// </summary>
[TestMethod]
public void CantReceiveBeliefsTest()
{
_environment.WorkerTemplate.Cognitive.MessageContent.CanReceiveBeliefs = false;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Influencers have no Influentialness
/// Belief should not change
/// </summary>
[TestMethod]
public void NoInfluentialnessTest()
{
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 0;
_simulation.Process();
CheckNoChange();
}
/// <summary>
/// Workers have no influenceability
/// Belief should not change
/// </summary>
[TestMethod]
public void NoInfluenceabilityTest()
{
_environment.WorkerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMax = 0;
_simulation.Process();
CheckNoChange();
}

private void CheckNoChange()
{
Assert.AreEqual(_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Sum,
_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.Last().Sum);
Assert.AreEqual(_environment.IterationResult.OrganizationFlexibility.Triads.First().Density,
_environment.IterationResult.OrganizationFlexibility.Triads.Last().Density);
}

[TestMethod]
public void NoTaskBlockedTest()
{
_environment.Model.MandatoryRatio = 0;
_simulation.Process();
CheckNoChange();
var tasksDoneRatio = _environment.TimeStep.Step * _environment.WorkersCount < Constants.Tolerance
? 0
: _environment.IterationResult.Tasks.Total * 100 /
(_environment.TimeStep.Step * _environment.WorkersCount);
Assert.AreEqual(100, tasksDoneRatio);
}
/// <summary>
/// Influencers strongly disagree
/// Belief should decrease
/// Triads should increase
/// </summary>
[TestMethod]
public void StronglyDisagreeTest()
{
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 1;
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMin = 1;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel= BeliefLevel.StronglyDisagree;
_simulation.Process();
Assert.IsTrue(_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Sum > _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.Last().Sum);
Assert.IsTrue(_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Mean > _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.Last().Mean);
Assert.IsTrue(_environment.IterationResult.OrganizationFlexibility.Triads.First().Density < _environment.IterationResult.OrganizationFlexibility.Triads.Last().Density);
}

/// <summary>
/// Influencers strongly agree
/// Belief should increase
/// Triads should increase
/// </summary>
[TestMethod]
public void StronglyAgreeTest()
{
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 1;
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMin = 1;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel = BeliefLevel.StronglyAgree;
_simulation.Process();
Assert.IsTrue(_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Sum < _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.Last().Sum);
Assert.IsTrue(_environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.First().Mean < _environment.IterationResult.OrganizationKnowledgeAndBelief.Beliefs.Last().Mean);
Assert.IsTrue(_environment.IterationResult.OrganizationFlexibility.Triads.First().Density < _environment.IterationResult.OrganizationFlexibility.Triads.Last().Density);
}

/// <summary>
/// Influencers strongly agree
/// Belief should increase
/// Triads should increase
/// </summary>
[TestMethod]
public void NeitherAgreeNorDisagreeTest()
{
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMax = 1;
_environment.InfluencerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMin = 1;
_environment.InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel = BeliefLevel.NeitherAgreeNorDisagree;
_simulation.Process();
CheckNoChange();
}
}
}
7 changes: 6 additions & 1 deletion Symu source code/SymuEngine/Classes/Agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ public Bits FilterKnowledgeToSend(ushort knowledgeId, byte knowledgeBit, Communi
public Bits FilterBeliefToSend(ushort beliefId, byte beliefBit, CommunicationTemplate channel)
{
// If don't have belief, can't send belief or no belief asked
if (!Cognitive.KnowledgeAndBeliefs.HasBelief || !Cognitive.MessageContent.CanSendBeliefs || beliefId == 0)
if (!Cognitive.KnowledgeAndBeliefs.HasBelief || !Cognitive.MessageContent.CanSendBeliefs)// || beliefId == 0)
{
return null;
}
Expand Down Expand Up @@ -1345,6 +1345,11 @@ public virtual void GetNewTasks()
/// <remarks>Don't use TaskProcessor.Post directly to handle the OnBeforeTaskPost event</remarks>
public void Post(SymuTask task)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}

OnBeforePostTask(task);
if (!task.IsBlocked)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,15 @@ public float NextInfluenceability()
/// <param name="beliefLevel"></param>
public void Learn(ushort beliefId, Bits beliefBits, AgentId agentId, BeliefLevel beliefLevel)
{
if (beliefId == 0 || beliefBits == null)
{
return;
}
//if (beliefId == 0 || beliefBits == null)
//{
// return;
//}

if (beliefId > 0 && beliefBits == null)
if ( beliefBits == null)// && beliefId > 0)
{
throw new ArgumentNullException(nameof(beliefBits));
return;
//throw new ArgumentNullException(nameof(beliefBits));
}

// Learning From agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void CopyTo(KnowledgeAndBeliefs knowledgeAndBeliefs)
knowledgeAndBeliefs.HasKnowledge = HasKnowledge;
knowledgeAndBeliefs.KnowledgeThreshHoldForDoing = KnowledgeThreshHoldForDoing;
knowledgeAndBeliefs.HasInitialBelief = HasInitialBelief;
knowledgeAndBeliefs.DefaultBeliefLevel = DefaultBeliefLevel;
knowledgeAndBeliefs.HasBelief = HasBelief;
knowledgeAndBeliefs.BeliefThreshHoldForReacting = BeliefThreshHoldForReacting;
}
Expand Down Expand Up @@ -243,10 +244,11 @@ public void AddKnowledge(Knowledge knowledge, KnowledgeLevel level, float minimu
/// Get the agent Beliefs
/// </summary>
public AgentBeliefs Beliefs => HasBelief ? _network.NetworkBeliefs.GetAgentBeliefs(_id) : null;

/// <summary>
/// Default belief level use to create new belief during simulation
/// </summary>
public BeliefLevel DefaultBeliefLevel { get; set; }
public BeliefLevel DefaultBeliefLevel { get; set; } = BeliefLevel.NeitherAgreeNorDisagree;

/// <summary>
/// Initialize the beliefs of the agent based on the belief network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public Bits GetFilteredBeliefToSend(AgentBelief agentBelief, byte beliefBit, Com
var minKnowledge = Math.Max(MinimumBeliefToSendPerBit,
medium.Cognitive.MessageContent.MinimumBeliefToSendPerBit);
// Random knowledgeBits to send
if (minBits > maxBits)
{
minBits = maxBits ;
}
var lengthToSend = DiscreteUniform.SampleToByte(minBits, maxBits);
if (lengthToSend == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void CopyTo(TasksAndPerformance tasksAndPerformance)
TasksLimit.CopyTo(tasksAndPerformance.TasksLimit);
}


#region Agent Learning

public ModelEntity LearningModel { get; set; } = new ModelEntity();
Expand Down

0 comments on commit 5c86911

Please sign in to comment.