A hands-on collection of classic and modern Artificial Intelligence algorithms, each implemented from scratch with worked examples and visualizations. Built around the topics of the CE417: Artificial Intelligence course.
- Author: Mohammad Javad Maheronnaghsh
- Last major update: October 25th, 2023
| # | Topic | Key idea |
|---|---|---|
| 1 | Adversarial Search | Minimax game playing |
| 2 | Local Search | Hill Climbing Β· Simulated Annealing Β· Genetic Algorithm |
| 3 | Informed Search | A* with a custom heuristic |
| 4 | Optimization | Gradient Descent (1D & 2D) |
| 5 | Reinforcement Learning | Q-Learning |
| 6 | Markov Decision Processes | Value Iteration |
| 7 | Particle Filtering | Probabilistic localization |
| 8 | Bayesian Networks | Naive Bayes Β· PageRank |
| 9 | Deep Learning | Neural nets, autoencoders, NLP, image classification |
Adversarial search is like a strategy game between two players, where one player tries to make the best moves to win while the other tries to stop them. It is used in games like chess or tic-tac-toe, where we plan our moves while thinking about how the opponent might counter them.
A two-player map-drawing game (red vs. blue). The board also has walls (yellow): if a player enters a yellow area it draws walls until it leaves again. A player loses when it has no available move. I implemented and compared three modes:
- Minimax vs. Minimax
- Minimax vs. Random Walk
- Random Walk vs. Random Walk
The comparison of results is in Adversarial Search/main.ipynb.
In local search we begin with a candidate solution and try to improve it through small changes β like rearranging the order of cities in the Traveling Salesman Problem to shorten the route. We keep going until no further improvement is possible. It finds a good solution, even if not necessarily the optimal one.
The Traveling Salesperson Problem: visit every city once and return to the start, minimizing total distance, solved three ways.
| Hill Climbing | Simulated Annealing | Genetic Algorithm |
|---|---|---|
![]() |
![]() |
![]() |
See Local Search/Traveling Salesperson Problem.ipynb.
Informed search, like A* with heuristics, is like using a map to find the quickest way to a destination. It balances how far we have already traveled (the cost) with an estimate of how much is left (the heuristic) to guide us toward the best path more efficiently.
A sliding-puzzle-style problem on a circular grid. We start from one table configuration and must reach a target one:
The allowed actions are moving an arbitrary column up/down or a row left/right, with cells circularly shifted. I designed my own admissible heuristic to guide A*. See Informed Search/.
Optimization is a key component of every ML/AI pipeline β it is how we reach the point where a model performs best. Gradient Descent is the workhorse: imagine standing on hilly terrain you can't fully see and repeatedly stepping in the steepest downhill direction until you reach the lowest point.
Functions and their derivatives, plus gradient descent in one and two dimensions, including a study of how the learning rate changes convergence.
| Derivatives | Gradient Descent (2D) |
|---|---|
![]() |
![]() |
Effect of the learning rate (1D) β too small is slow, too large overshoots:
| LR = 0.001 | LR = 0.1 | LR = 0.5 |
|---|---|---|
![]() |
![]() |
![]() |
See Optimization/Optimization.ipynb.
Reinforcement learning (RL) rewards desired behavior and penalizes undesired behavior. An agent learns by interacting with an environment, observing states, taking actions, and receiving rewards, seeking to maximize cumulative reward through trial and error. Unlike supervised learning, it learns from voluntary interaction rather than labeled examples.
A Q-Learning agent on two Gym environments:
Frozen Lake (discrete)
| Before training | After training |
|---|---|
![]() |
![]() |
Mountain Car (continuous)
| Before training | After training |
|---|---|
![]() |
![]() |
A Markov Decision Process is a mathematical framework for decision-making where outcomes are partly random and partly under the agent's control. It underpins most reinforcement-learning problems.
A generic MDP solver: pass any transition and reward function and it solves the problem via value iteration. I validated it on a worked example.
| Problem definition | Value iteration | Solution |
|---|---|---|
![]() |
![]() |
![]() |
The notebook also analyzes the effect of the discount factor (Ξ³). See Markov Decision Processes/main.ipynb.
Particle filtering estimates the hidden state of a system (such as a robot's position) from a stream of noisy measurements. It keeps a cloud of weighted "particles" β each a hypothesis about the true state β and repeatedly predicts, weights by how well each particle explains the latest observation, and resamples toward the more likely hypotheses.
A localization problem on a lunar surface: an object's position must be recovered from distance/angle readings to eight known radio stations (Copernicus, Montes, Ptolem, β¦), each with its own measurement noise. The solver triangulates the noisy readings to estimate the true coordinates.
- Stations and their noise levels are defined in
Particle Filtering/radio_stations.json. - The algorithm lives in
Particle Filtering/main.py, withinput_sample.txt/output_sample.txtshowing the expected I/O format.
Bayesian methods reason about uncertainty using probability. A Bayesian classifier predicts the most probable class given the evidence (Bayes' rule), while graph-based probabilistic models capture how entities influence one another.
1. Bayesian (Naive Bayes) MNIST classifier β classifies handwritten digits by learning a per-pixel probability distribution for each digit class, then choosing the most probable class for a new image.
| Sample digits | Learned per-class pixel likelihoods |
|---|---|
![]() |
![]() |
2. PageRank β implemented the PageRank algorithm from scratch (no networkx) to rank nodes in a graph by importance, then compared the result against the networkx reference implementation.
See Bayesian Networks/.
Deep learning uses multi-layer neural networks to learn rich representations directly from data, powering tasks from image recognition to natural-language understanding. This folder collects four projects, several built from scratch.
1. Neural Networks β MLP MNIST classifier (from scratch).
A multi-layer perceptron built without a deep-learning framework: custom loss function, activation functions, dense layers, forward/backward passes, a DataLoader, and a training loop that produces a smooth descending loss curve on MNIST.
β Deep Learning/Neural Networks/neural networks.ipynb
2. MNIST Denoiser β convolutional autoencoder. An autoencoder is trained to remove noise from MNIST digits: noise is added to the input, and the network learns to reconstruct the clean image.
| Clean | Noisy (input) | Denoised (output) |
|---|---|---|
![]() |
![]() |
![]() |
β Deep Learning/MNIST Denoiser/MNIST Denoiser.ipynb
3. Persian Comment Reviewer β NLP sentiment classification.
Classifies Persian product comments (from Digikala) as positive/negative. Includes a full text-processing pipeline β normalization, stemming/lemmatization (hazm), stop-word & punctuation removal, TF-IDF vectorization β and compares a Naive Bayes classifier with a neural model.
β Deep Learning/Persian Comment Reviewer/
4. Wedding Classifier β image classification.
Trains an image classifier to detect whether a photo depicts a wedding, using crowd-compiled (HIT) datasets, and evaluates how a model trained on Western vs. Non-Western imagery generalizes.
β Deep Learning/Wedding Classifier/Wedding Classifier.ipynb
Related areas touched or planned: uninformed search Β· CSP Β· HMM Β· regression Β· classification.
- Reinforcement learning: modeling how a child learns; animal voice recognition (signal processing)
- CSP: assigning tasks to TAs by ability, interest, and need
- HMM: modeling deadline-extension decisions
- Particle Filtering: robot navigation
- Bayesian Networks: cause and effect in psychology
- Regression: behavior/attributes β GPA
- Classification: thinking attributes β nationality
Contributions are warmly welcome β the goal is a valuable shared AI resource.
- Upload lecture notes and useful slides
- Add useful assignments with solutions (a question bank for TAs)
- Curate links to good courses and their assignments
- Add related repositories
π€ Contributions welcome! If you are interested in AI, feel free to open a PR to extend any section.
























