Skip to content

Commit

Permalink
Interaction sphere update
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Jun 10, 2020
1 parent 1c88a5e commit e272a11
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Symu source code/Symu/Classes/Agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,12 @@ protected virtual void SetModels()
/// </summary>
protected void FinalizeModels()
{
BeliefsModel.InitializeBeliefs();
KnowledgeModel.InitializeExpertise(Schedule.Step);
foreach (var agentKnowledge in KnowledgeModel.Expertise.List)
{
BeliefsModel.AddBelief(agentKnowledge.KnowledgeId);
}
BeliefsModel.InitializeBeliefs();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public float MinSphereDensity
{
throw new ArgumentOutOfRangeException("MinSphereDensity should be between [0;1]");
}
if (value > _maxSphereDensity)
{
throw new ArgumentOutOfRangeException("MinSphereDensity should be <= MaxSphereDensity");
}

_minSphereDensity = value;
}
Expand All @@ -76,34 +80,94 @@ public float MaxSphereDensity
{
throw new ArgumentOutOfRangeException("MaxSphereDensity should be between [0;1]");
}
if (value < _minSphereDensity)
{
throw new ArgumentOutOfRangeException("MaxSphereDensity should be <= MinSphereDensity");
}

_maxSphereDensity = value;
}
}

private float _socialDemographicWeight = 1;

/// <summary>
/// Weight of SocialDemographic in the calculus of DerivedParameter
/// Range[0;1]
/// </summary>
public float SocialDemographicWeight { get; set; } = 1;
public float SocialDemographicWeight
{
get => _socialDemographicWeight;
set
{
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("SocialDemographicWeight should be between [0;1]");
}

_socialDemographicWeight = value;
}
}

private float _relativeBeliefWeight = 1;

/// <summary>
/// Weight of RelativeBelief in the calculus of DerivedParameter
/// Range[0;1]
/// </summary>
public float RelativeBeliefWeight { get; set; } = 1;
public float RelativeBeliefWeight
{
get => _relativeBeliefWeight;
set
{
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("RelativeBeliefWeight should be between [0;1]");
}

_relativeBeliefWeight = value;
}
}

private float _relativeKnowledgeWeight = 1;

/// <summary>
/// Weight of RelativeKnowledge in the calculus of DerivedParameter
/// Range[0;1]
/// </summary>
public float RelativeKnowledgeWeight { get; set; } = 1;
public float RelativeKnowledgeWeight
{
get => _relativeKnowledgeWeight;
set
{
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("RelativeKnowledgeWeight should be between [0;1]");
}

_relativeKnowledgeWeight = value;
}
}

private float _relativeActivityWeight = 1;

/// <summary>
/// Weight of RelativeBeliefs in the calculus of DerivedParameter
/// Range[0;1]
/// </summary>
public float RelativeActivityWeight { get; set; } = 1;
public float RelativeActivityWeight
{
get => _relativeActivityWeight;
set
{
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("RelativeActivityWeight should be between [0;1]");
}

_relativeActivityWeight = value;
}
}

public void CopyTo(InteractionSphereModel entity)
{
Expand Down
2 changes: 1 addition & 1 deletion Symu source code/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void NextStep()
}

/// <summary>
/// Clone Sphere for the InteractionSphere
/// SetSphere for the InteractionSphere
/// </summary>
public void SetInteractionSphere(bool initialization)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void TransferTo(AgentId agentId, AgentId groupSourceId, AgentId groupTarg
/// <returns></returns>
public bool HasAgentActivities(AgentId agentId)
{
return AgentActivities.Any(a => a.Value.Exists(v => v.AgentId.Equals(agentId)));
return AgentActivities.Any(a => a.Value.Exists(v => v!= null && v.AgentId.Equals(agentId)));
}

#endregion
Expand Down
116 changes: 116 additions & 0 deletions Symu source code/Symu/Repository/Networks/Sphere/InteractionMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,60 @@ namespace Symu.Repository.Networks.Sphere
/// <example></example>
public static class InteractionMatrix
{
/// <summary>
/// Max value of the interaction matrix
/// </summary>
/// <param name="interactionMatrix"></param>
/// <returns></returns>
public static float GetMaxInteractionMatrix(float[,] interactionMatrix)
{
if (interactionMatrix == null)
{
throw new ArgumentNullException(nameof(interactionMatrix));
}

var actorsCount = interactionMatrix.GetLength(0);
if (actorsCount == 0 || actorsCount == 1)
{
return 0;
}

float max = 0;
for (var i = 0; i < actorsCount; i++)
for (var j = i + 1; j < actorsCount; j++)
{
max = Math.Max(max, interactionMatrix[i, j]);
}

return max;
}
/// <summary>
/// Max value of the interaction matrix
/// </summary>
/// <param name="interactionMatrix"></param>
/// <returns></returns>
public static float GetMinInteractionMatrix(float[,] interactionMatrix)
{
if (interactionMatrix == null)
{
throw new ArgumentNullException(nameof(interactionMatrix));
}

var actorsCount = interactionMatrix.GetLength(0);
if (actorsCount == 0 || actorsCount == 1)
{
return 0;
}

float min = 0;
for (var i = 0; i < actorsCount; i++)
for (var j = i + 1; j < actorsCount; j++)
{
min = Math.Min(min, interactionMatrix[i, j]);
}

return min;
}
/// <summary>
/// The likelihood that one actor is to attempt to interact with another is defined by the fact that
/// the actor i knows the fact k or not
Expand Down Expand Up @@ -70,6 +124,38 @@ public static DerivedParameter GetAverageInteractionMatrix(DerivedParameter[,] n
return new DerivedParameter(socialDemographics, beliefs, knowledge, activities);
}

/// <summary>
/// The likelihood that one actor is to attempt to interact with another is defined by the fact that
/// the actor i knows the fact k or not
/// </summary>
/// <param name="network"></param>
/// <returns></returns>
public static DerivedParameter GetMaxInteractionMatrix(DerivedParameter[,] network)
{
var knowledge = GetMaxInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Knowledge));
var activities = GetMaxInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Activities));
var beliefs = GetMaxInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Beliefs));
var socialDemographics =
GetMaxInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.SocialDemographics));
return new DerivedParameter(socialDemographics, beliefs, knowledge, activities);
}

/// <summary>
/// The likelihood that one actor is to attempt to interact with another is defined by the fact that
/// the actor i knows the fact k or not
/// </summary>
/// <param name="network"></param>
/// <returns></returns>
public static DerivedParameter GetMinInteractionMatrix(DerivedParameter[,] network)
{
var knowledge = GetMinInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Knowledge));
var activities = GetMinInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Activities));
var beliefs = GetMinInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.Beliefs));
var socialDemographics =
GetMinInteractionMatrix(GetInteractionMatrix(network, InteractionStrategy.SocialDemographics));
return new DerivedParameter(socialDemographics, beliefs, knowledge, activities);
}

public static uint NumberOfTriads(DerivedParameter[,] network)
{
if (network == null)
Expand Down Expand Up @@ -182,5 +268,35 @@ public static uint MaxTriads(int agentsCount)
{
return Combinatorics.Combinations(agentsCount, 3);
}

public static float GetDensity(DerivedParameter[,] network)
{
if (network == null)
{
throw new ArgumentNullException(nameof(network));
}

var actorsCount = network.GetLength(0);
var density = 0;
var total = 0;
for (var i = 0; i < actorsCount; i++)

// InteractionSphere is a symmetrical matrix with identity == 0
for (var j = i + 1; j < actorsCount; j++)
{
total++;
if (network[i, j].Homophily > 0)
{
density++;
}
}

if (total == 0)
{
return 0;
}

return (float)density / total;
}
}
}
8 changes: 8 additions & 0 deletions Symu source code/Symu/Repository/WhitePages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ public IEnumerable<Agent> FilteredAgentsByClassKey(byte classKey)
#region ToStop & Stopped Agents

public bool HasAgentsToStop => AllAgents().Count(a => a.State == AgentState.Stopping) > 0;
/// <summary>
/// Get the number of agents which are part of the interaction sphere
/// </summary>
public ushort GetInteractionSphereCount => (ushort) AllAgents().Count(a => a.Cognitive.InteractionPatterns.IsPartOfInteractionSphere);
/// <summary>
/// Get the agents which are part of the interaction sphere
/// </summary>
public IEnumerable<Agent> GetInteractionSphereAgents => AllAgents().Where(a => a.Cognitive.InteractionPatterns.IsPartOfInteractionSphere);

/// <summary>
/// Stop Agent is managed by the WhitePages services responsible of Agent Lifecycle Management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public void HandleSphere()

protected override void HandleResults()
{
var actorCount = (ushort) Environment.WhitePages.AllAgents()
.Count(a => a.Cognitive.InteractionPatterns.IsPartOfInteractionSphere);
var actorCount = Environment.WhitePages.GetInteractionSphereCount;

HandleTriads(actorCount);
HandleLinks(actorCount);
Expand Down

0 comments on commit e272a11

Please sign in to comment.