com.debacharya.nsgaii.mutation.SinglePointMutation 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!
/*
* MIT License
*
* Copyright (c) 2019 Debabrata Acharya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.debacharya.nsgaii.mutation;
import com.debacharya.nsgaii.datastructure.AbstractAllele;
import com.debacharya.nsgaii.datastructure.BooleanAllele;
import com.debacharya.nsgaii.datastructure.Chromosome;
import java.util.ArrayList;
import java.util.List;
public class SinglePointMutation extends AbstractMutation {
private static final String BOOLEAN_ALLELE_INSTANCE_ERROR = "SinglePointMutation works with BooleanAllele only. " +
"Please implement your own Mutation class by extending " +
"the AbstractMutation class to get your desired results.";
public SinglePointMutation() {
super();
}
public SinglePointMutation(float mutationProbability) {
super(mutationProbability);
}
@Override
public Chromosome perform(Chromosome chromosome) {
for(AbstractAllele allele : chromosome.getGeneticCode())
if(!(allele instanceof BooleanAllele))
throw new UnsupportedOperationException(SinglePointMutation.BOOLEAN_ALLELE_INSTANCE_ERROR);
List booleanGeneticCode = new ArrayList<>();
for(int i = 0; i < chromosome.getLength(); i++)
booleanGeneticCode.add(
i, new BooleanAllele(
this.shouldPerformMutation() ?
!((BooleanAllele) chromosome.getAllele(i)).getGene() :
((BooleanAllele) chromosome.getAllele(i)).getGene()
)
);
return new Chromosome(new ArrayList<>(booleanGeneticCode));
}
}