Skip to content

Commit

Permalink
testing out using a dict
Browse files Browse the repository at this point in the history
  • Loading branch information
ntalluri committed Dec 29, 2023
1 parent 472ba79 commit 24055bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 0 additions & 2 deletions graph_dir_col_flow1_c1.0.sif

This file was deleted.

2 changes: 1 addition & 1 deletion graphs/graph_dir_col/edges.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
A B 0.98 U
B C 0.77 U
A D 0.12 U
A D 0.12 D
C D 0.89 U
C E 0.59 U
C F 0.50 U
Expand Down
37 changes: 20 additions & 17 deletions minCostFlow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import argparse
from ortools.graph.python.min_cost_flow import SimpleMinCostFlow

# global dict for edge, directionality or adjacney matrix
edges_dict = dict()

def parse_nodes(node_file):
''' Parse a list of sources or targets and return a set '''
Expand All @@ -35,7 +35,6 @@ def construct_digraph(edges_file, cap):
with open(edges_file) as edges_f:
for line in edges_f:
tokens = line.strip().split()
print(tokens)
node1 = tokens[0]
if not node1 in idDict:
idDict[node1] = curID
Expand All @@ -46,17 +45,22 @@ def construct_digraph(edges_file, cap):
curID += 1
#Google's solver can only handle int weights, so round to the 100th
w = int((1-(float(tokens[2])))*100)
# grab directionality d = tokens[3]
# append edgepair: d to global dict
# might need to store it twice in both directions for undirected egde?

# if d = U:
# make a pair of directed edges
# elif d = D
# make one directed edge from node1 to node2
# else: raise Error print(f"d = {d}"")
G.add_arc_with_capacity_and_unit_cost(idDict[node1],idDict[node2], default_capacity, int(w))
G.add_arc_with_capacity_and_unit_cost(idDict[node2],idDict[node1], default_capacity, int(w))
d = tokens[3]

if d == "U":
edges_dict[(node1, node2)] = "U"
edges_dict[(node2, node1)] = "U"
G.add_arc_with_capacity_and_unit_cost(idDict[node1],idDict[node2], default_capacity, int(w))
G.add_arc_with_capacity_and_unit_cost(idDict[node2],idDict[node1], default_capacity, int(w))

elif d == "D":
edges_dict[(node1, node2)] = "D"
G.add_arc_with_capacity_and_unit_cost(idDict[node1],idDict[node2], default_capacity, int(w))
else:
raise ValueError (f"d = {d}")

print(edges_dict)

idDict["maxID"] = curID
return G,idDict

Expand Down Expand Up @@ -110,10 +114,9 @@ def write_output_to_sif(G,out_file_name,idDict):
continue
numE+=1

# check the edge in global dict and grab directionality
# d = global_dict[(node1, node2)]
# out_file.write(node1+"\t"+node2+"\t"+d+"\n")
out_file.write(node1+"\t"+node2+"\n")
d = edges_dict[(node1, node2)]
out_file.write(node1+"\t"+node2+"\t"+d+"\n")

print("Final network had %d edges" % numE)
out_file.close()

Expand Down

0 comments on commit 24055bb

Please sign in to comment.