1 State Space of a Problem Lecture 03 ITS033 – Programming & Algorithms Asst. Prof.

49
1 State Space of a Problem Lecture 03 ITS033 – Programming & Algorithms http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4 Asst. Prof. Dr. Bunyarit Uyyanonvara Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information, Computer and Communication Technology (ICT) Sirindhorn International Institute of Technology (SIIT) Thammasat University http://www.siit.tu.ac.th/bunyarit [email protected] 02 5013505 X 2005

Transcript of 1 State Space of a Problem Lecture 03 ITS033 – Programming & Algorithms Asst. Prof.

1

State Space of a Problem

Lecture 03

ITS033 – Programming & Algorithms

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

2

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force - Brute ForceTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08Topic 08 -- Transform and ConquerTransform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

3

This Week Overview State of a Problem State Space Rules State Space Traversal State space and problem solving

4

State Space: A problem representation

Lecture 02.1 ITS033 – Programming & Algorithms

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4

5

To solve a problem computationally, we have to translate To solve a problem computationally, we have to translate the problem into something that computer can the problem into something that computer can understand.understand.

In other words, we have to define In other words, we have to define state of the problemstate of the problem..

The states of a problem should be defined The states of a problem should be defined mathematicallymathematically or with or with a specific data structurea specific data structure..

State of a problemState of a problemState of a problemState of a problem

6

AA possible state of a problem possible state of a problem is one of the specific is one of the specific choices made for a solution’s components.choices made for a solution’s components.

Or a specific combination from all possibilities.Or a specific combination from all possibilities.

Or a possible step in the problem.Or a possible step in the problem.

State of a problemState of a problem

7

We have two empty containers (with no scale), one can We have two empty containers (with no scale), one can hold 4 gallons of water, the other 3 and plenty of hold 4 gallons of water, the other 3 and plenty of water.water.

How can we have exactly 2 gallons of water in How can we have exactly 2 gallons of water in container A ? container A ?

Write a program to solve this problem with user’s input Write a program to solve this problem with user’s input

of different size of containers ?of different size of containers ?

Example 1 : Water Jug Problem

3 Gal

BB

4 Gal

AA

8

Water Jug State

A state in this problem could be define as

(X, Y)

Where X is a number of gallons in A, and Y is a number of gallons in B

9

Water Jug Problem: Possible States

(0,0)

(1,1) (2,1) (3,1)

(0,2) (1,2) (3,2)

(1,3) (2,3) (3,3)

(4,0)

(4,3)

10

Two important states in a problemTwo important states in a problem

Initial StateInitial State is a first state given by the problem is a first state given by the problem

Goal StateGoal State is a solution state where the is a solution state where the problem wants to reach.problem wants to reach.

State of a problemState of a problemState of a problemState of a problem

11

Water Jug Problem

The initial state is when both jugs are empty:

(0,0)

The goal state is any state that has 2 gallons in container A:

(2,n) for any n

12

Water Jug States

(0,0) (2,0)

(2,1)

(2,2)

(2,3)

Goal StatesGoal States

Initial State

Initial State

13

Example 2: Knight Traversal Problem

On a 8 x 8 chess board, move a knight from any initial On a 8 x 8 chess board, move a knight from any initial position toposition to

all position on the board.all position on the board.

(see demo 1)(see demo 1)

+ It would be best if the knight + It would be best if the knight

make minimum number of move make minimum number of move

to complete the journey.to complete the journey.

(see demo 2)(see demo 2)

+ define State of the problem. + define State of the problem.

14

Knight Traversal

What’s wrong with this state representation ?

A state in this problem could be define as

(X, Y)

Where X is {1, 2, 3, 4, 5, 6, 7, 8} and Y is {1, 2, 3, 4, 5, 6, 7, 8}

15

The 8-puzzle - also known as the The 8-puzzle - also known as the sliding-block/tile-puzzle - is one of the most sliding-block/tile-puzzle - is one of the most popular instrument in the artificial intelligence (AI) popular instrument in the artificial intelligence (AI) studies. studies.

Arrange the tiles so that all the tiles are in Arrange the tiles so that all the tiles are in the correct positions. You do this by the correct positions. You do this by moving tiles. You can move a tile up, moving tiles. You can move a tile up, down, left, or right, so long as the following down, left, or right, so long as the following conditions are met:conditions are met:

A)A) there's no other tile blocking you in the there's no other tile blocking you in the direction of the movement; and direction of the movement; and

B)B) you're not trying to move outside of the you're not trying to move outside of the boundaries/edges. If you wish to try boundaries/edges. If you wish to try another picture, try this.another picture, try this.

Example 3 - 8 Puzzle

1468 2375

123

784 65

16

17

A man finds himself on a A man finds himself on a riverbank with a wolf, a goat, riverbank with a wolf, a goat, and a head of cabbage. He and a head of cabbage. He needs to transport all three to needs to transport all three to the other side of the river in the other side of the river in his boat. However, the boat his boat. However, the boat has room for only the man has room for only the man himself and himself and oneone other item. other item.

In his absence, the wolf would In his absence, the wolf would eat the goat, and the goat eat the goat, and the goat would eat the cabbage. would eat the cabbage. The The man is vegetarian who doesn’t man is vegetarian who doesn’t like to eat cabbage.like to eat cabbage.

Define state for this problem.Define state for this problem.

Example 4 - River-Crossing River-Crossing PuzzlePuzzle

18

State Space

Lecture 02.2

ITS033 – Programming & Algorithms

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4

19

State Space of a problemState Space of a problem is a space containing all possible is a space containing all possible states of the problem.states of the problem.

State SpaceState Space

20

Water Jug State Space The state can be represented by 2 integers x and

y: x = gallons in the 4 gallon jug y = gallons in the 3 gallon jug

State Space = (x,y) State Space = (x,y)

such that such that x x {0,1,2,3,4}, {0,1,2,3,4}, y y {0,1,2,3} {0,1,2,3}

21

Water Jug Problem: Possible States

(0,0) (1,0) (2,0) (3,0)

(0,1) (1,1) (2,1) (3,1)

(0,2) (1,2) (2,2) (3,2)

(0,3) (1,3) (2,3) (3,3)

(4,0)

(4,1)

(4,2)

(4,3)

22

It is usually not possible to It is usually not possible to listlist all possible states: all possible states: use abstractions to describe use abstractions to describe legal stateslegal states.. It may be easier to describe the It may be easier to describe the illegal statesillegal states.. Sometimes it is useful to provide a general Sometimes it is useful to provide a general

description of the state space and a set of description of the state space and a set of constraints.constraints.

State Space of a problemState Space of a problem

23

Two types of states in a problemTwo types of states in a problem Legal StatesLegal States are possible states in the problem which are possible states in the problem which

can be reached and follow the rules of that problem.can be reached and follow the rules of that problem.

Illegal StatesIllegal States are possible states in the problem that are possible states in the problem that violate the rules of that problem.violate the rules of that problem.

State of a problemState of a problem

24

Example Problem - 3 Puzzle Sliding Tile game with 3 tiles:

1 2

32

1

3

start state goal state

25

State Space: Rules

Lecture 02.3

ITS033 – Programming & Algorithms

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4

26

Solving a problem means searching for a path in the Solving a problem means searching for a path in the state space from a starting state to a goal state.state space from a starting state to a goal state.

That’s why we need to devise a set of operations (That’s why we need to devise a set of operations (rulesrules) ) that changes from one state to another.that changes from one state to another.

RulesRules

27

The problem solving system moves from one The problem solving system moves from one state to another according to well defined state to another according to well defined operations.operations.

Typically these operations are described as Typically these operations are described as

rulesrules..

A control system decides which rules are A control system decides which rules are applicable at any state, and resolves conflicts applicable at any state, and resolves conflicts and/or ambiguities.and/or ambiguities.

Rules or OperationsRules or Operations

28

Water Jug OperationsRule 1 : Fill B from pump move from any state (x,y) to a state (x,3)

Rule 2 : Fill A from pump move from any state (x,y) to a state (4,y)

Rule 3 : Empty B move from any state (x,y) to a state (x,0)

Rule 4 : Empty A move from any state (x,y) to a state (0,y)

Rule 5 : Fill Fill AA from from BB move from any state (x,y) to a state

(x+y, 0) ………. if x+y <= 4(x+y, 0) ………. if x+y <= 4

(4, x+y mod 4) .. if x+y > 4(4, x+y mod 4) .. if x+y > 4

Rule 6 : Fill Fill BB from from AA move from any state (x,y) to a state

(0, x+y) ………. if x+y <= 3(0, x+y) ………. if x+y <= 3

(x+y mod 3, 3) .. if x+y > 3(x+y mod 3, 3) .. if x+y > 3

29

Relationship between states

(0,0)

(4,0)

(4,3) (1,3)(0,0)

(0,3)

(4,3) (3,0)(0,0)

30

The search for a solution can be described by a tree - each node represents one state.

The path from a parent node to a child node represents an operation

Each node is a search tree has child nodes that represent Each node is a search tree has child nodes that represent each of the states reachable by the parent state.each of the states reachable by the parent state.

Another way of looking at it: Another way of looking at it: child nodes represent all available child nodes represent all available options once the search procedure reaches the parent nodeoptions once the search procedure reaches the parent node..

There are a number of strategies for traversing a search tree.There are a number of strategies for traversing a search tree.

Search ConceptsSearch ConceptsSearch TreeSearch TreeSearch ConceptsSearch ConceptsSearch TreeSearch Tree

31

River Crossing Problem: Relationship between states

32

Relationship between states

33

Rules: Knight Traversal Problem

Defines rules for Knight Traversal ProblemDefines rules for Knight Traversal Problem

34

State Space: Problem Solving with State Space

Lecture 02.4 ITS033 – Programming & Algorithms

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4

35

State Space Problem Problem Solving

Currently we have,Currently we have,

Set of possible states.Set of possible states.

Set of possible rules that change the state.Set of possible rules that change the state.

Specification of a starting state(s).Specification of a starting state(s).

Specification of a goal state(s).Specification of a goal state(s).

36

Simple Agent Algorithm

Problem-Solving-AgentProblem-Solving-Agent

1.1. Find an initial-stateFind an initial-state2.2. Define a goal stateDefine a goal state3.3. Define RulesDefine Rules4.4. Find a path from Initial State to a Goal stateFind a path from Initial State to a Goal state5.5. DoneDone

37

Simple Algorithm (not very clever)

.. Initial State

Random a rule

Change to a new state

Check if it is a goal state

done

yes

no

38

More complexed Algorithm

..Initial State

Pick a rule

Change to a new state

Check if it is a goal state

done

yes

no

Do something clever

39

Blind Blind (or uninformed) strategies do not (or uninformed) strategies do not exploit any of the information contained in exploit any of the information contained in a statea state

HeuristicHeuristic (or informed) strategies (or informed) strategies exploits such information to assess that exploits such information to assess that one node is “more promising” than one node is “more promising” than anotheranother

Blind Search Blind Search vs Heuristic Search Heuristic SearchBlind Search Blind Search vs Heuristic Search Heuristic Search

40

• The The blind searchblind search - a search for a solution at random. - a search for a solution at random.

• Blind searchBlind search, also called uninformed search, works with no , also called uninformed search, works with no information about the information about the search spacesearch space, other than to , other than to distinguish the goal state from all the others.distinguish the goal state from all the others.

• Pure blind search is usually simple to run, and therefore fast Pure blind search is usually simple to run, and therefore fast to realize. to realize.

• It often finds answers that are good enough for practical It often finds answers that are good enough for practical purposes, or at least can serve as the preliminary estimates. purposes, or at least can serve as the preliminary estimates.

Blind State Space SearchBlind State Space Search

41

Water Jug States

(0,0) (1,0) (2,0) (3,0)

(0,1) (1,1) (2,1) (3,1)

(0,2) (1,2) (2,2) (3,2)

(0,3) (1,3) (2,3) (3,3)

(4,0)

(4,1)

(4,2)

(4,3)

Goal StatesGoal States

Start State

Start State

42

Heuristic State Space Search

• The The heuristic searchheuristic search - a search for the best - a search for the best answer by assigning a value to each state in answer by assigning a value to each state in the State Spacethe State Space

43

Heuristic State Space Search

• ScoreScore for a state is a value calculated by comparing the for a state is a value calculated by comparing the state with the goal statestate with the goal state

• Such as a number of correct items, or a distance from a Such as a number of correct items, or a distance from a goal state.goal state.

• So the new rules will be chosen based on this values.So the new rules will be chosen based on this values.

44

1

2

3 4

5 6

7

8

1 2 3

4 5 6

7 8

Goal state

1 2 3

4 5

67 8

N1

N2

STATE

STATE

Score for a heuristic searchScore for a heuristic searchScore for a heuristic searchScore for a heuristic search

Score = correct position = 1 points

Score = correct position = 7 points

45

1

2

3 4

5 6

7

8

1 2 3

4 5 6

7 8

Goal state

1 2 3

4 5

67 8

N1

N2

STATE

STATE

For a For a blindblind strategy, N1 and N2 strategy, N1 and N2 are just two nodes (at some depth are just two nodes (at some depth in the search tree)in the search tree)

For a For a heuristicheuristic strategy counting strategy counting the number of misplaced tiles, N2 the number of misplaced tiles, N2 is more is more promising than N1promising than N1

Search ConceptsSearch ConceptsBlind Search Blind Search vs Heuristic Search Heuristic SearchSearch ConceptsSearch ConceptsBlind Search Blind Search vs Heuristic Search Heuristic Search

46

Heuristic State Space Search

• In doing so, it is more likely to get to a goal state faster In doing so, it is more likely to get to a goal state faster than blind search.than blind search.

47

ConclusionsTo solve any problems using computer

1. We define State of a Problem2. Limit the State Space3. Create Rules that move states 4. State Space Traversal or move among states

until Goal is found.

Problem is solved!

48

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force - Brute ForceTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08Topic 08 -- Transform and ConquerTransform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

49

End of Chapter 3

Thank You!

http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7&pageid=4