What if our models are neither continuous nor discrete? In this chapter we cover hybrid automata. Starting from a continuous setting, we introduce discrete events and look at the issues that arise in making such a transition. We analyze the issues of zero crossing and decidability; mode switching and its effect on derivatives; discrete transitions; and Zeno behavior.

## 1 Introduction

We begin by considering a classic example that builds on concepts that we are familiar with from modeling physical systems. Consider a ball that leaves the ground with a certain speed and at a certain angle, and determine where it lands. In all such high school and college physics questions, every story stops as soon as the ball hits its target. But what happens to the ball after it hits the ground?

A bouncing ball is a system that can be described by two different modes of behavior: falling; and bouncing or resting on the floor. How can we model a system that includes such different modes of behavior?

When the ball is falling we have only the effect of gravity, which has a constant g = 9.8 N/kg. In that case, the ball is governed by the Newtonian law of F = ma (force equals mass times acceleration). We should keep in mind that the net or “total” force on any object is the sum of all the forces applied on the object. This summation applies whether we are working in a one, two, or three dimensions. Since we have

\displaystyle \begin{aligned} \text{F}_{\text{gravity}} = ma, \end{aligned}
(3.1)

then if the only force acting on a mass is gravity, then we also know that

\displaystyle \begin{aligned} gm = ma. \end{aligned}
(3.2)

By dividing both sides by m we obtain a = g. By substituting 9.8 for the value of g, we get (in the dimensionless setting, where “m/s2” is ignored) that a = 9.8.

The position vector x is usually defined as the vector going from the floor level to the position of the ball. In this setting the acceleration goes in the opposite direction. Finally, as a differential equation in relation to the position x, the above equation simply means

\displaystyle \begin{aligned} x'' = - 9.8. \end{aligned}
(3.3)

Now let us turn to modeling the process of bouncing . One solution could be to imagine that, once the ball is in contact with the floor, it experiences another force that we will model as a spring force arising from contact with the ground. The higher the spring coefficient, the faster the bounce. To model this effect, we must return to our original equation and consider the entire situation. The total force on the object must still, as always, be equal to the mass times the acceleration. But the forces acting on the ball are now slightly different:

\displaystyle \begin{aligned} \text{F}_{\text{gravity}} + \text{F}_{\text{floor}} = ma. \end{aligned}
(3.4)

The gravity force and the acceleration are the same as above; but Ffloor will be determined by Hooke’s law, generating a force − kx where we can take k to be a large coefficient. Thus, Equation (3.4) can be expressed as

\displaystyle \begin{aligned} - g m + k( - x)= a m. \end{aligned}

Now, when solving for a we get

\displaystyle \begin{aligned}a = ( - gm - kx)/m.\end{aligned}

Now we notice that we need to specify the coefficient k. Interestingly, at this point we may also need to specify the mass (why did we not need to worry about the precise mass of the ball before?). However, we can avoid the need to specify both separately, because the equation can be further simplified to

\displaystyle \begin{aligned}a\ = \ - gm/m\ - (k/m)x\end{aligned}

which is can be written as

\displaystyle \begin{aligned} a = \ - g - (k/m) x.\end{aligned}

We can simply consider a situation where the ratio between k and m is high (such as 100); then we have all the information that we need for a model of a ball that can undergo simple elastic bouncing when it hits the floor. With this, we can replace the acceleration a with with x″, and km with 100, to get the differential equation:

\displaystyle \begin{aligned}x'' = - 9.8 - 100x.\end{aligned}

We can use Hooke’s law to model Ffloor, i.e., to model the floor as a spring.

Now all that remains is the question of how to specify the switch between being in flight and being in the bouncing state. This can be described as follows:

\displaystyle \begin{aligned} \mbox{if }(x>0) x''= - 9.8\mbox{ else } x'' = - 9.8 - 100 x. \end{aligned}

Once we introduce if-statements into our models we are no longer working with simple differential equations. Rather, we have moved into a world where our models are describing different behaviors when the system is under different modes . We can think of a mode as the domain of validity of certain conditions (such as x ≥ 0 or x ≤ 0 in the above example). More generally, we can choose to define the notion of a mode as an explicitly named state (such as “Falling” or “Bouncing”), which we treat as an explicit part of our model.

Exercise 3.1 Use Acumen to find out the effect of increasing the ratio km. For example, try out the simulation with different values for the spring coefficient to validate the model. What happens when you use 1000, 10,000, or 15,000 instead 100? What happens when you use 25,000 or 50,000?

## 2 Hybrid Automata

Hybrid systems are systems of mathematical equations that combine both continuous and discrete dynamics. Often, they are formalized as a kind of extension to finite state machines called hybrid automata. For our purposes in this book it suffices to have an intuitive understanding of these notions. To get started, to remind ourselves of finite state machines (or finite automata), let us take the example of a simple traffic light.

Example 3.1 Mathematically speaking, we may want to model a traffic light as being, at any point in time, in one of the three states. We can think of a state as simply one of the three constants in the set {Red, Green, Yellow}. Further, we may want to model the behavior of the traffic light machine by a set of rules that determine the allowable ways in which the state of the traffic light can change from one instance to another. Rules in the case of a finite state machine are simply pairs of states written as “s1  ⇒ s2.” The first state is the one we are in and the second state is the one we can go to next. So, a typical traffic light can be modeled with the following rules:

• Rule 1: Red ⇒ Green.

• Rule 2: Green ⇒ Yellow.

• Rule 3: Yellow ⇒ Red.

This is a purely discrete model. In a synchronous model of finite state machines, we can require that transitions only happen at previously determined “clock ticks.”

The notion of a trace of a finite state machine is simply a sequence of allowable state transitions that a finite state machine can undergo. For example, the following sequences are valid traces for the machine we described above: Red ⇒ Green ⇒ Yellow, as well as the trace Green ⇒ Yellow ⇒ Red ⇒ Green. However, the sequence Green ⇒ Yellow ⇒ Green is not a valid trace because we have no rule that allows the transition Yellow ⇒ Green. Similarly, it should also be noted that in this system there are no transitions from one state to itself. So, Red ⇒ Green ⇒ Green is not a valid trace either.

Now let us consider another example.

Example 3.2 Consider another finite state machine that represents whether an air-conditioning system is either cooling or on standby. In this case, we can have two simple states {Cool, Wait}. As you know, an air-conditioning system cools the room for a while, then switches back to the waiting state, then reverts to cooling, and so on. So, the rules for the possible transitions in such a system are quite simple:

• Rule 1: Cool ⇒ Wait.

• Rule 2: Wait ⇒ Cool.

During the cooling process, the system pumps heat out of a room at a constant rate, and the temperature goes down. In addition to the standard finite state machine model of this system, we may also want to build more details into our model. In particular, we might want to specify something about the temperature of the room explicitly; we might also want to specify the rate at which the room is gaining heat from the outside environment, and the rate at which the air-conditioning system is removing this heat when it is in the cooling state. Such information cannot be included in a traditional finite state machine model. However, we can extend the basic model and arrive at what is known as a hybrid automaton. This is where we can add information to each state about some continuous variables that can evolve according to certain rules over time (which itself is modeled as a dense time). For this example, all we have to do is the following: introduce a real-valued variable T to describe the current temperature of the room, and then add the following two rules about what happens to this variable when the system is in each of the two states.

• While in state Cool: T′ = −1/s.

• While in state Wait: T′ = 0.1/s.

In each of the two states there is a dynamical equation describing the rate of change of the temperature. Such equations can be much more involved than the ones considered here, and could, for example, be non-linear. However, what is important here is that this model specifies clearly what happens to the temperature in the room (in terms of the rate at which it changes) when it is in each of these two states. In addition, we may want to go back to our rules and specify how the thermostat (the controller for the air-conditioning unit) determines when it should change from one state to another. We can do this by creating an extended set of transition rules as follows:

• Rule 1: Cool ⇒ Wait is a transition that must occur when T < 20.

• Rule 2: Wait ⇒ Cool is a transition that must occur when T > 25.

Note that we use different temperatures to switch, to avoid alternating too quickly and potentially damaging our air-conditioning system.

Exercise 3.2 Use Acumen’s strings and case-statements to express the example system above as an Acumen model. Simulate the system with a starting value for Temperature as 23.

## 3 Reset Maps

Now we return to our bouncing ball example. In addition to specifying rules for how continuous variables can evolve inside a state, we can also perform a discrete change to a variable as we transition from one state to another. This allows us to perform instantaneous changes in direction without having to use, for example, a spring with a high stiffness coefficient. However it requires a further refinement to our notion of hybrid automata, namely, the introduction of reset maps. This extension simply allows us to specify that, as a transition occurs, we want to reset certain variables in our system to new values that are better suited to model the state of our system. Now we can model our bouncing ball example as follows:

• States = {Flying}.

• While in Flying: x″ = −9.8.

• Transition 1: Flying ⇒ Flying, if x = 0 and x′ < 0 then x′ ⇐ − 0.9 x′.

Note that in this system we only have a single state, “Flying.” The second bullet specifies the dynamics while in this state, namely, falling with a constant, negative acceleration. For this system, one state is actually enough because the work needed to model bouncing can be done in the transition from this state to itself, and in particular, in the reset map applied in the transition from this state to itself. The reset map is what is described by the part that says x′⇐ − 0.9x′, and it means that we want the x′ value (which is the speed of the bouncing ball) to be reset to a new value after the transition. That value will be the result of multiplying the value of x′ before the transition by 0.9. Furthermore, we will switch the sign of the speed, thus changing its direction, so that the ball that was falling down before the transition will be going upwards after the transition. Note that, as a result of the sudden change to the speed that occurs at that transition point, the acceleration will not be a well-defined notion at that instance.Footnote 1

Exercise 3.3 Write out such a model in Acumen, and simulate the system starting from a height of 10 and an initial speed of 0. How well does your model work? Make a note of any unexpected behavior that your simulation might exhibit.

A finite state machine or an automata is deterministic if its behavior is always uniquely defined when it is in any given state. If this is not the case, then the system is said to be non-deterministic. Most simulation tools only simulate deterministic systems.

## 4 Zero-Crossing

Using precise tests (such as x = 0) for transitioning from one state to another is generally quite challenging for simulation tools built on traditional numerical methods. This is because real numbers are generally represented in computers as floating points, and functions that change over time are represented as a sequence of floating point values defined at specific points in time, represented also by floating point values. For our purposes in this book, and to keep things simple, we will try to always express our models with more robust conditions such as x ≤ 0 even though we really do not intend to model the possibility of x being less than 0.

## 5 Zeno Behavior

Zeno’s paradox is a phenomenon that can happen in a hybrid system (a system that exhibits both continuous and discrete dynamic behavior). The hybrid automaton model of the bouncing ball exhibits this phenomenon. In particular, the consecutive bounces form a geometric series that can be shown to end in a finite time (which we call the Zeno point), even if the number of bounces that takes place beyond that point is unbounded.

To understand what is going on here for yourself, consider the situation where the ball is at height zero but is moving up. Calculate the time (based on the initial upward speed) until it hits the ground again. What is that ratio between the speed and the time it takes to hit the ground? Now note that there is a well-defined ratio between the upward speed at the start of this problem, and the upward speed of the ball after the bounce. Convince yourself that this ratio also determines a ratio between the current jump and the next jump. Convince yourself further that this ratio will be the same between any two consecutive jumps. Write out the formula for the time at which the ball will stop jumping, giving some initial parameters.

## 6 Modeling Elastic Collision

Note : While this topic is discussed here only briefly, it is an important example of discontinuity in physical models (and therefore an example of why we need hybrid systems to model physical systems). Also, it is important for understanding the ping pong model.

Collisions in basic mechanics are a simple class of problems involving time. Their characteristic is that there is a discontinuity in one of the derivatives. Consider the situation in Figure 3.1, where we have two co-linear masses that experience a collision where energy and momentum are conserved. How do we determine the speeds after the collision?

Let the velocity of the two balls be u 1 and u 2 before the collision and v 1 and v 2 after the collision, respectively. Conservation of momentum means that

\displaystyle \begin{aligned} m_{1}u_{1} + m_{2}u_{2} = m_{1}v_{1} + m_{2}v_{2}, \end{aligned}
(3.5)

whereas conservation of energy means that

\displaystyle \begin{aligned} \frac{m_{1}u_{1}^2}{2} + \frac{m_{2}u_{2}^2}{2} =\frac{m_{1}v_{1}^2}{2} + \frac{m_{2}v_{2}^2}{2}. \end{aligned}
(3.6)

By using (3.5) and (3.6) together we can solve for v 1 and v 2 in terms of u 1 and u 2. The solution is

\displaystyle \begin{aligned} v_1 & = \frac{(m_1 - m_2)u_1 + 2m_2u_2}{m_1 + m_2}, \\ v_2 & = \frac{(m_2 - m_1)u_2 + 2m_1u_1}{m_1 + m_2}. \end{aligned}

This tells us the speeds for both objects after the collision. When kinetic energy is conserved, we says that the collision is elastic, and inelastic when it is not conserved. The speed of each particle after an inelastic collision can be determined in the same way as above, but with the energy equation modified appropriately. Often, energy loss is specified in terms of a coefficient of restitution, which is the ratio between relative speeds before and after the collision.

The relative speed between two objects is the speed of the first measured from the second and vice versa. If two cars travel in the same direction at 100 and 120 km/h, respectively, the relative speed will be 20 km/h; if the two cars travel at the same speeds but at opposite directions, the relative speed will be 220 km/h.

## 7 Chapter Highlights

1. 1.

Hybrid Systems

1. (a)

Mix continuous and discrete systems

• Area of much of the research in formal analysis and verification of CPS today

2. (b)

Provide a natural model of cyber-physical system

3. (c)

Are also a more natural model of purely physical systems

• Impacts and discontinuities are naturally modeled as discrete events

4. (d)

Cyber systems have not only discrete but also continuous aspects

• Time, energy, time-to-failure, radiation, relativistic effects

2. 2.

Finite State Machines

1. (a)

Traffic light

2. (b)

States: Red, Green, and Yellow

3. (c)

Transitions

• Untimed

• Adding a timer and a reset map

3. 3.

Thermostat Example

1. (a)

States: Heating, Cooling

2. (b)

Equations: Heat equations

3. (c)

Guards: Leave a gap in between

4. 4.

Bouncing Ball Example

1. (a)

States: Falling, and Bouncing?

2. (b)

Equation: Falling

3. (c)

Guards: Hitting Zero (with refinement)

4. (d)

Zeno-behavior?

5. 5.

Computing the Zeno point for a Bouncing Ball

1. (a)

Equation for flight

2. (b)

Time in flight

3. (c)

Flight in terms of max height

4. (d)

Time in terms of coefficient

5. (e)

The limit of a series

## 8 Avoid Common Mistakes

These are remarks intended to help you avoid some common points of confusion:

• A state in a finite state machine has no memory (or additional state) inside it. When you are designing or specifying a finite state machine, if it looks like you need one with “memory,” split it into multiple states.

• A hybrid system is not just a finite state machine. So, for example, the preceding remark does not apply

• Non-deterministic is not the same as probabilistic. Probabilistic systems require much stronger assumptions about frequency in the longer term (probabilities or probability distributions). Non-determinism just means that you do not know exactly what the behavior will be (although you know exactly what set of behaviors is possible).

• Deterministic, non-deterministic, and probabilistic models are all mathematical objects.

## 9 Study Problems

1. 1.

Model the example illustrated in Figure 11 in Branicky’s paper (also mentioned below in the To Probe Further list) in Acumen. Include the following in your completed assignment: (a) your complete Acumen model, (b) the plot figure, and (c) an explanation of why Acumen cannot correctly simulate the system beyond the 4-s point.

2. 2.

Model the two systems illustrated in Figure 15 in Branicky’s paper in Acumen. Assume that f 0(x) = 10 − x and f 1(x) = 40 − x. A complete solution would include: (a) your complete Acumen model, (b) the plot figures (for both systems), and (c) an analysis in your own words of how well this simulation supports the point made in the paper about hysteresis.

3. 3.

Consider the situation where you are designing a futuristic traffic light.

1. (a)

Represent each move by an output of the name of the color (“Red,” “Orange,” “Blue,” or “Green”). Draw a state machine that shows the number of states needed for a traffic light that outputs a signal that carries red, then orange, then red, and then goes to blue, then red, and repeats this process.

2. (b)

Assume that the light stays in the red and green states for 60 s, and in all the other states only for 10 s. Write an Acumen object class My_Light that models this functionality. The only required field in this object is a signal my_choice which can be “R,” “O,” “B,” or “G” depending on the first letter of the color. Make sure that the object is self-contained and do not assume any inputs from the outside.

4. 4.

Compute the Zeno point for the basic bouncing ball example when the ball is dropped from a height of 10 m, taking gravity at 9.8, and a coefficient of restitution of 90%.

5. 5.

Consider the situation where you are designing a paper/rock/scissors player.

1. (a)

Represent each move by an output of the first letter (“P,” “R,” or “S”). Draw a state machine that shows the number of states needed for a simple player that repeatedly outputs a signal that carries paper, then rock, then paper, then scissors, and then repeats this sequence.

em[(b)] Assume that the machine must output each move for 0.75s. Write an Acumen object class My_PRS that models this functionality. The only required field in this object is a signal my_choice which can be “P,” “R,” or “S.” Make sure the object is self-contained, and do not assume any inputs from the outside.

6. 6.

Consider the situation where two cars are about to collide, and their speeds and masses are depicted in Figure 3.2.

1. (a)

Assume a coefficient of restitution of C. Write a formula for w 1 and w 2, the speeds after impact, measured in the same direction.

2. (b)

If both masses are the same (m 1 = m 2) and m 2 is static (v 2 = 0), by what fraction does the speed w 2 increase if the speed v 1 is increased by 10%? If the answer is not a simple fraction, you may write a formula.

3. (c)

If m 1 is half the mass of m 2 and m 2 is static (v 2 = 0), by what fraction does the speed w 2 increase if the speed v 1 is increased by 10%?

7. 7.

Consider Figure 3.3 indicating the “before collision” (above) and “after collision” (below) speeds for a collision between two masses (represented as cars).

1. (a)

Write down the equation for conservation of momentum. This is the equation that relates the “before” and “after” speeds.

2. (b)

Calculate the coefficient of restitution “c”’in this collision.

3. (c)

Assume that m 2  =  100. What is the energy lost in this collision?

4. (d)

Assume that m 2  =  100. How much is m 1?

8. 8.

Consider the diagram in Figure 3.4 depicting the “before” and “after” situation for a collision between two masses. The first object has mass m and the second has twice that mass. The second object is static before collision, and both objects are attached after the collision.

1. (a)

Calculate the coefficient of restitution c in this collision.

2. (b)

Write down the conservation of momentum equation relating “before” and “after” speeds.

3. (c)

What speed must v 1 be in order to have v′ equal to 1000?

4. (d)

Assume that m = 1000. What is the energy lost in the collision of part c?

9. 9.

Consider the diagram in Figure 3.5. This type of diagram is called a state diagram , and is widely used to express finite state machine models in an intuitive manner. According to this diagram, the initial state is S 1 because it has an arrow that has no explicit source, possible transitions are indicated by arrows going between states (sometimes the same state), and whenever there is a transition the digit indicated on the arrow is assigned to the variable Output.

1. (a)

Starting from the initial state, what state is the machine in after it has assigned to Output the values 0, 0, 1, 0 in that order? Write out the state of the machine after each of these four outputs individually.

2. (b)

Assume that the system has an input called Input, and that both states take the 0 transition if Input is less than or equal to 5, and take the 1 transition if Input is greater than five. Assume further that the system makes a transition decisions and actions together, and does so every 1 s. Describe this situation using the Acumen language in one model that has Input and Output as parameters.

## 10 Lab: Discrete Bouncing

The purpose of this lab is to introduce you to hybrid-systems modeling constructs and to connect the ideas discussed in this chapter to project activities.

Taking as a starting point the models you developed for the previous lab, compare and relate those models to the following model sketch: Next, note that bouncing in the original model worked by pretending there is a spring that gets activated when the ball is below ground level. Consider the possibility of creating a model where the bounce occurs instantaneously. Build and test a model that works in this way.

Once you are done with this exercise, compare your model to the following one. The model above has a curious feature. After the ball “reaches zero height,” it starts to slip down below level zero. That is because we are actually allowing gravity to be in effect in the case that x< = 0 and x’ > 0. This creates a cycle of small computational steps where the ball starts below the ground with a positive speed but is subjected to gravity, at the end of the cycle it has a small negative speed and has lost height, and impact condition is detected and the speed is set to positive half again, and the process repeats, but the ball is still losing height.

A better model is the following one, where we ensure that when the ball is below the ground but is on the rise it maintains this speed and is not subject to any additional forces: Compare your model to this one as well, and make note of the points that you found required extra care to be able to model instantaneous bouncing when you tried to build it yourself. While arriving at a simple and clear model of an instantaneously bouncing ball is challenging, it is representative of the frequently occurring situations where a dynamic within a continuous domain reaches a well-defined boundary that it is being pushed against, and where the dynamic of the boundary is to push back the object into the domain.

## 11 Project: Speed-Based Player for Ping Pong Robot

This chapter’s project activity is to develop a ping pong player that can outperform the other player, and at the very least, the default player. In this activity, your model controls the player by sending a speed signal to move the bat. Be careful, because the larger the speed you use, the faster you will run out of energy! Basically, the default player works by computing a bat speed and predicting two key points on the future trajectory of the ball. The first point (p1) is where the ball will hit the table. This is calculated by using the velocity of the ball. The second point (p2) is the highest point in the air that the ball will reach after it bounces on the table. This is calculated from the predicted speed after bouncing. Then, the bat moves to hit the ball at the highest point (pH) of the ball after it has bounced. It is important to note that the bat loses energy (maxE) whenever it moves and hits the ball, and that each player starts with a fixed energy level. Your modified player must consume less energy than the default player and also predict the second point better than the default player. You will need to develop your own strategy to make the bat move less or improve the velocity of the bat.

If you are using this textbook as part of a course, be sure to check with your instructor about whether there is a special edition of the model you are expected to use.

An important skill that good scientists and engineers need in order to complete projects faster and better is debugging. In essence, you need this skill to determine how to go from a system that is not quite doing what you want to one that is. The key to successful debugging is to work systematically to isolate and localize problems. Scientists and engineers that solve real problems seem to apply this skill extensively. Check out this article on this topic.

Designing any interesting system also involves accumulation of a lot of knowledge. Our memory is only one way of collecting knowledge, and it often does not work as well as we think it does. Documenting the result of each of your project activities is a good way to organize the knowledge that you accumulate. Therefore, think of them not only as something you write for the instructor, but also for yourself, and review them constantly during the process. Another very powerful way of accumulating knowledge is test cases. Build your own test cases and use them to automatically test your player as you are developing it. You can develop test cases by making alternative players that play with different strategies.

In future project activities, make sure that your player avoids each of the pitfalls that you discovered during this activity. We recommend that you share the players that you have developed among yourselves, as this will help you improve player development for the next activity, and increase your chances of winning at the finals.