Family edition
Map Robot Lab: What Does Best Mean? A Cost Function
In Map Robot Lab your child tells a route planner what a road costs, and finds that the fewest blocks, the fewest minutes and the quietest streets are three different routes.
What you need
- A computer or Chromebook with a current browser.
- The link: mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=fam-maprobotlab-3. No account, no email and nothing to install.
Before you start: try it once yourself
Open mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=fam-maprobotlab-3. A short guide opens over Level 5, Fewest blocks isn't fastest. The town fills the view from above: a lake in the middle, a green avenue around the edge where each road takes 1 minute, gray streets that take 2, and Main Street, stuck in traffic, at 5. Each road shows its minutes. The card at the right says the level's goal and, after each run, what happened, with the run's own numbers. The Trip log counts blocks, minutes, school roads and plans made, and the charts under the map keep every route tried on the trip.
- Level 5, every road 1. The starter blocks plan a route with a road costs return 1. Press play: 7 blocks in 24 minutes, along Main Street, where each road takes 5.
- Level 5, minutes. Put this road's minutes in place of the 1. The same planner finds 9 blocks in 10 minutes around by the green avenue, with 2 school roads. The level passes.
- Level 7. Open Levels and start Level 7. With the minutes cost: 10 blocks in 10 minutes and 2 school roads, which misses. The Cost drawer now has a second a road costs: this road's minutes plus (if this road is a school road, 10, else 0). Put it in place of the first: 14 blocks, 14 minutes, no school roads.
- Penalties. Plus 1 or 2: still 10 minutes past the school. Plus 3, 4, 5, 10 or 100: 14 minutes, no school roads.
- Speed. Slower, Real time and Speed up, under the map, change only how fast the van moves on the screen; the minutes do not change. Step moves the van one block, or the ripple one corner. The round play button runs the program from the start. The town has no randomness: the same program drives the same way on every run and every computer.
The ideas, in plain words
The planner finds the route with the lowest total cost, and the cost function says what each road costs. Children write it once, and the planner calls it for every road it looks along, giving it the road as its parameter: a procedure with a parameter, used by someone else's code.
Map apps let you avoid tolls or highways in a similar way: those roads count as worse, so the app picks them only when going around costs too much. The ripple is a simple version of what computer scientists call Dijkstra's algorithm, worked out in 1956 and published in 1959. Real map apps use the same idea with extra tricks that make it fast enough for millions of roads, and they get news while you drive, such as traffic and closed roads other drivers report.
Software engineers who build maps and delivery apps write route planners like this one, and logistics planners decide what a route should cost: time, fuel, tolls or safety. Software developers earned a median of $133,080 in May 2024 (U.S. Bureau of Labor Statistics).
Do it together
- Open mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=fam-maprobotlab-3 together and press play.
You'll know it worked when: 7 blocks in 24 minutes, through Main Street's traffic.
- In Blocks, put this road's minutes in a road costs in place of the 1. Back in Simulator, press play.
If you get stuck: The cost blocks are in the Cost drawer.
You'll know it worked when: 9 blocks in 10 minutes, around by the green avenue.
- Open Levels and start Level 7. Press play, then put the second a road costs block from Cost in place of yours and play again.
You'll know it worked when: First 10 minutes past the school; then 14 blocks, 14 minutes, and no school roads.
- Change the 10 to 2, then 3.
You'll know it worked when: With 2 the van still goes past the school; with 3 it goes around.
Talk about it
Ask these while you work together. A child who has understood it can answer them without help:
- Why did 7 blocks take more minutes than 9?
- What does the planner give the cost function, and what does it get back?
- Why does adding 10 on a school road change the route?
- Why does a penalty of 2 not keep the van away, and 3 does?
- Which route is best: the fastest or the quiet one?
For younger and older kids
Younger: For a younger child, stop after Level 5, and talk about a time the shortest way was not the fastest.
Older: For an older child, work out why 2 is not enough and 3 is: add up each route's cost.
Keep going
- In Python, write def cost(road): with an if: return road.minutes() + 3 if road.school() else road.minutes(). Check it gives the same route as the blocks.
- Find a penalty that keeps the van away in your own town on Level 9, with a school road it would really want.
- Write a cost that makes the van prefer streets over the avenue. What happens to the minutes?
Bring it into other subjects
- Math: write the total cost of each Level 7 route as an expression in p, the penalty: 10 + 2p and 14. For which p is going around cheaper?
- Social studies: why do towns slow traffic near schools? Find the school zone rules where you live.
- Science: a cost could be fuel instead of minutes. Which roads would use more fuel, and why?
- Careers: look up what a transportation planner does.
For your records
Ready to copy into an hour log, a portfolio or a transcript.
| Lesson | Map Robot Lab: What Does Best Mean? A Cost Function |
|---|---|
| Subjects | Computer science |
| Time | 45 minutes |
| What we did | Programmed a simulated delivery van's route planner with cost functions: fewest blocks, fewest minutes, and a penalty that avoided school roads, then tested penalties to find the smallest that worked. |
| Skills practiced | Cost functions; Procedures with parameters; Testing and refining |
Map Robot Lab: What Does Best Mean? A Cost Function
Activity sheet (optional)
Name: ______________________________ Date: ______________
Open the app: scan the QR code, or go to mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function
Essential question: What makes one route better than another, and who decides?
Warm-up: Is the shortest way to school always the fastest? Always the best? ________________________
- Vocabulary. Cost function, parameter, return value, penalty, optimal, trade-off: write each in your own words.
- Level 5. Every road costs 1: ____ blocks, ____ minutes. A road costs its minutes: ____ blocks, ____ minutes.
- Level 7. Minutes: ____ blocks, ____ minutes, ____ school roads. Plus 10 on a school road: ____ blocks, ____ minutes, ____ school roads.
- The smallest penalty. Penalty 1: ____ min, ____ school roads. 2: ____, ____. 3: ____, ____. 4: ____, ____. 5: ____, ____. The smallest that keeps the van away: ____.
1. Why does a penalty of 2 not keep the van away, and 3 does?
2. Which Level 7 route would you choose, and why?
Quick check
Name: ______________________________
1. Every road costs 1. The planner finds: (A) the fewest blocks (B) the fewest minutes (C) the quietest streets
2. The cost function is given: (A) the whole route (B) one road at a time (C) the goal
3. A friend wants the van to avoid Main Street. Use what you learned today to write a cost, and say how you would test it.
Answers
| Question | Answer | Why |
|---|---|---|
| 1 | A | Every road counts the same, so the lowest total is the fewest roads. |
| 2 | B | The planner calls it for each road it looks along, with that road as the parameter. |
| 3 | Open answer | Add a penalty to Main Street's roads, for example the minutes plus 10 when the road takes 5. Test it on a trip that would use Main Street, and try smaller penalties to find the smallest that works. |
If your state or umbrella school asks, this lesson covers CSTA 2017 2-AP-14, CSTA 2017 2-AP-17, CSTA 2017 1B-AP-08.