For a teacher with a class? The classroom edition has the full plan, slides and standards.

Homeschooling PDF

Family edition

Map Robot Lab: What Does Best Mean? A Cost Function

Ages 11-14 ยท about 45 minutes, in one sitting

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

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.

  1. 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.
  2. 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.
  3. 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.
  4. Penalties. Plus 1 or 2: still 10 minutes past the school. Plus 3, 4, 5, 10 or 100: 14 minutes, no school roads.
  5. 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.
A cost must be a number above 0, or the program stops with a line naming the road. A penalty is not a ban: in a town where going around cost more than the penalty adds, the van would still take the school road. Level 9, Your town, lets children build one.

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

  1. 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.

  2. 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.

  3. 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.

  4. 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:

The fourth question is the one that matters. A child who adds up both routes, 10 + 2 x 2 = 14 against 14, has understood that the planner compares totals, not single roads.

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

Bring it into other subjects

For your records

Ready to copy into an hour log, a portfolio or a transcript.

LessonMap Robot Lab: What Does Best Mean? A Cost Function
SubjectsComputer science
Time45 minutes
What we didProgrammed 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 practicedCost 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? ________________________

  1. Vocabulary. Cost function, parameter, return value, penalty, optimal, trade-off: write each in your own words.
  2. Level 5. Every road costs 1: ____ blocks, ____ minutes. A road costs its minutes: ____ blocks, ____ minutes.
  3. Level 7. Minutes: ____ blocks, ____ minutes, ____ school roads. Plus 10 on a school road: ____ blocks, ____ minutes, ____ school roads.
  4. 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

QuestionAnswerWhy
1AEvery road counts the same, so the lowest total is the fewest roads.
2BThe planner calls it for each road it looks along, with that road as the parameter.
3Open answerAdd 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.