当前位置: 首页>>代码示例>>Python>>正文


Python DataBase.getFitness方法代码示例

本文整理汇总了Python中DataBase.getFitness方法的典型用法代码示例。如果您正苦于以下问题:Python DataBase.getFitness方法的具体用法?Python DataBase.getFitness怎么用?Python DataBase.getFitness使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataBase的用法示例。


在下文中一共展示了DataBase.getFitness方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GeneticAlgorithm

# 需要导入模块: import DataBase [as 别名]
# 或者: from DataBase import getFitness [as 别名]

#.........这里部分代码省略.........
                    if (i <= cut):
                        child[i] = p2[i]
                    else:
                        child[i] = p1[i]
        else:
            if(brother):
                child = p1
            else:
                child = p2

        return child


    def mutation(self, childs):
        global heuristic_data_domain
        for i in range(len(childs)):
            for j in range(heuristic_data_dim):
                pr = random.random()
                if pr < 0.01:
                    childs[i][j] = random.uniform(heuristic_data_domain[j][0], heuristic_data_domain[j][1])


    def initialization(self):
        global heuristic_data_domain

        for i in range(self.dim):
            data = self.db.getRow(i)
            if (data):
                for j in range(heuristic_data_dim):
                    self.parents[i][j] = data[j+1]
            else:
                for j in range(heuristic_data_dim):
                    self.parents[i][j] = random.uniform(heuristic_data_domain[j][0], heuristic_data_domain[j][1])
                self.db.insert([i, self.parents[i], None])

    def replacement(self, childs, childs_fit):
        better_fitness = self.parents_fit[0]
        posP = 0
        for i in range(1, self.dim):
            if (self.parents_fit[i] < better_fitness):
                better_fitness = self.parents_fit[i]
                posP = i

        worst_fitness = 0
        posC = 0
        for i in range(self.dim):
            if(childs_fit[i] > worst_fitness ):
                worst_fitness = childs_fit[i]
                posC = i

        childs[posC] = self.parents[posP]
        self.parents = childs
        self.parents_fit = childs_fit

    def fitness(self, p):
        queueGA.put([True, p])
        fitness = queueBMO.get()
        if fitness == None:
            self.stop = True
            return None
        else:
            return fitness

    def mainLoop(self):
        childs_fit = [None] * self.dim

        while not self.stop:
            childs = self.crossover()
            self.mutation(childs)

            self.db.deleteAll()
            for i in range(self.dim):
                self.db.insert([i, childs[i], None])

            for i in range(self.dim):
                childs_fit[i] = self.fitness(childs[i])
                if stop:
                    return
                self.db.updateFitness(i, childs_fit[i])

            self.replacement(childs, childs_fit)

    def run(self):
        self.db = DataBase()
        self.db.start()


        self.initialization()
        for i in range(self.dim):
            if(self.db.getFitness(i) == None):
                self.parents_fit[i] = self.fitness(self.parents[i])
                if not self.stop:
                    self.db.updateFitness(i, self.parents_fit[i])
                else:
                    break
            else:
                self.parents_fit[i] = self.db.getFitness(i)

        self.mainLoop()
        print ("Stoping GA")
开发者ID:framg,项目名称:BMO,代码行数:104,代码来源:GeneticAlgorithm.py


注:本文中的DataBase.getFitness方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。