@@ -12,79 +12,22 @@ def measure_resolution_time(flow_graph):
1212 max_flow = flow_graph .get_max_flow ()
1313 end_time = time .time ()
1414 resolution_time = end_time - start_time
15- return resolution_time , max_flow
16- def process_instances (directory , num_iterations ):
17- instances = os .listdir (directory )
18- results = []
19- nodes = 0
20- edges = 0
21- for instance in instances :
22- file_path = os .path .join (directory , instance )
23- y = get_instance_size (file_path )
24- nodes += y [0 ]
25- edges += y [1 ]
26- # if instance[8] == "-":
27- # continue
28- # for _ in range(num_iterations):
29- # file_path = os.path.join(directory, instance)
30- # flow_graph = parse_file(file_path)
31- # resolution_times = []
32- # max_flows = []
33- # resolution_time, max_flow = measure_resolution_time(flow_graph)
34- # results.append((file_path, resolution_time, max_flow))
35- print (nodes / len (instances ), edges / len (instances ))
36- return results
37-
38-
39-
40- def analyze_results (results ):
41- # Separate the results by instance size
42- size_results = {}
43- for instance , resolution_time , max_flow in results :
44- num_nodes , num_edges = get_instance_size (instance ) # Implement a function to extract the number of nodes and edges from the instance name
45- size = (num_nodes , num_edges )
46- if size not in size_results :
47- size_results [size ] = []
48- size_results [size ].append (resolution_time )
49-
50- # Calculate average resolution time and standard deviation for each instance size
51- sizes = []
52- avg_times = []
53- std_devs = []
54- for size , times in size_results .items ():
55- sizes .append (size )
56- avg_time = statistics .mean (times )
57- avg_times .append (avg_time )
58- std_dev = statistics .stdev (times )
59- std_devs .append (std_dev )
60-
61- # Sort the sizes in ascending order based on the number of nodes
62- sizes .sort (key = lambda s : s [0 ])
63-
64- # Prepare labels for the x-axis
65- x_labels = [f"Nodes: { size [0 ]} , Edges: { size [1 ]} " for size in sizes ]
66-
67- # Plot the results
68- plt .errorbar (range (len (sizes )), avg_times , yerr = std_devs , fmt = 'o-' , label = 'Method X' ) # Replace 'Method X' with the appropriate method name
69- plt .xlabel ('Instance Size' )
70- plt .ylabel ('Resolution Time' )
71- plt .title ('Resolution Time Comparison' )
72- plt .xticks (range (len (sizes )), x_labels , rotation = 45 )
73- plt .legend ()
74- plt .grid (True )
75- plt .tight_layout ()
76- plt .show ()
77-
78- def get_instance_size (instance ):
79- with open (instance , 'r' ) as file :
80- lines = file .readlines ()
81-
82- num_nodes = int (lines [0 ].split ()[1 ])
83- num_edges = int (lines [3 ].split ()[1 ])
84-
85- return num_nodes , num_edges
86-
87-
15+ return resolution_time
16+ def process_instances ():
17+ for n in range (1 ,15 ):
18+ results = []
19+ for nodes in range (100 , 1600 , 100 ):
20+ for density in range (1 , 4 ):
21+ instance = f"inst-{ nodes } -0.{ density } .txt"
22+ file_path = os .path .join (directory , instance )
23+ flow_graph = parse_file (file_path )
24+ resolution_time = measure_resolution_time (flow_graph )
25+ results .append (resolution_time )
26+ export_results (results ,n )
27+
28+ def export_results (results ,n ):
29+ with open ("resPython" + str (n )+ ".txt" , "w" ) as file :
30+ for result in results :
31+ file .write (str (result )+ "\n " )
8832directory = "Instances"
89- results = process_instances (directory ,5 )
90- analyze_results (results )
33+ results = process_instances ()
0 commit comments