鱼C论坛

 找回密码
 立即注册
查看: 4472|回复: 2

[已解决]NSGA2算法Python实现

[复制链接]
发表于 2017-5-11 09:16:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 大甲鱼D 于 2017-5-11 09:25 编辑

附件里的代码是我根据Python社区里的一个文件改的,附件里还有NSGA2的算法原理图。NSGA2百度文库链接
这是链接,不了解NSGA2算法的鱼友可以看看,说的挺清楚的。
本人自学一点Python,水平实在有限,附件代码运行一直出错,也不断的在调试,可就是不行,也看不出来哪里有问题。
所以希望大家可以帮忙看看哪里出错了,或者有更好的实现方式。谢谢大家~
最佳答案
2017-5-12 22:02:44
139行
  1.    def __tournament(self, population):
  2.         participants = random.sample(population, self.num_of_tour_particips)
  3.         best = None
  4.         for participant in participants:
  5.             if best is None or self.crowding_operator(participant, best) == 1:
  6.                 best = participant
复制代码


这里面的self.crowding_operator(participant, best) 上文赋值best = None

QQ图片20170512215839.png
从这里也可以看出,你participant.rank的值也是None

79行
  1.     def crowding_operator(self, individual, other_individual):
  2.         if (individual.rank < other_individual.rank) or \
  3.                 ((individual.rank == other_individual.rank) and (
  4.                     individual.crowding_distance > other_individual.crowding_distance)):
  5.             return 1
  6.         else:
  7.             return -1
复制代码

def crowding_operator(self, individual, other_individual):                也就是在这里best做为了other_individual参数被传入
那么None应该是不会有.rank属性的
下一行中,两个None值进行比较。那么肯定是不会有结果的。

另外建议,你这么复杂的算法问题,通过这种方式提问,应该是不会有有效结果的。最少你要把大体上每个函数的用途,执行流程图中的那一部分功能给描述一下,目前面临的问题介绍一下。把问题往论坛上面一抛坐等结果是不可能的。

算法代码.rar

66.29 KB, 下载次数: 30

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-5-12 12:43:25 From FishC Mobile | 显示全部楼层
帮你顶下吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-12 22:02:44 | 显示全部楼层    本楼为最佳答案   
139行
  1.    def __tournament(self, population):
  2.         participants = random.sample(population, self.num_of_tour_particips)
  3.         best = None
  4.         for participant in participants:
  5.             if best is None or self.crowding_operator(participant, best) == 1:
  6.                 best = participant
复制代码


这里面的self.crowding_operator(participant, best) 上文赋值best = None

QQ图片20170512215839.png
从这里也可以看出,你participant.rank的值也是None

79行
  1.     def crowding_operator(self, individual, other_individual):
  2.         if (individual.rank < other_individual.rank) or \
  3.                 ((individual.rank == other_individual.rank) and (
  4.                     individual.crowding_distance > other_individual.crowding_distance)):
  5.             return 1
  6.         else:
  7.             return -1
复制代码

def crowding_operator(self, individual, other_individual):                也就是在这里best做为了other_individual参数被传入
那么None应该是不会有.rank属性的
下一行中,两个None值进行比较。那么肯定是不会有结果的。

另外建议,你这么复杂的算法问题,通过这种方式提问,应该是不会有有效结果的。最少你要把大体上每个函数的用途,执行流程图中的那一部分功能给描述一下,目前面临的问题介绍一下。把问题往论坛上面一抛坐等结果是不可能的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-27 11:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表