Skip to content

Commit

Permalink
Add SymuGroupAndInteraction project
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed May 12, 2020
1 parent 9595c1d commit 982d021
Show file tree
Hide file tree
Showing 260 changed files with 6,650 additions and 2,133 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

## 0.4.0 [](https://github.com/lmorisse/symu/compare/v0.4.0..v0.3.0)
* Add new project example: [SymuGroupAndInteraction](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuGroupAndInteraction) by [@lmorisse]
* Add new project example: [SymuGroupAndInteractionTests](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuGroupAndInteractionTests) by [@lmorisse]
* SymuEngine - add InteractionSphereModel by [@lmorisse]
* SymuEngine - update InteractionPatterns by [@lmorisse]

## 0.3.0 [](https://github.com/lmorisse/symu/compare/v0.3.0..v0.2.0)
* Add new project example: [SymuMessageAndTask](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuMessageAndTask) by [@lmorisse]
* Add new project example: [SymuMessageAndTaskTest](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuMessageAndTaskTests) by [@lmorisse]
Expand Down
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release notes

## 0.4.0
This release contains a new project example: [SymuGroupAndInteraction](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuGroupAndInteraction).

## 0.3.0
This release contains a new project example: [SymuMessageAndTask](https://github.com/lmorisse/Symu/tree/master/Symu%20examples/SymuMessageAndTask).

Expand Down
28 changes: 28 additions & 0 deletions Symu examples/SymuGroupAndInteraction/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.5.0" newVersion="1.2.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.4.5.0" newVersion="1.4.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.3.0" newVersion="4.1.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
116 changes: 116 additions & 0 deletions Symu examples/SymuGroupAndInteraction/Classes/ExampleEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#region Licence

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

#endregion

#region using directives

using System.Collections.Generic;
using SymuEngine.Classes.Agents;
using SymuEngine.Common;
using SymuEngine.Environment;
using SymuEngine.Repository.Networks.Knowledges;
using SymuTools.Math.ProbabilityDistributions;

#endregion

namespace SymuGroupAndInteraction.Classes
{
public class ExampleEnvironment : SymuEnvironment
{
public byte GroupsCount { get; set; } = 2;
public byte WorkersCount { get; set; } = 5;
public byte Knowledge { get; set; } = 0;
public byte Activities { get; set; } = 0;
public KnowledgeLevel KnowledgeLevel { get; set; } = KnowledgeLevel.FullKnowledge;

public override void SetModelForAgents()
{
base.SetModelForAgents();
Organization.Templates.Human.Cognitive.InteractionPatterns.IsolationIsRandom = false;
Organization.Templates.Human.Cognitive.InteractionPatterns.AgentCanBeIsolated = Frequency.Never;
Organization.Models.FollowGroupFlexibility = true;
Organization.Models.InteractionSphere.SphereUpdateOverTime = true;
Organization.Models.InteractionSphere.On = true;
var knowledges = new List<Knowledge>();
var activities = new List<string>();
for (var i = 0; i < GroupsCount; i++)
{
// knowledge length of 10 is arbitrary in this example
var knowledge = new Knowledge((ushort) i, i.ToString(), 10);
WhitePages.Network.AddKnowledge(knowledge);
knowledges.Add(knowledge);
activities.Add(i.ToString());
//Beliefs are created based on knowledge
}

for (var i = 0; i < GroupsCount; i++)
{
var group = new GroupAgent(Organization.NextEntityIndex(), this);

for (var j = 0; j < WorkersCount; j++)
{
var actor = new PersonAgent(Organization.NextEntityIndex(), this)
{
GroupId = group.Id
};
WhitePages.Network.AddMemberToGroup(actor.Id, 100, group.Id);
//Beliefs are added with knowledge
SetKnowledge(actor, knowledges, i);
SetActivity(actor.Id, activities, i, group.Id);
}
}
}

private void SetKnowledge(Agent actor, IReadOnlyList<Knowledge> knowledges, int i)
{
switch (Knowledge)
{
case 0:
// same Knowledge for all
actor.Cognitive.KnowledgeAndBeliefs.AddKnowledge(knowledges[0],
KnowledgeLevel,
Organization.Templates.Human.Cognitive.InternalCharacteristics);

break;
case 1:
// Knowledge is by group
actor.Cognitive.KnowledgeAndBeliefs.AddKnowledge(knowledges[i],
KnowledgeLevel,
Organization.Templates.Human.Cognitive.InternalCharacteristics);
break;
case 2:
// Knowledge is randomly defined for agentId
var index = DiscreteUniform.Sample(0, GroupsCount - 1);
actor.Cognitive.KnowledgeAndBeliefs.AddKnowledge(knowledges[index],
KnowledgeLevel,
Organization.Templates.Human.Cognitive.InternalCharacteristics);
break;
}
}

private void SetActivity(AgentId agentId, IReadOnlyList<string> activities, int i, AgentId groupId)
{
switch (Activities)
{
case 0:
// same activity for all
WhitePages.Network.NetworkActivities.AddActivity(agentId, activities[0], groupId);
break;
case 1:
// Activity is by group
WhitePages.Network.NetworkActivities.AddActivity(agentId, activities[i], groupId);
break;
case 2:
// Activity is randomly defined for agentId
var index = DiscreteUniform.Sample(0, GroupsCount - 1);
WhitePages.Network.NetworkActivities.AddActivity(agentId, activities[index], groupId);
break;
}
}
}
}
30 changes: 30 additions & 0 deletions Symu examples/SymuGroupAndInteraction/Classes/GroupAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#region Licence

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

#endregion

#region using directives

using SymuEngine.Classes.Agents;
using SymuEngine.Environment;

#endregion

namespace SymuGroupAndInteraction.Classes
{
public sealed class GroupAgent : Agent
{
public const byte ClassKey = 1;

public GroupAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
{
SetCognitive(Environment.Organization.Templates.Standard);
}
}
}
83 changes: 83 additions & 0 deletions Symu examples/SymuGroupAndInteraction/Classes/PersonAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#region Licence

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

#endregion

#region using directives

using System;
using SymuEngine.Classes.Agents;
using SymuEngine.Environment;
using SymuEngine.Messaging.Messages;
using SymuEngine.Repository;

#endregion

namespace SymuGroupAndInteraction.Classes
{
public sealed class PersonAgent : Agent
{
public const byte ClassKey = SymuYellowPages.Actor;

public PersonAgent(ushort agentKey, SymuEnvironment environment) : base(
new AgentId(agentKey, ClassKey),
environment)
{
SetCognitive(Environment.Organization.Templates.Human);
// Communication medium
Cognitive.InteractionCharacteristics.PreferredCommunicationMediums =
CommunicationMediums.FaceToFace;
}

/// <summary>
/// Agent is member of a group
/// </summary>
public AgentId GroupId { get; set; }


protected override void ActClassKey(Message message)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

base.ActClassKey(message);
switch (message.Subject)
{
case SymuYellowPages.Actor:
ActActor(message);
break;
}
}

private void ActActor(Message message)
{
switch (message.Action)
{
case MessageAction.Ask:
AskActor(message);
break;
}
}

private void AskActor(Message message)
{
// New interaction has already been accepted
// Let's reply positively
var reply = Message.ReplyMessage(message);
Send(reply);
}

public override void ActEndOfDay()
{
base.ActEndOfDay();
// Time for a coffee break and have interaction with other agents
Send(GroupId, MessageAction.Stop, SymuYellowPages.WorkingDay, CommunicationMediums.FaceToFace);
}
}
}
Loading

0 comments on commit 982d021

Please sign in to comment.