Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Embedded Robotics (Thomas Braunl, 2 ed, 2006)

.pdf
Скачиваний:
251
Добавлен:
12.08.2013
Размер:
5.37 Mб
Скачать

21 Genetic Programming

+

*

 

2

 

 

 

+

 

 

 

 

 

 

 

 

 

 

42

 

 

 

 

 

 

 

 

 

8

5

40

2

 

 

 

 

 

 

 

 

 

 

 

Figure 21.1: Tree structure and evaluation of S-expression

We deal only with integer data values. Our Lisp subset contains pre-defined constants zero, low, and high, and allows the generation of other integer constants by using the function (INC v). Information from the robot’s vision sensor can be obtained by calling obj_size or obj_pos. An evaluation of any of these two atoms will implicitly grab a new image from the camera and then call the color object detection procedure.

There are four atoms psd_aaa for measuring the distance between the robot and the nearest obstacle to the left, right, front, and back. Evaluating any of these atoms activates a measurement of the corresponding PSD (position sensitive device) sensor. These sensors are very useful for obstacle avoidance, wall-following, detecting other robots, etc.

There are four movement atoms remaining. Two for driving (forward and backward), and two for turning (left and right). When one of these is evaluated, the robot will drive (or turn, respectively) by a small fixed amount.

Finally, there are three program constructs for selection, iteration, and sequence. An “if-then-else” S-expression allows branching. Since we do not provide explicit relations, for example like (< 3 7), the comparison operator “less” is a fixed part of the if-statement. The S-expression contains two integer values for the comparison, and two statements for the “then” and “else” branch. Similarly, the while-loop has a fixed “less” comparison operator as loop condition. The iteration continues while the first integer value is less than the second. The two integer arguments are followed by the iteration statement itself.

These are all constructs, atoms, and S-expression lists allowed in our Lisp subset. Although more constructs might facilitate programming, it might make evolution more complex and would therefore require much more time to evolve useful solutions.

Although Lisp is an untyped language, we are only interested in valid S- expressions. Our S-expressions have placeholders for either integer values or statements. So during genetic processing, only integers may be put into integer slots and only statements may be put into statement slots. For example, the first two entries following the keyword in a WHILE_LESS-list must be integer values, but the third entry must be a statement. An integer value can be either

310

Lisp

value

statement

Name

Kind

Semantics

 

 

 

 

 

 

zero

atom, int, constant

0

 

 

 

low

atom, int, constant

20

 

 

 

high

atom, int, constant

40

 

 

 

(INC v)

list, int, function

Increment

 

 

v+1

obj_size

atom, int,

search image for color object,

 

image sensor

return height in pixels (0..60)

 

 

 

obj_pos

atom, int,

search image for color object,

 

image sensor

return x-position in pixels (0..80)

 

 

or return –1 if not found

 

 

 

psd_left

atom, int,

measure distance in mm to left

 

distance sensor

(0..999)

 

 

 

psd_right

atom, int,

measure distance in mm to right

 

distance sensor

(0..999)

 

 

 

psd_front

atom, int,

measure distance in mm to front

 

distance sensor

(0..999)

 

 

 

psd_back

atom, int,

measure distance in mm to back

 

distance sensor

(0..999)

 

 

 

turn_left

atom, statem., act.

rotate robot 10° to the left

 

 

 

turn_right

atom, statem., act.

rotate robot 10° to the right

 

 

 

drive_straight

atom, statem., act.

drive robot 10cm forward

 

 

 

drive_back

atom, statem., act.

drive robot 10cm backward

(IF_LESS

list, statement,

Selection

v1 v2 s1 s2)

program construct

if (v1<v2) s1; else s2;

(WHILE_LESS

list, statement,

Iteration

v1 v2 s)

program construct

while (v1<v2) s;

(PROGN2

list, statement,

Sequence

s1 s2)

program construct

s1; s2;

Table 21.2: Lisp subset for genetic programming

an atom or an S-expression, for example low or INC(zero). In the same way, a statement can be either an atom or an S-expression, for example

drive_straight or PROGN2(turn_left, drive_back).

311

21 Genetic Programming

We implemented the Lisp interpreter as a recursive C program (Lisp purists would have implemented it in Lisp) that is executed by the EyeSim simulator. Program 21.1 shows an extract of the main Lisp decoding routine.

Program 21.1: Lisp interpreter in C

1int compute(Node n)

2{ int ret, return_val1, return_val2;

3...

4CAMGetColFrame (&img, 0);

5if (DEBUG) LCDPutColorGraphic(&img);

6ret = -1; /* no return value */

7

8switch(n->symbol) {

9case PROGN2:

10compute(n->children[0]);

11compute(n->children[1]);

12break;

13

14case IF_LESS:

15return_val1 = compute(n->children[0]);

16return_val2 = compute(n->children[1]);

17if (return_val1 <= return_val2) compute(n->children[2]);

18

else

compute(n->children[3]);

19

break;

 

20

 

 

21case WHILE_LESS:

22do {

23return_val1 = compute(n->children[0]);

24return_val2 = compute(n->children[1]);

25if (return_val1 <= return_val2) compute(n->children[2]);

26} while (return_val1 <= return_val2);

27break;

28

29case turn_left: turn_left(&vwhandle);

30break;

31case turn_right: turn_right(&vwhandle);

32break;

33...

34case obj_size: ColSearch2 (img, RED_HUE, 10, &pos, &ret);

35break;

36case obj_pos: ColSearch2 (img, RED_HUE, 10, &ret, &val);

37break;

38case low: ret = LOW;

39break;

40case high: ret = HIGH;

41break;

42default: printf("ERROR in compute\n");

43exit(1);

44}

45return ret;

46}

312

Genetic Operators

21.3 Genetic Operators

Similar to the genetic algorithm operators in Chapter 20, we have crossover and mutation. However, here they are applied directly to a Lisp program.

Crossover Crossover (sexual recombination) operation for genetic programming recreates the diversity in the evolved population by combining program parts from two individuals:

1.Select two parent individuals from the current generation based on their fitness values.

2.Randomly determine a crossover point in each of the two parents. Both crossover points must match, i.e. they must both represent either a value or a statement.

3.Create the first offspring by using parent no. 1, replacing the sub-tree under its crossover point by the sub-tree under the crossover point from parent no. 2. Create the second offspring the same way, starting with parent no. 2.

Since we require the selected crossover points to match type, we have guaranteed that the two generated offspring programs will be valid and executable.

Crossover points can be external (a leaf node, i.e. replacing an atom) or internal (an internal tree node, i.e. replacing a function). External points may extend the program structure by increasing its depth. This occurs when one parent has selected an external point, and the other has selected an internal point for crossing over. An internal point represents a possibly substantial alteration of the program structure and therefore maintains the variety within the population.

 

if

 

 

 

 

 

 

if

 

 

obj_

<

 

 

 

 

 

obj_

<

 

right

20

left

right

 

 

 

20

prog

pos

 

 

 

pos

 

 

 

 

while

 

 

 

 

 

 

 

right

strai.

 

 

 

 

 

 

 

 

 

 

 

 

<

 

 

 

 

 

 

 

 

 

 

psd

 

 

 

 

 

 

 

 

while

 

 

20

prog

 

 

 

 

 

 

 

front

 

 

 

 

psd

<

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

right

strai.

 

20

left

 

 

 

 

front

 

Figure 21.2: Crossover

313

21 Genetic Programming

The following shows an example with the crossover point marked in bold face:

1.(IF_LESS obj_pos low turn_left turn_right)

2.(WHILE_LESS psd_front (PROGN2 turn_right drive_straight))

1.(IF_LESS obj_pos low (PROGN2 turn_right drive_straight) turn_right)

2.(WHILE_LESS psd_front turn_left)

Both selected crossover points represent statements. The statement turnleft in parent no. 1 is replaced by the PROGN2-statement of parent no. 2, whereas the PROGN2-statement of parent no. 2 is replaced by statement turn_left as the new child program. Figure 21.2 presents the crossover operator graphically for this example.

Mutation The mutation operation introduces a random change into an individual and thereby introduces diversity into the new individual and the next generation in general. While mutation is considered essential by some [Blickle, Thiele 1995], others believe it to be almost redundant [Koza 1992]. Mutation works as follows:

1.Select one parent from the current generation.

2.Select a mutation point.

3.Delete sub-tree at mutation point.

4.Replace sub-tree with randomly generated sub-tree.

 

if

 

 

 

 

 

if

 

 

 

obj_

<

 

 

 

<

 

 

 

20

prog

right

 

 

 

obj_

while

right

pos

 

 

 

pos 20

 

 

 

 

 

 

<

 

 

 

 

 

 

 

 

 

 

 

right

strai.

 

 

obj_

 

 

 

 

 

 

20

strai.

 

 

 

 

 

 

 

size

 

 

 

 

 

 

 

 

 

 

Figure 21.3: Mutation

The following shows an example with the mutation point marked in bold face:

(IF_LESS obj_pos low

(PROGN2 drive_straight drive-straight) turn_right)

(IF_LESS obj_pos low

(WHILE_LESS psd_front high drive_straight) turn_right)

314

Evolution

The selected sub-tree (PROG2N2-sequence) is deleted from the parent program and subsequently replaced by a randomly generated sub-tree (here: a WHILE-loop construct containing a drive_straight statement). Figure 21.3 presents the mutation operator graphically.

21.4 Evolution

Initial population

Evaluation and fitness

To start the evolutionary process, we first need an initial population. This consists of randomly generated individuals, which are random Lisp programs of limited tree depth. A large diversity of individuals improves the chances of locating the optimum solution after a set number of generations. [Koza 1992] suggests a number of methods to ensure a large diversity of different sizes and shapes in the initial population: full method, grow method, and ramped half- and-half (see below).

To ensure the validity and termination of each individual, the randomly generated Lisp programs must be sound and the root node must be a statement. All leaf nodes at the desired depth must be atoms.

A random program is initialized with a random statement for the root node. In case it is a function, the process continues recursively for all arguments until the maximum allowed depth is reached. For the leaf nodes, only atoms may be selected in the random process.

The “full method” requires the generated random tree to be fully balanced. That is, all leaves are at the same level, so there are no atoms at the inner level of the tree. This method ensures that the random program will be of the maximum allowed size.

The “grow method” allows the randomly generated trees to be of different shapes and heights. However, a maximum tree height is enforced.

The “ramped half-and-half method” is an often used mix between the grow method and the full method. It generates an equal number of grow trees and full trees of different heights and thereby increases variety in the starting generation. This method generates an equal number of trees of height 1, 2, ..., up to the allowed maximum height. For each height, half of the generated trees are constructed with the “full method” and half with the “grow method”.

The initial population should be checked for duplicates, which have a rather high probability if the number of statements and values is limited. Duplicates should be removed, since they reduce the overall diversity of the population if they are allowed to propagate.

Each individual (Lisp program) is now executed on the EyeSim simulator for a limited number of program steps. Depending on the nature of the problem, the program’s performance is rated either continually or after it terminates (before or at the maximum allowed number of time steps). For example, the fitness of a wall-following program needs to be constantly monitored during execution, since the quality of the program is determined by the robot’s distance to the wall at each time step. A search problem, on the other hand, only needs to check at the end of a program execution whether the robot has come

315

21 Genetic Programming

sufficiently close to the desired location. In this case, the elapsed simulation time for achieving the goal will also be part of the fitness function.

Selection After evaluating all the individuals of a population, we need to perform a selection process based on the fitness values assigned to them. The selection process identifies parents for generating the next generation with the help of previously described genetic operators. Selection plays a major role in genetic programming, since the diversity of the population is dependent on the choice of the selection scheme.

Fitness proportionate. The traditional genetic programming/genetic algorithm model selects individuals in the population according to their fitness value relative to the average of the whole population. However, this simple selection scheme has severe selection pressure that may lead to premature convergence. For example, during the initial population, an individual with the best fitness in the generation will be heavily selected, thus reducing the diversity of the population.

Tournament selection. This model selects n (e.g. two) individuals from the population and the best will be selected for propagation. The process is repeated until the number of individuals for the next generation is reached.

Linear rank selection. In this method, individuals are sorted according to their raw fitness. A new fitness value is then assigned to the individuals according to their rank. The ranks of individuals range from 1 to N. Now the selection process is identical to the proportionate schema. The advantage of the linear rank selection is that small differences between individuals are exploited and, by doing so, the diversity of the population is maintained.

Truncation selection. In this model, the population is first sorted according to its fitness values, and then from a certain point fitness value F, the poorer performing individuals below this value are cut off, only the better performing individuals remain eligible. Selection among these is now purely random; all remaining individuals have the same selection probability.

21.5 Tracking Problem

We chose a fairly simple problem to test our genetic programming engine, which can later be extended to a more complex scenario. A single robot is placed at a random position and orientation in a rectangular driving area enclosed by walls. A colored ball is also placed at a random position. Using its camera, the robot has to detect the ball, drive toward it, and stop close to it. The robot’s camera is positioned at an angle so the robot can see the wall ahead from any position in the field; however, note that the ball will not always be visible.

Before we consider evolving a tracking behavior, we thoroughly analyze the problem by implementing a hand-coded solution. Our idea for solving this problem is shown below.

316

Tracking Problem

In a loop, grab an image and analyze it as follows:

Convert the image from RGB to HSV.

Use the histogram ball detection routine from Section 17.6

(this returns a ball position in the range [0..79] (left .. right) or no ball, and a ball size in pixels [0..60]).

If the ball height is 20 pixels or more, then stop and terminate (the robot is then close enough to the ball).

Otherwise:

if no ball is detected or the ball position is less than 20, turn slowly left.

if the ball position is between 20 and 40, drive slowly straight.

if the ball position is greater than 40, turn slowly right.

We experimentally confirm that this straightforward algorithm solves the problem sufficiently. Program 21.2 shows the main routine, implementing the algorithm described above. ColSearch returns the x-position of the ball (or –1 if not detected) and the ball height in pixels. The statement VWDriveWait following either a VWDriveTurn or a VWDriveStraight command suspends execution until driving or rotation of the requested distance or angle has finished.

Program 21.2: Hand-coded tracking program in C

1

do

 

2

{ CAMGetColFrame(&c,0);

 

3

ColSearch(c, BALLHUE, 10, &pos, &val); /* search image */

4

if (val < 20) /* otherwise FINISHED */

5

6

{ if (pos == -1 || pos < 20) VWDriveTurn(vw, 0.10, 0.4);/* left */

7

else if (pos > 60)

VWDriveTurn(vw, -0.10, 0.4);/* right*/

8

else

VWDriveStraight(vw, 0.05, 0.1);

9

VWDriveWait(vw); /* finish motion */

10

}

 

11

} while (val < 20);

 

 

 

 

Program 21.3: Hand-coded tracking program in Lisp

1( WHILE_LESS obj_size low

2(IF_LESS obj_pos low rotate_left

3(IF_LESS obj_pos high drive_straight

4rotate_right )))

The next task is to hand-code the same solution in our Lisp notation. Program 21.3 shows the implementation as a Lisp string. This short program uses the following components:

Constants: low (20), high (40)

317

21 Genetic Programming

Sensor input: obj_size (0..60), obj_pos (–1, 0..79)

sensor values are evaluated from the image in each step

Constructs: (WHILE_LESS a b c)

C equivalent: while (a<b) c;

(IF_LESS a b c d)

C equivalent: if (a<b) c; else d;

Bearing in mind that obj_size and obj_pos are in fact calls to the image processing subroutine, the procedural translation of the Lisp program in Program 21.3 is almost identical to the hand-coded C solution in Program 21.2:

while (obj_size<20)

if (obj_pos<20) rotate_left;

else if (obj_pos<40) drive_straight; else rotate_right;

Figure 21.4 shows a graphical representation of the program tree structure. Of course the choice of the available constants and program constructs simplifies finding a solution and therefore also simplifies the evolution discussed below. Figure 21.5 and Figure 21.6 show the execution of the hand-coded solution from several different starting directions.

 

while

 

 

 

<

 

 

obj_

 

 

if

 

20

 

size

<

 

 

obj_

20 left

if

 

pos

 

<

obj_

pos 40 strai. right

Figure 21.4: Lisp program tree structure

Figure 21.5: Execution of hand-coded solution in EyeSim simulator

318

Evolution of Tracking Behavior

0.02

 

0

 

 

 

 

 

 

 

 

-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

-0.02

-0.04

-0.06

-0.08

-0.1

-0.12

-0.14

-0.16

Figure 21.6: Robot’s view and driving path in EyeSim simulator

21.6 Evolution of Tracking Behavior

Koza suggests the following steps for setting up a genetic programming system for a given problem [Koza 1992]:

1.Establish an objective.

2.Identify the terminals and functions used in the inductive programs.

3.Establish the selection scheme and its evolutionary operations.

4.Finalize the number of fitness cases.

5.Determine fitness function and hence the range of raw fitness values.

6.Establish the generation gap G, and the population M.

7.Finalize all control parameters.

8.Execute the genetic paradigm.

Our objective is to evolve a Lisp program that lets the robot detect a colored ball using image processing, drive toward it, and stop when it is close to it. Terminals are all statements that are also atoms, so in our case these are the four driving/turning routines from Table 21.2. Functions are all statements that are also lists, so in our case these are the three control structures sequence (PROGN2), selection (IF_LESS), and iteration (WHILE_LESS).

In addition, we are using a set of values, which comprises constants and implicit calls to image processing functions (obj_pos, obj_size) and to distance sensors (psd_aaa). Since we are not using PSD sensors for this experiment, we take these values out of the selection process. Table 21.3 shows all parameter choices for the experimental setup.

Figure 21.7 demonstrates the execution sequence for the evaluation procedure. The genetic programming engine generates a new generation of Lisp programs from the current generation. Each individual Lisp program is interpreted by the Lisp parser and run on the EyeSim simulator four times in order to determine its fitness. These fitness values are then in turn used as selection criteria for the genetic programming engine [Hwang 2002].

319