Map Robot Lab: What Does Best Mean? A Cost Function
Level 5 of Map Robot Lab asks for the Theater in 10 minutes or less. The planner with every road costing 1 finds the fewest blocks, 7, along Main Street, and takes 24 minutes in traffic. Students write a cost function, a road costs its minutes, and the same planner finds 9 blocks in 10 minutes around by the green avenue. Level 7 is 3 pm, school is letting out, and the fastest route goes past the school. Students add 10 to a school road's cost, and the van takes 14 blocks and 14 minutes with no school roads. Then they find the smallest penalty that still keeps it away, 3, and explain why.
At a glance
Essential question
What makes one route better than another, and who decides?
Objectives
Students will be able to:
- explain why the fewest blocks is not the fastest route
- write a cost function that the planner calls for every road
- add a penalty to keep the van off school roads
- test penalties and find the smallest that works
- explain that the cost function decides what the best route is
Vocabulary
Cost function, Parameter, Return value, Penalty, Optimal, Trade-off
Standards
2-AP-14, 2-AP-17, 1B-AP-08
Time and materials
45 minutes, grades 6-8.
- One computer or Chromebook per student
- A projector
- The worksheet and exit ticket, one copy each per student
- The rubric, one copy per student if you grade the work
Prep
Make copies of the worksheet and exit ticket. Before class, open mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=tpt-maprobotlab-3 once on a student computer, which checks that your school's filter allows the app, and follow the short guide yourself: about ten minutes.
Assessment
- Formative: the questions under What to look for, asked while students work, and the exit ticket (answer key included).
- Summative: the worksheet, scored with the rubric.
Technology and privacy
Runs in any browser, Chromebooks included, with nothing to install. Students need no account and no email: the link works signed out, and nothing they do is stored on our server unless they choose to save.
mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=tpt-maprobotlab-3mytekdev.com/tools/map-robot-lab/lessons/what-does-best-mean-a-cost-function/startEach student gets their own copy, and the same link reopens it next time. How to set up a classLesson sequence
| Minutes | Stage | What you do | What students do |
|---|---|---|---|
| 0 to 5 | Warm-up Hook | Show the title, essential question and Today I can slides (about 1 minute). Then the warm-up: is the shortest way always the fastest, or the best? | Answer the warm-up on the worksheet. |
| 5 to 13 | Direct instruction (I do) Model it | Project Level 5 with the starter. 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. Open Blocks: plan a route to the goal, with a road costs return 1. Press play: 7 blocks in 24 minutes, along Main Street. Show the Fewest blocks slide, then ask what the cost should be if we care about time. | Predict, watch, and fill in the vocabulary on the worksheet. |
| 13 to 25 | Guided practice (We do) Blocks, minutes, quiet streets | Students open the link or scan the QR code. A short guide walks them through changing the cost to this road's minutes on Level 5, then Level 7, where the second a road costs block in the Cost drawer adds 10 on a school road. 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. Watch for: Students with two a road costs blocks. Only one cost should be in the program: take the old one out when the new one goes in. | Follow the guide. Write each run's blocks, minutes and school roads on the worksheet. |
| 25 to 37 | Independent practice (You do) The smallest penalty | On Level 7, students change the 10 to 1, 2, 3, 4 and 5, run each, and fill in the table. Then they explain the jump between 2 and 3. Walk the room with the What to look for questions. If they ask: "With 2 the van still goes past the school; with 3 it goes around. What changed in the sums?" The jump It's working when: Past the school: 10 minutes, with 2 school roads. Around: 14 minutes, none. With a penalty of 2 the school route costs 10 + 2 x 2 = 14, a tie with going around; the tie goes the school way. With 3 it costs 16, more than 14, so the van goes around. | Test five penalties, fill in the table, and explain the result. |
| 37 to 45 | Closure Who decides? And exit ticket | Show the closure slide and take answers. Name the idea: the cost function is a procedure with a parameter, and it decides what best means. Hand out the exit ticket. | Discuss, then answer the exit ticket. |
Before you teach: a ten-minute walkthrough
Open mytekdev.com/try/maprobotlab?lesson=what-does-best-mean-a-cost-function&ref=tpt-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.
Background for the teacher
The planner finds the route with the lowest total cost, and the cost function says what each road costs. Students 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).
What to look for
Ask these while students work. A student who has understood the lesson 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?
Cross-curricular extensions
- 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.
Vocabulary
| Cost function | A procedure that says what a road counts as. The planner calls it for every road it looks along. |
| Parameter | The value a procedure is given. The cost function is given one road at a time. |
| Return value | What a procedure gives back. The cost function returns a number above 0. |
| Penalty | Extra cost added to something you want to avoid, like 10 on a school road. |
| Optimal | The best by some measure. The fewest blocks and the fewest minutes can be different routes. |
| Trade-off | Giving up some of one thing to get more of another: more minutes for no school roads. |
Differentiation
English learners
- Keep the vocabulary slide up, and point at the van, the road numbers or the Trip log as you say each word: the picture carries the meaning.
- Give a sentence frame on a card: When a road costs ____, the best route is the ____ one.
- Point at a road's number on the map as you say "this road's minutes".
Students with IEPs or 504 plans
- Pair students, one at the keyboard and one recording results.
- Give the penalty table with the values already written in.
- Where the IEP allows a modified assignment, score the first two rubric rows only.
Students who need more support
- Do Level 5 together on the projector before students start.
- Write the three costs on the board, 1, this road's minutes, minutes plus 10 on a school road, with the route each one finds.
- The card says what to try after every run that misses.
Students ready for more
- 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?
Adapting for other grades
Younger students: In grade 5, do Level 5 together on the projector and Level 7 with the ready-made block, without the penalty table. Score the first two rubric rows, total 8, and 6 meets the objectives.
Older students: In high school, write the cost in Python as a function with a parameter, and compare weighted costs in real systems: fuel, tolls, emissions. Then go on to Plans change, the next lesson in this series.
Map Robot Lab: What Does Best Mean? A Cost Function
Student worksheet
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?
Map Robot Lab: What Does Best Mean? A Cost Function
Exit ticket
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.
Answer key: exit ticket
| 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. |
Worksheet: what students should find
| Blank | What the app shows |
|---|---|
| Level 5 | Every road 1: 7 blocks, 24 minutes. Minutes: 9 blocks, 10 minutes. |
| Level 7 | Minutes: 10 blocks, 10 minutes, 2 school roads. Plus 10: 14 blocks, 14 minutes, 0 school roads. |
| The smallest penalty | 1: 10 min, 2 school roads. 2: 10, 2. 3: 14, 0. 4: 14, 0. 5: 14, 0. The smallest: 3. |
| Question 1 | The school route costs 10 plus the penalty on 2 school roads. With 2 that is 14, a tie with going around; with 3 it is 16, more than 14. |
| Question 2 | Open answer, justified by the minutes and the school roads. |
Rubric
Score each row from 1 to 4.
Name: ______________________________
| Criteria | 4 Exceeds | 3 Meets | 2 Approaching | 1 Beginning |
|---|---|---|---|---|
| Fewest blocks or fastest | Explains with the run's numbers why 7 blocks took 24 minutes and 9 blocks took 10. | Explains that the fewest blocks went through slow roads. | Says one was faster, without saying why. | No explanation. |
| The cost function | Writes the minutes cost and the school penalty, and explains that the planner calls the cost for each road. | Writes the minutes cost and the school penalty, and both levels pass. | Writes one cost. | Does not change the cost. |
| Testing penalties | Tests five penalties, finds 3, and explains the jump with the sums. | Tests five penalties and finds the smallest that works. | Tests some penalties. | Does not test. |
Total: ____ / 12. A score of 9 or more meets the objectives.
Standards
The lesson is built for CSTA 2-AP-14: the cost is a procedure with a parameter, a road, that the planner calls for every road, and changing it reuses the same planner for three meanings of best. Testing a range of penalties and refining the cost until the van avoids the school is 2-AP-17.
| Framework | Code | Grades | Standard | How the lesson meets it |
|---|---|---|---|---|
| CSTA 2017 | 2-AP-14 | 6-8 | Create procedures with parameters to organize code and make it easier to reuse. | Students write a road costs, a procedure with one parameter (the road) that returns a number, and the planner reuses it for every road on Levels 5 and 7. |
| CSTA 2017 | 2-AP-17 | 6-8 | Systematically test and refine programs using a range of test cases. | Students test penalties of 1 to 5 on Level 7, record each result, and refine the cost to the smallest that works. |
| CSTA 2017 | 1B-AP-08 | 3-5 | Compare and refine multiple algorithms for the same task and determine which is the most appropriate. | Students compare three costs for the same planner, fewest blocks, fewest minutes and no school roads, and decide which is the most appropriate for each trip. |
Checked December 2026 against: CSTA K-12 Computer Science Standards, Revised 2017.
Optional: save student work in a free class
The whole lesson runs at the link on the worksheet, with no accounts and no sign-up.
To keep students' work, and see it all in one place, make a free class. It takes about ten minutes, once, and covers every lesson in this series: mytekdev.com/tools/class-setup
Students in a class open mytekdev.com/tools/map-robot-lab/lessons/what-does-best-mean-a-cost-function/start instead of the worksheet link. Each one gets their own copy of the starter, and the same link reopens it next time.