com.debacharya.nsgaii.objectivefunction.ZDT1_2 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nsgaii Show documentation
Show all versions of nsgaii Show documentation
A NSGA-II implementation using Java. This implementation of NSGA-II algorithm is in pure reference to the
original published paper. This is not an effort to convert the originally implemented C code in Java.
The original C code by the authors has not be referred to while writing this implementation.
This is a fully customizable implementation of the NSGA-II algorithm, made as generic as possible.
This documentation assumes you have basic understanding of the NSGA-II algorithm. Apart from the core concepts
of the algorithm, everything else in this package can be implemented as per the user's choice and plugged
into the algorithm dynamically. Since NSGA-II is more like a set of protocols to follow as an algorithm rather
than a concrete implementation of every aspect, this package has been re-written from scratch keeping complete
customizability in mind. Apart from the core concepts of the algorithm, everything is considered to be a plugin
external to the algorithm that can be implemented by the user and dynamically plugged into the algorithm during
runtime as needed. This opens up the possibility of the package to be used simply as a PoC or be converted into
something much more complex according to the users needs.
The newest version!
package com.debacharya.nsgaii.objectivefunction;
import com.debacharya.nsgaii.datastructure.Chromosome;
import com.debacharya.nsgaii.datastructure.ValueAllele;
import java.util.List;
import java.util.stream.Collectors;
public class ZDT1_2 extends AbstractObjectiveFunction {
private static final String VALUE_ALLELE_INSTANCE_ERROR = "ZDT1 works with ValueAllele only. Please implement your own objective " +
"function class by extending the AbstractObjectiveFunction class to get " +
"your desired results or use genetic code of type ValueAllele to use this " +
"default implementation";
public ZDT1_2() {
this.objectiveFunctionTitle = "g(x) [1 - sqrt(x1 / g(x)]";
}
@Override
public double getValue(Chromosome chromosome) {
if(!(chromosome.getAllele(0) instanceof ValueAllele))
throw new UnsupportedOperationException(ZDT1_2.VALUE_ALLELE_INSTANCE_ERROR);
List geneticCode = chromosome.getGeneticCode().stream().map(e -> (ValueAllele) e).collect(Collectors.toList());
double size = geneticCode.size();
double sum = 0;
for(int i = 1; i < size; i++)
sum += geneticCode.get(i).getGene();
double g = 1 + ((9 * sum) / size);
double f = g * (1 - Math.sqrt(geneticCode.get(0).getGene() / g));
return f;
}
}