<jupyter_start><jupyter_text>Original Source: https://github.com/DEAP/deap/blob/master/examples/ga/onemax_numpy.pyDEAP is free software: you can redistribute it and/or modify   it under the terms of the GNU Lesser General Public License as   published by the Free Software Foundation, either version 3 of   the License, or (at your option) any later version.   DEAP is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the   GNU Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public   License along with DEAP. If not, see .<jupyter_code>#@title Install DEAP
!pip install deap --quiet
#@title Imports
import operator
import random
import math
import time

import numpy as np

from deap import base
from deap import benchmarks
from deap import creator
from deap import tools
from IPython.display import clear_output
#@title Setup Fitness Criteria
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Particle", np.ndarray, fitness=creator.FitnessMax, speed=list, 
    smin=None, smax=None, best=None)
#@title PSO Functions
def generate(size, pmin, pmax, smin, smax):
  part = creator.Particle(np.random.uniform(pmin, pmax, size)) 
  part.speed = np.random.uniform(smin, smax, size)
  part.smin = smin
  part.smax = smax
  return part

def updateParticle(part, best, phi1, phi2):
  u1 = np.random.uniform(0, phi1, len(part))
  u2 = np.random.uniform(0, phi2, len(part))
  v_u1 = u1 * (part.best - part)
  v_u2 = u2 * (best - part)
  part.speed += v_u1 + v_u2
  for i, speed in enumerate(part.speed):
    if abs(speed) < part.smin:
      part.speed[i] = math.copysign(part.smin, speed)
    elif abs(speed) > part.smax:
      part.speed[i] = math.copysign(part.smax, speed)
  part += part.speed
#@title Evaluation Function
distance = 575 #@param {type:"slider", min:10, max:1000, step:5}
def evaluate(individual):
  v = individual[0] if individual[0] > 0 else 0  #velocity
  a = individual[1] * math.pi / 180  #angle to radians
  return ((2*v**2 * math.sin(a) * math.cos(a))/9.8 - distance)**2,
#@title Add Functions to Toolbox
toolbox = base.Toolbox()
toolbox.register("particle", 
                 generate, size=2, pmin=-6, pmax=6, smin=-3, smax=3)
toolbox.register("population", 
                 tools.initRepeat, list, toolbox.particle)
toolbox.register("update", 
                 updateParticle, phi1=200, phi2=200)
toolbox.register("evaluate", evaluate)
#@title Code to Plot the Expression Tree
import matplotlib.pyplot as plt

def plot_population(pop):  
  xs = [x for x,_ in pop]
  ys = [y for _,y in pop]    
  plt.scatter(xs,ys)  
  plt.show()
#@title Run the Evolution
random.seed(64)
    
pop = toolbox.population(n=500)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)

logbook = tools.Logbook()
logbook.header = ["gen", "evals"] + stats.fields

GEN = 100
best = None

for g in range(GEN):          
  for part in pop:
    part.fitness.values = tuple(np.subtract((0,), toolbox.evaluate(part)))        
    if part.best is None or part.best.fitness < part.fitness:
      part.best = creator.Particle(part)
      part.best.fitness.values = part.fitness.values
    if best is None or best.fitness < part.fitness:
      best = creator.Particle(part)
      best.fitness.values = part.fitness.values
  for part in pop:
    toolbox.update(part, best)
  
  if (g+1) % 10 == 0:
    logbook.record(gen=g, evals=len(pop), **stats.compile(pop))      
    clear_output()
    print(best)
    plot_population(pop) 
    print(logbook.stream)
    time.sleep(1)


v, a = best
a = a * math.pi / 180  #angle to radians
distance = (2*v**2 * math.sin(a) * math.cos(a))/9.8
print(distance)<jupyter_output>574.9961005996289
{'fraction_non_alphanumeric': 0.12221095334685599, 'fraction_numerical': 0.02281947261663286, 'mean_word_length': 3.7358943577430974, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '142382', 'license': 'Apache-2.0', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1335, 'n_words': 399, 'path': 'EDL_4_PSO.ipynb', 'repo_name': 'galleon/Evolutionary-Deep-Learning', 'size': '3944'}