Skip to content

Commit

Permalink
Refactoring KnowledgeNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 31, 2020
1 parent 9e0eee2 commit 641dbb8
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 85 deletions.
4 changes: 2 additions & 2 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ protected override void InitializeModels()
base.InitializeModels();
// Initialize agent models
KnowledgeModel = new KnowledgeModel(AgentId, Environment.Organization.Models.Knowledge, Cognitive,
Environment.WhitePages.MetaNetwork);
Environment.WhitePages.MetaNetwork, Environment.Organization.Models.Generator);
BeliefsModel = new BeliefsModel(AgentId, Environment.Organization.Models.Beliefs, Cognitive,
Environment.WhitePages.MetaNetwork, Environment.Organization.Models.Generator);
LearningModel = new LearningModel(AgentId, Environment.Organization.Models,
Environment.WhitePages.MetaNetwork.Knowledge, Cognitive);
Environment.WhitePages.MetaNetwork, Cognitive, Environment.Organization.Models.Generator);
ForgettingModel = new ForgettingModel(KnowledgeModel.Expertise, Cognitive, Environment.Organization.Models);
InfluenceModel = new InfluenceModel(Environment.Organization.Models.Influence,
Cognitive, Environment.WhitePages.MetaNetwork, BeliefsModel, Environment.Organization.Models.Generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Symu.Common.Interfaces.Agent;
using Symu.DNA;
using Symu.DNA.Activities;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Knowledge;

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Belief;
using Symu.DNA.TwoModesNetworks.AgentBelief;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;
using Symu.Messaging.Templates;
using Symu.Repository.Entity;
using static Symu.Common.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using Symu.Common.Classes;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA.Knowledges;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;
using Symu.Repository.Entity;
using static Symu.Common.Constants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;
using Symu.Messaging.Templates;
using Symu.Repository.Entity;
using static Symu.Common.Constants;
Expand All @@ -33,9 +34,11 @@ namespace Symu.Classes.Agents.Models.CognitiveModels
/// <remarks>From Construct Software</remarks>
public class KnowledgeModel
{
private readonly RandomGenerator _model;
private readonly IAgentId _agentId;
private readonly KnowledgeAndBeliefs _knowledgeAndBeliefs;
private readonly MessageContent _messageContent;
private readonly AgentKnowledgeNetwork _agentKnowledgeNetwork;
private readonly KnowledgeNetwork _knowledgeNetwork;

/// <summary>
Expand All @@ -46,8 +49,9 @@ public class KnowledgeModel
/// <param name="entity"></param>
/// <param name="cognitiveArchitecture"></param>
/// <param name="network"></param>
/// <param name="model"></param>
public KnowledgeModel(IAgentId agentId, ModelEntity entity, CognitiveArchitecture cognitiveArchitecture,
MetaNetwork network)
MetaNetwork network, RandomGenerator model)
{
if (entity is null)
{
Expand All @@ -67,18 +71,20 @@ public class KnowledgeModel
On = entity.IsAgentOn();
_agentId = agentId;
_knowledgeNetwork = network.Knowledge;
_agentKnowledgeNetwork = network.AgentKnowledge;
_knowledgeAndBeliefs = cognitiveArchitecture.KnowledgeAndBeliefs;
_messageContent = cognitiveArchitecture.MessageContent;
_model = model;
if (_knowledgeAndBeliefs.HasKnowledge)
{
if (_knowledgeNetwork.Exists(_agentId))
if (_agentKnowledgeNetwork.Exists(_agentId))
{
Expertise = _knowledgeNetwork.GetAgentExpertise(_agentId);
Expertise = _agentKnowledgeNetwork.GetAgentExpertise(_agentId);
}
else
{
Expertise = new AgentExpertise();
_knowledgeNetwork.Add(_agentId, Expertise);
_agentKnowledgeNetwork.Add(_agentId, Expertise);
}
}
else
Expand Down Expand Up @@ -152,10 +158,10 @@ public void InitializeExpertise(ushort step)
return;
}

_knowledgeNetwork.AddAgentId(_agentId, Expertise);
_agentKnowledgeNetwork.AddAgentId(_agentId, Expertise);
//_knowledgeNetwork.InitializeExpertise(_agentId, !_knowledgeAndBeliefs.HasInitialKnowledge, step);

foreach (var agentKnowledge in _knowledgeNetwork.GetAgentExpertise(_agentId).List)
foreach (var agentKnowledge in _agentKnowledgeNetwork.GetAgentExpertise(_agentId).List)
{
InitializeAgentKnowledge((AgentKnowledge)agentKnowledge, !_knowledgeAndBeliefs.HasInitialKnowledge, step);
}
Expand Down Expand Up @@ -183,7 +189,7 @@ public void InitializeAgentKnowledge(AgentKnowledge agentKnowledge, bool neutral
}

var level = neutral ? KnowledgeLevel.NoKnowledge : agentKnowledge.KnowledgeLevel;
agentKnowledge.InitializeKnowledge(knowledge.Length, _knowledgeNetwork.Model, level, step);
agentKnowledge.InitializeKnowledge(knowledge.Length, _model, level, step);
}

/// <summary>
Expand All @@ -208,7 +214,7 @@ public void AddExpertise(AgentExpertise expertise)
return;
}

_knowledgeNetwork.Add(_agentId, expertise);
_agentKnowledgeNetwork.Add(_agentId, expertise);
}

/// <summary>
Expand Down Expand Up @@ -244,7 +250,7 @@ public void AddKnowledge(IId knowledgeId, KnowledgeLevel level, float minimumKno
}

var agentKnowledge = new AgentKnowledge(knowledgeId, level, minimumKnowledge, timeToLive);
_knowledgeNetwork.Add(_agentId, agentKnowledge);
_agentKnowledgeNetwork.Add(_agentId, agentKnowledge);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA.Knowledges;
using Symu.DNA;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;
using Symu.Repository.Entity;
using static Symu.Common.Constants;

Expand All @@ -33,9 +35,11 @@ namespace Symu.Classes.Agents.Models.CognitiveModels
/// <remarks>From Construct Software</remarks>
public class LearningModel : ModelEntity
{
private readonly RandomGenerator _model;
private readonly IAgentId _id;
private readonly InternalCharacteristics _internalCharacteristics;
private readonly KnowledgeNetwork _knowledgeNetwork;
private readonly AgentKnowledgeNetwork _agentKnowledgeNetwork;
private readonly byte _randomLevel;
/// <summary>
/// Accumulates all learning of the agent during the simulation
Expand Down Expand Up @@ -68,17 +72,17 @@ public float GetKnowledgePotential()
return Expertise.GetAgentKnowledges<AgentKnowledge>().Sum(l => l.GetKnowledgePotential());
}

public LearningModel(IAgentId agentId, OrganizationModels models, KnowledgeNetwork knowledgeNetwork,
CognitiveArchitecture cognitiveArchitecture)
public LearningModel(IAgentId agentId, OrganizationModels models, MetaNetwork network,
CognitiveArchitecture cognitiveArchitecture, RandomGenerator model)
{
if (models == null)
{
throw new ArgumentNullException(nameof(models));
}

if (knowledgeNetwork == null)
if (network == null)
{
throw new ArgumentNullException(nameof(knowledgeNetwork));
throw new ArgumentNullException(nameof(network));
}

if (cognitiveArchitecture == null)
Expand All @@ -90,15 +94,17 @@ public float GetKnowledgePotential()
_id = agentId;
TasksAndPerformance = cognitiveArchitecture.TasksAndPerformance;
_internalCharacteristics = cognitiveArchitecture.InternalCharacteristics;
_knowledgeNetwork = knowledgeNetwork;
_knowledgeNetwork = network.Knowledge;
_agentKnowledgeNetwork = network.AgentKnowledge;
_randomLevel = models.RandomLevelValue;
if (!cognitiveArchitecture.InternalCharacteristics.CanLearn || !cognitiveArchitecture.KnowledgeAndBeliefs.HasKnowledge)
{
// Agent is not concerned by this model
On = false;
}

Expertise = _knowledgeNetwork.Exists(_id) ? _knowledgeNetwork.GetAgentExpertise(_id): null;
Expertise = _agentKnowledgeNetwork.Exists(_id) ? _agentKnowledgeNetwork.GetAgentExpertise(_id): null;
_model = model;
}

public TasksAndPerformance TasksAndPerformance { get; set; }
Expand Down Expand Up @@ -346,15 +352,15 @@ public float NextLearningByDoing()
public void LearnNewKnowledge(IAgentId agentId, IId knowledgeId, float minimumKnowledge, short timeToLive,
ushort step)
{
if (_knowledgeNetwork.Exists(agentId, knowledgeId))
if (_agentKnowledgeNetwork.Exists(agentId, knowledgeId))
{
return;
}

var agentKnowledge = new AgentKnowledge(knowledgeId, KnowledgeLevel.NoKnowledge, minimumKnowledge, timeToLive);
_knowledgeNetwork.Add(agentId, agentKnowledge);
_agentKnowledgeNetwork.Add(agentId, agentKnowledge);
var knowledge = _knowledgeNetwork.GetKnowledge<Knowledge>(knowledgeId);
agentKnowledge.InitializeKnowledge(knowledge.Length, _knowledgeNetwork.Model, KnowledgeLevel.NoKnowledge, step);
agentKnowledge.InitializeKnowledge(knowledge.Length, _model, KnowledgeLevel.NoKnowledge, step);
}

private static float LearningStandardDeviationValue(GenericLevel level)
Expand Down
2 changes: 1 addition & 1 deletion SourceCode/Symu/Classes/Task/SymuTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using Symu.Common;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.Repository.Entity;
using Symu.Results.Blocker;
using static Symu.Common.Constants;
Expand Down
6 changes: 3 additions & 3 deletions SourceCode/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public virtual void InitializeIteration()
{
Messages.Clear();
WhitePages.Clear();
WhitePages.MetaNetwork.Knowledge.Model =
Organization.Models.Generator;
//WhitePages.MetaNetwork.Knowledge.Model =
// Organization.Models.Generator;
//WhitePages.MetaNetwork.Beliefs.Model =
// Organization.Models.Generator;
IterationResult.Initialize();
Expand Down Expand Up @@ -454,7 +454,7 @@ public virtual void AddOrganizationDatabase()
public void SetDatabases()
{
foreach (var database in Organization.Databases.Select(databaseEntity =>
new Database(databaseEntity, Organization.Models, WhitePages.MetaNetwork.Knowledge)))
new Database(databaseEntity, Organization.Models, WhitePages.MetaNetwork)))
{
WhitePages.MetaNetwork.Resources.Add(database);
}
Expand Down
2 changes: 1 addition & 1 deletion SourceCode/Symu/Repository/Entity/Activity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using System.Linq;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Activities;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Knowledge;

#endregion

Expand Down
5 changes: 5 additions & 0 deletions SourceCode/Symu/Repository/Entity/AgentBelief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,10 @@ public float CompareTo(IAgentBelief other)
}
return 0;
}

public void SetBeliefBit(byte index, float value)
{
BeliefBits.SetBit(index, value);
}
}
}
2 changes: 1 addition & 1 deletion SourceCode/Symu/Repository/Entity/AgentKnowledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Symu.Common.Classes;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA.Knowledges;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;

namespace Symu.Repository.Entity
{
Expand Down
2 changes: 1 addition & 1 deletion SourceCode/Symu/Repository/Entity/Belief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
using Symu.Common.Classes;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Belief;
using Symu.DNA.OneModeNetworks.Knowledge;

#endregion

Expand Down
10 changes: 6 additions & 4 deletions SourceCode/Symu/Repository/Entity/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Knowledges;
using Symu.DNA;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.DNA.Resources;
using Symu.DNA.TwoModesNetworks.AgentKnowledge;

#endregion

Expand Down Expand Up @@ -51,7 +53,7 @@ public class Database : IResource
public IId Id => Entity.Id;

public Database(DatabaseEntity entity, OrganizationModels organizationModels,
KnowledgeNetwork knowledgeNetwork)
MetaNetwork metaNetwork)
{
if (entity == null)
{
Expand All @@ -65,8 +67,8 @@ public class Database : IResource

Entity = new DatabaseEntity(entity.Id, entity.CognitiveArchitecture);
var agentId = new AgentId(Entity.Id, 0);
_learningModel = new LearningModel(agentId, organizationModels, knowledgeNetwork,
entity.CognitiveArchitecture);
_learningModel = new LearningModel(agentId, organizationModels, metaNetwork,
entity.CognitiveArchitecture, organizationModels.Generator);
_forgettingModel = new ForgettingModel(_database, entity.CognitiveArchitecture, organizationModels);
}

Expand Down
9 changes: 5 additions & 4 deletions SourceCode/Symu/Repository/Entity/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
using Symu.Classes.Agents;
using Symu.Classes.Organization;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Knowledges;
using Symu.DNA;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.Messaging.Templates;

#endregion
Expand All @@ -25,15 +26,15 @@ namespace Symu.Repository.Entity
/// </summary>
public class Email : Database
{
public static Email CreateInstance(IId id, OrganizationModels organizationModels, KnowledgeNetwork knowledgeNetwork)
public static Email CreateInstance(IId id, OrganizationModels organizationModels, MetaNetwork metaNetwork)
{
CommunicationTemplate communication = new EmailTemplate();
var entity = new DatabaseEntity(id, communication);
return new Email(entity, organizationModels, knowledgeNetwork);
return new Email(entity, organizationModels, metaNetwork);
}

private Email(DatabaseEntity entity, OrganizationModels organizationModels,
KnowledgeNetwork knowledgeNetwork) : base(entity, organizationModels, knowledgeNetwork)
MetaNetwork metaNetwork) : base(entity, organizationModels, metaNetwork)
{
}

Expand Down
2 changes: 1 addition & 1 deletion SourceCode/Symu/Repository/Entity/Knowledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using System;
using Symu.Common.Interfaces.Entity;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.DNA.Knowledges;
using Symu.DNA.OneModeNetworks.Knowledge;

#endregion

Expand Down
9 changes: 5 additions & 4 deletions SourceCode/Symu/Repository/Entity/Wiki.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
using Symu.Classes.Agents;
using Symu.Classes.Organization;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Knowledges;
using Symu.DNA;
using Symu.DNA.OneModeNetworks.Knowledge;
using Symu.Messaging.Templates;

#endregion
Expand All @@ -25,15 +26,15 @@ namespace Symu.Repository.Entity
/// </summary>
public class Wiki : Database
{
public static Wiki CreateInstance(IId agentId, OrganizationModels organizationModels, KnowledgeNetwork knowledgeNetwork)
public static Wiki CreateInstance(IId agentId, OrganizationModels organizationModels, MetaNetwork metaNetwork)
{
CommunicationTemplate communication = new ViaPlatformTemplate();
var entity = new DatabaseEntity(agentId, communication);
return new Wiki(entity, organizationModels, knowledgeNetwork);
return new Wiki(entity, organizationModels, metaNetwork);
}

private Wiki(DatabaseEntity entity, OrganizationModels organizationModels,
KnowledgeNetwork knowledgeNetwork) : base(entity, organizationModels, knowledgeNetwork)
MetaNetwork metaNetwork) : base(entity, organizationModels, metaNetwork)
{
}

Expand Down

0 comments on commit 641dbb8

Please sign in to comment.