Monday, May 30, 2011

AI- Simple Implementation of Uninformed Search Strategies- PartII

My code search for a number in the range from 0, 1, 2, .N. so, the initial state is 0 and the goal state is the number specified by the user. Each step of number generation costs random number or 1. Class GetSucc.cs define GetSussessor() function which inculdes the possible set of actions that are required for positive number generation inorder. The input of GetSussessor () is the current state of the problem (i.e. initial State 0) and the output is the ArrayList of next states that are reached from current state(i.e. from 0 the next states 1,2  assuming that our tree is the binary tree).
class GetSucc
    {
       //if search go forward from 0 to number n 
        public ArrayList GetSussessor(int State)
        {
            ArrayList Result = new ArrayList();
            Result.Add(2 * State + 1);
            Result.Add(2 * State + 2);
            return Result;
        }
       //if search go backward from n to number 0
        public ArrayList GetSussessor_Reverse(int State)
        {
            ArrayList Result = new ArrayList();
            if (State % 2 == 0)
            {
                int P = State / 2 - 1;
                Result.Add(P);
                Result.Add(State - 1);
            }
            else 
            {
                int Sib = State + 1;
                Result.Add(Sib / 2 - 1);
                Result.Add(Sib);
            
            }
           
            return Result;
            
        }
        //if the cost of each node must be determined
 public ArrayList GetSussessor(int State,Node Parent)
        {
            ArrayList Result = new ArrayList();
            Random n = new Random();
            Test s = new Test();
 Result.Add(new Node(2* State + 1,Parent,n.Next(1,100)+Parent.Cost));
 Result.Add(new Node(2* State + 2, Parent,n.Next(1,100) + Parent.Cost));
            Result.Sort(s);
            return Result;
        }
}//end class
//implement the interface IComparer to compare objects in ArrayList 
    public class Test : IComparer
    {
        public int Compare(object x, object y)
        {
            int val1 = ((Node)x).Cost;
            int val2 = ((Node)y).Cost;
            if (val1 <= val2)
                return 1;
            else
                return 0;
        }
    }//end class Test