简体   繁体   中英

How to do parallel Python Gekko?

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))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM