Created
July 28, 2021 19:55
-
-
Save yitonghe00/9b918014559c8207588404ac11841fbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here | |
function yourRobot({place, parcels}, route) { | |
if (route.length == 0) { | |
const routes = parcels.map(parcel => { | |
let route, pickingUp = false; | |
if (parcel.place != place) { | |
route = findRoute(roadGraph, place, parcel.place); | |
pickingUp = true; | |
} else { | |
route = findRoute(roadGraph, place, parcel.address); | |
} | |
return { route, pickingUp }; | |
}); | |
const getRouteRating = ({ route, pickingUp }) => (pickingUp ? 0.5 : 0) - route.length; | |
route = routes.reduce((a, b) => getRouteRating(a) > getRouteRating(b) ? a : b).route; | |
} | |
return {direction: route[0], memory: route.slice(1)}; | |
} | |
runRobotAnimation(VillageState.random(), yourRobot, []); | |
countSteps = (state, robot, memory) => { | |
for (let turn = 0;; turn++) { | |
if (state.parcels.length == 0) { | |
return turn; | |
} | |
let action = robot(state, memory); | |
state = state.move(action.direction); | |
memory = action.memory; | |
} | |
} | |
function compareRobots(robot1, memory1, robot2, memory2) { | |
// Your code here | |
let steps1 = 0, steps2 = 0; | |
for (let i = 0; i < 100; i++) { | |
const task = VillageState.random(); | |
steps1 += countSteps(task, robot1, memory1); | |
steps2 += countSteps(task, robot2, memory2); | |
} | |
console.log(`Robot1 usually takes ${steps1 / 100} steps`); | |
console.log(`Robot2 usually takes ${steps2 / 100} steps`); | |
} | |
compareRobots(yourRobot, [], goalOrientedRobot, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment