-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharncode.py
More file actions
175 lines (150 loc) · 6.05 KB
/
Copy patharncode.py
File metadata and controls
175 lines (150 loc) · 6.05 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
import ConfigParser, os
import logging
import random
import numpy as np
import extendedarn as arn
from evodevo import Agent
from numpy import array as nparray
from functools import partial
from bitstring import *
from math import exp
import sys
from sys import stdout
from subprocess import call
from utils import *
from utils.bitstrutils import *
from extendedarn import *
log = logging.getLogger(__name__)
### Problem base to use with CellCoDe
class CellProb:
def __init__(self, evaluate, numins, numouts):
self.eval_ = evaluate
self.ninp = numins
self.nout = numouts
self.print_ = printcell
def printcell(cell):
return (cell.input_weights, cell.hidden_weights, cell.output_weights)
def nn(inputs, input_weights, hidden_weights, output_weights):
if len(inputs) > input_weights.shape[0]:
inputs = inputs[:input_weights.shape[0]]
hidenum = hidden_weights.shape[0]
outnum = output_weights.shape[1]
#print inputs.shape, ' ', input_weights.shape, ' ', hidden_weights.shape, ' ', output_weights.shape
K,Z,Y = [],[],[]
for i in range(hidenum):
K.append(sigmoid(inputs, input_weights[:,i]))
#for i in range(hidenum):
# Z.append(sigmoid(K, hidden_weights[:,i]))
for i in range(outnum):
Y.append(sigmoid(K,output_weights[:,i]))
return Y
def sigmoid(inputs,weights):
#print len(inputs), ' ', len(weights)
return 1.0 / (1.0 + exp(-np.dot(inputs, weights)))
class Cell(Agent):
phenotype = None
genotype = None
fitness = None
def __init__(self, config, gcode = None, parentfit = 1e4, **kwargs):
Agent.__init__(self, parentfit)
generator = arn.bindparams(config, arn.generatechromo)
if gcode == None:
gcode = generator()
self.genotype = arn.ARNetwork(gcode, config, **kwargs)
while (self.genotype.numeff == 0 or
self.genotype.numrec == 0 or
self.genotype.numtf == 0):
gcode = generator()
self.genotype = arn.ARNetwork(gcode, config, **kwargs)
#initialize phenotype
weights = self.genotype.eweights - self.genotype.iweights
nump = self.genotype.numtf
numinp = self.genotype.numrec
numout = self.genotype.numeff
class Pheno(): pass
self.phenotype = Pheno()
self.phenotype.input_weights = weights[nump:nump+numinp,:nump]
#print self.phenotype.input_weights
self.phenotype.hidden_weights = weights[:nump,:nump]
#print self.phenotype.hidden_weights
self.phenotype.output_weights = weights[:nump,nump+numinp:]
#print self.phenotype.output_weights
self.fitness = 1e9
def __str__(self):
return "### Agent ###\n%s\n%s: %f" % (self.arn,self.circuit,
self.fitness)
def setARN(self, arn):
self.arn = arn
#TODO: move this inside the problem
def evaluatecircuit(phenotype, test = False, **kwargs):
n = 3
ok=0
intinps = range(pow(2,n))
#if not test:
# intinps = intinps + intinps
#random.shuffle(intinps)
try:
if kwargs['shuffle']:
random.shuffle(intinps)
except KeyError: pass
for i in intinps:
inputs = BitStream(uint = i, length = n)
#print inputs.bin
#not normalized only floated
normalized = nparray([float(inputs.bin[i])
for i in range(n)])
out = nn(normalized,
phenotype.input_weights,
phenotype.hidden_weights,
phenotype.output_weights)
print 'OUT: ', out
out = (0 if out[0] < .5 else 1)
print 'OUT: ', out
if out == inputs[1+inputs[0]]:
ok += 1
#print 'SILENT: ', kwargs['silentmode']
#if not kwargs['silentmode']:
# plotindividual(phenotype,**kwargs)
return len(intinps) - ok
if __name__ == '__main__':
arnconfigfile = '../configfiles/arnsim.cfg'
log.setLevel(logging.DEBUG)
cfg = ConfigParser.ConfigParser()
cfg.readfp(open(arnconfigfile))
proteins=[]
p = CellProb(evaluatecircuit, 3, 1)
nump = 0
try:
f = open(sys.argv[1], 'r')
genome = BitStream(bin=f.readline())
arnet = ARNetwork(genome, cfg)
except:
while nump < 4 or nump > 32 or numeff == 0 or not arnet.receptors:
genome = BitStream(float=random.random(), length=32)
for i in range(cfg.getint('default','initdm')):
genome = dm_event(genome,
.02)
arnet = ARNetwork(genome, cfg, problem = p)
nump = len(arnet.promlist)
numeff = len(arnet.effectors)
offspring = None
themother = Cell(cfg,genome,problem = p )
eval_ = bindparams(cfg, evaluatecircuit)
#plot_ = bindparams(cfg, plotindividual)
pop = [(themother, eval_(themother.phenotype)),
(None,0)]
while pop[0][1] > 0:
offspring = Cell(cfg, bitflipmutation(
pop[0][0].genotype.code,.01), problem = p)
while not offspring.genotype.effectors:
offspring = Cell(cfg, bitflipmutation(
pop[0][0].genotype,.01))
pop[1] = (offspring,eval_(offspring.phenotype))
pop.sort(key = lambda x: x[1])
print pop
#for p in arnet.proteins: print p
f = open('genome.save','w')
f.write(pop[0][0].genotype.code.bin)
f.close
#print genome.bin
#plot_(pop[0][0].genotype)