# Homework 2 - Coding Robot Behaviors

## Due Friday, February 8, 2002

NOTE: This assignment, like others in this class, is due at the beginning of the class period. This means that if you are even a minute late, you lose 20%. If you are worried about potentially being late, turn in your homework ahead of time. Do this by submitting them to me during office hours or by sliding it under my office door. Do not send assignments to me through email or leave them in my departmental mail box.

While the textbook for this course is excellent in some respects, there are also some flaws. One flaw is that the code given sometimes doesn't match the text that describes it. We'll treat this bug as a feature, however, and make homework assignments out of correcting the textbook.

## The assignment.

Examine the code in Chapter 3 of the textbook (this is variously described as C code and pseudo-code) and read the accompanying text.

Note that the mistakes describing the code begin with the very first example. On page 78, the text claims that, for the code shown there, "Each execution through the loop would cause the agent to move for one second, then the loop would repeat." This is in conflict with the latter statement on that same page that "[i]f the agent does not sense the releaser for the behavior, the agent does nothing." Most likely what is meant is that if the predator is sensed, then the agent will flee for one second, otherwise the agent will not move on that pass through the loop, after which the loop will repeat.

Okay, I've corrected the first mistake and I hope you get the idea of how to carefully read the text and examine the code to find and correct problems. Now it is your turn.

1. Examine the code that starts on page 79 and carries over onto page 80.

1. Find the undeclared variables and correct them.
2. Explain how the code conflicts with the statement on page 79 that it "shows what happens in a sequence of behaviors, where the agent eats, then nurses its young, then sleeps, and repeats the sequence." Correct this statement to match the code.
3. Explain how the code conflicts with the statement on page 79 that "[i]f the behavior isn't finished, the releasers remain unchanged and no other behavior is triggered. Correct the code to match this statement.
4. Explain how the code conflicts with the statement on page 80 that "[i]f the agent sleeps and wakes up, but isn't hungry ... the agent will just sit there until it gets hungry." Correct this statement to match the code.

2. Examine the code that starts on page 81 and carries over onto page 82.

1. Find the undeclared variable and correct it. (Note that is same variable was undeclared in the code found entirely on page 80.)
2. On page 82 there is a statement that in this code "each behavior takes control and runs to completion." (This would only be true if the functions called were modified.) This is a problem, claims the author, because "the agent cannot react to a predator until it has finished the sequence of behaviors." Would her proposed solution that "[c]alls to the fleeing behavior could be inserted between each behavior" really make things better? Explain your answer. Would her proposed solution that "fleeing could be processed on an interrupt basis" really make things better? Explain your answer.
3. The author claims that starting with an explicit sequence of behaviors then adding fixes, such as processing interrupts, make "the program less general purpose and harder to add [to] and maintain." Comment on this statement, in light of the difficulty the author had in matching code and text for the implicit sequence version that you corrected above.

## What to turn in.

Turn in a typed copy of your answers to these questions. In total, your answers should run from 1.5 to 2 pages in length (roughly 80 characters per line, 50 lines per page).