Exemple #1
0
        public bool isGoalState(Object state)
        {
            Q4State jstate = (Q4State)state;

            if (jstate.location.ToLower().Equals("bucharest"))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #2
0
        private void btnDoDemo_Click(object sender, System.EventArgs e)
        {
            this.textBox1.Text = (System.Environment.NewLine + "Romania Lugoj To Bucharest Puzzle -->");

            Q4State initialState = new Q4State("Lugoj");

            try
            {
                Problem problem = new Problem(initialState,
                                              new Q4SuccessorFunction(),
                                              new Q4GoalTest(),
                                              new Q4StepCostFunction(),
                                              new Q4HeuristicFunction());

                //Search search = new BreadthFirstSearch(new TreeSearch());
                //Search search = new DepthFirstSearch(new TreeSearch());
                //Search search = new DepthFirstSearch(new GraphSearch());
                //Search search = new DepthLimitedSearch(9);
                //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);
                }
                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   = (String)iter.Current;
                    String value = searchMetrics.get(key);
                    this.textBox1.Text += (System.Environment.NewLine + key + ": " + value);
                }
            }
            catch (Exception ex)
            {
                this.textBox1.Text += ex.Message;
            }
        }
Exemple #3
0
        public override bool Equals(object obj)
        {
            Q4State state = (Q4State)obj;

            return(location.ToLower().Equals(state.location.ToLower()));
        }
Exemple #4
0
 public Q4State(Q4State state)
 {
     this.location = state.location;
 }
Exemple #5
0
		public Q4State(Q4State state) 
		{
			this.location = state.location;
		}
Exemple #6
0
        public ArrayList getSuccessors(Object state)
        {
            ArrayList list   = new ArrayList(20);
            Q4State   jstate = (Q4State)state;

            string location = jstate.location;

            Successor s;
            Q4State   successor_state;


            //these functions are used to track the expanded nodes step/heuristic costs
            //Q4StepCostFunction sn = new Q4StepCostFunction();
            //Q4HeuristicFunction hn = new Q4HeuristicFunction();

            //System.out.println("Expanding " + location);

            if (location.Equals("Oradea"))
            {
                successor_state = new Q4State("Zerind");
                s = new Successor("go to  Zerind", successor_state);
                list.Add(s);

                successor_state = new Q4State("Sibiu");
                s = new Successor("go to  Sibiu", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Zerind"))
            {
                successor_state = new Q4State("Oradea");
                s = new Successor("go to  Oradea", successor_state);
                list.Add(s);

                successor_state = new Q4State("Arad");
                s = new Successor("go to Arad", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Arad"))
            {
                successor_state = new Q4State("Zerind");
                s = new Successor("go to Zerind", successor_state);
                list.Add(s);

                successor_state = new Q4State("Timisoara");
                s = new Successor("go to Timisoara", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Timisoara"))
            {
                successor_state = new Q4State("Arad");
                s = new Successor("go to Arad", successor_state);
                list.Add(s);

                successor_state = new Q4State("Lugoj");
                s = new Successor("go to  Lugoj", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Lugoj"))
            {
                successor_state = new Q4State("Timisoara");
                s = new Successor("go to Timisoara", successor_state);
                list.Add(s);

                successor_state = new Q4State("Mehadia");
                s = new Successor("go to Mehadia", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Mehadia"))
            {
                successor_state = new Q4State("Dobreta");
                s = new Successor("go to Dobreta", successor_state);
                list.Add(s);

                successor_state = new Q4State("Lugoj");
                s = new Successor("go to Lugoj", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Dobreta"))
            {
                successor_state = new Q4State("Mehadia");
                s = new Successor("go to Mehadia", successor_state);
                list.Add(s);

                successor_state = new Q4State("Craiova");
                s = new Successor("go to Craiova", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Sibiu"))
            {
                successor_state = new Q4State("Arad");
                s = new Successor("go to Arad", successor_state);
                list.Add(s);

                successor_state = new Q4State("Oradea");
                s = new Successor("go to Oradea", successor_state);
                list.Add(s);

                successor_state = new Q4State("Fagaras");
                s = new Successor("go to Fagaras", successor_state);
                list.Add(s);

                successor_state = new Q4State("Rimnicu Vilcea");
                s = new Successor("go to Rimnicu Vilcea", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Rimnicu Vilcea"))
            {
                successor_state = new Q4State("Sibiu");
                s = new Successor("go to Sibiu", successor_state);
                list.Add(s);

                successor_state = new Q4State("Pitesti");
                s = new Successor("go to Pitesti", successor_state);
                list.Add(s);

                successor_state = new Q4State("Craiova");
                s = new Successor("go to Craiova", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Craiova"))
            {
                successor_state = new Q4State("Rimnicu Vilcea");
                s = new Successor("go to Rimnicu Vilcea", successor_state);
                list.Add(s);

                successor_state = new Q4State("Dobreta");
                s = new Successor("go to Dobreta", successor_state);
                list.Add(s);

                successor_state = new Q4State("Pitesti");
                s = new Successor("go to Pitesti", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Pitesti"))
            {
                successor_state = new Q4State("Rimnicu Vilcea");
                s = new Successor("go to Rimnicu Vilcea", successor_state);
                list.Add(s);

                successor_state = new Q4State("Craiova");
                s = new Successor("go to Craiova", successor_state);
                list.Add(s);

                successor_state = new Q4State("Bucharest");
                s = new Successor("go to Bucharest", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Fagaras"))
            {
                successor_state = new Q4State("Sibiu");
                s = new Successor("go to Sibiu", successor_state);
                list.Add(s);

                successor_state = new Q4State("Bucharest");
                s = new Successor("go to Bucharest", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Bucharest"))
            {
                successor_state = new Q4State("Fagaras");
                s = new Successor("go to Fagaras", successor_state);
                list.Add(s);

                successor_state = new Q4State("Pitesti");
                s = new Successor("go to Pitesti", successor_state);
                list.Add(s);

                successor_state = new Q4State("Giurgiu");
                s = new Successor("go to Giurgiu", successor_state);
                list.Add(s);

                successor_state = new Q4State("Urziceni");
                s = new Successor("go to Urziceni", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Giurgiu"))
            {
                successor_state = new Q4State("Bucharest");
                s = new Successor("go to Bucharest", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Urziceni"))
            {
                successor_state = new Q4State("Bucharest");
                s = new Successor("go to Bucharest", successor_state);
                list.Add(s);

                successor_state = new Q4State("Vaslui");
                s = new Successor("go to Vaslui", successor_state);
                list.Add(s);

                successor_state = new Q4State("Hirsova");
                s = new Successor("go to Hirsova", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Vaslui"))
            {
                successor_state = new Q4State("Urziceni");
                s = new Successor("go to Urziceni", successor_state);
                list.Add(s);

                successor_state = new Q4State("Iasi");
                s = new Successor("go to Iasi", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Iasi"))
            {
                successor_state = new Q4State("Vaslui");
                s = new Successor("go to Vaslui", successor_state);
                list.Add(s);

                successor_state = new Q4State("Neamt");
                s = new Successor("go to Neamt", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Neamt"))
            {
                successor_state = new Q4State("Iasi");
                s = new Successor("go to Iasi", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Hirsova"))
            {
                successor_state = new Q4State("Urziceni");
                s = new Successor("go to Urziceni", successor_state);
                list.Add(s);

                successor_state = new Q4State("Eforie");
                s = new Successor("go to Eforie", successor_state);
                list.Add(s);
            }
            else if (location.Equals("Eforie"))
            {
                successor_state = new Q4State("Hirsova");
                s = new Successor("go to Hirsova", successor_state);
                list.Add(s);
            }



            return(list);
        }
Exemple #7
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
        public double calculateStepCost(Object fromState, Object toState, string action)
        {
            Q4State Q4FromState = (Q4State)fromState;
            Q4State Q4ToState   = (Q4State)toState;


            string from = Q4FromState.location;
            string to   = Q4ToState.location;

            if ((from.Equals("Oradea") && to.Equals("Zerind")) ||
                (from.Equals("Zerind") && to.Equals("Oradea")))
            {
                return(71);
            }
            else if ((from.Equals("Oradea") && to.Equals("Sibiu")) ||
                     (from.Equals("Sibiu") && to.Equals("Oradea")))
            {
                return(151);
            }
            else if ((from.Equals("Zerind") && to.Equals("Arad")) ||
                     (from.Equals("Arad") && to.Equals("Zerind")))
            {
                return(75);
            }
            else if ((from.Equals("Arad") && to.Equals("Sibiu")) ||
                     (from.Equals("Sibiu") && to.Equals("Arad")))
            {
                return(140);
            }
            else if ((from.Equals("Arad") && to.Equals("Timisoara")) ||
                     (from.Equals("Timisoara") && to.Equals("Arad")))
            {
                return(118);
            }
            else if ((from.Equals("Timisoara") && to.Equals("Lugoj")) ||
                     (from.Equals("Lugoj") && to.Equals("Timisoara")))
            {
                return(111);
            }
            else if ((from.Equals("Lugoj") && to.Equals("Mehadia")) ||
                     (from.Equals("Mehadia") && to.Equals("Lugoj")))
            {
                return(70);
            }
            else if ((from.Equals("Mehadia") && to.Equals("Dobreta")) ||
                     (from.Equals("Dobreta") && to.Equals("Mehadia")))
            {
                return(75);
            }
            else if ((from.Equals("Dobreta") && to.Equals("Craiova")) ||
                     (from.Equals("Craiova") && to.Equals("Dobreta")))
            {
                return(120);
            }
            else if ((from.Equals("Craiova") && to.Equals("Rimnicu Vilcea")) ||
                     (from.Equals("Rimnicu Vilcea") && to.Equals("Craiova")))
            {
                return(146);
            }
            else if ((from.Equals("Craiova") && to.Equals("Pitesti")) ||
                     (from.Equals("Pitesti") && to.Equals("Craiova")))
            {
                return(138);
            }
            else if ((from.Equals("Rimnicu Vilcea") && to.Equals("Sibiu")) ||
                     (from.Equals("Sibiu") && to.Equals("Rimnicu Vilcea")))
            {
                return(80);
            }
            else if ((from.Equals("Rimnicu Vilcea") && to.Equals("Pitesti")) ||
                     (from.Equals("Pitesti") && to.Equals("Rimnicu Vilcea")))
            {
                return(97);
            }
            else if ((from.Equals("Sibiu") && to.Equals("Fagaras")) ||
                     (from.Equals("Fagaras") && to.Equals("Sibiu")))
            {
                return(99);
            }
            else if ((from.Equals("Fagaras") && to.Equals("Bucharest")) ||
                     (from.Equals("Bucharest") && to.Equals("Fagaras")))
            {
                return(211);
            }
            else if ((from.Equals("Pitesti") && to.Equals("Bucharest")) ||
                     (from.Equals("Bucharest") && to.Equals("Pitesti")))
            {
                return(101);
            }
            else if ((from.Equals("Bucharest") && to.Equals("Giurgiu")) ||
                     (from.Equals("Giurgiu") && to.Equals("Bucharest")))
            {
                return(90);
            }
            else if ((from.Equals("Bucharest") && to.Equals("Urziceni")) ||
                     (from.Equals("Urziceni") && to.Equals("Bucharest")))
            {
                return(85);
            }
            else if ((from.Equals("Urziceni") && to.Equals("Vaslui")) ||
                     (from.Equals("Vaslui") && to.Equals("Urziceni")))
            {
                return(142);
            }
            else if ((from.Equals("Vaslui") && to.Equals("Iasi")) ||
                     (from.Equals("Iasi") && to.Equals("Vaslui")))
            {
                return(92);
            }
            else if ((from.Equals("Iasi") && to.Equals("Neamt")) ||
                     (from.Equals("Neamt") && to.Equals("Iasi")))
            {
                return(87);
            }
            else if ((from.Equals("Urziceni") && to.Equals("Hirsova")) ||
                     (from.Equals("Hirsova") && to.Equals("Urziceni")))
            {
                return(98);
            }
            else if ((from.Equals("Hirsova") && to.Equals("Eforie")) ||
                     (from.Equals("Eforie") && to.Equals("Hirsova")))
            {
                return(86);
            }

            return(0);
        }
Exemple #8
0
        public int getHeuristicValue(Object state)
        {
            Q4State Q4FromState = (Q4State)state;
            string  from        = Q4FromState.location;

            if (from.Equals("Oradea"))
            {
                return(380);
            }
            else if (from.Equals("Zerind"))
            {
                return(374);
            }
            else if (from.Equals("Arad"))
            {
                return(366);
            }
            else if (from.Equals("Timisoara"))
            {
                return(329);
            }
            else if (from.Equals("Lugoj"))
            {
                return(244);
            }
            else if (from.Equals("Mehadia"))
            {
                return(241);
            }
            else if (from.Equals("Dobreta"))
            {
                return(242);
            }
            else if (from.Equals("Craiova"))
            {
                return(160);
            }
            else if (from.Equals("Rimnicu Vilcea"))
            {
                return(193);
            }
            else if (from.Equals("Sibiu"))
            {
                return(253);
            }
            else if (from.Equals("Fagaras"))
            {
                return(176);
            }
            else if (from.Equals("Pitesti"))
            {
                return(100);
            }
            else if (from.Equals("Bucharest"))
            {
                return(0);
            }
            else if (from.Equals("Giurgiu"))
            {
                return(77);
            }
            else if (from.Equals("Urziceni"))
            {
                return(80);
            }
            else if (from.Equals("Vaslui"))
            {
                return(199);
            }
            else if (from.Equals("Iasi"))
            {
                return(226);
            }
            else if (from.Equals("Neamt"))
            {
                return(234);
            }
            else if (from.Equals("Hirsova"))
            {
                return(151);
            }
            else if (from.Equals("Eforie"))
            {
                return(161);
            }
            return(0);
        }
Exemple #9
0
		public ArrayList getSuccessors(Object state)
		{
			ArrayList list = new ArrayList(20);
			Q4State jstate = (Q4State) state;
        
			string location = jstate.location;
        
			Successor s;
			Q4State successor_state ;
        
        
			//these functions are used to track the expanded nodes step/heuristic costs
			//Q4StepCostFunction sn = new Q4StepCostFunction();
			//Q4HeuristicFunction hn = new Q4HeuristicFunction();
        
			//System.out.println("Expanding " + location);
         
			if (location.Equals("Oradea"))
			{
				successor_state = new Q4State("Zerind");
				s = new Successor("go to  Zerind", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Sibiu");
				s = new Successor("go to  Sibiu", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Zerind"))
			{
				successor_state = new Q4State("Oradea");
				s = new Successor("go to  Oradea", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Arad");
				s = new Successor("go to Arad", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Arad"))
			{
				successor_state = new Q4State("Zerind");
				s = new Successor("go to Zerind", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Timisoara");
				s = new Successor("go to Timisoara", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Timisoara"))
			{
				successor_state = new Q4State("Arad");
				s = new Successor("go to Arad", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Lugoj");
				s = new Successor("go to  Lugoj", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Lugoj"))
			{
				successor_state = new Q4State("Timisoara");
				s = new Successor("go to Timisoara", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Mehadia");
				s = new Successor("go to Mehadia", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Mehadia"))
			{
				successor_state = new Q4State("Dobreta");
				s = new Successor("go to Dobreta", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Lugoj");
				s = new Successor("go to Lugoj", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Dobreta"))
			{
				successor_state = new Q4State("Mehadia");
				s = new Successor("go to Mehadia", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Craiova");
				s = new Successor("go to Craiova", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Sibiu"))
			{
				successor_state = new Q4State("Arad");
				s = new Successor("go to Arad", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Oradea");
				s = new Successor("go to Oradea", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Fagaras");
				s = new Successor("go to Fagaras", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Rimnicu Vilcea");
				s = new Successor("go to Rimnicu Vilcea", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Rimnicu Vilcea"))
			{
				successor_state = new Q4State("Sibiu");
				s = new Successor("go to Sibiu", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Pitesti");
				s = new Successor("go to Pitesti", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Craiova");
				s = new Successor("go to Craiova", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Craiova"))
			{
				successor_state = new Q4State("Rimnicu Vilcea");
				s = new Successor("go to Rimnicu Vilcea", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Dobreta");
				s = new Successor("go to Dobreta", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Pitesti");
				s = new Successor("go to Pitesti", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Pitesti"))
			{
				successor_state = new Q4State("Rimnicu Vilcea");
				s = new Successor("go to Rimnicu Vilcea", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Craiova");
				s = new Successor("go to Craiova", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Bucharest");
				s = new Successor("go to Bucharest", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Fagaras"))
			{
				successor_state = new Q4State("Sibiu");
				s = new Successor("go to Sibiu", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Bucharest");
				s = new Successor("go to Bucharest", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Bucharest"))
			{
				successor_state = new Q4State("Fagaras");
				s = new Successor("go to Fagaras", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Pitesti");
				s = new Successor("go to Pitesti", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Giurgiu");
				s = new Successor("go to Giurgiu", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Urziceni");
				s = new Successor("go to Urziceni", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Giurgiu"))
			{
				successor_state = new Q4State("Bucharest");
				s = new Successor("go to Bucharest", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Urziceni"))
			{
				successor_state = new Q4State("Bucharest");
				s = new Successor("go to Bucharest", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Vaslui");
				s = new Successor("go to Vaslui", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Hirsova");
				s = new Successor("go to Hirsova", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Vaslui"))
			{
				successor_state = new Q4State("Urziceni");
				s = new Successor("go to Urziceni", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Iasi");
				s = new Successor("go to Iasi", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Iasi"))
			{
				successor_state = new Q4State("Vaslui");
				s = new Successor("go to Vaslui", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Neamt");
				s = new Successor("go to Neamt", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Neamt"))
			{
				successor_state = new Q4State("Iasi");
				s = new Successor("go to Iasi", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Hirsova"))
			{
				successor_state = new Q4State("Urziceni");
				s = new Successor("go to Urziceni", successor_state);
				list.Add(s);
            
				successor_state = new Q4State("Eforie");
				s = new Successor("go to Eforie", successor_state);
				list.Add(s);
			}
			else if (location.Equals("Eforie"))
			{
				successor_state = new Q4State("Hirsova");
				s = new Successor("go to Hirsova", successor_state);
				list.Add(s);
			}
       
               
            
        
			return list;
		}
Exemple #10
0
		private void btnDoDemo_Click(object sender, System.EventArgs e)
		{
			this.textBox1.Text = (System.Environment.NewLine + "Romania Lugoj To Bucharest Puzzle -->");
        
			Q4State initialState = new Q4State("Lugoj");
			try 
			{
				Problem problem = new Problem(initialState, 
					new Q4SuccessorFunction(),
					new Q4GoalTest(),
					new Q4StepCostFunction(),
					new Q4HeuristicFunction());
            
				//Search search = new BreadthFirstSearch(new TreeSearch());
				//Search search = new DepthFirstSearch(new TreeSearch());
				//Search search = new DepthFirstSearch(new GraphSearch());
				//Search search = new DepthLimitedSearch(9);
				//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);
				}
				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 = (String) iter.Current;
					String value = searchMetrics.get(key);
					this.textBox1.Text+=(System.Environment.NewLine + key +": " + value);
				}
            
			} 
			catch (Exception ex) 
			{
				this.textBox1.Text+=ex.Message;
			}
		}