4. Models and Objects

Part of CS:2820 Object Oriented Software Development Notes, Spring 2021
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science


A model is an abstraction of some aspect of reality. So, for example, we can describe a flood control reservoir such as the Coralville reservoir by a set of variables and functions:

  • inflow(t), the inflow at time t in volume per unit time.
  • outflow(t), the outflow at time t in volume per unit time.
  • volume(t), the reservoir volume at time t.
  • level(t), the reservoir level at time t, an altitude.
  • gate-setting(t), the altitude of the edge of the sluice gate at time t.
  • f1(v) gives the level as a function of volume v.
  • f2(h) gives the outflow as a function if h, the height of the reservoir over the sluice gate.
  • The model connects these variables and functions to describe the behavior of the reservoir:

  • volume(t) = volume(t – 1) + inflow(t) – outflow(t)
  • level(t) = f1( volume(t) )
  • outflow(t) = f2( level(t) – gate-setting(t) )
  • This model is an oversimplification. It ignores groundwater, it ignores bank erosion, it ignores evaporation. All real models are oversimplifications.

    The U.S. Army Corps of Engineers in Rock Island spends quite a bit of effort on such models, they are essential to management of the many dams and flood control reservoirs that they operate. They use stream gauging stations, rainfall data and weather forecasts, plus the predictions for upstream reservoirs in order to get inflow data, and then they use this to test different gate setting strategies in order to minimize flood damage and maximize navigation on the Mississippi River.

    We will focus on that kind of model; it is what is called a continuous model because the system being modeled is a continuous system. That is, variables and functions have real values, and the functions are all continuous functions. Continuous systems are best described by differential equations. Take a numerical analysis course if you want to learn more about that kind of modeling. Here, we will focus on discrete event models.

    For a second continuous example, consider a model of a satellite in near Earth orbit. This is a problem in orbital dynamics. For short-term dynamics, we can ignore the effects of the Moon, the Sun and other planets, and we can ignore the rotation of the Earth, treating both the Earth and Moon as point masses. The variables that interest us are:

  • t, the time
  • r(t), the center to center distance from earth to satellite, as a function of t
  • v(t), the velocity of the satellite as a function of time.
  • a(t), the acceleration of the satellite as a function of time.
  • f(t), the force on the satellite as a function of time.
  • Some constants matter:

  • G, the gravitational constant
  • me, the mass of the earth
  • ms, the mass of the satellite
  • Δt, the time step
  • All of the above are 3 dimensional vectors, with x y and z components. The basic rules are the rules of physics:

  • f(t) = Gmems / |r(t)|2
  • a(t) = f(t) / ms
  • v(t + Δt) = v(t) + a(t)Δt
  • r(t + Δt) = r(t) + v(t)Δt

    Given an initial position and velocity, this model predicts the future behavior of the satellite. Smaller values of Δt and higher precision arithmetic lead to more precise predictions. Numerical analysis can be used to derive far more accurate models from this framework.

    Discrete Event Models:

    Our modern ideas of object-oriented programming originated in a Norwegian research group that was heavily ivolved in discrete event models and discrete event simulation. Here are some examples of systems that illustrate this kind of model:

    Example 1: A Highway Network

    Consider modelling a highway network. A model of a highway network will typically have:

    Roads connect intersections, and they have attributes such as length and, travel-time. In a simple model that ignores congestion, we can ignore the possibility that the road also serves as a parking lot during rush hour.

    Intersections tie together roads, intersections may only be occupied by one vehicle at a time, and they may have one of several different control algorithms: Uncontrolled, traffic circle, stop-signs, or traffic light. The control algorithm determines how vehicles pass through the intersection from road to road.

    Vehicles may either be running on a road or waiting at an intersection. Each vehicle has a plan that names a series of roads that it intends to follow to get from its current position to its destination. Vehicles may also carry cargo and passengers.

    In an object-oriented model, we can consider each of the above to be the name of a class, and each attribute of the objects in a class can be considered to be a field of that class. Some of these classes are more complex than others. Intersections have subclasses depending on the control algorithm. Vehicles may have subclasses that depend on the type (taxis and dump trucks have very different behavior).

    The events that interest us constitute another class. The following events matter in this highway network:

    Vehicle enters road
    When a vehicle enters a road, it will arrive at the intersection at the other end at a time determined by the road and (in more complex models) by the number of other vehicles on that road.

    Vehicle arrives at intersection
    When a vehicle arrives at an intersection, it will enter one of the roads leaving that intersection at a later time determined by the rules for that intersection.

    The primary attribute of every event is the time at which it occurs. In a discrete event model, each event occurs at a specific instant of time. Any process that takes time is considered to occur as a sequence of events spanning that time, where each event in that sequence marks a points where the state of the model must be checked to determine when the next event in that sequence occurs.

    Of course, if your model is being used in a context where there is a graphic display of the progress of the model, each vehicle, road and intersection will need to have display coordinates associated with it, but simulation visualization only matters after the underlying model is completed, and here, our focus is on the model.

    Example 2: Digital Logic

    As a second example consider digital logic. Here, the major classes were:

    Logic gates
    A logic gate is a component that computes some logical operation such as and, nand, or, nor, or not. These should be familiar Boolean operators, although you may not be used to thinking of them as physical objects. Aside from the logical function computed by each gate, the attributes of a gate are the wires to which it connects, the current value of the output of that gate, and the time delay of that gate, from a change in one of its inputs to a change in its output.

    Wires may be attached to the output of any logic gate to carry the value output by that gate to the inputs of zero or more other gates. The attributes of a wire are the logical value it is currently presenting to its output(s) and the time delay the wire introduces from its input to its output(s).

    Of course, if your goal is to manipulate digital logic circuits on a display screen, each gate and wire must know where it is on the display.

    Events, in a digital logic circuit, represent changes in the value of the output of a gate or the output of a wire. As in the traffic example, these output state changes are considered to be instantaneous, and for boolean logic we use only two values, true and false. (Not all logic is boolean; in the 1960's, the Russians built a run 50 ternary mainframe computers with the three values true, unknown and false.)

    It turns out that the model just presented is just a bit too simplistic: To realistically simulate digital logic circuits containing feedback loops, you need the time delay of gates to have a small random component. Without this, as it turns out, metastable states of the feedback loops do not behave realistically. While you have no need to know this about logic circuitry, the refinement of a model to incorporate such details is very typical of real-world modelling.

    Example 3: Neural Networks

    A third example, neural networks, model the behavior of neurons in the brains of animals, including people. Note that the term neural network has another meaning, referring to classification algorithms inspired by biological neural networks. Here, we are talking about models of actual nervous systems, not artificial intelligence algorithms. The components that matter in such a model are:

    A neuron is a type of cell that fires when its membrane potential exceeds some threshold. In the absence of external stimulii, the membrane potential decreases exponentially with a fixed half-life. There are two types of external stimulii, exitatory and inhibitory. Exitatory stimulii add a fixed increment to the membrane potential. Inhibitory stimulii subtract a fixed increment (or alternatively, they increase the threshold, which reverts exponentially to its default value).

    An axon is a part of a neuron that connects the cell body with remote connections to other neurons. The only characteristic we care about is the delay from when the cell body fires to when the synapses on that axon transmit their inhibitory or excitatory inputs to other neurons. Each synapse may have a different delay.

    A synapse may be either inhibitory or excitatory, and in either case, it has a strength, the amount by which it changes the membrane potential of the neuron to which it connects. The difference between inhibition and excitation can be modeled by the sign of the strength, positive for excitation, negative for inhibition.

    This is vastly oversimplified, but it is still a useful approximation for how neural networks work. This model does not cover learning, which is believed to involve modification to the synapses, and it does not cover exhaustion of the neurotransmitters when a synapse fires too frequently. Again, adding such detail is typical of how models are refined.

    Example 4: Epidemic dynamics

    A model of an epidemic ignores many details of human behavior, viewing people only as being subject to infection. The goal of the model is to study the spread of infection through the community, so the model needs to track people's contact patterns. The basic components that matter in such a model are:

    People move from place to place. When multiple people share the same place, if one of them is infected, the others have a probability of infection that depends on the place and how long they are together there. People have the following states, uninfected and vulnerable, latent, infectious, bedridden, recovered, and dead. When a person is infected, they become latent for a while, then they become infectious, while still going about their business, and then some fraction become bedridden, while others recover. Among the bedridden, some recover, and some die. When a person is not bedridden or dead, they move from place to place in patterns that depend on their class. Classes of people include students, workers, and homemakers.

    People move from their homes to schools, stores, and workplaces. Schools and stores are types of workplaces because some people go there for work. Schools are also frequented by students, and stores are also frequented by customers. Homemakers, workers and students have different patterns of visiting stores.

    This model can be made arbitrarily complex, giving it more and more detail, but the goal is to ask, if one person in the community becomes infected, how does the infection spread through the community, how many are bedridden at any time (a measure of Hospital demand), and how many die as a function of time.

    Once the basic model works, you can do things like examine the impact of policy changes such as closing schools when the infection rate crosses some threshold. With a slightly more complex model, subdividing workplaces between essential and non-essential, you can examine the impact of closing some categoryof non-essential businesses.