Download modules/deap/examples/pso/basic.py from Johnny5b/portable-ai-runner: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/datasets/Johnny5b/portable-ai-runner/resolve/main/modules/deap/examples/pso/basic.py
- Command line
-
hf download hf://datasets/Johnny5b/portable-ai-runner/modules/deap/examples/pso/basic.py
-
curl -L -o basic.py https://huggingface.co/datasets/Johnny5b/portable-ai-runner/resolve/main/modules/deap/examples/pso/basic.py
3.35 kB
| # This file is part of DEAP. | |
| # | |
| # DEAP 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 <http://www.gnu.org/licenses/>. | |
| import operator | |
| import random | |
| import numpy | |
| import math | |
| from deap import base | |
| from deap import benchmarks | |
| from deap import creator | |
| from deap import tools | |
| creator.create("FitnessMax", base.Fitness, weights=(1.0,)) | |
| creator.create("Particle", list, fitness=creator.FitnessMax, speed=list, | |
| smin=None, smax=None, best=None) | |
| def generate(size, pmin, pmax, smin, smax): | |
| part = creator.Particle(random.uniform(pmin, pmax) for _ in range(size)) | |
| part.speed = [random.uniform(smin, smax) for _ in range(size)] | |
| part.smin = smin | |
| part.smax = smax | |
| return part | |
| def updateParticle(part, best, phi1, phi2): | |
| u1 = (random.uniform(0, phi1) for _ in range(len(part))) | |
| u2 = (random.uniform(0, phi2) for _ in range(len(part))) | |
| v_u1 = map(operator.mul, u1, map(operator.sub, part.best, part)) | |
| v_u2 = map(operator.mul, u2, map(operator.sub, best, part)) | |
| part.speed = list(map(operator.add, part.speed, map(operator.add, 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[:] = list(map(operator.add, part, part.speed)) | |
| 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=2.0, phi2=2.0) | |
| toolbox.register("evaluate", benchmarks.h1) | |
| def main(): | |
| pop = toolbox.population(n=5) | |
| stats = tools.Statistics(lambda ind: ind.fitness.values) | |
| stats.register("avg", numpy.mean) | |
| stats.register("std", numpy.std) | |
| stats.register("min", numpy.min) | |
| stats.register("max", numpy.max) | |
| logbook = tools.Logbook() | |
| logbook.header = ["gen", "evals"] + stats.fields | |
| GEN = 1000 | |
| best = None | |
| for g in range(GEN): | |
| for part in pop: | |
| part.fitness.values = toolbox.evaluate(part) | |
| if not part.best or part.best.fitness < part.fitness: | |
| part.best = creator.Particle(part) | |
| part.best.fitness.values = part.fitness.values | |
| if not best or best.fitness < part.fitness: | |
| best = creator.Particle(part) | |
| best.fitness.values = part.fitness.values | |
| for part in pop: | |
| toolbox.update(part, best) | |
| # Gather all the fitnesses in one list and print the stats | |
| logbook.record(gen=g, evals=len(pop), **stats.compile(pop)) | |
| print(logbook.stream) | |
| return pop, logbook, best | |
| if __name__ == "__main__": | |
| main() | |