Skip to content

Commit

Permalink
updated mcf code
Browse files Browse the repository at this point in the history
  • Loading branch information
ntalluri committed Jan 29, 2024
1 parent dab0a3e commit 5d1f66e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ More details of the algorithm can be found in:
Chris S Magnano, Anthony Gitter.
*npj Systems Biology and Applications*, 7:12, 2021.

## Edge Handling
The code is designed to process both undirected and directed edges, prioritizing directed edges in scenarios where an equivalent undirected edge exists and selecting higher edge weights in the case of duplicate edges.

## Input Format Example
The input should be formatted as follows, with columns for node1, node2, rank, and direction:
```
A B 0.9 U
B A 0.1 D
...
```
In this format, "U" represents an undirected edge, and "D" represents a directed edge.

## Dependencies

Google's [OR-Tools library](https://developers.google.com/optimization/flow/mincostflow) is required to run this script.
Expand All @@ -32,3 +44,8 @@ Python 3 is required to run this script
> --output Prefix for all output files.
>
> --capacity The amount of flow which can pass through a single edge.
## Testing
`python test_minCostFlow.py`

The code executes two sets of graph series, namely the 'graph series' and the 'test series' The graphs series of graphs are used to check the code's correctness. Except for internal tiebreaking by the solver, each result is deterministic. The tests series of graphs are used to verify whether the code is executing appropriately depending on distinct edge cases. The expected results for both series can be found in graphs/correct_outputs.txt for the graph series and tests/correct_outputs.txt for the test series.
8 changes: 4 additions & 4 deletions minCostFlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def construct_digraph(edges_file, cap):

if d == "D":
if edge in directed_dict:
if w > directed_dict[edge]:
if w < directed_dict[edge]:
directed_dict[edge] = w
elif sorted_edge in undirected_dict:
del undirected_dict[sorted_edge]
Expand All @@ -65,13 +65,13 @@ def construct_digraph(edges_file, cap):
if edge not in directed_dict and sorted_edge not in directed_dict and sorted_edge not in undirected_dict:
undirected_dict[sorted_edge] = w
elif sorted_edge in undirected_dict:
if w > undirected_dict[sorted_edge]:
if w < undirected_dict[sorted_edge]:
undirected_dict[sorted_edge] = w
else:
raise ValueError (f"Cannot add edge: d = {d}")

# print("undirected_dict: ", undirected_dict)
# print("directed_dict: ", directed_dict)
print("undirected_dict: ", undirected_dict)
print("directed_dict: ", directed_dict)
# go through and add the edges from directed_dict and undirected_dict to G
for key, value in directed_dict.items():
G.add_arc_with_capacity_and_unit_cost(idDict[key[0]],idDict[key[1]], default_capacity, int(value))
Expand Down
4 changes: 2 additions & 2 deletions test_minCostFlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
command = "python"
script = "minCostFlow.py"

print("testing code functionality")
print("TEST SERIES")
for i in range (1,8):

print("test: ",i)
Expand All @@ -21,7 +21,7 @@
subprocess.run(cmd)


print("\ntesting code correctness")
print("\nGRAPHS SERIES")
for i in range (1,14):
print("graph: ",i)
args = [
Expand Down
24 changes: 14 additions & 10 deletions tests/correct_outputs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ B C D
test2: check if higher edge weight is selected for the same directed edge from the input
Output:
A B D
B D D
B C D

test3: If a directed edge is present in the input and an undirected edge from that edge already exists, the directed edge is prioritized and added to directed_dict and the undirected edge is deleted from undirected_dict.
A C D
C D D
Output:
B C D
A B D

test4: check if unique undirected edges are added to undirected_dict
A B D
B D D
Output:
A B U
B C U

test5: check that an undirected edge is not added if a directed edge of that edge already exists
A B U
B D U
Output:
A B D
B C D

test6: check if higher edge weight is selected for the same undirected edge from the input
Output:
A B U
B D U
B C U

test7: check that code still runs and outputs an error message with an empty edges.txt
A B U
B D U
Output:
N/A
4 changes: 2 additions & 2 deletions tests/test2/edges.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A B 0.2 D
B C 0.2 D
A B 0.1 D
B C 0.3 D
A B 0.9 D
B C 0.1 D
1 change: 0 additions & 1 deletion tests/test5/edges.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
A B 0.1 D
A B 0.1 U
B A 0.1 U
B A 0.1 D
B C 0.1 D
B C 0.1 U
4 changes: 2 additions & 2 deletions tests/test6/edges.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A B 0.2 U
B C 0.2 U
A B 0.1 U
B C 0.3 U
A B 0.9 U
B C 0.1 U

0 comments on commit 5d1f66e

Please sign in to comment.