Page 176 - Introduction to AI Robotics
P. 176

159
                                      5.2 Behaviors as Objects in OOP
                                      It is better software engineering to write a general move to goal behavior,
                                      where only what is the goal—a red region or a blue region—varies. The goal
                                      for the current instance can be passed in at instantiation through the object
                                      constructor.
                                        Writing a single generic behavior for move_to_goal(color) is more
                                      desirable than writing a move_to_red and a move_to_blue behaviors.
                                      From a software engineering perspective, writing two behaviors which do
                                      the same thing is an opportunity to introduce a programming bug in one
                                      of them and not notice because they are supposed to be the same. Generic
                                      behaviors also share the same philosophy as factoring in mathematics. Con-
                                                                    2
                                      sider simplifying the equation 45 x +  x +  . The first step is to fact 9or out  0  5
                                                                                                       4
                                      any common term to simplify the equation. In this case, 45 can be factored
                                      and the equation rewritten as 45(  x +  2 . The color of the goal, red or 1blue,  )
                                      was like the common coefficient of 45; it is important, but tends to hide that
                                      the key to the solution was the move-to-goal part, or x.
                                        Modular, generic code can be handled nicely by schemas as shown in
                                      Fig. 5.2. The behavior move_to_goal would consist of a perceptual schema,
                                      which will be called extract-goal, and a motor schema, which uses an
                                      attractive field called pfields.attraction. extract-goal uses the af-
                                      fordance of color to extract where the goal is in the image, and then computes
                                      the angle to the center of the colored region and the size of the region. This
                         AFFORDANCE   information forms the percept of the goal; the affordance of the Coke can is
                                      the color, while the information extracted from the perception is the angle
                                      and size. The attraction motor schema takes that percept and is responsible
                                      for using it to turn the robot to center on the region and move forward. It
                                      can do this easily by using an attractive field, where the larger the region, the
                                      stronger the attraction and the faster the robot moves.
                                        The move_to_goal behavior can be implemented as a primitive behavior,
                                      where goal_color is a numerical means of representing different colors
                                      such as red and blue:

                                         move_to_goal(goal_color):
                                         Object  Behavioral Analog  Identifier
                                         Data    percept          goal_angle
                                                                  goal_strength
                                         Methods  perceptual_schema  extract_goal(goal_color)
                                                 motor_schema     pfields.attraction(goal_angle, goal_strength)


                                        The above table implies some very important points about programming
                                      with behaviors:
   171   172   173   174   175   176   177   178   179   180   181