-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
266 lines (231 loc) · 6.92 KB
/
main.js
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
class Node{
constructor(category,type,val,children){
this.category = category
this.type=type
this.val=val
if (children) this.children=children
else this.children=[]
}
addChild(node){
this.children.push(node)
}
addChildInRandomPosition(node){
if (this.children.length === 0) {
this.children = [node]
} else {
let randIndex = nSidedDie(this.children.length + 1)
this.children= this.children
.slice(0,randIndex)
.concat([node])
.concat(this.children.slice(randIndex))
}
}
output(){
console.log(this.type,this.val,this.children)
}
}
let opsLeft
let currentGeneration=[]
let mainLoop,numberOfGenerations=0
let MODEL = [1,2,3]
function startEvolving(){
opsLeft=0,currentGeneration=[],mainLoop,numberOfGenerations=0,highestFitness=0;
pause()
console.clear()
let arr=JSON.parse(document.querySelector('input').value)
if (!Array.isArray(arr)) return
MODEL=arr
for (let i=5;i>=0;i--){
let nextProgram = document.getElementById('program'+i)
nextProgram.innerText=""
}
for(let i=0;i<500;i++){
let treeRoot = generateSeedTree(MAX_INITIAL_DEPTH,MAX_NUMBER_OF_INITIAL_ACTIONS)
let program = generateProgram(treeRoot)
let output = evaluateProgram(program)
if (output) {
let fitVal = fitness(MODEL,output,program.length)
currentGeneration.push(
{"tree":treeRoot,
"fitness":fitVal
}
)
}
}
continueLoop()
}
function continueLoop(){
pause()
mainLoop = setInterval(function(){
genOutput.innerText=numberOfGenerations
currentGeneration= evolveNextGeneration(currentGeneration)
numberOfGenerations+=1
},0)
}
function pause(){
clearInterval(mainLoop)
}
function pushProgramToDiv(program,output,fitness){
for (let i=5;i>0;i--){
let nextProgram = document.getElementById('program'+i)
let prevProgram = document.getElementById('program'+(i-1))
nextProgram.innerText=prevProgram.innerText
}
let firstProgram = document.getElementById('program0')
firstProgram.innerText=program+'\n\n output: ['+ output +']\n fitness: '+ fitness
}
//const MODEL = [1,1,2,3,5,8]
const MAX_INITIAL_DEPTH=5
const MAX_NUMBER_OF_INITIAL_ACTIONS = 6
//GREATER value = higher mutation rate, minimum val=2
const MUTATE_LOWER_LEVEL_EXPRESSION_RATE = 4
//LOWER value = higher mutation rate, minimum val=2
const TOP_LEVEL_MUTATION_RATE = 20
const MAX_DESCENT_DEPTH=3
const MAX_ADDITION_DEPTH=3
const NUM_OF_GENERATIONS=500
let highestFitness=0;
const fitOutput=document.getElementById("currentFitness")
const genOutput=document.getElementById("currentGeneration")
function evolveNextGeneration(prevGeneration){
let totalFitness = 0
prevGeneration.forEach(program=>{
if (program.fitness) totalFitness+=program.fitness
})
let avgFitness = totalFitness/prevGeneration.length
fitOutput.innerText=avgFitness
let sortedGen = prevGeneration.sort((a,b)=>{return b.fitness - a.fitness})
let nextGen=[],index=0
for (let i=0;i<400;i++){
if (i%40==0)index+=1
if (!sortedGen[index]) {
index+=1
continue
}
nextGen.push(sortedGen[index])
let mutatedSurvivorTree = selectMutationType(sortedGen[index].tree,MAX_DESCENT_DEPTH,MAX_ADDITION_DEPTH)
let program = generateProgram(mutatedSurvivorTree)
let output = evaluateProgram(program)
if (output) {
let fitVal = fitness(MODEL,output,program.length)
if (fitVal>highestFitness){
highestFitness=fitVal
pushProgramToDiv(program,output,fitVal)
}
nextGen.push(
{"tree":mutatedSurvivorTree,
"fitness":fitVal
}
)
}
}
return nextGen
}
function fitness(modelArr,actual,programLength){
let fitVal=100 - Math.abs(modelArr.length-actual.length)*5 //- programLength/20
for (let i=0;i<modelArr.length;i++){
if (typeof actual[i] == typeof modelArr[i]){
fitVal-=Math.abs(modelArr[i]-actual[i])*10//*(10-i*2)
}
else{fitVal-=10}
}
return fitVal
}
function evaluateProgram(program){
try{
let output= new Function(program)
return output()
}
catch(error){
return undefined
}
}
function generateSeedTree(maxDepth,maxNumberOfActions){
let numberOfActions = nSidedDie(maxNumberOfActions)+1
let rootNode= new Node('topLevel',null,[]);
while (0<numberOfActions){
opsLeft=maxDepth
let child=chooseRandomFromArr(programActions)()
if (child) rootNode.addChild(child)
numberOfActions--
}
return rootNode
}
function generateProgram(rootNode){
let program='let output = [];\nfunction fun(a,b,c){\n'
program+=astToText(rootNode)+'}\nfun('+MODEL.length+',0,0)\n return output'
return program
}
function nSidedDie(n){
if (n === 0 || n === 1) return 0;
return Math.round(Math.random()*(n-1))
}
function selectMutationType(rootNode,maxDescentDepth,maxAdditionDepth){
let choice = nSidedDie(MUTATE_LOWER_LEVEL_EXPRESSION_RATE)
if (choice==1) rootNode = MutateTopLevelAction(rootNode,maxDescentDepth)
else if (choice>=2) {
rootNode = MutateLowerLevelExpression(rootNode,maxDescentDepth,maxAdditionDepth)
}
return rootNode
}
//action mutations
function MutateTopLevelAction(rootNode,maxDepth){
let choice = nSidedDie(TOP_LEVEL_MUTATION_RATE)
if (choice==2 && rootNode.children.length>1) rootNode = deleteRandomAction(rootNode)
else if (choice==1) rootNode = replaceRandomAction(rootNode,maxDepth)
else if (choice==0) rootNode = addRandomAction(rootNode,maxDepth)
return rootNode
}
function deleteRandomAction(rootNode){
let choice = nSidedDie(rootNode.children.length)
rootNode.children.splice(choice,1)
return rootNode
}
function addRandomAction(rootNode,maxDepth){
opsLeft=maxDepth
let child=chooseRandomFromArr(programActions)()
if (child) {
rootNode.addChildInRandomPosition(child)
}
return rootNode
}
function replaceRandomAction(rootNode,maxDepth){
opsLeft=maxDepth
let child=chooseRandomFromArr(programActions)()
let choice = nSidedDie(rootNode.children.length)
rootNode.children[choice]=child
return rootNode
}
///expression and conditional mutations
function MutateLowerLevelExpression(rootNode,maxDescentDepth,maxAdditionDepth){
if (!rootNode) return
let roll = nSidedDie(rootNode.children.length)
let child = rootNode.children[roll]
let randomDescentDepth=Math.max(1, nSidedDie(maxDescentDepth))
let randomAdditionDepth=nSidedDie(maxAdditionDepth)
let newNode = descendToDepthAndMutate(child,randomDescentDepth,randomAdditionDepth)
rootNode.children[roll] = newNode
return rootNode
}
function descendToDepthAndMutate(node,descentDepth,AdditionDepth){
if (descentDepth>0 && node.children!=null && node.children.length !== 0){
let roll = nSidedDie(node.children.length)
let child = node.children[roll]
// if (node.children.length == 0) debugger
// roll
let deepResult = descendToDepthAndMutate(
child,
descentDepth-1,
AdditionDepth
)
node.children[roll] = deepResult
return node
}
else return mutate(node,AdditionDepth)
}
function mutate(node,maxExtraDepth){
opsLeft = maxExtraDepth
if (node.category=="expression") return chooseExpression()
else if (node.category=="condition") return chooseRandomFromArr(conditions)
else if (node.category=="conditionParent") return chooseRandomCondition()
}