Skip to content

Commit

Permalink
Added progress bars to option generators
Browse files Browse the repository at this point in the history
  • Loading branch information
arunchaganty committed Oct 11, 2011
1 parent 7a7128b commit fff0cb3
Showing 1 changed file with 74 additions and 4 deletions.
78 changes: 74 additions & 4 deletions src/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import numpy as np
import networkx as nx
import util
import sys
import pdb

from ProgressBar import ProgressBar

class Environment:
"""Environment represented as an MDP"""
S = 0
Expand Down Expand Up @@ -222,8 +225,27 @@ def make_options_from_random_nodes( g, gr, count ):
random.shuffle( nodes )
nodes = nodes[ :count ]

# Get paths to node
return [ OptionEnvironment.make_point_option( g, gr, n ) for n in nodes ]
print "Building options...\n\n"
progress = ProgressBar( 0, count, mode='fixed' )
# Needed to prevent glitches
print progress, "\r",
oldprog = str(progress)

options = []
for n in nodes:
options.append( OptionEnvironment.make_point_option( g, gr, n ) )


# print progress
progress.increment_amount()
if oldprog != str(progress):
print progress, "\r",
sys.stdout.flush()
oldprog=str(progress)

print "\n\n"

return options

@staticmethod
def make_options_from_betweenness( g, gr, count ):
Expand All @@ -241,8 +263,27 @@ def make_options_from_betweenness( g, gr, count ):
local_maximas.sort( key = lambda (x,v): -v )
local_maximas = [ x for (x,v) in local_maximas[ :count ] ]

# Get paths to node
return [ OptionEnvironment.make_point_option( g, gr, n ) for n in local_maximas ]
print "Building options...\n\n"
progress = ProgressBar( 0, count, mode='fixed' )
# Needed to prevent glitches
print progress, "\r",
oldprog = str(progress)

options = []
for n in local_maximas:
options.append( OptionEnvironment.make_point_option( g, gr, n ) )


# print progress
progress.increment_amount()
if oldprog != str(progress):
print progress, "\r",
sys.stdout.flush()
oldprog=str(progress)

print "\n\n"

return options

@staticmethod
def make_options_from_random_paths( g, gr, count, markov ):
Expand All @@ -252,6 +293,12 @@ def make_options_from_random_paths( g, gr, count, markov ):
nodes = g.nodes()
random.shuffle( nodes )

print "Building options...\n\n"
progress = ProgressBar( 0, count, mode='fixed' )
# Needed to prevent glitches
print progress, "\r",
oldprog = str(progress)

options = []
for node in nodes:
if len( options ) > count:
Expand All @@ -269,6 +316,15 @@ def make_options_from_random_paths( g, gr, count, markov ):
if o:
options.append( o )

# print progress
progress.increment_amount()
if oldprog != str(progress):
print progress, "\r",
sys.stdout.flush()
oldprog=str(progress)

print "\n\n"

# Get paths to node
return options

Expand All @@ -283,6 +339,11 @@ def make_options_from_small_world( g, gr, count, markov, r = None ):
# Get all the edges in the graph
path_lengths = nx.shortest_path_length( g ).items()
random.shuffle( path_lengths )
print "Building options...\n\n"
progress = ProgressBar( 0, count, mode='fixed' )
# Needed to prevent glitches
print progress, "\r",
oldprog = str(progress)

options = []
for node, dists in path_lengths:
Expand All @@ -309,6 +370,15 @@ def make_options_from_small_world( g, gr, count, markov, r = None ):
if o:
options.append( o )

# print progress
progress.increment_amount()
if oldprog != str(progress):
print progress, "\r",
sys.stdout.flush()
oldprog=str(progress)

print "\n\n"

# Get paths to node
return options

Expand Down

0 comments on commit fff0cb3

Please sign in to comment.