1

I'm using Google's ortools.constraint_solver to find solutions to the Traveling Salesman Problem. As seen here. Everything works as expected when I run the program on one set of points.

Now that I got that working, I'm attempting to solve the TSP for multiple sets of points, by looping over all the sets and calling the the constraint solver on each set.

I have the below code re-defining the gflag tsp_size depending on the size of a particular problem (not all sets are the same size):

gflags.DEFINE_integer('tsp_size', len(points), 'Size of Traveling Salesman Problem instance.')

Again, it works when there is only one set being specified, but upon running it for the second set, I get the error: gflags.DuplicateFlagError: The flag 'tsp_size' is defined twice.

How can I make separate instances of the constraint solver within the same run of a program, considering I have multiple graphs?

sgarza62
  • 5,998
  • 8
  • 49
  • 69
  • You may want to re-read [ask]. – boardrider Jun 28 '15 at 20:30
  • 1
    @boardrider Could you explain your distaste for the question? I see you haven't made any significant contributions to the site yet; maybe you're misunderstanding what is considered acceptable? :) – sgarza62 Jun 29 '15 at 03:35

1 Answers1

0

I solved this by importing the module at the start of each iteration, then deleting it in sys.modules.

This gives me a new instance of the constraint solver for each set, with gflag config values specific to that set's size :)

for graph in graphs:
    import shp
    shp.run(graph)
    del sys.modules['shp']

EDIT:

The above code was giving me issues, because shp.py wasn't being run as main. I ended up getting this to work with:

from subprocess import call

# graph_files is a collection of filenames each referencing graph txt files I'm using.    
for fn in graph_files:
    call(["python", "shp.py", fn])

Use subprocess.Popen to do this asynchronously.

Hope this helps someone.

sgarza62
  • 5,998
  • 8
  • 49
  • 69