Unit 1: AI algorithm implementation and mini projects
Artificial Intelligence Laboratory notes · PTU syllabus (UGCC2522)
On this page
Unit summary
This lab implements classic AI algorithms in Python — DFS, BFS (Water Jug), hill climbing, A*, logic evaluation and optimisation — performs NLP with NLTK, and ends with mini projects such as Minimax games, N-Queens, a rule-based expert system, a chatbot and a CNN image classifier.
After this unit you can
- Implement uninformed and informed search algorithms
- Evaluate propositional logic and perform NLP with NLTK
- Build game-playing and constraint-satisfaction mini projects
- Create a rule-based chatbot and a simple CNN classifier
PTU syllabus topics
- Depth-First Search on a graph
- Water Jug problem via BFS
- Hill Climbing search
- A* Search on a grid
- propositional logic expression evaluation
- optimization for maximum value in a list
- NLP tasks with NLTK (tokenizing, stop-word filtering, stemming, POS tagging, chunking, NER)
- mini projects — Minimax for 2-player games
- 4-Queens CSP
- Magic Square constraint propagation
- rule-based expert system
- simple decision-making AI agent
- rule-based chatbot
- CNN image classification
- 1Tokenise
Split text into words
- 2Remove stop words
Drop 'the', 'is', 'and'
- 3Stem or lemmatise
Reduce words to their root
- 4Tag
Part-of-speech tagging
- 5Extract
Named entities and chunks
Topic 1
Search algorithms
pythonfrom collections import deque
def bfs_water_jug(a, b, target):
seen, q = set(), deque([((0, 0), [])])
while q:
(x, y), path = q.popleft()
if x == target or y == target: return path + [(x, y)]
if (x, y) in seen: continue
seen.add((x, y))
moves = [(a, y), (x, b), (0, y), (x, 0),
(x - min(x, b - y), y + min(x, b - y)),
(x + min(y, a - x), y - min(y, a - x))]
for m in moves: q.append((m, path + [(x, y)]))
bfs_water_jug(4, 3, 2)- DFS: recursive with a visited set.
- Hill climbing: move to the best neighbour while it improves the score; it can get stuck at a local maximum.
- *A on a grid:** use a priority queue ordered by f = g + h with Manhattan distance as h.
Topic 2
Logic and NLP with NLTK
pythonimport nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
text = "Students at SBS are learning artificial intelligence"
tokens = nltk.word_tokenize(text)
filtered = [w for w in tokens if w.lower() not in stopwords.words("english")]
stems = [PorterStemmer().stem(w) for w in filtered]
tags = nltk.pos_tag(tokens) # part-of-speech tags
entities = nltk.ne_chunk(tags) # named entity recognitionPropositional logic can be evaluated by generating all truth assignments with itertools.product([True, False], repeat=n).
Topic 3
Mini projects
Minimax game
Tic-tac-toe with an unbeatable AI
4-Queens CSP
Backtracking placement
Magic square
Constraint propagation
Expert system
If-then rules, e.g. disease symptoms
Decision agent
Chooses actions from percepts
Rule-based chatbot
Pattern matching on keywords
CNN classifier
Keras model on an image dataset
pythondef minimax(board, is_max):
winner = check_winner(board)
if winner is not None: return winner # +1, -1 or 0
scores = []
for move in empty_cells(board):
board[move] = "X" if is_max else "O"
scores.append(minimax(board, not is_max))
board[move] = " "
return max(scores) if is_max else min(scores)Key terms
- Water Jug problem
- A classic state-space search puzzle
- Hill climbing
- A local search that always moves to a better neighbour
- POS tagging
- Labelling words with parts of speech
- Named entity recognition
- Finding names of people, places and organisations
- CNN
- Convolutional neural network for images
Quick revision
- BFS finds the shortest Water Jug solution.
- A* uses f = g + h; Manhattan distance on grids.
- NLTK: tokenize, stopwords, stem, pos_tag, ne_chunk.
- Minimax alternates max and min recursively.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is the state representation in the Water Jug problem?
- Q2.Why can hill climbing fail?
- Q3.What are stop words?
- Q4.What does pos_tag do?
- Q5.How does a rule-based chatbot work?
Long-answer questions
- Q1.Implement the Water Jug problem using BFS and explain the state space.
- Q2.Implement A* search on a grid and explain the heuristic.
- Q3.Perform tokenisation, stop-word removal, stemming, POS tagging and NER on a paragraph using NLTK.
- Q4.Implement tic-tac-toe using the Minimax algorithm.
Stuck on this unit?
Message SBS on WhatsApp for help with Artificial Intelligence Laboratory, or to ask about studying BCA at Synetic.
