guofei9987 / guofei9987/scikit-opt
单参数GA优化时,不匹配报错
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
我想求一个单参数函数的最大值问题,但是最后报错说形状不匹配。

代码如下:
~~~
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)
in
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)
~~~
不知道什么原因,希望有人给解释一下,谢谢大家!
Contributor guide
Research direction
Reproduce the single-parameter example, then inspect sko/GA.py around run() and sko/operators/crossover.py around crossover_2point_bit(), as shown in the traceback. Determine why the chromosome arrays have incompatible shapes during crossover; done means the example completes without the broadcasting error and returns best_x and best_y.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100