CS50 Intro To AI With Python - Notes
CS50 Intro To AI With Python - Notes
BASICS
1. Search
2. Knowledge
3. Uncertainty
4. Optimization
5. Learning
6. Neural Networks
7. Language (NLP)
SEARCH
e.g. 15 puzzle is example of search. The goal is to sort them.
A maze or google maps in real world.
Agent: entity that perceives its environment and acts upon that environment. E.g. in maps ,
agent is car.
State: a configuration of agent and its environment e.g. a specific setting of 15 puzzle
Initial state: the state in which the agent begins
Actions: choices that can be made in a state
OR actions(s) – s is input - return the set of actions (output) that can be executed in state s
Some actions are valid in some states not in others.
Transition model: a description of what state results from performing any applicable action
in any state
OR Results(s,a) – function with two inputs – returns the state resulting from performing
actions a in state s
( state and action result In an output a new desired state, this is transition model)
State Space: an interconnection of different states representing different transition models.
The states are then represented as a graph of nodes for simplicity.
Goal test: a way to determine whether a given state in a goal state. It can be one or many.
Path Cost: numerical cost associated with a given path.
We will deal with data so we have to use some data structure to store it. Here node is used
Node: a data structure that keep tracks of
A state
A parent (node that generated this node)
An action (action applied to parent to get node)
A path cost (from initial state to node)
Approach:
Starting from an initial state, explore all possible options from there and store them in a
single data structure called frontier.
Structure of Frontier:
How we structure our frontier, how we add and remove nodes effects efficiency a lot.
Depth first Search:
DFS is search algo that always expands the deepest node in the frontier.
Stack: Last in first out datatype. This stack-based approach to design frontier is called depth
first search.
Breadth first Search:
BFS is search algo that always expands the shallowest node in the frontier.
Queue: First In First Out datatype. This queue-based approach to design frontier is called
breadth first search.
In a maze, DFS hits all dead ends one by one until it finds solution while BFS explores all
paths simultaneously and finds solution. DFS can take more time in some cases while BFS
can take more memory.
A human chooses a path which he estimates is closer to the goal. So to imitate that our algo
should be intelligent to choose paths. This divides Searches into two categories
Uninformed Search
Informed Search
All the steps are labeled with their Manhattan distance ( number of steps to reach goal
optimistically i.e. ignoring walls/obstacles in case of maze). On reaching a decision point it
chooses path with first step having shorter Manhattan distance. It is better than BFS but
sometimes its not the optimal solution.
A* Search:
Search algo that expands node with lowest value of g(n) + h(n)
g(n) = cost to reach node
h(n)= estimated cost to reach goal
A* search makes a choice based on sum of g(n) i.e. no of steps it took to reach current node
from initial position and h(n) i.e. estimated cost to reach goal.
Optimal if
Adversarial Search:
Having an adversary who is acting against you, analyzing his actions and deciding
your actions is adversarial search.
Computer = X Human Player = O
X wins = 1
Draw = 0
O wins = -1
Minimax:
Max(x) aims to maximize score.
Min(o) aims to minimize score.
Game
S0 : initial state
Player(s): returns which player to move in state s
Action(s): returns legal moves in state s
Result(s,a): returns state after action a taken in state s
Terminal(s): check if state s is a terminal state
Utility(s): final numerical value for terminal state s e.g. 1,0,-1
Each decision is based on evaluating all possible actions by both players and result of it. The
action which results in max score for X or min score for O is chosen. It makes a decision tree
which gets bigger when actions increase.
Alpha-beta Pruning:
This is an optimization technique for minimax.
Alpha beta are two values that tell the best u can do or the worst u do . pruning is cutting off
some branches of search tree for efficiency.
Knowledge
Knowledge based agents
Agents that reason by operating on internal representation of knowledge
Sentence
An assertion about the world in a knowledge representation language
Propositional Logic
P Q R – symbols for proposition
Logical Connectives – Not, and, or, implication and biconditional
Model
Assignment of truth value to every propositional symbol (a ‘’possible world”)
Knowledge base
A set of sentences known by a knowledge-based agent
Entailment
Alpha entails bets
In every model in which sentence a is true, sentences b is also true
Inference
The process of deriving new sentences from old ones
Inference Algorithms
Does KB entails a?
Model Checking
To determine if KB entails a:
o Enumerate all possible models
o If in every model where KB is true, a is true then KB entails a.
o Otherwise KB does not entails a.
Theorem Proving:
Complementary literals:
A or b. if a is false, b must be true. Or P or Q, when not P is true/ P is not true, Q is true.
Even if Q consists of more than one statements, if it complements P, it (or all Qs) will be true
when P is not true
OR
When P or Q
Not P or R
Then Q or R must be true (R can also contain multiple statements)
Clause
Disjunction (connected with or) of literals e.g. P or Q or R
Conjunctive Normal Form or CNF
Logical sentence that is a conjunction of clauses
e.g. (A or B or C) and (D or not E) and (F or G). here 3 clauses are making a conjunctive
normal form.
Conversion to CNF
Eliminate biconditionals
o Turn( a if and only if b) into (a implies) and (b implies a)
Eliminate implications
o Turn (a implies b) into not a or B
Move not inwards by De Morgans Laws
o Turn not( a and b) into not a or not b
Use distributive law to distribute v wherever possible
Inference by Resolution
Conversion to CNF using inference rules. We resolve the equation by using these rules.
o P and not P gives () empty clause which is equivalent to false.
Inference by Resolution
o To determina if KB entails a
o Check if (KB entails not a ) is a contradiction?
If so, then KB entails a,
Otherwise, no entailment.
o To determine if KB entails a:
o Convert (KB and not a) to CNF
o Keep changing to see if we can use resolution to produce a new clause.
If ever we produce the empty clause(equivalent to fakse) we have a
contradiction, and KB entails a.
Otherwise, if we cant add new clauses, no entailment
First Order Logic
In propositional logic, we have propositional symbols
Types of symbols
Constant symbols: e.g. objects, people, houses
Predicate Symbol: e.g. Relations or functions
e.g. person(Minerva) here person is predicate and Minerva is constant symbol
belongs to(Minerva, Gryffindor) here Minerva belongs to Gryffindor, it’s a binary relation
Quantifier
1. Universal Quantification
Upside A means for all values of constants
2. Existential Quantification
Expression is true for some value of constants
o