OCR H446, section 2.1.1 (d)
You need to be able to devise an abstract model for a variety of situations: look at a real or described situation, and decide what to represent and how, so that it can be handled by a program.
A method for building a model
- State the purpose. What question must the model answer? What will it be used for?
- Identify the main things (entities) involved, and the relationships between them.
- List the properties (attributes) of each thing, and decide which are needed for the purpose. Keep those, and discard the rest.
- Decide what can change, and what actions (operations) can be done.
- Choose a suitable representation: variables, data structures, classes, graphs, state machines, equations.
- State your assumptions and limitations.
- Test the model against known cases, and refine it.
Choosing a representation
| Situation | Suitable model |
|---|---|
| Things with properties (a student, a product) | A record, or a class with attributes |
| A collection of similar things | An array or list |
| Connections between things (roads, friends, networks) | A graph (nodes and edges) |
| A hierarchy (folders, family trees) | A tree |
| Things with different states that change in response to events | A state machine (states and transitions) |
| Last in, first out or first in, first out behaviour | A stack or queue |
| Looking up values by a key | A dictionary or hash table |
| Numerical behaviour over time | Equations and a simulation |
Example 1: a library
Purpose: record which member has borrowed which book.
- Entities:
Member,Book,Loan. - Properties kept: a member's ID and name; a book's ISBN and title; a loan's dates.
- Properties discarded: the member's address and hobbies, the book's colour and weight.
- Relationships: a member has many loans, and a book has many loans (over time).
- Operations: borrow, return, check if overdue.
from datetime import date, timedelta
class Book:
def __init__(self, isbn, title):
self.isbn = isbn
self.title = title
self.on_loan = False
class Member:
def __init__(self, member_id, name):
self.member_id = member_id
self.name = name
class Loan:
def __init__(self, member, book, borrowed):
self.member = member
self.book = book
self.borrowed = borrowed
self.due = borrowed + timedelta(days=21) # three weeks
def is_overdue(self, today):
return today > self.due
loans = []
def borrow(member, book, today):
if not book.on_loan:
book.on_loan = True
loans.append(Loan(member, book, today))
def return_book(loan):
loan.book.on_loan = False
loans.remove(loan)
Example 2: a road network
Purpose: find the shortest route between two places.
- Entities: junctions and roads.
- Kept: the connections and the distance of each road.
- Discarded: the shape, name and surface of the roads.
- Model: a weighted graph, stored as a dictionary of lists:
roads = {
"A": [("B", 4), ("C", 2)],
"B": [("A", 4), ("D", 5)],
"C": [("A", 2), ("D", 8)],
"D": [("B", 5), ("C", 8)],
}
A standard algorithm (Dijkstra's, 2.3.1) can now be used. The abstraction has turned a real-world problem into a known one.
Example 3: a vending machine (a state machine)
Purpose: control what the machine does as the user acts.
- States:
Idle,HasMoney,Dispensing. - Events: coin inserted, product chosen, product taken.
- Kept: the order of events and the rules. Discarded: the mechanics of the motors and the look of the machine.
state = "Idle"
def handle(event):
global state
if state == "Idle" and event == "coin":
state = "HasMoney"
elif state == "HasMoney" and event == "choose":
state = "Dispensing"
elif state == "Dispensing" and event == "taken":
state = "Idle"
Example 4: a lift
Purpose: decide which floor to go to next.
- Kept: the current floor, the direction, and the set of floors requested.
- Discarded: the lift's size, speed profile and decor.
- Model: variables
flooranddirection, and a set of requests.
floor = 0
direction = "up"
requests = {3, 5}
def next_stop():
above = [f for f in requests if f > floor]
below = [f for f in requests if f < floor]
if direction == "up" and above:
return min(above)
if below:
return max(below)
return min(above) if above else None
Example 5: a game board
Purpose: play noughts and crosses.
- Kept: the 3 by 3 grid and the mark in each square (
"X","O"or empty), and whose turn it is. - Discarded: the colour of the pen, and the size of the paper.
- Model: a 2D array.
array board[3,3]
board[1,1] = "X"
Checking and improving the model
- Does it have everything needed to answer the question?
- Does it contain anything unnecessary (which makes it more complicated)?
- Test with real examples, including unusual ones.
- State the assumptions, such as "all lifts travel at the same speed".
Worked example
Task: devise an abstract model for a school canteen queue, to estimate how long pupils wait.
- Purpose: estimate waiting times, so that staffing can be planned.
- Entities: pupils and servers.
- Kept: the arrival time of each pupil, the time to serve each one, the number of servers, and the queue order.
- Discarded: pupils' names, what they order, their year group, and the weather.
- Model: a queue data structure (first in, first out). Each pupil is an item with an arrival time and a serving time. Servers take the pupil at the front when free. The simulation moves time forward in steps and records each pupil's wait.
- Assumptions: serving times are random but within a known range, and no pupil leaves the queue. These limit how accurate it is, and could be improved with real data.
Exam tip: state what you include and what you leave out, why, and which structure represents it (a record, graph, queue, state machine and so on). Show that the model fits the purpose.
Check yourself
What is the first step in devising an abstract model?
State the purpose: what the model is for, and what question it must answer. That decides what to keep and what to leave out.
Which data structure would you use to model a road network for finding routes?
A weighted graph, with junctions as nodes and roads as edges labelled with distances.
How would you model a machine that changes its behaviour in response to events?
A state machine, with states and transitions caused by events.
Why should assumptions be stated when you devise a model?
They show where the model may differ from reality, so the limits of its accuracy are clear.