/
pickParent.js
47 lines (43 loc) · 1.31 KB
/
pickParent.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
var nAttributes = 15;
module.exports = pickParent;
function pickParent(currentChoices, chooseId, key /* , parents */){
if(!currentChoices.has(chooseId)){
currentChoices.set(chooseId, initializePick())
}
// console.log(chooseId);
var state = currentChoices.get(chooseId);
// console.log(state.curparent);
state.i++
if(["wheel_radius", "wheel_vertex", "wheel_density"].indexOf(key) > -1){
state.curparent = cw_chooseParent(state);
return state.curparent;
}
state.curparent = cw_chooseParent(state);
return state.curparent;
function cw_chooseParent(state) {
var curparent = state.curparent;
var attributeIndex = state.i;
var swapPoint1 = state.swapPoint1
var swapPoint2 = state.swapPoint2
// console.log(swapPoint1, swapPoint2, attributeIndex)
if ((swapPoint1 == attributeIndex) || (swapPoint2 == attributeIndex)) {
return curparent == 1 ? 0 : 1
}
return curparent
}
function initializePick(){
var curparent = 0;
var swapPoint1 = Math.floor(Math.random() * (nAttributes));
var swapPoint2 = swapPoint1;
while (swapPoint2 == swapPoint1) {
swapPoint2 = Math.floor(Math.random() * (nAttributes));
}
var i = 0;
return {
curparent: curparent,
i: i,
swapPoint1: swapPoint1,
swapPoint2: swapPoint2
}
}
}