如何同时运行两个优化任务

huangapple go评论75阅读模式
英文:

How to run two optimizations in parallel

问题

我正在尝试优化一个耗时较长的函数。有许多不同的全局优化器,所以我想并行运行它们,查看它们在不同窗口中的进展。以下是我的非并行代码:

优化 1
from scipy.optimize import basinhopping

def build_show_bh(MIN=None):
    if MIN is None:
        MIN = [0]
    def fn(xx, f, accept):
        if f < MIN[-1]:
            print([round(x, 2) for x in xx], f)
            MIN.append(f)
    return fn

x0 = (0.5, 0.5, 0.5)
bounds = [(0, 1)] * 3
minimizer_kwargs = dict(method="L-BFGS-B", bounds=bounds)
progress_f = [0]
c = build_show_bh(progress_f)
print("Optimizing using basinhopping")
res = basinhopping(
    opt,
    x0,
    minimizer_kwargs=minimizer_kwargs,
    niter=10,
    callback=c,
    disp=True
)
print(f"external way of keeping track of MINF: {progress_f}")
优化 2
import dlib
lower_bounds = [0] * 3
upper_bounds = [1] * 3
x, y = dlib.find_max_global(opt_dlib, lower_bounds, upper_bounds, 10)
print(f"The optimal inputs are {x} with value {y}")

我该如何实现并行运行这些优化器?我使用的是 Python 3.10。

英文:

I am trying to optimize a function which takes a long time. There are many different global optimizers so I would like to run them in parallel, seeing the progress they make in different windows. Here is my non-parallel code:

##### optimization 1 ####
from scipy.optimize import basinhopping

def build_show_bh(MIN=None):
    if MIN is None:
        MIN = [0]
    def fn(xx, f, accept):
        if f &lt; MIN[-1]:
            print([round(x, 2) for x in xx], f)
            MIN.append(f)
    return fn

x0 = (0.5, 0.5, 0.5)
bounds = [(0,1)]*3
minimizer_kwargs = dict(method=&quot;L-BFGS-B&quot;, bounds=bounds)
progress_f = [0]
c = build_show_bh(progress_f)
print(&quot;Optimizing using basinhopping&quot;)
res = basinhopping(
    opt, 
    x0,
    minimizer_kwargs=minimizer_kwargs,
    niter=10,
    callback=c, 
    disp=True
)
print(f&quot;external way of keeping track of MINF: {progress_f}&quot;)

##### optimization 2 ####

import dlib
lower_bounds = [0]*3
upper_bounds = [1]*3
x, y = dlib.find_max_global(opt_dlib, lower_bounds, upper_bounds, 10)
print(f&quot;The optimal inputs are {x} with value {y}&quot;)

How can I do this?

I am on Python 3.10.

答案1

得分: 2

你可以尝试使用ProcessPoolExecutor来解决GIL问题,并将每个优化任务作为单独的进程运行:

from threading import Lock

print_lock = Lock()

from scipy.optimize import basinhopping


def scipy_optimize():
    def build_show_bh(MIN=None):
        if MIN is None:
            MIN = [0]
        def fn(xx, f, accept):
            if f < MIN[-1]:
                with print_lock:
                    print([round(x, 2) for x in xx], f)
                MIN.append(f)
        return fn

    x0 = (0.5, 0.5, 0.5)
    bounds = [(0,1)]*3
    minimizer_kwargs = dict(method="L-BFGS-B", bounds=bounds)
    progress_f = [0]
    c = build_show_bh(progress_f)
    with print_lock:
        print("Optimizing using basinhopping")

    res = basinhopping(
        opt, 
        x0,
        minimizer_kwargs=minimizer_kwargs,
        niter=10,
        callback=c, 
        disp=True
    )
    with print_lock:
        print(f"external way of keeping track of MINF: {progress_f}")

##### optimization 2 ####

import dlib

def dlib_optimize():
    lower_bounds = [0]*3
    upper_bounds = [1]*3
    x, y = dlib.find_max_global(opt_dlib, lower_bounds, upper_bounds, 10)
    with print_lock:
        print(f"The optimal inputs are {x} with value {y}")

with concurrent.futures.ProcessPoolExecutor() as executor:
    executor.submit(scipy_optimize)
    executor.submit(dlib_optimize)

注意:代码中的一些HTML实体编码已被更正。

英文:

You can try using a ProcessPoolExecutor to lift the GIL and run each optimization as a separate proces:

from threading import Lock

print_lock = Lock()

from scipy.optimize import basinhopping


def scipy_optimize():
    def build_show_bh(MIN=None):
        if MIN is None:
            MIN = [0]
        def fn(xx, f, accept):
            if f &lt; MIN[-1]:
                with print_lock:
                    print([round(x, 2) for x in xx], f)
                MIN.append(f)
        return fn

    x0 = (0.5, 0.5, 0.5)
    bounds = [(0,1)]*3
    minimizer_kwargs = dict(method=&quot;L-BFGS-B&quot;, bounds=bounds)
    progress_f = [0]
    c = build_show_bh(progress_f)
    with print_lock:
        print(&quot;Optimizing using basinhopping&quot;)

    res = basinhopping(
        opt, 
        x0,
        minimizer_kwargs=minimizer_kwargs,
        niter=10,
        callback=c, 
        disp=True
    )
    with print_lock:
        print(f&quot;external way of keeping track of MINF: {progress_f}&quot;)

##### optimization 2 ####

import dlib

def dlib_optimize():
    lower_bounds = [0]*3
    upper_bounds = [1]*3
    x, y = dlib.find_max_global(opt_dlib, lower_bounds, upper_bounds, 10)
    with print_lock:
        print(f&quot;The optimal inputs are {x} with value {y}&quot;)

with concurrent.futures.ProcessPoolExecutor() as executor:
    executor.submit(scipy_optimize)
    executor.submit(dlib_optimize)

huangapple
  • 本文由 发表于 2023年6月19日 22:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定