|
| 1 | +import motile |
| 2 | +import networkx as nx |
| 3 | +from motile.costs import EdgeSelection, NodeSelection |
| 4 | + |
| 5 | + |
| 6 | +def test_empty_graph() -> None: |
| 7 | + """Test that solving an empty graph does not error and returns empty solution.""" |
| 8 | + nx_graph = nx.DiGraph() |
| 9 | + graph = motile.TrackGraph(nx_graph) |
| 10 | + |
| 11 | + solver = motile.Solver(graph) |
| 12 | + solver.add_cost(NodeSelection(constant=-1)) |
| 13 | + solver.add_cost(EdgeSelection(constant=-1)) |
| 14 | + |
| 15 | + # Should not error |
| 16 | + solver.solve() |
| 17 | + solution_graph = solver.get_selected_subgraph() |
| 18 | + |
| 19 | + # Solution should be empty |
| 20 | + assert len(solution_graph.nodes) == 0 |
| 21 | + assert len(solution_graph.edges) == 0 |
| 22 | + |
| 23 | + |
| 24 | +def test_graph_with_no_edges() -> None: |
| 25 | + """Test that solving a graph with nodes but no edges does not error.""" |
| 26 | + cells = [ |
| 27 | + {"id": 0, "t": 0}, |
| 28 | + {"id": 1, "t": 0}, |
| 29 | + {"id": 2, "t": 1}, |
| 30 | + ] |
| 31 | + |
| 32 | + nx_graph = nx.DiGraph() |
| 33 | + nx_graph.add_nodes_from([(cell["id"], cell) for cell in cells]) |
| 34 | + # No edges added |
| 35 | + |
| 36 | + graph = motile.TrackGraph(nx_graph) |
| 37 | + |
| 38 | + solver = motile.Solver(graph) |
| 39 | + solver.add_cost(NodeSelection(constant=-1)) |
| 40 | + solver.add_cost(EdgeSelection(constant=-1)) |
| 41 | + |
| 42 | + # Should not error |
| 43 | + solver.solve() |
| 44 | + solution_graph = solver.get_selected_subgraph() |
| 45 | + |
| 46 | + # All nodes should be selected due to negative cost |
| 47 | + assert set(solution_graph.nodes.keys()) == {0, 1, 2} |
| 48 | + # No edges should be selected (none exist) |
| 49 | + assert len(solution_graph.edges) == 0 |
0 commit comments