Skip to content

Commit

Permalink
Extract interfaces from BeliefsNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 27, 2020
1 parent edc1c69 commit 9efc210
Show file tree
Hide file tree
Showing 94 changed files with 1,092 additions and 753 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## Unreleased

* SymuTools refactored in Symu.Common solution
* Prepare Symu.org for ONA project
* Prepare Symu.org for Symu.DNA project
** Extract interfaces from metanetwork
** Move Networks to Symu.DNA repo

## 0.91 [](https://github.com/lmorisse/symu/compare/v0.9.1..v0.9.0) by [lmorisse](https://github.com/lmorisse)
* Split Agent into ReactiveAgent and CognitiveAgent
Expand Down
5 changes: 3 additions & 2 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Murphies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Symu.Common.Interfaces.Entity;
using Symu.Messaging.Messages;
using Symu.Repository;
using Symu.Repository.Entity;
using Symu.Repository.Networks.Sphere;
using Symu.Results.Blocker;
using static Symu.Common.Constants;
Expand Down Expand Up @@ -610,7 +611,7 @@ public void CheckBlockerIncompleteBelief(SymuTask task, IId knowledgeId)
byte mandatoryIndex = 0;
byte requiredIndex = 0;

var belief = Environment.WhitePages.MetaNetwork.Beliefs.GetBelief(knowledgeId);
var belief = Environment.WhitePages.MetaNetwork.Beliefs.GetBelief<Belief>(knowledgeId);
Environment.Organization.Murphies.IncompleteBelief.CheckBelief(belief, taskBits, BeliefsModel.Beliefs,
ref mandatoryScore, ref requiredScore,
ref mandatoryIndex, ref requiredIndex);
Expand Down Expand Up @@ -679,7 +680,7 @@ public void CheckRiskAversion(SymuTask task, IId knowledgeId)
float mandatoryScore = 0;
byte mandatoryIndex = 0;

var belief = Environment.WhitePages.MetaNetwork.Beliefs.GetBelief(knowledgeId);
var belief = Environment.WhitePages.MetaNetwork.Beliefs.GetBelief<Belief>(knowledgeId);
MurphyIncompleteBelief.CheckRiskAversion(belief, taskBits, BeliefsModel.Beliefs, ref mandatoryScore,
ref mandatoryIndex, -Cognitive.InternalCharacteristics.RiskAversionValue());
if (!(mandatoryScore <= -Cognitive.InternalCharacteristics.RiskAversionValue()))
Expand Down
52 changes: 24 additions & 28 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
using Symu.Classes.Agents.Models.CognitiveTemplates;
using Symu.Classes.Blockers;
using Symu.Classes.Task.Manager;
using Symu.Common;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Environment;
using Symu.Messaging.Messages;
using Symu.Repository;
Expand All @@ -33,13 +35,11 @@ namespace Symu.Classes.Agents
/// </summary>
public abstract partial class CognitiveAgent: ReactiveAgent
{

private byte _newInteractionCounter;
private readonly CognitiveArchitectureTemplate _cognitiveTemplate;

/// <summary>
/// constructor for generic new()
/// Use with CreateAgent method
/// </summary>
protected CognitiveAgent()
private CognitiveAgent()
{
}

Expand All @@ -48,9 +48,10 @@ protected CognitiveAgent()
/// </summary>
/// <param name="agentId"></param>
/// <param name="environment"></param>
protected CognitiveAgent(AgentId agentId, SymuEnvironment environment)
/// <remarks> Make constructor private and create a factory method to create an agent that call the Initialize method</remarks>
protected CognitiveAgent(AgentId agentId, SymuEnvironment environment) : base(agentId, environment)
{
CreateAgent(agentId, environment);
_cognitiveTemplate = environment.Organization.Templates.Standard;
}

/// <summary>
Expand All @@ -59,9 +60,10 @@ protected CognitiveAgent(AgentId agentId, SymuEnvironment environment)
/// <param name="agentId"></param>
/// <param name="environment"></param>
/// <param name="template"></param>
protected CognitiveAgent(AgentId agentId, SymuEnvironment environment, CognitiveArchitectureTemplate template)
/// <remarks> Make constructor private and create a factory method to create an agent that call the Initialize method</remarks>
protected CognitiveAgent(AgentId agentId, SymuEnvironment environment, CognitiveArchitectureTemplate template) : base(agentId, environment)
{
CreateAgent(agentId, environment, template);
_cognitiveTemplate = template;
}

/// <summary>
Expand Down Expand Up @@ -121,28 +123,22 @@ protected CognitiveAgent(AgentId agentId, SymuEnvironment environment, Cognitive

#region Initialization

protected override void CreateAgent(AgentId agentId, SymuEnvironment environment)
/// <summary>
/// Should be called after the constructor
/// </summary>
protected override void Initialize()
{
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
CreateAgent(agentId, environment, environment.Organization.Templates.Standard);
SetTemplate(_cognitiveTemplate);
SetCognitive();
// Databases are added to CognitiveAgent only, as it is a source of knowledge
foreach (var database in environment.Organization.Databases)
foreach (var database in Environment.Organization.Databases)
{
// Organization databases are used by every one
var agentResource = new AgentResource(database.Id, new ResourceUsage(0), 100);
environment.WhitePages.MetaNetwork.Resources.Add(AgentId, agentResource);
Environment.WhitePages.MetaNetwork.Resources.Add(AgentId, agentResource);
}
}

protected void CreateAgent(AgentId agentId, SymuEnvironment environment,
CognitiveArchitectureTemplate agentTemplate)
{
base.CreateAgent(agentId, environment);
SetTemplate(agentTemplate);
SetCognitive();
// intentionally before base.Initialize()
base.Initialize();
}

/// <summary>
Expand Down Expand Up @@ -236,13 +232,13 @@ protected override void InitializeModels()
// Initialize agent models
KnowledgeModel = new KnowledgeModel(AgentId, Environment.Organization.Models.Knowledge, Cognitive,
Environment.WhitePages.MetaNetwork);
BeliefsModel = new BeliefsModel(AgentId, Environment.Organization.Models.Beliefs, Cognitive,
Environment.WhitePages.MetaNetwork);
LearningModel = new LearningModel(AgentId, Environment.Organization.Models,
Environment.WhitePages.MetaNetwork.Knowledge, Cognitive);
ForgettingModel = new ForgettingModel(KnowledgeModel.Expertise, Cognitive, Environment.Organization.Models);
InfluenceModel = new InfluenceModel(AgentId, Environment.Organization.Models.Influence,
Cognitive.InternalCharacteristics, Environment.WhitePages.MetaNetwork);
BeliefsModel = new BeliefsModel(AgentId, Environment.Organization.Models.Beliefs, Cognitive,
Environment.WhitePages.MetaNetwork);
Cognitive, Environment.WhitePages.MetaNetwork, BeliefsModel);
ActivityModel = new ActivityModel(AgentId, Cognitive, Environment.WhitePages.MetaNetwork);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Activities;
using Symu.DNA.Knowledges;
using Symu.Repository.Entity;
using Symu.Repository.Networks;

Expand Down
160 changes: 146 additions & 14 deletions SourceCode/Symu/Classes/Agents/Models/CognitiveModels/BeliefsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using System;
using System.Linq;
using Symu.Common;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
Expand Down Expand Up @@ -77,7 +78,7 @@ public class BeliefsModel
/// Get the agent Beliefs
/// </summary>
public AgentBeliefs Beliefs =>
_knowledgeAndBeliefs.HasBelief ? _networkBeliefs.GetAgentBeliefs(_agentId) : null;
On && _knowledgeAndBeliefs.HasBelief ? _networkBeliefs.GetAgentBeliefs(_agentId) : null;

/// <summary>
/// Initialize the beliefs of the agent based on the belief network
Expand All @@ -95,7 +96,7 @@ public void InitializeBeliefs()
_networkBeliefs.AddAgentId(_agentId);
}

_networkBeliefs.InitializeBeliefs(_agentId, !_knowledgeAndBeliefs.HasInitialBelief);
InitializeBeliefs(!_knowledgeAndBeliefs.HasInitialBelief);
}

/// <summary>
Expand All @@ -109,7 +110,21 @@ public void AddBeliefs(AgentExpertise expertiseAgent)
return;
}

_networkBeliefs.Add(_agentId, expertiseAgent, _knowledgeAndBeliefs.DefaultBeliefLevel);
AddBeliefs(expertiseAgent, _knowledgeAndBeliefs.DefaultBeliefLevel);
}
public void AddBeliefs(AgentExpertise expertise, BeliefLevel beliefLevel)
{
if (expertise is null)
{
throw new ArgumentNullException(nameof(expertise));
}

_networkBeliefs.AddAgentId(_agentId);

foreach (var agentBelief in expertise.List.Select(agentKnowledge => new AgentBelief(agentKnowledge.KnowledgeId, beliefLevel)))
{
_networkBeliefs.AddBelief(_agentId, agentBelief);
}
}

/// <summary>
Expand All @@ -119,12 +134,7 @@ public void AddBeliefs(AgentExpertise expertiseAgent)
/// <param name="knowledgeId"></param>
public void AddBelief(IId knowledgeId)
{
if (!_knowledgeAndBeliefs.HasBelief || !On)
{
return;
}

_networkBeliefs.Add(_agentId, knowledgeId, _knowledgeAndBeliefs.DefaultBeliefLevel);
AddBelief(knowledgeId, _knowledgeAndBeliefs.DefaultBeliefLevel);
}

/// <summary>
Expand All @@ -140,12 +150,18 @@ public void AddBelief(IId knowledgeId, BeliefLevel beliefLevel)
return;
}

_networkBeliefs.Add(_agentId, knowledgeId, beliefLevel);
var agentBelief = new AgentBelief(knowledgeId, beliefLevel);
_networkBeliefs.Add(_agentId, agentBelief);
}

public AgentBelief GetAgentBelief(IId beliefId)
{
return Beliefs.GetAgentBelief<AgentBelief>(beliefId);
}

public AgentBelief GetBelief(IId beliefId)
public Belief GetBelief(IId beliefId)
{
return Beliefs.GetBelief(beliefId);
return _networkBeliefs.GetBelief<Belief>(beliefId);
}

/// <summary>
Expand Down Expand Up @@ -177,13 +193,13 @@ public Bits FilterBeliefToSend(IId beliefId, byte beliefBit, CommunicationTempla
}

// If Agent don't have the belief, he can't reply
if (!Beliefs.BelievesEnough(beliefId, beliefBit,
if (!BelievesEnough(beliefId, beliefBit,
_messageContent.MinimumBeliefToSendPerBit))
{
return null;
}

var agentBelief = Beliefs.GetBelief(beliefId);
var agentBelief = Beliefs.GetAgentBelief<AgentBelief>(beliefId);
if (agentBelief is null)
{
throw new ArgumentNullException(nameof(agentBelief));
Expand Down Expand Up @@ -225,5 +241,121 @@ public Bits FilterBeliefToSend(IId beliefId, byte beliefBit, CommunicationTempla
// We don't find always what we were looking for
return Math.Abs(beliefBitsToSend.GetSum()) < Tolerance ? null : beliefBitsToSend;
}

/// <summary>
/// Check that agent has the BeliefId[knowledgeBit] == 1
/// </summary>
/// <param name="beliefId"></param>
/// <param name="beliefBit"></param>
/// <param name="beliefThreshHoldForAnswer"></param>
/// <returns>true if the agent has the knowledge</returns>
public bool BelievesEnough(IId beliefId, byte beliefBit, float beliefThreshHoldForAnswer)
{
if (!Beliefs.Contains(beliefId))
{
return false;
}

var belief = GetAgentBelief(beliefId);
return belief.BelievesEnough(beliefBit, beliefThreshHoldForAnswer);
}

/// <summary>
/// Get the sum of all the beliefs
/// </summary>
/// <returns></returns>
public float GetBeliefsSum()
{
if (! On || Beliefs == null)
{
return 0;
}
return Beliefs.GetAgentBeliefs<AgentBelief>().Sum(l => l.GetBeliefSum());
}

/// <summary>
/// Get the maximum potential of all the beliefs
/// </summary>
/// <returns></returns>
public float GetBeliefsPotential()
{
if (Beliefs == null)
{
return 0;
}
return Beliefs.GetAgentBeliefs<AgentBelief>().Sum(l => l.GetBeliefPotential());
}

/// <summary>
/// Initialize AgentBelief with a stochastic process
/// </summary>
/// <param name="neutral"></param>
public void InitializeBeliefs(bool neutral)
{
if (!_networkBeliefs.Exists(_agentId))
{
throw new NullReferenceException(nameof(_agentId));
}

foreach (var agentBelief in Beliefs.GetAgentBeliefs<AgentBelief>())
{
InitializeAgentBelief(agentBelief, neutral);
}
}

/// <summary>
/// Initialize AgentBelief with a stochastic process based on the agent belief level
/// </summary>
/// <param name="agentBelief">agentBelief to initialize</param>
/// <param name="neutral">if !HasInitialBelief, then a neutral initialization is done</param>
public void InitializeAgentBelief(AgentBelief agentBelief, bool neutral)
{
if (agentBelief == null)
{
throw new ArgumentNullException(nameof(agentBelief));
}

var belief = GetBelief(agentBelief.BeliefId);
if (belief == null)
{
throw new NullReferenceException(nameof(belief));
}

var level = neutral ? BeliefLevel.NoBelief : agentBelief.BeliefLevel;
var beliefBits = belief.InitializeBits(_networkBeliefs.Model, level);
agentBelief.SetBeliefBits(beliefBits);
}

/// <summary>
/// agent learn beliefId with a weight of influenceability * influentialness
/// </summary>
/// <param name="beliefId"></param>
/// <param name="beliefBits"></param>
/// <param name="influenceWeight"></param>
/// <param name="beliefLevel"></param>
public void Learn(IId beliefId, Bits beliefBits, float influenceWeight,
BeliefLevel beliefLevel)
{
LearnNewBelief(beliefId, beliefLevel);
_networkBeliefs.GetAgentBelief<AgentBelief>(_agentId, beliefId).Learn(beliefBits, influenceWeight);
}

/// <summary>
/// Agent don't have still this belief, it's time to learn a new one
/// </summary>
/// <param name="beliefId"></param>
/// <param name="beliefLevel"></param>
public void LearnNewBelief(IId beliefId, BeliefLevel beliefLevel)
{
if (_networkBeliefs.Exists(_agentId, beliefId))
{
return;
}

var agentBelief = new AgentBelief(beliefId, beliefLevel);
_networkBeliefs.Add(_agentId, agentBelief);
InitializeBeliefs(true);
}

}
}

0 comments on commit 9efc210

Please sign in to comment.