SlideShare a Scribd company logo
1 of 5
Download to read offline
import sys
sys.path.append('aima-python')
from search import *
from games import *
import math
def k_in_row(k, board, move, player, delta_x_y):
'''Adapted from AIMA code in games.py
Purpose:
Function to help determine a winner in k-in-a-row
type games like TicTacToe, Connect4, Gomoku
Parameters:
k: int number in a row necessary to win
board: dictionary with (row,col) keys, 'X' or 'O' values
move: where to search from
player: str 'X' or 'O'
delta_x_y: which direction to search in
Return Value:
True if there is a line through move on board for player.
'''
(delta_x, delta_y) = delta_x_y
x, y = move
n = 0 # n is number of moves in row
while board.get((x, y)) == player:
n += 1
x, y = x + delta_x, y + delta_y
x, y = move
while board.get((x, y)) == player:
n += 1
x, y = x - delta_x, y - delta_y
n -= 1 # Because we counted move itself twice
return n >= k
def is_winner(k, h, v, board, player):
'''Adapted from AIMA code in games.py
Purpose:
Determine if given player wins in a k-in-a-row type game.
Parameters:
k: int number in a row necessary to win
h: width (horizontal) of board
v: height (vertical) of board
board: dictionary with (row,col) keys, 'X' or 'O' values
player: str 'X' or 'O'
Return Value:
True if player wins, False otherwise
'''
for row in range(1, h+1):
for col in range(1, v+1):
if (k_in_row(k, board, (row,col), player, (0,1)) or
k_in_row(k, board, (row,col), player, (1,0)) or
k_in_row(k, board, (row,col), player, (1,-1)) or
k_in_row(k, board, (row,col), player, (1,1))):
return True
return False
def connect4_eval_bad(state):
'''Example of a bad evaluation function: It gives a high
score to any state whose board has a lot of Xs in the 7th row
(that is, the bottom row)
This is a terrible plan, but at least you can see it in action,
as the agent will prefer to put its pieces on across the bottom row
if at all possible.
(Change it to row 2 or 3 if you want to see different behavior)
'''
if is_winner(4, 7, 6, state.board, 'X'):
return 1
if is_winner(4,7,6, state.board, 'O'):
return -1
ev = 0
for col in range(1,7):
if (7,col) in state.board and state.board[(7,col)] == 'X':
ev += 1
# Divide by 42 to make sure that ev is less than 1
# It is important to make sure that your evaluation is never better or worse than
# actual win/loss utility (represented by 1/-1)
return ev / 42
def connect4_eval(state):
'''Your Connect 4 evaluation function
Hopefully better than mine!
'''
h, v = 6, 7
k = 4
max_player = state.to_move
min_player = 'O' if max_player == 'X' else 'X'
def count_runs(board, player):
runs = 0
# Count contiguous runs in rows
for row in range(1, h+1):
for col in range(1, v-k+2):
if all(board.get((row, col+i), '.') == player for i in range(k)):
runs += 1
# Count contiguous runs in columns
for col in range(1, v+1):
for row in range(1, h-k+2):
if all(board.get((row+i, col), '.') == player for i in range(k)):
runs += 1
# Count contiguous runs in diagonals (NW-SE)
for row in range(1, h-k+2):
for col in range(1, v-k+2):
if all(board.get((row+i, col+i), '.') == player for i in range(k)):
runs += 1
# Count contiguous runs in diagonals (NE-SW)
for row in range(1, h-k+2):
for col in range(k, v+1):
if all(board.get((row+i, col-i), '.') == player for i in range(k)):
runs += 1
return runs
max_runs = count_runs(state.board, max_player)
min_runs = count_runs(state.board, min_player)
return max_runs - min_runs
def ab_cutoff_player(game, state):
return alpha_beta_cutoff_search(state, game, d=2, eval_fn=connect4_eval_bad)
class HW3:
def __init__(self):
pass
def ttt_game(self):
tt = TicTacToe()
tt.play_game(alpha_beta_player,query_player)
def c4_game(self):
c4 = ConnectFour()
c4.play_game(ab_cutoff_player,query_player)
def problem_1d():
# write your code for problem 1d here
pass
def problem_2b():
# write your code for problem 2b here
pass
def main():
hw3 = HW3()
# An example for you to follow to get you started on Games
print('Playing Tic-Tac-Toe...')
print('======================')
hw3.ttt_game()
## uncomment the following lines to play connect 4
#print('Playing Connect 4...')
#print('====================')
#hw3.c4_game()
if __name__ == '__main__':
main()
1
c. In the function connect4 eval, write a new evaluation function that isnt terrible. Using this
function, write code that will play an alpha-beta agent with depth cutoff 4 using your evaluation
function against a random agent. Describe your evaluation function in your writeup and explain
why you think it should do well. d. In the function problem
1d, run 20 games of a random agent versus your alpha-betacutoff agent, with each agent playing
10 times as X and 10 times as O. Have the function return its results as a tuple containing wins as
X as the first item, and wins as O as the second item. So if your agent wins every single game, it
will return (10,10).
2
a. Find the definition for Gomoku (5-in-a-row) in the aima code. Build an evaluation function for
gomoku. In your writeup, briefly explain what your evaluation function is doing.
b. In the function problem 2b, run 20 games of a random agent versus an alpha-betacutoff agent
using your evaluation function, with each agent playing 10 times as X and 10 times as O. Choose
a depth cutoff that will allow each game to complete in under 20 seconds. Have the function
return its results as a tuple containing wins as X as the first item, and wins as O as the second
item. So if your agent wins every single game, it will return (10,10).
I hve posted this question but haven't received an answer for 1d and 2b. please provide me with
the code for functions 1d and 2b. Don't leave them unanswered. Thanks

More Related Content

Similar to import sys sys.path.append(aima-python) from search import .pdf

Hive function-cheat-sheet
Hive function-cheat-sheetHive function-cheat-sheet
Hive function-cheat-sheetDr. Volkan OBAN
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquapariwar
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - pythonSunjid Hasan
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React AlicanteIgnacio Martín
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdfkesav24
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212Mahmoud Samir Fayed
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1Ke Wei Louis
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYvikram mahendra
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Ignacio Martín
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFTvikram mahendra
 
Microsoft Word Practice Exercise Set 2
Microsoft Word   Practice Exercise Set 2Microsoft Word   Practice Exercise Set 2
Microsoft Word Practice Exercise Set 2rampan
 

Similar to import sys sys.path.append(aima-python) from search import .pdf (18)

Hive function-cheat-sheet
Hive function-cheat-sheetHive function-cheat-sheet
Hive function-cheat-sheet
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
Game playing
Game playingGame playing
Game playing
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
Redux Sagas - React Alicante
Redux Sagas - React AlicanteRedux Sagas - React Alicante
Redux Sagas - React Alicante
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212
 
Python Programming
Python Programming Python Programming
Python Programming
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFT
 
Microsoft Word Practice Exercise Set 2
Microsoft Word   Practice Exercise Set 2Microsoft Word   Practice Exercise Set 2
Microsoft Word Practice Exercise Set 2
 
tic-tac-toe: Game playing
 tic-tac-toe: Game playing tic-tac-toe: Game playing
tic-tac-toe: Game playing
 

More from maheshkumar12354

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfmaheshkumar12354
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfmaheshkumar12354
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfmaheshkumar12354
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfmaheshkumar12354
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfmaheshkumar12354
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfmaheshkumar12354
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfmaheshkumar12354
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfmaheshkumar12354
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfmaheshkumar12354
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfmaheshkumar12354
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfmaheshkumar12354
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfmaheshkumar12354
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfmaheshkumar12354
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfmaheshkumar12354
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfmaheshkumar12354
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfmaheshkumar12354
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfmaheshkumar12354
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfmaheshkumar12354
 

More from maheshkumar12354 (20)

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdf
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdf
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdf
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdf
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdf
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdf
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdf
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdf
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdf
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdf
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdf
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdf
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdf
 

Recently uploaded

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

import sys sys.path.append(aima-python) from search import .pdf

  • 1. import sys sys.path.append('aima-python') from search import * from games import * import math def k_in_row(k, board, move, player, delta_x_y): '''Adapted from AIMA code in games.py Purpose: Function to help determine a winner in k-in-a-row type games like TicTacToe, Connect4, Gomoku Parameters: k: int number in a row necessary to win board: dictionary with (row,col) keys, 'X' or 'O' values move: where to search from player: str 'X' or 'O' delta_x_y: which direction to search in Return Value: True if there is a line through move on board for player. ''' (delta_x, delta_y) = delta_x_y x, y = move n = 0 # n is number of moves in row while board.get((x, y)) == player: n += 1 x, y = x + delta_x, y + delta_y x, y = move while board.get((x, y)) == player: n += 1 x, y = x - delta_x, y - delta_y n -= 1 # Because we counted move itself twice return n >= k def is_winner(k, h, v, board, player): '''Adapted from AIMA code in games.py Purpose: Determine if given player wins in a k-in-a-row type game.
  • 2. Parameters: k: int number in a row necessary to win h: width (horizontal) of board v: height (vertical) of board board: dictionary with (row,col) keys, 'X' or 'O' values player: str 'X' or 'O' Return Value: True if player wins, False otherwise ''' for row in range(1, h+1): for col in range(1, v+1): if (k_in_row(k, board, (row,col), player, (0,1)) or k_in_row(k, board, (row,col), player, (1,0)) or k_in_row(k, board, (row,col), player, (1,-1)) or k_in_row(k, board, (row,col), player, (1,1))): return True return False def connect4_eval_bad(state): '''Example of a bad evaluation function: It gives a high score to any state whose board has a lot of Xs in the 7th row (that is, the bottom row) This is a terrible plan, but at least you can see it in action, as the agent will prefer to put its pieces on across the bottom row if at all possible. (Change it to row 2 or 3 if you want to see different behavior) ''' if is_winner(4, 7, 6, state.board, 'X'): return 1 if is_winner(4,7,6, state.board, 'O'): return -1 ev = 0 for col in range(1,7): if (7,col) in state.board and state.board[(7,col)] == 'X': ev += 1
  • 3. # Divide by 42 to make sure that ev is less than 1 # It is important to make sure that your evaluation is never better or worse than # actual win/loss utility (represented by 1/-1) return ev / 42 def connect4_eval(state): '''Your Connect 4 evaluation function Hopefully better than mine! ''' h, v = 6, 7 k = 4 max_player = state.to_move min_player = 'O' if max_player == 'X' else 'X' def count_runs(board, player): runs = 0 # Count contiguous runs in rows for row in range(1, h+1): for col in range(1, v-k+2): if all(board.get((row, col+i), '.') == player for i in range(k)): runs += 1 # Count contiguous runs in columns for col in range(1, v+1): for row in range(1, h-k+2): if all(board.get((row+i, col), '.') == player for i in range(k)): runs += 1 # Count contiguous runs in diagonals (NW-SE) for row in range(1, h-k+2): for col in range(1, v-k+2): if all(board.get((row+i, col+i), '.') == player for i in range(k)): runs += 1 # Count contiguous runs in diagonals (NE-SW) for row in range(1, h-k+2): for col in range(k, v+1): if all(board.get((row+i, col-i), '.') == player for i in range(k)): runs += 1 return runs max_runs = count_runs(state.board, max_player)
  • 4. min_runs = count_runs(state.board, min_player) return max_runs - min_runs def ab_cutoff_player(game, state): return alpha_beta_cutoff_search(state, game, d=2, eval_fn=connect4_eval_bad) class HW3: def __init__(self): pass def ttt_game(self): tt = TicTacToe() tt.play_game(alpha_beta_player,query_player) def c4_game(self): c4 = ConnectFour() c4.play_game(ab_cutoff_player,query_player) def problem_1d(): # write your code for problem 1d here pass def problem_2b(): # write your code for problem 2b here pass def main(): hw3 = HW3() # An example for you to follow to get you started on Games print('Playing Tic-Tac-Toe...') print('======================') hw3.ttt_game() ## uncomment the following lines to play connect 4 #print('Playing Connect 4...') #print('====================') #hw3.c4_game() if __name__ == '__main__': main() 1 c. In the function connect4 eval, write a new evaluation function that isnt terrible. Using this
  • 5. function, write code that will play an alpha-beta agent with depth cutoff 4 using your evaluation function against a random agent. Describe your evaluation function in your writeup and explain why you think it should do well. d. In the function problem 1d, run 20 games of a random agent versus your alpha-betacutoff agent, with each agent playing 10 times as X and 10 times as O. Have the function return its results as a tuple containing wins as X as the first item, and wins as O as the second item. So if your agent wins every single game, it will return (10,10). 2 a. Find the definition for Gomoku (5-in-a-row) in the aima code. Build an evaluation function for gomoku. In your writeup, briefly explain what your evaluation function is doing. b. In the function problem 2b, run 20 games of a random agent versus an alpha-betacutoff agent using your evaluation function, with each agent playing 10 times as X and 10 times as O. Choose a depth cutoff that will allow each game to complete in under 20 seconds. Have the function return its results as a tuple containing wins as X as the first item, and wins as O as the second item. So if your agent wins every single game, it will return (10,10). I hve posted this question but haven't received an answer for 1d and 2b. please provide me with the code for functions 1d and 2b. Don't leave them unanswered. Thanks