Exemple #1
0
        //the step cost is the amount of water that's been transfered. To get it just sum up the absolute val of the differences
        //in each jug from one state to the next, and divide it by the number of jugs that had a change
        public double calculateStepCost(Object fromState, Object toState, String action)
        {
            Q2State q2FromState = (Q2State)fromState;
            Q2State q2ToState   = (Q2State)toState;

            int difference = Math.Abs(q2FromState.jugArray[0] - q2ToState.jugArray[0])
                             + Math.Abs(q2FromState.jugArray[1] - q2ToState.jugArray[1])
                             + Math.Abs(q2FromState.jugArray[2] - q2ToState.jugArray[2]);

            //now find out how many jugs were changed (should only ever be 1 or 2)
            int count = 0;

            if (Math.Abs(q2FromState.jugArray[0] - q2ToState.jugArray[0]) > 0)
            {
                count += 1;
            }

            if (Math.Abs(q2FromState.jugArray[1] - q2ToState.jugArray[1]) > 0)
            {
                count += 1;
            }

            if (Math.Abs(q2FromState.jugArray[2] - q2ToState.jugArray[2]) > 0)
            {
                count += 1;
            }

            return((double)(difference / count));
        }
Exemple #2
0
 public Q2State(Q2State state)
 {
     for (int i = 0; i < 3; i++)
     {
         this.jugArray[i] = state.jugArray[i];
     }
 }
Exemple #3
0
        public bool isGoalState(Object state)
        {
            Q2State jstate = (Q2State)state;

            if (jstate.jugArray[0] == 1 || jstate.jugArray[1] == 1 || jstate.jugArray[2] == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #4
0
        public override bool Equals(object obj)
        {
            Q2State state = (Q2State)obj;

            for (int i = 0; i < 3; i++)
            {
                if (this.jugArray[i] != state.jugArray[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #5
0
        public ArrayList getSuccessors(Object state)
        {
            ArrayList list   = new ArrayList(20);
            Q2State   jstate = (Q2State)state;

            //By filling
            for (int i = 0; i < 3; i++)
            {
                if (jstate.jugArray[i] < Q2State.capacityArray[i])
                {
                    Q2State successor_state = new Q2State(jstate);
                    successor_state.jugArray[i] = Q2State.capacityArray[i];
                    Successor s = new Successor("fill " + i, successor_state);
                    list.Add(s);
                }
            }
            //By emptying
            for (int i = 0; i < 3; i++)
            {
                if (jstate.jugArray[i] > 0)
                {
                    Q2State successor_state = new Q2State(jstate);
                    successor_state.jugArray[i] = 0;
                    Successor s = new Successor("empty " + i, successor_state);
                    list.Add(s);
                }
            }
            //By pouring j to i
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (j != i)
                    {
                        int newjug_i = min(jstate.jugArray[i] + jstate.jugArray[j], Q2State.capacityArray[i]);
                        int juginc_i = newjug_i - jstate.jugArray[i];
                        if (juginc_i > 0)
                        {
                            Q2State successor_state = new Q2State(jstate);
                            successor_state.jugArray[i] += juginc_i;
                            successor_state.jugArray[j] -= juginc_i;
                            Successor s = new Successor("pouring " + j + " to " + i, successor_state);
                            list.Add(s);
                        }
                    }
                }
            }
            return(list);
        }
Exemple #6
0
		public ArrayList getSuccessors(Object state)
		{
			ArrayList list = new ArrayList(20);
			Q2State jstate = (Q2State) state;
        
			//By filling
			for(int i=0; i<3; i++) 
			{
				if (jstate.jugArray[i] < Q2State.capacityArray[i]) 
				{
					Q2State successor_state = new Q2State(jstate);
					successor_state.jugArray[i] = Q2State.capacityArray[i];
					Successor s = new Successor("fill " + i, successor_state);
					list.Add(s);
				}
			}
			//By emptying
			for(int i=0; i<3; i++) 
			{
				if (jstate.jugArray[i] > 0) 
				{
					Q2State successor_state = new Q2State(jstate);
					successor_state.jugArray[i] = 0;
					Successor s = new Successor("empty " + i, successor_state);
					list.Add(s);
				}
			}                   
			//By pouring j to i
			for(int i=0; i<3; i++) 
			{
				for(int j=0; j<3; j++) 
				{
					if (j != i) 
					{
						int newjug_i = min(jstate.jugArray[i] + jstate.jugArray[j], Q2State.capacityArray[i]);
						int juginc_i = newjug_i - jstate.jugArray[i];
						if (juginc_i > 0) 
						{
							Q2State successor_state = new Q2State(jstate);
							successor_state.jugArray[i] += juginc_i;
							successor_state.jugArray[j] -= juginc_i;
							Successor s = new Successor("pouring " + j + " to " + i, successor_state);
							list.Add(s);
						}
					}
				}
			}        
			return list;
		}
Exemple #7
0
        private void btnJugs_Click(object sender, System.EventArgs e)
        {
            this.textBox1.Text = ("Jugs Puzzle -->" + System.Environment.NewLine);

            Q2State initialState = new Q2State();

            try
            {
                Problem problem = new Problem(initialState,
                                              new Q2SuccessorFunction(),
                                              new Q2GoalTest(),
                                              new Q2StepCostFunction());

                Search search = new BreadthFirstSearch(new TreeSearch());
                //Search search = new BreadthFirstSearch(new GraphSearch());
                //Search search = new DepthFirstSearch(new TreeSearch());
                //Search search = new DepthFirstSearch(new GraphSearch());
                //Search search = new DepthLimitedSearch(12);
                //Search search = new IterativeDeepeningSearch();
                //Search search = new AStarSearch(new TreeSearch());

                ArrayList solution = search.search(problem);

                if (solution.Count == 0)
                {                 //empty list means failure
                    this.textBox1.Text += (System.Environment.NewLine + "No Solution" + System.Environment.NewLine);
                }
                //this.textBox1.Text += (solution + System.Environment.NewLine);
                for (int i = 0; i < solution.Count; i++)
                {
                    this.textBox1.Text += solution[i].ToString() + System.Environment.NewLine;
                }

                //Printing metrics
                Metrics     searchMetrics = search.getMetrics();
                IEnumerator iter          = searchMetrics.keySet().GetEnumerator();
                while (iter.MoveNext())
                {
                    string key   = iter.Current.ToString();
                    string value = searchMetrics.get(key);
                    this.textBox1.Text += (key + ": " + value);
                }
            }
            catch (Exception ex)
            {
                this.textBox1.Text += ex.Message;
            }
        }
Exemple #8
0
		private void btnJugs_Click(object sender, System.EventArgs e)
		{
			this.textBox1.Text = ("Jugs Puzzle -->" + System.Environment.NewLine);
        
			Q2State initialState = new Q2State();
			try 
			{
				Problem problem = new Problem(initialState, 
					new Q2SuccessorFunction(),
					new Q2GoalTest(),
					new Q2StepCostFunction());
            
				Search search = new BreadthFirstSearch(new TreeSearch());
				//Search search = new BreadthFirstSearch(new GraphSearch());
				//Search search = new DepthFirstSearch(new TreeSearch());
				//Search search = new DepthFirstSearch(new GraphSearch());
				//Search search = new DepthLimitedSearch(12);
				//Search search = new IterativeDeepeningSearch();
				//Search search = new AStarSearch(new TreeSearch());
            
				ArrayList solution = search.search(problem);
            
				if (solution.Count == 0) 
				{ //empty list means failure
					this.textBox1.Text += (System.Environment.NewLine + "No Solution" +System.Environment.NewLine);
				}
				//this.textBox1.Text += (solution + System.Environment.NewLine);
				for (int i = 0; i < solution.Count; i++)
				{
					this.textBox1.Text += solution[i].ToString() + System.Environment.NewLine;
				}
            
				//Printing metrics
				Metrics searchMetrics = search.getMetrics();
				IEnumerator iter = searchMetrics.keySet().GetEnumerator();
				while (iter.MoveNext()) 
				{
					string key = iter.Current.ToString();
					string value = searchMetrics.get(key);
					this.textBox1.Text += (key +": " + value);
				}
            
			} 
			catch (Exception ex) 
			{
				this.textBox1.Text += ex.Message;
			}
		}
Exemple #9
0
		public Q2State(Q2State state) 
		{
			for(int i=0; i<3; i++) 
				this.jugArray[i] = state.jugArray[i];
		}