单参数GA优化时,不匹配报错
flyinginte opened this issue · 2 comments
flyinginte commented
我想求一个单参数函数的最大值问题,但是最后报错说形状不匹配。
代码如下:
from sko.GA import GA
import math
def cal(x):
return 1/(x * math.sin(10*math.pi*x) + 2)
ga = GA(func=cal, n_dim=1, size_pop=40, max_iter=200, prob_mut=0.001, lb=[-1], ub=[2], precision=1e-7)
best_x, best_y= ga.run()
print(best_x, best_y)
报错提示如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-70-a1f0ff067adf> in <module>
6
7 ga = GA(func=cal, n_dim=1, size_pop=40, max_iter=200, prob_mut=0.001, lb=[-1], ub=[2], precision=1e-7)
----> 8 best_x, best_y= ga.run()
9
10 print(best_x, best_y)
C:\Users\FLJ\Anaconda3\lib\site-packages\sko\GA.py in run(self, max_iter)
81 self.ranking()
82 self.selection()
---> 83 self.crossover()
84 self.mutation()
85
C:\Users\FLJ\Anaconda3\lib\site-packages\sko\operators\crossover.py in crossover_2point_bit(self)
42 mask[i, n1:n2] = 1
43 mask2 = (Chrom1 ^ Chrom2) & mask
---> 44 Chrom1 ^= mask2
45 Chrom2 ^= mask2
46 return self.Chrom
ValueError: non-broadcastable output operand with shape (20,1,25) doesn't match the broadcast shape (20,20,25)
不知道什么原因,希望有人给解释一下,谢谢大家!
Lesile-Liu commented
The function return numpy array. You can change it to scalar. For example sum(1/(x * math.sin(10math.pix) + 2))
flyinginte commented
It does works!
Although I can not fully understand it yet.
thank you for your professional answers!