FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code/code/rencode2.py at master · rmlopes/code · GitHub
rmlopes
code
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
code
/
code
/
rencode2.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
284 lines (251 loc) · 10.7 KB
Breadcrumbs
code
/
code
/
rencode2.py
Copy path
File metadata and controls
284 lines (251 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import
logging
from
functools
import
partial
from
bitstring
import
BitStream
import
math
import
arn
import
rencode
from
rencode
import
evaluatecircuit
,
regressionfun
,
nnlikefun
,
mergefun
,
P
,
cleanpairs
from
arnmiguel
import
*
from
evodevo
import
Problem
,
Agent
from
utils
import
*
from
utils
.
bitstrutils
import
*
from
utils
.
mathlogic
import
*
log
=
logging
.
getLogger
(
__name__
)
def
printrencode2
(
phenotype
,
**
kwargs
):
return
rencode
.
printdotcircuit
(
phenotype
.
circuits
[
phenotype
.
output_idx
],
**
kwargs
)
def
printmultiplecircuit
(
phenotype
,
labels
=
None
,
arnet
=
None
):
circuit
=
phenotype
.
getcircuit
(
phenotype
.
output_idx
)
if
not
arnet
:
arnet
=
phenotype
.
arnet
s
=
'digraph best {
\n
ordering = out;
\n
'
for
c
in
circuit
:
shape
=
'oval'
if
c
[
0
]
in
arnet
.
effectorproms
:
shape
=
'hexagon'
elif
c
[
0
]
in
arnet
.
receptorproms
:
shape
=
'rectangle'
s
+=
'%i [label="%s",shape="%s"];
\n
'
%
(
c
[
0
],
c
[
1
],
shape
)
#else labels[c[1]])
for
inp
in
c
[
2
]:
aux
=
"dir=back"
if
inp
<
0
:
aux
+=
",style=dotted"
s
+=
'%i -> %i [%s];
\n
'
%
(
c
[
0
],
abs
(
inp
),
aux
)
s
+=
'}'
return
s
### Agent model to use with this CoDe module
class
ARNGPAgent
(
Agent
):
genotype
=
None
phenotype
=
None
fitness
=
None
def
__init__
(
self
,
config
,
problem
,
gcode
=
None
,
parent
=
None
):
Agent
.
__init__
(
self
,
parent
)
generator
=
bindparams
(
config
,
self
.
generate
)
if
gcode
==
None
:
numprods
=
0
numtfs
=
0
while
not
numprods
or
not
numtfs
:
arnet
=
ARNetwork
(
generator
(),
config
,
problem
=
problem
)
numprods
=
arnet
.
numeff
numtfs
=
arnet
.
numtf
else
:
arnet
=
ARNetwork
(
gcode
,
config
,
problem
=
problem
)
self
.
genotype
=
arnet
self
.
phenotype
=
Phenotype
(
arnet
,
problem
)
self
.
problem
=
problem
self
.
fitness
=
1e9
def
__str__
(
self
):
return
"### Agent ###
\n
%s
\n
%s: %f"
%
(
self
.
arn
,
self
.
phenotype
,
self
.
fitness
)
def
pickled
(
self
):
return
(
self
.
genotype
.
code
.
to01
(),
self
.
phenotype
.
output_idx
)
def
print_
(
self
):
return
printmultiplecircuit
(
self
.
phenotype
)
class
DMAgent
(
ARNGPAgent
):
def
__init__
(
self
,
config
,
problem
,
gcode
=
None
,
parent
=
None
):
self
.
generate
=
arn
.
generatechromo
ARNGPAgent
.
__init__
(
self
,
config
,
problem
,
gcode
,
parent
)
class
RndAgent
(
ARNGPAgent
):
def
__init__
(
self
,
config
,
problem
,
gcode
=
None
,
parent
=
None
):
self
.
generate
=
partial
(
arn
.
generatechromo_rnd
,
genomesize
=
32
*
pow
(
2
,
config
.
getint
(
'default'
,
'initdm'
)))
ARNGPAgent
.
__init__
(
self
,
config
,
problem
,
gcode
,
parent
)
class
Phenotype
:
def
__init__
(
self
,
arnet
,
problem
):
self
.
funskel
=
regressionfun
self
.
problem
=
problem
self
.
products
=
arnet
.
effectorproms
self
.
circuits
=
[]
self
.
graph
=
arnet
.
ebindings
-
arnet
.
ibindings
cleanpairs
(
self
.
graph
)
self
.
arnet
=
arnet
self
.
output_idx
=
0
if
problem
.
nout
==
1
:
for
i
in
range
(
len
(
self
.
products
)):
self
.
output_idx
=
i
queue
=
self
.
getinputs
(
arnet
.
numtf
+
arnet
.
numrec
+
i
)
fset
=
problem
.
funs
if
not
queue
:
fset
=
problem
.
terms
circuit
=
[(
arnet
.
effectors
[
i
][
0
],
self
.
problem
.
nodemap_
(
arnet
.
effectors
[
i
][
4
],
fset
),
[
q
[
0
]
for
q
in
queue
])]
self
.
circuits
.
append
(
self
.
buildcircuit
(
queue
,
circuit
))
else
:
queue
=
[]
circuit
=
[]
#outputs = self.selectoutputs(problem.nout, problem)
#for o in outputs:
#slice-out the index of the output
# circuit.append(o[:3])
# queue.extend(self.getinputs(arnet.numtf+arnet.numrec+o[3]))
for
i
in
range
(
arnet
.
numeff
):
inps
=
self
.
getinputs
(
arnet
.
numtf
+
arnet
.
numrec
+
i
)
fset
=
problem
.
funs
if
not
inps
:
fset
=
problem
.
terms
circuit
.
append
((
arnet
.
effectors
[
i
][
0
],
self
.
problem
.
nodemap_
(
arnet
.
effectors
[
i
][
4
],
fset
),
[
inp
[
0
]
for
inp
in
inps
]))
queue
.
extend
(
inps
)
queue
.
sort
(
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
self
.
circuits
.
append
(
self
.
buildcircuit
(
queue
,
circuit
))
#self.circuitstr = map(lambda x: compile(x,'<string>','eval'),
# evaluatecircuit(self.circuits[0], mergefun, dict(),
# *problem.terms, nout=problem.nout))
#self.replaceinp = re.compile('inputs')
def
__len__
(
self
,
index
=
0
):
return
len
(
self
.
circuits
[
index
])
def
__call__
(
self
,
*
inputs
,
**
kwargs
):
# #print self.circuitstr
#vardict = dict(globals())
# globals()['inputs']=inputs
#print vardict
#return map(lambda x: eval(x,globals(),{'inputs':inputs}), self.circuitstr)
#print r
#return r
return
evaluatecircuit
(
self
.
getcircuit
(
self
.
output_idx
),
self
.
funskel
,
dict
(),
*
inputs
,
nout
=
self
.
problem
.
nout
)
def
__eq__
(
self
,
other
):
if
len
(
self
.
circuits
)
!=
len
(
other
.
circuits
):
return
False
for
i
in
range
(
len
(
self
.
circuits
)):
if
self
.
circuits
[
i
]
!=
other
.
circuits
[
i
]:
return
False
return
True
def
selectoutputs
(
self
,
n
,
problem
):
'''Selects the output nodes by number of input edges.'''
temp
=
list
()
for
i
in
range
(
self
.
arnet
.
numeff
):
inps
=
self
.
getinputs
(
self
.
arnet
.
numtf
+
self
.
arnet
.
numrec
+
i
)
fset
=
problem
.
funs
if
not
inps
:
fset
=
problem
.
terms
temp
.
append
((
self
.
arnet
.
effectors
[
i
][
0
],
self
.
problem
.
nodemap_
(
self
.
arnet
.
effectors
[
i
][
4
],
fset
),
[
inp
[
0
]
for
inp
in
inps
],
i
))
#temp.sort(key=lambda x: len(x[2]), reverse=True)
return
temp
[:
n
]
def
getcircuit
(
self
,
index
=
0
):
return
self
.
circuits
[
index
]
def
buildcircuit
(
self
,
queue
,
circuit
=
[],
force_inputs
=
False
):
if
not
queue
:
#if len(circuit) > 1 or force_inputs:
circuit
.
extend
(
self
.
getinputnodes
())
return
circuit
blacklist
=
[
c
[
0
]
for
c
in
circuit
]
while
True
:
if
not
queue
:
circuit
.
extend
(
self
.
getinputnodes
())
return
circuit
pnext
=
queue
.
pop
(
0
)
if
pnext
[
0
]
not
in
blacklist
+
self
.
arnet
.
receptorproms
:
break
;
blacklist
.
append
(
pnext
[
0
])
index
=
self
.
arnet
.
promlist
.
index
(
pnext
[
0
])
signature
=
self
.
arnet
.
proteins
[
index
][
4
]
fun
=
self
.
problem
.
nodemap_
(
signature
,
self
.
problem
.
funs
)
inputs
=
self
.
getinputs
(
index
)
inputs
=
filter
(
lambda
x
:
x
[
0
]
not
in
blacklist
,
inputs
)
ar
=
self
.
problem
.
arity
[
fun
]
if
ar
>
0
:
inputs
=
inputs
[:
ar
]
if
not
inputs
:
fun
=
str
(
random
.
choice
([
0
,
1
]))
#fun = self.problem.nodemap_(signature,self.problem.terms)
circuit
.
append
((
pnext
[
0
],
fun
,[
i
[
0
]
for
i
in
inputs
]))
queue
.
extend
(
inputs
)
queue
.
sort
(
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
return
self
.
buildcircuit
(
queue
,
circuit
)
def
getinputs
(
self
,
index
):
inputs
=
[]
for
e
,
i
in
zip
(
self
.
graph
[:,
index
],
range
(
self
.
graph
.
shape
[
0
])):
if
e
>
0
:
inputs
.
append
(((
self
.
arnet
.
promlist
+
self
.
arnet
.
receptorproms
)[
i
],
e
))
inputs
.
sort
(
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
return
inputs
def
getinputnodes
(
self
):
return
map
(
lambda
x
: (
x
[
0
],
self
.
problem
.
terms
[
x
[
0
]],
[]),
self
.
arnet
.
receptors
)
def
printgraph
(
self
):
s
=
'digraph best {
\n
ordering = out;
\n
'
shape
=
'hexagon'
arnet
=
self
.
arnet
labelidx
=
0
for
outprom
in
arnet
.
effectorproms
:
s
+=
'%i [label="%s",shape="hexagon"];
\n
'
%
(
outprom
,
labelidx
)
for
e
,
i
in
zip
(
self
.
graph
[:,
arnet
.
numtf
+
arnet
.
numrec
+
labelidx
],
range
(
self
.
graph
.
shape
[
0
])):
if
e
>
0
:
s
+=
'%i -> %i [dir=back,weight=%i];
\n
'
%
\
(
outprom
, (
arnet
.
promlist
+
arnet
.
receptorproms
)[
i
],
e
)
labelidx
+=
1
for
tf
,
idx
in
zip
(
arnet
.
promlist
,
range
(
arnet
.
numtf
)):
s
+=
'%i [label="%s"];
\n
'
%
(
tf
,
labelidx
)
for
e
,
i
in
zip
(
self
.
graph
[:,
idx
],
range
(
self
.
graph
.
shape
[
0
])):
if
e
>
0
:
s
+=
'%i -> %i [dir=back,weight=%i];
\n
'
%
\
(
tf
, (
arnet
.
promlist
+
arnet
.
receptorproms
)[
i
],
e
)
labelidx
+=
1
for
rec
in
arnet
.
receptorproms
:
s
+=
'%i [label="%s",shape="rectangle"];
\n
'
%
(
rec
,
labelidx
)
labelidx
+=
1
s
+=
'}'
return
s
###########################################################################
### Test ###
###########################################################################
if
__name__
==
'__main__'
:
import
ConfigParser
import
random
from
bitstring
import
BitStream
from
bitstringutils
import
dm_event
from
arn
import
ARNetwork
from
evodevo
import
Problem
log
.
setLevel
(
logging
.
DEBUG
)
cfg
=
ConfigParser
.
ConfigParser
()
cfg
.
readfp
(
open
(
'test-rencode.cfg'
))
arncfg
=
ConfigParser
.
ConfigParser
()
arncfg
.
readfp
(
open
(
'arnsim-rencode2.cfg'
))
proteins
=
[]
nump
=
0
while
nump
<
3
:
genome
=
BitStream
(
float
=
random
.
random
(),
length
=
32
)
for
i
in
range
(
5
):
genome
=
dm_event
(
genome
,
.02
)
arnet
=
ARNetwork
(
genome
,
arncfg
)
nump
=
len
(
arnet
.
promlist
)
for
p
in
arnet
.
proteins
:
print
p
prob
=
Problem
(
defaultnodemap
,
regressionfun
,
None
)
circuit
=
buildcircuit
(
arnet
,
prob
,
False
)
_printcircuit
(
circuit
)
print
printdotcircuit
(
circuit
,
prob
.
labels
)
Back
|
FazBrowse Home
|
New Git URL