![JAR search and dependency download from the Maven repository](/logo.png)
net.sourceforge.cilib.entity.topologies.SpeciationTopology Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cilib-library Show documentation
Show all versions of cilib-library Show documentation
A library of composable components enabling simpler Computational Intelligence
/** __ __
* _____ _/ /_/ /_ Computational Intelligence Library (CIlib)
* / ___/ / / / __ \ (c) CIRG @ UP
* / /__/ / / / /_/ / http://cilib.net
* \___/_/_/_/_.___/
*/
package net.sourceforge.cilib.entity.topologies;
import com.google.common.collect.UnmodifiableIterator;
import fj.*;
import fj.data.List;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.util.distancemeasure.DistanceMeasure;
import net.sourceforge.cilib.util.distancemeasure.EuclideanDistanceMeasure;
/**
*
* @INPROCEEDINGS{Li04adaptivelychoosing,
* author = {Xiaodong Li},
* title = {Adaptively Choosing Neighbourhood Bests Using Species in a Particle Swarm Optimizer for Multimodal Function Optimization},
* booktitle = {in GECCO-2004},
* year = {2004},
* pages = {105--116},
* publisher = {Springer-Verlag}
* }
*
*/
public class SpeciationTopology extends AbstractTopology {
private DistanceMeasure distanceMeasure;
private ControlParameter radius;
public SpeciationTopology() {
this.distanceMeasure = new EuclideanDistanceMeasure();
this.radius = ConstantControlParameter.of(100);
this.neighbourhoodSize = ConstantControlParameter.of(20);
}
public SpeciationTopology(SpeciationTopology copy) {
super(copy);
this.distanceMeasure = copy.distanceMeasure;
this.radius = copy.radius.getClone();
this.neighbourhoodSize = copy.neighbourhoodSize.getClone();
}
@Override
public SpeciationTopology getClone() {
return new SpeciationTopology(this);
}
public void setRadius(ControlParameter radius) {
this.radius = radius;
}
public void setDistanceMeasure(DistanceMeasure distanceMeasure) {
this.distanceMeasure = distanceMeasure;
}
public DistanceMeasure getDistanceMeasure() {
return distanceMeasure;
}
public ControlParameter getRadius() {
return radius;
}
public static F, Boolean> inRadius(final DistanceMeasure distance, final ControlParameter radius, final T other) {
return new F, Boolean>() {
@Override
public Boolean f(P2 a) {
if (distance.distance(a._1().getCandidateSolution(), other.getCandidateSolution()) < radius.getParameter()) {
return true;
}
return false;
}
};
}
public static F, Boolean> exists(final int index) {
return new F, Boolean>() {
@Override
public Boolean f(P2 a) {
if (a._2() == index) {
return true;
}
return false;
}
};
}
public static F>, List> getNeighbourhood(final DistanceMeasure distance,
final ControlParameter radius, final ControlParameter size, final int index) {
return new F>, List>() {
@Override
public List f(List> a) {
if (a.isEmpty()) {
return List.nil();
}
List> sorted = a.sort(Ord.>ord(new F2, P2, Ordering>() {
@Override
public Ordering f(P2 a, P2 b) {
int result = -a._1().getFitness().compareTo(b._1().getFitness()) + 1;
return Ordering.values()[result];
}
}.curry()));
List> filtered = sorted.filter(inRadius(distance, radius, sorted.head()._1()));
List> neighbours = filtered.take((int) size.getParameter());
if(neighbours.exists(SpeciationTopology.exists(index))) {
return neighbours.map(P2.__2());
}
List> remainder = sorted.minus(Equal.>equal(
new F2, P2, Boolean>() {
@Override
public Boolean f(P2 a, P2 b) {
return a._2() == b._2();
}
}.curry()), neighbours);
return this.f(remainder);
}
};
}
@Override
protected Iterator neighbourhoodOf(final E e) {
return new UnmodifiableIterator() {
private List neighbours = getNeighbourhood(distanceMeasure, radius, neighbourhoodSize, entities.indexOf(e))
.f(List.iterableList((java.util.List) entities).zipIndex());
private int index = -1;
@Override
public boolean hasNext() {
return neighbours.isNotEmpty();
}
@Override
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
index = neighbours.head();
neighbours = neighbours.orTail(P.p(List.nil()));
return entities.get(index);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy