-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
238 lines (174 loc) · 6.77 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
var neatjs = require('neatjs');
///////////////////////////////////////////////////
// seed creation
var weightRange = 2;
var connectionProportion = 1;
var ins = 2;
//var ins = 1;
//var outs = 1;
var outs = 2;
var seedCount = 5;
var initialPopulationSeeds = [];
// create initial seed genomes for coming population(s members)
for( var i=0; i < seedCount; i++ ) {
//clear out genome IDs and innovation IDs
// -> not sure why / if this is needed?
neatjs.neatGenome.Help.resetGenomeID();
// NeatGenome.Help.resetInnovationID();
var neatGenome = neatjs.neatGenome.Help.CreateGenomeByInnovation(
ins,
outs,
{
connectionProportion: connectionProportion,
connectionWeightRange: weightRange
}
);
initialPopulationSeeds.push( neatGenome );
}
console.log( initialPopulationSeeds );
///////////////////////////////////////////////////
// Interactive Evolution Computation (IEC) setup
var np = new neatjs.neatParameters();
// defaults taken from
// https://github.com/OptimusLime/win-gen/blob/d11e6df5e7b8948f292c999ad5e6c24ab0198e23/old/plugins/NEAT/neatPlugin.js#L63
// https://github.com/OptimusLime/win-neat/blob/209f00f726457bcb7cd63ccc1ec3b33dec8bbb66/lib/win-neat.js#L20
np.pMutateAddConnection = .13;
np.pMutateAddNode = .13;
np.pMutateDeleteSimpleNeuron = .00;
np.pMutateDeleteConnection = .00;
np.pMutateConnectionWeights = .72;
np.pMutateChangeActivations = .02;
np.pNodeMutateActivationRate = 0.2;
np.connectionWeightRange = 3.0;
np.disallowRecurrence = true;
// IEC options taken from
// https://github.com/OptimusLime/win-Picbreeder/blob/33366ef1d8bfd13c936313d2fdb2afed66c31309/html/pbHome.html#L95
// https://github.com/OptimusLime/win-Picbreeder/blob/33366ef1d8bfd13c936313d2fdb2afed66c31309/html/pbIEC.html#L87
var iecOptions = {
initialMutationCount : 5,
postMutationCount : 5 // AKA mutationsOnCreation
};
var iecGenerator = new neatjs.iec( np, initialPopulationSeeds, iecOptions );
//var iecGenerator = neatjs.iec.GenericIEC;
// iecGenerator( np, seeds, {postMutationCount: mutationsOnCreation} );
//var co = iecGenerator.createNextGenome.call(iecGenerator, genomeParents);
///////////////////////////////////////////////////
// Create first population from seeds
var currentPopulationIndex = 0;
var populations = {};
var populationSize = 10;
var firstPopulation = [];
for( var i=0; i < populationSize; i++ ) {
// individuals in the first population have no actual parents;
// instead they are mutations of some random seed genome:
var onePopulationMember = iecGenerator.createNextGenome( [] );
firstPopulation.push( onePopulationMember );
}
populations[currentPopulationIndex] = firstPopulation;
displayCurrentGeneration();
renderPopulation( currentPopulationIndex );
// console.log( np );
// console.log( populations );
function renderPopulation( populationIndex ) {
var numberOfSeries = 4096; // maximum umber allowed according to the Web Audio API
var populationToRender = populations[populationIndex];
// $('#population-container').empty();
for( var i=0; i < populationToRender.length; i++ ) {
var oneMember = populationToRender[i];
var oneMemberCPPN = oneMember.offspring.networkDecode();
var oneMemberOutputs = [];
for( var j=0; j < numberOfSeries; j++ ) {
var rangeFraction = j / (numberOfSeries-1);
var yInputSignal = lerp( -1, 1, rangeFraction );
var extraInput = Math.sin(Math.abs(10*yInputSignal));
// var inputSignals = [1, Math.abs(yInputSignal), yInputSignal]; // d(istance), input
var inputSignals = [Math.abs(yInputSignal), extraInput]; // d(istance), input
//var inputSignals = [Math.abs(yInputSignal)]; // d(istance), input
oneMemberCPPN.clearSignals();
oneMemberCPPN.setInputSignals( inputSignals );
oneMemberCPPN.recursiveActivation();
oneMemberOutputs.push(
[j, oneMemberCPPN.getOutputSignal(0), oneMemberCPPN.getOutputSignal(1)] );
}
// var oneMemberDiv = $('<div/>', {'id': 'member-container-'+i});
// var oneMemberGraphContainer = $('<div/>', {'id': 'graph-'+i});
// oneMemberDiv.append( oneMemberGraphContainer );
// $('#population-container').append( oneMemberDiv );
// ...bah, I put all the html by hand into index.html, as Dygraph doesn't
// pick up dynamically created elements...
new Dygraph(
document.getElementById("graph-"+i),
oneMemberOutputs,
{
labels: ["time (frequency?) domain", "modulation", "carrier" ],
valueRange: [-1, 1]
}
);
}
}
function evolveNextGeneration() {
// let's get all selected individuals in the UI, to use as parents
var parentIndexes = [];
$( "input[name^='member-']:checked" ).each(function(){
parentIndexes.push( parseInt( $(this).attr("name").substring(7) ) );
});
if( parentIndexes.length < 1 ) {
alert("At least one parent needs to be selected for the next generation.");
return;
}
var currentPopulation = populations[currentPopulationIndex];
var parents = [];
$.each( parentIndexes, function( oneParentIndex, value ) {
parents.push( currentPopulation[oneParentIndex].offspring );
});
console.log( parents );
// let's create a new population from the chosen parents
var newPopulation = [];
for( var i=0; i < populationSize; i++ ) {
var onePopulationMember = iecGenerator.createNextGenome( parents );
newPopulation.push( onePopulationMember );
}
currentPopulationIndex++;
populations[currentPopulationIndex] = newPopulation;
displayCurrentGeneration();
renderPopulation( currentPopulationIndex );
$( "input[name^='member-']" ).each( function(){
$(this).attr( 'checked', false );
});
}
$(function() {
$(".evolve").click( function() {
evolveNextGeneration();
});
});
/*
var oneCPPN = firstPopulation[0].offspring.networkDecode();
var numberOfSeries = 4096; // maximum umber allowed according to the Web Audio API
var testOutputs = [];
for( var i=0; i < numberOfSeries; i++ ) {
var rangeFraction = i / (numberOfSeries-1);
var yInputSignal = lerp( -1, 1, rangeFraction );
var inputSignals = [1, Math.abs(yInputSignal), yInputSignal]; // d(istance), input
//var inputSignals = [Math.sin(10*yInputSignal)]; // input
oneCPPN.clearSignals();
oneCPPN.setInputSignals( inputSignals );
//oneCPPN.multipleSteps(30);
oneCPPN.recursiveActivation();
testOutputs.push( [i, oneCPPN.getOutputSignal(0), oneCPPN.getOutputSignal(1)] );
}
// console.log( oneCPPN );
// console.log( testOutputs );
new Dygraph( document.getElementById("graph"),
testOutputs,
{
labels: ["time (frequency?) domain", "modulation", "carrier" ],
valueRange: [-1, 1]
}
);
*/
function lerp( from, to, fraction ) {
return from + fraction * ( to - from );
}
function displayCurrentGeneration() {
$('h2').text( "Generation " + currentPopulationIndex );
}