[Artificial Intelligence Experiment] Predicate representation and production knowledge representation

1. Experiment purpose

1. Familiar with predicate logic notation;

2. Understand and master production knowledge representation methods and implement the rule base of production systems.

2. Experimental content

Implemented through C language programming:

1. Monkey picking banana problem

2. Animal identification system

(1) Establish rules for identifying seven types of animal identification systems;

(2) Determine the animals that need to be identified and their attributes (7 animals, namely: tiger, leopard, zebra, giraffe, penguin, ostrich and albatross).

3. Problem description

1. There is a monkey (i.e. robot) in the house, located at location a. There is a bunch of bananas on the ceiling above point c. The monkey wants to eat it, but cannot pick it. There is also a box at B of the room. If the monkey stands on the box, it can touch the ceiling. As shown in Figure 1, for the above problems, knowledge can be described through predicate logic representation. Realize the solution process of the monkey picking banana problem.

2. Establish a rule base for the animal identification system and write programs to identify 7 types of animals including tiger, leopard, zebra, giraffe, penguin, ostrich, and albatross.

In order to identify these animals, a rule base including the following can be established based on the characteristics of animal identification:

R1: if the animal has hair then the animal is a mammal

R2: if the animal has milk then the animal is a mammal

R3: if the animal has feathers then the animal is a bird

R4: if the animal can fly and lay eggs then the animal is a bird

R5: if the animal eats meat then the animal is a carnivore

R6: if the animal has sharp teeth and claws and eyes forward then the animal is a carnivore.

R7: if the animal is a mammal and has hooves then the animal is an ungulate

R8: if the animal is a mammal and the ruminant then the animal is an ungulate

R9: if the animal is a mammal and is a carnivore and has yellowish brown color and dark spots then the animal is a leopard

R10: if the animal is a mammal and is a carnivore and has yellowish brown color and black stripes then the animal is a tiger

R11: if the animal is an ungulate and has a long neck and long legs and dark spots then the animal is a giraffe

R12: if the animal is an ungulate and has black stripes then the animal is a zebra

R13: if the animal is a bird and cannot fly and has a long neck and long legs and is black and white then the animal is an ostrich

R14: if the animal is a bird and cannot fly and can swim and has black and white colors then the animal is a penguin.

R15: if the animal is a bird and good at flying then the animal is an albatross

4. Source program

1. The source program of the monkey picking banana problem (implemented in C language, only the main function code is shown)

1.int i = 0; //Used to record the number of steps, it is a global variable
2.
3.void monkey_solution() //Solution begins
4.{
5. int j = 0;
6. char monkey, box, banana, a[5];
7. printf("Please enter the positions of the monkey, box, and banana (separated by,):");
8. for (int j = 0; j < 5; j + + )
9. {
10. scanf_s("%c", & amp;a[j]);
11. }
12. monkey = a[0];
13. box = a[2];
14. banana = a[4];
15. if (monkey != box)
16. {
17. monkey_goto_box(monkey,box);
18. }
19. if (box != banana)
20. {
21. monkey_move_box(box,banana);
twenty two. }
23. monkey_on_box();
24. monkey_get_banana();
25.}
26.
27.void monkey_goto_box(char x, char y) //The monkey walks from x to y
28.{
29. extern int i;
30. i = i + 1;
31. printf("Step %d: Monkey walks from %c to %c\\
",i,x,y);
32.}
33.
34.void monkey_move_box(char x, char y) //The monkey moves the box from x to y
35.{
36. extern int i;
37. i = i + 1;
38. printf("Step %d: Monkey transports the box from %c to %c\\
", i, x, y);
39.}
40.
41.void monkey_on_box() //Monkey climbs into the box
42.{
43. extern int i;
44. i = i + 1;
45. printf("Step %d: Monkey climbs into the box\\
", i);
46.}
47.
48.void monkey_get_banana() //The monkey picked the banana
49.{
50. extern int i;
51. i = i + 1;
52. printf("Step %d: Monkey picked banana\\
", i);
53.}

2. Source program of animal identification system (implemented in C language, only the main function code is shown)

1.void animal_system() //System display
2.{
3. printf("[Animal Classification AI System v1.0]\\
");
4. //Animal feature database, the feature numbers are 1~24 in order, and the animal names are 25~31
5. char *features[] = { "", "has milk", "has hair", "has feathers", "can fly", "can lay eggs", \ "eat meat", "has sharp teeth", "has claws", "eyes forward", "has hooves",
6. "rumination", "tawny", "dark spots", "black stripes", "long neck", "long legs", "flightless" , "Can swim", "Available in black and white", "Good at flying" };
7. int i = 1; //Only display the animal feature number
8. while (i < 21)
9. {
10. printf("%d.%s ",i,features[i]);
11. i = i + 1;
12. if (i % 10 == 1)
13. {
14. printf("\\
");
15. }
16. }
17. //User input
18. begin:printf("\\
Please select the characteristic numbers the animal has (separate them with spaces, enter 999 to end):");
19. int a[21] = { 0 };
20. for (int j = 1; j < 21; j + + )
twenty one. {
22. int temp;
23. scanf_s("%d", & amp;temp);
24. if (temp == 999) break;
25. if (temp < 1 || temp > 20)
26. {
27. printf("The serial number is wrong, please re-enter:");
28. goto begin;
29. }
30. a[j] = temp;
31. }
32. rules(a);
33.}
34.
35.int rules( int *arr ) //Rule base
36.{
37. int temp, f;
38. int b[15] = { 0 };
39. f = 0;
40. for (int j = 1; j < 21; j + + )
41. {
42. temp = arr[j];
43.
44. //Start logical reasoning and confirmation to determine the animal type
45. if (temp == 1 || temp == 2) //Satisfies the conditions for mammals
46. {
47. if (temp == 1) printf("\\
Usage rules: animals with milk are mammals\\
");
48. if (temp == 2) printf("\\
Usage rules: animals with hair are mammals\\
");
49. b[1] = 1; //Indicates mammals
50.continue;
51. }
52. if (temp == 3 || (temp == 4 & amp; & temp == 5))
53. {
54. if (temp == 3) printf("\\
Usage rules: Feathered animals are birds\\
");
55. if (temp == 4 & amp; & temp == 5) printf("\\
Usage rules: animals that can fly and lay eggs are birds\\
");
56. b[2] = 1; //Indicates birds
57.continue;
58. }
59. if (temp == 6 || temp == 7 || temp == 8 || temp == 9)
60. {
61. if (temp == 6)
62. {
63. printf("\\
Usage rules: animals that eat meat are carnivores\\
");
64. b[3] = 1; //Indicates carnivores
65. }
66. if (temp == 7 || temp == 8 || temp == 9) f = f + 1;
67. if (f == 3)
68. {
69. printf("\\
Usage rules: Animals with claws, sharp teeth and eyes facing forward are carnivores\\
");
70. b[3] = 1; //Indicates carnivores
71. }
72.continue;
73. }
74. if (b[1] == 1 & amp; & amp; (temp == 10 || temp == 11))
75. {
76. if (temp == 10) printf("\\
Usage rules: Hoofed mammals are ungulates\\
");
77. if (temp == 11) printf("\\
Usage rules: ruminant mammals are ungulates\\
");
78. b[4] = 1; //Indicates hoofed animals
79.continue;
80. }
81.
82.
83.
84. //Draw the final conclusion
85. if ( b[1] == 1 & amp; & amp; b[3] == 1) //Carnivorous mammals
86. {
87. if (temp == 12) b[5] = 1; //There is yellowish brown
88. if (temp == 13) b[6] = 1; //There are dark spots
89. if (temp == 14) b[7] = 1; //There are black stripes
90. if ((b[5] == 1 & amp; & temp == 13) || (b[6] == 1 & amp; & temp == 12))
91. {
92. printf("\\
Usage rules: The carnivorous mammal with yellowish brown and dark spots is a leopard\\
");
93. printf("\\
\\
The recognized animal is: leopard\\
");
94. break;
95. }
96. if ((b[5] == 1 & amp; & temp == 14) || (b[7] == 1 & amp; & temp == 12))
97. {
98. printf("\\
Usage rules: The carnivorous mammal with tawny and black stripes is a tiger\\
");
99. printf("\\
\\
The recognized animal is: tiger\\
");
100. break;
101. }
102. }
103. if ( b[4] == 1 ) //hoofed animals
104. {
105. if (temp == 13) b[6] = 1; //There are dark spots
106. if (temp == 15) b[8] = 1; //has a long neck
107. if (temp == 16) b[9] = 1; //has long legs
108. if (temp == 14)
109. {
110. printf("\\
Usage rules: ungulates with black stripes are zebras\\
");
111. printf("\\
\\
The recognized animal is: zebra\\
");
112.break;
113. }
114. if ( b[6] == 1 & amp; & amp; b[8] == 1 & amp; & amp; b[9] == 1 )
115. {
116. printf("\\
Usage rules: An ungulate animal with a long neck, long legs, and dark spots is a giraffe\\
");
117. printf("\\
\\
The recognized animal is: giraffe\\
");
118.break;
119. }
120. }
121. if ( b[2] == 1 )
122. {
123. if (temp == 15) b[8] = 1; //has a long neck
124. if (temp == 16) b[9] = 1; //has long legs
125. if (temp == 17) b[10] = 1; //Cannot fly
126. if (temp == 18) b[11] = 1; //can swim
127. if (temp == 19) b[12] = 1; //There are two colors: black and white
128. if (temp == 20) b[13] = 1; //good at flying
129. if ( b[8] == 1 & amp; & amp; b[9] == 1 & amp; & amp; b[10] == 1 & amp; & amp; b[12] == 1 )
130. {
131. printf("\\
Usage rules: ostriches are flightless birds with long necks, long legs, and black and white colors\\
");
132. printf("\\
\\
The recognized animal is: ostrich\\
");
133.break;
134. }
135. if ( b[10] == 1 & amp; & amp; b[11] == 1 & amp; & amp; b[12] == 1 )
136. {
137. printf("\\
Usage rules: The flightless and swimming birds with black and white colors are penguins\\
");
138. printf("\\
\\
The recognized animal is: penguin\\
");
139.break;
140. }
141. if ( b[13] == 1 )
142. {
143. printf("\\
Usage rules: The bird that is good at flying is the albatross\\
");
144. printf("\\
\\
The recognized animal is: albatross\\
");
145. break;
146. }
147. }
148. if ( j == 20 )
149. {
150. printf("\\
\\
Sorry! Recognition failed!\\
");
151. }
152. }
153.}

5. Experimental process and results

1. Monkey picking banana problem

Problem-solving ideas:

Set the predicate logic according to the meaning of the question as follows:

monkey_goto_box(x,y): The monkey walks from x to y

monkey_move_box(x,y): The monkey moves the box from x to y

The last two steps for the monkey to pick the banana must be the monkey climbing into the box and the monkey picking the banana, so there is no need to consider other situations in the last two steps. The action before climbing onto the box is determined by the relative positions of the three initial monkey-banana boxes.

Two basic rules are set here:

Rule 1: If the monkey and the box are not in the same location, the monkey needs to go to the box.

Rule 2: If the box and the banana are not in the same location, transport the box to the banana.

Input the initial positions of the monkey, banana, and box as a, b, and c respectively. The running results are as follows:

The above results are the output of the monkey, banana, and box with different initial positions. For the situation where two of the three are in the same position or all three are in the same position, the running results are as follows:

2. Animal identification system

Problem-solving ideas:

① Establish the feature array rule of each rule based on the animal recognition rules given in the question;

②The first step to determine the specific type of animal is to identify the category of the animal.

The categories of animals in this question include four categories: mammals, birds, carnivores, and ungulates;

③On the basis of knowing the animal category and then based on the specific detailed characteristics, the specific type of animal can be judged.

Identify the tiger:

Identifying zebras:

Identify an albatross:

Recognition failed:

6. Problems encountered and solutions

Question 1: In the process of implementing the animal recognition system in C language, since multiple conditions are met at the same time or not at the same time, a large number of logical ANDs and logical ORs need to be used for combination, and multiple variables need to be set for marking. This becomes very complicated because only one condition can be executed and recognized at a time.

Solution: In the process of using logical AND and logical OR, the most common problem is that it is difficult to grasp the priorities of different operators, which leads to the difference between the expected goals and the actual results. Therefore, you need to add parentheses when using operators to ensure that the conditions operate as expected. In addition, there are too many variables used for marking, which are difficult to remember and distinguish. Using arrays can obtain a large number of variables of the same type at one time. Therefore, you only need to remember the array name and the sequence number of the corresponding array variable to achieve a more efficient implementation. Variable tags.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python introductory skill treePreliminary knowledgeIntroduction to Python 383,250 people are learning the system