guofei9987 / scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)

Home Page:https://scikit-opt.github.io/scikit-opt/#/en/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

非优化参数的传入

cumttc opened this issue · comments

当使用目标函数时,有一些参数需要传入但不需要优化,例如:y=x[0]*b+x[1]**c+d
x是需要优化的,但bcd是三个不需要优化的变量(需要传入目标函数)。在scipy库中用的是args{...},在sko中是如何实现的呢?

commented

我寻思这个不是目标函数内部实现吗

你可以选择 curry 化把你的目标函数修改一下,比如你的目标函数image,你可以用lambda把参数 b,c,d都固定住:

b = 1
c = 2
d = 3
y  = lambda x : x[0] * b + x[1] ** c + d

或者你使用下面的方法:

>> def curry1(b):
   ...:     def curry2(c):
   ...:         def curry3(d):
   ...:             def f(x):
   ...:                 return x[0] * b + x[1] ** c + d
   ...:             return f
   ...:         return curry3
   ...:     return curry2
>> f = curry1(1)(2)(3)

无论哪种,你把 b ,c,d 都固定住之后就可以直接传到 sko 就可以