0

The code is :

package classes;

public class Test {
    private static double mutationRate = 0.5;

    public static void main(String[] args) {
        Population pop = new Population();
        pop.initialise();
        Population po = new Population();
        po.getIndividusList().add(pop.getFittest());
        po.getIndividusList().add(mutate(pop.getIndividusList().get(1)));
    }

    private static Chromosom mutate(Chromosom l) { // changer les couples d'interventions des parcs)
        // loop through genes
        Chromosom ch = new Chromosom();
        for (int i = 0; i < l.size(); i++)
            ch.put(i, l.get(i));

        for (int i = 0; i < ch.size(); i++) {
            double alea = Math.random() * 13;
            int moisIntervention1 = (int) alea;

            Intervention interv1 = new Intervention(1, moisIntervention1);
            ch.get(i).modInterventions(ch.get(i).intervention2(interv1));
        }
        return ch;
    }
}

The problem is that I did not change the instance pop but when I change the other instance po, pop changes too.

7
  • 1
    As you populate po with the contents of pop, when you change po, pop still points at the same changed objects. Commented Aug 11, 2014 at 12:07
  • You need to actually copy the contents of pop into po... not point like the above comment Commented Aug 11, 2014 at 12:07
  • Excellent variable naming, though. Commented Aug 11, 2014 at 12:09
  • What does class Population look like? Are its member variables static (which means they are shared between all instances of Population)? Commented Aug 11, 2014 at 12:09
  • The problem is likely in your implementation of the class Population Commented Aug 11, 2014 at 12:09

2 Answers 2

1

java pass by value.

when you call this mutate(pop.getIndividusList().get(1))

you are sending pop's instance, so it will get change.

Supose pop.getIndividusList().get(1) return String varibale do this way

String var=pop.getIndividusList().get(1);

then call mutate(var)

Sign up to request clarification or add additional context in comments.

4 Comments

so what can i do ? there is any solution to not sending pop's instance ? thanks
what pop.getIndividusList().get(1) return? store it in some variable and then mutate(variableHavingValue)
what pop.getIndividusList().get(1) return an instance of another class. i do what you told me to do and i still have the same probleme. i did: Chromosom var= what pop.getIndividusList().get(1) then i call mutate(var)
pop.getIndividusList().get(1) implement hash table
0

I'm unsure about whether I understood the problem, but I think that you mean that when you alter the items in Population po, the items in Population pop mirror those changes.

That is, indeed, the expected behavior of your code: to populate po, you are adding items from pop - (pop.getFittest, pop.getList.get(1) ).

But the individuals are, I believe, instances of objects, so add/remove and similar operations work with references to the objects, and not with copies of them. Therefore, as you have 2 references to the same obj, any change is mirrored.

IF you want to create a copy, you should add to po a new object with the same state, either by creating a constructor that takes another instance as parameter, implementing a copy method, or something similar.

It should be something like this:

    Population po = new Population();
    Individual fittest = pop.getFittest();
    Individual poCopy = new Individual();
    //ADD CODE HERE TO COPY ALL THE FIELDS FROM fittest TO poCopy
    //....

    po.getIndividusList().add(poCopy);

1 Comment

thank you for your answer i try this methode but i still have the same problem

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.