2

Sometimes my Python Gekko application is solved better with one solver over another. It is difficult to predict which solver will perform best. Because Python Gekko supports local or remote solves on different servers with m.GEKKO(server='http://{address}'), is it possible to create a parallel Gekko application that will try all the solvers simultaneously on any number of computers (including local) and then kill the other processes when the first one returns successfully? I've been looking at multi-threading and parallel packages for Python. Are there any that work well with Gekko to do parallel solves? Here is a sequential prototype:

from gekko import GEKKO
m = GEKKO()
x = m.Var(); y = m.Var()
m.Equation(x**2+y**2==1)
m.Maximize(x+y)
# try all solvers
for i in range(1,4):
    m.options.SOLVER = i
    m.solve()
    if m.options.APPSTATUS==1:
        print('Success: ' + str(i))
TexasEngineer
  • 684
  • 6
  • 15
  • 1
    Does this help? https://apmonitor.com/me575/index.php/Main/ParallelComputing I'm not sure whether parallel processing with `multiprocessing` or a multi-threaded application with `threading` would be best. It seems that multi-threading would be better if you are solving on a networked cluster of computers because your main script could spawn those sequentially and then wait for the first one to return. – John Hedengren Nov 16 '19 at 05:27
  • 1
    Here is some information on how to terminate the other threads once one is complete: https://stackoverflow.com/questions/17564804/how-to-wait-until-only-the-first-thread-is-finished-in-python – John Hedengren Nov 17 '19 at 13:48

1 Answers1

2

You can solve on any number of servers simultaneously with a multi-threaded application. Here is a parallel version of your serial application using a queue although it doesn't stop the other threads when the first completes. Even though the other threads are not stopped, you could include a MAX_TIME value so that the threads are forced to complete within a specified number of seconds. This approach does allow you to continue the main program while letting the other threads die when they complete or reach the max time limit.

from gekko import GEKKO
import queue, threading

def solve(rq, solver, server):
    #print('Solver ' + str(solver) + ' Server: ' + str(server) + '\n')
    if server=='localhost':
        m = GEKKO(remote=False)
    else:
        m = GEKKO(server=server)
    x = m.Var(); y = m.Var()
    m.Equation(x**2+y**2==1)
    m.Maximize(x+y)
    m.options.SOLVER = solver
    m.options.MAX_TIME = 2
    m.solve(disp=False)
    m.cleanup()
    rq.put(('solver',solver,'server',server))

# Select servers and solvers (1=APOPT, 2=BPOPT, 3=IPOPT, etc)
APOPT = 1; BPOPT = 2; IPOPT = 3
jobs = {'https://byu.apmonitor.com':APOPT,\
        'localhost':APOPT,\
        'https://byu.apmonitor.com':IPOPT,\
        'https://apmonitor.com':IPOPT}

# Run jobs, get only first result
q = queue.Queue()
threads = [threading.Thread(target=solve, args=(q, solver, server)) \
           for server, solver in jobs.items()]
for th in threads:
    th.daemon = True
    th.start()
first = q.get(); print(first)

If you would also like to get the second or more results then you can add another line with more q.get() calls.

second = q.get(); print(second)
John Hedengren
  • 12,068
  • 1
  • 21
  • 25