Skip to content

Commit

Permalink
Converting test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
tkralphs committed Mar 26, 2019
1 parent 6edb6e0 commit 02d0316
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 45 deletions.
37 changes: 21 additions & 16 deletions test/compare_gimpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Compare running times of gimpy and regimpy (reimplementation) and theoretical
for following algorithms DFS, BFS, augmenting flow, cycle canceling, simplex.
'''
from __future__ import division
from __future__ import print_function
from builtins import str
from builtins import range
from past.utils import old_div


#TODO(aykut)
Expand Down Expand Up @@ -345,65 +350,65 @@ def produce_graphs():
theoretical.
'''
# get number of runs, common for all graphs
n = range(len(run_time['DFS']['theoretical']))
n = list(range(len(run_time['DFS']['theoretical'])))
for a in algo:
# ========= reimplementation
# create graph for algorithm a
scale = run_time[a]['regimpy'][-1] / run_time[a]['theoretical'][-1]
scale = old_div(run_time[a]['regimpy'][-1], run_time[a]['theoretical'][-1])
scaled_theoretical = [scale*t for t in run_time[a]['theoretical']]
pyplot.plot(n, run_time[a]['regimpy'], 'bs', label='actual runtime')
pyplot.plot(n, scaled_theoretical, 'g^', label='theoretical runtime')
pyplot.legend(loc='lower right')
pyplot.title('regimpy '+a+' running time vs theoretical')
pyplot.xlabel('instances')
pyplot.ylabel('running time')
print a+"_regimpy.png written to disk."
print(a+"_regimpy.png written to disk.")
pyplot.savefig(a+'_regimpy.png')
pyplot.close()
# ========= old gimpy
# create graph for algorithm a
scale = run_time[a]['gimpy'][-1] / run_time[a]['theoretical'][-1]
scale = old_div(run_time[a]['gimpy'][-1], run_time[a]['theoretical'][-1])
scaled_theoretical = [scale*t for t in run_time[a]['theoretical']]
pyplot.plot(n, run_time[a]['gimpy'], 'bs', label='actual runtime')
pyplot.plot(n, scaled_theoretical, 'g^', label='theoretical runtime')
pyplot.legend(loc='lower right')
pyplot.title('old gimpy '+a+' running time vs theoretical')
pyplot.xlabel('instances')
pyplot.ylabel('running time')
print a+"_gimpy.png written to disk."
print(a+"_gimpy.png written to disk.")
pyplot.savefig(a+'_gimpy.png')
pyplot.close()

if __name__=='__main__':
nr_seed = 1
for i in range(nr_seed):
print "Seed", i
print("Seed", i)
for gen in dense_generator:
print gen
print(gen)
# unzip generator
(numnodes, density, demand_numnodes, supply_numnodes,
demand_range, cost_range, capacity_range) = gen
# generate graphs
#g, rg = generate_graph(i+1)
g = None
rg = generate_graph(i+1)
print "Testing DFS..."
print("Testing DFS...")
test_DFS(g, rg)
print "Testing BFS..."
print("Testing BFS...")
test_BFS(g, rg)
print "Testing Dijkstra..."
print("Testing Dijkstra...")
test_dijkstra(g, rg)
print "Testing Kruskal..."
print("Testing Kruskal...")
test_kruskal(g, rg)
print "Testing Prim..."
print("Testing Prim...")
test_prim(g, rg)
print "Testing PreflowPush..."
print("Testing PreflowPush...")
test_preflow_push(g, rg)
print "Testing augmenting path..."
print("Testing augmenting path...")
test_augmenting_path(g, rg)
print "Testing cycle canceling..."
print("Testing cycle canceling...")
test_cycle_canceling(g, rg)
print "Testing network simplex..."
print("Testing network simplex...")
test_network_simplex(g, rg)
insert_theoretical_runing_times(rg)
write_result_files()
Expand Down
33 changes: 19 additions & 14 deletions test/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
This script runs algorithms provided by gimpy. Measures running times of
algorithms and compares it to theoretical running times (after scaling).
'''
from __future__ import division
from __future__ import print_function
from builtins import str
from builtins import range
from past.utils import old_div

try:
from src.gimpy import Graph, DIRECTED_GRAPH
Expand Down Expand Up @@ -304,11 +309,11 @@ def produce_graphs():
theoretical.
'''
# get number of runs, common for all graphs
n = range(len(run_time['DFS']['theoretical']))
n = list(range(len(run_time['DFS']['theoretical'])))
for a in algo:
# ========= gimpy
# create graph for algorithm a
scale = run_time[a]['gimpy'][-1] / run_time[a]['theoretical'][-1]
scale = old_div(run_time[a]['gimpy'][-1], run_time[a]['theoretical'][-1])
scaled_theoretical = [scale*t for t in run_time[a]['theoretical']]
pyplot.plot(n, run_time[a]['gimpy'], 'bs', label='actual runtime')
pyplot.plot(n, scaled_theoretical, 'g^', label='theoretical runtime')
Expand All @@ -317,36 +322,36 @@ def produce_graphs():
pyplot.xlabel('instances')
pyplot.ylabel('running time')
pyplot.savefig(a+'.png')
print a+".png written to disk."
print(a+".png written to disk.")
pyplot.close()

if __name__=='__main__':
nr_seed = 1
for i in range(nr_seed):
print "Seed", i
print("Seed", i)
for gen in dense_generator:
print gen
print(gen)
#gen = (numnodes, density, demand_numnodes, supply_numnodes,
# demand_range, cost_range, capacity_range)
# generate graphs
rg = generate_graph(i+1, gen)
print "Testing DFS..."
print("Testing DFS...")
test_DFS(rg)
print "Testing BFS..."
print("Testing BFS...")
test_BFS(rg)
print "Testing Dijkstra..."
print("Testing Dijkstra...")
test_dijkstra(rg)
print "Testing Kruskal..."
print("Testing Kruskal...")
test_kruskal(rg)
print "Testing Prim..."
print("Testing Prim...")
test_prim(rg)
print "Testing PreflowPush..."
print("Testing PreflowPush...")
test_preflow_push(rg)
print "Testing augmenting path..."
print("Testing augmenting path...")
test_augmenting_path(rg)
print "Testing cycle canceling..."
print("Testing cycle canceling...")
test_cycle_canceling(rg)
print "Testing network simplex..."
print("Testing network simplex...")
test_network_simplex(rg)
insert_theoretical_runing_times(rg)
write_result_files()
Expand Down
3 changes: 2 additions & 1 deletion test/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
it is teaching/research oriented. This script demonstrates the Graph class's
capability as a graphviz interface.
'''
from __future__ import print_function

from gimpy import Graph, DIRECTED_GRAPH

Expand Down Expand Up @@ -44,6 +45,6 @@
# create second cluster
c.create_cluster(['b0', 'b1', 'b2', 'b3'], cluster_attrs, node_attrs)
# print graph in dot language to stdout
print c.to_string()
print(c.to_string())
c.set_display_mode('pygame')
c.display()
7 changes: 5 additions & 2 deletions test/test_cycle_canceling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'''
tests if cycle canceling method works properly.
'''
from __future__ import print_function
from builtins import str
from builtins import range

from test_algorithms import generate_graph

Expand All @@ -17,7 +20,7 @@

if __name__=='__main__':
generator = (10, 0.8, 3, 2, (5,10), (0,5), (100,200))
print 'Seed'.ljust(5), 'simplex'.ljust(8), 'cycle canceling'
print('Seed'.ljust(5), 'simplex'.ljust(8), 'cycle canceling')
for seed in range(10):
# cycle canceling flows
cc_flows = {}
Expand All @@ -39,4 +42,4 @@
cost_e = g.get_edge_attr(e[0], e[1], 'cost')
s_cost += s_flows[e]*cost_e
cc_cost += cc_flows[e]*cost_e
print str(seed).ljust(5), str(s_cost).ljust(8), str(cc_cost)
print(str(seed).ljust(5), str(s_cost).ljust(8), str(cc_cost))
21 changes: 11 additions & 10 deletions test/test_display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# import classes
from gimpy import Graph, Tree, BinaryTree
# import dependency related globals. All these dependencies are optional.
Expand All @@ -7,25 +8,25 @@
from gimpy import XDOT_INSTALLED, ETREE_INSTALLED

if PYGAME_INSTALLED:
print 'Pygame is installed.'
print('Pygame is installed.')
elif not PYGAME_INSTALLED:
print 'Warning: Pygame can not found.'
print('Warning: Pygame can not found.')
if DOT2TEX_INSTALLED:
print 'Dot2tex is installed.'
print('Dot2tex is installed.')
elif not DOT2TEX_INSTALLED:
print 'Warning: Dot2tex can not found.'
print('Warning: Dot2tex can not found.')
if PIL_INSTALLED:
print 'PIL (Python Imaging Library) is installed.'
print('PIL (Python Imaging Library) is installed.')
elif not PIL_INSTALLED:
print 'Warning: PIL (Python Imaging Library) can not found.'
print('Warning: PIL (Python Imaging Library) can not found.')
if XDOT_INSTALLED:
print 'Xdot is installed.'
print('Xdot is installed.')
elif not XDOT_INSTALLED:
print 'Warning: Xdot can not found.'
print('Warning: Xdot can not found.')
if ETREE_INSTALLED:
print 'Etree is installed.'
print('Etree is installed.')
elif not ETREE_INSTALLED:
print 'Warning: Etree can not found.'
print('Warning: Etree can not found.')

if __name__=="__main__":
g = Graph(display='off', layout='dot')
Expand Down
3 changes: 2 additions & 1 deletion test/test_strong_components.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from gimpy import Graph, DIRECTED_GRAPH

def generate_test_instance1():
Expand Down Expand Up @@ -35,5 +36,5 @@ def generate_test_instance2():
#g.label_strong_component(0)
g.tarjan()
for n in g.get_node_list():
print n, g.get_node_attr(n, 'component')
print(n, g.get_node_attr(n, 'component'))
g.display()
3 changes: 2 additions & 1 deletion test/test_tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from gimpy import BinaryTree

if __name__=='__main__':
Expand All @@ -16,7 +17,7 @@
t.dfs()
# test bfs
t.bfs()
print 'off display done'
print('off display done')
# test print_nodes(display='pygame')
t.print_nodes(display='pygame')
# test dfs(display='pygame')
Expand Down

0 comments on commit 02d0316

Please sign in to comment.