示例#1
0
 public MoveToCommand(int agentId, int row, int col)
 {
     Name     = "moveto";
     AgentId  = agentId;
     Location = new Coords(row, col);
 }
示例#2
0
 public abstract IAgentCommand NextCommand(Coords location, bool isDirty);
示例#3
0
        private void _Run()
        {
            round = 0;
            State = SimState.RUNNING;

            foreach (var view in Views)
            {
                view.ShowState(round, new List <IAgentCommand>(), space.space, space.agentSpace);
            }
            PublishStats(round);
            foreach (var view in Views)
            {
                view.SimStarted(0, null);
            }

            // Start Simulation
            // Each agent is allowed one command per round of lifecycle
            // Viewer is updated after every round.
            // Simulation continues till simulation goal is reached.
            if (agents.Count > 0)
            {
                round = 1;
                while (space.HasDirty())
                {
                    if (Abort)
                    {
                        Abort = false;
                        State = SimState.ABORTED;
                        break;
                    }

                    List <IAgentCommand> commands = new List <IAgentCommand>();
                    foreach (var a in agents)
                    {
                        (int row, int col) = space.WhereAmI(a.AgentId);
                        bool dirty = space.IsDirty(row, col);
                        var  cmd   = a.NextCommand(new Coords(row, col), dirty);
                        try
                        {
                            if (cmd is CleanCommand)
                            {
                                Coords c = cmd.Location;
                                space.Clean(a.AgentId, c.Row, c.Column);
                            }
                            else if (cmd is MoveToCommand)
                            {
                                Coords c = cmd.Location;
                                space.MoveAgent(a.AgentId, c.Row, c.Column);
                            }
                            cmd.Status        = true;
                            cmd.FailureReason = null;
                            commands.Add(cmd);
                            a.CommandResult(true, null,
                                            SimulationErrorCode.SIM_ERR_SUCCESS, cmd.Location);
                        }
                        catch (SimulationException e)
                        {
                            cmd.Status        = false;
                            cmd.FailureReason = e.Message;
                            commands.Add(cmd);
                            a.CommandResult(false, e.Message, e.ErrorCode, e.Location);
                        }
                        catch (Exception e)
                        {
                            cmd.Status        = false;
                            cmd.FailureReason = e.Message;
                            commands.Add(cmd);
                            a.CommandResult(false, e.Message,
                                            SimulationErrorCode.SIM_ERR_UNKNOWN, cmd.Location);
                        }
                    }
                    foreach (var view in Views)
                    {
                        view.ShowState(round, commands, space.space, space.agentSpace);
                    }
                    PublishStats(round);
                    round += 1;
                }
            }

            foreach (var view in Views)
            {
                view.SimComplete();
            }

            Prepare();
        }