Answers.Body: <p>For this question:</p>
  <p>Is there a design approach that will allow me to add additional rules without having to go back-and-forth the VehicleState class and add/modify properties in order to make sure that every possible type of Percept can be handled by the agent's internal state?,</p>
  <p>I think that you can implement a class called  policy or stragegy, with different subclasses, and each object could have a different strategy.</p>
  <p>To construct a kind of mindset refering to multi-agent simulations I think that you can find some inspiration in <a href="http://cormas.cirad.fr/" rel="nofollow noreferrer">http://cormas.cirad.fr/</a>.</p>
  <p>I have worked in some related problems, and it should be useful to look at <a href="https://agritrop.cirad.fr/541188/" rel="nofollow noreferrer">https://agritrop.cirad.fr/541188/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-31T10:57:44.770Z
Answers.LastActivityDate: 2021-07-31T11:14:32.660Z
Answers.LastEditDate: 2021-07-31T11:14:32.660Z
Answers.UserDisplayName: Hermes Morales
Body: <p>I had no idea that there is a stack exchange community for A.I. :-/ So I repost this question here in hope of some guidelines. I tried to delve into the materials discussed in AI: A Modern Approach course book, I am struggling to wrap my head around the model I'm trying to build without some code examples to aid me fill some gaps.</p>
  <p>I have a hard time understanding how to combine a rule-based decision making approach for an agent in an agent-based model I try to develop.</p>
  <p>The interface of the agent is a very simple one.</p>
  <pre><code>public interface IAgent
  {
     string ID { get; }
  
     void Execute(IAgentMessage message,
                  IActionScheduler actionScheduler);
  }
  </code></pre>
  <p>For the sake of the example, let's assume that the agents represent Vehicles which traverse roads inside a large warehouse, in order to load and unload their cargo. Their route (sequence of roads, from the start point until the agent's destination) is assigned by another agent, the Supervisor. The goal of a vehicle agent is to traverse its assigned route, unload the cargo, load a new one, receive another assigned route by the Supervisor and repeat the process.</p>
  <p>The vehicles must also be aware of potential collisions, for example at intersection points, and give priority based on some rules (for example, the one carrying the heaviest cargo has priority).</p>
  <p>As far as I can understand, this is the internal structure of the agents I want to build:</p>
  <p><a href="https://i.sstatic.net/fecyQm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fecyQm.png" alt="enter image description here" /></a></p>
  <p>So the Vehicle Agent can be something like:</p>
  <pre><code>public class Vehicle : IAgent
  {
    public VehicleStateUpdater { get; set; }
  
    public RuleSet RuleSet { get; set; }
  
    public VehicleState State { get; set; }
  
    public void Execute(IAgentMessage message, IActionScheduler actionScheduler)
    {
      VehicleStateUpdater.UpdateState(VehicleState, message);
      Rule validRule = RuleSet.Match(VehicleState);
      VehicleStateUpdater.UpdateState(VehicleState, validRule);
      validRule.Fire(this, VehicleState, actionScheduler);
    }
  }
  </code></pre>
  <p>For the Vehicle agent's internal state I was considering something like:</p>
  <pre><code>public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  }
  </code></pre>
  <p>For this example, 3 rules must be implemented for the Vehicle Agent.</p>
  <ol>
  <li>If another vehicle is near the agent (e.g. less than 50 meters), then the one with the heaviest cargo has priority, and the other agents must hold their position.</li>
  <li>When an agent reaches their destination, they unload the cargo, load a new one and wait for the Supervisor to assign a new route.</li>
  <li>At any given moment, the Supervisor, for whatever reason, might send a command, which the recipient vehicle must obey (Hold Position or Continue).</li>
  </ol>
  <p>The VehicleStateUpdater must take into consideration the current state of the agent, the type of received percept and change the state accordingly. So, in order for the state to reflect that e.g. a command was received by the Supervisor, one can modify it as follows:</p>
  <pre><code>public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  
    // Additional Property
    public RadioCommand ActiveCommand { get; set; }
  }
  </code></pre>
  <p>Where RadioCommand can be an enumeration with values None, Hold, Continue.</p>
  <p>But now I must also register in the agent's state if another vehicle is approaching. So I must add another property to the VehicleState.</p>
  <pre><code>public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  
    public RadioCommand ActiveCommand { get; set; }
  
    // Additional properties
    public bool IsAnotherVehicleApproaching { get; set; }
  
    public Location ApproachingVehicleLocation { get; set; }
  }
  </code></pre>
  <p>This is where I have a huge trouble understanding how to proceed and I get a feeling that I do not really follow the correct approach. First, I am not sure how to make the VehicleState class more modular and extensible. Second, I am not sure how to implement the rule-based part that defines the decision making process. Should I create mutually exclusive rules (which means every possible state must correspond to no more than one rule)? Is there a design approach that will allow me to add additional rules without having to go back-and-forth the VehicleState class and add/modify properties in order to make sure that every possible type of Percept can be handled by the agent's internal state?</p>
  <p>The examples I've seen in the Artificial Intelligence: A Modern Approach course book and in other sources are too simple for me to &quot;grasp&quot; the concept in question when a more complex model must be designed.</p>
  <p>I would be grateful if someone can point me in the right direction concerning the implementation of the rule-based part.</p>
  <p>I am writing in C# but as far as I can tell it is not really relevant to the broader issue I am trying to solve.</p>
  <p>An example of a rule I tried to incorporate:</p>
  <pre><code>public class HoldPositionCommandRule : IAgentRule&lt;VehicleState&gt;
  {
      public int Priority { get; } = 0;
  
      public bool ConcludesTurn { get; } = false;
  
  
      public void Fire(IAgent agent, VehicleState state, IActionScheduler actionScheduler)
      {
          state.Navigator.IsMoving = false;
          //Use action scheduler to schedule subsequent actions...
      }
  
      public bool IsValid(VehicleState state)
      {
          bool isValid = state.RadioCommandHandler.HasBeenOrderedToHoldPosition;
          return isValid;
      }
  }
  </code></pre>
  <p>A sample of the agent decision maker that I also tried to implement.</p>
  <pre><code>public void Execute(IAgentMessage message,
                      IActionScheduler actionScheduler)
  {
      _agentStateUpdater.Update(_state, message);
      Option&lt;IAgentRule&lt;TState&gt;&gt; validRule = _ruleMatcher.Match(_state);
      validRule.MatchSome(rule =&gt; rule.Fire(this, _state, actionScheduler));
  }
  </code></pre>
  
Tags: <multi-agent-systems><rule-based-systems>
Title: How to implement a rule-based decision maker for an agent-based model?
UserDisplayName: Vector Zita
ThreadText: # How to implement a rule-based decision maker for an agent-based model?
  
  (Asked by: username_0 on 2021-07-28)
  
  I had no idea that there is a stack exchange community for A.I. :-/ So I repost this question here in hope of some guidelines. I tried to delve into the materials discussed in AI: A Modern Approach course book, I am struggling to wrap my head around the model I'm trying to build without some code examples to aid me fill some gaps.
  I have a hard time understanding how to combine a rule-based decision making approach for an agent in an agent-based model I try to develop.
  The interface of the agent is a very simple one.
  public interface IAgent
  {
     string ID { get; }
  
     void Execute(IAgentMessage message,
                  IActionScheduler actionScheduler);
  }
  
  For the sake of the example, let's assume that the agents represent Vehicles which traverse roads inside a large warehouse, in order to load and unload their cargo. Their route (sequence of roads, from the start point until the agent's destination) is assigned by another agent, the Supervisor. The goal of a vehicle agent is to traverse its assigned route, unload the cargo, load a new one, receive another assigned route by the Supervisor and repeat the process.
  The vehicles must also be aware of potential collisions, for example at intersection points, and give priority based on some rules (for example, the one carrying the heaviest cargo has priority).
  As far as I can understand, this is the internal structure of the agents I want to build:
  
  So the Vehicle Agent can be something like:
  public class Vehicle : IAgent
  {
    public VehicleStateUpdater { get; set; }
  
    public RuleSet RuleSet { get; set; }
  
    public VehicleState State { get; set; }
  
    public void Execute(IAgentMessage message, IActionScheduler actionScheduler)
    {
      VehicleStateUpdater.UpdateState(VehicleState, message);
      Rule validRule = RuleSet.Match(VehicleState);
      VehicleStateUpdater.UpdateState(VehicleState, validRule);
      validRule.Fire(this, VehicleState, actionScheduler);
    }
  }
  
  For the Vehicle agent's internal state I was considering something like:
  public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  }
  
  For this example, 3 rules must be implemented for the Vehicle Agent.
  
  If another vehicle is near the agent (e.g. less than 50 meters), then the one with the heaviest cargo has priority, and the other agents must hold their position.
  When an agent reaches their destination, they unload the cargo, load a new one and wait for the Supervisor to assign a new route.
  At any given moment, the Supervisor, for whatever reason, might send a command, which the recipient vehicle must obey (Hold Position or Continue).
  
  The VehicleStateUpdater must take into consideration the current state of the agent, the type of received percept and change the state accordingly. So, in order for the state to reflect that e.g. a command was received by the Supervisor, one can modify it as follows:
  public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  
    // Additional Property
    public RadioCommand ActiveCommand { get; set; }
  }
  
  Where RadioCommand can be an enumeration with values None, Hold, Continue.
  But now I must also register in the agent's state if another vehicle is approaching. So I must add another property to the VehicleState.
  public class VehicleState
  {
    public Route Route { get; set; }
  
    public Cargo Cargo { get; set; }
  
    public Location CurrentLocation { get; set; }
  
    public RadioCommand ActiveCommand { get; set; }
  
    // Additional properties
    public bool IsAnotherVehicleApproaching { get; set; }
  
    public Location ApproachingVehicleLocation { get; set; }
  }
  
  This is where I have a huge trouble understanding how to proceed and I get a feeling that I do not really follow the correct approach. First, I am not sure how to make the VehicleState class more modular and extensible. Second, I am not sure how to implement the rule-based part that defines the decision making process. Should I create mutually exclusive rules (which means every possible state must correspond to no more than one rule)? Is there a design approach that will allow me to add additional rules without having to go back-and-forth the VehicleState class and add/modify properties in order to make sure that every possible type of Percept can be handled by the agent's internal state?
  The examples I've seen in the Artificial Intelligence: A Modern Approach course book and in other sources are too simple for me to "grasp" the concept in question when a more complex model must be designed.
  I would be grateful if someone can point me in the right direction concerning the implementation of the rule-based part.
  I am writing in C# but as far as I can tell it is not really relevant to the broader issue I am trying to solve.
  An example of a rule I tried to incorporate:
  public class HoldPositionCommandRule : IAgentRule
  {
      public int Priority { get; } = 0;
  
      public bool ConcludesTurn { get; } = false;
  
  
      public void Fire(IAgent agent, VehicleState state, IActionScheduler actionScheduler)
      {
          state.Navigator.IsMoving = false;
          //Use action scheduler to schedule subsequent actions...
      }
  
      public bool IsValid(VehicleState state)
      {
          bool isValid = state.RadioCommandHandler.HasBeenOrderedToHoldPosition;
          return isValid;
      }
  }
  
  A sample of the agent decision maker that I also tried to implement.
  public void Execute(IAgentMessage message,
                      IActionScheduler actionScheduler)
  {
      _agentStateUpdater.Update(_state, message);
      Option> validRule = _ruleMatcher.Match(_state);
      validRule.MatchSome(rule => rule.Fire(this, _state, actionScheduler));
  }
  
  **Tags:** multi-agent-systems, rule-based-systems
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-07-31. Score: 0
  
  For this question:
  
  Is there a design approach that will allow me to add additional rules without having to go back-and-forth the VehicleState class and add/modify properties in order to make sure that every possible type of Percept can be handled by the agent's internal state?,
  
  I think that you can implement a class called policy or stragegy, with different subclasses, and each object could have a different strategy.
  
  To construct a kind of mindset refering to multi-agent simulations I think that you can find some inspiration in <http://cormas.cirad.fr/>.
  
  I have worked in some related problems, and it should be useful to look at <https://agritrop.cirad.fr/541188/>
  
  
  ---
  
  