Skip to content

Commit

Permalink
Refactoring agent constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Jun 9, 2020
1 parent d1967a5 commit 60ec5a2
Show file tree
Hide file tree
Showing 79 changed files with 1,902 additions and 1,557 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

* Refactoring CommunicationTemplates
* Add LearningModel.OnAfterLearning event
* Refactoring Agent constructor and CommunicationTemplates
* Add CyclicalIsolation by [lmorisse](https://github.com/lmorisse)
* Add PromoterTemplate by [lmorisse](https://github.com/lmorisse)
* Add Iterations and Charts in SymuMessageAndTask example by [lmorisse](https://github.com/lmorisse)
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ A good branch name would be ISSUENUMBER-FEATURE-YOU-IMPLEMENT

### Get the test suite running
Support your developments in unit testing.
Make sure that the existing tests of the SymuEngineTests and SymuToolsTests projects still pass with your modifications.
Make sure that the existing tests of the SymuTests and SymuToolsTests projects still pass with your modifications.

### Implement your fix or feature

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,25 @@ public override void SetOrganization(OrganizationEntity organization)
IterationResult.OrganizationKnowledgeAndBelief.On = true;
IterationResult.OrganizationFlexibility.On = true;

// Interaction sphere setup
organization.Models.InteractionSphere.On = true;
organization.Models.InteractionSphere.SphereUpdateOverTime = true;
organization.Models.InteractionSphere.FrequencyOfSphereUpdate = TimeStepType.Monthly;
organization.Models.InteractionSphere.RandomlyGeneratedSphere = false;
// Interaction sphere setup
organization.Models.InteractionSphere.RelativeBeliefWeight = 0.5F;
organization.Models.InteractionSphere.RelativeActivityWeight = 0;
organization.Models.InteractionSphere.RelativeKnowledgeWeight = 0.25F;
organization.Models.InteractionSphere.SocialDemographicWeight = 0.25F;

Organization.Communication.Email.CostToReceiveLevel = GenericLevel.None;
Organization.Communication.Email.CostToSendLevel = GenericLevel.None;

SetDebug(false);
}

public override void SetModelForAgents()
public override void SetAgents()
{
base.SetModelForAgents();
base.SetAgents();

#region Common

Expand All @@ -90,57 +93,24 @@ public override void SetModelForAgents()

var agentIds = new List<AgentId>();

#region Influencer

InfluencerTemplate.Cognitive.InteractionPatterns.IsolationCyclicity = Cyclicity.None;
InfluencerTemplate.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
InfluencerTemplate.Cognitive.InteractionPatterns.AllowNewInteractions = false;
InfluencerTemplate.Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
#region Agents

for (var j = 0; j < InfluencersCount; j++)
{
var actor = new InfluencerAgent(Organization.NextEntityIndex(), this);
//Beliefs are added with knowledge based on DefaultBeliefLevel of the influencer cognitive template
SetKnowledge(actor, Knowledges);
var actor = new InfluencerAgent(Organization.NextEntityIndex(), this, InfluencerTemplate);
Influencers.Add(actor);
agentIds.Add(actor.Id);
}

#endregion

#region worker

WorkerTemplate.Cognitive.InteractionPatterns.IsolationCyclicity = Cyclicity.None;
WorkerTemplate.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
WorkerTemplate.Cognitive.InteractionPatterns.AllowNewInteractions = false;
WorkerTemplate.Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
WorkerTemplate.Cognitive.InternalCharacteristics.InfluentialnessRateMin = 0;
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 based on DefaultBeliefLevel of the worker cognitive template
SetKnowledge(actor, Knowledges);
var actor = new PersonAgent(Organization.NextEntityIndex(), this, WorkerTemplate);
agentIds.Add(actor.Id);
}

#endregion

WhitePages.Network.NetworkLinks.AddLinks(agentIds);
}

private void SetKnowledge(Agent actor, IReadOnlyList<Knowledge> knowledges)
{
for (var i = 0; i < KnowledgeCount; i++)
{
actor.KnowledgeModel.AddKnowledge(knowledges[i].Id,
KnowledgeLevel.FullKnowledge,
Organization.AgentTemplates.Human.Cognitive.InternalCharacteristics);
actor.BeliefsModel.AddBelief(knowledges[i].Id);
}
}
}
}
39 changes: 35 additions & 4 deletions Symu examples/SymuBeliefsAndInfluence/Classes/InfluencerAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#region using directives

using System;
using System.Collections.Generic;
using Symu.Classes.Agents;
using Symu.Classes.Agents.Models.CognitiveTemplates;
using Symu.Common;
using Symu.Environment;
using Symu.Messaging.Messages;
using Symu.Repository;
using Symu.Repository.Networks.Knowledges;

#endregion

Expand All @@ -22,12 +26,39 @@ namespace SymuBeliefsAndInfluence.Classes
public sealed class InfluencerAgent : Agent
{
public const byte ClassKey = SymuYellowPages.Actor;
public IEnumerable<Knowledge> Knowledges => ((ExampleEnvironment)Environment).Knowledges;

public InfluencerAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
public InfluencerAgent(ushort agentKey, SymuEnvironment environment, CognitiveArchitectureTemplate template) : base(
new AgentId(agentKey, ClassKey), environment, template)
{
SetCognitive(((ExampleEnvironment) Environment).InfluencerTemplate);
}

/// <summary>
/// Customize the cognitive architecture of the agent
/// After setting the Agent template
/// </summary>
protected override void SetCognitive()
{
base.SetCognitive();
Cognitive.InteractionPatterns.IsolationCyclicity = Cyclicity.None;
Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
Cognitive.InteractionPatterns.AllowNewInteractions = false;
Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
}

/// <summary>
/// Customize the models of the agent
/// After setting the Agent basics models
/// </summary>
protected override void SetModels()
{
base.SetModels();
foreach (var knowledge in Knowledges)
{
KnowledgeModel.AddKnowledge(knowledge.Id, KnowledgeLevel.FullKnowledge, Cognitive.InternalCharacteristics);
BeliefsModel.AddBelief(knowledge.Id, Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel);
}
}

/// <summary>
Expand Down
43 changes: 37 additions & 6 deletions Symu examples/SymuBeliefsAndInfluence/Classes/PersonAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Symu.Classes.Agents.Models.CognitiveTemplates;
using Symu.Classes.Blockers;
using Symu.Classes.Task;
using Symu.Common;
using Symu.Environment;
using Symu.Messaging.Messages;
using Symu.Repository;
Expand All @@ -29,17 +30,47 @@ public sealed class PersonAgent : Agent
{
public const byte ClassKey = SymuYellowPages.Actor;

public PersonAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
public PersonAgent(ushort agentKey, SymuEnvironment environment, CognitiveArchitectureTemplate template) : base(
new AgentId(agentKey, ClassKey), environment, template)
{
SetCognitive(Template);
}

private SimpleHumanTemplate Template => ((ExampleEnvironment) Environment).WorkerTemplate;
public List<Knowledge> Knowledges => ((ExampleEnvironment) Environment).Knowledges;
public IEnumerable<Knowledge> Knowledges => ((ExampleEnvironment) Environment).Knowledges;
public IEnumerable<AgentId> Influencers => ((ExampleEnvironment) Environment).Influencers.Select(x => x.Id);

/// <summary>
/// Customize the cognitive architecture of the agent
/// After setting the Agent template
/// </summary>
protected override void SetCognitive()
{
base.SetCognitive();

Cognitive.InteractionPatterns.IsolationCyclicity = Cyclicity.None;
Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
Cognitive.InteractionPatterns.AllowNewInteractions = false;
Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
Cognitive.InternalCharacteristics.InfluentialnessRateMin = 0;
Cognitive.InternalCharacteristics.InfluentialnessRateMax = 0F;
Cognitive.TasksAndPerformance.CanPerformTaskOnWeekEnds = true;
}

/// <summary>
/// Customize the models of the agent
/// After setting the Agent basics models
/// </summary>
protected override void SetModels()
{
base.SetModels();
foreach (var knowledge in Knowledges)
{
KnowledgeModel.AddKnowledge(knowledge.Id, KnowledgeLevel.FullKnowledge, Cognitive.InternalCharacteristics);
//Beliefs are added with knowledge based on DefaultBeliefLevel of the worker cognitive template
BeliefsModel.AddBelief(knowledge.Id, Cognitive.KnowledgeAndBeliefs.DefaultBeliefLevel);
}
}

public override void GetNewTasks()
{
var task = new SymuTask(Schedule.Step)
Expand Down
2 changes: 1 addition & 1 deletion Symu examples/SymuBeliefsAndInfluence/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void LoadSettings()
tbInfluencers.Text = _environment.InfluencersCount.ToString(CultureInfo.InvariantCulture);
tbKnowledge.Text = _environment.KnowledgeCount.ToString(CultureInfo.InvariantCulture);

HasBeliefs.Checked = OrganizationEntity.AgentTemplates.Human.Cognitive.KnowledgeAndBeliefs.HasBelief;
HasBeliefs.Checked = OrganizationEntity.Templates.Human.Cognitive.KnowledgeAndBeliefs.HasBelief;
ThresholdForReacting.Text = OrganizationEntity.Murphies.IncompleteBelief.ThresholdForReacting.ToString(CultureInfo.InvariantCulture);

#region Influencer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Symu.Classes.Organization;
using Symu.Classes.Scenario;
using Symu.Common;
using Symu.Engine;
using Symu.Repository.Networks.Beliefs;
using Symu.Tools;
Expand Down Expand Up @@ -290,7 +291,7 @@ public void FullWeightTest1()
/// <summary>
/// Influencers strongly disagree
/// Belief should decrease
/// Triads should increase
/// Triads should increase (but sometimes it takes more than just 60 steps
/// </summary>
[TestMethod]
public void StronglyDisagreeTest()
Expand All @@ -306,7 +307,7 @@ public void StronglyDisagreeTest()
_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 <
Assert.IsTrue(_environment.IterationResult.OrganizationFlexibility.Triads.First().Density <=
_environment.IterationResult.OrganizationFlexibility.Triads.Last().Density);
}

Expand Down
30 changes: 11 additions & 19 deletions Symu examples/SymuGroupAndInteraction/Classes/ExampleEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ExampleEnvironment : SymuEnvironment
public byte Knowledge { get; set; } = 0;
public byte Activities { get; set; } = 0;
public KnowledgeLevel KnowledgeLevel { get; set; } = KnowledgeLevel.FullKnowledge;
public List<Knowledge> Knowledges { get; }= new List<Knowledge>();

public override void SetOrganization(OrganizationEntity organization)
{
Expand All @@ -39,43 +40,41 @@ public override void SetOrganization(OrganizationEntity organization)

base.SetOrganization(organization);

Organization.AgentTemplates.Human.Cognitive.InteractionPatterns.IsolationCyclicity = Cyclicity.None;
Organization.AgentTemplates.Human.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
Organization.Models.InteractionSphere.SphereUpdateOverTime = true;
Organization.Models.InteractionSphere.On = true;
Organization.Models.Generator = RandomGenerator.RandomUniform;
IterationResult.OrganizationFlexibility.On = true;
SetDebug(false);
}

public override void SetModelForAgents()
public override void SetAgents()
{
base.SetModelForAgents();
var knowledges = new List<Knowledge>();
base.SetAgents();
var activities = new List<string>();
Knowledges.Clear();
for (var i = 0; i < GroupsCount; 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);
Knowledges.Add(knowledge);
activities.Add(i.ToString());
//Beliefs are created based on knowledge
}

for (var i = 0; i < GroupsCount; i++)
{
var group = new GroupAgent(Organization.NextEntityIndex(), this);

for (var j = 0; j < WorkersCount; j++)
{
var actor = new PersonAgent(Organization.NextEntityIndex(), this)
var actor = new PersonAgent(Organization.NextEntityIndex(), this, Organization.Templates.Human)
{
GroupId = group.Id
};
WhitePages.Network.AddMemberToGroup(actor.Id, 100, group.Id);
//Beliefs are added with knowledge
SetKnowledge(actor, knowledges, i);
SetKnowledge(actor, Knowledges, i);
SetActivity(actor.Id, activities, i, group.Id);
}
}
Expand All @@ -87,23 +86,16 @@ private void SetKnowledge(Agent actor, IReadOnlyList<Knowledge> knowledges, int
{
case 0:
// same Knowledge for all
actor.KnowledgeModel.AddKnowledge(knowledges[0].Id,
KnowledgeLevel,
Organization.AgentTemplates.Human.Cognitive.InternalCharacteristics);

WhitePages.Network.NetworkKnowledges.Add(actor.Id, knowledges[0].Id, KnowledgeLevel, actor.Cognitive.InternalCharacteristics);
break;
case 1:
// Knowledge is by group
actor.KnowledgeModel.AddKnowledge(knowledges[i].Id,
KnowledgeLevel,
Organization.AgentTemplates.Human.Cognitive.InternalCharacteristics);
WhitePages.Network.NetworkKnowledges.Add(actor.Id, knowledges[i].Id, KnowledgeLevel, actor.Cognitive.InternalCharacteristics);
break;
case 2:
// Knowledge is randomly defined for agentId
var index = DiscreteUniform.Sample(0, GroupsCount - 1);
actor.KnowledgeModel.AddKnowledge(knowledges[index].Id,
KnowledgeLevel,
Organization.AgentTemplates.Human.Cognitive.InternalCharacteristics);
WhitePages.Network.NetworkKnowledges.Add(actor.Id, knowledges[index].Id, KnowledgeLevel, actor.Cognitive.InternalCharacteristics);
break;
}
}
Expand Down
5 changes: 2 additions & 3 deletions Symu examples/SymuGroupAndInteraction/Classes/GroupAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#region using directives

using Symu.Classes.Agents;
using Symu.Classes.Agents.Models.CognitiveTemplates;
using Symu.Environment;

#endregion
Expand All @@ -21,10 +22,8 @@ public sealed class GroupAgent : Agent
public const byte ClassKey = 1;

public GroupAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
new AgentId(agentKey, ClassKey), environment)
{
SetCognitive(Environment.Organization.AgentTemplates.Standard);
}
}
}

0 comments on commit 60ec5a2

Please sign in to comment.