-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization_example.py
More file actions
42 lines (27 loc) · 896 Bytes
/
Copy pathoptimization_example.py
File metadata and controls
42 lines (27 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import time
import random
from darwin import Environment
from darwin.mutation import WeightedMutationStrategy
class Gene(object):
def __init__(self, value=0):
self.value = value
class Genome(object):
def __init__(self, genes):
self.genes = list([] or genes)
def mutate_genome(genome):
gene = random.choice(genome.genes)
gene.value += random.random() - .5
def fitness(genome):
time.sleep(.0001)
return sum(
gene.value
for gene
in genome.genes
)
def optimized_copy(obj):
return Genome([Gene(gene.value) for gene in obj.genes])
mutation_strategy = WeightedMutationStrategy({mutate_genome: 1})
env = Environment(fitness, mutation_strategy, n_jobs=-1, copy_fn=optimized_copy)
first_individual = Genome(genes=[Gene()])
population = [first_individual]
env.evolve(population, n_generations=10, population_size=10000)