Study Guide

Modelling the Real World

How to design, use and evaluate a model of something real: deciding what to keep and what to leave out, describing a system by its states and the events that change them, running a simple simulation of a canteen queue, and judging where a model stops being reliable.

Lesson 5.20 Algorithms and Programming

For: Key Stage 3 learners (about ages 12 to 14), and anyone starting to think about how programs represent real systems

Stage: Think

A map that is not a map

A fictional town’s bus map shows each route as a coloured line, with the stops as dots along it. The lines are drawn straight or at gentle angles, the dots are evenly spaced, and there are no streets, no hills and no distances. Yet people find their way round the town with it every day.

Think it through

What does the bus map leave out? Why is it still useful, and for which questions would it be no use at all?

Show a model answer

It leaves out the real shape of the streets, the real distances between stops and anything that is not a bus route. That is why it is easy to read: the passenger sees only the stops and the connections. It is useful for “which route, and where do I change?” and no use for “how long is the walk between these stops?”, because that detail was left out.

Stage: Discover

What a model keeps, and why

A fictional school canteen could be represented in different ways: a floor plan that shows where the tills and tables are, a price list that shows each item and its cost, or a count of how many pupils arrive and are served each minute.

Think it through

The headteacher asks: “How long will the queue at the canteen get at lunchtime?” Which of the three representations would you use, and what from the other two would you not need?

Show a model answer

The count of pupils arriving and being served each minute, because the question is about how the queue grows and shrinks over time. The floor plan (where the tables are) and the price list (what each item costs) are not needed to answer it.

A model is a simplified representation of something real, made to answer particular questions. It is made by abstraction: keeping the details that matter to the question and leaving out the rest. The same canteen has a different model for each question, and no model keeps everything, because a model that kept everything would be as complicated as the canteen.

Check your understanding

A model is built to answer “how long will the queue get?”. Which detail does the model need?
Show the answer

Answer: C. Pupils arriving each minute, and how many the till serves.

The question decides what the model keeps. A different question, such as “what will lunch cost?”, would keep different details.

Stage: Predict

A crossing’s lights

A fictional pedestrian crossing has traffic lights for cars and a signal for people. Cars normally have a green light. When a person presses the button, the lights change in a fixed order to stop the cars so that people can cross, and then go back.

Think it through

List the different situations the crossing can be in, such as which lights are showing, and say what makes it change from one situation to the next. Try to find four situations and two things that make the crossing change.

Show a model answer

The four situations (simplified for this lesson) are: cars have a green light (Cars go); cars have an amber light (Cars slow); cars have a red light and the green man shows (People cross); cars still have red and the green man flashes (People hurry). Two things make the crossing change: a person pressing the button, and a timer running out.

Each situation the system can be in is a state: a complete description of how things are now, which is all the model needs to remember to know what can happen next. Something that happens, such as the button being pressed, is an event. A change from one state to another, caused by an event, is a state transition.

The crossing’s model has four states, Cars go, Cars slow, People cross and People hurry, and two events, button pressed and timer ends. Its rules are:

  • When the state is Cars go, pressing the button moves to Cars slow. The timer does nothing.
  • Cars slow, People cross and People hurry each move on to the next state when the timer ends, in that order. After People hurry the state goes back to Cars go.
  • In every state except Cars go, pressing the button changes nothing.

Check your understanding

Which of these is an event, not a state, in the crossing’s model?
Show the answer

Answer: A. The button is pressed.

A state is how things are; an event is something that happens.

Stage: Experiment

Writing the model as a table

A table with a row for each state and event, and the state that follows, describes the whole behaviour without any code. Use the rules above to fill in the next state for each row. The first is given.

The crossing: state, event and next state
State nowEventNext state
Cars gobutton pressedCars slow
Cars gotimer ends
Cars slowtimer ends
Cars slowbutton pressed
People crosstimer ends
People crossbutton pressed
People hurrytimer ends
People hurrybutton pressed

    Show the completed table
    State nowEventNext state
    Cars gobutton pressedCars slow
    Cars gotimer endsCars go
    Cars slowtimer endsPeople cross
    Cars slowbutton pressedCars slow
    People crosstimer endsPeople hurry
    People crossbutton pressedPeople cross
    People hurrytimer endsCars go
    People hurrybutton pressedPeople hurry

    Four of the eight rows leave the state unchanged. A model needs a row for an event that does nothing, because someone can still cause it.

    Now use the model. The crossing starts in Cars go, and these events happen in order. Work out the state after each event, using the table.

    Running the crossing through seven events
    StepEventState after
    1timer endsCars go
    2button pressed
    3timer ends
    4timer ends
    5button pressed
    6timer ends
    7timer ends

      Show the completed table
      StepEventState after
      1timer endsCars go
      2button pressedCars slow
      3timer endsPeople cross
      4timer endsPeople hurry
      5button pressedPeople hurry
      6timer endsCars go
      7timer endsCars go

      The model starts in Cars go. The first row is given.

      Check your understanding

      At step 5 the button is pressed while the state is People hurry, and the state stays the same. What is the best explanation, in this model?
      Show the answer

      Answer: D. In this model a press only matters when cars are going, so otherwise it changes nothing.

      A model makes decisions, such as “a press is ignored here”. Another designer might choose to remember the press.

      Stage: Apply

      A game character as states

      The same idea models a character in a fictional platform game. The character can be Standing, Running or Jumping. The events are arrow key held, arrow key released, jump key pressed and lands. The model has these rows:

      State nowEventNext state
      Standingarrow key heldRunning
      Standingjump key pressedJumping
      Runningarrow key releasedStanding
      Runningjump key pressedJumping
      JumpinglandsStanding

      Apply it

      A tester presses the jump key while the character is already Jumping. Which pair of state and event has no row in the table?
      Show the answer

      Answer: B. Jumping with the event jump key pressed.

      Any pair of state and event that can really happen needs a decision, even if the decision is “nothing changes”.

      Think it through

      Decide what the model should do when the jump key is pressed while Jumping, and write the row you would add. Give a reason, and say how the game would feel to play with your choice.

      Show a model answer

      Two reasonable rows are Jumping, jump key pressed, Jumping (the press is ignored, so the character cannot jump twice, which makes the game harder and the jumps more predictable) and a new state such as Double jumping (the character jumps again in mid-air, which makes the game feel freer but needs another state and more rows). Both are right: the model records a design decision, and the designer, not the table, makes it.

      Stage: Predict

      Running a model over time

      Back to the canteen. A fictional model of the lunch queue uses these rules: pupils arrive each minute in the numbers 4, 4, 3, 1, 1, 0 and 0, for seven minutes; one till serves up to 2 pupils every minute; and the queue is empty at the start.

      Think it through

      Without working it out exactly, guess: does the queue grow at first, and does it ever get shorter? How long do you think the longest queue will be, and in which minute?

      Show what happens

      At first more pupils arrive each minute (4, 4, 3) than the till serves (2), so the queue grows. Later, when only 1 or 0 arrive, the till serves 2, so the queue shrinks. The exact length of the longest queue needs the table below.

      Each minute follows the same rule: the pupils waiting before serving are the queue left from the minute before plus the pupils who arrive; the till then serves the smaller of 2 and the number waiting; the queue left is what remains. Running a model like this, one step at a time, to see what happens is a simulation.

      Simulating seven minutes of the lunch queue (one till, serving 2 pupils a minute)
      MinutePupils arriveWaiting before servingServedQueue left
      14422
      24
      33
      41
      51
      60
      70

        Show the completed table
        MinutePupils arriveWaiting before servingServedQueue left
        14422
        24624
        33725
        41624
        51523
        60321
        70110

        The first row is given. Minute 7 shows why “served” is the smaller of 2 and the number waiting.

        Check your understanding

        What is the longest queue left at the end of any minute, and in which minute?
        Show the answer

        Answer: A. 5 pupils, at the end of minute 3.

        A simulation lets you see how a system changes over time without waiting for it to happen.

        Stage: Explain

        Asking what if?

        The headteacher asks whether opening a second till, so that 4 pupils are served each minute, would fix the long queue. It is cheaper to change the model than to change the canteen.

        Think it through

        Change the model so that the till or tills serve 4 pupils a minute, keeping the same arrivals (4, 4, 3, 1, 1, 0, 0). Predict what happens to the queue. Then work out the queue left each minute, to see if you were right.

        Show a model answer

        Minute 1: 4 arrive, 4 are served, 0 left. Minute 2: 4 arrive, 4 served, 0 left. Minute 3: 3 arrive, 3 served, 0 left. Minutes 4 and 5: 1 arrives and 1 is served, 0 left. Minutes 6 and 7: nothing arrives, nothing is waiting. The queue left is 0 in every minute, so with this model the second till would keep the queue from forming at all.

        Check your understanding

        A pupil asks whether three pupils served per minute instead of four would be enough. Using the same arrivals, what is the longest queue left at the end of a minute?
        Show the answer

        Answer: C. 2 pupils, at the end of minutes 2 and 3.

        Changing a number in the model and running it again is how a simulation answers “what if?” without trying it for real.

        Stage: Apply

        How far can a model be trusted?

        The queue model is simple on purpose. It assumes that pupils arrive in the numbers given and that every pupil takes exactly the same time to serve. It leaves out pupils who join and leave, pupils who choose slowly, a till that stops for a minute, and a bell that brings everyone at once. Each of these could make the real queue different.

        A model is not a copy of reality, and a simulation does not prove what will happen. A model shows what happens if its assumptions are true. A good model is right enough for its purpose, and is tested against real observations. Where it is wrong, the assumptions are what to examine.

        Apply it

        On a real Tuesday the longest queue in the canteen was 9 pupils, but the model with one till said 5. What is the most sensible next step?
        Show the answer

        Answer: B. Compare its assumptions with what really happened, then adjust the model.

        Evaluating a model means comparing it with real observations and asking which assumptions are responsible when they disagree.

        Apply it

        Dev (fictional) says: “The simulation proves a second till will remove the queue.” Which reply is most accurate?
        Show the answer

        Answer: A. It shows what would happen if its assumptions hold, which a real day may not match.

        A good model supports a decision; it does not make it.

        Stage: Apply

        Design a model of your own

        A fictional school drinks machine takes a coin, lets the user choose a drink, gives the drink, and returns to waiting. A user can also press a refund button after paying and before choosing.

        Think it through

        Design a state model for the machine. Give its states and its events, then write the table with a row for every pair of state and event. Say one thing your model leaves out.

        Show a model answer

        One model has three states, Waiting, Paid and Giving drink, and four events, coin inserted, choice made, refund pressed and drink taken.

        State nowEventNext state
        Waitingcoin insertedPaid
        Waitingchoice madeWaiting
        Waitingrefund pressedWaiting
        Waitingdrink takenWaiting
        Paidcoin insertedPaid
        Paidchoice madeGiving drink
        Paidrefund pressedWaiting
        Paiddrink takenPaid
        Giving drinkcoin insertedGiving drink
        Giving drinkchoice madeGiving drink
        Giving drinkrefund pressedGiving drink
        Giving drinkdrink takenWaiting

        Every state has a row for every event, with “nothing changes” where an event cannot matter. The model leaves out many details: the price, whether the drink is in stock, how much money was paid, and what happens if the user never takes the drink.

        Stage: Consolidate

        Show what you know

        Think it through

        Explain in your own words what a model keeps and leaves out, and what a state, an event and a simulation are. Use a system that is not in this lesson, such as a school bell, a lift or a washing machine.

        Show a model answer

        A good answer says that a model is a simplified version made to answer a question, which keeps what the question needs and leaves out the rest. For a lift: the states might be “stopped at floor 2” or “moving up”; an event is a button pressed or a floor reached; a transition is the lift moving from one state to the next; a simulation would run a day of button presses through the model to see how long people wait. The model leaves out the lift’s weight limit, its speed and its colour.

        Apply it

        A fictional town wants to test whether a new bus timetable would shorten the waiting time at its stops. Which model should the town build?
        Show the answer

        Answer: D. Buses and passengers arriving at each stop minute by minute, without colours or prices.

        The question decides what the model must keep. Everything else can be left out.

        Think it through

        Which part of this lesson are you least sure about? Write one question you would ask your teacher.

        Show a model answer

        There is no wrong answer. Two questions worth asking: “how do I decide what to leave out?” (ask what the model is for, and keep what that question needs) and “how do I know a model is good?” (compare it with real observations and examine the assumptions where they differ).

        • A model is a simplified representation made to answer a question. Abstraction keeps what matters to the question and leaves out the rest, so different questions need different models of the same thing.
        • A state is how a system is now; an event is something that happens; a state transition is the change from one state to another caused by an event. A table of state, event and next state describes the behaviour, and needs a row for events that change nothing.
        • A simulation runs a model step by step, such as a queue minute by minute. Changing a number and running it again answers a what-if question without changing the real thing.
        • A model shows what happens if its assumptions are true. Evaluate it by comparing it with real observations and examining which assumptions differ.

        Sources and scope

        • What this lesson is. The twentieth lesson of the Algorithms and Programming module, written for this site. It covers modelling in the Key Stage 3 programme of study without needing a programming language. Its curriculum links are provisional and imply no review or endorsement by any examination board.
        • Original material. The bus map, the crossing, the platform game, the canteen and its numbers, the drinks machine and Dev are fictional, written for this lesson. Every table, the simulation and the second-till results were worked out by hand and checked with a short Python program.
        • Simplified on purpose. The crossing has four states and two events, and real crossings have more (flashing amber, detectors and timers of different lengths). The queue uses invented numbers and a single rule, with no randomness, and the lesson does not teach how to model chance. Models are built with tables and not in code.
        • Left to later lessons. Writing a model as a program, and models that use random numbers.