Forums > Javascript

Problem with multi-dimensional array

December 26, 2008 | 8:17 pm

Hello,

I am working on a GA algorithm but I have a problem with my array
contening the whole genome.

My array (genome) is first declared among other variables:

var population = 20;
var numGenes = 183;
var genome=new Array(population);
for (var i=0; i

genome[i]=new Array(183)
} ;

Then the array is filled with random values:

function init() {
for (var a = 0; a < population; a++) {
genome[a] = [];
for (b = 0 ; b < numGenes; b++) {
genome[a][b] = Math.random();
}
};
};

There is additional code in between but which is not linked to this
array.

PROBLEM : afterwards (after the selection step) I want to copy parts
of the array into another one. But even before doing it, I try to
display the genome array in the Max window using code below but it
crashes Max:

selectedGenome = [];
for (var a = 0; a < population; a++) {
post("genome[a] : "+genome[a]+"n");
};

However this code is accepted:

post("genome[a][0] : "+genome[a][0]+"n")

It seems I cannot handle neither a complete dimension of my array nor
the complete array any more. Indeed this instruction crashes Max too:

post("Genome : "+genome+"n")

What should I modify to make the code function properly?

Thank you in advance.

Roald Baudoux


December 26, 2008 | 10:17 pm

hello,

> On 26 Dec 2008, at 21:17, Roald Baudoux wrote:
> I try to display the genome array in the Max window using code
> below but it crashes Max:
>
> selectedGenome = [];
> for (var a = 0; a < population; a++) {
> post("genome[a] : "+genome[a]+"n");
> };
>>
>
> What should I modify to make the code function properly?

you could try something like this (hope it comes through alright):

var population = 20;
var numGenes = 18;
var genome=new Array(population);

function init() {
for (var a = 0; a < population; a++) {
genome[a] = new Array(numGenes);
for (b = 0; b < numGenes; b++) {
genome[a][b] = Math.random();
}
}
}

function printme() {
for (var a = 0; a < population; a++) {
post("genome["+a+"]: ", genome[a], "n");
}
}

///////

watch out your use of semicola. there is no ";" after a for loop.
after all that’s not supercollider, is it? ;)
and what’s the "genome[a] = [];" supposed to do?

hth,
volker.


December 26, 2008 | 11:18 pm


December 27, 2008 | 10:57 am

>>
> Hello Volker,
>
> yihbirnq! (Yes It Helps But It Raises New Questions!)
>
> Printing using your code works properly but my question is rather
> about array handling than printing. I wanted to print because
> trying to copy between arrays using genome[a] led to a crash too.
> Isn’t JS supposed to be able to handle genome[a] in one go, both
> for printing and copying?

there is no direct way to make independent copies of arrays in js.
if you assign one array to another, you just pass the reference, not
the actual data.
using the .slice(0) method is a way to fake/force an independent copy.

> However I do not understand why your code works and mine crashes
> Max. Could it be because of the array size? Max wouldn’t be able to
> print 183 floats in one go?

no, i don’t think it’s the size.
AFAIK, the difference in the post() method between using "+" or ","
is that the plus is trying to concatenate the items into a string,
while the comma is treating the elements indiviually and forms a list
or message (from the max point of view).
your code snippet doesn’t crash here, so i can’t comment on that.
but the error is probably happening somewhere else.

>
> Having looked at your code, I have tried the following:
>
> function printmetoo() {
> post("genome : ", genome, "n");
> }
>
> It doesn’t crash Max but instead of my genome, what I see in the
> Max window is:
> js: genome : jsobject 41046904 jsobject 41046912 jsobject
> 41046920 jsobject 41046928 jsobject 41046936 jsobject
> 41046944 jsobject 41046952 jsobject 41046960 jsobject
> 41046968 jsobject 41046976 jsobject 41046984 jsobject
> 41046992 jsobject 41047000 jsobject 41047008 jsobject
> 41047016 jsobject 41047024 jsobject 41047032 jsobject
> 41047048 jsobject 41047056 jsobject 41047064
>
> I am puzzled.

Arrays are unrolled to one level in max. so direct printing of a
multidim array won’t work – or will not work the way you want it.
what you see is the first level of the multidim array, where every
index contains again an array.
an array is an object. so you get "jsobject somenumber".

>
>> ///////
>>
>> watch out your use of semicola. there is no ";" after a for loop.
>
> I understand a semicolon is not mandatory there but can it cause
> any problem?

no it shouldn’t. js is quite forgiving about ‘misuse’ of semcolons
anyway.
but i think, mostly ‘sticking to the rules’ is a good idea when
writing code, as it leads to better readabilty in the end.
>
>> and what’s the "genome[a] = [];" supposed to do?
>
> It’s a way to clean the array, ensuring no values from a previous
> run would remain. Do you thing I should rather clean the array
> explicitely?

ok, should work in this context.

cheers,
volker.


December 28, 2008 | 7:24 pm


Viewing 5 posts - 1 through 5 (of 5 total)