Advertisement
Guest User

StateSpaceRepresentationFollowingTheMathematicalDescription

a guest
Sep 30th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace StateSpaceRepresentationFollowingTheMathematicalDescription
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<Operator> operators = new List<Operator>();
  14.             operators.Add(new MoveMonksAndCannibals(0,1));
  15.             operators.Add(new MoveMonksAndCannibals(0,2));
  16.             operators.Add(new MoveMonksAndCannibals(1,0));
  17.             operators.Add(new MoveMonksAndCannibals(2,0));
  18.             operators.Add(new MoveMonksAndCannibals(1, 1));  
  19.             State startState = new MonksAndCannibalsState();
  20.             StateSpaceRepresentation monksAndCannibalsProblem = new StateSpaceRepresentation(startState, operators);
  21.             BackTrack bt = new BackTrack(monksAndCannibalsProblem);
  22.             Node terminalNode = bt.GraphSearch();
  23.             if (terminalNode == null) Console.WriteLine("There is no solution");
  24.             else
  25.             {
  26.                 Console.WriteLine("A solution is:");
  27.                 terminalNode.PrintPath();
  28.             }
  29.             Console.ReadLine();
  30.         }
  31.     }
  32.     /// <summary>
  33.     /// The common interface for states of the state-space representation
  34.     /// </summary>
  35.     abstract class State
  36.     {
  37.         public abstract bool IsState();
  38.         public abstract bool IsGoalState();
  39.         public override abstract bool Equals(Object a);
  40.         public override int GetHashCode() { return base.GetHashCode(); }
  41.     }
  42.     /// <summary>
  43.     /// The common interface for operators of the state-space representation
  44.     /// </summary>
  45.     abstract class Operator
  46.     {
  47.         public State Operate(State a)
  48.         {
  49.             if (!PreCondition(a)) return null;
  50.             State b = operate(a);
  51.             if (!PostCondition(b)) return null;
  52.             return b;
  53.         }
  54.         public abstract bool PreCondition(State a);
  55.         protected abstract State operate(State a);
  56.         public abstract bool PostCondition(State a);
  57.         public bool IsApplicableOn(State a)
  58.         {
  59.             if (!PreCondition(a)) return false;
  60.             State b = operate(a);
  61.             if (!PostCondition(b)) return false;
  62.             return true;
  63.         }
  64.     }
  65.     class StateSpaceRepresentation
  66.     {
  67.         State startState;
  68.         List<Operator> operators;
  69.         public StateSpaceRepresentation(State startState, List<Operator> operators)
  70.         {
  71.             this.startState = startState;
  72.             this.operators = operators;
  73.         }
  74.         public bool IsState(State state) { return state.IsState(); }
  75.         public State GetStartState() { return startState; }
  76.         public bool IsGoalState(State state) { return state.IsGoalState(); }
  77.         public List<Operator> GetOperators() { return operators; }
  78.     }
  79.  
  80.     /// <summary>
  81.     /// Monks and Cannibals Problem
  82.     /// Note that this class is immutable!
  83.     /// </summary>
  84.     class MonksAndCannibalsState : State
  85.     {
  86.         int monksOnLeft, cannibalsOnLeft, boat, monksOnRight, cannibalsOnRight;
  87.         public int MonksOnLeft { get { return monksOnLeft; } }
  88.         public int CannibalsOnLeft { get { return cannibalsOnLeft; } }
  89.         public int Boat { get { return boat; } }
  90.         public int MonksOnRight { get { return monksOnRight; } }
  91.         public int CannibalsOnRight { get { return cannibalsOnRight; } }
  92.  
  93.         public MonksAndCannibalsState(int monksOnLeft, int cannibalsOnLeft, int boat, int monksOnRight, int cannibalsOnRight)
  94.         {
  95.             this.monksOnLeft = monksOnLeft;
  96.             this.cannibalsOnLeft = cannibalsOnLeft;
  97.             this.boat = boat;
  98.             this.monksOnRight = monksOnRight;
  99.             this.cannibalsOnRight = cannibalsOnRight;
  100.         }
  101.  
  102.         // this creates the start state
  103.         public MonksAndCannibalsState()
  104.         {
  105.             monksOnLeft = 3;
  106.             cannibalsOnLeft = 3;
  107.             boat = 1; // 1: left, -1: right
  108.             monksOnRight = 0;
  109.             cannibalsOnRight = 0;
  110.         }
  111.         public override bool IsState()
  112.         {
  113.             return monksOnLeft + monksOnRight == 3 &&
  114.                 cannibalsOnLeft + cannibalsOnRight == 3 &&
  115.                 monksOnLeft >= 0 && monksOnLeft <= 3 &&
  116.                 cannibalsOnLeft >= 0 && cannibalsOnLeft <= 3 &&
  117.                 monksOnRight >= 0 && monksOnLeft <= 3 &&
  118.                 cannibalsOnRight >= 0 && cannibalsOnRight <= 3 &&
  119.                 (boat == 1 || boat == -1) &&
  120.                 (monksOnLeft >= cannibalsOnLeft || monksOnLeft == 0) &&
  121.                 (monksOnRight >= cannibalsOnRight || monksOnRight == 0);
  122.         }
  123.         public override bool IsGoalState()
  124.         {
  125.             return monksOnRight == 3 && cannibalsOnRight == 3 && boat == -1;
  126.         }
  127.         public override bool Equals(object obj)
  128.         {
  129.             MonksAndCannibalsState other = (MonksAndCannibalsState) obj;
  130.             return this.monksOnLeft == other.monksOnLeft &&
  131.                 this.cannibalsOnLeft == other.cannibalsOnLeft &&
  132.                 this.boat == other.boat &&
  133.                 this.monksOnRight == other.monksOnRight &&
  134.                 this.cannibalsOnRight == other.cannibalsOnRight;
  135.         }
  136.         public override int GetHashCode()
  137.         {
  138.             return (monksOnLeft.GetHashCode() * 1 +
  139.                 cannibalsOnLeft.GetHashCode() * 3 +
  140.                 monksOnRight.GetHashCode() * 5 +
  141.                 cannibalsOnRight.GetHashCode() * 7) * boat;
  142.         }
  143.         public override string ToString()
  144.         {
  145.             return monksOnLeft + ", " + cannibalsOnLeft + ", " + boat + ", " + monksOnRight + ", " + cannibalsOnRight;
  146.         }
  147.     }
  148.     class MoveMonksAndCannibals : Operator
  149.     {
  150.         int numberOfMonksToMove, numberOfCannibalsToMove;
  151.         public MoveMonksAndCannibals(int numberOfMonksToMove, int numberOfCannibalsToMove)
  152.         {
  153.             this.numberOfMonksToMove = numberOfMonksToMove;
  154.             this.numberOfCannibalsToMove = numberOfCannibalsToMove;
  155.         }
  156.         public override bool PreCondition(State a)
  157.         {
  158.             MonksAndCannibalsState state = (MonksAndCannibalsState)a;
  159.             if (state.Boat == 1) // the boat is on the left side
  160.             {
  161.                 return state.MonksOnLeft >= numberOfMonksToMove &&
  162.                        state.CannibalsOnLeft >= numberOfCannibalsToMove;
  163.             }
  164.             else // the boat is on the right side
  165.             {
  166.                 return state.MonksOnRight >= numberOfMonksToMove &&
  167.                        state.CannibalsOnRight >= numberOfCannibalsToMove;
  168.             }
  169.         }
  170.         protected override State operate(State a)
  171.         {
  172.             MonksAndCannibalsState state = (MonksAndCannibalsState)a;
  173.             int monksOnLeft, cannibalsOnLeft, boat, monksOnRight, cannibalsOnRight;
  174.             monksOnLeft = state.MonksOnLeft;
  175.             cannibalsOnLeft = state.CannibalsOnLeft;
  176.             boat = state.Boat;
  177.             monksOnRight = state.MonksOnRight;
  178.             cannibalsOnRight = state.CannibalsOnRight;
  179.             if (boat == 1) // the boat is on the left
  180.             {
  181.                 monksOnLeft -= numberOfMonksToMove;
  182.                 cannibalsOnLeft -= numberOfCannibalsToMove;
  183.                 boat = -1;
  184.                 monksOnRight += numberOfMonksToMove;
  185.                 cannibalsOnRight += numberOfCannibalsToMove;
  186.             }
  187.             else
  188.             {
  189.                 monksOnLeft += numberOfMonksToMove;
  190.                 cannibalsOnLeft += numberOfCannibalsToMove;
  191.                 boat = +1;
  192.                 monksOnRight -= numberOfMonksToMove;
  193.                 cannibalsOnRight -= numberOfCannibalsToMove;
  194.             }
  195.             return new MonksAndCannibalsState(monksOnLeft, cannibalsOnLeft, boat, monksOnRight, cannibalsOnRight);
  196.         }
  197.         public override bool PostCondition(State a)
  198.         {
  199.             return a.IsState();
  200.         }
  201.         public override string ToString()
  202.         {
  203.             return numberOfMonksToMove + ", " + numberOfCannibalsToMove;
  204.         }
  205.     }
  206.     class Node
  207.     {
  208.         State state;
  209.         int depth;
  210.         Node parentNode;
  211.         List<Operator> operators;
  212.         int indexOfLastTestedOperator = -1;
  213.         public Node(State state, List<Operator> operators)
  214.         {
  215.             this.state = state;
  216.             depth = 0;
  217.             parentNode = null;
  218.             this.operators = new List<Operator>(operators); // we clone the list of operators
  219.         }
  220.         public Node(State state, int depth, Node parentNode, List<Operator> operators) : this(state, operators)
  221.         {
  222.             this.depth = depth;
  223.             this.parentNode = parentNode;
  224.         }
  225.         public bool IsTerminalNode() { return state.IsGoalState(); }
  226.         public Node GetParentNode() { return parentNode; }
  227.         public int GetDepth() { return depth;  }
  228.         public Node GetNextChildNode()
  229.         {
  230.             State newState;
  231.             do
  232.             {
  233.                 indexOfLastTestedOperator++;
  234.                 if (indexOfLastTestedOperator == operators.Count) return null;
  235.                 newState = operators[indexOfLastTestedOperator].Operate(state);
  236.             } while (newState==null);
  237.             return new Node(newState, depth + 1, this, operators);
  238.         }
  239.         public override bool Equals(object obj)
  240.         {
  241.             Node csúcsToCompare = (Node)obj;
  242.             return state.Equals(csúcsToCompare.state);
  243.         }
  244.         public override int GetHashCode()
  245.         {
  246.             return state.GetHashCode();
  247.         }
  248.         public override string ToString()
  249.         {
  250.             return "depth = " + depth + ", state = " + state;
  251.         }
  252.         public void PrintPath()
  253.         {
  254.             Node actualNode = this;
  255.             Stack<Node> path = new Stack<Node>();
  256.             while (actualNode != null)
  257.             {
  258.                 path.Push(actualNode);
  259.                 actualNode = actualNode.GetParentNode();
  260.             }
  261.             foreach (Node act in path) Console.WriteLine(act);
  262.         }
  263.     }
  264.     class BackTrack
  265.     {
  266.         StateSpaceRepresentation problemRepresentation;
  267.         public BackTrack(StateSpaceRepresentation problemRepresentation)
  268.         {
  269.             this.problemRepresentation = problemRepresentation;
  270.         }
  271.         public Node GraphSearch()
  272.         {
  273.             State startState = problemRepresentation.GetStartState();
  274.             List<Operator> operators = problemRepresentation.GetOperators();
  275.             Node actualNode = new Node(startState, operators);
  276.             while(!actualNode.IsTerminalNode())
  277.             {
  278.                 Node newNode = actualNode.GetNextChildNode();
  279.                 while (newNode == null)
  280.                 {
  281.                     actualNode = actualNode.GetParentNode(); // backtrack
  282.                     if (actualNode == null) return null;
  283.                     newNode = actualNode.GetNextChildNode();
  284.                 }
  285.                 actualNode = newNode;
  286.                 // cycle detection
  287.                 Node parentNodeForCicleDetection = actualNode.GetParentNode();
  288.                 while (parentNodeForCicleDetection != null)
  289.                 {
  290.                     if (actualNode.Equals(parentNodeForCicleDetection))
  291.                     {
  292.                         actualNode = actualNode.GetParentNode(); // backtrack
  293.                         break;
  294.                     }
  295.                     parentNodeForCicleDetection = parentNodeForCicleDetection.GetParentNode();
  296.                 }
  297.             }
  298.             return actualNode;
  299.         }
  300.     }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement