python遗传算法代码实现,某公司有6个建筑工地要开工,每个工地的位置(用平面坐标系 a , b 表示,距离单位:千米)及水泥日用量 d (吨)。目前有两个临时料场位于 A (5,1), B (2,7),日储量各有20吨。假设从料场到工地之间均有直线道路相连。 (1)试制定每天的供应计划,即从 A , B 两料场分别向各工地运送多少吨水泥,使总的吨千米数最小。 (2)为了进一步减少吨千米数,打算舍弃两个临时料场,改建两个新的,日储量各为20吨,问应建在何处,节省的吨千米数有多大?

时间: 2024-03-18 13:42:00 浏览: 33
以下是Python遗传算法代码实现,求解每天的供应计划: ```python import random # 工地位置和水泥日用量 sites = [(1, 3, 12), (4, 6, 8), (7, 2, 15), (2, 8, 10), (5, 5, 6), (8, 4, 9)] # 临时料场位置和日储量 depots = [('A', 5, 1, 20), ('B', 2, 7, 20)] # 遗传算法参数 POP_SIZE = 50 # 种群大小 CROSS_RATE = 0.8 # 交叉概率 MUTATION_RATE = 0.01 # 变异概率 N_GENERATIONS = 500 # 迭代次数 # 计算两点之间的距离 def distance(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 # 计算吨千米数 def cost(d, path): total_dist = distance(depots[path[0]][1], depots[path[0]][2], sites[path[0]][0], sites[path[0]][1]) * d[path[0]] for i in range(len(path) - 1): dist = distance(sites[path[i]][0], sites[path[i]][1], sites[path[i+1]][0], sites[path[i+1]][1]) total_dist += dist * d[path[i+1]] total_dist += distance(depots[path[-1]][1], depots[path[-1]][2], sites[path[-1]][0], sites[path[-1]][1]) * d[path[-1]] return total_dist # 初始化种群 def init_population(): population = [] for i in range(POP_SIZE): path = list(range(len(sites))) random.shuffle(path) population.append(path) return population # 选择 def select(population, fitness): idx = random.randint(0, len(population) - 1) for i in range(3): new_idx = random.randint(0, len(population) - 1) if fitness[new_idx] < fitness[idx]: idx = new_idx return population[idx] # 交叉 def crossover(parent1, parent2): if random.random() < CROSS_RATE: r1 = random.randint(0, len(parent1) - 1) r2 = random.randint(r1, len(parent1) - 1) temp = parent1[r1:r2] offspring = [i for i in parent2 if i not in temp] offspring[r1:r2] = temp return offspring else: return parent1 # 变异 def mutate(path): if random.random() < MUTATION_RATE: r1 = random.randint(0, len(path) - 1) r2 = random.randint(0, len(path) - 1) path[r1], path[r2] = path[r2], path[r1] return path # 遗传算法主函数 def genetic_algorithm(): population = init_population() for generation in range(N_GENERATIONS): # 计算适应度 fitness = [1 / cost(sites[i][2], population[i]) for i in range(POP_SIZE)] # 选择、交叉、变异 new_population = [] for i in range(POP_SIZE): parent1 = select(population, fitness) parent2 = select(population, fitness) offspring = crossover(parent1, parent2) offspring = mutate(offspring) new_population.append(offspring) population = new_population # 返回最优解 best_path = population[0] for path in population: if cost(sites[path[0]][2], path) < cost(sites[best_path[0]][2], best_path): best_path = path return best_path # 输出每天的供应计划 best_path = genetic_algorithm() print("每天的供应计划为:") for i in range(len(best_path)): depot = 'A' if i < len(best_path) / 2 else 'B' site = best_path[i] amount = sites[site][2] print(f"从{depot}料场向工地{site+1}运送{amount}吨水泥。") print("总的吨千米数为:", cost(sites[best_path[0]][2], best_path)) ``` 以下是Python遗传算法代码实现,求解建立新的料场所节省的吨千米数: ```python import itertools # 建立新料场的位置候选集合 new_depots = [(x, y, 20) for x, y in itertools.product(range(1, 9), repeat=2) if (x, y) not in [(5, 1), (2, 7)]] # 计算吨千米数 def cost_v2(d, path): total_dist = distance(path[0][1], path[0][2], sites[path[0][0]][0], sites[path[0][0]][1]) * d[path[0][0]] for i in range(len(path) - 1): dist = distance(sites[path[i][0]][0], sites[path[i][0]][1], sites[path[i+1][0]][0], sites[path[i+1][0]][1]) total_dist += dist * d[path[i+1][0]] total_dist += distance(path[-1][1], path[-1][2], sites[path[-1][0]][0], sites[path[-1][0]][1]) * d[path[-1][0]] return total_dist # 遗传算法主函数 def genetic_algorithm_v2(): population = init_population() for generation in range(N_GENERATIONS): # 计算适应度 fitness = [1 / cost_v2(sites[i][2], population[i]) for i in range(POP_SIZE)] # 选择、交叉、变异 new_population = [] for i in range(POP_SIZE): parent1 = select(population, fitness) parent2 = select(population, fitness) offspring = crossover(parent1, parent2) offspring = mutate(offspring) new_population.append(offspring) population = new_population # 返回最优解 best_path = population[0] for path in population: if cost_v2(sites[path[0][0]][2], path) < cost_v2(sites[best_path[0][0]][2], best_path): best_path = path return best_path # 输出建立新的料场所节省的吨千米数 best_path = genetic_algorithm_v2() old_cost = cost(sites[best_path[0][0]][2], best_path) new_cost = cost_v2(sites[best_path[0][0]][2], best_path) print(f"建立新料场所节省的吨千米数为:{old_cost - new_cost}。") ``` 注意:为了便于求解,本代码实现中假设每个工地的水泥日用量是固定的,且每个工地都需要供应水泥。实际应用中,这些假设可能需要根据实际情况进行修改。

最新推荐

recommend-type

python 遗传算法求函数极值的实现代码

今天小编就为大家分享一篇python 遗传算法求函数极值的实现代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

详解用python实现简单的遗传算法

主要介绍了详解用python实现简单的遗传算法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

Python3 A*寻路算法实现方式

今天小编就为大家分享一篇Python3 A*寻路算法实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python实现七个基本算法的实例代码

每个数据元素都存储在相对于其他数据元素的位置。 由于这些索引值是有序的,我们可以按顺序访问它们。 这个过程产实现的搜索即为顺序查找。 顺序查找原理剖析:从列表中的第一个元素开始,我们按照基本的顺序排序,...
recommend-type

Python实现统计英文单词个数及字符串分割代码

主要介绍了Python实现统计英文单词个数及字符串分割方法,本文分别给出代码实例,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。