-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (173 loc) · 5.61 KB
/
main.py
File metadata and controls
195 lines (173 loc) · 5.61 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import cgp
import numpy as np
from time import time
from src.utils import make_run_dir
from src.environment import Environment
from src.organism import MaterialProperties
from src.evolutionary_algorithm import CGPGeneticAlgorithm
def define_seedling():
"""Produces the seedling parameters.
The seedling is the initial starting point of development for each organism.
Returns:
A dictionary containing parameters of the seedling.
"""
nodes = np.array([[0.0, 0.0],
[12.5, 21.650635],
[25.0, 0.0],
[37.5, 21.650635],
[50.0, 0.0],
[62.5, 21.650635],
[75.0, 0.0],
[87.5, 21.650635],
[100.0, 0.0]])
edges = np.array([[0, 1], [0, 2], [1, 2], [1, 3], [2, 3], [2, 4], [3, 4], [3, 5], [4, 5], [4, 6], [5, 6],
[5, 7], [6, 7], [6, 8], [7, 8]])
cs_areas = np.full((edges.shape[0],), 1.0)
node_constraints = np.array([0, 2, 4, 6, 8])
materials = MaterialProperties()
materials.names = ["Steel"] * edges.shape[0]
materials.young_mods = np.full((edges.shape[0],), 7e10)
materials.densities = np.full((edges.shape[0],), 7872)
materials.poisson_ratios = np.full((edges.shape[0],), 0.3)
seedling = {"nodes": nodes, "edges": edges, "cs_areas": cs_areas, "materials": materials,
"node_constraints": node_constraints}
return seedling
def define_environment():
reactions = np.array([[1, 1], # Node 0
[0, 0], # Node 1
[0, 0], # Node 2
[0, 0], # Node 3
[0, 0], # Node 4
[0, 0], # Node 5
[0, 0], # Node 6
[0, 0], # Node 7
[0, 1]]) # Node 8
loads = np.array([[0, 0], # Node 0
[0, 0], # Node 1
[0, 0], # Node 2
[0, 0], # Node 3
[0, -17000], # Node 4
[0, 0], # Node 5
[0, 0], # Node 6
[0, 0], # Node 7
[0, 0]]) # Node 8
environment = Environment(reactions=reactions, loads=loads)
return environment
if __name__ == "__main__":
fixed_seed = None
if fixed_seed is None:
seed = int(time() * 1000) % (2 ** 32 - 1)
else:
seed = fixed_seed
print(f"--- Running Experiment with Seed: {seed} ---")
grn_type = "node-edge-etg-advanced-agg" # Options: "node-edge-etg", "node-edge-etg-advanced-agg", "node-edge-etg-with-neighbors"
run_dir = make_run_dir(grn_type)
seedling = define_seedling()
environment = define_environment()
if grn_type == "node-edge-etg":
genome_params_edge = {
"n_inputs": 2,
"n_outputs": 1,
"n_columns": 8,
"n_rows": 2,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
genome_params_node = {
"n_inputs": 2,
"n_outputs": 2,
"n_columns": 8,
"n_rows": 2,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
elif grn_type == "node-edge-etg-advanced-agg":
genome_params_edge = {
"n_inputs": 10,
"n_outputs": 1,
"n_columns": 10,
"n_rows": 3,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
genome_params_node = {
"n_inputs": 10,
"n_outputs": 2,
"n_columns": 10,
"n_rows": 3,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
elif grn_type == "node-edge-etg-with-neighbors":
genome_params_edge = {
"n_inputs": 4,
"n_outputs": 1,
"n_columns": 10,
"n_rows": 5,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
genome_params_node = {
"n_inputs": 4,
"n_outputs": 2,
"n_columns": 10,
"n_rows": 5,
"levels_back": 3,
"primitives": (
cgp.Add,
cgp.Sub,
cgp.Mul,
cgp.ConstantFloat,
),
}
else:
print('new methods should be implemented')
ga = CGPGeneticAlgorithm(
seedling=seedling,
environment=environment,
genome_params_node=genome_params_node,
genome_params_edge=genome_params_edge,
generations=150,
population_size=512,
population_decay=1.0,
min_population_size=128,
run_dir=run_dir,
initial_epsilon=1.0,
epsilon_taper=0.4,
crossover_rate= 0.4,
num_devo_steps=10,
top_k=32,
grn_type=grn_type,
verbose=True,
num_threads=1,
seed=seed
)
start_time = time()
ga.fit()
print(f"Finished in {round((time()-start_time)/60, 3)} minutes")
print("The end of program")