-1

Been working on this for a couple of hours so thought I'd ask, When I create an array of this object the memory array which is given a random input causes the rest of the array to have the same output: see code below.

Bee Class:

private class Bee{
    private int status;
    private float[] memory;
    private double quality;
    private int visits;

    private Bee(int status, float[] memory, double quality, int visits){
        this.status = status;
        this.memory = memory;
        this.quality = quality;
        this.visits = visits;
    }
}

The individual bees quality when printed are different yet their memory stay all the same.

for(int i = 0; i < this.bees.length; i++){
    System.out.println(this.bees[i].memory[0]+" "+this.bees[i].memory[1]);
    System.out.println(this.bees[i].quality);
}

Sample Output:

3.4968524 8.354554
1581.5435920638447
3.4968524 8.354554
82.46318172154176
3.4968524 8.354554
66.25267691464408

I don't understand why the quality is working but not the memory?

Any help or suggestions would be greatly appreciated.

EDIT:

for(int i = 0; i < totalNumberbees; i++){
    int beestatus;

    if(i < numberInactive){
         beestatus = 0;//inactive
         indexesOfInactivebees[i] = i;

    } else if(i < (numberInactive + numberScout)){
         beestatus = 2;//scout

    } else{
         beestatus = 1;//active
    }

    float[] randomMemory = GenerateRandomMemory();
    this.bees[i] = new Bee(beestatus, randomMemory, MeasureQuality(randomMemory), 0);
}

private float[] GenerateRandomMemory(){
    float[] result = new float[this.functionData.vars.length];
    result = this.functionData.vars;

    for(int i = 0; i < result.length; i++){
        float r1 = new Random().nextFloat()*10;
        result[i] = r1;
    }
    return result;
}

Here is the GenerateRandomClass and the rest of the code the initialises the bees.

2
  • 4
    Please post GenerateRandomMemory and MeasureQuality. Commented May 6, 2012 at 20:43
  • do you use the same array float[] randomMemory for all the Bee objects? Commented May 6, 2012 at 20:45

2 Answers 2

0

GenerateRandomMemory() outputs the same instance of float array. Check it or post its source code.

0

It seems randomMemory array is initialized only once. either you are calling GenerateRandomMemory() before the loop or it returns the same reference every time it's called.

2
  • I don't think it is the case, since quality is between different objects differs. In order for that to be correct, MeasureQuality() should provide non-deterministic, or at least varying result each run. Commented May 6, 2012 at 20:52
  • We cannot clarify this unless he provides us the whole relevant code instead of this shortened version. Commented May 6, 2012 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.