目标函数(黑匣子函数)由优化求解器评估,而不考虑它使用的算法?

2024-09-27 20:16:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这里的主要思想是了解如何在目标函数定义中使用黑盒函数,以及优化算法如何调用这些函数。在

假设函数定义如下:

f是一个在给定条件下最小化的目标函数问题。让我们说:

f(十一,易)=(艾茜)+(毕喜义)对于n=1,2

式中Yi=N(X1,X2,…Xn)是一个黑盒函数(模拟),其分析形式未知,将所有Xi作为输入

N表示正在模拟的网络。在

Ai和Bi是常数

该问题有以下限制:

X1+X2+…+Xn=C

下面的函数定义只是为了说明我如何调用模拟结果并将其用于优化目标。如果能做得更好,我也愿意接受建议。(但我的主要问题是遵循我的函数定义)

def fun(X): 
   sim_inpfile(X)  # function that has been already defined which is called now by taking x as arguments to write the input file to a simulation
   Popen('python Test02.py')  # running a python code to execute the simulation       
   jn.output.Results  # Extracting results of simulation by importing a custom made python module called jn 
   Y=[] # Simulation Result
   for i in range(len(jn.output.Results.res)):
      if jn.output.Results.res[i].name in nodes:
          y += [(jn.output.Results.res[i].pr)]  # extracting y as a list from the simulation results
   sum = 0 # optimization objective
   for i in range(len(a)):  # len[a]=len[b]=len[X]=len[Y]
      sum += a[i]*X[i]+ b[i]*X[i]*Y[i] #a and b are constants which are lists with the same size as x and y
  return sum   #the ultimate objective function that takes y(simulation results) as arguments to return the optimization objective value

我现在调用python优化求解器。在

^{pr2}$

问题:

  • 对于算法的每次迭代,目标函数是 要求评估?

  • 如果是,我的编码方式是否是返回目标的适当方式
    每次迭代时使用黑盒函数求值的函数 解决者?

  • 另外,如果任何python优化包中存在或类似的问题或类似的问题和示例,请共享它们的链接或线程。

感谢您抽出时间,您的经验可能会帮助我解决这个问题。在


Tags: theto函数in目标outputlen定义
1条回答
网友
1楼 · 发布于 2024-09-27 20:16:58

这是一个非常广泛的问题,优化是一个非常复杂的主题!所以我只想说几句:

For each iteration of the algorithm, will the objective function be called for evaluation?

当您调用它时,该函数将在每次迭代中多次调用,因为您没有提供jacobian。SLSQP将使用数值微分来推断它将采取的步进方向(以及它的长度)!在

If yes, is the way I've coded, an appropriate way to return my objective function with the black-box function evaluation at each iteration of the solver?

不,可能没有!在

第一件显而易见的事情是模拟的基于进程的调用,这会导致开销。如果你的模拟在时间上占主导地位,这就不那么相关了,但在这种情况下,这通常是错误的方法。在

其他备注

  • scipy的大多数优化器都假设一个平滑的、确定性的优化问题。你的可能会使两者都失效(而且会发生很多不好的事情)。平滑性可能很难解释,但是当你理解你的优化(随机数?)时,可以分析确定性/随机性
    • 请记住,如果是一个迭代模拟,在没有任何形式的收敛准则的情况下,在x迭代之后停止,那么随机性可能更为关键
    • 在数值微分的情况下,所有这些事情都更加引人注目(简化示例:用x调用vs.用x+1e-15调用;如果模拟误差在这个尺度或更高的范围内:坏的)
  • 那些一阶/二阶优化器(如SLSQP)的设计并不是为了有效地进行函数求值
    • 数值微分会让事情变得更糟
  • 全局优化/无导数优化通常使用其他方法,而不是scipy中的方法
    • 通常为零阶方法(不是基于梯度的)
    • 经常引入一些代理丢失/假设,如RBF核和co
    • 它们在函数调用方面效率更高,而且通常也被设计成在不平滑和不确定行为的情况下不会中断
    • 但是:全局优化/无导数优化通常比光滑的确定性优化更难(理论上已经很难了)

所以尽管你的问题没有太多细节(细节/假设很重要!),scipy不是实现您想要的最有用的库!有一些关于全局/无导数优化的调查论文(example)概述了备选方案。例如CoinOR的RBFOpt就是一个例子(arrirychoosed;开源),它的页面总结了上面提到的问题:

It does not assume that f(x) is known in analytical form: f(x) is simply a black-box that, given input values, produces output values. The bounds on the variables x_L, x_U are assumed to be finite. RBFOpt is especially targeted at problems for which each evaluation of the objective function f(x) is expensive (in terms of computing time, or cost, or some other measure) and we want to find a global minimum of the function with as few function evaluations as possible. Since this is a very difficult class of problems (we do not assume availability of first order derivatives), RBFOpt works best on problems that are relatively small dimensional (up to 20 variables, ideally less than 10) and for which the bounding box is not too large.

相关问题 更多 >

    热门问题