Showing posts with label Artificial intellegence. Show all posts
Showing posts with label Artificial intellegence. Show all posts

Sunday, 16 June 2019

Write a program for list manipulation using arithmetic and comparison operators.

1.  Find maximum number out of LIST of numbers
maxl([M|[]],M).
maxl([H|T],M):-
maxl(T,M1), H>M1,
M is H. maxl([H|T],M):-
maxl(T,M1), H<M1,
M is M1.

Input: maxl([8,6,4,2],X).
Output: X = 8

2.  Find minimum number out of LIST of numbers

minl([M|[]],M).
minl([H|T],M):-
minl(T,M1), H<M1,
M is H. minl([H|T],M):-
minl(T,M1), H>M1,
M is M1.

Input: maxl([8,6,4,2],X).
Output: X = 2

3.  Sort the given LIST in ascending order

bubblesort(L, L1) :- (bubble(L, L2)
-> bubblesort(L2, L1)
; L = L1 ).

bubble([A, B|T], L) :- ( A > B
-> L = [B, A|T]
; L = [A | L1],
bubble([B|T], L1)).
Input: bubblesort([8,6,4,2],X).
Output: X = [2, 4, 6, 8].

4.  Sort given list in descending order

bubblesort(L, L1) :- (bubble(L, L2)
-> bubblesort(L2, L1)
; L = L1 ).

bubble([A, B|T], L) :- ( A < B
-> L = [B, A|T]
; L = [A | L1],
bubble([B|T], L1)).

Input: bubblesort([8,6,4,2],X).
Output: X = [8, 6, 4, 2].

5.  Add two lists and store result in third list.

list_sum([],[],[]).
list_sum([H1|T1],[H2|T2],[X|L3]):- list_sum(T1,T2,L3),
X is H1+H2.

Input: list_sum([1,2,3],[4,5,6],X).
Output: X = [5, 7, 9].

6.  Apply subtraction between TWO lists and store result in third list.

list_sub([],[],[]).
list_sub([H1|T1],[H2|T2],[X|L3]):- list_sub(T1,T2,L3),
X is H1-H2.

Input: list_sub([9,6,3],[4,5,6],X).
Output: X = [5, 1, -3].

7.  Apply multiplication between TWO lists and store result in third list.

list_mul([],[],[]).
list_mul([H1|T1],[H2|T2],[X|L3]):- list_mul(T1,T2,L3),
X is H1*H2.
Input: list_sub([9,6,3],[4,5,6],X).
Output: X = [36, 30, 18].

Introduction to Recursion. Write a program to enhance the program of relationships including recursive rules. [Relationships such as ancestors, descendants, predecessors etc.]

Introduction to recursion in prolog:
The recursion in any language is a function that can call itself until the goal has been succeed. In Prolog, recursion appears when a predicate contains a goal that refers to itself.
As we have seen in the earlier chapters when a rule is called Prolog create a new query with new variables. So, it makes no difference whether a rule calls another rule or calls itself.
In Prolog and in any language, a recursive definition always has at least two parts. A first fact that acts like a stopping condition and a rule that call itself simplified. At each level the first fact is checked. If the fact is true then the recursion ends. If not, then recursion continues.
A recursive rule must never call itself with the same arguments! If that happens then the program will never end.

A Prolog program including Recursion:

parent(john,tom). parent(john,bob). parent(john,sue). parent(mary,bob). parent(mary,sue). parent(tom,mercy). parent(bob,clue). parent(bob,ray). parent(sue,clue). parent(sue,ray).

parent(sou,vedik). parent(vedik,harsh). parent(harsh,ved). parent(ved,milan). parent(milan,vishal). parent(vishal,chakki).

male(john). male(tom). male(ray). male(bob). female(mary). female(mercy). female(sue). female(clue).

grandp(X,Y):-
parent(X,A),parent(A,Y).


brother(X,Y):-
parent(A,Y),parent(A,X),male(X).

sister(X,Y):-
parent(A,Y)parent(A,X),female(X).

uncle(X,Y):-
parent(A,X),brother(Y,A).

aunty(X,Y):-
parent(A,X),sister(Y,A).

cousins(X,Y):-
uncle(X,A),parent(A,Y), Y\=X.

ansc(X,Y):-
parent(X,Y).

ansc(X,Y):-
ansc(A,Y),parent(X,A).

Input:

parent(X,tom). parent(X,mercy). parent(X,harsh). parent(X,ved). male(mary). ansc(X,chakki).
;
;
;
;

Introduction to Prolog Programming. Write a program to insert different relationship in terms of facts and rules. [Relationships such as: parent, child, sister, wife, son, daughter, etc.]. Write a program to create friend relationship in terms of facts and rules

Prolog Program to insert different relationships in terms of facts and rules:

parent(john,tom). parent(john,bob). parent(john,sue). parent(mary,bob). parent(mary,sue). parent(tom,mercy). parent(bob,clue). parent(bob,ray). parent(sue,clue). parent(sue,ray).
parent(sou,vedik). parent(ved,harsh). parent(harsh,vijay).

parent(vijay,milan). parent(milan,vishal). parent(vishal,chakki).

male(john). male(tom). male(ray). male(bob). female(mary). female(mercy). female(sue). female(clue).

grandp(X,Y):-
parent(X,A),parent(A,Y).

brother(X,Y):-
parent(A,Y),parent(A,X),male(X).

sister(X,Y):-
parent(A,Y)parent(A,X),female(X).

uncle(X,Y):-
parent(A,X),brother(Y,A).

aunty(X,Y):-
parent(A,X),sister(Y,A).

cousins(X,Y):-
uncle(X,A),parent(A,Y), Y\=X.

ansc(X,Y):-
parent(X,Y).

ansc(X,Y):-
ansc(A,Y),parent(X,A).
Input:

parent(X,tom). parent(X,mercy). parent(X,harsh). parent(X,vijay). male(mary).

Write a program to implement A* algorithm. Artificial intelligence GTU


import java.util.*;

public class AStar {

public static final int DIAGONAL_COST = 14;

public static final int V_H_COST = 10;

static class Cell{

int heuristicCost = 0; //Heuristic cost

int finalCost = 0; //G+H

int i, j;

Cell parent;

Cell(int i, int j){

this.i = i;

this.j = j;

}

@Override

public String toString(){

return "["+this.i+", "+this.j+"]";

}

}

//Blocked cells are just null Cell values in grid

static Cell [][] grid = new Cell[5][5];

static PriorityQueue<Cell> open;

static boolean closed[][];

static int startI, startJ;

static int endI, endJ;

public static void setBlocked(int i, int j){

grid[i][j] = null;

}

public static void setStartCell(int i, int j){





startI = i;

startJ = j;

}

public static void setEndCell(int i, int j){

endI = i;

endJ = j;

}

static void checkAndUpdateCost(Cell current, Cell t, int cost){ if(t == null || closed[t.i][t.j])return;

int t_final_cost = t.heuristicCost+cost;

boolean inOpen = open.contains(t);

if(!inOpen || t_final_cost<t.finalCost){

t.finalCost = t_final_cost;

t.parent = current;

if(!inOpen)open.add(t);

}

}

public static void AStar(){

//add the start location to open list.

open.add(grid[startI][startJ]);

Cell current;

while(true){

current = open.poll();

if(current==null)break;

closed[current.i][current.j]=true;

if(current.equals(grid[endI][endJ])){

return;

}

Cell t;

if(current.i-1>=0){

t = grid[current.i-1][current.j];





checkAndUpdateCost(current, t, current.finalCost+V_H_COST); if(current.j-1>=0){

t = grid[current.i-1][current.j-1];

checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);

}

if(current.j+1<grid[0].length){

t = grid[current.i-1][current.j+1];

checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);

}

}

if(current.j-1>=0){

t = grid[current.i][current.j-1];

checkAndUpdateCost(current, t, current.finalCost+V_H_COST);

}



if(current.j+1<grid[0].length){

t = grid[current.i][current.j+1];

checkAndUpdateCost(current, t, current.finalCost+V_H_COST);

}

if(current.i+1<grid.length){

t = grid[current.i+1][current.j];

checkAndUpdateCost(current, t, current.finalCost+V_H_COST);



if(current.j-1>=0){

t = grid[current.i+1][current.j-1];

checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);

}

if(current.j+1<grid[0].length){

t = grid[current.i+1][current.j+1];

checkAndUpdateCost(current, t, current.finalCost+DIAGONAL_COST);

}





}

}

}

/*    Params : tCase = test case No.

x, y = Board's dimensions

si, sj = start location's x and y coordinates

ei, ej = end location's x and y coordinates

int[][] blocked = array containing inaccessible cell coordinates */

public static void test(int tCase, int x, int y, int si, int sj, int ei, int ej, int[][] blocked){ System.out.println("\n\nTest Case #"+tCase);

//Reset

grid = new Cell[x][y];

closed = new boolean[x][y];

open = new PriorityQueue<>((Object o1, Object o2) -> { Cell c1 = (Cell)o1;

Cell c2 = (Cell)o2;

return c1.finalCost<c2.finalCost?-1: c1.finalCost>c2.finalCost?1:0;

});

//Set start position

setStartCell(si, sj); //Setting to 0,0 by default. Will be useful for the UI part

//Set End Location

setEndCell(ei, ej);

for(int i=0;i<x;++i){

for(int j=0;j<y;++j){

grid[i][j] = new Cell(i, j);

grid[i][j].heuristicCost = Math.abs(i-endI)+Math.abs(j-endJ);

}

}

grid[si][sj].finalCost = 0;

/*Set blocked cells. Simply set the cell values to null for blocked cells*/ for(int i=0;i<blocked.length;++i){




setBlocked(blocked[i][0], blocked[i][1]);

}

//Display initial map

System.out.println("Grid: ");

for(int i=0;i<x;++i){

for(int j=0;j<y;++j){

if(i==si&&j==sj)System.out.print("SO "); //Source

else if(i==ei && j==ej)System.out.print("DE "); //Destination else if(grid[i][j]!=null)System.out.printf("%-3d ", 0); else System.out.print("BL ");

}

System.out.println();

}

System.out.println();

AStar();

System.out.println("\nScores for cells: ");

for(int i=0;i<x;++i){

for(int j=0;j<x;++j){

if(grid[i][j]!=null)System.out.printf("%-3d ", grid[i][j].finalCost);

else System.out.print("BL ");

}

System.out.println();

}

System.out.println();

if(closed[endI][endJ]){

System.out.println("Path: ");

Cell current = grid[endI][endJ];

System.out.print(current);

while(current.parent!=null){

System.out.print(" -> "+current.parent);

current = current.parent;





}

System.out.println();

}else System.out.println("No possible path");

}

public static void main(String[] args) throws Exception{

//test case , grid size, start and end position and array of blocked links test(1, 5, 5, 0, 0, 3, 2, new int[][]{{0,4},{2,2},{3,1},{3,3}});

test(2, 5, 5, 0, 0, 4, 4, new int[][]{{0,4},{2,2},{3,1},{3,3}}); test(3, 7, 7, 2, 1, 5, 4, new int[][]{{4,1},{4,3},{5,3},{2,3}}); test(1, 5, 5, 0, 0, 4, 4, new int[][]{{3,4},{3,3},{4,3}});

}

}




It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...