英文:
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 < 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}")
##### 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"The optimal inputs are {x} with value {y}")
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 < 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论