Saturday, 3 August 2013

Development of AI

INTRODUCTION
Artificial intelligence is technology and a branch of computer science that studies and develops intelligent machines and software. Luger and Stubblefield define Artificial Intelligence as the branch of computer science that is concerned with the automation of intelligent behavior. The field of artificial intelligence attempts to understand intelligent entities. One reason to study it is to learn more about our own intelligence [1]. It is a broad topic, comprising of diverse fields of study, from neuroscience to expert systems. The common element that all the fields of AI have in between them is the creation of machines that can "think" like human. The theory and insights brought about by AI research are the likely trends in the future of computing [2].
    DEVELOPMENT
The field of AI research was founded at a conference on the campus of Dartmouth College in the summer of 1956. By the end of the two-month conference, artificial intelligence had found its niche. However, the main concept of Artificial Intelligence was initiated when Alan Turing proposed “Turing Test” in 1950. Artificial intelligence research has progressed very much since the Dartmouth conference, but the ultimate AI system has yet to be invented [3]. The main advances over the past sixty years have been advances in search algorithms, machine learning algorithms, and integrating statistical analysis into understanding the world at large [4].

Brief introduction to Cognitive Science

1.    INTRODUCTION
Cognitive science is the scientific study of the human mind [1]. It is the interdisciplinary study of mind and intelligence, embracing philosophy, psychology, artificial intelligence, neuroscience, linguistics and anthropology [2]. Cognitive Science deals with the different capabilities of human mind from fundamental functions such as learning to complex capabilities such as high level logic and decision making.
The fundamental concept of cognitive science is "that thinking can best be understood in terms of representational structures in the mind and computational procedures that operate on those structures" [3]. This field is often perceived as similar to the physical sciences, and uses the scientific methods including simulation and modeling for better understanding, comparing the output of such models with features of human behavior in many cases. It is a collaborative endeavor of psychology, computer science, neuroscience, linguistics, and others related fields [4].
2.    HISTORY/DEVELOPMENT
Cognitive science (CS) is a young discipline that emerged from a research program started in 1975 [4]. However, the roots of modern Cognitive Science trace back to 1840s when Charles Babbage thought of making an “Analytical Engine” which was the first attempt at implementing AI. The modern culture of cognitive science was realized during the early cyberneticists in the 1930s and 1940s, such as Warren McCulloch and Walter Pitts, who sought to understand the organizing principles of the mind.

Friday, 28 June 2013

8085 program for 8 bit binary division

Here, dividend is of 16 bits and divisor is of 8 bits. Dividend is stored in 8DC1 (LSB) and 8DC2 (MSB), divisor is stored in 8DC3, quotient is in 8DC4 and remainder is in 8DC5.

Mnemonics:

LHLD 8DC1H
LDA 8DC3H
MOV B, A
MVI C, 08

LOOP:
   DAD H
   MOV A, H
   SUB B
   JC AHEAD
   MOV H, A
   INR L

AHEAD:
   DCR C
   JNZ LOOP
   SHLD 8DC4H
   HLT

Observation Table:
Memory Address
Content
8DC1
04H
8DC2
01H
8DC3
03H
8DC4
56H
8DC5
02H


©Dixit Bhatta 2013

Tuesday, 25 June 2013

8085 program for multi-byte decimal addition

The counter is stored in 8E00H. The first set of numbers is stored from 8E01H onward, and the second set starts from 8F01H. The sum of numbers in corresponding positions is finally placed in the address of the first set of numbers.

Mnemonics:

LXI H, 8E00H
LXI D, 8F01H
MOV C, M
INX H

LOOP:
   LDAX D
   ADD M
   MOV M, A
   DCR C
   INX H
   INX D
   JNZ LOOP
   HLT

Observation table:
Memory Address
Content Before
Content After
8E00
03H
03H
8E01
12
23
8E02
10
25
8E03
09
20
8F01
11
11
8F02
15
15
8F03
11
11

©Dixit Bhatta 2013

Monday, 10 June 2013

C++ Program to Concatenate two strings

/*WAP that has a class with character array as its data member.
One object should contain "Computer Scientists are" and another
should contain " creators of logic". Member function join() should
concatanate two strings by passing two objects as arguments. Display
the concatanated string through a member function. Use constructor
to allocate and intialize the data member. Make one function for
concatanation of two strings.*/

#include <iostream.h>
#include <conio.h>

class concatanate{
char string[50];
   public:
    static count =1;
      concatanate(){ //constructor
      if (count == 1){
          strcpy(string, "Computer Scientists are");
            count++;
         }
         else{
          strcpy(string, " creators of logic.");
            count++;
         }
      }
      //function to concatanate strings
      void join(concatanate a, concatanate b){
      concatanate merged;
         strcat(a.string , b.string);//concatanate a and b strings
         strcpy(merged.string, a.string);
         display(merged);
      }
      //function to display concatanated string
      void display(concatanate a){
      cout<<"Concatanated String is:"<<endl<<endl;
         cout<<a.string;
      }
     
};

void main(){
clrscr();
   concatanate a,b;
   a.join(a,b);
   getch();
}

Thursday, 6 June 2013

Basic concept of Static SQL and Dynamic SQL

Static SQL:

Static SQL is the type of SQL in which we cannot change the form of SQL statements unless we change our program. The output of our program will be similar each time based upon the input value. If we use static SQL statements of UPDATE a table, then the table will be updated every time we enter new data. We cannot expect other operations from the program unless we change the whole SQL statements to make them do so.

However, the static SQL can be made flexible to some extent by using so called host variables.

This model can be useful in developing programs that are used to do same tasks time and again, like keeping record of items sold in a store (a billing system).

Dynamic SQL:

Basically, a Dynamic SQL program generates SQL statements or can take SQL statements as input, in form of string, in order to provide flexibility to the program. That is why, it can be considered to be more interactive than Static SQL. In a deeper sense, it is capable of translating the user input into an executable SQL statement. The input is capable of changing the operation performed by the program.

However, there are some statements in SQL that cannot be used dynamically.

This model is useful in programs in which we might need to use multiple kinds of operations on the same data. It is also used in such cases where we may need to use many Static SQL statements, which might prove tedious and lengthy job.  Thus, dynamic SQL provides better flexibility but still due to its complexity, it is slower than Static SQL while execution. 

Tuesday, 4 June 2013

Horner's Method

Horner's method is an efficient way of evaluating polynomials and their derivatives at a given point. It is also used for a compact presentation of the long division of a polynomial by a linear polynomial.
A polynomial Pn(x) can be written in the descending order of the powers of x:

Pn(x) = anxn + an-1xn-1 + ... + a1x + a0
or in the ascending order of the exponents:

Pn(x) = a0 + a1x + ... + an-1xn-1 + anxn.
The Horner scheme computes the value Pn(z) of the polynomial P at x = z as the sequence of steps starting with the leading coefficient an using:
bk = z·bk+1 + ak
ck = z·ck+1 + bk
                The next value of the xn in the iterative step is found by,
                xk+1 = xk – (b0/c1), where b0 = Pn(xk) = b0 and c1 = P’n(xk) in shortcut.

Generally, the iteration is continued until the required amount of precision is obtained.

Example: Solve the equation x3+9x2-18 using Horner’s Method where z= x0 = 1.5.
            Here, a0=-18, a1=0, a2=9, and a3=1.
            Starting from a3, b3=c3=a3=1.
            Hence,
            b2 = 9+1x1.5 = 10.5                          c2 = 10.5+1x1.5 = 12
            b1 = 0+10.5x1.5 = 15.75                  c1 = 15.75+12x1.5 = 33.75
            b0 = -18+15.75x1.5 = 5.625                 

            So, x1 = x0 – (b0/c1) = 1.5 – (5.625/33.75) = 1.333
            Þ x1 = z = 1.333
            
            Now, for the new value of z,
            b2 = 9 + 1x1.333 = 10.333                               c1 = 28.881
            b1 = 0+10.333x1.333 = 13.774
            b0 = -18 + 13.774x1.333 = 0.361
            So, x2 = x1 – (b0/c1) = 1.333 – (0.361/28.881) = 1.320
            Þ x2 = z = 1.320

            Again, for new value of z,
            b0 = -0.018           c1 = 28.987
            So, x3 = x2 – (b0/c1) = 1.320 – (-0.018/28.987) = 1.320

Therefore, Approximate Root = 1.320 correct to 3 decimal places. 

Saturday, 1 June 2013

Inner Join, Outer Join and Union

Inner Join: Inner join is a way of combining two relations by matching a tuple of a relation with corresponding tuple in next relation, only when they have a common value in a particular attribute.

Outer Join: Outer join is a way of combining two relations by matching a tuple of a relation with corresponding tuple in next relation, but it also includes tuples which have no match in other relation. The tuple which exist on only one relation take null value in remaining attributes. An outer join is either a left-outer join or a right-outer join based on whether all items are taken from relation on left or relation on right respectively.

Union: Union is a way of combining two relations by taking all the elements of both relations.  

Take the following example:

Relation A:
Name
Age
Randy
16
Nathan
12
Sunny
15

Relation B:
Name
Grade
Randy
11
Nathan
7
Yogi
10
Rachel
8

Union of A and B:
Name
Age
Name
Grade
Randy
16
Randy
11
Nathan
12
Nathan
7
Sunny
15
Sunny
null
Yogi
null
Yogi
10
Rachel
null
Rachel
8
*union has all elements from both relations.

Inner Join:
Name
Age
Name
Grade
Randy
16
Randy
11
Nathan
12
Nathan
7
*inner join has only elements which can be matched from A to B.

Outer Join:
Name
Age
Name
Grade
Randy
16
Randy
11
Nathan
12
Nathan
7
Yogi
null
Yogi
10
Rachel
null
Rachel
8

*it has even elements which are not in A and this outer join is specifically a right outer join as it includes all items from relation B which we assume to be on the right.

Friday, 31 May 2013

Java program to simulate FIFO and LRU page replacement algorithms

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.JFileChooser;

/**
 * This class reads a user chosen file for the reference string
 * (there should be spaces between the values in the string)
 * and simulates the selected page replacement policy i.e.
 * FIFO or LRU. It displays the status of page frames at each
 * new value in the string and saves the paging report in a plain
 * text file.
 * @author Dixit Bhatta
 * **/
public class pgReplace {

public static void main(String[] args) throws IOException {
ArrayList<Integer> ref = new ArrayList<Integer>();//creating new HashSet

try {
Scanner filein = new Scanner(getInputFileNameFromUser()); //open file
while(filein.hasNext()) {
String frame = filein.next();
int frm = Integer.parseInt(frame);
ref.add(frm);// add the integers to HashSet.
}//end of while

filein.close();//close the file

} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}

//display the size if it the set is not empty
if(!ref.isEmpty()){
int size = ref.size();
System.out.println("The length of the Reference String is: " + size);
}
//printing the read reference string using an iterator
Iterator<Integer> iter = ref.iterator();
while (iter.hasNext())
System.out.print(iter.next() +" ");

//converting the arraylist to an array
Integer frame[] = new Integer[ref.size()];
   frame = ref.toArray(frame);

   System.out.printf("\nSelect the Page Replacement Algorithm: ");
   System.out.printf("\n1.FIFO\n2.LRU\n? ");

   Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int ch = sc.nextInt();

switch(ch){
   case 1: fifo(frame,ref.size());
    break;
   case 2: lru(frame,ref.size());
    break;
   default: System.out.printf("\nInvlaid Choice");
   }
 
}
/**Simulates LRU page replacement for given reference string
* @throws IOException **/
public static void lru(Integer[] page, int n) throws IOException {
int [] frame = new int[10];
int []used = new int[10];
int index = 0;
int i,j,k,temp;
int flag=0,pf=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf("\tLRU Page Replacement");
System.out.printf("\nEnter number of Frames: ");
Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int nf = sc.nextInt();
for(i=0;i<nf;i++)
frame[i]= -1;

for(i=0;i<n;i++){
flag=0;
for(j=0;j<nf;j++){
if(frame[j]==page[i]){//no fault
System.out.printf("\n%d: ", page[i]);
out.printf("\n%d: ", page[i]);
flag=1;
break;
}
}
if(flag==0){//fault occurs
for(j=0;j<nf;j++)
used[j]=0;//all unused initially
//moving through pages and searching recently used pages
try{
for(j = 0,temp= i-1;j < nf-1;j++,temp--){
for(k = 0;k < nf;k++){
if(frame[k]==page[temp])
used[k]=1;
//mark the recently used pages
}
}
}
catch(ArrayIndexOutOfBoundsException e){
}
for(j=0;j<nf;j++)
if(used[j]==0)
index=j;
//replace the lru page with new page
frame[index]=page[i];
System.out.printf("\n%d: ", page[i]);
System.out.printf("--->F ");
out.printf("\n%d: ", page[i]);
out.printf("--->F ");
pf++;//no of page faults
}

for(k= nf-1;k>=0;k--)
if(frame[k] != -1){
System.out.printf(" %d",frame[k]);//print frames
out.printf(" %d",frame[k]);
}
}

System.out.printf("\nNumber of page faults is: %d ",pf);
out.printf("\nNumber of page faults is: %d ",pf);
out.close();
write.close();
}

/**Simulates FIFO page replacement for given reference string
* @throws IOException **/
public static void fifo(Integer[] pages, int pg) throws IOException {
int [] frame = new int[25];
int i,k,avail,count=0;
BufferedWriter write = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(write);
System.out.printf("\tFIFO Page Replacement");
System.out.printf("\nEnter number of Frames: ");
Scanner sc = new Scanner(System.in);
   while (!sc.hasNextInt()) {
       sc.next(); // discard next token, which isn't a valid int
   }
   int nof = sc.nextInt();
for(i=0;i<nof;i++)
frame[i]= -1;

   int j=0;
   System.out.printf("\n");
   out.printf("\n");
for(i=0;i<pg;i++){
System.out.printf("%d\t",pages[i]);
out.printf("%d\t",pages[i]);
avail=0;
for(k=0;k<nof;k++)
if(frame[k]==pages[i])
avail=1;

  if (avail==0){
frame[j]=pages[i];
j=(j+1) % nof;
count++;

for(k=0;k<nof;k++)
if(frame[k]!=-1){
System.out.printf("%d",frame[k]);
out.printf("%d",frame[k]);
}
   
System.out.printf("-->F");
out.printf("-->F");
     }

     if(avail==1){
   for(k=0;k<nof;k++)
    if(frame[k]!=-1){
    System.out.printf("%d",frame[k]);
    out.printf("%d",frame[k]);
    }
     }
     System.out.printf("\n");
     out.printf("\n");
}
System.out.printf("\nNo of Faults: %d",count);
out.printf("\nNo of Faults: %d",count);
out.close();
write.close();
}
/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}//end of choosing file

}

©Dixit Bhatta 2013

Thursday, 30 May 2013

Java program to reverse the content of a text file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.JFileChooser;

/**
 * This class reads a user chosen file, reads its words
 * reverses the order in which the words are stored in
 * the file i.e. the last word becomes first and the first
 * word becomes last.
 * @author Dixit Bhatta
 * **/
public class revFile {

public static void main(String[] args) throws IOException{
ArrayList<String> rev = new ArrayList<String>();//creating new ArrayList
String absolutePath = null; //to store path of the file
try {
File file = getInputFileNameFromUser();
//store the path of file to modify it later
absolutePath = file.getAbsolutePath();
Scanner filein = new Scanner(file); //open file
while(filein.hasNext()) {
String frame = filein.next();
rev.add(frame);// add the strings i.e. words to the ArrayList.
}//end of while

filein.close();//close the file

} catch (FileNotFoundException e) {//handle exception in case of error
System.err.println("FileNotFoundException: " + e.getMessage());
}

//display the number of words if it the file is not empty
if(!rev.isEmpty()){
int size = rev.size();
System.out.println("The number of words in file is: " + size);
}
Collections.reverse(rev);//reverse the arraylist
//use buffered writer and print writer to write into the file
BufferedWriter write = new BufferedWriter(new FileWriter(absolutePath));
PrintWriter out = new PrintWriter(write);
//writing the strings in the file using an iterator
Iterator<String> iter = rev.iterator();
while (iter.hasNext())
out.write(iter.next() + " ");
out.close();
}

/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}//end of choosing file


}

©Dixit Bhatta 2013

Sunday, 26 May 2013

C program for implementing Restoring Division algorithm

/*Program for implementing Restoring Division algorithm.*/
#include <stdio.h>
#include <conio.h>
#include <math.h>

int a=0,b=0,c=0,com[5]={1,0,0,0,0},s=0;
int anum[5]={0},anumcp[5] ={0},bnum[5]={0};
int acomp[5]={0},bcomp[5]={0},rem[5]={0},quo[5]={0},res[5]={0};

void binary(){
     a = fabs(a);
     b = fabs(b);
     int r, r2, i, temp;
     for(i = 0; i < 5; i++){
           r = a % 2;
           a = a / 2;
           r2 = b % 2;
           b = b / 2;
           anum[i] = r;
           anumcp[i] = r;
           bnum[i] = r2;
           if(r2 == 0){
                bcomp[i] = 1;
           }
           if(r == 0){
                acomp[i] =1;
           }
     }
   //part for two's complementing
   c = 0;
   for( i = 0; i < 5; i++){
           res[i] = com[i]+ bcomp[i] + c;
           if(res[i]>=2){
                c = 1;
           }
           else
                c = 0;
           res[i] = res[i]%2;
     }
   for(i = 4; i>= 0; i--){
     bcomp[i] = res[i];
   }
}
void add(int num[]){
     int i;
     c = 0;
     for( i = 0; i < 5; i++){
           res[i] = rem[i]+ num[i] + c;
           if(res[i]>=2){
                c = 1;
           }
           else
                c = 0;
           res[i] = res[i]%2;
     }
     for(i = 4; i>= 0; i--){
           rem[i] = res[i];
           printf("%d",rem[i]);
     }
     printf(":");
     for(i = 4; i>= 0; i--){
           printf("%d",anumcp[i]);
     }
}
void shl(){//for shift left
     int i;
     for(i = 4; i > 0  ; i--){//shift the remainder
           rem[i] = rem[i-1];
     }
     rem[0] = anumcp[4];
     for(i = 4; i > 0  ; i--){//shift the remtient
           anumcp[i] = anumcp[i-1];
     }
     anumcp[0] = 0;
     printf("\nSHIFT LEFT: ");//display together
     for(i = 4; i>= 0; i--){
           printf("%d",rem[i]);
     }
     printf(":");
     for(i = 4; i>= 0; i--){
           printf("%d",anumcp[i]);
     }
}

void main(){
     clrscr();
     int i;
     printf("\t\tRESTORING DIVISION ALGORITHM");
     printf("\nEnter two numbers to multiply: ");
     printf("\nBoth must be less than 16");
     //simulating for two numbers each below 16
     do{
           printf("\nEnter A: ");
           scanf("%d",&a);
           printf("Enter B: ");
           scanf("%d",&b);
     }while(a>=16 || b>=16);

     printf("\nExpected Quotient = %d", a/b);
     printf("\nExpected Remainder = %d", a%b);
     if(a*b <0){
           s = 1;
     }

     binary();
     printf("\n\nUnsigned Binary Equivalents are: ");
     printf("\nA = ");
     for(i = 4; i>= 0; i--){
           printf("%d",anum[i]);
     }
     printf("\nB = ");
     for(i = 4; i>= 0; i--){
           printf("%d",bnum[i]);
     }
     printf("\nB'+ 1 = ");
     for(i = 4; i>= 0; i--){
           printf("%d",bcomp[i]);
     }
     printf("\n\n-->");
     //division part
     shl();
     for(i=0;i<5;i++){
           printf("\n-->"); //start with subtraction
           printf("\nSUB B: ");
           add(bcomp);
           if(rem[4]==1){//simply add for restoring
                printf("\n-->RESTORE");
                printf("\nADD B: ");
                anumcp[0] = 0;
                add(bnum);
           }
           else{
                anumcp[0] = 1;
           }
           if(i<4)
                shl();

     }
     printf("\n----------------------------");
     printf("\nSign of the result = %d",s);
     printf("\nRemainder is = ");
     for(i = 4; i>= 0; i--){
           printf("%d",rem[i]);
     }
     printf("\nQuotient is = ");
     for(i = 4; i>= 0; i--){
           printf("%d",anumcp[i]);
     }
getch();

}