Skip to content

Commit

Permalink
Refactoring Edge Constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Oct 7, 2020
1 parent 581cfb1 commit dc71ecf
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 83 deletions.
3 changes: 1 addition & 2 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ public bool AcceptNewInteraction(IAgentId senderId)
}

// Message.Sender is now part of agent interaction sphere
var interaction = new ActorActor(AgentId, senderId);
Environment.MainOrganization.MetaNetwork.ActorActor.Add(interaction);
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, AgentId, senderId);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public ActorTaskModel(IAgentId agentId, CognitiveArchitecture cognitiveArchitect
/// <param name="taskId"></param>
public void AddActorTask(IAgentId taskId)
{
var actorTask = new ActorTask(_agentId, taskId);
_actorTaskNetwork.Add(actorTask);
_ = new ActorTask(_actorTaskNetwork, _agentId, taskId);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ public void AddBelief(IAgentId beliefId, BeliefLevel beliefLevel)
return;
}

var actorBelief = new ActorBelief(_agentId, beliefId, beliefLevel);
_actorBeliefNetwork.Add(actorBelief);
_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
}


Expand All @@ -181,8 +180,7 @@ public void AddBeliefFromKnowledgeId(IAgentId knowledgeId, BeliefLevel beliefLev
throw new NullReferenceException(nameof(belief));
}

var actorBelief = new ActorBelief(_agentId, belief.EntityId, beliefLevel);
_actorBeliefNetwork.Add(actorBelief);
_ = new ActorBelief(_actorBeliefNetwork, _agentId, belief.EntityId, beliefLevel);
}

public ActorBelief GetActorBelief(IAgentId beliefId)
Expand Down Expand Up @@ -425,8 +423,7 @@ public void LearnNewBelief(IAgentId beliefId, BeliefLevel beliefLevel)
return;
}

var actorBelief = new ActorBelief(_agentId, beliefId, beliefLevel);
_actorBeliefNetwork.Add(actorBelief);
_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
InitializeBeliefs(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ public void AddKnowledge(IAgentId knowledgeId, KnowledgeLevel level, float minim
return;
}

var actorKnowledge = new ActorKnowledge(_agentId, knowledgeId, level, minimumKnowledge, timeToLive);
_actorKnowledgeNetwork.Add(actorKnowledge);
_ = new ActorKnowledge(_actorKnowledgeNetwork, _agentId, knowledgeId, level, minimumKnowledge, timeToLive);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,8 @@ public float NextLearningByDoing()
return;
}

var agentKnowledge = new ActorKnowledge(agentId, knowledgeId, KnowledgeLevel.NoKnowledge, minimumKnowledge,
var agentKnowledge = new ActorKnowledge(_entityKnowledgeNetwork, agentId, knowledgeId, KnowledgeLevel.NoKnowledge, minimumKnowledge,
timeToLive);
_entityKnowledgeNetwork.Add(agentKnowledge);
var knowledge = _knowledgeNetwork.GetEntity<Knowledge>(knowledgeId);
agentKnowledge.InitializeKnowledge(knowledge.Length, _model, KnowledgeLevel.NoKnowledge, step);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public void AddResourceTasks(IEnumerable<ITask> tasks)

foreach (var task in tasks)
{
var resourceTask = new ResourceTask(_resourceId, task.EntityId);
_resourceTaskNetwork.Add(resourceTask);
_ = new ResourceTask(_resourceTaskNetwork, _resourceId, task.EntityId);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions SourceCode/Symu/Repository/Edges/ActorBelief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Symu.Common.Interfaces;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.OrgMod.Edges;
using Symu.OrgMod.GraphNetworks.TwoModesNetworks;
using Symu.Repository.Entities;
using static Symu.Common.Constants;

Expand Down Expand Up @@ -44,6 +45,10 @@ public ActorBelief(IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel)
{
BeliefLevel = beliefLevel;
}
public ActorBelief(ActorBeliefNetwork network, IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel) : base(network, actorId, beliefId)
{
BeliefLevel = beliefLevel;
}

/// <summary>
/// The value used to feed the matrix network
Expand Down
52 changes: 48 additions & 4 deletions SourceCode/Symu/Repository/Edges/ActorKnowledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Symu.Common.Interfaces;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.OrgMod.Edges;
using Symu.OrgMod.GraphNetworks.TwoModesNetworks;
using Symu.Repository.Entities;

#endregion
Expand All @@ -26,21 +27,22 @@ namespace Symu.Repository.Edges
/// KnowledgeId, KnowledgeLevel, KnowledgeBits
/// </summary>
/// <example>Dev Java, test, project management, sociology, ...</example>
public class ActorKnowledge : EntityKnowledge //IActorKnowledge
public class ActorKnowledge : EntityKnowledge
{
/// <summary>
/// Constructor used by WorkerCognitiveAgent for ForgettingKnowledge
/// </summary>
/// <param name="network"></param>
/// <param name="actorId"></param>
/// <param name="knowledgeId"></param>
/// <param name="knowledgeBits"></param>
public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, KnowledgeBits knowledgeBits) : base(actorId,
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeBits knowledgeBits) : base(network, actorId,
knowledgeId)
{
KnowledgeBits = knowledgeBits;
Length = KnowledgeBits?.Length ?? 0;
}

}
/// <summary>
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
/// </summary>
Expand All @@ -61,6 +63,27 @@ public class ActorKnowledge : EntityKnowledge //IActorKnowledge
Length = KnowledgeBits.Length;
}

/// <summary>
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
/// </summary>
/// <param name="network"></param>
/// <param name="actorId"></param>
/// <param name="knowledgeId"></param>
/// <param name="knowledgeBits"></param>
/// <param name="minimumKnowledge"></param>
/// <param name="timeToLive"></param>
/// <param name="step"></param>
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, float[] knowledgeBits, float minimumKnowledge,
short timeToLive,
ushort step = 0) : base(network, actorId, knowledgeId)
{
MinimumKnowledge = minimumKnowledge;
TimeToLive = timeToLive;
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
KnowledgeBits.SetBits(knowledgeBits, step);
Length = KnowledgeBits.Length;
}

/// <summary>
/// Constructor based on the knowledge Id and the knowledge Level.
/// KnowledgeBits is not yet initialized.
Expand All @@ -81,6 +104,27 @@ public class ActorKnowledge : EntityKnowledge //IActorKnowledge
Length = KnowledgeBits.Length;
}

/// <summary>
/// Constructor based on the knowledge Id and the knowledge Level.
/// KnowledgeBits is not yet initialized.
/// NetworkKnowledges.InitializeAgentKnowledge must be called to initialized KnowledgeBits
/// </summary>
/// <param name="network"></param>
/// <param name="actorId"></param>
/// <param name="knowledgeId"></param>
/// <param name="level"></param>
/// <param name="minimumKnowledge"></param>
/// <param name="timeToLive"></param>
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeLevel level, float minimumKnowledge,
short timeToLive) : base(network, actorId, knowledgeId)
{
KnowledgeLevel = level;
MinimumKnowledge = minimumKnowledge;
TimeToLive = timeToLive;
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
Length = KnowledgeBits.Length;
}

/// <summary>
/// The value used to feed the matrix network
/// For a binary matrix network, the value is 1
Expand Down
4 changes: 1 addition & 3 deletions SourceCode/Symu/Repository/Entities/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ public void InitializeKnowledge(IAgentId knowledgeId, ushort step)
}

var knowledge = MetaNetwork.Knowledge.GetEntity<Knowledge>(knowledgeId);
var resourceKnowledge =
new ActorKnowledge(EntityId, knowledgeId, KnowledgeLevel.NoKnowledge, 0, TimeToLive);
var resourceKnowledge = new ActorKnowledge(MetaNetwork.ResourceKnowledge, EntityId, knowledgeId, KnowledgeLevel.NoKnowledge, 0, TimeToLive);
resourceKnowledge.InitializeWith0(knowledge.Length, step);
Add(resourceKnowledge);
}

/// <summary>
Expand Down
23 changes: 8 additions & 15 deletions SourceCode/SymuTests/Classes/Agents/CognitiveAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace SymuTests.Classes.Agents
[TestClass]
public class CognitiveAgentTests : BaseTestClass
{
private IActorActor _actorActor;
private TestCognitiveAgent _agent;
private TestCognitiveAgent _agent2;
private Belief _belief;
Expand Down Expand Up @@ -76,7 +75,6 @@ public void Initialize()
_agent.Start();

_agent2 = TestCognitiveAgent.CreateInstance(Environment);
_actorActor = new ActorActor(_agent.AgentId, _agent2.AgentId);
_agent2.Start();
}

Expand Down Expand Up @@ -122,8 +120,7 @@ public void HasEmailTest()
Assert.IsFalse(_agent.HasEmail);
var email = EmailEntity.CreateInstance(Environment.MainOrganization.MetaNetwork, MainOrganization.Models);
var usage = new ResourceUsage(0);
var actorResource = new ActorResource(_agent.AgentId, email.EntityId, usage);
Environment.MainOrganization.MetaNetwork.ActorResource.Add(actorResource);
_ = new ActorResource(Environment.MainOrganization.MetaNetwork.ActorResource, _agent.AgentId, email.EntityId, usage);
_agent.EmailId = email.EntityId;
Assert.IsTrue(_agent.HasEmail);
Assert.IsNotNull(_agent.Email);
Expand Down Expand Up @@ -272,9 +269,8 @@ public void LearnBeliefsFromPostMessageTest()
private void SetExpertise(KnowledgeBits bit0S)
{
_agent.Cognitive.KnowledgeAndBeliefs.HasKnowledge = true;
var knowledge = new Knowledge(MainOrganization.MetaNetwork, MainOrganization.Models, "1", 1);
var actorKnowledge = new ActorKnowledge(_agent.AgentId, knowledge.EntityId, bit0S);
Environment.MainOrganization.MetaNetwork.ActorKnowledge.Add(actorKnowledge);
var knowledge = new Knowledge(Environment.MainOrganization.MetaNetwork, MainOrganization.Models, "1", 1);
_ = new ActorKnowledge(Environment.MainOrganization.MetaNetwork.ActorKnowledge, _agent.AgentId, knowledge.EntityId, bit0S);
}

#endregion
Expand Down Expand Up @@ -836,7 +832,7 @@ public void AcceptNewInteractionTest()
public void AcceptNewInteractionTest1()
{
_agent.Cognitive.InteractionPatterns.IsPartOfInteractionSphere = true;
Environment.MainOrganization.MetaNetwork.ActorActor.Add(_actorActor);
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
Assert.IsTrue(_agent.AcceptNewInteraction(_agent2.AgentId));
}

Expand Down Expand Up @@ -962,7 +958,7 @@ public void GetAgentIdsForInteractionsTest1()
[TestMethod]
public void GetAgentIdsForInteractionsTest2()
{
Environment.MainOrganization.MetaNetwork.ActorActor.Add(_actorActor);
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
Environment.MainOrganization.MetaNetwork.InteractionSphere.SetSphere(true,
Environment.WhitePages.AllAgentIds().ToList(), Environment.MainOrganization.MetaNetwork);
Assert.IsTrue(_agent.GetAgentIdsForInteractions(InteractionStrategy.Homophily).Any());
Expand Down Expand Up @@ -1362,12 +1358,9 @@ public void TryRecoverBlockerIncompleteKnowledgeTest()
var teammate = AddAgent();
var group = TestCognitiveAgent.CreateInstance(2, Environment);
group.Start();
var actorOrganization = new ActorOrganization(_agent.AgentId, group.AgentId);
var teammateOrganization = new ActorOrganization(teammate.AgentId, group.AgentId);
Environment.MainOrganization.MetaNetwork.ActorOrganization.Add(actorOrganization);
Environment.MainOrganization.MetaNetwork.ActorOrganization.Add(teammateOrganization);
var interaction = new ActorActor(_agent.AgentId, teammate.AgentId);
Environment.MainOrganization.MetaNetwork.ActorActor.Add(interaction);
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, _agent.AgentId, group.AgentId);
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, teammate.AgentId, group.AgentId);
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, teammate.AgentId);
teammate.KnowledgeModel.AddKnowledge(_knowledge.EntityId, KnowledgeLevel.FullKnowledge, 0, -1);
teammate.KnowledgeModel.InitializeExpertise(0);
_agent.KnowledgeModel.SetKnowledge(_knowledge.EntityId, 0, 1, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public void Initialize()
_belief.InitializeBits(MainOrganization.Models.Generator, BeliefLevel.NeitherAgreeNorDisagree);
_beliefBitsNeutral = _belief.InitializeBits(MainOrganization.Models.Generator, BeliefLevel.NoBelief);

Network.Belief.Add(_belief);
_actorBelief = new ActorBelief(_agentId, _belief.EntityId, BeliefLevel.NeitherAgreeNorDisagree);
Network.ActorBelief.Add(_actorBelief);

_actorBelief = new ActorBelief(Network.ActorBelief, _agentId, _belief.EntityId, BeliefLevel.NeitherAgreeNorDisagree);

_taskBits.SetMandatory(new byte[] {0});
_taskBits.SetRequired(new byte[] {0});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void InitializeAgentKnowledgeTest()
public void InitializeExpertiseTest1()
{
_cognitiveArchitecture.KnowledgeAndBeliefs.HasInitialKnowledge = true;
Network.Knowledge.Add(_knowledge);
_knowledgeModel.AddKnowledge(_knowledge.EntityId, new float[] {0}, 0, -1);
_knowledgeModel.InitializeExpertise(0);
Assert.IsNotNull(_knowledgeModel.GetActorKnowledge(_knowledge.EntityId).KnowledgeBits);
Expand Down Expand Up @@ -371,8 +370,7 @@ public void HasTest()
{
float[] bits = {0, 1};
var knowledgeBits = new KnowledgeBits(bits, 0, -1);
var actorKnowledge = new ActorKnowledge(_agentId, _knowledge.EntityId, knowledgeBits);
Network.ActorKnowledge.Add(actorKnowledge);
_ = new ActorKnowledge(Network.ActorKnowledge, _agentId, _knowledge.EntityId, knowledgeBits);
Assert.IsFalse(_knowledgeModel.KnowsEnough(_knowledge.EntityId, 0, 0.1F, 0));
Assert.IsTrue(_knowledgeModel.KnowsEnough(_knowledge.EntityId, 1, 0.1F, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public void Initialize()
_beliefsModel = new BeliefsModel(_agentId, modelEntity, _cognitiveArchitecture, Network,
MainOrganization.Models.Generator);
_belief = new Belief(Network, 1, MainOrganization.Models.Generator, BeliefWeightLevel.RandomWeight);
_actorBelief = new ActorBelief(_agentId, _belief.EntityId, BeliefLevel.NeitherAgreeNorDisagree);
Network.ActorBelief.Add(_actorBelief);

_actorBelief = new ActorBelief(Network.ActorBelief, _agentId, _belief.EntityId, BeliefLevel.NeitherAgreeNorDisagree);

_taskBits.SetMandatory(new byte[] {0});
_taskBits.SetRequired(new byte[] {0});
}
Expand Down

0 comments on commit dc71ecf

Please sign in to comment.