Unit 1: AI and soft computing implementation
Artificial Intelligence & Soft Computing Laboratory notes · PTU syllabus (PGCA1929)
On this page
- Unit summary
- Logic programming: primes and family tree
- Puzzle solver and uninformed search
- Heuristic search: A*
- Text tokenisation, bag of words and category prediction
- Audio signal visualisation and generation
- Perceptron with fixed increment learning
- ADALINE and MADALINE for AND
- Associative memory: Hebb and outer product rules
- Back-propagation network for 3 epochs
- Fuzzy set operations and max–min composition
- Genetic algorithm: maximise f(x) = x² over 6 iterations
- Key terms
- Quick revision
- Important questions
Unit summary
This lab puts AI and soft computing into practice: logic programming, puzzle solving, uninformed and heuristic search, text processing and classification, audio signals, perceptron and ADALINE training, associative memories with Hebb and outer-product rules, back-propagation, fuzzy set operations and a genetic algorithm.
After this unit you can
- Write logic programs and search algorithms
- Process and classify text; visualise audio
- Train perceptron, ADALINE, associative and back-propagation networks
- Implement fuzzy operations and a genetic algorithm
PTU syllabus topics
- Logic programming for prime numbers and family tree relationships
- puzzle solver
- uninformed and heuristic search implementation
- text tokenization and Bag-of-Words frequency extraction
- text category prediction
- audio signal visualization and generation
- perceptron training with fixed increment learning
- ADALINE/MADALINE AND function implementation
- auto-associative and hetero-associative networks via HEBB and outer product rules
- backpropagation network for 3 epochs
- fuzzy set operations (union, intersection, complement, difference) and max-min composition
- genetic algorithm function maximization over 6 iterations
Perceptron rule
w ← w + η (t − y) x
Hebb rule
Δw = x × y
ADALINE (delta rule)
w ← w + η (t − net) x
Max-min composition
μ(x, z) = max over y of min(μR(x, y), μS(y, z))
Topic 1
Logic programming: primes and family tree
prolog% Prolog family tree
parent(ram, sita). parent(ram, mohan). parent(sita, anu).
male(ram). male(mohan). female(sita). female(anu).
father(X, Y) :- parent(X, Y), male(X).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
% ?- grandparent(ram, anu). → true
% primes
prime(2).
prime(N) :- N > 2, N mod 2 =\= 0, \+ has_factor(N, 3).
has_factor(N, F) :- F * F =< N, (N mod F =:= 0 ; F2 is F + 2, has_factor(N, F2)).Topic 2
Puzzle solver and uninformed search
pythonfrom collections import deque
def water_jug(a=4, b=3, goal=2):
start, seen, q = (0, 0), {(0, 0)}, deque([((0, 0), [])])
while q:
(x, y), path = q.popleft()
if x == goal or y == goal: return path + [(x, y)]
for s in [(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))]:
if s not in seen: seen.add(s); q.append((s, path + [(x, y)]))
print(water_jug()) # BFS: shortest sequence of states- DFS uses a stack instead of a queue; compare nodes expanded.
Topic 3
Heuristic search: A*
pythonimport heapq
def astar(graph, h, start, goal):
open_ = [(h[start], 0, start, [start])]; closed = set()
while open_:
f, g, n, path = heapq.heappop(open_)
if n == goal: return path, g
if n in closed: continue
closed.add(n)
for m, cost in graph[n].items():
heapq.heappush(open_, (g + cost + h[m], g + cost, m, path + [m]))
g = {'S': {'A': 1, 'B': 4}, 'A': {'B': 2, 'G': 12}, 'B': {'G': 3}, 'G': {}}
print(astar(g, {'S': 7, 'A': 6, 'B': 2, 'G': 0}, 'S', 'G')) # (['S','A','B','G'], 6)Topic 4
Text tokenisation, bag of words and category prediction
pythonfrom sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
docs = ["cheap loan offer now", "meeting at ten tomorrow", "win cash offer", "project meeting notes"]
labels = ["spam", "ham", "spam", "ham"]
cv = CountVectorizer(); X = cv.fit_transform(docs)
print(cv.get_feature_names_out()); print(X.toarray()) # word frequencies
clf = MultinomialNB().fit(X, labels)
print(clf.predict(cv.transform(["cash loan offer"]))) # ['spam']Topic 5
Audio signal visualisation and generation
pythonimport numpy as np, matplotlib.pyplot as plt
from scipy.io import wavfile
fs = 44100; t = np.linspace(0, 1, fs, endpoint=False)
tone = 0.5 * np.sin(2 * np.pi * 440 * t) # 440 Hz
wavfile.write("tone.wav", fs, (tone * 32767).astype(np.int16))
plt.plot(t[:500], tone[:500]); plt.title("Waveform"); plt.show()
spec = np.abs(np.fft.rfft(tone)); plt.plot(np.fft.rfftfreq(fs, 1 / fs), spec); plt.xlim(0, 1000); plt.show()Topic 6
Perceptron with fixed increment learning
pythonimport numpy as np
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]); y = np.array([0, 0, 0, 1]) # AND
w, b = np.zeros(2), 0
for epoch in range(10):
for xi, t in zip(X, y):
out = 1 if xi @ w + b > 0 else 0
w += (t - out) * xi; b += (t - out) # fixed increment
print(w, b)Topic 7
ADALINE and MADALINE for AND
yin = b + Σ xi wi
Net input (bipolar inputs and targets)
wi = wi + α (t − yin) xi
Weight update
b = b + α (t − yin)
Bias update
- Train with bipolar AND (inputs ±1; target 1 only for (1, 1)), α = 0.1, until total squared error stops falling. MADALINE combines several ADALINEs with a majority or AND unit for XOR-type problems.
Topic 8
Associative memory: Hebb and outer product rules
pythonimport numpy as np
s = np.array([1, -1, 1, -1]); t = np.array([1, -1])
W_hetero = np.outer(s, t) # hetero-associative: s → t
W_auto = np.outer(s, s) # auto-associative: s → s
print(np.sign(s @ W_hetero), np.sign(s @ W_auto))
noisy = np.array([1, 1, 1, -1]); print(np.sign(noisy @ W_auto)) # recalls sTopic 9
Back-propagation network for 3 epochs
pythonimport numpy as np
sig = lambda z: 1 / (1 + np.exp(-z))
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]); Y = np.array([[0], [1], [1], [0]]) # XOR
rng = np.random.default_rng(1); W1, W2 = rng.normal(size=(2, 3)), rng.normal(size=(3, 1)); lr = 0.5
for epoch in range(3):
H = sig(X @ W1); O = sig(H @ W2)
dO = (Y - O) * O * (1 - O); dH = dO @ W2.T * H * (1 - H)
W2 += lr * H.T @ dO; W1 += lr * X.T @ dH
print(epoch + 1, float(np.mean((Y - O) ** 2))) # error per epochTopic 10
Fuzzy set operations and max–min composition
pythonA = {'x1': 0.2, 'x2': 0.7, 'x3': 1.0}; B = {'x1': 0.5, 'x2': 0.4, 'x3': 0.6}
union = {k: max(A[k], B[k]) for k in A}; inter = {k: min(A[k], B[k]) for k in A}
compA = {k: round(1 - v, 2) for k, v in A.items()}
diff = {k: min(A[k], 1 - B[k]) for k in A} # A − B
import numpy as np
R = np.array([[0.6, 0.3], [0.2, 0.9]]); S = np.array([[1.0, 0.5], [0.8, 0.4]])
T = np.array([[max(min(R[i, k], S[k, j]) for k in range(2)) for j in range(2)] for i in range(2)])
print(union, inter, compA, diff, T, sep="\n") # T = [[0.6,0.5],[0.8,0.4]]Topic 11
Genetic algorithm: maximise f(x) = x² over 6 iterations
pythonimport random
f = lambda x: x * x
pop = [random.randint(0, 31) for _ in range(4)] # 5-bit chromosomes
for gen in range(6):
fit = [f(x) for x in pop]
parents = random.choices(pop, weights=[v + 1 for v in fit], k=4) # roulette wheel
kids = []
for a, b in zip(parents[::2], parents[1::2]):
cut = random.randint(1, 4); m = (1 << cut) - 1
kids += [(a & ~m) | (b & m), (b & ~m) | (a & m)] # single-point crossover
pop = [x ^ (1 << random.randint(0, 4)) if random.random() < 0.1 else x for x in kids] # mutation
print(gen + 1, pop, max(f(x) for x in pop))Key terms
- BFS
- Breadth-first search exploring level by level
- Bag of words
- Text represented by word counts
- Fixed increment rule
- Perceptron weight update by the error times input
- Outer product rule
- W = sᵀt for associative memory
- Max–min composition
- Fuzzy relation composition using max of mins
Quick revision
- Prolog facts, rules, queries.
- BFS water jug; A* with f = g + h.
- CountVectorizer, Naive Bayes.
- Sine wave, WAV file, FFT.
- Perceptron, ADALINE, Hebb, back-propagation.
- Fuzzy union, intersection, complement, difference; GA selection, crossover, mutation.
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.Write a Prolog rule for grandparent.
- Q2.How does BFS solve the water jug problem?
- Q3.What does the A* evaluation function contain?
- Q4.What is a bag of words?
- Q5.State the ADALINE weight update rule.
- Q6.What is roulette-wheel selection?
Long-answer questions
- Q1.Implement A* search and trace it on a graph.
- Q2.Train a perceptron for the AND function.
- Q3.Implement a back-propagation network for XOR.
- Q4.Implement fuzzy set operations and max–min composition.
- Q5.Implement a genetic algorithm to maximise a function.
Stuck on this unit?
Message SBS on WhatsApp for help with Artificial Intelligence & Soft Computing Laboratory, or to ask about studying M.Sc IT at Synetic.
