Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
699 views

CS50 Intro To AI With Python - Notes

CS50 intro to AI with Python covers the basics of search, knowledge, uncertainty, optimization, learning, and neural networks. It discusses search algorithms like depth-first search, breadth-first search, greedy best-first search, and A* search. It also covers knowledge representation using propositional logic and inference. Knowledge engineering techniques like solving logic puzzles and the Mastermind game are demonstrated.

Uploaded by

Maria Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
699 views

CS50 Intro To AI With Python - Notes

CS50 intro to AI with Python covers the basics of search, knowledge, uncertainty, optimization, learning, and neural networks. It discusses search algorithms like depth-first search, breadth-first search, greedy best-first search, and A* search. It also covers knowledge representation using propositional logic and inference. Knowledge engineering techniques like solving logic puzzles and the Mastermind game are demonstrated.

Uploaded by

Maria Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CS50 intro to AI with Python

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.

At the end of the day the goal is to find a solution.


Solution: a sequence of actions that leads from the initial state to a goal state
Optimal solution: a solution that has the lowest path cost among all the possible solutions

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.

 Start with a frontier that contains the initial state.


 Repeat:
o If frontier is empty there is no solution.
o Remove a node from frontier (the algo will select the node as initial state)
o If node (the selected one) contains goal, return the solution
o Else expand the node i.e. add resulting (options that are one hop away) nodes
to frontier
The problem can be if two nodes have bi-directional relation. It will make an infinite loop
between both nodes. The solution is; algo should know which nodes it has already explored
so it doesn’t go back to same node again and again.
Revised Approach:

 Start with a frontier that contains the initial state.


 Start with an empty explored set.
 Repeat:
o If frontier is empty there is no solution.
o Remove a node from frontier (the algo will select the node as initial state)
o If node (the selected one) contains goal, return the solution
o Else Add the node to explored set
o Expand the node (the selected one which just went to explored set) i.e. add
resulting (options that are one hop away) nodes to frontier if they aren’t
already in frontier/explored set

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.

// Python code to implement DFS and BFS


The code is same for both, the only word stack or queue in frontier design makes the
difference.

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

Uninformed Search: Search strategy that uses no problem-specific knowledge


Informed Search: Search strategy that uses problem-specific knowledge to find solutions
more efficiently.

Greedy Best-first Search


GBFS is search algo that explores the node that is closest to the goal, as estimated by a
heuristic function h(n) (estimate of how close we are to goal)

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

 h(n) is admissible (never overestimates the true cost)


 h(n) is consistent (for every node n and n’ with step cost c , h(n) <= h(n’)+c)

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.

Depth Limited Minimax


Evaluate finite no of moves not all. Maybe 10 or 20 moves ahead and tells the game result.
Evaluation function
What if game isn’t over then it will tell in who’s favour the game is going which is a win
probability calculated by evaluation function.
“function that estimates the expected utility of the game from a given state”

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.

//code for propositional logic concepts


Knowledge Engineering
Clue Game
A person , room and murder weapon is enclosed in an envelope hich is solution while other
cards are shown. We have to find from clues the correct solution to mystery i.e. cards in
envelope.
//code for clue game using knowledge engineering
Logic Puzzle
Propositional symbols for four harry potter school houses
Adding knowledge
AI will solve the puzzle for you
Mastermind
Putting colors in correct order while some info is given
Inference Rules

 In implication, premises or condition to be is above a horizontal line and the


condition is below it. And it is called Modus Ponens when a is true then b is true as
well.
 Elimination: when a and b both are true, just a is also true (we are eliminating b
cause we are interested in a)
 Double Negation Elimination: Two negation signs are eliminated. E,g, its not true
that harry didn’t pass the test. It gives result harry passed the test.
 Implication Negation: if a implies b then it becomes negation a or b.
 Eliminating biconditionals: a if and only if b, it gives result (a implies b) and (b
implies a)
 De Morgan’s Law: not(a and b) then result is not a or not b OR not(a or b) then
result is not a and not b
 Distributive Law: (a and(b or c)) it forms (a and b) or (a and c) OR (a or(b and c)) it
forms (a or b) and (a or c) OR

Theorem Proving:

 Initial state: starting knowledge base


 Actions: inference rules
 Transition model: new knowledge base after inference
 Goal test: check statement we’re trying to prove
 Path cost function: number of steps in proof

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

You might also like