锦标赛算法Python实现

锦标赛算法Python实现

本人代码小白,正在学习算法(以Python实现),每次找经典算法都是观摩大神的C/Java语言的代码,去写Python的,花费了不少时间,甚是心疼。
希望这篇文章能够帮到大家。

算法原理

https://blog.csdn.net/lsjseu/article/details/11704199

参考的Java代码

http://blog.csdn.net/hopeztm/article/details/7921686

Python代码

class Node:
    def __init__(self, _data, _id):
        self.data = _data
        self.id = _id
    def show(self):
        print(self.id, self.data)


def Adjust(data, idx):
    while (idx != 0):
        if (idx % 2 == 1):
            if (data[idx].data < data[idx + 1].data):
                data[(idx - 1) // 2] = data[idx]
            else:
                data[(idx - 1) // 2] = data[idx + 1]
            idx = (idx - 1) // 2
        else:
            if (data[idx - 1].data < data[idx].data):
                data[idx // 2 - 1] = data[idx - 1]
            else:
                data[idx // 2 - 1] = data[idx]
            idx = idx // 2 - 1
    return data


def Sort(data):
    length = len(data)
    n = 1
    while n < length:
        n <<= 1
        # print("n=",n)
    nTreeSize = 2 * n - 1
    nodes = []
    for i in range(nTreeSize):
        nodes.append(Node(0, 0))
    # 初始化竞赛数数据
    for i in range(n - 1, nTreeSize):
        idx = i - (n - 1)
        if idx < length:
            nodes[i] = Node(data[idx], i)
        else:
            nodes[i] = Node(9999, -1)
    for i in range(n - 2, -1, -1):
        if (nodes[i * 2 + 1].data < nodes[i * 2 + 2].data):
            nodes[i] = nodes[i * 2 + 1]
        else:
            nodes[i] = nodes[i * 2 + 2]
    # 实现排序
    B = []
    for i in range(0, length):
        # 取出最小的
        B.append(nodes[0].data)
        nodes[nodes[0].id].data = 9999
        Adjust(nodes, nodes[0].id)
    return B


if __name__ == "__main__":
    Arr = [2,5,9,1,11,20,15,7,4,6]
    Arr2 = Sort(Arr)
    print("newArr=",Arr2)
  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用锦标赛算法的遗传算法示例代码: ```python import random # 适应度函数,用于评估个体的适应度 def fitness(individual): return sum(individual) # 生成随机个体 def generate_individual(length): return [random.randint(0, 1) for _ in range(length)] # 生成初始种群 def generate_population(size, individual_length): return [generate_individual(individual_length) for _ in range(size)] # 锦标赛选择算法 def tournament_selection(population, fitness_func, tournament_size): # 随机选择tournament_size个个体 tournament = random.sample(population, tournament_size) # 计算每个个体的适应度 fitnesses = [fitness_func(individual) for individual in tournament] # 返回适应度最高的个体 return tournament[fitnesses.index(max(fitnesses))] # 遗传算法主函数 def genetic_algorithm(population_size, individual_length, fitness_func, tournament_size=2): # 生成初始种群 population = generate_population(population_size, individual_length) # 迭代100次 for i in range(100): # 选择两个个体 parent1 = tournament_selection(population, fitness_func, tournament_size) parent2 = tournament_selection(population, fitness_func, tournament_size) # 交叉 crossover_point = random.randint(1, individual_length - 1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] # 变异 mutation_point = random.randint(0, individual_length - 1) child1[mutation_point] = 1 - child1[mutation_point] mutation_point = random.randint(0, individual_length - 1) child2[mutation_point] = 1 - child2[mutation_point] # 将新个体加入种群 population += [child1, child2] # 选择适应度最高的前population_size个个体作为新一代种群 population = sorted(population, key=fitness_func, reverse=True)[:population_size] # 返回适应度最高的个体 return max(population, key=fitness_func) # 示例 individual_length = 10 population_size = 50 tournament_size = 3 individual = genetic_algorithm(population_size, individual_length, fitness, tournament_size) print(individual) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值