Skip to content

Commit

Permalink
Update SymuBeliefsAndInfluence
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed May 14, 2020
1 parent f01a507 commit f6efb93
Show file tree
Hide file tree
Showing 12 changed files with 502 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using System.Collections.Generic;
using SymuEngine.Classes.Agents;
using SymuEngine.Classes.Agents.Models.CognitiveArchitecture;
using SymuEngine.Classes.Agents.Models.Templates;
using SymuEngine.Classes.Task;
using SymuEngine.Common;
using SymuEngine.Environment;
using SymuEngine.Messaging.Messages;
Expand All @@ -26,26 +28,49 @@ public class ExampleEnvironment : SymuEnvironment
{
public byte KnowledgeCount { get; set; } = 2;
public byte WorkersCount { get; set; } = 5;
public byte InfluencersCount { get; set; } = 5;
public byte Knowledge { get; set; } = 0;
public List<Knowledge> Knowledges { get; private set; }
public List<Knowledge> Knowledges { get; private set; }
public List<InfluenceurAgent> Influencers { get; } = new List<InfluenceurAgent>();
public SimpleHumanTemplate InfluencerTemplate { get; } = new SimpleHumanTemplate();
public SimpleHumanTemplate WorkerTemplate { get; } = new SimpleHumanTemplate();
public MurphyTask Model { get; } = new MurphyTask();

public override void SetModelForAgents()
{
base.SetModelForAgents();
Organization.Models.Generator = RandomGenerator.RandomUniform;

Organization.Templates.Human.Cognitive.InteractionPatterns.IsolationIsRandom = false;
Organization.Templates.Human.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
#region Influencer
InfluencerTemplate.Cognitive.InteractionPatterns.IsolationIsRandom = false;
InfluencerTemplate.Cognitive.InteractionPatterns.IsolationIsRandom = false;
InfluencerTemplate.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
InfluencerTemplate.Cognitive.InteractionPatterns.AllowNewInteractions = false;
InfluencerTemplate.Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;
InfluencerTemplate.Cognitive.KnowledgeAndBeliefs.HasInitialBelief = true;
InfluencerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMin = 0;
InfluencerTemplate.Cognitive.InternalCharacteristics.InfluenceabilityRateMax = 0.1F;
#endregion

#region worker
WorkerTemplate.Cognitive.InteractionPatterns.IsolationIsRandom = false;
WorkerTemplate.Cognitive.InteractionPatterns.IsolationIsRandom = false;
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 = 0.1F;
#endregion

Organization.Models.FollowGroupKnowledge = true;
Organization.Models.FollowGroupFlexibility= true;
Organization.Models.InteractionSphere.On = true;
Organization.Models.InteractionSphere.SphereUpdateOverTime = true;
Organization.Models.InteractionSphere.FrequencyOfSphereUpdate = TimeStepType.Monthly;
Organization.Models.InteractionSphere.RandomlyGeneratedSphere = false;
Organization.Templates.Human.Cognitive.InteractionPatterns.AllowNewInteractions = false;
Organization.Templates.Human.Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.Email;


Knowledges = new List<Knowledge>();
for (var i = 0; i < KnowledgeCount; i++)
{
Expand All @@ -70,6 +95,14 @@ public override void SetModelForAgents()
Organization.Models.InteractionSphere.RelativeKnowledgeWeight = 0F;
Organization.Models.InteractionSphere.SocialDemographicWeight = 0.5F;
WhitePages.Network.NetworkLinks.AddLinks(agentIds);

for (var j = 0; j < WorkersCount; j++)
{
var actor = new InfluenceurAgent(Organization.NextEntityIndex(), this);
//Beliefs are added with knowledge
SetKnowledge(actor, Knowledges);
Influencers.Add(actor);
}
}

private void SetKnowledge(Agent actor, IReadOnlyList<Knowledge> knowledges)
Expand Down
63 changes: 63 additions & 0 deletions Symu examples/SymuBeliefsAndInfluence/Classes/InfluencerAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#region Licence

// Description: Symu - SymuGroupAndInteraction
// 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 System.Linq;
using SymuEngine.Classes.Agents;
using SymuEngine.Classes.Agents.Models.CognitiveArchitecture;
using SymuEngine.Classes.Task;
using SymuEngine.Environment;
using SymuEngine.Messaging.Messages;
using SymuEngine.Repository;

#endregion

namespace SymuBeliefsAndInfluence.Classes
{
public sealed class InfluenceurAgent : Agent
{
public const byte ClassKey = SymuYellowPages.Actor;
public InfluenceurAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
{
SetCognitive(((ExampleEnvironment) Environment).InfluencerTemplate);
}

/// <summary>
/// This is where the main logic of the agent should be placed.
/// </summary>
/// <param name="message"></param>
public override void ActMessage(Message message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}

base.ActMessage(message);
switch (message.Subject)
{
case SymuYellowPages.Belief:
AskBelief(message);
break;
}
}

private void AskBelief(Message message)
{
var replyMessage = Message.ReplyMessage(message);
// In this reply message, agent will send back its own belief if he can send beliefs
// that will have an impact on the beliefs of the sender if he can receive beliefs
Reply(replyMessage);
}
}
}
80 changes: 18 additions & 62 deletions Symu examples/SymuBeliefsAndInfluence/Classes/PersonAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#region using directives

using System;
using System.Collections.Generic;
using System.Linq;
using SymuEngine.Classes.Agents;
using SymuEngine.Classes.Agents.Models.CognitiveArchitecture;
using SymuEngine.Classes.Agents.Models.Templates;
using SymuEngine.Classes.Task;
using SymuEngine.Environment;
using SymuEngine.Messaging.Messages;
using SymuEngine.Repository;
using SymuEngine.Repository.Networks.Knowledges;

#endregion

Expand All @@ -25,15 +28,16 @@ namespace SymuBeliefsAndInfluence.Classes
public sealed class PersonAgent : Agent
{
public const byte ClassKey = SymuYellowPages.Actor;
private readonly MurphyTask _model = new MurphyTask();

private MurphyTask Model => ((ExampleEnvironment) Environment).Model;
private SimpleHumanTemplate Template => ((ExampleEnvironment) Environment).WorkerTemplate;
public List<Knowledge> Knowledges => ((ExampleEnvironment) Environment).Knowledges;
public PersonAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
{
SetCognitive(Environment.Organization.Templates.Human);
_model.RequiredRatio = 0.2F;
_model.RequiredMandatoryRatio = 2F;
SetCognitive(Template);
Model.RequiredRatio = 0.2F;
Model.RequiredMandatoryRatio = 2F;
}

public override void GetNewTasks()
Expand All @@ -42,46 +46,29 @@ public override void GetNewTasks()
{
Weight = 1
};
task.SetKnowledgesBits(_model, ((ExampleEnvironment)Environment).Knowledges, 1);

var doTask = true;
foreach (var knowledgeId in task.KnowledgesBits.KnowledgeIds)
{
if (!CheckBelief(task, knowledgeId))
{
doTask = false;
break;
}
}

if (doTask)
{
TaskProcessor.Post(task);
}
task.SetKnowledgesBits(Model, Knowledges, 1);
}

/// <summary>
/// True if belief are ok to do the task
/// </summary>
/// <param name="task"></param>
/// <param name="knowledgeId"></param>
/// <param name="mandatoryScore"></param>
/// <param name="requiredScore"></param>
/// <param name="mandatoryIndex"></param>
/// <param name="requiredIndex"></param>
/// <returns></returns>
public bool CheckBelief(SymuTask task, ushort knowledgeId)
protected override void CheckBlockerBelief(SymuTask task, ushort knowledgeId, float mandatoryScore, float requiredScore, byte mandatoryIndex, byte requiredIndex)
{
if (task is null)
{
throw new ArgumentNullException(nameof(task));
}

var taskBits = task.KnowledgesBits.GetBits(knowledgeId);
float mandatoryScore = 0;
float requiredScore = 0;
byte mandatoryIndex = 0;
byte requiredIndex = 0;
Cognitive.KnowledgeAndBeliefs.CheckBelief(knowledgeId, taskBits, ref mandatoryScore, ref requiredScore,
ref mandatoryIndex, ref requiredIndex);
if (!(mandatoryScore < 0F))
{
return true;
task.Blockers.Add(0, TimeStep.Step);
return ;
}

// mandatoryScore is not enough => Worker don't want to do the task
Expand All @@ -96,37 +83,6 @@ public bool CheckBelief(SymuTask task, ushort knowledgeId)
};
SendToMany(teammates, MessageAction.Ask, SymuYellowPages.Belief, attachments, CommunicationMediums.Email);
}

return false;

}

/// <summary>
/// This is where the main logic of the agent should be placed.
/// </summary>
/// <param name="message"></param>
public override void ActMessage(Message message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}

base.ActMessage(message);
switch (message.Subject)
{
case SymuYellowPages.Belief:
AskBelief(message);
break;
}
}

private void AskBelief(Message message)
{
var replyMessage = Message.ReplyMessage(message);
// In this reply message, agent will send back its own belief if he can send beliefs
// that will have an impact on the beliefs of the sender if he can receive beliefs
Reply(replyMessage);
}
}
}

0 comments on commit f6efb93

Please sign in to comment.