Showing posts with label MSc IT AI Practicals. Show all posts
Showing posts with label MSc IT AI Practicals. Show all posts

Thursday, 10 October 2013


AIM: Write a program in Lisp to implement decision tree.

Post by: Sohrab Vakharia

(Enter a decimal string of repeated entries of suitable length. Apply binary decision tree to find repetitions of each decimal digit in the string.)

Source Code:
(defun MyTree(mylist)
    (setq CntrArr 0)
    (setq ResArr (make-array '(1 10)))
    (loop
        (when (equal CntrArr 10) (return))
        (setf (aref ResArr 0 CntrArr) 0)
        (incf CntrArr)
    )
    (loop
        (when (equal (first mylist) nil) (return))
        (if (<= (first mylist) (/ 10 2))
            (if (< (first mylist) ( / 5 2))
                (if (< (first mylist) ( / 3 2))
                    (if (< (first mylist) ( / 1 2))
                        (setf (aref ResArr 0 0) (incf (aref ResArr 0 0)))
                        (setf (aref ResArr 0 1) (incf (aref ResArr 0 1)))
                    )
                    (setf (aref ResArr 0 2) (incf (aref ResArr 0 2)))
                )
                (if (< (first mylist) ( / 9 2))
                    (if (< (first mylist) ( / 7 2))
                         (setf (aref ResArr 0 3) (incf (aref ResArr 0 3)))
                        (setf (aref ResArr 0 4) (incf (aref ResArr 0 4)))
                    )
                    (setf (aref ResArr 0 5) (incf (aref ResArr 0 5)))
                )
            )
            (if (< (first mylist) ( / 15 2))
                (if (< (first mylist) ( / 13 2))
                    (if (< (first mylist) ( / 12 2))
                        (setf (aref ResArr 0 5) (incf (aref ResArr 0 5)))
                        (setf (aref ResArr 0 6) (incf (aref ResArr 0 6)))
                    )
                    (setf (aref ResArr 0 7) (incf (aref ResArr 0 7)))
                )
                (if (< (first mylist) ( / 19 2))
                    (if (< (first mylist) ( / 17 2))
                         (setf (aref ResArr 0 8) (incf (aref ResArr 0 8)))
                        (setf (aref ResArr 0 9) (incf (aref ResArr 0 9)))
                    )
                    (setf (aref ResArr 0 10) (incf (aref ResArr 0 10)))
                )
            )
        )
    (setq mylist (rest mylist))
)   
   (print ResArr)
   
)

Output:


Prof.Mahendra Kanojia

AIM: Write a program in Lisp to demonstrate the crossover genetic operator. 

Post by: Sohrab Vakharia

(Enter population of binary strings. Perform crossover based on a random crossing site value. Consider fitness value as the number of repetitions of specified bit in the string. Perform the evolution for a specified number generation).

Source Code:
(defun crossover(List1 List2)
    (setq cnt 0)
    (setq A '())
    (setq B '())
    (setq cpt (random(length List1)))
   
    (loop
        (when (and (equal (first List1) nil) (equal (first List2) nil))(return))
            (if (<= cpt cnt)
                (block blk1
                    (setq A(cons(first List2) A))
                    (setq B(cons(first List1) B))           
                )
                (block blk2
                    (setq A(cons(first List1) A))
                    (setq B(cons(first List2) B))           
                )
            )
        (incf cnt)
        (setq List1 (rest List1))
        (setq List2 (rest List2))
    )
    (setq List1 (append A  B))
    (setq List2 (append B  A))
)

(defun actualcrossover(LAlpha LBeta  LGamma )
    (setq Lgencntr 0)(setq i 1)
    (setq TempLst '())
    (loop
        (when (equal Lgencntr LGamma)(return))
            (setq Postcntr 0)(setq TempLst '())
            (setq TempLst (crossover LAlpha LBeta  ))
            (setq LenTempLst (length TempLst))
            (setq LAlpha '())
            (setq LBeta '())
            (loop
                (when (equal (first TempLst) nil)(return))
                (if( < Postcntr (/ LenTempLst 2))
                        (setq LAlpha (cons (first TempLst ) LAlpha))
                        (setq LBeta (cons (first TempLst ) LBeta))
                )
                (incf Postcntr)
                (setq TempLst (rest TempLst ))
            )
            (incf Lgencntr)(incf i)
            (format t "~% ~A Point of Crossover in list ~A" (- i 1) cpt)   
            (format t "~% ")   
            (format t "~% After Crossover the output shows :" )
            (print LAlpha)
            (print LBeta)
            (print '*****************************)
    ))

Output:

Prof.Mahendra Kanojia

AIM: Write a program in Lisp to perform roulette wheel selection.

Post by: Sohrab Vakharia

(Enter a population of binary strings. Consider fitness value as the number of repetitions of a specified bit (say 1) in the string.)

Source Code:
(defun wheel(list1 list2 list3 list4)
    (setq llist1 (funlist list1))
    (setq llist2 (funlist list2))
    (setq llist3 (funlist list3))
    (setq llist4 (funlist list4))
    (print "The highest fitness of string is: ")
    (setq m1(max llist1 llist2 llist3 llist4))
    (print m1)
    (if (= m1 llist1)(print "First String is Strongest")())
    (if (= m1 llist2)(print "Second String is Strongest")())
    (if (= m1 llist3)(print "Third String is Strongest")())
    (if (= m1 llist4)(print "Fourth String is Strongest")())
)
(defun funlist(list1)
    (setq list1 (reverse list1))
    (setq len (length list1))
    (setq sum1 0)
    (setq count 1)
    (setq sum 0)
    (setq co 1)
    (setq cn 2)
    (setq fr (car list1))
    (setq list1 (cdr list1))
    (setq cn1 1)
    (setq sum (* fir 1))
    (setq sum1 (+ sum1 sum))
        (loop
            (when (>= count len) (return))
            (setq fir (car list1))
            (setq list1 (cdr list1))
            (setq cn1 (* cn cn1))
            (setq sum (* fir cn1))
            (setq sum1 (+ sum1 sum))
            (incf count)
        )
    (print "sum1")
    (print sum1)
)

Output:

Prof.Mahendra Kanojia

AIM: Write a program in Lisp to demonstrate working of an artificial neuron.

Post by: Sohrab Vakharia

(Enter an input vector X and weight vector W. Calculate weighted sum XW. Transform this using signal or activation functions like logistic, threshold, hyperbolic-tangent, linear, exponential, sigmoid etc. and display the output.).

Source Code:
(defun nnFunction (actSignlist wghtList c n)
(setq wghtSum 0)
(setq resFunc 0)
(loop
    (when (equal (first actSignlist) nil) (return))
    (setq wghtSum ( + wghtSum ( * (first actSignlist) (first 
wghtList))))
    (print wghtSum)
    (setq actSignlist (rest actSignlist))
    (print actSignlist)
    (setq wghtList (rest wghtList))
    (print wghtList)
)

(print "Hyperbolic Tangent Signal Function")
(print ( tan ( * 10 wghtSum)))

(print "Threshold Linear Signal Function")
(if  (<= 1 (* c wghtSum)) (print 1) (print 0))

(print "Linear Signal Function")
(print (* c wghtSum))

 (print "Logistical Signal Function")
(print ( / 1 ( + 1 (exp( - 0 ( * c  wghtSum))))))

(print "Threshold Exponential Signal Function")
(print ( min 1 (exp (* c wghtSum))))

)

Output:


Prof.Mahendra Kanojia

AIM: Write a program in Lisp to demonstrate that a fuzzy system is a structured numerical estimator-using example of controlling an inverted pendulum.

Post by: Sohrab Vakharia

(Enter two strings for angle of pendulum θ and angular velocity Δθ. Convert them into strings of fuzzy using five fuzzy set values NM (Negative medium), NS (Negative Small), ZE(Zero), PS (Positive Small) and PM ( Positive Medium). Apply FAM rules to find the output values v, the current to the motor control of the pendulum to each input set of (θ, Δθ). Create a string of fuzzy output values of the current.)

Source Code:
(defun FAM (angpen anguvel)
    (cond
        ( ( equal angpen "NM")
            (block blkone
                ( cond
                    ( ( equal anguvel "NM") nil)
                    ( ( equal anguvel "NS") nil)
                    ( ( equal anguvel "ZE") (print "PM"))
                    ( ( equal anguvel "PS") nil)
                    ( ( equal anguvel "PM") nil)
                )
            )
        )
        ( ( equal angpen "NS")
            (block blkone
                ( cond
                    ( ( equal anguvel "NM") nil)
                    ( ( equal anguvel "NS") nil)
                    ( ( equal anguvel "ZE") (print "PS"))
                    ( ( equal anguvel "PS") nil)
                    ( ( equal anguvel "PM") nil)
                )
            )
        )
        ( ( equal angpen "ZE")
            (block blkone
                ( cond
                    ( ( equal anguvel "NM") nil)
                    ( ( equal anguvel "NS") nil)
                    ( ( equal anguvel "ZE") (print "ZE"))
                    ( ( equal anguvel "PS") nil)
                    ( ( equal anguvel "PM") nil)
                )
            )
        )
        ( ( equal angpen "PS")
            (block blkone
                ( cond
                    ( ( equal anguvel "NM") nil)
                    ( ( equal anguvel "NS") nil)
                    ( ( equal anguvel "ZE") (print "NS"))
                    ( ( equal anguvel "PS") nil)
                    ( ( equal anguvel "PM") nil)
                )
            )
        )
        ( ( equal angpen "PM")
            (block blkone
                ( cond
                    ( ( equal anguvel "NM") nil)
                    ( ( equal anguvel "NS") nil)
                    ( ( equal anguvel "ZE") (print "NM"))
                    ( ( equal anguvel "PS") nil)
                    ( ( equal anguvel "PM") nil)
                )
            )
        )
    )
)

Output:




Prof.Mahendra Kanojia

AIM: Write a program in Lisp to enter a number n and create a list of length n of repeated elements. 
Count the number of repetitions of each element in the list. Display the count value of each element in character.

Post by: Sohrab Vakharia

( Eg (1 2 1 1 3) should give the output “element 1: three times”).

Output:




Prof.Mahendra Kanojia

AIM: Write Lisp program to enter a list of integers between two given integers and generate new lists by performing different list manipulation functions

Post by: Sohrab Vakharia

like first, first, rest, last, append, reduce, reverse, combs, reverse etc.

Output:




Prof.Mahendra Kanojia

AIM : Write and execute the statements for the following in LISP:

Post by: Sohrab Vakharia

i. Define the function average, which will take two numbers as arguments and compute their average.
ii. Define the function that computes the factorial of a number.

1)    Average function




2)    Factorial



Prof.Mahendra Kanojia

AIM : Write and execute the statements for the following in LISP:

Post by: Sohrab Vakharia

Addition, subtraction, multiplication & division of two numbers.

Output:





Prof.Mahendra Kanojia

AIM: Write a program using C/C++/Java for implementing the Breadth First Search Algorithm

Post by: Sohrab Vakharia

Source Code:
import java.io.*; 
class BreadthFirst 
{ 
public static void main(String args[]) throws IOException 
{ 
    int i,n,j,k; 
    System.out.println("How many nodes do you want ?:") ; 
               BufferedReader br= new BufferedReader (new InputStreamReader(System.in)); 
    n =Integer.parseInt(br.readLine()); 
    int q[] = new int[10]; 
    int m[] = new int[10]; 
    int a[][] = new int[10][10]; 
    for (i=0; i<10; i++) 
    { 
        m[i] = 0; 
    } 
    System.out.println("\n\nEnter 1 if edge is present between nodes else 0 if not"); 
    for (i=0; i<n; i++) 
    { 
        System.out.println("\n"); 
        for (j=i; j<n; j++) 
        { System.out.println("Edge between node "+ (i+1) + " and node" + (j+1)+ ":" );                    
a[i][j]=Integer.parseInt(br.readLine()); 
            a[j][i]=a[i][j];     
        } 
        a[i][i]=0;  } 
    System.out.println("\nOrder of accessed nodes according to Breadth First: \n"); 
    q[0] = 0; m[0] = 1; 
        int u; 
        int node=1; 
     int  start1=1, start=0; 
     while(node>0) 
    { 
       u=q[start];start++; 
       System.out.println("  " +(u+1)); 
       node--; 
       for(j=0;j<n;j++) 
       { 
         if(a[u][j]==1) 
          { 
            if(m[j]==0) 
             {   
               m[j]=1; 
               q[start1]=j; 
               node++; 
        start1++; 
              } 
          } 
       } 
    }     
  } 
} 

Output:

Prof.Mahendra Kanojia

AIM: Write a program using C/C++/Java for implementing the Depth First Search Algorithm

Post by: Sohrab Vakharia

Source Code:
import java.io.*; 
class DepthFirst 
{ 
static void dfs1(int a[ ][ ], int m[ ], int i, int n) 
{ 
    int j; 
    System.out.println("\t" + (i+1)); 
    m[i] = 1; 
    for(j=0; j<n; j++) 
        if(a[i][j]==1 && m[j]==0) 
            dfs1(a,m,j,n); 
} 
public static void main(String args[]) throws IOException 
{ 
    int  n, i, j; 
    System.out.println("How many vertices do you want ? : "); 
    BufferedReader br= new BufferedReader (new InputStreamReader(System.in)); 
     n =Integer.parseInt(br.readLine()); 
    int m[]= new int[n]; 
    int a[][] = new int[n][n]; 
    for (i=0; i<n; i++) 
    { 
        m[i] = 0; 
    } 
    System.out.println("\n\nEnter 1 if edge is present between vertices else 0 if not"); 
    for (i=0; i<n; i++) 
    { 
        System.out.println("\n"); 
        for (j=i; j<n; j++) 
        { 
            System.out.println("Edge between " + (i+1) + " and " +  (j+1)+ " : "); 
            a[i][j] =Integer.parseInt(br.readLine()); 
            a[j][i]=a[i][j]; 
        } 
        a[i][i] = 0; 
    } 
    System.out.println("\nOrder of accessed nodes according to Depth First Search : \t"); 
    for (i=0; i<n; i++) 
        if (m[i]==0) 
            dfs1(a,m,i,n);      
} 
}

Output:

Prof.Mahendra Kanojia