SlideShare a Scribd company logo
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
Artificial Intelligence (CS-636)
Objectives:
✓ General Problem Solver (GPS)
✓ Examples:
✓ GPS Code in Python
NOTE: The credit of the python version of GPS goes to Dr. Umair (Associate professor)
who worked in BIIT for a very long time. We appreciate his efforts in this regard.
General Problem Solver
The main idea of GPS is to solve a problem using a process called means-ends analysis, where
the problem is stated in terms of what we want to achieve at the end. We can solve a problem if
we can find some way to eliminate “the difference between what I have (current state) and
what I want (goal state and search forward to the goal, or to employ a mixture of different
search strategies.
In Python we can refine these notions as follows:
1. We can represent the current state of the world —“what I have”— or the goal state –“what
I want” – as sets of conditions. We can use lists to implement these states.. Thus, a
typical goal might be the list of two conditions (rich famous) and a typical current state
might be (unknown poor).
2. We need a list of allowable operators. This list will be constant over the course of a
problem, or even a series of problems, but we want to be able to change it and tackle a
new problem domain. The list of operators will contain dictionaries whose keys will be
✓ action
✓ preconditions
✓ add
✓ delete
3. An operator can be represented as a structure composed of an action, a list of
preconditions and a list of effects. We can place limits on the kinds of possible effects
by saying that an effect either adds or deletes a condition from the current state. Thus,
the list of effects can be split into an add-list and a delete-list.
4. A complete problem is described to GPS in terms of a starting state, a goal state, and a
set of known operators. Thus, GPS will be a function of three arguments. For example,
a sample call might be:
GPS ( current_state , goal_state , list-of-operators)
Tracing of GPS function
1. Pick first goal from goal_state list.
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
2. Check it in current_state list , it its already in current state the goal is achieved
3. If goal is not in current_state list then check it in add list, if found in add list of some
operator then check its preconditions list, We can apply an operator if we can achieve
all the preconditions, so if preconditions are not in current_state list then try to achieve
those preconditions first.
4. Once the preconditions have been achieved, apply an operator’s add and delete in
current_states list.
Example 1: Write GPS operators to achieve final state by moving blocks present in the
sequence as shown in initial state. Note that at one time only one block can be picked (only
from top).
block_ops = [
{"action":"Move block C on table",
"preconds":["Block C on A", "C-free"],
"add":["Block C on table", "A-free"],
"delete":["Block C on A"]},
{"action":"Move block A on table",
"preconds":["Block A on B","A-free"],
"add":["Block A on table", "B-free"],
"delete":["Block A on B"]},
{"action":"Move block B on C",
"preconds":["Block B on table", "B-free", "C-free"],
"add":["Block B on C"],
"delete":["Block B on table","C-free"]},
{"action":"Move block A on B",
"preconds":["Block A on table", "A-free","B-free"],
"add":["Block A on B"],
"delete":["Block A on table", "B-free"]}
]
gps(["Block B on table", "Block A on B", "Block C on A", "C-free"],
["Block C on table", "Block B on C", "Block A on B", "A-free"]
,block_ops )
TRACING:
0 Achieving: BLOCK C ON TABLE
A
B
C
Final State
C
A
B
Initial State
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
1 Achieving: BLOCK C ON A
1 Achieved: BLOCK C ON A
1 Achieving: C-FREE
1 Achieved: C-FREE
0 Action: MOVE BLOCK C ON TABLE Achieved: BLOCK C ON TABLE
0 Achieving: BLOCK B ON C
1 Achieving: BLOCK B ON TABLE
1 Achieved: BLOCK B ON TABLE
1 Achieving: B-FREE
2 Achieving: BLOCK A ON B
2 Achieved: BLOCK A ON B
2 Achieving: A-FREE
2 Achieved: A-FREE
1 Action: MOVE BLOCK A ON TABLE Achieved: B-FREE
1 Achieving: C-FREE
1 Achieved: C-FREE
0 Action: MOVE BLOCK B ON C Achieved: BLOCK B ON C
0 Achieving: BLOCK A ON B
1 Achieving: BLOCK A ON TABLE
1 Achieved: BLOCK A ON TABLE
1 Achieving: A-FREE
1 Achieved: A-FREE
1 Achieving: B-FREE
1 Achieved: B-FREE
0 Action: MOVE BLOCK A ON B Achieved: BLOCK A ON B
0 Achieving: A-FREE
0 Achieved: A-FREE
EXECUTING MOVE BLOCK C ON TABLE
EXECUTING MOVE BLOCK A ON TABLE
EXECUTING MOVE BLOCK B ON C
EXECUTING MOVE BLOCK A ON B
Example 2: A Robot is standing outside a room, the door is closed and the lights are off,
Robot can walk. In its mind each action (operator) is store. The final goal is to turn on the
lights of the room. Robot will reason that to turn on the lights it has to move inside, but to
move inside the door should be opened. So in execution the first step would be the opening of
door. Then walking inside and then turning on the lights. We will see how this reasoning
process is done in the mind of the Robot, which is using GPS program. Consider following
function call of GPS.
gps ( [“standing-outside”, “door-closed”, “lights-off”], [“lights-on”], lights-ops)
lights_ops = [
{"action":"Turn ON lights",
"preconds":[" inside-room”, “lights-off "],
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
"add":[" lights-on "],
"delete":[" lights-off"]},
{"action":"Walk inside the room",
"preconds":[" standing-outside” ,”door-open "],
"add":[" inside-room "],
"delete":[" standing-outside "]},
{"action":" Open the door ",
"preconds":["door-closed"],
"add":["door-opened"],
"delete":["door-closed"]}]
Tracing
Do as your assignment
Example 3: Driving son to school
sch_ops = [ {
"action": "drive son to school",
"preconds": ["son at home", "car works"],
"add": ["son at school"],
"delete": ["son at home"]
},
{ "action": "shop installs battery",
"preconds": ["car needs battery", "shop knows problem", "shop has
money"],
"add": ["car works"],
"delete": []
},
{ "action": "tell shop problem",
"preconds": ["in communication with shop"],
"add": ["shop knows problem"],
"delete": []
},
{ "action": "telephone shop",
"preconds": ["know phone number"],
"add": ["in communication with shop"],
"delete": []
},
{ "action": "look up number",
"preconds": ["have phone book"],
"add": ["know phone number"],
"delete": []
},
{ "action": "give shop money",
"preconds": ["have money"],
"add": ["shop has money"],
"delete": ["have money"]
}
]
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
gps(["son at home", "have money", "have phone book", "car needs
battery"], ["son at school"],sch_ops )
Tracing
0 Achieving: SON AT SCHOOL
1 Achieving: SON AT HOME
1 Achieved: SON AT HOME
1 Achieving: CAR WORKS
2 Achieving: CAR NEEDS BATTERY
2 Achieved: CAR NEEDS BATTERY
2 Achieving: SHOP KNOWS PROBLEM
3 Achieving: IN COMMUNICATION WITH SHOP
4 Achieving: KNOW PHONE NUMBER
5 Achieving: HAVE PHONE BOOK
5 Achieved: HAVE PHONE BOOK
4 Action: LOOK UP NUMBER Achieved: KNOW PHONE NUMBER
3 Action: TELEPHONE SHOP Achieved: IN COMMUNICATION WITH SHOP
2 Action: TELL SHOP PROBLEM Achieved: SHOP KNOWS PROBLEM
2 Achieving: SHOP HAS MONEY
3 Achieving: HAVE MONEY
3 Achieved: HAVE MONEY
2 Action: GIVE SHOP MONEY Achieved: SHOP HAS MONEY
1 Action: SHOP INSTALLS BATTERY Achieved: CAR WORKS
0 Action: DRIVE SON TO SCHOOL Achieved: SON AT SCHOOL
EXECUTING LOOK UP NUMBER
EXECUTING TELEPHONE SHOP
EXECUTING TELL SHOP PROBLEM
EXECUTING GIVE SHOP MONEY
EXECUTING SHOP INSTALLS BATTERY
EXECUTING DRIVE SON TO SCHOOL
Example 4: Tower of Hanoi is a puzzle with three poles and three disks. Initial state of puzzle
stacked all three disks (whose sizes are in decreasing order) in pole one. The goal is to shift all
these disks from pole1 to pole 3 with following rules.
i. Only one disk can be moved at a time.
ii. No disk can be placed on top of a disk that is smaller than it.
Write down GPS operators using python to solve this puzzle. Also trace steps followed in its
solution.
The gps function call with lists of initial states and goal states is given below.
gps( [‘large-on-pole1’,‘medium-on-pole1’,‘small-on-
pole1’,‘pole2-empty’,‘pole3-empty’], [ ‘large-on-
pole3’,‘medium-on-pole3’,‘small-on-pole3’], tower_operators)
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
tower_operators = [
{"action":"Move small on pole-3",
"preconditions":["small on pole-1", "pole-3 empty"],
"add":["small on pole-3"],
"delete":["small on pole-1",'pole-3 empty']},
{"action":"Move medium on pole-2",
"preconditions":["medium on pole-1", "small on pole-3", "pole-2
empty"],
"add":["medium on pole-2"],
"delete":["medium on pole-1","pole-2 empty"]},
{"action":"Move small on pole 2",
"preconditions":["small on pole-3", "medium on pole-2"],
"add":["small on pole-2","pole-3 empty"],
"delete":["small on pole-3"]},
{"action":"Move large on pole-3",
"preconditions":["large on pole-1","medium on pole-2", "small on
pole-2", "pole-3 empty"],
"add":["large on Pole-3", "pole-3 empty"],
"delete":["Block A on table", "B-free"]},
{"action": "Move small on pole-1",
"preconditions": ["small on pole-2", "pole-1 empty"],
"add": ["small on pole-1"],
"delete": ["small on pole-2", 'pole-1 empty']},
{"action": "Move medium on pole-3",
"preconditions": ["medium on pole-2", "large on pole-3", "small on
pole-1"],
"add": ["medium on pole-3"],
"delete": ["medium on pole-2"]},
{"action": "Move small on pole-3",
"preconditions": ["small on pole-1"],
"add": ["small on pole-3"],
"delete": ["small on pole-1"]},
Tracing
0 Achieving: LARGE ON POLE-3
0 Consider: MOVE LARGE ON POLE-3
1 Achieving: LARGE ON POLE-1
1 Achieved: LARGE ON POLE-1
1 Achieving: MEDIUM ON POLE-2
1 Consider: MOVE MEDIUM ON POLE-2
2 Achieving: MEDIUM ON POLE-1
2 Achieved: MEDIUM ON POLE-1
2 Achieving: SMALL ON POLE-3
2 Consider: MOVE SMALL ON POLE-3
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
3 Achieving: SMALL ON POLE-1
3 Achieved: SMALL ON POLE-1
3 Achieving: POLE-3 EMPTY
3 Achieved: POLE-3 EMPTY
2 Action: MOVE SMALL ON POLE-3 Achieved: SMALL ON POLE-3
2 Achieving: POLE-2 EMPTY
2 Achieved: POLE-2 EMPTY
1 Action: MOVE MEDIUM ON POLE-2 Achieved: MEDIUM ON POLE-2
1 Achieving: SMALL ON POLE-2
1 Consider: MOVE SMALL ON POLE 2
2 Achieving: SMALL ON POLE-3
2 Achieved: SMALL ON POLE-3
2 Achieving: MEDIUM ON POLE-2
2 Achieved: MEDIUM ON POLE-2
1 Action: MOVE SMALL ON POLE 2 Achieved: SMALL ON POLE-2
1 Achieving: POLE-3 EMPTY
1 Achieved: POLE-3 EMPTY
0 Action: MOVE LARGE ON POLE-3 Achieved: LARGE ON POLE-3
0 Achieving: MEDIUM ON POLE-3
0 Consider: MOVE MEDIUM ON POLE-3
1 Achieving: MEDIUM ON POLE-2
1 Achieved: MEDIUM ON POLE-2
1 Achieving: LARGE ON POLE-3
1 Achieved: LARGE ON POLE-3
1 Achieving: SMALL ON POLE-1
1 Consider: MOVE SMALL ON POLE-1
2 Achieving: SMALL ON POLE-2
2 Achieved: SMALL ON POLE-2
2 Achieving: POLE-1 EMPTY
0 Achieving: SMALL ON POLE-3
GPS Code
def gps(initial_states, goal_states, operators):
prefix = 'EXECUTING '
for operator in operators:
operator['action'] = operator['action'].upper()
operator['add'].append(prefix + operator['action'])
operator['preconds'] = [pre.upper() for pre in
operator['preconds']]
operator['delete'] = [dlst.upper() for dlstin
operator['delete']]
operator['add'] = [add.upper() for add in operator['add']]
initial_states = [state.upper() for state in initial_states]
goal_states = [goal.upper() for goal in goal_states]
final_states = achieve_all(initial_states, operators, goal_states,
[])
if not final_states:
return None
actions = [state for state in final_statesif
state.startswith(prefix)]
Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16
for a in actions:
print(a)
return actions
def achieve_all(states, ops, goals, goal_stack):
for goal in goals:
states = achieve(states, ops, goal, goal_stack)
if not states:
return None
for goal in goals:
if goal not in states:
return None
return states
def achieve(states, operators, goal, goal_stack):
print(len(goal_stack), 'Achieving: %s' % goal)
if goal in states:
print(len(goal_stack), 'Achieved: %s' % goal)
return states
if goal in goal_stack:
return None
for op in operators:
if goal not in op['add']:
continue
result = apply_operator(op, states, operators, goal, goal_stack)
if result:
return result
def apply_operator(operator, states, ops, goal, goal_stack):
result = achieve_all(states, ops, operator['preconds'], [goal] +
goal_stack)
if not result:
return None
print(len(goal_stack), 'Action: %s' % operator['action'], '
Achieved: %s' % goal)
add_list, delete_list = operator['add'], operator['delete']
return [state for state in result if state not in delete_list] +
add_list
-----------------------END -------------------

More Related Content

Similar to AI-WK-8-Lec-15-16.pdf

The Ring programming language version 1.5.2 book - Part 9 of 181
The Ring programming language version 1.5.2 book - Part 9 of 181The Ring programming language version 1.5.2 book - Part 9 of 181
The Ring programming language version 1.5.2 book - Part 9 of 181
Mahmoud Samir Fayed
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 
AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015
fridolin.wild
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
jinalgoti
 
10.ppt
10.ppt10.ppt
10.ppt
BNJYOTHI
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Sergii Khomenko
 
iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기
Wanbok Choi
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeMongoDB
 
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
Achievement Unlocked: Drive development, increase velocity, and write blissfu...Achievement Unlocked: Drive development, increase velocity, and write blissfu...
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
All Things Open
 
Reinforcement Learning using OpenAI Gym
Reinforcement Learning using OpenAI GymReinforcement Learning using OpenAI Gym
Reinforcement Learning using OpenAI Gym
Muhammad Aleem Siddiqui
 
What Swift can teach us all
What Swift can teach us allWhat Swift can teach us all
What Swift can teach us all
Pablo Villar
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
enrollmy training
 
Ai lecture 7(unit02)
Ai lecture  7(unit02)Ai lecture  7(unit02)
Ai lecture 7(unit02)
vikas dhakane
 
C Operators
C OperatorsC Operators
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
ransayo
 
Complex Architectures in Ember
Complex Architectures in EmberComplex Architectures in Ember
Complex Architectures in Ember
Matthew Beale
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Stripe
 

Similar to AI-WK-8-Lec-15-16.pdf (20)

The Ring programming language version 1.5.2 book - Part 9 of 181
The Ring programming language version 1.5.2 book - Part 9 of 181The Ring programming language version 1.5.2 book - Part 9 of 181
The Ring programming language version 1.5.2 book - Part 9 of 181
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015AR community meeting, Seoul, Korea, October 6, 2015
AR community meeting, Seoul, Korea, October 6, 2015
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
10.ppt
10.ppt10.ppt
10.ppt
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with Puppet
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
 
iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기iOS 개발자의 Flutter 체험기
iOS 개발자의 Flutter 체험기
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
Achievement Unlocked: Drive development, increase velocity, and write blissfu...Achievement Unlocked: Drive development, increase velocity, and write blissfu...
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
 
Reinforcement Learning using OpenAI Gym
Reinforcement Learning using OpenAI GymReinforcement Learning using OpenAI Gym
Reinforcement Learning using OpenAI Gym
 
What Swift can teach us all
What Swift can teach us allWhat Swift can teach us all
What Swift can teach us all
 
Sbaw090519
Sbaw090519Sbaw090519
Sbaw090519
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
Ai lecture 7(unit02)
Ai lecture  7(unit02)Ai lecture  7(unit02)
Ai lecture 7(unit02)
 
C Operators
C OperatorsC Operators
C Operators
 
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
9 11 25 14 44 6 41 15 57 9 39 16 41 2 58 8 43 12 4.docx
 
Complex Architectures in Ember
Complex Architectures in EmberComplex Architectures in Ember
Complex Architectures in Ember
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 

Recently uploaded

一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 

Recently uploaded (20)

一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 

AI-WK-8-Lec-15-16.pdf

  • 1. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 Artificial Intelligence (CS-636) Objectives: ✓ General Problem Solver (GPS) ✓ Examples: ✓ GPS Code in Python NOTE: The credit of the python version of GPS goes to Dr. Umair (Associate professor) who worked in BIIT for a very long time. We appreciate his efforts in this regard. General Problem Solver The main idea of GPS is to solve a problem using a process called means-ends analysis, where the problem is stated in terms of what we want to achieve at the end. We can solve a problem if we can find some way to eliminate “the difference between what I have (current state) and what I want (goal state and search forward to the goal, or to employ a mixture of different search strategies. In Python we can refine these notions as follows: 1. We can represent the current state of the world —“what I have”— or the goal state –“what I want” – as sets of conditions. We can use lists to implement these states.. Thus, a typical goal might be the list of two conditions (rich famous) and a typical current state might be (unknown poor). 2. We need a list of allowable operators. This list will be constant over the course of a problem, or even a series of problems, but we want to be able to change it and tackle a new problem domain. The list of operators will contain dictionaries whose keys will be ✓ action ✓ preconditions ✓ add ✓ delete 3. An operator can be represented as a structure composed of an action, a list of preconditions and a list of effects. We can place limits on the kinds of possible effects by saying that an effect either adds or deletes a condition from the current state. Thus, the list of effects can be split into an add-list and a delete-list. 4. A complete problem is described to GPS in terms of a starting state, a goal state, and a set of known operators. Thus, GPS will be a function of three arguments. For example, a sample call might be: GPS ( current_state , goal_state , list-of-operators) Tracing of GPS function 1. Pick first goal from goal_state list.
  • 2. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 2. Check it in current_state list , it its already in current state the goal is achieved 3. If goal is not in current_state list then check it in add list, if found in add list of some operator then check its preconditions list, We can apply an operator if we can achieve all the preconditions, so if preconditions are not in current_state list then try to achieve those preconditions first. 4. Once the preconditions have been achieved, apply an operator’s add and delete in current_states list. Example 1: Write GPS operators to achieve final state by moving blocks present in the sequence as shown in initial state. Note that at one time only one block can be picked (only from top). block_ops = [ {"action":"Move block C on table", "preconds":["Block C on A", "C-free"], "add":["Block C on table", "A-free"], "delete":["Block C on A"]}, {"action":"Move block A on table", "preconds":["Block A on B","A-free"], "add":["Block A on table", "B-free"], "delete":["Block A on B"]}, {"action":"Move block B on C", "preconds":["Block B on table", "B-free", "C-free"], "add":["Block B on C"], "delete":["Block B on table","C-free"]}, {"action":"Move block A on B", "preconds":["Block A on table", "A-free","B-free"], "add":["Block A on B"], "delete":["Block A on table", "B-free"]} ] gps(["Block B on table", "Block A on B", "Block C on A", "C-free"], ["Block C on table", "Block B on C", "Block A on B", "A-free"] ,block_ops ) TRACING: 0 Achieving: BLOCK C ON TABLE A B C Final State C A B Initial State
  • 3. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 1 Achieving: BLOCK C ON A 1 Achieved: BLOCK C ON A 1 Achieving: C-FREE 1 Achieved: C-FREE 0 Action: MOVE BLOCK C ON TABLE Achieved: BLOCK C ON TABLE 0 Achieving: BLOCK B ON C 1 Achieving: BLOCK B ON TABLE 1 Achieved: BLOCK B ON TABLE 1 Achieving: B-FREE 2 Achieving: BLOCK A ON B 2 Achieved: BLOCK A ON B 2 Achieving: A-FREE 2 Achieved: A-FREE 1 Action: MOVE BLOCK A ON TABLE Achieved: B-FREE 1 Achieving: C-FREE 1 Achieved: C-FREE 0 Action: MOVE BLOCK B ON C Achieved: BLOCK B ON C 0 Achieving: BLOCK A ON B 1 Achieving: BLOCK A ON TABLE 1 Achieved: BLOCK A ON TABLE 1 Achieving: A-FREE 1 Achieved: A-FREE 1 Achieving: B-FREE 1 Achieved: B-FREE 0 Action: MOVE BLOCK A ON B Achieved: BLOCK A ON B 0 Achieving: A-FREE 0 Achieved: A-FREE EXECUTING MOVE BLOCK C ON TABLE EXECUTING MOVE BLOCK A ON TABLE EXECUTING MOVE BLOCK B ON C EXECUTING MOVE BLOCK A ON B Example 2: A Robot is standing outside a room, the door is closed and the lights are off, Robot can walk. In its mind each action (operator) is store. The final goal is to turn on the lights of the room. Robot will reason that to turn on the lights it has to move inside, but to move inside the door should be opened. So in execution the first step would be the opening of door. Then walking inside and then turning on the lights. We will see how this reasoning process is done in the mind of the Robot, which is using GPS program. Consider following function call of GPS. gps ( [“standing-outside”, “door-closed”, “lights-off”], [“lights-on”], lights-ops) lights_ops = [ {"action":"Turn ON lights", "preconds":[" inside-room”, “lights-off "],
  • 4. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 "add":[" lights-on "], "delete":[" lights-off"]}, {"action":"Walk inside the room", "preconds":[" standing-outside” ,”door-open "], "add":[" inside-room "], "delete":[" standing-outside "]}, {"action":" Open the door ", "preconds":["door-closed"], "add":["door-opened"], "delete":["door-closed"]}] Tracing Do as your assignment Example 3: Driving son to school sch_ops = [ { "action": "drive son to school", "preconds": ["son at home", "car works"], "add": ["son at school"], "delete": ["son at home"] }, { "action": "shop installs battery", "preconds": ["car needs battery", "shop knows problem", "shop has money"], "add": ["car works"], "delete": [] }, { "action": "tell shop problem", "preconds": ["in communication with shop"], "add": ["shop knows problem"], "delete": [] }, { "action": "telephone shop", "preconds": ["know phone number"], "add": ["in communication with shop"], "delete": [] }, { "action": "look up number", "preconds": ["have phone book"], "add": ["know phone number"], "delete": [] }, { "action": "give shop money", "preconds": ["have money"], "add": ["shop has money"], "delete": ["have money"] } ]
  • 5. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 gps(["son at home", "have money", "have phone book", "car needs battery"], ["son at school"],sch_ops ) Tracing 0 Achieving: SON AT SCHOOL 1 Achieving: SON AT HOME 1 Achieved: SON AT HOME 1 Achieving: CAR WORKS 2 Achieving: CAR NEEDS BATTERY 2 Achieved: CAR NEEDS BATTERY 2 Achieving: SHOP KNOWS PROBLEM 3 Achieving: IN COMMUNICATION WITH SHOP 4 Achieving: KNOW PHONE NUMBER 5 Achieving: HAVE PHONE BOOK 5 Achieved: HAVE PHONE BOOK 4 Action: LOOK UP NUMBER Achieved: KNOW PHONE NUMBER 3 Action: TELEPHONE SHOP Achieved: IN COMMUNICATION WITH SHOP 2 Action: TELL SHOP PROBLEM Achieved: SHOP KNOWS PROBLEM 2 Achieving: SHOP HAS MONEY 3 Achieving: HAVE MONEY 3 Achieved: HAVE MONEY 2 Action: GIVE SHOP MONEY Achieved: SHOP HAS MONEY 1 Action: SHOP INSTALLS BATTERY Achieved: CAR WORKS 0 Action: DRIVE SON TO SCHOOL Achieved: SON AT SCHOOL EXECUTING LOOK UP NUMBER EXECUTING TELEPHONE SHOP EXECUTING TELL SHOP PROBLEM EXECUTING GIVE SHOP MONEY EXECUTING SHOP INSTALLS BATTERY EXECUTING DRIVE SON TO SCHOOL Example 4: Tower of Hanoi is a puzzle with three poles and three disks. Initial state of puzzle stacked all three disks (whose sizes are in decreasing order) in pole one. The goal is to shift all these disks from pole1 to pole 3 with following rules. i. Only one disk can be moved at a time. ii. No disk can be placed on top of a disk that is smaller than it. Write down GPS operators using python to solve this puzzle. Also trace steps followed in its solution. The gps function call with lists of initial states and goal states is given below. gps( [‘large-on-pole1’,‘medium-on-pole1’,‘small-on- pole1’,‘pole2-empty’,‘pole3-empty’], [ ‘large-on- pole3’,‘medium-on-pole3’,‘small-on-pole3’], tower_operators)
  • 6. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 tower_operators = [ {"action":"Move small on pole-3", "preconditions":["small on pole-1", "pole-3 empty"], "add":["small on pole-3"], "delete":["small on pole-1",'pole-3 empty']}, {"action":"Move medium on pole-2", "preconditions":["medium on pole-1", "small on pole-3", "pole-2 empty"], "add":["medium on pole-2"], "delete":["medium on pole-1","pole-2 empty"]}, {"action":"Move small on pole 2", "preconditions":["small on pole-3", "medium on pole-2"], "add":["small on pole-2","pole-3 empty"], "delete":["small on pole-3"]}, {"action":"Move large on pole-3", "preconditions":["large on pole-1","medium on pole-2", "small on pole-2", "pole-3 empty"], "add":["large on Pole-3", "pole-3 empty"], "delete":["Block A on table", "B-free"]}, {"action": "Move small on pole-1", "preconditions": ["small on pole-2", "pole-1 empty"], "add": ["small on pole-1"], "delete": ["small on pole-2", 'pole-1 empty']}, {"action": "Move medium on pole-3", "preconditions": ["medium on pole-2", "large on pole-3", "small on pole-1"], "add": ["medium on pole-3"], "delete": ["medium on pole-2"]}, {"action": "Move small on pole-3", "preconditions": ["small on pole-1"], "add": ["small on pole-3"], "delete": ["small on pole-1"]}, Tracing 0 Achieving: LARGE ON POLE-3 0 Consider: MOVE LARGE ON POLE-3 1 Achieving: LARGE ON POLE-1 1 Achieved: LARGE ON POLE-1 1 Achieving: MEDIUM ON POLE-2 1 Consider: MOVE MEDIUM ON POLE-2 2 Achieving: MEDIUM ON POLE-1 2 Achieved: MEDIUM ON POLE-1 2 Achieving: SMALL ON POLE-3 2 Consider: MOVE SMALL ON POLE-3
  • 7. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 3 Achieving: SMALL ON POLE-1 3 Achieved: SMALL ON POLE-1 3 Achieving: POLE-3 EMPTY 3 Achieved: POLE-3 EMPTY 2 Action: MOVE SMALL ON POLE-3 Achieved: SMALL ON POLE-3 2 Achieving: POLE-2 EMPTY 2 Achieved: POLE-2 EMPTY 1 Action: MOVE MEDIUM ON POLE-2 Achieved: MEDIUM ON POLE-2 1 Achieving: SMALL ON POLE-2 1 Consider: MOVE SMALL ON POLE 2 2 Achieving: SMALL ON POLE-3 2 Achieved: SMALL ON POLE-3 2 Achieving: MEDIUM ON POLE-2 2 Achieved: MEDIUM ON POLE-2 1 Action: MOVE SMALL ON POLE 2 Achieved: SMALL ON POLE-2 1 Achieving: POLE-3 EMPTY 1 Achieved: POLE-3 EMPTY 0 Action: MOVE LARGE ON POLE-3 Achieved: LARGE ON POLE-3 0 Achieving: MEDIUM ON POLE-3 0 Consider: MOVE MEDIUM ON POLE-3 1 Achieving: MEDIUM ON POLE-2 1 Achieved: MEDIUM ON POLE-2 1 Achieving: LARGE ON POLE-3 1 Achieved: LARGE ON POLE-3 1 Achieving: SMALL ON POLE-1 1 Consider: MOVE SMALL ON POLE-1 2 Achieving: SMALL ON POLE-2 2 Achieved: SMALL ON POLE-2 2 Achieving: POLE-1 EMPTY 0 Achieving: SMALL ON POLE-3 GPS Code def gps(initial_states, goal_states, operators): prefix = 'EXECUTING ' for operator in operators: operator['action'] = operator['action'].upper() operator['add'].append(prefix + operator['action']) operator['preconds'] = [pre.upper() for pre in operator['preconds']] operator['delete'] = [dlst.upper() for dlstin operator['delete']] operator['add'] = [add.upper() for add in operator['add']] initial_states = [state.upper() for state in initial_states] goal_states = [goal.upper() for goal in goal_states] final_states = achieve_all(initial_states, operators, goal_states, []) if not final_states: return None actions = [state for state in final_statesif state.startswith(prefix)]
  • 8. Dr. Sadaf Gull / sadaf@biit.edu.pk Lecture#15,16 for a in actions: print(a) return actions def achieve_all(states, ops, goals, goal_stack): for goal in goals: states = achieve(states, ops, goal, goal_stack) if not states: return None for goal in goals: if goal not in states: return None return states def achieve(states, operators, goal, goal_stack): print(len(goal_stack), 'Achieving: %s' % goal) if goal in states: print(len(goal_stack), 'Achieved: %s' % goal) return states if goal in goal_stack: return None for op in operators: if goal not in op['add']: continue result = apply_operator(op, states, operators, goal, goal_stack) if result: return result def apply_operator(operator, states, ops, goal, goal_stack): result = achieve_all(states, ops, operator['preconds'], [goal] + goal_stack) if not result: return None print(len(goal_stack), 'Action: %s' % operator['action'], ' Achieved: %s' % goal) add_list, delete_list = operator['add'], operator['delete'] return [state for state in result if state not in delete_list] + add_list -----------------------END -------------------