find & replace text tool

介绍了一款能够在大量文件中轻松搜索并替换文本的工具,对于需要在众多文件中进行文本修改的任务非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 if you need to find and replace a text among 10000 files... it is gonna be a touch job. I`I{VG Gc  
now I introduce this tool for you: ;{5?(QH;')  
(6}Kuv?<f)  
actualsr {b9|r&</  
http://www.divlocsoft.com/download.htm -t9;x/v?  
rA[4[ k+  
which is able to search and replace text easily 1#y,G7?_   
请修改这段代码,使得运行过程中指数函数的指数部分只有常数 # -*- coding: UTF-8 -*- import os import pandas as pd import math import random import operator from deap import creator, base, tools, gp, algorithms import numpy from fontTools.misc.py23 import xrange from matplotlib import pyplot as plt import pygraphviz as pgv import datetime import sys path = os.getcwd() curr_time = datetime.datetime.now() time_str = datetime.datetime.strftime(curr_time, &#39;%Y%m%d%H%M&#39;) def text_save(filename, data): file = open(filename, &#39;a&#39;) for i in range(len(data)): s = str(data[i]).replace(&#39;[&#39;, &#39;&#39;).replace(&#39;]&#39;, &#39;&#39;).replace(&#39;\n&#39;, &#39;&#39;) s = s.replace(&quot;&#39;&quot;, &#39;&#39;).replace(&#39;,&#39;, &#39;&#39;) + &#39;\n&#39; file.write(s) file.close() print(&quot;Saving the file succeeded&quot;) return def ProDiv(left, right): if right == 0: return 1000000 else: return left / right def ProSqrt(x): if x &lt;= 0: return 1000000 else: return x ** 0.5 def Prolog(x): if x &lt;= 0: return 1000000 else: return math.log(x) def Proexp(x): if x &gt;= 100: return 1000000 else: return math.exp(x) def Prosquare(x): if x &lt;= 0: return 1000000 else: if x ** 2 &gt; 1e30: return 1000000 else: return x ** 2 def Propow(down, up): &quot;&quot;&quot; 安全的指数计算函数,指数必须是纯常数且范围在(-10,10) 参数: down: 底数(可以包含变量) up: 指数(必须是纯常数,范围在-10到10之间) 返回: 计算结果,错误时返回1000000 &quot;&quot;&quot; # 确保指数是有效常数 # 1. 检查是否为数字类型 if not isinstance(up, (int, float)): return 1000000 # 2. 检查是否为特殊值(NaN或无穷大) if math.isnan(up) or math.isinf(up): return 1000000 # 3. 检查指数范围(-10 &lt; up &lt; 10) if up &lt;= -10 or up &gt;= 10: return 1000000 # 处理底数为零的情况 if down == 0: return 0.0 if up &gt; 0 else 1000000 # 0的正指数=0,负指数未定义 # 处理负底数 if down &lt; 0: return 1000000 # 使用对数预测结果大小,防止溢出 try: # 计算结果的绝对值的对数 log_value = abs(up) * math.log10(abs(down)) # 检查结果是否过大(&gt;10^50)或过小(&lt;10^-50) if log_value &gt; 50 or log_value &lt; -50: return 1000000 except (ValueError, OverflowError): return 1000000 # 正常计算并捕获可能的溢出错误 try: result = down ** up # 额外检查是否得到无穷大 if math.isinf(result): return 1000000 return result except (OverflowError, ValueError): return 1000000 # 更可靠的常数节点检查 def is_constant_node(node): &quot;&quot;&quot;检查节点是否是真正的常数节点&quot;&quot;&quot; # 常数节点必须有 value 属性 if not hasattr(node, &#39;value&#39;): return False # 确保值是可接受的数字类型 if not isinstance(node.value, (int, float)): return False # 确保值在合理范围内 if abs(node.value) &gt; 1e10: # 防止过大或过小的常数 return False return True # 更严格的幂函数检查 def check_pow_arguments(individual): &quot;&quot;&quot;确保所有 Propow 节点的指数参数都是常数&quot;&quot;&quot; # 首先将个体转换为字符串表达式 expr_str = str(individual) # 查找所有 Propow 函数调用 pow_calls = [] start_idx = 0 while True: idx = expr_str.find(&quot;Propow(&quot;, start_idx) if idx == -1: break # 找到匹配的右括号 depth = 1 end_idx = idx + len(&quot;Propow(&quot;) while end_idx &lt; len(expr_str) and depth &gt; 0: if expr_str[end_idx] == &#39;(&#39;: depth += 1 elif expr_str[end_idx] == &#39;)&#39;: depth -= 1 end_idx += 1 # 提取参数部分 args_str = expr_str[idx + len(&quot;Propow(&quot;):end_idx - 1].strip() args = [arg.strip() for arg in args_str.split(&#39;,&#39;) if arg.strip()] # 确保有两个参数 if len(args) == 2: pow_calls.append((args[0], args[1])) start_idx = end_idx # 检查每个幂调用的指数参数 for base, exponent in pow_calls: # 指数必须是数字(常数) try: float(exponent) except ValueError: # 如果无法转换为浮点数,说明是变量 print(f&quot;发现非法幂函数表达式: Propow({base}, {exponent})&quot;) return False return True # 在初始种群生成时强制验证 def create_valid_individual(): &quot;&quot;&quot;创建并验证个体,直到找到有效的个体&quot;&quot;&quot; max_attempts = 100 attempts = 0 while attempts &lt; max_attempts: ind = tool.individual() if check_pow_arguments(ind): return ind attempts += 1 # 如果多次尝试后仍无法生成合法个体,创建一个安全的默认个体 # 例如: Propow(1.0, 1.0) default_expr = gp.PrimitiveTree.from_string( &quot;Propow(1.0, 1.0)&quot;, pset ) return creator.Individual(default_expr) # 修改种群创建方法 def create_valid_population(n): &quot;&quot;&quot;创建并验证种群&quot;&quot;&quot; pop = [] for _ in range(n): pop.append(create_valid_individual()) return pop # 增强的交叉操作 def custom_cxOnePoint(ind1, ind2): max_attempts = 50 attempts = 0 while attempts &lt; max_attempts: new_ind1, new_ind2 = gp.cxOnePoint(ind1, ind2) if check_pow_arguments(new_ind1) and check_pow_arguments(new_ind2): return new_ind1, new_ind2 attempts += 1 # 如果多次尝试后仍无法生成合法个体,返回原始个体 return ind1, ind2 # 增强的变异操作 def custom_mutate(individual, expr, pset): max_attempts = 50 attempts = 0 while attempts &lt; max_attempts: new_individual, = gp.mutUniform(individual, expr, pset) if check_pow_arguments(new_individual): return new_individual, attempts += 1 # 如果多次尝试后仍无法生成合法个体,返回原始个体 return individual, def Psin(l, r): return math.sin(l * r) def Pcos(l, r): return math.cos(l * r) def generate_random_float(min_value, max_value, decimal_places): scale = 10 ** decimal_places random_float = random.uniform(min_value, max_value) rounded_float = round(random_float, decimal_places) scaled_float = int(rounded_float * scale) / scale return scaled_float min_val = 0.0 max_val = 1.0 decimal_places = 4 # 定义 primitive set pset = gp.PrimitiveSet(&#39;main&#39;, 4) # 定义变量个数 pset.renameArguments(ARG0=&#39;Fr&#39;) # 定义变量名称 pset.renameArguments(ARG1=&#39;We&#39;) pset.renameArguments(ARG2=&#39;Re&#39;) pset.renameArguments(ARG3=&#39;Bo&#39;) # 只保留乘除和幂运算 pset.addPrimitive(operator.mul, 2, name=&#39;mul&#39;) pset.addPrimitive(ProDiv, 2, name=&#39;ProDiv&#39;) pset.addPrimitive(Propow, 2, name=&#39;Propow&#39;) # 添加常数生成器 pset.addEphemeralConstant(&#39;c1&#39;, lambda: generate_random_float(-2, 2, 4)) pset.addEphemeralConstant(&#39;c2&#39;, lambda: generate_random_float(-1, 1, 4)) pset.addEphemeralConstant(&#39;c3&#39;, lambda: generate_random_float(0, 1, 4)) pset.addEphemeralConstant(&#39;c4&#39;, lambda: generate_random_float(1, 2, 4)) # 创建fitness类、individual类 creator.create(&#39;FitnessMin&#39;, base.Fitness, weights=(-1.0,)) creator.create(&#39;Individual&#39;, gp.PrimitiveTree, fitness=creator.FitnessMin) # 定义个体的生成方法、种群的生成方法 tool = base.Toolbox() tool.register(&#39;expr&#39;, gp.genHalfAndHalf, pset=pset, min_=1, max_=3) tool.register(&#39;individual&#39;, tools.initIterate, creator.Individual, tool.expr) tool.register(&#39;population&#39;, tools.initRepeat, list, tool.individual) tool.register(&#39;compile&#39;, gp.compile, pset=pset) tool.register(&#39;expr_mut&#39;, gp.genFull, pset=pset, min_=0, max_=2) # 生成一个subtree # 修改变异操作,确保变异后的个体符合要求 def custom_mutate(individual, expr, pset): new_individual, = gp.mutUniform(individual, expr, pset) while not check_pow_arguments(new_individual): new_individual, = gp.mutUniform(individual, expr, pset) return new_individual, # 修改交叉操作,确保交叉后的个体符合要求 def custom_cxOnePoint(ind1, ind2): new_ind1, new_ind2 = gp.cxOnePoint(ind1, ind2) while not check_pow_arguments(new_ind1) or not check_pow_arguments(new_ind2): new_ind1, new_ind2 = gp.cxOnePoint(ind1, ind2) return new_ind1, new_ind2 # 定义评价函数 def fit_evaluation(individual, input1, input2, input3, input4, input5, output): func = tool.compile(expr=individual) # pset上面已经给过了 sqerrors = ((abs((1 + func(Fr, We, Re, Bo)) * dPa - dPe) / dPe * 100) for Fr, We, Re, Bo, dPa, dPe in zip(input1, input2, input3, input4, input5, output)) return math.fsum(sqerrors) / len(output), # 必须返回一个tuple path = os.getcwd() filename = path + &#39;/&#39; + &#39;data_horizontal_mg_4g&#39; + &#39;.xlsx&#39; book = pd.read_excel(filename) data = book.values # 检查文件是否存在 if os.path.exists(filename): # 确保已经导入了os模块 try: # 读取Excel文件 book = pd.read_excel(filename) data = book.values print(f&quot;成功读取文件: {filename}&quot;) print(f&quot;数据形状: {data.shape}&quot;) except Exception as e: print(f&quot;读取文件时发生错误: {e}&quot;) else: print(f&quot;错误:文件不存在 - {filename}&quot;) # 可以在这里添加创建文件或其他处理逻辑 # x = data[:, 37] # X = (data[:, 31]/data[:, 32])**0.5 Fr = data[:, 28] # (data[:, 1]+2.2) We = data[:, 32] Re = data[:, 24] Bo = data[:, 36] # N=(WeG*Fr**2)**0.25 # Pred = data[:, 19] # f = data[:, 35]/data[:, 36] dPl = data[:, 61] dPe = data[:, 65] Y = (data[:, 67] / data[:, 66]) ** 0.5 dPa = data[:, 66] # /1000000000 # 定义evaluate、select、mate、mutate(这几个名字必须这样取,否则出错) tool.register(&#39;evaluate&#39;, fit_evaluation, input1=Fr, input2=We, input3=Re, input4=Bo, input5=dPa, output=dPe) tool.register(&#39;select&#39;, tools.selTournament, tournsize=3) # 注册修改后的交叉操作 tool.register(&#39;mate&#39;, custom_cxOnePoint) # 注册修改后的变异操作 tool.register(&#39;mutate&#39;, custom_mutate, expr=tool.expr_mut, pset=pset) # 限制树深度 tool.decorate(&quot;mate&quot;, gp.staticLimit(key=operator.attrgetter(&quot;height&quot;), max_value=10)) tool.decorate(&quot;mutate&quot;, gp.staticLimit(key=operator.attrgetter(&quot;height&quot;), max_value=10)) def drawBestInd(expr): # 输入参数是一个表达式(expr/tree),这个例子中就是individual nodes, edges, labels = gp.graph(expr) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog=&quot;dot&quot;) for i in nodes: n = g.get_node(i) n.attr[&quot;label&quot;] = labels[i] # curr_time = datetime.datetime.now() # time_str = datetime.datetime.strftime(curr_time, &#39;%Y%m%d%H%M&#39;) g.draw(time_str + &quot;tree.pdf&quot;) def varOr(population, toolbox, lambda_, cxpb, mutpb): &quot;&quot;&quot;Part of an evolutionary algorithm applying only the variation part (crossover, mutation **or** reproduction). The modified individuals have their fitness invalidated. The individuals are cloned so returned population is independent of the input population. :param population: A list of individuals to vary. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param lambda\_: The number of children to produce :param cxpb: The probability of mating two individuals. :param mutpb: The probability of mutating an individual. :returns: The final population. The variation goes as follow. On each of the *lambda_* iteration, it selects one of the three operations; crossover, mutation or reproduction. In the case of a crossover, two individuals are selected at random from the parental population :math:`P_\mathrm{p}`, those individuals are cloned using the :meth:`toolbox.clone` method and then mated using the :meth:`toolbox.mate` method. Only the first child is appended to the offspring population :math:`P_\mathrm{o}`, the second child is discarded. In the case of a mutation, one individual is selected at random from :math:`P_\mathrm{p}`, it is cloned and then mutated using using the :meth:`toolbox.mutate` method. The resulting mutant is appended to :math:`P_\mathrm{o}`. In the case of a reproduction, one individual is selected at random from :math:`P_\mathrm{p}`, cloned and appended to :math:`P_\mathrm{o}`. This variation is named *Or* because an offspring will never result from both operations crossover and mutation. The sum of both probabilities shall be in :math:`[0, 1]`, the reproduction probability is 1 - *cxpb* - *mutpb*. &quot;&quot;&quot; assert (cxpb + mutpb) &lt;= 1.0, ( &quot;The sum of the crossover and mutation probabilities must be smaller &quot; &quot;or equal to 1.0.&quot;) offspring = [] for _ in xrange(lambda_): op_choice = random.random() if op_choice &lt; cxpb: # Apply crossover ind1, ind2 = map(toolbox.clone, random.sample(population, 2)) ind1, ind2 = toolbox.mate(ind1, ind2) del ind1.fitness.values offspring.append(ind1) elif op_choice &lt; cxpb + mutpb: # Apply mutation ind = toolbox.clone(random.choice(population)) ind, = toolbox.mutate(ind) del ind.fitness.values offspring.append(ind) else: # Apply reproduction offspring.append(random.choice(population)) return offspring def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): &quot;&quot;&quot;This is the :math:`(\mu + \lambda)` evolutionary algorithm. :param population: A list of individuals. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param mu: The number of individuals to select for the next generation. :param lambda\_: The number of children to produce at each generation. :param cxpb: The probability that an offspring is produced by crossover. :param mutpb: The probability that an offspring is produced by mutation. :param ngen: The number of generation. :param stats: A :class:`~deap.tools.Statistics` object that is updated inplace, optional. :param halloffame: A :class:`~deap.tools.HallOfFame` object that will contain the best individuals, optional. :param verbose: Whether or not to log the statistics. :returns: The final population :returns: A class:`~deap.tools.Logbook` with the statistics of the evolution. The algorithm takes in a population and evolves it in place using the :func:`varOr` function. It returns the optimized population and a :class:`~deap.tools.Logbook` with the statistics of the evolution. The logbook will contain the generation number, the number of evaluations for each generation and the statistics if a :class:`~deap.tools.Statistics` is given as argument. The *cxpb* and *mutpb* arguments are passed to the :func:`varOr` function. The pseudocode goes as follow :: evaluate(population) for g in range(ngen): offspring = varOr(population, toolbox, lambda_, cxpb, mutpb) evaluate(offspring) population = select(population + offspring, mu) First, the individuals having an invalid fitness are evaluated. Second, the evolutionary loop begins by producing *lambda_* offspring from the population, the offspring are generated by the :func:`varOr` function. The offspring are then evaluated and the next generation population is selected from both the offspring **and** the population. Finally, when *ngen* generations are done, the algorithm returns a tuple with the final population and a :class:`~deap.tools.Logbook` of the evolution. This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`, :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be registered in the toolbox. This algorithm uses the :func:`varOr` variation. &quot;&quot;&quot; logbook = tools.Logbook() logbook.header = [&#39;gen&#39;, &#39;nevals&#39;] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Begin the generational process for gen in range(1, ngen + 1): # Vary the population offspring = varOr(population, toolbox, lambda_, cxpb, mutpb) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Select the next generation population population[:] = toolbox.select(population + offspring, mu) best_ind = halloffame.items[0] # Update the statistics with the new population record = stats.compile(population) if stats is not None else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) temp = logbook.stream if verbose: print(temp) print(best_ind) f = open(time_str + &quot;outputfile.dat&quot;, &quot;a&quot;) f.write(str(temp) + &#39;\n&#39;) f.write(str(best_ind) + &#39;\n&#39;) f.close() # 关闭文件 if halloffame.items[0].fitness.values[0] &lt; 20: print(&quot;绝对百分比误差小于20%,完成计算!&quot;) print(best_ind) return population, logbook return population, logbook def varAnd(population, toolbox, cxpb, mutpb): r&quot;&quot;&quot;Part of an evolutionary algorithm applying only the variation part (crossover **and** mutation). The modified individuals have their fitness invalidated. The individuals are cloned so returned population is independent of the input population. :param population: A list of individuals to vary. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param cxpb: The probability of mating two individuals. :param mutpb: The probability of mutating an individual. :returns: A list of varied individuals that are independent of their parents. The variation goes as follow. First, the parental population :math:`P_\mathrm{p}` is duplicated using the :meth:`toolbox.clone` method and the result is put into the offspring population :math:`P_\mathrm{o}`. A first loop over :math:`P_\mathrm{o}` is executed to mate pairs of consecutive individuals. According to the crossover probability *cxpb*, the individuals :math:`\mathbf{x}_i` and :math:`\mathbf{x}_{i+1}` are mated using the :meth:`toolbox.mate` method. The resulting children :math:`\mathbf{y}_i` and :math:`\mathbf{y}_{i+1}` replace their respective parents in :math:`P_\mathrm{o}`. A second loop over the resulting :math:`P_\mathrm{o}` is executed to mutate every individual with a probability *mutpb*. When an individual is mutated it replaces its not mutated version in :math:`P_\mathrm{o}`. The resulting :math:`P_\mathrm{o}` is returned. This variation is named *And* because of its propensity to apply both crossover and mutation on the individuals. Note that both operators are not applied systematically, the resulting individuals can be generated from crossover only, mutation only, crossover and mutation, and reproduction according to the given probabilities. Both probabilities should be in :math:`[0, 1]`. &quot;&quot;&quot; offspring = [toolbox.clone(ind) for ind in population] # Apply crossover and mutation on the offspring for i in range(1, len(offspring), 2): if random.random() &lt; cxpb: offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1], offspring[i]) del offspring[i - 1].fitness.values, offspring[i].fitness.values for i in range(len(offspring)): if random.random() &lt; mutpb: offspring[i], = toolbox.mutate(offspring[i]) del offspring[i].fitness.values return offspring def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__): &quot;&quot;&quot;This algorithm reproduce the simplest evolutionary algorithm as presented in chapter 7 of [Back2000]_. :param population: A list of individuals. :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution operators. :param cxpb: The probability of mating two individuals. :param mutpb: The probability of mutating an individual. :param ngen: The number of generation. :param stats: A :class:`~deap.tools.Statistics` object that is updated inplace, optional. :param halloffame: A :class:`~deap.tools.HallOfFame` object that will contain the best individuals, optional. :param verbose: Whether or not to log the statistics. :returns: The final population :returns: A class:`~deap.tools.Logbook` with the statistics of the evolution The algorithm takes in a population and evolves it in place using the :meth:`varAnd` method. It returns the optimized population and a :class:`~deap.tools.Logbook` with the statistics of the evolution. The logbook will contain the generation number, the number of evaluations for each generation and the statistics if a :class:`~deap.tools.Statistics` is given as argument. The *cxpb* and *mutpb* arguments are passed to the :func:`varAnd` function. The pseudocode goes as follow :: evaluate(population) for g in range(ngen): population = select(population, len(population)) offspring = varAnd(population, toolbox, cxpb, mutpb) evaluate(offspring) population = offspring As stated in the pseudocode above, the algorithm goes as follow. First, it evaluates the individuals with an invalid fitness. Second, it enters the generational loop where the selection procedure is applied to entirely replace the parental population. The 1:1 replacement ratio of this algorithm **requires** the selection procedure to be stochastic and to select multiple times the same individual, for example, :func:`~deap.tools.selTournament` and :func:`~deap.tools.selRoulette`. Third, it applies the :func:`varAnd` function to produce the next generation population. Fourth, it evaluates the new individuals and compute the statistics on this population. Finally, when *ngen* generations are done, the algorithm returns a tuple with the final population and a :class:`~deap.tools.Logbook` of the evolution. .. note:: Using a non-stochastic selection method will result in no selection as the operator selects *n* individuals from a pool of *n*. This function expects the :meth:`toolbox.mate`, :meth:`toolbox.mutate`, :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be registered in the toolbox. .. [Back2000] Back, Fogel and Michalewicz, &quot;Evolutionary Computation 1 : Basic Algorithms and Operators&quot;, 2000. &quot;&quot;&quot; logbook = tools.Logbook() logbook.header = [&#39;gen&#39;, &#39;nevals&#39;] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) # Begin the generational process for gen in range(1, ngen + 1): # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Vary the pool of individuals offspring = varAnd(offspring, toolbox, cxpb, mutpb) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Replace the current population by the offspring population[:] = offspring # Append the current generation statistics to the logbook best_ind = halloffame.items[0] record = stats.compile(population) if stats else {} logbook.record(gen=gen, nevals=len(invalid_ind), **record) temp = logbook.stream if verbose: print(temp) print(best_ind) f = open(time_str + &quot;outputfile.dat&quot;, &quot;a&quot;) f.write(str(temp) + &#39;\n&#39;) f.write(str(best_ind) + &#39;\n&#39;) f.close() # 关闭文件 if halloffame.items[0].fitness.values[0] &lt; 20: print(&quot;绝对百分比误差小于20%,完成计算!&quot;) print(best_ind) return population, logbook return population, logbook def main(): random.seed(318) ### 参数设置 cxpb = 0.5 # 交叉概率 mutpb = 0.15 # 变异概率0 ngen = 1500 # 迭代次数 popSize = 150 # 种群规模 ### 生成初始种群 - 使用新的验证方法 print(&quot;创建初始种群...&quot;) pop = create_valid_population(popSize) print(&quot;初始种群创建完成&quot;) # 验证初始种群中的所有个体 for i, ind in enumerate(pop): if not check_pow_arguments(ind): print(f&quot;警告: 初始种群中的个体 {i} 无效!&quot;) print(str(ind)) hof = tools.HallOfFame(1) invalid_fit = [ind for ind in pop if not ind.fitness.valid] fitnesses = tool.map(tool.evaluate, invalid_fit) # 求初始种群的每个个体的适应度值,是一个list for ind, fit in zip(invalid_fit, fitnesses): ind.fitness.values = fit # 给每个ind的fitness赋值 hof.update(pop) best_inds = [] # 记录每一代的最优个体 best_ind = hof.items[0] best_inds.append(best_ind) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register(&#39;avg&#39;, numpy.mean, axis=0) ### axis = 0表示数组的列求平均;axis = 1 表示数组的行求平均 stats.register(&#39;std&#39;, numpy.std, axis=0) stats.register(&#39;min&#39;, numpy.min, axis=0) stats.register(&#39;max&#39;, numpy.max, axis=0) ### record是一个字典:record{&lsquo;avg&rsquo;:[],&#39;std&#39;:[],...},每个value是一个list record = stats.compile(pop) for key in record: record[key] = record[key][0] logbook = tools.Logbook() logbook.record(gen=0, eval=popSize, best_ind=best_ind, **record) logbook.header = &#39;gen&#39;, &#39;min&#39;, &#39;max&#39;, &#39;avg&#39;, &#39;std&#39;, &#39;eval&#39;, &#39;best_ind&#39; print(logbook) print(&#39;--------开始迭代--------&#39;) for g in range(ngen): ### select 选出popSize个个体进入下一代 # print(&#39;The&#39;, g + 1, &#39;step&#39;) offspring = tool.select(pop, len(pop)) # offspring = list(map(tool.clone, offspring)) # offspring = tool.map(tool.clone, offspring) offspring = [tool.clone(ind) for ind in offspring] # Apply crossover and mutation on the offspring for i in range(1, len(offspring), 2): if random.random() &lt; cxpb: offspring[i - 1], offspring[i] = tool.mate(offspring[i - 1], offspring[i]) del offspring[i - 1].fitness.values, offspring[i].fitness.values for i in range(len(offspring)): if random.random() &lt; mutpb: offspring[i], = tool.mutate(offspring[i]) del offspring[i].fitness.values # Evaluate the individuals with an invalid fitness invalid_fit = [ind for ind in offspring if not ind.fitness.valid] fitnesses = tool.map(tool.evaluate, invalid_fit) for ind, fit in zip(invalid_fit, fitnesses): ind.fitness.values = fit hof.update(offspring) # 找到本代的最优个体 pop[:] = offspring # 更新种群 best_ind = hof.items[0] best_inds.append(best_ind) record = stats.compile(pop) # 数据统计 for key in record: record[key] = record[key][0] logbook.record(gen=g + 1, eval=len(invalid_fit), best_ind=best_ind, **record) temp = logbook.stream print(temp) # print logbook.stream ff = open(time_str + &quot;outputfile.dat&quot;, &quot;a&quot;) ff.write(str(temp) + &#39;\n&#39;) # f.write(str(best_ind) + &#39;\n&#39;) ff.close() # 关闭文件 if hof.items[0].fitness.values[0] &lt; 20: print(&quot;绝对百分比误差小于20%,完成计算!&quot;) print(best_ind) print(&#39;--------迭代结束-------&#39;) break print(&#39;--------迭代结束-------&#39;) # print(logbook) # pop = tool.population(n=150) # hof = tools.HallOfFame(1) # # stats_fit = tools.Statistics(lambda ind: ind.fitness.values) # stats_size = tools.Statistics(len) # mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) # mstats.register(&quot;avg&quot;, numpy.mean) # mstats.register(&quot;std&quot;, numpy.std) # mstats.register(&quot;min&quot;, numpy.min) # mstats.register(&quot;max&quot;, numpy.max) # pop, log = eaSimple(pop, tool, 0.5, 0.1, 5000, stats=mstats, halloffame=hof, verbose=True) # pop, log = eaMuPlusLambda(pop, tool, 20, 40, 0.5, 0.1, 5000, # stats=mstats, halloffame=hof, verbose=True) # print log # return pop, log, hof drawBestInd(expr=hof.items[0]) # 画出实际函数和优化的函数 # temp = numpy.zeros((len(data))) temp = numpy.zeros((len(data), 5)) func = tool.compile(expr=hof.items[0]) for i in range(len(data)): temp[i, 1] = (func(Fr[i], We[i], Re[i], Bo[i]) + 1) * dPa[ i] # func(FrG, Bo, Rel, ReG, dPa) func(Fr, Bo, f, Pred, x, N) x1 = numpy.arange(0, (max(data[:, 65])), 10) y1 = 1.3 * x1 y2 = 0.7 * x1 # func = tool.compile(expr=hof.items[0]) # z = [func(x) for x in points] temp[:, 0] = data[:, 65] temp[:, 2] = data[:, 66] # temp[:, 3] = data[:, 7] # temp[:, 4] = data[:, 8] filename = time_str + &#39;_data.dat&#39; text_save(filename, temp) # f1 = open(time_str + &quot;outputfile.dat&quot;, &quot;a&quot;) # f1.write( + &#39;\n&#39;) # f1.close ax1 = plt.subplot(211) plt.plot(data[:, 65], temp[:, 1], &#39;o&#39;, markeredgecolor=&#39;b&#39;, markerfacecolor=&#39;none&#39;, label=&#39;EXP-PRE-ENV&#39;) plt.plot(x1, y1, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;+30%&#39;) plt.plot(x1, y2, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;-30%&#39;) plt.legend(loc=&#39;best&#39;) plt.xscale(&#39;log&#39;) plt.yscale(&#39;log&#39;) ax1 = plt.subplot(212) plt.plot(data[:, 65], data[:, 66], &#39;o&#39;, markeredgecolor=&#39;r&#39;, markerfacecolor=&#39;none&#39;, label=&#39;EXP-PRE_ANA&#39;) plt.plot(x1, y1, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;+30%&#39;) plt.plot(x1, y2, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;-30%&#39;) plt.legend(loc=&#39;best&#39;) plt.xscale(&#39;log&#39;) plt.yscale(&#39;log&#39;) # ax1 = plt.subplot(223) # plt.plot(data[:, 5], data[:, 7], &#39;o&#39;, markeredgecolor=&#39;c&#39;, markerfacecolor=&#39;none&#39;, label=&#39;EXP-PRE_MSH&#39;) # plt.plot(x1, y1, &#39;--&#39;, color=&#39;k&#39;)#, label=&#39;+30%&#39;) # plt.plot(x1, y2, &#39;--&#39;, color=&#39;k&#39;)#, label=&#39;-30%&#39;) # plt.legend(loc=&#39;best&#39;) # plt.xscale(&#39;log&#39;) # plt.yscale(&#39;log&#39;) # ax1 = plt.subplot(224) # plt.plot(data[:, 5], data[:, 8], &#39;o&#39;, markeredgecolor=&#39;g&#39;, markerfacecolor=&#39;none&#39;, label=&#39;EXP-PRE_M&amp;H&#39;) plt.plot(x1, y1, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;+30%&#39;) plt.plot(x1, y2, &#39;--&#39;, color=&#39;k&#39;) # , label=&#39;-30%&#39;) # plt.legend(loc=&#39;best&#39;) # plt.xscale(&#39;log&#39;) # plt.yscale(&#39;log&#39;) # ax.set_aspect(1) # curr_time = datetime.datetime.now() # time_str=datetime.datetime.strftime(curr_time, &#39;%Y%m%d%H%M&#39;) plt.savefig(time_str + &#39;res.pdf&#39;) plt.show() main()
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值