Jug problem What is Jug problem 4 litter
Jug problem
What is Jug problem? 4 litter Jug 3 litter Jug • Use only 2 kinds of jugs above, get 2 litter water. • No tics on each jug.
It’s easy for us…? • Please consider. • Answer is in the next page…
An example of answer 1. Fill up 3 l jug. 2. Move water in 4 l jug into 3 l jug as much as you can. 3. Empty 3 l jug. 4. Move all water in 4 l jug into 3 l jug. 5. Fill up 4 l jug. 6. Move water in 4 l jug into 3 l jug as much as you can. 7. 2 litter water remains in 4 l jug.
Solve this problem by OPS 5 • Find the order of operation for jugs and water. – Of course, we should consider that program and programmer don’t know the answer. • What type of data (class) do we need? • What rules do we need?
Image of program Status of jugs Rule Fill up 4 l jug Rule Move water from 4 l jug to 3 l jug. Rule • Each rule will be executable depending on the status of jugs. (System choose a rule from executable rules. ) • Repeating rule execution, finish execution when goal status is satisfied.
What rule do we need? • Write all actions for each status of jugs. Ex:If 4 l jug is not full, full up 4 l jug. Formal expression: if x < 4 then (x, y)-> (4, y) Here, (x, y) means amount of water in 4 l and 3 l jug respectively. • Exercise 1 Write all actions in Japanese(or English) and the formal expression.
Make a rule • Convert a written description to a form of rule. (p rule 1 (youki ^4 l < 4 ^3 l <Y>) --> (make youki ^4 l 4 ^3 l <Y>) ) • Ordinary, class should be defined before rule description, however, in this tutorial, we define rule before class definition, then class will be defined depending on the rule description.
Class definition • Define classes according to the rule. • Consider about the rule in previous page… (literalize youki 4 l 3 l ) • Exercise 2 Convert all actions in Ex. 1 into the format of rule in OPS 5. Caution: It is insufficient this class definition. If you add some attribute in making rule, you should modify class definition. Making rule and class definition process will be repeated.
Rule for goal • Goal status is (x, y)=(2, 0), then goal rules will be… (p gole (youki ^4 l 2 ^3 l 0) --> (write |Goal!|(crlf)) (halt) )
Infinite loop problem… • Infinite loop will occur if you make only rules about jugs. Initial: (0, 0) Fill up 4 l jug (4、0) Empty 4 l jug Fill up 4 l jug (0、0) (4、0) A rule for avoiding appearance of same status repeatedly is needed not to get into a infinite loop.
How to deal with the problem • If same status is appeared, the last one is not needed. Initial: (0, 0) Fill up 4 l jugs Empty 4 l jugs (4、0) Same status (0、0) This status is cause of a infinite loop. Remove it. Exercise 3: Make a rule of this operation. It is needed some way to compare new or old data.
From here… • In following pages, class and rules definition is described. These are just examples. • It is not only one way to define class and rules. • There are many kinds of programs to solve this problem. • In this theme, all programs that can solve this problem are accepted.
Class definition • Class definition is following. (literalize youki 4 l 3 l goukei num) • “goukei” keeps the value of the total amount of water. • “num” keeps the value to compare old and new data for avoiding infinite loop.
Rule definition • Rule example If 3 l jug is not full and total amount of water is less than or equal to 3 l, move all water in 4 l jug into 3 l jug (p rule (youki ^4 l {<X> > 0} ^3 l {<Y> < 3} ^goukei <= 3 ^num <N>) --> (make youki ^4 l 0 ^3 l (compute <X> + <Y>) ^goukei (compute <X> + <Y>) ^num (compute <N> + 1 )) (write |4 l=0, 3 l=|(compute <X> + <Y>)(crlf)) ) *In command part, ”make” command is appropriate, because it can not compare old and new data if “modify” command is used. “^num” is a attribute for compare new or old data. In each making of the data, num is increased.
Additional explanation • It is almost by implement the explanation so far. • If some error occurs, please check spaces between words or symbols. (Space is needed in any borders between words or symbols. Otherwise, errors will occur. ) • Furthermore, it is able to set priority for each rules. Please refer the additional explanations in the text book.
- Slides: 17