artificial intelligence assignment_coe3

Upload: sumeet-ten-doeschate

Post on 10-Jan-2016

213 views

Category:

Documents


0 download

DESCRIPTION

,v,df,

TRANSCRIPT

ARTIFICIAL INTELLIGENCE

ARTIFICIAL INTELLIGENCEMrs. Jhilik BhattacharyaName: Sumeet LallaRoll No: 101203104Group: COE9

Artificial Intelligence Assignment

1. Tower of Hanoi

def hanoi(n, source, helper, target): if n > 0: hanoi(n - 1, source, target, helper) if source: target.append(source.pop()) hanoi(n - 1, helper, source, target) source = [4,3,2,1]target = []helper = []hanoi(len(source),source,helper,target)

print source, helper, target

Test Cases are: a)1,2,3,4 b) 1,3,4

2. Tic Tac Toe(AI):

import random import poc_ttt_gui import poc_ttt_provided as provided

NTRIALS = 100 # Number of trials to run MCMATCH = 1.0 # Score for squares played by the machine player MCOTHER = 1.0 # Score for squares played by the other player

def mc_trial(board, player): """ Takes a board and the next player to move and plays the game picking random moves. Modifies the board and returns when the game is over """ player_win = board.check_win() while player_win == None: empty = board.get_empty_squares() next_move = empty[random.randrange(len(empty))] board.move(next_move[0], next_move[1], player) player = provided.switch_player(player) player_win = board.check_win() def mc_update_scores(scores, board, player): """ Takes a grid of scores, a completed board, and which player is the machine player. Updates the scores board directly """ winner = board.check_win() if winner == None or winner == provided.DRAW: return if winner == player: match_score = MCMATCH other_score = -1 * MCOTHER else: match_score = -1 * MCMATCH other_score = MCOTHER for row in range(board.get_dim()): for col in range(board.get_dim()): status = board.square(row, col) if status == provided.EMPTY: pass elif status == player: scores[row][col] += match_score else: scores[row][col] += other_score def get_best_move(board, scores): """ Takes a current board and grid of scores and returns the square with the maximum score """ empty = board.get_empty_squares() if len(empty) == 0: return vals = [scores[square[0]][square[1]] for square in empty] max_val = max(vals) moves = [] for row in range(board.get_dim()): for col in range(board.get_dim()): if scores[row][col] == max_val and ((row, col) in empty): moves.append((row, col)) ret_move = moves[random.randrange(len(moves))] return ret_move

def mc_move(board, player, trials): """ Takes current board, which player is the machine player, and the number of trials to run. Returns a move for the machine player in the form of a (row, col) tuple """ scores = [[0 for dummy_x in range(board.get_dim())] for dummy_y in range(board.get_dim())] for _ in range(trials): trial_board = board.clone() mc_trial(trial_board, player) mc_update_scores(scores, trial_board, player) return get_best_move(board, scores)

provided.play_game(mc_move, NTRIALS, False)poc_ttt_gui.run_gui(3, provided.PLAYERX, mc_move, NTRIALS, False)

Test Cases: X for player O for AI X for AI and O for player

3. Water Jug Problem using DFS and BFS

#include

#include

struct node

{

int x, y;

struct node *next;

}*root, *left, *right;

void DFS()

{

struct node *temp;

temp = left;

printf(DFS Result\nStart State :: (%d , %d)\n, root->x, root->y);

printf(Possible DFS Result 1\n);

while(1)

{

printf((%d , %d)\n, temp->x, temp->y);

if(temp->next == NULL)

break;

temp = temp->next;

}

temp = right;

printf(Possible DFS Result 2\n);

while(1)

{

printf((%d , %d)\n, temp->x, temp->y);

if(temp->next == NULL)

break;

temp = temp->next;

}

}

void BFS(int reqJug1, int reqJug2)

{

struct node *temp1 = left, *temp2 = right;

printf(\nBFS Result\n);

printf((%d , %d)\n, root->x, root->y);

while(1)

{

printf((%d , %d)\n, temp1->x, temp1->y);

if((temp1->x == reqJug1)&&(temp1->y == reqJug2))

break;

temp1 = temp1->next;

printf((%d , %d)\n, temp2->x, temp2->y);

if((temp2->x == reqJug1)&&(temp2->y == reqJug2))

break;

temp2 = temp2->next;

}

}

int isNodePresent(struct node *nextState, int maxJug1, int maxJug2, int reqJug1, int reqJug2)

{

struct node *temp;

if((nextState->x == reqJug1) && (nextState->y == reqJug2))

return(0);

if((nextState->x == maxJug1) && (nextState->y == maxJug2))

return(1);

if((nextState->x == 0) && (nextState->y == 0))

return(1);

temp = left;

while(1)

{

if((temp->x == nextState->x) && (temp->y == nextState->y))

return(1);

else if(temp->next == NULL)

break;

else

temp = temp->next;

}

temp = right;

while(1)

{

if((temp->x == nextState->x) && (temp->y == nextState->y))

return(1);

else if(temp->next == NULL)

break;

temp = temp->next;

}

return(0);

}

struct node* genNewState(struct node *crntState, int maxJug1, int maxJug2, int reqJug1, int reqJug2)

{

int d;

struct node *nextState;

nextState = (struct node*)malloc(sizeof(struct node));

nextState->x = maxJug1;

nextState->y = crntState->y;

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

nextState->x = crntState->x;

nextState->y = maxJug2;

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

nextState->x = 0;

nextState->y = crntState->y;

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

nextState->y = 0;

nextState->x = crntState->x;

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

if((crntState->y < maxJug2) && (crntState->x != 0))

{

d = maxJug2 crntState->y;

if(d >= crntState->x)

{

nextState->x = 0;

nextState->y = crntState->y + crntState->x;

}

else

{

nextState->x = crntState->x d;

nextState->y = crntState->y + d;

}

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

}

if((crntState->x < maxJug1) && (crntState->y != 0))

{

d = maxJug1 crntState->x;

if(d >= crntState->y)

{

nextState->y = 0;

nextState->x = crntState->x + crntState->y;

}

else

{

nextState->y = crntState->y d;

nextState->x = crntState->x + d;

}

if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1, reqJug2) != 1)

return(nextState);

}

return(NULL);

}

void generateTree(int maxJug1, int maxJug2, int reqJug1, int reqJug2)

{

int flag1, flag2;

struct node *tempLeft, *tempRight;

root = (struct node*)malloc(sizeof(struct node));

root->x = 0; root->y = 0; root->next = NULL;

left = (struct node*)malloc(sizeof(struct node));

left->x = 0; left->y = maxJug2; left->next = NULL;

right = (struct node*)malloc(sizeof(struct node));

right->x = maxJug1; right->y = 0; right->next = NULL;

tempLeft = left;

tempRight = right;

while(1)

{

flag1 = 0; flag2 = 0;

if((tempLeft->x != reqJug1) || (tempLeft->y != reqJug2))

{

tempLeft->next = genNewState(tempLeft, maxJug1, maxJug2, reqJug1, reqJug2);

tempLeft = tempLeft->next;

tempLeft->next = NULL;

flag1 = 1;

}

if((tempRight->x != reqJug1) || (tempRight->y != reqJug2))

{

tempRight->next = genNewState(tempRight, maxJug1, maxJug2, reqJug1, reqJug2);

tempRight = tempRight->next;

tempRight->next = NULL;

flag2 = 1;

}

if((flag1 == 0) && (flag2 == 0))

break;

}

}

void main()

{

int maxJug1, maxJug2, reqJug1, reqJug2;

clrscr();

printf(Enter the maximum capacity of jug1 :: );

scanf(%d, &maxJug1);

printf(Enter the maximum capacity of jug2 :: );

scanf(%d, &maxJug2);

printf(Enter the required water in jug1 :: );

scanf(%d, &reqJug1);

printf(Enter the required water in jug2 :: );

scanf(%d, &reqJug2);

generateTree(maxJug1, maxJug2, reqJug1, reqJug2);

DFS();

BFS(reqJug1, reqJug2);

getch();

}

Test Cases: 4 3 0 2 3 4 2 0