Exemple #1
0
 //private DictionaryList<Knowledge, NabfAgent> _knowledgeToAgent = new DictionaryList<Knowledge, NabfAgent>();
 //private DictionaryList<NabfAgent, Knowledge> _agentToKnowledge = new DictionaryList<NabfAgent, Knowledge>();
 public bool Subscribe(NabfAgent agent)
 {
     if (_sharingList.Contains(agent))
         return false;
     _sharingList.Add(agent);
     return true;
 }
Exemple #2
0
 public void SendKnowledgeToManager(List<Knowledge> sentKnowledge, NabfAgent sender)
 {
     //Knowledge kl;
     foreach (Knowledge k in sentKnowledge)
     {
         if (!_knowledgeBase.Contains(k))
         {
             _knowledgeBase.Add(k);
             foreach (NabfAgent a in _sharingList)
             {
                 if (a == sender)
                     continue;
                 a.Raise(new NewKnowledgeEvent(k));
             }
             //_knowledgeToAgent.Add(k, sender);
             //_agentToKnowledge.Add(sender, k);
         }
         //else
         //{
             //kl = _knowledgeBase.Keys.First(pk => k.Equals(pk));
             //_knowledgeToAgent.Add(kl, sender);
             //_agentToKnowledge.Add(sender, kl);
         //}
     }
     //SendKnowledgeToSubscribedAgents();
 }
Exemple #3
0
 public void ApplyToNotice(Notice notice, int desirability, NabfAgent a)
 {
     foreach (Notice n in _availableJobs.Get(NoticeToJobType(notice)))
     {
         if (n.ContentIsEqualTo(notice))
         {
             n.Apply(desirability, a);
             _agentToNotice.Add(a, n);
             break;
         }
     }
 }
        public void Initialization()
        {
            km = new KnowledgeManager();
            agent1 = new NabfAgent("agent1");
            agent2 = new NabfAgent("agent2");
            agent3 = new NabfAgent("agent3");
            agent4 = new NabfAgent("agent4");
            nk1 = new NodeKnowledge("vertex1", 0);
            nk2 = new NodeKnowledge("vertex2", 0);
            ek = new EdgeKnowledge("vertex1", "vertex2", 0);

            evtTriggered = 0;
            k = new List<Knowledge>();
        }
Exemple #5
0
        public void TwoAgentsApplyToSameNoticeThenUnApply_NoticeExists_Success()
        {
            List<Node> testNodes = new List<Node>() { new Node(), new Node() };

            Notice no = new DisruptJob(1, testNodes, ID++);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Disrupt, 1, testNodes, out no);
            List<NoticeBoard.JobType> jobs = new List<NoticeBoard.JobType>() { NoticeBoard.JobType.Disrupt };
            List<Notice> possibleJobs = new List<Notice>();
            possibleJobs.AddRange(nb.GetNotices(jobs));

            Assert.AreEqual(1, possibleJobs.Count);

            int desirability = 1, desirability2 = 99;
            NabfAgent a1 = new NabfAgent("agent1"), a2 = new NabfAgent("agent2");
            Notice n = nb.GetNotices(jobs).First<Notice>();

            nb.ApplyToNotice(possibleJobs[0], desirability, a1);
            Assert.AreEqual(1, n.GetAgentsApplied().Count);

            nb.ApplyToNotice(possibleJobs[0], desirability2, a2);
            Assert.AreEqual(2, n.GetAgentsApplied().Count);

            nb.UnApplyToNotice(possibleJobs[0], a2);
            Assert.AreEqual(1, n.GetAgentsApplied().Count);
        }
Exemple #6
0
        public void FindJobsForAgents_AllJobsFilledAllTheTime_Success()
        {
            int evtTriggered = 0;

            List<Node> testNodes = new List<Node>() { new Node(), new Node() };

            Notice no = new DisruptJob(1, testNodes, ID++), no2 = new AttackJob(1, testNodes, ID++), no3 = new OccupyJob(2, testNodes, ID++);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Disrupt, 1, testNodes, out no);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Attack, 1, testNodes, out no2);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Occupy, 2, testNodes, out no3);

            NabfAgent a1 = new NabfAgent("agent1"), a2 = new NabfAgent("agent2"), a3 = new NabfAgent("agent3"), a4 = new NabfAgent("agent4");

            nb.ApplyToNotice(no, 1, a1);
            nb.ApplyToNotice(no2, 50, a1);
            nb.ApplyToNotice(no, 50, a2);
            nb.ApplyToNotice(no2, 1, a2);

            nb.ApplyToNotice(no, 1, a3);

            nb.ApplyToNotice(no3, 50, a3);
            nb.ApplyToNotice(no3, 100, a4);

            int maxDesirabilityForFirstNotice = -1, maxDesirabilityForSecondNotice = -1, maxDesirabilityForThirdNotice = -1;
            int agentsAppliedToFirstNotice = -1, agentsAppliedToSecondNotice = -1, agentsAppliedToThirdNotice = -1;
            NabfAgent agentAppliedToFirstNotice1 = null, agentAppliedToFirstNotice2 = null, agentAppliedToSecondNotice1 = null, agentAppliedToSecondNotice2 = null, agentAppliedToThirdNotice = null;
            NabfAgent agentOnFirstNotice1 = null, agentOnFirstNotice2 = null, agentOnSecondNotice = null, agentOnThirdNotice = null;
            int i = 0;
            Notice firstNotice = null, secondNotice = null, thirdNotice = null;
            nb.NoticeIsReadyToBeExecutedEvent += (sender, evt) =>
            {
                evtTriggered++;
                i++;
                if (i == 1)
                {
                    firstNotice = evt.Notice;
                    agentsAppliedToFirstNotice = evt.Notice.GetAgentsApplied().Count;
                    maxDesirabilityForFirstNotice = evt.Notice.HighestAverageDesirabilityForNotice;
                    agentOnFirstNotice1 = evt.Agents[0];
                    agentOnFirstNotice2 = evt.Agents[1];
                    agentAppliedToFirstNotice1 = evt.Notice.GetAgentsApplied()[0];
                    agentAppliedToFirstNotice2 = evt.Notice.GetAgentsApplied()[1];
                }
                if (i == 2)
                {
                    secondNotice = evt.Notice;
                    agentsAppliedToSecondNotice = evt.Notice.GetAgentsApplied().Count;
                    maxDesirabilityForSecondNotice = evt.Notice.HighestAverageDesirabilityForNotice;
                    agentOnSecondNotice = evt.Agents[0];
                    agentAppliedToSecondNotice1 = evt.Notice.GetAgentsApplied()[0];
                    agentAppliedToSecondNotice2 = evt.Notice.GetAgentsApplied()[1];
                }
                if (i == 3)
                {
                    thirdNotice = evt.Notice;
                    agentsAppliedToThirdNotice = evt.Notice.GetAgentsApplied().Count;
                    maxDesirabilityForThirdNotice = evt.Notice.HighestAverageDesirabilityForNotice;
                    agentOnThirdNotice = evt.Agents[0];
                    agentAppliedToThirdNotice = evt.Notice.GetAgentsApplied()[0];
                }
            };
            nb.FindJobsForAgents();

            Assert.AreEqual(3, evtTriggered);
            Assert.IsTrue(firstNotice.ContentIsEqualTo(no3));
            Assert.IsTrue(secondNotice.ContentIsEqualTo(no2) || secondNotice.ContentIsEqualTo(no));
            Assert.IsTrue(thirdNotice.ContentIsEqualTo(no2) || thirdNotice.ContentIsEqualTo(no));
            Assert.AreEqual(75, maxDesirabilityForFirstNotice);
            Assert.AreEqual(50, maxDesirabilityForSecondNotice);
            Assert.AreEqual(50, maxDesirabilityForThirdNotice);
            Assert.AreEqual(2, agentsAppliedToFirstNotice);
            Assert.AreEqual(2, agentsAppliedToSecondNotice);
            Assert.AreEqual(1, agentsAppliedToThirdNotice);
            Assert.IsTrue(a3.Name == agentAppliedToFirstNotice1.Name || a4.Name == agentAppliedToFirstNotice1.Name);
            Assert.IsTrue(a3.Name == agentAppliedToFirstNotice2.Name || a4.Name == agentAppliedToFirstNotice2.Name);
            Assert.IsTrue(a1.Name == agentAppliedToSecondNotice1.Name || a2.Name == agentAppliedToSecondNotice1.Name);
            Assert.IsTrue(a1.Name == agentAppliedToSecondNotice2.Name || a2.Name == agentAppliedToSecondNotice2.Name);
            Assert.IsTrue(a1.Name == agentAppliedToThirdNotice.Name || a2.Name == agentAppliedToThirdNotice.Name);
            Assert.AreEqual(a3.Name, agentOnFirstNotice1.Name);
            Assert.AreEqual(a4.Name, agentOnFirstNotice2.Name);
            Assert.AreEqual(a2.Name, agentOnSecondNotice.Name);
            Assert.AreEqual(a1.Name, agentOnThirdNotice.Name);
        }
Exemple #7
0
        public void FindJobsForAgentsSomeAgentsIsPreferedForMultipleMultiJobs_AllJobsFilledAtStart_Success()
        {
            #region setup
            List<Node> nodes = new List<Node>() { new Node(), new Node(), new Node() };

            NabfAgent agent1 = new NabfAgent("a1"), agent2 = new NabfAgent("a2"), agent3 = new NabfAgent("a3"),
                agent4 = new NabfAgent("a4"), agent5 = new NabfAgent("a5"), agent6 = new NabfAgent("a6");

            Notice notice1 = new OccupyJob(2, nodes, ID++), notice2 = new DisruptJob(2, nodes, ID++), notice3 = new AttackJob(2, nodes, ID++),
                notice4 = new RepairJob(nodes, ID++);

            //nb.AddNotice(notice1); nb.AddNotice(notice2); nb.AddNotice(notice3); nb.AddNotice(notice4);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Occupy, 2, nodes, out notice1);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Disrupt, 2, nodes, out notice2);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Attack, 2, nodes, out notice3);
            nb.CreateAndAddNotice(NoticeBoard.JobType.Repair, 1, nodes, out notice4);
            #endregion

            #region agent desirability
            nb.ApplyToNotice(notice1, 110, agent1);
            nb.ApplyToNotice(notice2, 10, agent1);
            nb.ApplyToNotice(notice3, 11, agent1);
            nb.ApplyToNotice(notice4, 1111, agent1);

            nb.ApplyToNotice(notice1, 120, agent2);
            nb.ApplyToNotice(notice2, 20, agent2);
            nb.ApplyToNotice(notice3, 22, agent2);
            nb.ApplyToNotice(notice4, 2222, agent2);

            nb.ApplyToNotice(notice1, 130, agent3);
            nb.ApplyToNotice(notice2, 30, agent3);
            nb.ApplyToNotice(notice3, 33, agent3);

            nb.ApplyToNotice(notice1, 140, agent4);
            nb.ApplyToNotice(notice2, 40, agent4);
            nb.ApplyToNotice(notice3, 44, agent4);

            nb.ApplyToNotice(notice1, 150, agent5);
            nb.ApplyToNotice(notice2, 50, agent5);
            nb.ApplyToNotice(notice3, 55, agent5);

            nb.ApplyToNotice(notice1, 99999999, agent6);
            nb.ApplyToNotice(notice2, 99999999, agent6);
            nb.ApplyToNotice(notice3, 99999999, agent6);
            #endregion

            int evtTriggered = 0;
            List<Notice> notices = new List<Notice>();
            List<List<NabfAgent>> listOfListOfAgents = new List<List<NabfAgent>>();
            List<int> averageDesires = new List<int>();
            nb.NoticeIsReadyToBeExecutedEvent += (sender, evt) =>
            {
                notices.Add(evt.Notice);
                listOfListOfAgents.Add(evt.Agents);
                averageDesires.Add(evt.Notice.HighestAverageDesirabilityForNotice);

                evtTriggered++;
            };
            nb.FindJobsForAgents();

            Assert.AreEqual(3, evtTriggered);

            Assert.AreEqual(notice1, notices[0]);
            Assert.AreEqual(notice4, notices[1]);
            Assert.AreEqual(notice3, notices[2]);

            Assert.IsTrue(listOfListOfAgents[0][0].Name == agent6.Name || listOfListOfAgents[0][0].Name == agent5.Name);
            Assert.IsTrue(listOfListOfAgents[0][1].Name == agent6.Name || listOfListOfAgents[0][1].Name == agent5.Name);
            Assert.AreEqual(listOfListOfAgents[1][0].Name, agent2.Name);
            Assert.IsTrue(listOfListOfAgents[2][0].Name == agent4.Name || listOfListOfAgents[2][0].Name == agent3.Name);
            Assert.IsTrue(listOfListOfAgents[2][1].Name == agent4.Name || listOfListOfAgents[2][1].Name == agent3.Name);

            Assert.AreEqual((99999999 + 150) / 2, averageDesires[0]);
            Assert.AreEqual(2222, averageDesires[1]);
            Assert.AreEqual((44 + 33) / 2, averageDesires[2]);
        }
Exemple #8
0
 public void UnApplyToNotice(Notice notice, NabfAgent a)
 {
     foreach (Notice n in _availableJobs.Get(NoticeToJobType(notice)))
     {
         if (n.ContentIsEqualTo(notice))
         {
             n.UnApply(a, this);
             _agentToNotice.Remove(a, n);
             break;
         }
     }
 }
Exemple #9
0
 public ICollection<Notice> GetNotices(NabfAgent agent)
 {
     throw new NotImplementedException();
 }
Exemple #10
0
 public bool Unsubscribe(NabfAgent agent)
 {
     return _sharingList.Remove(agent);
 }