geatpy icon indicating copy to clipboard operation
geatpy copied to clipboard

请问为什么程序运行到遗传算法第二代的时候,就会报ObjV is illegal的错误?

Open abc-hy opened this issue 3 years ago • 18 comments

请教一个使用遗传算法进行单目标优化的问题: 在运行到第一代的时候,目标函数值可以被赋到ObjV这个属性里面,但是只要到了第二代,就没有办法赋值进去,就会报一个objv is illegal的错误,所以对于单目标的优化,ObjV的行数是,种群个数乘以进化代数,然后列数是一列吗?我的矩阵就是这个大小,但是还是说格式不对,所以说objv应该是什么排列格式呢?

abc-hy avatar Jun 22 '21 03:06 abc-hy

就是很简单的Numpy ndarray的2维数组。行数等于种群规模;列数等于目标数目。

geatpy-dev avatar Jun 22 '21 10:06 geatpy-dev

行数不是种群数乘以进化代数吗?第二代的目标函数值也接在原来的后面吗?那为什么我的不能赋值呢? 因为这个矩阵会一起产生所有的决策变量,而我的代码需要一个一个的输入计算出相应的值,所以我自己写了一个循环。但是第二代的时候目标函数值就没办法赋到objv里了,我把代码发给你,能帮我看看是什么问吗? 而且,我有四个决策变量,第1,2,4是离散的,第3是连续的,我的vartype,不能像example那样乘以Dim,乘了就报错。说大小不对。所以Dim是决策变量的个数吗?

abc-hy avatar Jun 22 '21 11:06 abc-hy

附件是我的代码。

On Tue, Jun 22, 2021 at 5:21 AM geatpy @.***> wrote:

就是很简单的Numpy ndarray的2维数组。行数等于种群规模;列数等于目标数目。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-865859804, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCXVZ4YN52BVJEWL4ATTUBP2RANCNFSM47C2PY2A .

abc-hy avatar Jun 22 '21 12:06 abc-hy

哦,我知道了,所以objv是只能保存一代的目标函数值得吧。之前想错了。现在可以运行了。 另外有一个问题也想请教一下您: 运行完之后,这个trace的dictionary里面的f_avg和f_best怎么输出来呢? [image: image.png]

On Tue, Jun 22, 2021 at 7:04 AM Haiying Yang @.***> wrote:

附件是我的代码。

On Tue, Jun 22, 2021 at 5:21 AM geatpy @.***> wrote:

就是很简单的Numpy ndarray的2维数组。行数等于种群规模;列数等于目标数目。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-865859804, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCXVZ4YN52BVJEWL4ATTUBP2RANCNFSM47C2PY2A .

abc-hy avatar Jun 22 '21 16:06 abc-hy

python字典操作。

geatpy-dev avatar Jun 23 '21 14:06 geatpy-dev

请问,这个算法可以支持GPU吗?

On Wed, Jun 23, 2021 at 9:52 AM geatpy @.***> wrote:

python字典操作。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-866907277, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCTCECB7MALWMDKYEILTUHYKRANCNFSM47C2PY2A .

abc-hy avatar Jun 24 '21 00:06 abc-hy

可以在aimFunc里面用GPU计算目标函数值。

geatpy-dev avatar Jun 24 '21 10:06 geatpy-dev

请问是aimFunc哪个部分需要作一些什么操作吗?有GPU的代码案例供参考吗?我在geatpy网上好像没看到GPU格式的。 在main.py里面,有个myAlgorithm.run()这个语句,应该就是执行文件,是这里需要改吗?还是说需要调用一些库呢?

On Thu, Jun 24, 2021 at 5:44 AM geatpy @.***> wrote:

可以在aimFunc里面用GPU计算目标函数值。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-867534242, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCUQ53GMVQ4HG7RRIL3TUMEBRANCNFSM47C2PY2A .

abc-hy avatar Jun 24 '21 15:06 abc-hy

不需要改。在aimFunc里面你可以用任何方式给ObjV赋值,包括使用GPU计算的方式。

geatpy-dev avatar Jun 25 '21 08:06 geatpy-dev

[image: image.png] 如何赋值呢?我用torch.tensor把self.sum_output2转换成了torch里的tensor的格式,然后把这个tensor放到cuda上了,之后赋值给ObjV,但是他说赋值格式不合法。所以ObjV必须是一个numpy的array吗?但是numpy的array没有cuda这个attribute。。。 下面是报错信息: [image: image.png]

下面这个是直接把赋给ObjV的矩阵转成cuda的报错信息: [image: image.png] 因为之前赋给ObjV的是一个numpy.array,所以没办法把那个放到cuda上。

怎么样把ObjV赋值成GPU的方式呢?

On Fri, Jun 25, 2021 at 3:41 AM geatpy @.***> wrote:

不需要改。在aimFunc里面你可以用任何方式给ObjV赋值,包括使用GPU计算的方式。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-868327713, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCTX2PA7EOGZUX76QN3TUQ6KFANCNFSM47C2PY2A .

abc-hy avatar Jun 25 '21 12:06 abc-hy

这个就需要你自己解决了。已超出我的工作职责范围。熟悉Numpy和torch自然就能解决。

geatpy-dev avatar Jun 25 '21 12:06 geatpy-dev

可以用cupy这个库把numpy换成是cupy的吗?

On Fri, Jun 25, 2021 at 7:48 AM geatpy @.***> wrote:

这个就需要你自己解决了。已超出我的工作职责范围。熟悉Numpy和torch自然就能解决。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-868475923, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCVHF6LQQATJY3LPDDDTUR3LRANCNFSM47C2PY2A .

abc-hy avatar Jun 25 '21 15:06 abc-hy

可以的,无论用什么方式计算出目标函数值都没有关系。只要最后赋值给ObjV的格式符合Geatpy数据结构即可。

geatpy-dev avatar Jun 25 '21 16:06 geatpy-dev

我看到以前的一个回复,下面的截图,这边回复说GPU还没有开。那所以说,现在我们需要弄成GPU的不是遗传算法自身吗?而是外面处理这些数据的地方?所以,遗传算法自身还是在cpu的?改了之后,只是我自己写的其他程序比如处理数据那些是在GPU上的吗? [image: image.png]

On Fri, Jun 25, 2021 at 11:45 AM geatpy @.***> wrote:

可以的,无论用什么方式计算出目标函数值都没有关系。只要最后赋值给ObjV的格式符合Geatpy数据结构即可。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-868694929, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCQCDNGAJZR2R4MEE6LTUSXD7ANCNFSM47C2PY2A .

abc-hy avatar Jun 25 '21 19:06 abc-hy

是的,遗传算法本体不需要GPU,仅在目标函数计算上需要用GPU。

geatpy-dev avatar Jun 26 '21 03:06 geatpy-dev

那这样提速程度是不是不大呢?另外,在目标函数计算上需要用到GPU, 这个是指哪个阶段呢?是指,每一次得到一个目标函数值,再赋值到ObjV里,这个阶段,还是说,已经赋过了,挑选最优的那个阶段呢?

On Fri, Jun 25, 2021 at 10:03 PM geatpy @.***> wrote:

是的,遗传算法本体不需要GPU,仅在目标函数计算上需要用GPU。

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/geatpy-dev/geatpy/issues/198#issuecomment-868935301, or unsubscribe https://github.com/notifications/unsubscribe-auth/APF2ZCVJVCH7EQUUC4ZT57DTUU7OZANCNFSM47C2PY2A .

abc-hy avatar Jun 26 '21 03:06 abc-hy

前者。

geatpy-dev avatar Jun 26 '21 04:06 geatpy-dev

调试两天了,请问大佬们最后怎么解决好的? 我也是第二代报同样的错误。obj格式也设置成了2维数组,但是pop.ObjV.shape[0] =1 !=pop.sizes

Jacksonvillor avatar Apr 11 '23 13:04 Jacksonvillor