Cambridge A Level · Part 3 · Chapter 22
Artificial Intelligence (AI)
How a computer can find the shortest route, learn from experience, fit a line to data and copy (very roughly) the neurons in your brain. Every figure, question and exam question from the chapter is here, with labs you can run.
Guru Jazzy🎯 Learning objectives
By the end of this chapter you should be able to:
- show understanding of how graphs can be used to aid Artificial Intelligence (AI)
- show understanding of how artificial neural networks have helped with machine learning
- show understanding of Deep Learning, Machine Learning and Reinforcement Learning, and the reasons for using these methods
- show understanding of back propagation of errors and regression methods in machine learning
🧭 22.01 An overview
"Artificial intelligence" is hard to define, because intelligence itself is hard to define. You might say working out 43 × 13 in your head takes intelligence. A calculator gets the same answer, but nobody calls a calculator intelligent. So a definition like this one is not really good enough:
Artificial intelligence involves the automation of intelligent behaviour.
Everyone agrees that AI is part of computer science, and that it has many separate sub-areas (several of them are in this chapter). The usual conclusion is that a vague definition works best, for example:
Artificial Intelligence is concerned with "how to make computers do things at which, at the moment, people are better."
— E. Rich, Artificial Intelligence, McGraw-Hill, 1983
🗺️ The chapter on one page
Four big ideas, and how they connect. (The Reflection Point at the end of the chapter asks you to draw a chart like this yourself — try it before you look at mine on the last tab.)
Nodes and weighted edges. Dijkstra finds the shortest path from one node to every other; A* adds a heuristic to head for one target.
A system that improves at a task through experience: supervised, unsupervised or reinforcement learning.
Fitting a mathematical function to past data so it can predict new outputs.
Layers of simple artificial neurons; they learn by back propagation of errors. Many hidden layers = deep learning.
🕸️ 22.02 How graphs can be used in AI Figure 22.01
A graph is a collection of nodes (or vertices) which may be joined by edges. Each node has a name. An edge can carry a label, which is a numerical value.
A common use: the nodes are places and the edge labels are the distances between them. An edge is only drawn when you can travel directly between that pair of places. A graph like this can be used to find the shortest route between two nodes that are not next to each other.
We could find the shortest route from A to D ourselves by listing every possible route and adding up each one:
| Route | Overall distance |
|---|---|
| A → B → C → D | 40 + 10 + 40 = 90 |
| A → B → F → E → D | 40 + 15 + 20 + 5 = 80, the shortest |
| A → F → E → D | 60 + 20 + 5 = 85 |
| A → F → B → C → D | 60 + 15 + 10 + 40 = 125 |
Fine for six nodes — but for a graph with 100 nodes this would take far too long. Luckily, AI algorithms have been developed to solve this kind of problem.
Book slip fixed: the book says "Using Figure 20.01" here; it means Figure 22.01.
🔬 Lab: the brute-force way
Pick two nodes of Figure 22.01. The lab lists every route that does not visit a node twice — exactly what the book did by hand.
Can you think of two alternatives for what the graph in Figure 22.01 might represent? For each, state what the edge labels would represent.
- A railway or bus network: nodes are stations; edge labels are the fare or the journey time between neighbouring stations.
- A computer network: nodes are routers; edge labels are the delay (latency) or the cost of sending data along each link.
- Others: rooms in a building and walking times; tasks in a project and the time between them; pipes in a water network and their capacity.
🛣️ Dijkstra's algorithm
Dijkstra's algorithm finds the shortest path from one starting node to every other node. Here is the book's Structured English design for a simplified version:
Identify the source node (S) where the path starts.
Create an empty set called the ShortestPath set.
Create another set called RemainingNodes and put all of the nodes
into this, including the source node (S).
Create a record that stores:
node names
calculated values for the distance to the node from the source node
the sequence of nodes in the route to the node.
Set the distance value for the source node S to be 0.
Set the distance value for all other nodes to be INFINITY
(a large value greater than any value that will be calculated).
While the ShortestPath set does not include all of the nodes:
Pick the node (N) from the RemainingNodes set that has the
lowest distance value.
Move this node into the ShortestPath set.
For each node in the RemainingNodes set that is adjacent to N:
Calculate a new distance value by adding the label of the
edge connecting the two nodes to the stored distance for N.
If this value is less than the value currently stored,
replace the stored value by the new one.
If a new value has been stored, enter the sequence of
nodes used to obtain this value. Table 22.01 shows the algorithm working on Figure 22.01. Node names are red while still in the RemainingNodes set. The node N picked at each stage is in black. Data turns grey when it can no longer change, because the node has moved into the ShortestPath set.
Notice that some data never changes once it is set (nodes C and E), but other data has to be updated later (nodes D and F).
🔬 Lab: Dijkstra step by step Table 22.01
Press Next step to build Table 22.01 one stage at a time. Then load the exam-question graph to check your answer to Exam question 1, or make a random graph for fresh practice.
⭐ The A* algorithm
Dijkstra finds the best route from one node to all the others. Often you only want the best route to one target. You could stop Dijkstra once the target's route is fixed, but it would still do far more work than needed — at the start it may be busy exploring routes heading in completely the wrong direction.
A* ("A star") is a modification of Dijkstra designed to fix this. One step changes. In Dijkstra the step is:
Calculate a new distance value by adding the label of the edge connecting the two nodes to the already stored distance for N.
In A* it is expanded to:
Calculate a new distance value by adding the label of the edge connecting the two nodes to the already stored distance for N.
Calculate an estimated value for the distance of N from the destination node and add this to the new distance value.
The estimate of the distance still to go needs a heuristic function. Whatever function is chosen, it must guarantee that the estimate is lower than the actual value (it must never over-estimate).
🗺️ A worked example: seven towns Figure 22.02 · Table 22.02 · Figure 22.03
The task: find the shortest route between two towns using the existing roads. Figure 22.02 is a basic map of seven towns, A–G.
The map matters because it was drawn from an x–y coordinate for each town, and those coordinates are the basis of the heuristic. Pythagoras's theorem gives the straight-line ("as the crow flies") distance:
Direct distance = √( (difference in x)² + (difference in y)² )
This straight-line distance should always be less than any distance actually travelled along roads.
| A | B | C | D | E | F | G |
|---|---|---|---|---|---|---|
| 40,20 | 30,30 | 80,35 | 130,30 | 120,20 | 45,5 | 100,30 |
Table 22.02 The x–y coordinates for the towns A–G
📐 Heuristic calculator
Pick two towns to see the straight-line estimate worked out.
The roads are shown as a graph in Figure 22.03. An edge means a road links the two towns directly, and its label is the distance along that road.
There are many versions of A*. The book points out that the version it demonstrates does not exactly match any one version in the literature.
📜 The book's Structured English for A*
Create three lists: the initial list, the open list and the closed list.
Insert an empty record for each node in the graph into the initial list.
Store the x and y coordinates of the node in each record of the initial list.
Initialise the value for distance travelled to zero in each record.
Create a look-up table with an entry for each pair of nodes that have
a direct connecting road.
Store the distance of travel along that road for each pair of nodes.
Identify the target node for the travel.
Identify the starting node and copy its record into the open list.
Delete the record for the starting node from the initial list.
Now recursively apply the following until all possibilities have been examined:
For the parent node in the open list identify all adjacent nodes in the initial list.
Copy the record for each of the adjacent nodes into the open list.
Delete the records for the adjacent nodes from the initial list.
Check if the target node is now in the open list.
If the target node is NOT in the open list:
For each adjacent node now added, use the look-up table to find the
distance of travel from the parent node.
If options exist, calculate a value for each route and choose the smallest.
For each adjacent node update the distance travelled and the path sequence.
For each adjacent node use the heuristic function to calculate an
estimated distance to travel.
For each adjacent node calculate an estimated total distance from start
node to target node (distance travelled + estimated distance to travel).
Identify the adjacent node with the lowest estimated total. Leave its
record in the open list as a parent node. Copy the records for the
remaining nodes into the closed list and delete them from the open list.
Continue with the next iteration of the recursion.
If the target node IS in the open list, for this node only:
Use the look-up table to find the actual distance from the parent node.
Calculate the total distance travelled.
If the target's record has zero distance travelled, enter the value now
calculated and the path sequence.
If it already has a value, compare; if the new route is shorter, update
the distance travelled and the path sequence.
Copy the record for the target node into the initial list and delete it
from the open list.
If there is another adjacent node in the open list, continue with the
actions above for when the target is not in the open list.
If the open list is now empty, rewind to check nodes in the closed list
that have not been included in previous path sequences. For each,
calculate an estimated total distance from start node to target node.
If this estimate is greater than the stored distance travelled for the
target, continue to rewind. If it is less, copy the node's record into
the open list as a parent node and continue with the actions above for
when the target is not in the open list. 🚶 The book's walk-through: from A to D
- With A as the parent, B, C and F are the adjacent nodes brought into the open list:
There are two possible calculations for C and for F, but in each case the direct road is shorter.
Node Distance travelled g Estimated further distance h Total f B 13 100 113 C 45 50.2 (the book rounds to 51) 95.2 (book: 96) F 20 88.6 (book: 89) 108.6 (book: 109) - C has the lowest total, so B and F move to the closed list and C becomes the new parent. The only adjacent node still in the initial list is G, which comes into the open list.
- G becomes the parent for its adjacent nodes D and E. D is the target. The actual distance A → C → G → D is 45 + 25 + 40 = 110. This is stored in D's record, and D's record moves back to the initial list.
- E is the only node left in the open list, so it becomes the parent. Its only adjacent node in the initial list is D, which comes back into the open list. The actual distance A → C → G → E → D is 45 + 25 + 10 + 15 = 95. That is lower than 110, so D's record is updated and moved back to the initial list.
- The rewind now checks B and F. Their estimated totals (113 via B, about 109 via F) are both greater than 95, so they can be ignored. The shortest route is A–C–G–E–D, total distance 95.
🔬 Lab: A* step by step on Figure 22.03
This uses the standard textbook form of A* (open list ranked by f = g + h, with h the straight-line distance from Table 22.02). It finds the same route as the book's version. Change the start or target and compare how much work A* does against Dijkstra.
A heuristic is a rule of thumb: a quick way to get a good-enough estimate instead of an exact answer. A heuristic function takes a node and returns an estimate of how far it is from the goal. Straight-line distance is the classic one for maps. On a grid where you can only move up, down, left and right, the "Manhattan distance" (|Δx| + |Δy|) is used instead. A heuristic that never over-estimates is called admissible; with one, A* is guaranteed to find the shortest path.
- Grid maps: divide the area into squares; each free square is a node, joined to its free neighbours. Squares covered by an obstacle are simply left out of the graph. This is how many video games move characters.
- Visibility graphs: nodes are the corners of the obstacles; an edge joins two corners if you can see one from the other.
- Navigation meshes: the walkable floor is split into polygons, and A* runs over the polygons.
- Robots add the obstacles to their map as their sensors detect them, then re-plan the route (for example with the D* family of algorithms).
📈 22.03 Machine learning
The requirements for machine learning can be summarised as:
- a computer-based system has a defined task or tasks to perform
- knowledge is acquired through the experience of performing the tasks
- as a result of this experience and knowledge, the performance of future tasks is improved.
Learning from experience is a sign of intelligence, so machine learning is one of the many approaches under the umbrella of AI. Learning can happen in several ways; three are considered here.
🧩 Unsupervised learning
In unsupervised learning the system has to draw its own conclusions from its experience of the results of the tasks it has performed. Algorithms are needed that can organise or categorise the knowledge it gains. One example identifies "conceptual clusters" using a hierarchy: all the knowledge starts in the root of a tree, then groups are moved into branches depending on their attributes.
Unsupervised learning is now a dominant activity. Powerful computers with access to huge data banks make decisions based on recorded past actions. Our activity on the web is recorded and stored, then used to decide which products or services to recommend to us next. There is no theoretical framework for this — it is rather as though the intelligence is built into the data.
👩🏫 Supervised learning
In supervised learning the system is fed knowledge with an associated classification. For example, an AI program might be developed to mark exam questions. During training it is given answers to exam questions together with a grade or categorised comments for each one.
A special case: expert systems
An expert system always focuses on a narrowly defined domain of knowledge. Human experts are given samples of data to analyse and provide the conclusions to be drawn. The data and conclusions go into the knowledge base. The system is tested by an expert supplying sample data and checking the system's conclusions; if performance is poor, more data and conclusions are added.
🏆 Reinforcement learning
Reinforcement learning has some features of unsupervised learning and some of supervised learning. It has its own vocabulary:
- An agent is learning how best to perform in an environment.
- The environment has many defined states.
- At each step the agent takes an action.
- The agent has a policy that guides its actions.
- The policy is influenced by the recorded history and by knowledge of the current state of the environment.
- An action changes the environment to a new state.
- After each action the agent receives a reward: a measure of how effective the action was towards the overall goal.
- The policy guides the agent in deciding whether the next action should exploit knowledge it already has or explore a new avenue.
In summary, the aim is to maximise the reward by improving the policy. It is a trial-and-error search for the best performance, needing many repeated attempts at the same problem.
Applications: playing logic games such as backgammon; robotics, where a robot learns to do a task well; navigating a maze — when the agent turns the way that eventually leads to the exit it gets a positive reward, and a wrong turn earns a negative one.
🤖 Lab: an agent learns a maze
The robot starts bottom-left (S) and wants the exit (★). Each move costs −1, the trap (✖) costs −10, the exit pays +20. It learns a value for every state–action pair (this is Q-learning, one real reinforcement-learning method). Arrows show its current policy; brighter squares are states it now values more.
Try ε = 0: the agent only exploits and often gets stuck with a poor route. A little exploring helps it discover better ones.
🎮 Which kind of learning?
📉 Regression analysis methods Figure 22.04
Sometimes the aim of an AI system is to predict a numerical output from input values. To use regression analysis, the system is first given real values for both the inputs and the outputs. This data is used to look for a correlation. If a correlation exists that can be written as a mathematical formula, that formula can then predict outputs when new data is input.
The simplest case: one input quantity and a linear relationship. Example: an AI system predicting candidates' A Level Computer Science marks. We might expect them to correlate with the candidates' IGCSE Mathematics marks. Figure 22.04 shows what might be found from historic data.
There is good correlation. The straight line is the calculated best fit, and its formula could sensibly be used to predict future A Level Computer Science marks from IGCSE Mathematics marks.
Regression can be more complicated. A formula could be fitted using marks from three different exams as inputs. Or a non-linear relationship may be needed — for example, predicting the sales of a new product whose sales are expected to grow exponentially.
🔬 Lab: fit the best line
The dots start as data like Figure 22.04 (read off the graph by eye). Click or tap the chart to add students, then see the least-squares line and the correlation coefficient r update. Use the line to predict a mark.
🧠 22.04 Artificial neural networks Figure 22.05
The neural networks in our brains give us our intelligence, so it seems obvious to use artificial neural networks as a foundation for AI systems. Figure 22.05 shows two nerve cells.
At one end of a nerve cell are many dendrites, which can receive signals. At the other end are many axon terminal buttons, which can transmit signals. The synapse is the region between an axon terminal button and a dendrite, and it contains neurotransmitters. When a cell receives input signals, the voltage in the axon rises. At some threshold voltage the neurotransmitters are activated and signals are sent to the dendrites of neighbouring cells.
🔺 Modelling it in a computer Figure 22.06
An artificial neural network can be built in software or hardware. In Figure 22.06 the triangles are the nodes, which represent artificial neurons (they are sometimes drawn as circles). A node can receive one or more inputs and send its output to one or more other nodes.
Modelling a node: a weighting factor is applied to each input; the weighted values are summed; then an activation function computes the node's output. Any input that is not a number must first be converted into one so the weighted values can be summed.
Figure 22.06 has three layers. The left column of three nodes receives the input; the right column gives the output. The two nodes in between form a hidden layer. Some networks have several hidden layers.
🔬 Lab: one artificial neuron
Set the inputs and weights. The node multiplies, sums, adds a bias, then applies the activation function — just like a nerve cell "firing" once a threshold is passed.
🔋 An example: battery cost-effectiveness
Imagine an AI system using a neural network to estimate the cost-effectiveness of batteries, based on the price paid and the lifetime of use.
- Each input represents one example battery — data that identifies the battery and its price.
- One hidden node might be concerned with the type of device the battery is used in; the other with the type of user of that device.
- The output nodes compute an estimated cost per unit of time for a specific battery.
Every node has adjustable factors: the weighting factors for each input and the activation function. During initial learning these must be tuned to make the predictions as accurate as possible. The method used is back propagation of errors:
- Collect real battery-lifetime data from actual use.
- Create the system with trial values for all the adjustable factors.
- Run it with the input data that matches the real use. Each output node now has an error: the difference between its output and the real lifetime value.
- Re-run with different weights and activation functions for the output-layer nodes, to see how the accuracy of the output depends on those factors.
- Do the same for the nodes in the hidden layer, and finally for the input layer.
- The system can now predict the cost-effectiveness of other batteries. As more real-use data becomes available, back propagation can be applied again to improve it further.
🔬 Lab: watch back propagation learn
A tiny network (2 inputs → 3 hidden nodes → 1 output) learns XOR: output 1 only when exactly one input is 1. A single neuron can't do this; a hidden layer can. Each epoch runs all four examples, measures the error at the output, then passes it backwards to adjust every weight a little. Watch the error curve fall.
An early successful application of an artificial neural network was NETtalk. It took text as input and output synthesised sound reading the text. The network had 7 × 29 input nodes and 26 output nodes. Can you investigate why that number of input nodes was chosen, and what some of the 80 nodes in the hidden layer did?
- NETtalk (Sejnowski and Rosenberg, 1987) looked at a window of 7 characters: the letter being pronounced plus 3 on each side, because how a letter sounds in English depends on its neighbours (think of the "c" in cat and city).
- Each character position had 29 input nodes, one per possible symbol: 26 letters plus space and two punctuation marks. Exactly one is "on" for each position, so 7 × 29 = 203 inputs.
- The 26 outputs coded the features of the sound (where and how it's made in the mouth, stress, syllable boundaries), which drove a speech synthesiser.
- After training, analysis of the 80 hidden nodes showed they had organised themselves without being told to: some groups responded to vowels and others to consonants, and finer groups separated sounds that are pronounced in similar ways.
🏗️ Deep Learning
In the brain, neurons are thought to be arranged in layers: the lower layers have easily understood functions, while higher layers do more abstract processing. With today's computing power, artificial neural networks are being built with large numbers of hidden layers to try to do something similar. These are Deep Learning systems.
- Artificial Intelligence
- Graph-based search / path-finding
- Dijkstra's algorithm
- A* algorithm (Dijkstra + heuristic)
- Expert systems (AI, but not machine learning)
- Machine learning
- Supervised learning
- Regression analysis
- Artificial neural networks trained by back propagation of errors
- Deep learning (many hidden layers)
- Unsupervised learning (e.g. clustering, recommendations)
- Reinforcement learning (agent, policy, reward)
- Supervised learning
- Graph-based search / path-finding
📌 Summary
- A graph can be constructed from nodes and edges, where the edges carry numerical value labels.
- Algorithms are available to find the shortest path between two nodes in a graph.
- Machine learning can be supervised or unsupervised.
- Regression analysis involves finding a mathematical equation which is a best fit to sample data.
- Artificial neural networks are modelled using nodes which receive input and provide output.
- Back propagation of errors can be used for machine learning using artificial neural networks.
🃏 Key-term flashcards
Tap a card to flip it.
✅ Quick quiz
📝 Exam-style questions
Write your answer first, then open the model answer. These are my mark-scheme-style points, not official mark schemes.
Grey = fixed (already in the ShortestPath set). You can also replay this in the Dijkstra lab on the "Graphs & Dijkstra" tab — choose Exam question 1.
| ShortestPath set | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| {} | 0 | ∞ | ∞ | ∞ | ∞ | ∞ |
| {A} | 0 A | 10 A-B | ∞ | ∞ | ∞ | 25 A-F |
| {A,B} | 0 A | 10 A-B | 60 A-B-C | ∞ | ∞ | 15 A-B-F |
| {A,B,F} | 0 A | 10 A-B | 60 A-B-C | ∞ | 30 A-B-F-E | 15 A-B-F |
| {A,B,E,F} | 0 A | 10 A-B | 60 A-B-C | 38 A-B-F-E-D | 30 A-B-F-E | 15 A-B-F |
| {A,B,D,E,F} | 0 A | 10 A-B | 40 A-B-F-E-D-C | 38 A-B-F-E-D | 30 A-B-F-E | 15 A-B-F |
| {A,B,C,D,E,F} | 0 A | 10 A-B | 40 A-B-F-E-D-C | 38 A-B-F-E-D | 30 A-B-F-E | 15 A-B-F |
Key points an examiner looks for: F improves from 25 to 15 via B; C starts at 60 via B but improves to 40 via D; nodes join the set in the order A, B, F, E, D, C.
Any five points:
- Each circle is a node, representing an artificial neuron.
- The bottom row is the input layer: these nodes receive the input values.
- The middle row is a hidden layer: it is not seen from outside and does the intermediate processing.
- The top row is the output layer; the arrows at the top are the outputs of the network.
- The lines/arrows are connections carrying values from one layer to the next; each connection has a weighting factor.
- Each node sums its weighted inputs and applies an activation function to produce its output.
- Every node in one layer is connected to every node in the next layer.
- Set initial (trial/random) values for the weights and activation functions.
- Run the network with training data for which the correct outputs are known.
- Calculate the error at each output node: the difference between the actual output and the expected output.
- Adjust the weights/activation values of the output layer nodes to reduce the error…
- …then work backwards through the hidden layer(s), and finally the input layer.
- Repeat with more training data until the error is acceptably small.
Machine learning (3 points):
- A system has a defined task to perform.
- It acquires knowledge from experience of doing the task / analysing previous performance.
- It uses this to improve its performance of future tasks without being explicitly reprogrammed (supervised, unsupervised or reinforcement).
Artificial neural network (3 points):
- A model, in software or hardware, based on the neurons of the brain.
- Made of nodes arranged in layers: input, hidden and output.
- Each node applies weights to its inputs, sums them and uses an activation function to produce an output; the weights are adjusted during learning.
Deep learning (3 points):
- Uses an artificial neural network…
- …with an exceptionally large number of hidden layers.
- Lower layers carry out simple functions and higher layers more abstract processing, needing large amounts of data and computing power.
- Machine learning using artificial neural networks (including deep learning) — a form of supervised learning.
- Because the correct output must be known for each training example, so the error at each output node can be calculated and passed back through the layers to adjust the weights.
Guru Jazzy · Cambridge AS & A Level Computer Science · Chapter 22 Artificial Intelligence · Figures from the Cambridge coursebook (Langfield & Duddell)