Я написал функцию для генерации нового набора списков логических значений из заданного логического списка.Когда я выхожу из кода, он меняет значение моего исходного списка.
Я пытался распечатать свой оригинальный список после каждой операции.Это изменяет исходное значение после следующего кода в функции, называемой synthesizeChild.
for (Map.Entry < Double, List < Integer >> entry: finalID.entrySet()) {
if (decode.equals(entry.getKey())) {
chromosome.setId(entry.getValue().get(LOCAL_RANDOM.nextInt(entry.getValue().size())));
}
}
Это функции, связанные с функцией synthesizeChild.
public static Population synthesizeChild(final Population parentArg) {
List < Chromosome > tempChr = new ArrayList < >();
for (int i = 0; i < parentArg.getPopulace().size(); i++) {
tempChr.add(parentArg.getPopulace().get(i));
}
// tempChr.addAll(parentArg.getPopulace());
Population tempparent = new Population(tempChr);
Population child = new Population();
List < Chromosome > populace = new ArrayList < >();
Map < Double,
List < Integer >> finalID = maplookupID();
/**
* child chromosomes undergo crossover and mutation.
* the child chromosomes are selected using binary tournament selection.
* crossover returns an array of exactly two child chromosomes synthesized from two parent
* chromosomes.
*/
while (populace.size() < Configuration.getPopulationSize())
for (Chromosome chromosome: crossover(binaryTournamentSelection(new Population(tempparent.getPopulace())), binaryTournamentSelection(new Population(tempparent.getPopulace())))) {
chromosome = (mutation(chromosome));
if (!penalityFunction(chromosome)) {
continue;
}
Double decode = decodeGeneticCode(chromosome.getGeneticCode());
for (Map.Entry < Double, List < Integer >> entry: finalID.entrySet()) {
if (decode.equals(entry.getKey())) chromosome.setId(entry.getValue().get(LOCAL_RANDOM.nextInt(entry.getValue().size())));
}
if (!isDuplicate(tempparent, chromosome)) {
populace.add(chromosome);
}
}
child.setPopulace(populace);
return child;
}
public static boolean isDuplicate(final Population parentList, final Chromosome chromosome) {
for (int j = 0; j < parentList.getPopulace().size(); j++) {
if (parentList.getPopulace().get(j).getId() == (chromosome.getId())) {
return true;
}
}
return false;
}
public static Map < Double,
List < Integer >> maplookupID() {
Map < Integer,
Double > maplookupid = lookupfoID();
Map < Double,
List < Integer >> finalID = new HashMap < >();
Set < Double > decimalValues = new HashSet < >();
for (Map.Entry < Integer, Double > entry: maplookupid.entrySet()) {
decimalValues.add(entry.getValue());
}
for (Double decimal: decimalValues) {
{
List < Integer > listofID = new ArrayList < >();
for (Map.Entry < Integer, Double > entry: maplookupid.entrySet()) {
if (decimal.equals(entry.getValue())) {
listofID.add(entry.getKey());
}
}
finalID.put(decimal, listofID);
}
}
return finalID;
}
public static double decodeGeneticCode(final Allele[] geneticCode) {
double value = 0;
String binaryString = "";
for (Allele bit: geneticCode) binaryString += bit.getGene() ? "1": "0";
for (int i = 0; i < binaryString.length(); i++) if (binaryString.charAt(i) == '1') value += Math.pow(2, binaryString.length() - 1 - i);
return value;
}
Исходное значение parentArg не должно изменяться.Но это меняется.Кто-нибудь может решить эту проблему?