All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.debacharya.nsgaii.mutation.PolynomialMutation Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 3.2.0
Show newest version
package com.debacharya.nsgaii.mutation;

import com.debacharya.nsgaii.Service;
import com.debacharya.nsgaii.datastructure.Chromosome;
import com.debacharya.nsgaii.datastructure.ValueAllele;
import com.debacharya.nsgaii.mutation.AbstractMutation;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class PolynomialMutation extends AbstractMutation {

	private final double origin;
	private final double bound;
	private final double distributionIndex;

	public PolynomialMutation(double origin, double bound, double distributionIndex, float mutationProbability) {
		super(mutationProbability);
		this.origin = origin;
		this.bound = bound;
		this.distributionIndex = distributionIndex;
	}

	public PolynomialMutation(double origin, double bound, double distributionIndex) {
		super();
		this.origin = origin;
		this.bound = bound;
		this.distributionIndex = distributionIndex;
	}

	public PolynomialMutation(double origin, double bound) {
		this(origin, bound, 20);
	}

	public PolynomialMutation(double distributionIndex) {
		this(0, 1, distributionIndex);
	}

	public PolynomialMutation(float mutationProbability) {
		this(0, 1, 20, mutationProbability);
	}

	@Override
	public Chromosome perform(Chromosome chromosome) {

		List childGeneticCode = new ArrayList<>();
		List parentGeneticCode = chromosome.getGeneticCode().stream().map(e -> (ValueAllele) e).collect(Collectors.toList());

		for(int i = 0; i < parentGeneticCode.size(); i++)
			childGeneticCode.add(i, new ValueAllele(
				this.shouldPerformMutation() ?
					this.getMutatedValue(parentGeneticCode.get(i).getGene()) :
					parentGeneticCode.get(i).getGene()
			));

		return new Chromosome(new ArrayList<>(childGeneticCode));
	}

	private double getMutatedValue(double originalValue) {

		double probability = ThreadLocalRandom.current().nextDouble();
		double delta = Service.roundOff(this.generateDelta(probability), 4);
		double result;

		if(probability < 0.5)
			result = originalValue + (delta * (originalValue - this.origin));
		else
			result = originalValue + (delta * (this.bound - originalValue));

		if(result < this.origin)
			result = this.origin;
		else if(result > this.bound)
			result = this.bound;

		return Service.roundOff(result, 4);
	}

	private double generateDelta(double probability) {
		return (probability < 0.5)
			? (Math.pow(
				2 * probability,
				1 / (this.distributionIndex + 1)
			) - 1)
			: (1 - Math.pow(
				2 * (1 - probability),
				1 / (this.distributionIndex + 1)
			));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy