Skip to content

Commit

Permalink
an example comparing them!!
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Henkel <[email protected]>
  • Loading branch information
ct2034 committed Nov 3, 2024
1 parent 68b3af8 commit 3842a82
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ roadmaps/benchmark_plots/benchmark_*.png

# build folder for cvt
roadmaps/cvt/build/*
path_save.pkl
81 changes: 81 additions & 0 deletions planner/cbs-ta_vs_tcbs/evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import numpy as np

from planner.mapf_implementations.plan_cbs_ta import plan as cbs_ta_plan
from planner.tcbs.plan import plan as tcbs_plan


def tcbs_with_single_goals(gridmap, starts, goals, timeout):
"""
Adapter to call TCBS with single goals.
(Normally, TCBS defines jobs as transport tasks,
i.e., with start and goal locations.)
"""
# jobs must be transport tasks
jobs = []
for g in goals:
jobs.append((tuple(g), tuple(g), 0)) # start = goal = g, time = 0
# map must be time-expanded
t_max = gridmap.shape[0] * gridmap.shape[1] * 2
_map = np.repeat(gridmap[:, :, np.newaxis], t_max, axis=2)
return tcbs_plan(
agent_pos=[tuple(s) for s in starts],
jobs=jobs,
alloc_jobs=[],
idle_goals=[],
grid=_map,
)


def execute_both_planners(gridmap, starts, goals, timeout):
cbs_ta_res = cbs_ta_plan(gridmap, starts, goals, timeout)
tcbs_res = tcbs_with_single_goals(gridmap, starts, goals, timeout)
return cbs_ta_res, tcbs_res


def get_cost_from_tcbs_res(tcbs_res):
"""
Return the sum of costs from TCBS result.
This must be comparable to the cost from CBS-TA.
So we have to ignore the last step, where the agents stay at the goal.
"""
_, _, paths = tcbs_res
cost = 0
for path in paths:
to_start, _ = path
cost += len(to_start) - 1

return cost


def get_cost_from_cbs_ta_res(cbs_ta_res):
"""
Return the sum of costs from CBS-TA result.
"""
return cbs_ta_res["statistics"]["cost"]


if __name__ == "__main__":
gridmap = np.array([[0] * 3] * 3)
starts = [[0, 0], [0, 1], [0, 2]]
goals = [[2, 0], [2, 1], [1, 2]]
res = execute_both_planners(gridmap, starts, goals, 10)
res_cbs_ta, res_tcbs = res

print("=" * 10)

print("CBS-TA")
print(res_cbs_ta)
print("-" * 10)

print("Cost from CBS-TA")
print(get_cost_from_cbs_ta_res(res_cbs_ta))
print("-" * 10)

print("TCBS")
print(res_tcbs)
print("-" * 10)

print("Cost from TCBS")
print(get_cost_from_tcbs_res(res_tcbs))
print("=" * 10)
4 changes: 2 additions & 2 deletions planner/mapf_implementations/plan_cbs_ta.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def call_subprocess(fname_infile, fname_outfile, timeout):
return out_data


def plan_in_gridmap(gridmap: np.ndarray, starts, goals, timeout):
def plan(gridmap: np.ndarray, starts, goals, timeout):
# solving memoryview: underlying buffer is not C-contiguous
gridmap = np.asarray(gridmap, order="C")
uuid_str = str(uuid.uuid1())
Expand All @@ -144,5 +144,5 @@ def plan_in_gridmap(gridmap: np.ndarray, starts, goals, timeout):
gridmap = np.array([[0, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 1]])
starts = [[1, 2], [3, 2], [2, 1]]
goals = [[3, 0], [2, 2], [0, 0]]
res = plan_in_gridmap(gridmap, starts, goals, 10)
res = plan(gridmap, starts, goals, 10)
print(res)
4 changes: 2 additions & 2 deletions planner/mapf_implementations/plan_cbs_ta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import numpy as np

from planner.mapf_implementations.plan_cbs_ta import plan_in_gridmap
from planner.mapf_implementations.plan_cbs_ta import plan


class TestPlanCBS_TA(unittest.TestCase):
def test_basic_example(self):
gridmap = np.array([[0, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 1]])
starts = [[1, 2], [3, 2], [2, 1]]
goals = [[3, 0], [2, 2], [0, 0]]
res = plan_in_gridmap(gridmap, starts, goals, 10)
res = plan(gridmap, starts, goals, 10)

self.assertEqual(res["statistics"]["cost"], 6)
self.assertEqual(res["statistics"]["makespan"], 3)
Expand Down
5 changes: 4 additions & 1 deletion planner/tcbs/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def plan(
def alloc_threads():
global pool
n = multiprocessing.cpu_count()
n = min(n, 8)
pool = multiprocessing.Pool(processes=n)
return pool

Expand Down Expand Up @@ -697,7 +698,9 @@ def get_paths(_condition: dict, _state: tuple):
}
)
global pool
res = list(pool.map(get_paths_for_agent, valss))
res_out = pool.map(get_paths_for_agent, valss)
# res_out = map(get_paths_for_agent, valss)
res = list(res_out)
for r in res:
if not r:
return False
Expand Down

0 comments on commit 3842a82

Please sign in to comment.