简体   繁体   English

怎么做并行Python Gekko?

[英]How to do parallel Python Gekko?

Sometimes my Python Gekko application is solved better with one solver over another.有时我的 Python Gekko 应用程序使用一个求解器比另一个求解器更好。 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?因为 Python Gekko 支持使用m.GEKKO(server='http://{address}')在不同服务器上进行本地或远程求解,是否可以创建一个并行的 Gekko 应用程序,该应用程序将在任意数量的计算机上同时尝试所有求解器(包括本地)然后在第一个成功返回时杀死其他进程? I've been looking at multi-threading and parallel packages for Python.我一直在研究 Python 的多线程和并行包。 Are there any that work well with Gekko to do parallel solves?有没有可以与 Gekko 一起很好地进行并行求解的方法? 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.这是使用queue的串行应用程序的并行版本,尽管它不会在第一个线程完成时停止其他线程。 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.即使其他线程没有停止,您也可以包含一个MAX_TIME,以便强制线程在指定的秒数内完成。 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.如果您还想获得第二个或更多结果,那么您可以添加另一行带有更多q.get()调用。

second = q.get(); print(second)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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