-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
93 lines (50 loc) · 2.26 KB
/
test.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from mlagents_envs.environment import UnityEnvironment
from gym_unity.envs import UnityToGymWrapper
import evolvepy as ep
import numpy as np
from matplotlib import pyplot as plt
import unity_utils as uutils
if __name__ == "__main__":
args = {"env_path": "D:\\Github\\ml-agents\\builds\\3dball_single\\UnityEnvironment.exe"}
evaluator = ep.evaluator.ProcessEvaluator(uutils.Unity3DBallEvaluator, args = args)
sizes = [20, 20, 2] # Units of each layer
input_size = 8 # Network input size (= observation size)
names = []
chr_sizes = []
types = []
ranges = []
for i in range(len(sizes)):
total_weights = input_size*sizes[i]
names.append("layer"+str(i)+"w")
names.append("layer"+str(i)+"b")
chr_sizes.append(total_weights)
chr_sizes.append(sizes[i])
ranges.append((-1.0, 1.0))
ranges.append((-1.0, 1.0))
types.append(np.float32)
types.append(np.float32)
input_size = sizes[i]
descriptor = ep.generator.Descriptor(chr_sizes, ranges, types, names)
import multiprocessing as mp
population_size = (100//mp.cpu_count())*mp.cpu_count()
population_size = 12
first = ep.generator.Layer()
sort = ep.generator.Sort()
first.next = sort
concat = ep.generator.Concatenate()
predation = ep.generator.RandomPredation(int(0.75*population_size))
combine = ep.generator.CombineLayer(ep.generator.selection.tournament, ep.generator.crossover.one_point)
mutation = ep.generator.mutation.NumericMutationLayer(ep.generator.mutation.sum_mutation, 1.0, 0.9, (-0.5, 0.5))
filter0 = ep.generator.FilterFirsts(int(np.floor(0.95*population_size)))
sort.next = predation
predation.next = combine
combine.next = mutation
mutation.next = filter0
filter0.next = concat
filter1 = ep.generator.FilterFirsts(int(np.ceil(0.05*population_size)))
sort.next = filter1
filter1.next = concat
generator = ep.generator.Generator(first_layer=first, last_layer=concat, descriptor=descriptor)
dyn_mut = ep.callbacks.DynamicMutation([mutation.name], refinement_patience=5, exploration_patience=5, refinement_steps=5)
evolver = ep.Evolver(generator, evaluator, population_size, [dyn_mut])
evolver.evolve(1)