Example #1
0
 public JocksNerdsState(JocksNerdsState state)
 {
     for (int i = 0; i < 3; i++)
     {
         this.locationArray[i] = state.locationArray[i];
     }
 }
 //if the # of Jocks on a side is > the number of Nerds and there's at least one nerd
 //on that side then the state is invalid
 private bool isValidState(JocksNerdsState state)
 {
     if ((state.locationArray[1] > state.locationArray[0] && state.locationArray[0] > 0) || ((3 - state.locationArray[1]) > (3 - state.locationArray[0]) && (3 - state.locationArray[0]) > 0))
     {
         return(false);
     }
     return(true);
 }
Example #3
0
        public bool isGoalState(Object state)
        {
            JocksNerdsState jstate = (JocksNerdsState)state;

            //if there's no one left on the left bank and the boat is on the right side then it's over
            if (jstate.locationArray[0] == 0 && jstate.locationArray[1] == 0 && jstate.locationArray[2] % 2 == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #4
0
        //when creating states, it's absolutely CRITICAL to override
        //Equals and GetHashCode or the application won't be able
        //to evaluate GraphSearch properly


        //if the number of nerds or jocks differs, or the boat is on the opposite side, return false
        public override bool Equals(object o)
        {
            JocksNerdsState state = (JocksNerdsState)o;

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

            return(true);
        }
        public ArrayList getSuccessors(Object state)
        {
            ArrayList       list            = new ArrayList(20);
            JocksNerdsState jocksNerdsState = (JocksNerdsState)state;

            int numberNerdsLeft  = jocksNerdsState.locationArray[0];
            int numberJocksLeft  = jocksNerdsState.locationArray[1];
            int numberNerdsRight = 3 - jocksNerdsState.locationArray[0];
            int numberJocksRight = 3 - jocksNerdsState.locationArray[1];
            int turnCount        = jocksNerdsState.locationArray[2];

            /*
             *
             * if the turn count for the current state is even the boat is on on the left bank,
             * otherwise it's on the right side
             * try moving every combination of nerds and jocks to the opposite bank.
             * Also need to make sure that the # of jocks on one side never outnumbers the
             * number of nerds on that side.
             *
             */

            if (jocksNerdsState.locationArray[2] % 2 == 0)
            {
                //the boat's on the left so move people to the right
                JocksNerdsState successor_state;



                //move 1 nerd to the right if the Nerds won't be eaten as a result
                if (numberNerdsLeft > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] -= 1;
                    successor_state.locationArray[1]  = numberJocksLeft;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 1 nerd right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }


                //move 2 Nerds right if the Nerds won't be eaten as a result
                if (numberNerdsLeft > 1)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] -= 2;
                    successor_state.locationArray[1]  = numberJocksLeft;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 2 Nerds right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }


                //move 1 Jock right if the Nerds won't be eaten as a result
                if (numberJocksLeft > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0]  = numberNerdsLeft;
                    successor_state.locationArray[1] -= 1;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 1 Jock right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }

                //move 2 Jocks right if the Nerds won't be eaten as a result
                if (numberJocksLeft > 1)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0]  = numberNerdsLeft;
                    successor_state.locationArray[1] -= 2;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 2 Jocks right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }

                //move a Jock and a nerd right
                if (numberJocksLeft > 0 && numberNerdsLeft > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] -= 1;
                    successor_state.locationArray[1] -= 1;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move a nerd and Jock right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }
            }
            else
            {
                //the boat is on the right so try moving people to the left

                //the boat is on the left so move people to the right
                JocksNerdsState successor_state;



                //move 1 nerd to the left if the Nerds won't be eaten as a result
                if (numberNerdsRight > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] += 1;
                    successor_state.locationArray[1]  = numberJocksLeft;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 1 nerd left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }


                //move 2 Nerds left if the Nerds won't be eaten as a result
                if (numberNerdsRight > 1)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] += 2;
                    successor_state.locationArray[1]  = numberJocksLeft;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 2 Nerds left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }


                //move 1 Jock left if the Nerds won't be eaten as a result
                if (numberJocksRight > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0]  = numberNerdsLeft;
                    successor_state.locationArray[1] += 1;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 1 Jock left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }

                //move 2 Jocks left if the Nerds won't be eaten as a result
                if (numberJocksRight > 1)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0]  = numberNerdsLeft;
                    successor_state.locationArray[1] += 2;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move 2 Jocks left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }

                //move a Jock and a nerd left
                if (numberJocksRight > 0 && numberNerdsRight > 0)
                {
                    successor_state = new JocksNerdsState(jocksNerdsState);
                    successor_state.locationArray[0] += 1;
                    successor_state.locationArray[1] += 1;
                    successor_state.locationArray[2]  = turnCount + 1;
                    if (isValidState(successor_state))
                    {
                        Successor s = new Successor("move a nerd and Jock left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", " + successor_state.locationArray[2] + ")\n", successor_state);
                        list.Add(s);
                    }
                }
            }



            return(list);
        }
Example #6
0
		private void btnJocks_Click(object sender, System.EventArgs e)
		{
			this.textBox1.Text = "";
			JocksNerdsState initialState = new JocksNerdsState();
			try 
			{
				Problem problem = new Problem(initialState, 
					new JocksNerdsSuccessorFunction(),
					new JocksNerdsGoalTest());
            
				//Search search = new BreadthFirstSearch(new TreeSearch());
				//Search search = new BreadthFirstSearch(new GraphSearch());

				//this one never ends because it tries to traverse a tree of effectively infinite depth
				//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 GraphSearch());
				//Search search = new GreedyBestFirstSearch(new GraphSearch());
				//Search search = new HillClimbingSearch();
				Search search = new SimulatedAnnealingSearch();

            
				ArrayList solution = search.search(problem);
            
				if (solution.Count == 0) 
				{ //empty list means failure
					//System.out.println("\nNo Solution\n");
					this.textBox1.Text = System.Environment.NewLine + "No Solution";
				}
				for (int i = 0; i < solution.Count; i++)
				{
					this.textBox1.Text += solution[i].ToString() + System.Environment.NewLine;
				}
            
				//Printing metrics
				Metrics searchMetrics = search.getMetrics();
				//ArrayList iter = (ArrayList)searchMetrics.keySet();
				ICollection col = searchMetrics.keySet();
				IEnumerator iter = col.GetEnumerator();
				//iter.GetEnumerator();
				//Array r = new Array();
				//iter.MoveNext();
				
				//iter.
            
            
				while(iter.MoveNext())
				{
					string key =  iter.Current.ToString();
					string val = searchMetrics.get(key);
					this.textBox1.Text += System.Environment.NewLine + key + ": " + val;
					//iter.MoveNext();
				}
            
			} 
			catch (Exception ex) 
			{
				//e.printStackTrace();
				this.textBox1.Text += ex.Message;
			}
		}
Example #7
0
        private void btnJocks_Click(object sender, System.EventArgs e)
        {
            this.textBox1.Text = "";
            JocksNerdsState initialState = new JocksNerdsState();

            try
            {
                Problem problem = new Problem(initialState,
                                              new JocksNerdsSuccessorFunction(),
                                              new JocksNerdsGoalTest());

                //Search search = new BreadthFirstSearch(new TreeSearch());
                //Search search = new BreadthFirstSearch(new GraphSearch());

                //this one never ends because it tries to traverse a tree of effectively infinite depth
                //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 GraphSearch());
                //Search search = new GreedyBestFirstSearch(new GraphSearch());
                //Search search = new HillClimbingSearch();
                Search search = new SimulatedAnnealingSearch();


                ArrayList solution = search.search(problem);

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

                //Printing metrics
                Metrics searchMetrics = search.getMetrics();
                //ArrayList iter = (ArrayList)searchMetrics.keySet();
                ICollection col  = searchMetrics.keySet();
                IEnumerator iter = col.GetEnumerator();
                //iter.GetEnumerator();
                //Array r = new Array();
                //iter.MoveNext();

                //iter.


                while (iter.MoveNext())
                {
                    string key = iter.Current.ToString();
                    string val = searchMetrics.get(key);
                    this.textBox1.Text += System.Environment.NewLine + key + ": " + val;
                    //iter.MoveNext();
                }
            }
            catch (Exception ex)
            {
                //e.printStackTrace();
                this.textBox1.Text += ex.Message;
            }
        }
		//if the # of Jocks on a side is > the number of Nerds and there's at least one nerd 
		//on that side then the state is invalid
		private bool isValidState(JocksNerdsState state)
		{
        
			if ((state.locationArray[1] > state.locationArray[0] && state.locationArray[0] > 0) || ((3 - state.locationArray[1]) > (3 - state.locationArray[0]) && (3 - state.locationArray[0]) > 0))
			{
				return false;
			}
			return true;
		}
		public ArrayList getSuccessors(Object state)
		{
			ArrayList list = new ArrayList(20);
			JocksNerdsState jocksNerdsState = (JocksNerdsState) state;
        
			int numberNerdsLeft = jocksNerdsState.locationArray[0];
			int numberJocksLeft = jocksNerdsState.locationArray[1];
			int numberNerdsRight = 3 - jocksNerdsState.locationArray[0];
			int numberJocksRight = 3 - jocksNerdsState.locationArray[1];
			int turnCount = jocksNerdsState.locationArray[2];
        
			/*
        
			 if the turn count for the current state is even the boat is on on the left bank,
			 otherwise it's on the right side
			 try moving every combination of nerds and jocks to the opposite bank.
			 Also need to make sure that the # of jocks on one side never outnumbers the
			 number of nerds on that side.
         
			 */
        
			if(jocksNerdsState.locationArray[2] % 2 == 0)
			{
				//the boat's on the left so move people to the right
				JocksNerdsState successor_state;
             
             
             
				//move 1 nerd to the right if the Nerds won't be eaten as a result
				if (numberNerdsLeft > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] -= 1;
					successor_state.locationArray[1] = numberJocksLeft;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 1 nerd right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
            
				//move 2 Nerds right if the Nerds won't be eaten as a result
				if (numberNerdsLeft > 1)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] -= 2;
					successor_state.locationArray[1] = numberJocksLeft;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 2 Nerds right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
            
				//move 1 Jock right if the Nerds won't be eaten as a result
				if (numberJocksLeft > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] = numberNerdsLeft;
					successor_state.locationArray[1] -= 1;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 1 Jock right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
				//move 2 Jocks right if the Nerds won't be eaten as a result
				if (numberJocksLeft > 1)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] = numberNerdsLeft;
					successor_state.locationArray[1] -= 2;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 2 Jocks right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
				//move a Jock and a nerd right
				if (numberJocksLeft > 0 && numberNerdsLeft > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] -= 1;
					successor_state.locationArray[1] -= 1;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move a nerd and Jock right (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
			}   
			else
			{   
				//the boat is on the right so try moving people to the left
            
				//the boat is on the left so move people to the right
				JocksNerdsState successor_state;
             
             
             
				//move 1 nerd to the left if the Nerds won't be eaten as a result
				if (numberNerdsRight > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] += 1;
					successor_state.locationArray[1] = numberJocksLeft;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 1 nerd left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
            
				//move 2 Nerds left if the Nerds won't be eaten as a result
				if (numberNerdsRight> 1)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] += 2;
					successor_state.locationArray[1] = numberJocksLeft;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 2 Nerds left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
            
				//move 1 Jock left if the Nerds won't be eaten as a result
				if (numberJocksRight > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] = numberNerdsLeft;
					successor_state.locationArray[1] += 1;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 1 Jock left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
				//move 2 Jocks left if the Nerds won't be eaten as a result
				if (numberJocksRight > 1)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] = numberNerdsLeft;
					successor_state.locationArray[1] += 2;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move 2 Jocks left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
				//move a Jock and a nerd left
				if (numberJocksRight > 0 && numberNerdsRight > 0)
				{
					successor_state = new JocksNerdsState(jocksNerdsState);
					successor_state.locationArray[0] += 1;
					successor_state.locationArray[1] += 1;
					successor_state.locationArray[2] = turnCount + 1;
					if(isValidState(successor_state))
					{
						Successor s = new Successor("move a nerd and Jock left (array = " + successor_state.locationArray[0] + ", " + successor_state.locationArray[1] + ", "  + successor_state.locationArray[2] + ")\n", successor_state);
						list.Add(s);
					}
                 
				}
            
        
			}
        
       
        
			return list;
		}
Example #10
0
		public JocksNerdsState(JocksNerdsState state) 
		{
			for(int i=0; i<3; i++) 
				this.locationArray[i] = state.locationArray[i];
		}