这里的主要思想是了解如何在目标函数定义中使用黑盒函数以及优化算法如何调用这些函数。
假设我们有一个定义如下的函数:
f 是针对给定问题最小化的目标函数。让我们说:
f(Xi,Yi)=(Ai.Xi)+(Bi.Xi.Yi) 对于 i=1,2...n
其中 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 优化求解器。
res = scipy.optimize.minimize(fun, x0, method='SLSQP', bounds=bnds, constraints=cons) # x0=initial x values, bounds= Variable bounds on X, Constraint =equality constraint
问题:
对于算法的每次迭代,是否会调用目标函数进行评估?
如果是,那么我编码的方式
是在求解器的每次迭代中使用黑盒函数评估返回目标函数的适当方式吗?此外,如果任何 python 优化包或类似问题和示例中存在或类似问题,请分享他们的链接或线程。
感谢您抽出宝贵的时间,您的经验可能会帮助我解决这个问题。