How to Simulate Artificial Intelligence in Java?

Artificial, Intelligence, Simulation, Java

AI: Universe Knowledge Base

Objective

  • Create and use trees
  • Simulate Artificial Intelligence

Instructions

Develop a java program that learns about a universe of your choice by asking the user yes/no questions.

Universe Knowledge Base

Develop a program that begins with minimal knowledge.  When the program incorrectly guesses an answer, it asks for the answer and asks for a way to identify the item.  The program builds a binary tree of questions and items.  A YES response to a question is stored in the question’s left child while a NO answer is stored in the question’s right child.

 

Binary Tree Knowledge

package artificialintelligenceanimal.colorado.nodes;

public class BinaryTreeNode<E>
{

   private E data;
   private BinaryTreeNode<E> left, right;   

   
   public BinaryTreeNode(E initialData, BinaryTreeNode<E> initialLeft, BinaryTreeNode<E> initialRight)
   {
      data = initialData;
      left = initialLeft;
      right = initialRight;
   }       
   
   
 
   public E getData( )   
   {
      return data;
   }
   
   
 
   public BinaryTreeNode<E> getLeft( )
   {
      return left;                                               
   }
   
   
 
   public E getLeftmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getLeftmostData( );
   }
      

   public BinaryTreeNode<E> getRight( )
   {
      return right;                                               
   }

 
   public E getRightmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getRightmostData( );
   }
   
   

   public void inorderPrint( )
   {
      if (left != null)
         left.inorderPrint( );
      System.out.println(data);
      if (right != null)
         right.inorderPrint( );
   }  

   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                               
   }

 
   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   }
   
      

   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }   

 
   public void print(int depth)
   {
      int i;
   
      // Print the indentation and the data from the current node:
      for (i = 1; i <= depth; i++)
         System.out.print(”    “);
      System.out.println(data);

      // Print the left subtree (or a dash if there is a right child and no left child)   
      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print(”    “);
         System.out.println(“–“);
      }

      // Print the right subtree (or a dash if there is a left child and no left child)  
      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print(”    “);
         System.out.println(“–“);
      }
   }
   

   public BinaryTreeNode<E> removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }

   public BinaryTreeNode<E> removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
   }
       

   public void setData(E newData)   
   {
      data = newData;
   }                                                               
   
   

   public void setLeft(BinaryTreeNode<E> newLeft)
   {                    
      left = newLeft;
   }
    

   public void setRight(BinaryTreeNode<E> newRight)
   {                    
      right = newRight;
   }  
    
    

public static <E> BinaryTreeNode<E> treeCopy(BinaryTreeNode<E> source)
   {
        BinaryTreeNode<E> leftCopy;
        BinaryTreeNode<E> rightCopy;

      if (source == null)
         return null;
      else
      {
         leftCopy = treeCopy(source.left);
         rightCopy = treeCopy(source.right);
         return new BinaryTreeNode<E>(source.data, leftCopy, rightCopy);
      }
   }
   

   public static <E> long treeSize(BinaryTreeNode<E> root)
   {
      if (root == null)
         return 0;
      else
         return 1 + treeSize(root.left) + treeSize(root.right);
   }   

}
           

Learning Start

package artificialintelligenceanimal;

import static artificialintelligenceanimal.MAIN.learn;
import static artificialintelligenceanimal.MAIN.SysStatement;

public   class LearningStart {
       
      public static void play(artificialintelligenceanimal.colorado.nodes.BinaryTreeNode<String> current)   
   {   
      while (!current.isLeaf( ))   
      {   
         if (SysStatement(current.getData( )))   
            current = current.getLeft( );   
         else   
            current = current.getRight( );   
      }   
   
      System.out.print(“My guess is ” + current.getData( ) + “. “);   
      if (!SysStatement(“Am I right [continue]?”))   
         learn(current);   
      else   
      {   
         
         System.out.println(“I win!?”);   
         
      }   
   }   
      
   
}

Main.java

package artificialintelligenceanimal;
import artificialintelligenceanimal.colorado.nodes.BinaryTreeNode;
import java.util.Scanner;
 
public class MAIN
{
  private static Scanner stdin = new Scanner(System.in);   
         final static String ROOT_QUESTION = “Does it have legs?”;   
      final static  String LEFT_QUESTION = “Is it a cat?”;   
      final  static String RIGHT_QUESTION = “Is it a snake?”;   
      final  static String ANIMAL1 = “Cat”;   
      final  static String ANIMAL2 = “snake”;   
      final  static String ANIMAL3 = “snake”;   
      final static  String ANIMAL4 = “EARTHWORM”;   
      
   public static void main(String[ ] args)   
   {   
      BinaryTreeNode<String> root;   
   
      training_Instruction( );   
      root = beginning( );   
      do   
         LearningStart.play(root);   
      while (SysStatement(“Continue?”));   
   
   }   
    public static boolean SysStatement(String prompt)   
   {   
      String answer;   
       
      System.out.print(prompt + ” [Y or N]: “);   
      answer = stdin.nextLine( ).toUpperCase( );   
      while (!answer.startsWith(“Y”) && !answer.startsWith(“N”))   
      {   
     System.out.print(“Invalid response. Please type Y or N: “);   
     answer = stdin.nextLine( ).toUpperCase( );   
      }   
   
      return answer.startsWith(“Y”);   
   }   
   
      
      
   public static void training_Instruction( )   
   {   
      System.out.println(“Think of an animal and I will guess it.”);    
       
   }   

 
   public static BinaryTreeNode<String> beginning( )      
   {   
      BinaryTreeNode<String> root;   
      BinaryTreeNode<String> child;   
   
     
       
      root = new BinaryTreeNode<String>(ROOT_QUESTION, null, null);   
   
         
      child = new BinaryTreeNode<String>(LEFT_QUESTION, null, null);   
      child.setLeft(new BinaryTreeNode<String>(ANIMAL1, null, null));   
      child.setRight(new BinaryTreeNode<String>(ANIMAL2, null, null));   
      root.setLeft(child);   
   
         
      child = new BinaryTreeNode<String>(RIGHT_QUESTION, null, null);   
      child.setLeft(new BinaryTreeNode<String>(ANIMAL3, null, null));   
      child.setRight(new BinaryTreeNode<String>(ANIMAL4, null, null));   
      root.setRight(child);   
   
      return root;   
   }   
    
    
      
   public static void learn(BinaryTreeNode<String> current)   
     
   {   
      String guessAnimal;      
      String correctAnimal;    
      String newQuestion;            
         
      guessAnimal = current.getData( );   
      System.out.println(“I give up. What is the name of animal? “);   
      correctAnimal = stdin.nextLine( );   
      System.out.println(“Please type a yes/no question that will distinguish a”);   
      System.out.println(correctAnimal + ” from a ” + guessAnimal + “.”);   
      newQuestion = stdin.nextLine( );   
         
      current.setData(newQuestion);   
      System.out.println(“As a ” + correctAnimal + “, ” + newQuestion);   
      if (SysStatement(“Please answer”))   
      {   
         current.setLeft(new BinaryTreeNode<String>(correctAnimal, null, null));   
         current.setRight(new BinaryTreeNode<String>(guessAnimal, null, null));   
      }   
      else   
      {   
         current.setLeft(new BinaryTreeNode<String>(guessAnimal, null, null));   
         current.setRight(new BinaryTreeNode<String>(correctAnimal, null, null));   
      }   
      
     System.out.println(“\n”);   
           
   }   
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here