iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >如何用Python从零开始实现简单遗传算法
  • 902
分享到

如何用Python从零开始实现简单遗传算法

2023-06-15 16:06:24 902人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

今天就跟大家聊聊有关如何用python从零开始实现简单遗传算法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。遗传算法是一种随机全局优化算法。连同人工神经网络,它可能是最流行和广为人知

今天就跟大家聊聊有关如何用python从零开始实现简单遗传算法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

遗传算法是一种随机全局优化算法。连同人工神经网络,它可能是最流行和广为人知的生物学启发算法之一。该算法是一种进化算法,它通过自然选择,具有二进制表示形式和基于遗传重组和遗传突变的简单算子,来执行受进化生物学理论启发的优化过程。

遗传算法

遗传算法是一种随机全局搜索优化算法。它受到自然选择进化生物学理论的启发。具体来说,是将对遗传学的理解与理论相结合的新综合方法。

该算法使用遗传表示(位串),适应度(功能评估),基因重组(位串交叉)和突变(翻转位)的类似物。该算法的工作原理是首先创建固定大小的随机位串。重复算法的主循环固定次数的迭代,或者直到在给定迭代次数的最佳解决方案中看不到进一步的改善为止。该算法的一次迭代就像是进化的一代。

首先,使用目标函数评估总体位串(候选解决方案)。每个候选解决方案的目标函数评估被视为解决方案的适用性,可以将其最小化或最大化。然后,根据他们的健康状况选择父母。给定的候选解决方案可以用作父级零次或多次。一种简单有效的选择方法包括从总体中随机抽取k个候选者,并从适应性最好的组中选择成员。这就是所谓的锦标赛选择,其中k是一个超参数,并设置为诸如3的值。这种简单的方法模拟了成本更高的适应度成比例的选择方案。

父母被用作生成下一代候选点的基础,并且人口中的每个职位都需要一个父母。

然后将父母配对,并用来创建两个孩子。使用交叉算子执行重组。这涉及在位串上选择一个随机的分割点,然后创建一个子对象,该子对象的位从第一个父级到分割点直至从第二个父级到字符串的末尾。然后,为第二个孩子倒转此过程。

例如,两个父母:

parent1 = 00000

parent2 = 11111

可能会导致两个交叉孩子:

子1 = 00011

孩童2 = 11100

这称为单点交叉,并且操作员还有许多其他变体。

交叉概率是对每对父母概率应用的,这意味着在某些情况下,父母的副本将作为孩子而不是重组算子。交叉由设置为较大值(例如80%或90%)的超参数控制。变异涉及在已创建的子候选解决方案中翻转位。通常,将突变率设置为1 / L,其中L是位串的长度。

例如,如果问题使用具有20位的位串,则良好的默认突变率将是(1/20)= 0.05或5%的概率。

这定义了简单的遗传算法过程。这是一个很大的研究领域,并且对该算法进行了许多扩展。

现在我们已经熟悉了简单的遗传算法过程,下面让我们看一下如何从头开始实现它。

从零开始的遗传算法

在本节中,我们将开发遗传算法的实现。第一步是创建随机位串。我们可以使用布尔值True和False,字符串值“ 0”和“1”,或者整数值0和1。在这种情况下,我们将使用整数值。我们可以使用randint()函数生成一个范围内的整数值数组,并且可以将范围指定为从0开始且小于2的值,例如 0或1。为了简化起见,我们还将候选解决方案表示为列表而不是NumPy数组。可以如下创建初始的随机位串填充,其中“n_pop”是控制填充大小的超参数,“n_bits”是定义单个候选解决方案中位数的超参数:

# initial population of random bitstring  pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]

接下来,我们可以枚举固定数量的算法迭代,在这种情况下,该迭代由名为“ n_iter”的超参数控制。

...  # enumerate generations   for gen in range(n_iter):   ...

算法迭代的第一步是评估所有候选解。

我们将使用一个名为Objective()的函数作为通用目标函数,并对其进行调用以获取适合度得分,我们将其最小化。

# evaluate all candidates in the population  scores = [objective(c) for c in pop]

然后,我们可以选择将用于创建子代的父代。

锦标赛选择过程可以实现为一种函数,该函数可以获取总体并返回一个选定的父级。使用默认参数将k值固定为3,但是您可以根据需要尝试使用不同的值。

# tournament selection  def selection(pop, scores, k=3):   # first random selection   selection_ix = randint(len(pop))   for ix in randint(0, len(pop), k-1):  # check if better (e.g. perfORM a tournament)   if scores[ix] < scores[selection_ix]:   selection_ix = ix   return pop[selection_ix]

然后,我们可以为总体中的每个位置调用一次此函数,以创建父母列表。

# select parents  selected = [selection(pop, scores) for _ in range(n_pop)]

然后,我们可以创建下一代。

这首先需要执行交叉的功能。此功能将占用两个父级和交叉率。交叉率是一个超参数,它确定是否执行交叉,如果不进行交叉,则将父级复制到下一代。这是一个概率,通常具有接近1.0的较大值。

下面的crossover()函数使用范围为[0,1]的随机数来实现交叉以确定是否执行了交叉,然后如果要进行交叉则选择有效的分割点。

# crossover two parents to create two children  def crossover(p1, p2, r_cross):   # children are copies of parents by default   c1, c2 = p1.copy(), p2.copy()   # check for recombination   if rand() < r_cross:   # select crossover point that is not on the end of the string   pt = randint(1, len(p1)-2)  # perform crossover   c1 = p1[:pt] + p2[pt:]   c2 = p2[:pt] + p1[pt:]   return [c1, c2]

我们还需要执行突变的功能。该过程简单地以“ r_mut”超参数控制的低概率翻转位。

# mutation operator  def mutation(bitstring, r_mut):   for i in range(len(bitstring)):   # check for a mutation   if rand() < r_mut:   # flip the bit   bitstring[i] = 1 - bitstring[i]

然后,我们可以遍历父级列表并创建要用作下一代的子级列表,根据需要调用交叉和变异函数。

# create the next generation  children = list()  for i in range(0, n_pop, 2):   # get selected parents in pairs   p1, p2 = selected[i], selected[i+1]   # crossover and mutation   for c in crossover(p1, p2, r_cross):   # mutation   mutation(c, r_mut)   # store for next generation   children.append(c)

我们可以将所有这些结合到一个名为generic_alGorithm()的函数中,该函数采用目标函数的名称和搜索的超参数,并返回在搜索过程中找到的最佳解决方案。

# genetic algorithm  def genetic_algorithm(objective, n_bits, n_iter, n_pop, r_cross, r_mut):   # initial population of random bitstring   pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]   # keep track of best solution   best, best_eval = 0, objective(pop[0])   # enumerate generations   for gen in range(n_iter):    # evaluate all candidates in the population    scores = [objective(c) for c in pop]    # check for new best solution    for i in range(n_pop):     if scores[i] < best_eval:      best, best_eval = pop[i], scores[i]      print(">%d, new best f(%s) = %.3f" % (gen,  pop[i], scores[i]))    # select parents    selected = [selection(pop, scores) for _ in range(n_pop)]    # create the next generation    children = list()    for i in range(0, n_pop, 2):     # get selected parents in pairs     p1, p2 = selected[i], selected[i+1]     # crossover and mutation     for c in crossover(p1, p2, r_cross):      # mutation      mutation(c, r_mut)      # store for next generation      children.append(c)    # replace population    pop = children   return [best, best_eval]

现在,我们已经开发了遗传算法的实现,让我们探讨如何将其应用于目标函数。

OneMax的遗传算法

在本节中,我们将遗传算法应用于基于二进制字符串的优化问题。该问题称为OneMax,并根据字符串中的1的个数评估二进制字符串。例如,长度为20位的位串对于全1的字符串的得分为20。假设我们已经实现了遗传算法以最小化目标函数,则可以在此评估中添加负号,以便大的正值变为大的负值。下面的onemax()函数实现了此功能,并将整数值的位串作为输入,并返回值的负和。

# objective function  def onemax(x):   return -sum(x)

接下来,我们可以配置搜索。

搜索将运行100次迭代,我们将在候选解决方案中使用20位,这意味着最佳适应度为-20.0。

人口总数将为100,我们将使用90%的交叉率和5%的突变率。经过一番尝试和错误后,才选择此配置。

# define the total iterations  n_iter = 100  # bits  n_bits = 20  # define the population size  n_pop = 100  # crossover rate  r_cross = 0.9  # mutation rate  r_mut = 1.0 / float(n_bits)

然后可以调用搜索并报告最佳结果。

# perform the genetic algorithm search  best, score = genetic_algorithm(onemax, n_bits, n_iter, n_pop, r_cross, r_mut)  print('Done!')  print('f(%s) = %f' % (best, score))

结合在一起,下面列出了将遗传算法应用于OneMax目标函数的完整示例。

# genetic algorithm search of the one max optimization problem  from numpy.random import randint  from numpy.random import rand   # objective function  def onemax(x):   return -sum(x)   # tournament selection  def selection(pop, scores, k=3):   # first random selection   selection_ix = randint(len(pop))   for ix in randint(0, len(pop), k-1):    # check if better (e.g. perform a tournament)    if scores[ix] < scores[selection_ix]:     selection_ix = ix   return pop[selection_ix]  # crossover two parents to create two children  def crossover(p1, p2, r_cross):   # children are copies of parents by default   c1, c2 = p1.copy(), p2.copy()   # check for recombination   if rand() < r_cross:    # select crossover point that is not on the end of the string    pt = randint(1, len(p1)-2)    # perform crossover    c1 = p1[:pt] + p2[pt:]    c2 = p2[:pt] + p1[pt:]   return [c1, c2]  # mutation operator  def mutation(bitstring, r_mut):   for i in range(len(bitstring)):    # check for a mutation    if rand() < r_mut:     # flip the bit     bitstring[i] = 1 - bitstring[i]  # genetic algorithm  def genetic_algorithm(objective, n_bits, n_iter, n_pop, r_cross, r_mut):   # initial population of random bitstring   pop = [randint(0, 2, n_bits).tolist() for _ in range(n_pop)]   # keep track of best solution   best, best_eval = 0, objective(pop[0])   # enumerate generations   for gen in range(n_iter):    # evaluate all candidates in the population    scores = [objective(c) for c in pop]    # check for new best solution    for i in range(n_pop):     if scores[i] < best_eval:      best, best_eval = pop[i], scores[i]      print(">%d, new best f(%s) = %.3f" % (gen,  pop[i], scores[i]))    # select parents    selected = [selection(pop, scores) for _ in range(n_pop)]    # create the next generation    children = list()    for i in range(0, n_pop, 2):     # get selected parents in pairs     p1, p2 = selected[i], selected[i+1]     # crossover and mutation     for c in crossover(p1, p2, r_cross):      # mutation      mutation(c, r_mut)      # store for next generation      children.append(c)    # replace population    pop = children   return [best, best_eval]  # define the total iterations  n_iter = 100  # bits  n_bits = 20  # define the population size  n_pop = 100  # crossover rate  r_cross = 0.9  # mutation rate  r_mut = 1.0 / float(n_bits)  # perform the genetic algorithm search  best, score = genetic_algorithm(onemax, n_bits, n_iter, n_pop, r_cross, r_mut)  print('Done!')  print('f(%s) = %f' % (best, score))

运行示例将报告沿途发现的最佳结果,然后在搜索结束时给出最终的最佳解决方案,我们希望这是最佳解决方案。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到搜索在大约八代之后找到了最佳解决方案。

>0, new best f([1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]) = -14.000  >0, new best f([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0]) = -15.000  >1, new best f([1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1]) = -16.000  >2, new best f([0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]) = -17.000  >2, new best f([1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) = -19.000  >8, new best f([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) = -20.000  Done!  f([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) = -20.000000

连续函数优化的遗传算法

优化OneMax功能不是很有趣。我们更可能希望优化连续函数。例如,我们可以定义x ^ 2最小化函数,该函数接受输入变量并在f(0,0)= 0.0时具有最优值。

# objective function  def objective(x):   return x[0]**2.0 + x[1]**2.0

我们可以使用遗传算法最小化此功能。首先,我们必须定义每个输入变量的界限。

# define range for input  bounds = [[-5.0, 5.0], [-5.0, 5.0]]

我们将“ n_bits”超参数作为目标函数每个输入变量的位数,并将其设置为16位。

# bits per variable  n_bits = 16

这意味着在给定两个输入变量的情况下,我们的实际位字符串将具有(16 * 2)= 32位。

# mutation rate  r_mut = 1.0 / (float(n_bits) * len(bounds))

接下来,我们需要确保初始填充会创建足够大的随机位串。

# initial population of random bitstring  pop = [randint(0, 2, n_bits*len(bounds)).tolist() for _ in range(n_pop)]

最后,在使用目标函数评估每个位串之前,我们需要将这些位串解码为数字。

我们可以通过首先将每个子字符串解码为整数,然后将整数缩放到所需范围来实现此目的。这将提供一个范围内的值向量,然后可将其提供给目标函数进行评估。

下面的decode()函数以函数的界限,每个变量的位数和一个位串作为输入来实现此目的,并返回已解码实数值的列表。

# decode bitstring to numbers  def decode(bounds, n_bits, bitstring):   decoded = list()   largest = 2**n_bits   for i in range(len(bounds)):    # extract the substring    start, end = i * n_bits, (i * n_bits)+n_bits    substring = bitstring[start:end]    # convert bitstring to a string of chars    chars = ''.join([str(s) for s in substring])    # convert string to integer    intinteger = int(chars, 2)    # scale integer to desired range    value = bounds[i][0] + (integer/largest) * (bounds[i][1] - bounds[i][0])    # store    decoded.append(value)   return decoded

然后,我们可以在算法循环的开始处调用它来解码总体,然后评估总体的解码版本。

# decode population  decoded = [decode(bounds, n_bits, p) for p in pop]  # evaluate all candidates in the population  scores = [objective(d) for d in decoded]

结合在一起,下面列出了用于连续函数优化的遗传算法的完整示例。

# genetic algorithm search for continuous function optimization  from numpy.random import randint  from numpy.random import rand   # objective function  def objective(x):   return x[0]**2.0 + x[1]**2.0   # decode bitstring to numbers  def decode(bounds, n_bits, bitstring):   decoded = list()   largest = 2**n_bits   for i in range(len(bounds)):    # extract the substring    start, end = i * n_bits, (i * n_bits)+n_bits    substring = bitstring[start:end]    # convert bitstring to a string of chars    chars = ''.join([str(s) for s in substring])    # convert string to integer    intinteger = int(chars, 2)    # scale integer to desired range    value = bounds[i][0] + (integer/largest) * (bounds[i][1] - bounds[i][0])    # store   decoded.append(value)   return decoded  # tournament selection  def selection(pop, scores, k=3):   # first random selection   selection_ix = randint(len(pop))   for ix in randint(0, len(pop), k-1):    # check if better (e.g. perform a tournament)    if scores[ix] < scores[selection_ix]:     selection_ix = ix   return pop[selection_ix]  # crossover two parents to create two children  def crossover(p1, p2, r_cross):   # children are copies of parents by default   c1, c2 = p1.copy(), p2.copy()   # check for recombination   if rand() < r_cross:    # select crossover point that is not on the end of the string    pt = randint(1, len(p1)-2)    # perform crossover    c1 = p1[:pt] + p2[pt:]    c2 = p2[:pt] + p1[pt:]   return [c1, c2]   # mutation operator  def mutation(bitstring, r_mut):   for i in range(len(bitstring)):    # check for a mutation    if rand() < r_mut:     # flip the bit     bitstring[i] = 1 - bitstring[i]  # genetic algorithm  def genetic_algorithm(objective, bounds, n_bits, n_iter, n_pop, r_cross, r_mut):   # initial population of random bitstring   pop = [randint(0, 2, n_bits*len(bounds)).tolist() for _ in range(n_pop)]   # keep track of best solution   best, best_eval = 0, objective(pop[0])   # enumerate generations   for gen in range(n_iter):    # decode population    decoded = [decode(bounds, n_bits, p) for p in pop]    # evaluate all candidates in the population    scores = [objective(d) for d in decoded]    # check for new best solution    for i in range(n_pop):     if scores[i] < best_eval:      best, best_eval = pop[i], scores[i]      print(">%d, new best f(%s) = %f" % (gen,  decoded[i], scores[i]))    # select parents    selected = [selection(pop, scores) for _ in range(n_pop)]    # create the next generation    children = list()   for i in range(0, n_pop, 2):     # get selected parents in pairs     p1, p2 = selected[i], selected[i+1]     # crossover and mutation     for c in crossover(p1, p2, r_cross):      # mutation      mutation(c, r_mut)      # store for next generation      children.append(c)    # replace population    pop = children   return [best, best_eval]  # define range for input  bounds = [[-5.0, 5.0], [-5.0, 5.0]]  # define the total iterations  n_iter = 100  # bits per variable  n_bits = 16  # define the population size  n_pop = 100  # crossover rate  r_cross = 0.9  # mutation rate  r_mut = 1.0 / (float(n_bits) * len(bounds))  # perform the genetic algorithm search  best, score = genetic_algorithm(objective, bounds, n_bits, n_iter, n_pop, r_cross, r_mut)  print('Done!')  decodedecoded = decode(bounds, n_bits, best)  print('f(%s) = %f' % (decoded, score))

运行示例将报告最佳解码结果以及运行结束时的最佳解码解决方案。

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

在这种情况下,我们可以看到该算法发现了一个非常接近f(0.0,0.0)= 0.0的输入。

>0, new best f([-0.785064697265625, -0.807647705078125]) = 1.268621  >0, new best f([0.385894775390625, 0.342864990234375]) = 0.266471  >1, new best f([-0.342559814453125, -0.1068115234375]) = 0.128756  >2, new best f([-0.038909912109375, 0.30242919921875]) = 0.092977  >2, new best f([0.145721435546875, 0.1849365234375]) = 0.055436  >3, new best f([0.14404296875, -0.029754638671875]) = 0.021634  >5, new best f([0.066680908203125, 0.096435546875]) = 0.013746  >5, new best f([-0.036468505859375, -0.10711669921875]) = 0.012804  >6, new best f([-0.038909912109375, -0.099639892578125]) = 0.011442  >7, new best f([-0.033111572265625, 0.09674072265625]) = 0.010455  >7, new best f([-0.036468505859375, 0.05584716796875]) = 0.004449  >10, new best f([0.058746337890625, 0.008087158203125]) = 0.003517  >10, new best f([-0.031585693359375, 0.008087158203125]) = 0.001063  >12, new best f([0.022125244140625, 0.008087158203125]) = 0.000555  >13, new best f([0.022125244140625, 0.00701904296875]) = 0.000539  >13, new best f([-0.013885498046875, 0.008087158203125]) = 0.000258  >16, new best f([-0.011444091796875, 0.00518798828125]) = 0.000158  >17, new best f([-0.0115966796875, 0.00091552734375]) = 0.000135  >17, new best f([-0.004730224609375, 0.00335693359375]) = 0.000034  >20, new best f([-0.004425048828125, 0.00274658203125]) = 0.000027  >21, new best f([-0.002288818359375, 0.00091552734375]) = 0.000006  >22, new best f([-0.001983642578125, 0.00091552734375]) = 0.000005  >22, new best f([-0.001983642578125, 0.0006103515625]) = 0.000004  >24, new best f([-0.001373291015625, 0.001068115234375]) = 0.000003  >25, new best f([-0.001373291015625, 0.00091552734375]) = 0.000003  >26, new best f([-0.001373291015625, 0.0006103515625]) = 0.000002  >27, new best f([-0.001068115234375, 0.0006103515625]) = 0.000002  >29, new best f([-0.000152587890625, 0.00091552734375]) = 0.000001  >33, new best f([-0.0006103515625, 0.0]) = 0.000000  >34, new best f([-0.000152587890625, 0.00030517578125]) = 0.000000  >43, new best f([-0.00030517578125, 0.0]) = 0.000000  >60, new best f([-0.000152587890625, 0.000152587890625]) = 0.000000  >65, new best f([-0.000152587890625, 0.0]) = 0.000000  Done!  f([-0.000152587890625, 0.0]) = 0.000000

看完上述内容,你们对如何用Python从零开始实现简单遗传算法有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网Python频道,感谢大家的支持。

--结束END--

本文标题: 如何用Python从零开始实现简单遗传算法

本文链接: https://www.lsjlt.com/news/280802.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何用Python从零开始实现简单遗传算法
    今天就跟大家聊聊有关如何用Python从零开始实现简单遗传算法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。遗传算法是一种随机全局优化算法。连同人工神经网络,它可能是最流行和广为人知...
    99+
    2023-06-15
  • Python中怎么实现一个简单遗传算法
    今天就跟大家聊聊有关Python中怎么实现一个简单遗传算法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。遗传算法遗传算法是模仿自然选择过程的优化算法。 他们没有使用"数学技...
    99+
    2023-06-16
  • 如何使用Python实现遗传算法
    本篇内容介绍了“如何使用Python实现遗传算法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!遗传算法是模仿自然界生物进化机制发展起来的随机...
    99+
    2023-07-05
  • Flex2.0如何从零开始实现文件上传
    这篇文章给大家介绍Flex2.0如何从零开始实现文件上传,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Flex2.0 从零开始实现文件上传以前在Flex1.5的时候也做过,不过当初使用的是oreilly的cos.jar...
    99+
    2023-06-17
  • python如何实现高效的遗传算法
    小编给大家分享一下python如何实现高效的遗传算法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!遗传算法属于一种优化算法。如果你有一个待优化函数,可以考虑次算法...
    99+
    2023-06-14
  • 从零开始:如何在Java中实现二维码编程算法?
    二维码是一种二维条码,可以存储大量的数据信息。在现代社会中,二维码已经成为了商业、社交、娱乐等领域的重要工具。本文将介绍如何使用Java编程语言实现二维码编程算法。 一、二维码的基本概念 二维码是由黑白相间的小正方形组成的图案,可以编码大...
    99+
    2023-10-21
    npm 二维码 编程算法
  • 从零开始,如何用Go语言开发一个简单的Web应用
    从零开始,如何用Go语言开发一个简单的Web应用简介:Go语言是一种开源的编程语言,它具有高效、简洁以及并发编程的优势,因此在Web应用的开发中越来越受到开发者的欢迎。本文将指导读者从零开始,使用Go语言来开发一个简单的Web应用。步骤一:...
    99+
    2023-11-20
    开发 Go语言 Web应用
  • 怎么用python代码实现遗传算法
    要使用Python代码实现遗传算法,可以按照以下步骤进行操作:1. 定义问题:首先,需要明确要解决的问题是什么,例如优化问题、寻找最...
    99+
    2023-10-10
    python
  • python如何实现使用遗传算法进行图片拟合
    小编给大家分享一下python如何实现使用遗传算法进行图片拟合,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!引言算法思路假设我们有这样一个生物族群,他们的每个基因片段都是一个个三角形(即只含三个点和颜色信息),他们每个个体...
    99+
    2023-06-29
  • 从零开始学习Go语言单链表的实现方法
    从零开始学习Go语言单链表的实现方法 在学习数据结构与算法时,单链表是一个基础且重要的数据结构之一。本文将介绍如何使用Go语言实现单链表,并通过具体的代码示例帮助读者更好地理解这个数据...
    99+
    2024-04-02
  • 从零开始学习 Windows 编程:Go 语言实现算法 API
    作为一名程序员,我们需要不断地学习新的技能和知识。而在 Windows 平台上开发程序是一个必须掌握的技能。本文将介绍如何使用 Go 语言实现算法 API,帮助你从零开始学习 Windows 编程。 一、Windows 程序开发环境搭建 ...
    99+
    2023-08-15
    windows 编程算法 api
  • python遗传算法之geatpy如何安装使用
    这篇文章主要介绍了python遗传算法之geatpy如何安装使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python遗传算法之geatpy如何安装使用文章都会有所收获,下面我们一起来看看吧。1. geat...
    99+
    2023-07-06
  • 使用Python实现遗传算法的完整代码
    目录遗传算法具体步骤:1.2 实验代码1.3 实验结果1.4 实验总结1、如何在算法中实现“优胜劣汰”?2 、如何保证进化一直是在正向进行?3、交叉如何实现?...
    99+
    2023-03-23
    Python 遗传算法 python算法
  • python实现使用遗传算法进行图片拟合
    目录引言预备知识及准备工作打开图片随机生成生物族群按照生物性状画图对比生物个体和目标图片的相似度保存图片算法主体交叉互换基因突变基因片段易位增加基因片段减少基因片段变异繁殖淘汰拟合示...
    99+
    2024-04-02
  • 使用Python实现的遗传算法 附完整代码
    遗传算法是模仿自然界生物进化机制发展起来的随机全局搜索和优化方法,它借鉴了达尔文的进化论和孟德尔的遗传学说。其本质是一种高效、并行、全局搜索的方法,它能在搜索过程中自动获取和积累有关搜索空间的知识,并自适应的控制搜索过程以求得最优解。遗传算...
    99+
    2023-09-26
    Python 遗传算法 flask Powered by 金山文档
  • 从零开始学习 Java:简单易懂的入门指南之查找算法及排序算法(二十)
    查找算法及排序算法 常见的七种查找算法:1. 基本查找2. 二分查找3. 插值查找4. 斐波那契查找5. 分块查找6. 哈希查找7. 树表查找 四种排序算法:1. 冒泡排序1.1 算法步骤...
    99+
    2023-08-31
    算法 排序算法 学习
  • 从零开始学习如何导出数据的Golang实现
    从零开始学习如何导出数据的Golang实现 在日常的开发过程中,经常会遇到需要将数据导出到文件中的情况。无论是将数据库中的数据导出为csv文件,还是将日志数据导出为文本文件,我们往往需...
    99+
    2024-02-28
    学习 golang 数据导出 csv文件 golang开发 标准库
  • 从零开始学习ASP异步编程,轻松实现Linux编程算法
    ASP异步编程是一种高效的编程方式,它可以让程序在等待某些操作完成的同时,继续执行其他任务。本文将从零开始介绍ASP异步编程的基础知识,并演示如何在Linux平台上实现编程算法。 一、异步编程基础 在传统的同步编程中,程序必须等待某个操作完...
    99+
    2023-11-03
    异步编程 linux 编程算法
  • Node.js Promises 项目实战:从零开始构建一个简单的 HTTP 应用程序
    Node.js Promises 项目实战 1. 环境搭建 首先,我们需要安装 Node.js 和必要的 npm 模块。在您的终端中运行以下命令: npm install -g nodejs npm install express bod...
    99+
    2024-02-13
    Node.js Promises HTTP GET POST express body-parser
  • 从零开始学Python和Spring,教你如何使用数组
    Python和Spring是目前非常流行的编程语言和框架,其中数组是它们的重要组成部分。数组是一种数据结构,可以存储相同类型的数据,并且可以通过索引访问它们。在本文中,我们将从零开始学习Python和Spring中的数组,以及如何使用它们...
    99+
    2023-06-20
    spring 数组 教程
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作