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

com.debacharya.nsgaii.mutation.SwapMutation 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.Reporter;
import com.debacharya.nsgaii.Service;
import com.debacharya.nsgaii.datastructure.AbstractAllele;
import com.debacharya.nsgaii.datastructure.Chromosome;

import java.util.ArrayList;
import java.util.List;

public class SwapMutation extends AbstractMutation {

	private static final String TOO_MANY_SWAP_POSITIONS = "The number of swaps provided is more than the total length of the chromosome." +
															" Swap Mutation is hence not possible here.";

	final int swaps;

	public SwapMutation() {
		this(1);
	}

	public SwapMutation(float mutationProbability) {
		this(mutationProbability, 1);
	}

	public SwapMutation(int swaps) {
		super();
		this.swaps = swaps;
	}

	public SwapMutation(float mutationProbability, int swaps) {
		super(mutationProbability);
		this.swaps = swaps;
	}

	@Override
	public Chromosome perform(Chromosome chromosome) {

		int totalSwaps = this.swaps * 2;

		if(chromosome.getLength() < totalSwaps)
			throw new UnsupportedOperationException(SwapMutation.TOO_MANY_SWAP_POSITIONS);

		List randomPositions = Service.generateUniqueRandomNumbers(totalSwaps, chromosome.getLength());
		int halfSize = randomPositions.size() / 2;

		for(int i = 0; i < halfSize; i++) {

			int j = i + halfSize;
			int p1 = randomPositions.get(i);
			int p2 = randomPositions.get(j);

			AbstractAllele temp = chromosome.getAllele(p1).getCopy();

			chromosome.setAllele(p1, chromosome.getAllele(p2));
			chromosome.setAllele(p2, temp);
		}

		return new Chromosome(new ArrayList<>(chromosome.getGeneticCode()));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy