Skip to content

Commit

Permalink
Extract InteractionNetwork interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 25, 2020
1 parent 90f8af4 commit 9fe121a
Show file tree
Hide file tree
Showing 27 changed files with 738 additions and 558 deletions.
6 changes: 4 additions & 2 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Symu.Common.Math.ProbabilityDistributions;
using Symu.Messaging.Manager;
using Symu.Messaging.Messages;
using Symu.Repository.Entity;

#endregion

Expand Down Expand Up @@ -115,7 +116,7 @@ public bool AcceptNewInteraction(AgentId senderId)
return true;
}

if (Environment.WhitePages.MetaNetwork.Links.HasActiveLink(AgentId, senderId))
if (Environment.WhitePages.MetaNetwork.Interactions.HasActiveInteraction(AgentId, senderId))
{
return true;
}
Expand Down Expand Up @@ -143,7 +144,8 @@ public bool AcceptNewInteraction(AgentId senderId)
if (Environment.Organization.Models.InteractionSphere.SphereUpdateOverTime)
{
// Message.Sender is now part of agent interaction sphere
Environment.WhitePages.MetaNetwork.Links.AddLink(AgentId, senderId);
var interaction = new Interaction(AgentId, senderId);
Environment.WhitePages.MetaNetwork.Interactions.AddInteraction(interaction);
}

return true;
Expand Down
22 changes: 18 additions & 4 deletions SourceCode/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,33 @@ public virtual void InitializeIteration()
SetKnowledges();
SetAgents();
// Intentionally after SetAgents
InitializeNetworkLinks();
InitializeInteractionNetwork();
WhitePages.SetStarted();
}

/// <summary>
/// Initialize the network links.
/// Initialize the network interactions.
/// For performance it is not done in AddMemberToGroup at initialization
/// </summary>
public void InitializeNetworkLinks()
public void InitializeInteractionNetwork()
{
foreach (var groupId in WhitePages.MetaNetwork.Groups.GetGroups().ToList())
{
WhitePages.MetaNetwork.Links.AddLinks(WhitePages.MetaNetwork.Groups.GetAgents(groupId, new ClassId(SymuYellowPages.Actor)).ToList());
var agentIds = WhitePages.MetaNetwork.Groups.GetAgents(groupId, new ClassId(SymuYellowPages.Actor))
.ToList();

var count = agentIds.Count;
for (var i = 0; i < count; i++)
{
var agentId1 = agentIds[i];
// interaction are undirected
for (var j = i + 1; j < count; j++)
{
var agentId2 = agentIds[j];
var interaction = new Interaction(agentId1, agentId2);
WhitePages.MetaNetwork.Interactions.AddInteraction(interaction);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@
#region using directives

using System;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Repository.Networks.Interactions;
using static Symu.Common.Constants;

#endregion

namespace Symu.Repository.Networks.Link
namespace Symu.Repository.Entity
{
/// <summary>
/// Sphere of interaction - link are bidirectional.
///Default implementation of IInteraction
/// Defines the interaction between two agents used by InteractionNetwork
/// link are bidirectional.
/// AgentId1 has the smallest key
/// AgentId2 has the highest key
/// </summary>
public class LinkEntity
public class Interaction : IInteraction
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="agentId1"></param>
/// <param name="agentId2"></param>
public LinkEntity(IAgentId agentId1, IAgentId agentId2)
public Interaction(IAgentId agentId1, IAgentId agentId2)
{
if (agentId1 == null)
{
Expand All @@ -47,13 +50,18 @@ public LinkEntity(IAgentId agentId1, IAgentId agentId2)
AgentId2 = agentId1;
}

Activate();
IncreaseWeight();
}

public Interaction(IAgentId agentId1, IAgentId agentId2, float weight) : this(agentId1, agentId2)
{
Weight = weight;
}

/// <summary>
/// Number of links between the two agents
/// Number of interactions between the two agents
/// </summary>
public byte Count { get; private set; }
public float Weight { get; private set; }

/// <summary>
/// Unique key of the agent with the smallest key
Expand All @@ -65,33 +73,54 @@ public LinkEntity(IAgentId agentId1, IAgentId agentId2)
/// </summary>
public IAgentId AgentId2 { get; }

public bool IsActive => Count > 0;
public bool IsPassive => Count == 0;
public bool IsActive => Weight > 0;
public bool IsPassive => Weight < Tolerance;

public void Activate()
/// <summary>
/// Increase the weight of the interaction
/// </summary>
public void IncreaseWeight()
{
Count++;
Weight++;
}

public void Deactivate()
/// <summary>
/// Decrease the weight of the interaction
/// </summary>
public void DecreaseWeight()
{
if (Count > 0)
if (Weight > 0)
{
Count--;
Weight--;
}
}

public bool HasActiveLinks(IAgentId agentId)
/// <summary>
/// Agent has active interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId"></param>
/// <returns></returns>
public bool HasActiveInteractions(IAgentId agentId)
{
return IsActive && (AgentId1.Equals(agentId) || AgentId2.Equals(agentId));
}

public bool HasActiveLink(IAgentId agentId1, IAgentId agentId2)
/// <summary>
/// Agent has active interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId1"></param>
/// <param name="agentId2"></param>
/// <returns></returns>
public bool HasActiveInteraction(IAgentId agentId1, IAgentId agentId2)
{
return IsActive && HasLink(agentId1, agentId2);
}

public bool HasPassiveLink(IAgentId agentId1, IAgentId agentId2)
/// <summary>
/// Agent has passive interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId1"></param>
/// <param name="agentId2"></param>
/// <returns></returns>
public bool HasPassiveInteraction(IAgentId agentId1, IAgentId agentId2)
{
return IsPassive && HasLink(agentId1, agentId2);
}
Expand All @@ -113,7 +142,13 @@ public bool HasLink(IAgentId agentId1, IAgentId agentId2)

public override bool Equals(object obj)
{
return obj is LinkEntity link &&
return obj is Interaction link &&
link.HasLink(AgentId1, AgentId2);
}

public bool Equals(IInteraction obj)
{
return obj is Interaction link &&
link.HasLink(AgentId1, AgentId2);
}
}
Expand Down
70 changes: 70 additions & 0 deletions SourceCode/Symu/Repository/Networks/Interactions/IInteraction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#region Licence

// Description: SymuBiz - Symu
// Website: https://symu.org
// Copyright: (c) 2020 laurent morisseau
// License : the program is distributed under the terms of the GNU General Public License

#endregion

using Symu.Common.Interfaces.Agent;

namespace Symu.Repository.Networks.Interactions
{
/// <summary>
/// Defines the interaction between two agents used by InteractionNetwork
/// link are bidirectional.
/// AgentId1 has the smallest key
/// AgentId2 has the highest key
/// </summary>
/// <remarks>You can define your own definition of a passive/active interaction</remarks>
public interface IInteraction
{
/// <summary>
/// Unique key of the agent with the smallest key
/// </summary>
IAgentId AgentId1 { get; }

/// <summary>
/// Unique key of the agent with the highest key
/// </summary>
IAgentId AgentId2 { get; }
float Weight { get; }

bool IsActive {get; }
bool IsPassive { get; }

bool HasLink(IAgentId agentId1, IAgentId agentId2);
bool Equals(object obj);
bool Equals(IInteraction obj);

/// <summary>
/// Increase the weight of the interaction - if interaction are weighted
/// </summary>
void IncreaseWeight();
/// <summary>
/// Decrease the weight of the interaction - if interaction are weighted
/// </summary>
void DecreaseWeight();
/// <summary>
/// Agent has active interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId"></param>
/// <returns></returns>
bool HasActiveInteractions(IAgentId agentId);
/// <summary>
/// Agent has active interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId1"></param>
/// <param name="agentId2"></param>
/// <returns></returns>
bool HasActiveInteraction(IAgentId agentId1, IAgentId agentId2);
/// <summary>
/// Agent has passive interaction based on the weight of the interaction
/// </summary>
/// <param name="agentId1"></param>
/// <param name="agentId2"></param>
/// <returns></returns>
bool HasPassiveInteraction(IAgentId agentId1, IAgentId agentId2);
}
}

0 comments on commit 9fe121a

Please sign in to comment.