I want to give genetic algorithms a chance but I can't seem to find a solution for this problem.
This is my code:
var encodings = {
0000: 0,
0001: 1,
0010: 2,
0011: 3,
0100: 4,
0101: 5,
0110: 6,
0111: 7,
1000: 8,
1001: 9,
1010: "+",
1011: "-",
1100: "*",
1101: "/"
};
var chromosome = "";
for (var i = 0; i < 36; i++) {
chromosome += Math.round(Math.random());
}
var chromArray = chromosome.match(/.{1,4}/g);
document.write(chromArray + "<br>");
for (var o = 0; o < 9; o++) {
document.write(encodings[chromArray[o]]);
}
If you run the code, you see that there are a lot of undefineds in the output. What would cause this?
Thanks!
You should convert the keys of the object into strings
It should be:
var encodings =
{
"0000": 0,
"0001": 1,
"0010": 2,
"0011": 3,
"0100": 4,
"0101": 5,
"0110": 6,
"0111": 7,
"1000": 8,
"1001": 9,
"1010": "+",
"1011": "-",
"1100": "*",
"1101": "/"
};
var chromosome = "";
for (var i = 0; i < 36; i++)
{
chromosome += Math.round(Math.random());
}
var chromArray = chromosome.match(/.{1,4}/g);
document.write(chromArray + "<br>");
for (var o = 0; o < 9; o++)
{
document.write(encodings[chromArray[o]]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With