Unit 1 of 1 · M.Sc IT Sem 4

Unit 1: AI and soft computing implementation

Artificial Intelligence & Soft Computing Laboratory notes · PTU syllabus (PGCA1929)

4 min read11 topics11 exam questions
On this page
  1. Unit summary
  2. Logic programming: primes and family tree
  3. Puzzle solver and uninformed search
  4. Heuristic search: A*
  5. Text tokenisation, bag of words and category prediction
  6. Audio signal visualisation and generation
  7. Perceptron with fixed increment learning
  8. ADALINE and MADALINE for AND
  9. Associative memory: Hebb and outer product rules
  10. Back-propagation network for 3 epochs
  11. Fuzzy set operations and max–min composition
  12. Genetic algorithm: maximise f(x) = x² over 6 iterations
  13. Key terms
  14. Quick revision
  15. 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
Key formulasLearning rules used in the lab
  • 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))

1

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)).
2

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.
3

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)
4

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']
5

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()
6

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)
7

Topic 7

ADALINE and MADALINE for AND

Key formulasADALINE (delta or LMS rule)
  • 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.
8

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 s
9

Topic 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 epoch
10

Topic 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]]
11

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

  1. Q1.Write a Prolog rule for grandparent.
  2. Q2.How does BFS solve the water jug problem?
  3. Q3.What does the A* evaluation function contain?
  4. Q4.What is a bag of words?
  5. Q5.State the ADALINE weight update rule.
  6. Q6.What is roulette-wheel selection?

Long-answer questions

  1. Q1.Implement A* search and trace it on a graph.
  2. Q2.Train a perceptron for the AND function.
  3. Q3.Implement a back-propagation network for XOR.
  4. Q4.Implement fuzzy set operations and max–min composition.
  5. 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.

WhatsApp us