Exemplo n.º 1
0
        public override void Act(ActressMas.Message message)
        {
            try
            {
                Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

                string        action;
                List <string> parameters;
                Utils.ParseMessage(message.Content, out action, out parameters);

                switch (action)
                {
                case Actions.DonePhase1:
                    doneSplit++;
                    if (doneSplit == Utils.validagentPool.Count)
                    {
                        //remove invalid workers(Nodes)
                        for (int j = 0; j < Utils.agentPool.Count; j++)
                        {
                            if (!Utils.validagentPool.Contains(Utils.agentPool[j]))
                            {
                                Send(Utils.agentPool[j], new BaseMessage
                                {
                                    Action = Actions.Terminate2
                                }.ToString());
                            }
                        }
                        Utils.agentPool = Utils.validagentPool;
                        for (int j = 0; j < Utils.validagentPool.Count; j++)
                        {
                            Send(Utils.validagentPool[j], new BaseMessage
                            {
                                Action = Actions.SortAndAssignData
                            }.ToString());
                        }
                    }

                    break;

                case Actions.DoneSortAndAssignData:
                    doneAssignData++;
                    if (doneAssignData == Utils.agentPool.Count)
                    {
                        while (Utils.agentPool.Count > 1)
                        {
                            MergeAndCompare();
                        }
                        Send(Utils.agentPool[0], new BaseMessage
                        {
                            Action = Actions.Print
                        }.ToString());
                        Stop();
                    }
                    break;
                }
            }
            catch (Exception e)
            {
            }
        }
        public override void Act(Queue <Message> messages)
        {
            while (messages.Count > 0)
            {
                Message message = messages.Dequeue();
                Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

                FloorPlanMessage floorPlanMessage = JsonSerializer.Deserialize <FloorPlanMessage>(message.Content);

                switch (floorPlanMessage.type)
                {
                case Start:
                    HandleStart(message.Sender, floorPlanMessage);
                    break;

                case MessageType.Move:
                    HandleChangePosition(message.Sender, floorPlanMessage);
                    break;

                default:
                    break;
                }

                guiForm.UpdatePlanetGUI();
            }
        }
Exemplo n.º 3
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

            string action;
            string parameters;

            Utils.ParseMessage(message.Content, out action, out parameters);

            switch (baseMessage.Action)
            {
            case Actions.DoneAssignAndSort:
                countdone_sort_asignData++;
                if (countdone_sort_asignData == Utils.numAg)
                {
                    int k = Utils.numAg - 1;
                    while (k > 0)
                    {
                        evenPhase();
                        oddPhase();
                        k--;
                        Console.WriteLine(k + " Phase ");
                    }
                    for (int j = 0; j < Utils.agentPool.Count; j++)
                    {
                        Send(Utils.agentPool[j], new BaseMessage {
                            Action = Actions.PrintData
                        }.ToString());
                    }
                }


                break;

            case Actions.DonePrint:
                countdone_done_print++;
                if (countdone_done_print == Utils.numAg)
                {
                    ////asdas
                    ///
                    for (int i = 0; i < Utils.lsort.Count; i++)
                    {
                        Console.Write(Utils.lsort[i] + ",");
                    }
                    Utils.Destination = Utils.lsort.ToArray();
                    Stop();
                }

                break;
            }
        }
Exemplo n.º 4
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            switch (baseMessage.Action)
            {
            case Actions.Restart:
            case Actions.Start:
                Sort();
                break;
            }
        }
Exemplo n.º 5
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            switch (baseMessage.Action)
            {
            case Actions.Done:
                _done++;
                if (_done == Utils.NoAgents)
                {
                    Stop();
                }
                break;
            }
        }
Exemplo n.º 6
0
        public override void Act(ActressMas.Message message)
        {
            try
            {
                Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

                string action; string parameters;
                Utils.ParseMessage(message.Content, out action, out parameters);

                switch (action)
                {
                case "position":
                    HandlePosition(message.Sender, parameters);
                    break;

                case "change":
                    HandleChange(message.Sender, parameters);
                    break;

                case "pick-up":
                    HandlePickUp(message.Sender, parameters);
                    break;

                case "carry":
                    HandleCarry(message.Sender, parameters);
                    break;

                case "unload":
                    HandleUnload(message.Sender);
                    break;

                default:
                    break;
                }

                _formGui.UpdatePlanetGUI();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 7
0
        public override void Act(ActressMas.Message message)
        {
            var standardMessage = new StandardMessage(message.Content);

            switch (standardMessage.Action)
            {
            case Actions.Sort:

                if (standardMessage.Right == standardMessage.Left)
                {
                    if (standardMessage.SourceToDestination)
                    {
                        Utils.Destination[standardMessage.Left] = Utils.Source[standardMessage.Left];
                    }

                    SendMergeMessage();
                }
                else if ((standardMessage.Right - standardMessage.Left) <= Utils.Threshold)
                {
                    Array.Sort(Utils.Source, standardMessage.Left,
                               standardMessage.Right - standardMessage.Left + 1);

                    if (standardMessage.SourceToDestination)
                    {
                        for (int i = standardMessage.Left; i <= standardMessage.Right; i++)
                        {
                            Utils.Destination[i] = Utils.Source[i];
                        }
                    }

                    SendMergeMessage();
                }
                else
                {
                    standardMessage.Action = Actions.Split;
                    Send(Agents.MasterAgent, standardMessage.ToString());
                }

                break;
            }
        }
Exemplo n.º 8
0
        public override void Act(ActressMas.Message message)
        {
            var standardMessage = new StandardMessage(message.Content);

            switch (standardMessage.Action)
            {
            case Actions.Split:
                SendMessage(standardMessage, message.Sender, true);
                SendMessage(standardMessage, message.Sender, false);
                break;

            case Actions.Merge:
                _expectedMerge--;

                if (_expectedMerge == 0)
                {
                    while (_calls.Count > 0)
                    {
                        var currentCall = _calls.Pop();
                        int middle      = (currentCall.Left + currentCall.Right) / 2;
                        if (currentCall.SourceToDestination)
                        {
                            ParallelAlgorithm.MergeInnerPar(Utils.Source, currentCall.Left,
                                                            middle, middle + 1, currentCall.Right, Utils.Destination, currentCall.Left);
                        }
                        else
                        {
                            ParallelAlgorithm.MergeInnerPar(Utils.Destination, currentCall.Left,
                                                            middle, middle + 1, currentCall.Right, Utils.Source, currentCall.Left);
                        }
                    }

                    Environment.StopAll();
                }

                break;
            }
        }
Exemplo n.º 9
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            switch (baseMessage.Action)
            {
            case Actions.Done:
                Broadcast(message.Sender, new BaseMessage {
                    Action = Actions.Restart
                }.ToString());
                _phase++;
                break;

            case Actions.IsSorted:
                _done++;

                if (_done == 2)
                {
                    Stop();
                }

                break;
            }
        }
Exemplo n.º 10
0
        public override void Act(ActressMas.Message message)
        {
            try
            {
                if (_turns == 0)
                {
                    Thread.Sleep(Utils.Delay);
                }

                Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

                string action; string parameters;
                Utils.ParseMessage(message.Content, out action, out parameters);

                for (int i = 0; i < States.Length; i++)
                {
                    if (States[i] == TerrainState.GettingWater)
                    {
                        States[i] = TerrainState.Water;
                    }
                }

                switch (action)
                {
                case "move":
                    if (_turns == 4)
                    {
                        States[4] = TerrainState.Fire;                  // at some point, start a fire
                    }
                    if (_turns == 20)
                    {
                        States[7] = TerrainState.Fire;
                    }
                    if (_turns == 50)
                    {
                        States[9] = TerrainState.Fire;
                    }

                    if (parameters == "right")
                    {
                        PatrolPosition++;
                    }
                    else if (parameters == "left")
                    {
                        PatrolPosition--;
                    }

                    UpdateVisualField(message.Sender);
                    _turns++;
                    break;

                case "get-water":
                    if (States[PatrolPosition] == TerrainState.Water)
                    {
                        States[PatrolPosition] = TerrainState.GettingWater;
                        Send(message.Sender, "got-water");
                    }
                    UpdateVisualField(message.Sender);
                    break;

                case "drop-water":
                    if (States[PatrolPosition] == TerrainState.Fire)
                    {
                        States[PatrolPosition] = TerrainState.Normal;
                        Send(message.Sender, "fire-out");
                        UpdateVisualField(message.Sender);
                    }
                    break;

                default:
                    break;
                }

                _formGui.UpdateTerrainGUI();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //Console.WriteLine(ex.ToString()); // for debugging
            }
        }
Exemplo n.º 11
0
        public override void Act(ActressMas.Message message)
        {
            try
            {
                Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

                string action;
                string parameters;
                Utils.ParseMessage(message.Content, out action, out parameters);

                switch (action)
                {
                case "done-asignData":
                    doneAssign++;
                    // first split [L,R]
                    if (doneAssign == 2)
                    {
                        while (!Utils.finalSplit)
                        {
                            for (int i = 1; i < Utils.agentPool.Count; i += 1)
                            {
                                Send(Utils.agentPool[i], "split-own");
                            }
                        }
                    }

                    break;

                case "split-own":
                    int[] sp1_own = data.Take((data.Length + 1) / 2).ToArray();
                    int[] sp2_own = data.Skip((data.Length + 1) / 2).ToArray();
                    if (sp1_own.Length >= Utils.Threshold && sp2_own.Length >= Utils.Threshold)
                    {
                        int a = CreateWorker(Utils.agentPool.Count);
                        int b = CreateWorker(Utils.agentPool.Count);
                        Thread.Sleep(200);
                        Send(Utils.agentPool[a], Utils.Str("asign", fromArrayToString(sp1_own)));
                        Send(Utils.agentPool[b], Utils.Str("asign", fromArrayToString(sp2_own)));
                    }
                    else
                    {
                        if (this.data.Length >= Utils.Threshold)
                        {
                            Utils.validagentPool.Add(this.Name);
                        }
                        Utils.finalSplit = true;
                        Send(Agents.MasterAgent, Utils.Str("done-phase1", this.Name));
                    }
                    break;

                //main split from [Worker00]
                case "split":

                    int[] toSplit = fromStringToArray(parameters);
                    int[] sp1     = toSplit.Take((toSplit.Length + 1) / 2).ToArray();
                    int[] sp2     = toSplit.Skip((toSplit.Length + 1) / 2).ToArray();
                    if (sp1.Length >= 2 && sp2.Length >= 2)
                    {
                        CreateWorker(Utils.agentPool.Count);
                        CreateWorker(Utils.agentPool.Count);
                        Thread.Sleep(200);
                        Send(Utils.agentPool[1], Utils.Str("asignData", fromArrayToString(sp1)));
                        Send(Utils.agentPool[2], Utils.Str("asignData", fromArrayToString(sp2)));
                    }
                    break;

                case "asign":
                    Assign(parameters);
                    break;

                case "asignData":
                    Assign(parameters);
                    Send(Utils.agentPool[0], "done-asignData");

                    break;

                case "sort-asignData":
                    //SortAndAssign(parameters);
                    Sort();
                    Send(Agents.MasterAgent, "done-sort-asignData");
                    break;

                //i+1
                case "merge-and-compare":
                    //send i -> data  ->merge
                    int index = Utils.agentPool.IndexOf(this.Name);
                    if (index != 0 && index != Utils.numAg)
                    {
                        Send(Utils.agentPool[index - 1], Utils.Str("merge", fromArrayToString(this.data)));
                    }
                    Utils.agentPool.Remove(this.Name);
                    Stop();
                    //stop
                    break;

                case "merge":
                    int[] received = fromStringToArray(parameters);
                    int[] own      = this.data;
                    int[] result   = received
                                     .Concat(own)
                                     .OrderBy(x => x)
                                     .ToArray();
                    this.data = result;

                    break;

                case "print":

                    Console.WriteLine();
                    for (int i = 0; i < data.Length; i++)
                    {
                        Utils.Destination[i] = data[i];
                        Console.Write(data[i] + "_  ");
                    }
                    Stop();
                    break;

                case "terminate":
                    Utils.agentPool.Remove(this.Name);
                    Stop();
                    break;

                //stop - without removing from agentPool
                case "terminate2":
                    Stop();
                    break;
                }
            }
            catch (Exception e)
            {
                Stop();
            }
        }
Exemplo n.º 12
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            Console.WriteLine("\t[{1} -> {0}]: {2}", this.Name, message.Sender, message.Content);

            string action;
            string parameters;

            Utils.ParseMessage(message.Content, out action, out parameters);

            switch (baseMessage.Action)
            {
            case Actions.AssignAndSort:
                asignData(parameters);
                //Sort();
                Sort2();
                Send(Agents.MasterAgent, new BaseMessage
                {
                    Action = Actions.DoneAssignAndSort
                }.ToString());
                break;

            case Actions.EvenPhase:

                if (hasNeig(this.Name))
                {
                    int indx = Utils.agentPool.IndexOf(this.Name);
                    Send(Utils.agentPool[indx + 1], Utils.Str(new BaseMessage
                    {
                        Action = Actions.MergeAndSplit
                    }.ToString(), fromArray(this.data)));
                }

                break;

            case Actions.OddPhase:

                if (hasNeig(this.Name))
                {
                    int indx = Utils.agentPool.IndexOf(this.Name);
                    Send(Utils.agentPool[indx + 1], Utils.Str(new BaseMessage
                    {
                        Action = Actions.MergeAndSplit
                    }.ToString(), fromArray(this.data)));
                }
                break;

            case Actions.MergeAndSplit:

                Console.WriteLine("Receive: " + parameters + " Own: " + fromArray(this.data));

                int[] received = asignData(parameters, 0);
                int[] own      = this.data;
                int[] result   = received
                                 .Concat(own)
                                 .OrderBy(x => x)
                                 .ToArray();
                int[] sendBck = result.Take((result.Length + 1) / 2).ToArray();
                this.data = result.Skip((result.Length + 1) / 2).ToArray();
                Send(message.Sender, Utils.Str(new BaseMessage
                {
                    Action = Actions.RefreshData
                }.ToString(), fromArray(sendBck)));

                break;

            case Actions.RefreshData:
                Console.Write("  Agent: " + this.Name + " data:  ");
                for (int i = 0; i < data.Length; i++)
                {
                    Console.Write(data[i] + ". ");
                }
                Console.WriteLine();
                Console.WriteLine("  will update data with : " + parameters);
                this.data = asignData(parameters, 0);
                break;

            case Actions.PrintData:
                for (int i = 0; i < data.Length; i++)
                {
                    Utils.lsort.Add(data[i]);
                }

                Utils.destination.Add(this.Name, this.data);
                Send(Agents.MasterAgent, new BaseMessage
                {
                    Action = Actions.DonePrint
                }.ToString());
                Stop();
                break;
            }
        }
Exemplo n.º 13
0
        public override void Act(ActressMas.Message message)
        {
            var baseMessage = new BaseMessage(message.Content);

            switch (baseMessage.Action)
            {
            case Actions.Sort:

                if (Utils.SkipNext)
                {
                    Send(Agents.MasterAgent, new BaseMessage
                    {
                        Action = Actions.Done
                    }.ToString());

                    Stop();
                }

                int blockSize = (int)decimal.Round((decimal)Utils.Length / Utils.NoAgents);
                _startIndex = blockSize * _agentIndex;

                if (Utils.NoAgents - 1 == _agentIndex || _startIndex + blockSize > Utils.Length)
                {
                    _endIndex = Utils.Length;
                }
                else
                {
                    _endIndex = (_agentIndex + 1) * blockSize;
                }

                if (_endIndex == Utils.Length)
                {
                    Utils.SkipNext = true;
                }

                for (int i = _startIndex; i < _endIndex; i++)
                {
                    int value = Utils.Source[i];
                    int rank  = 0;

                    for (int j = 0; j < Utils.Length; j++)
                    {
                        if (value > Utils.Source[j])
                        {
                            rank++;
                        }

                        if (value == Utils.Source[j] && (i < j))
                        {
                            rank++;
                        }
                    }

                    Utils.Destination[rank] = value;
                }

                Send(Agents.MasterAgent, new BaseMessage
                {
                    Action = Actions.Done
                }.ToString());

                Stop();

                break;
            }
        }