Skip to content

Commit

Permalink
Ultimos cambios
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiamberardino committed Feb 15, 2017
1 parent 6885935 commit 62c2768
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 51 deletions.
14 changes: 0 additions & 14 deletions src/main/java/com/mgiamberardino/jnetic/operators/Condition.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@FunctionalInterface
public interface Selector<T,U extends Comparable<U>> {

public List<T> select(Population<T> population, Function<T, U> aptitudeFunction);
public List<T> select(Population<T> population, Function<T, U> aptitudeFunction, Double elitePortion);

}
20 changes: 10 additions & 10 deletions src/main/java/com/mgiamberardino/jnetic/population/Evolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.mgiamberardino.jnetic.operators.Condition;
import com.mgiamberardino.jnetic.operators.Selector;
import com.mgiamberardino.jnetic.population.Population.Parents;
import com.mgiamberardino.jnetic.util.Selectors;

public class Evolution<T, U extends Comparable<U>> {

Expand All @@ -26,18 +23,21 @@ public class Evolution<T, U extends Comparable<U>> {
private Function<T, U> aptitudeFunction;
private Function<Map<T,U>, Supplier<Parents<T>>> parentSupplierBuilder = Evolution::defaultSupplier;
private Predicate<T> validator = o -> true;
private Double elitePortion;

public static <T, U extends Comparable<U>> Evolution<T, U> of(Population<T> population, Function<T, U> aptitudeFunction) {
return new Evolution<T, U>(population, aptitudeFunction);
public static <T, U extends Comparable<U>> Evolution<T, U> of(Population<T> population, Function<T, U> aptitudeFunction, double elitePortion) {
return new Evolution<T, U>(population, aptitudeFunction,elitePortion);
}

Evolution(Population<T> population, Function<T, U> aptitudeFunction){
Evolution(Population<T> population, Function<T, U> aptitudeFunction, Double elitePortion){
this.population = population;
this.aptitudeFunction = aptitudeFunction;
this.elitePortion = elitePortion;
this.selector = Selectors.binaryTournament(this ,0.25, 0.75);
}

public Evolution<T,U> evolve() {
List<T> parents = selector.select(population, aptitudeFunction);
List<T> parents = selector.select(population, aptitudeFunction, elitePortion);
Map<T,U> aptitudes = parents.stream()
.collect(Collectors.toMap(Function.identity(), aptitudeFunction, (s1, s2) -> s1));
List<T> sons = Stream.generate(parentSupplierBuilder.apply(aptitudes))
Expand All @@ -52,9 +52,9 @@ public Evolution<T,U> evolve() {
return this;
}

public Evolution<T, U> evolveUntil(Condition<T> condition){
public Evolution<T, U> evolveUntil(Predicate<Population<T>> condition){
Integer i = 0;
while(! condition.apply(population)){
while(! condition.test(population)){
evolve();
System.out.println("Generacion " + i + ":");
System.out.println(population.stream().collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public Stream<T> stream() {
return members.stream();
}

public <U extends Comparable<U>> Evolution<T, U> evolution(Function<T, U> aptitudeFunction) {
return new Evolution<T, U>(this, aptitudeFunction);
public <U extends Comparable<U>> Evolution<T, U> evolution(Function<T, U> aptitudeFunction, double elitePortion) {
return new Evolution<T, U>(this, aptitudeFunction, elitePortion);
}

private List<T> members() {
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/mgiamberardino/jnetic/util/Conditions.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.mgiamberardino.jnetic.util;

import java.util.function.Function;
import java.util.function.Predicate;

import com.mgiamberardino.jnetic.operators.Condition;
import com.mgiamberardino.jnetic.population.Population;

public class Conditions {

public static <T> Condition<T> after(int iterations){
public static <T> Predicate<Population<T>> after(int iterations){
return (population) -> population.getGeneration() == iterations;
}

public static <T> Condition<T> converge(Double difference, Function<Population<T>, Double> avgFunction){
return new Condition<T>() {
public static <T> Predicate<Population<T>> converge(Double difference, Function<Population<T>, Double> avgFunction){
return new Predicate<Population<T>>() {

private Double lastAvg = 0.0;
private int times = 0;
@Override
public Boolean apply(Population<T> population) {
public boolean test(Population<T> population) {
Double avg = avgFunction.apply(population);
Double diff = Math.abs(lastAvg - avg);
System.out.println("Last: " + lastAvg + " New: " + avg + "Dif: " + diff);
Expand All @@ -33,14 +33,10 @@ public Boolean apply(Population<T> population) {
};
}

public static <T> Condition<T> contains(T value){
return new Condition<T>() {

@Override
public Boolean apply(Population<T> population) {
public static <T> Predicate<Population<T>> contains(T value){
return (Population<T> population) -> {
return population.stream().anyMatch(t -> t.equals(value));
}
};
};
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/mgiamberardino/jnetic/util/Crossers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mgiamberardino.jnetic.util;

public class Crossers {



}
71 changes: 62 additions & 9 deletions src/main/java/com/mgiamberardino/jnetic/util/Selectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,84 @@
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import com.mgiamberardino.jnetic.operators.Selector;
import com.mgiamberardino.jnetic.population.Evolution;
import com.mgiamberardino.jnetic.population.Population;

public class Selectors {

public static <T,U extends Comparable<U>> Selector<T, U> truncatedSelection(Double portion){
return (population, aptitudeFunction) -> {
return (population, aptitudeFunction, elitePortion) -> {
List<T> result = population.stream().collect(Collectors.toList());
Collections.sort(result , Collections.reverseOrder(Population.comparator(aptitudeFunction)));
return result.stream().limit((long) (population.size() * portion)).collect(Collectors.toList());
Double calculatedPortion = portion > elitePortion
? portion
: elitePortion;
return result.stream().limit((long) (population.size() * calculatedPortion)).collect(Collectors.toList());
};
}

public static <T> Selector<T, Double> rouletteStochasticAcceptance(Double portion){
return (population, aptitudeFunction) -> {
List<T> result = population.stream().collect(Collectors.toList());
Double max = result.stream().map(aptitudeFunction).sorted().reduce((act, last) -> last).orElse(null);
Collections.shuffle(result);
return result.stream()
return (population, aptitudeFunction, elitePortion) -> {
long toSelect = Math.round(population.size() * portion);
List<T> result = getElite(population, elitePortion, toSelect, aptitudeFunction);
if (result.size() >= toSelect){
return result;
}
List<T> individuals = population.stream().collect(Collectors.toList());
Double max = population.stream().map(aptitudeFunction).sorted().reduce((act, last) -> last).orElse(null);
Collections.shuffle(individuals);
population.stream()
.filter(t -> new Random(System.currentTimeMillis()).nextDouble() < (aptitudeFunction.apply(t)/max))
.limit((long) (population.size() * portion))
.collect(Collectors.toList());
.limit(toSelect - result.size())
.forEach(result::add);
return result;
};
}

public static <T> Selector<T, Double> deterministicBinaryTournament(Double portion){
return binaryTournament(portion, 1.0);
}

public static <T, U extends Comparable<U>> Selector<T, U> binaryTournament(Evolution<T,U> e, Double portion, Double probability){
return binaryTournament(portion, probability);
}

public static <T, U extends Comparable<U>> Selector<T,U> binaryTournament(Double portion, Double probability){
return (population, aptitudeFunction, elitePortion) -> {
long toSelect = Math.round(population.size() * portion);
List<T> individuals = population.stream().collect(Collectors.toList());
List<T> result = getElite(population, elitePortion, toSelect, aptitudeFunction);
if (result.size() >= toSelect){
return result;
}
Stream.generate(() -> selectForTournament(individuals, 2, aptitudeFunction, probability))
.limit(toSelect - result.size())
.forEach(result::add);
return result;
};
}

private static <T, U extends Comparable<U>> T selectForTournament(List<T> population, int size, Function<T, U> aptitudeFunction, double probability){
Random r = new Random(System.currentTimeMillis());
return IntStream.generate(() -> r.nextInt(population.size()))
.limit(size)
.mapToObj(population::get)
.sorted(Population.comparator(aptitudeFunction))
.reduce((i1, i2) -> r.nextDouble() <= probability ? i2 : i1)
.get();
}

private static <T, U extends Comparable<U>> List<T> getElite(Population<T> population, double elitePortion, long toSelect, Function<T, U> aptitudeFunction) {
long eliteToSelect = Math.round(population.size() * elitePortion);
long eliteRealTotal = eliteToSelect < toSelect ? eliteToSelect : toSelect;
List<T> sorted = population.stream().sorted(Collections.reverseOrder(Population.comparator(aptitudeFunction))).collect(Collectors.toList());
return sorted.stream().limit(eliteRealTotal).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mgiamberardino.jnetic.population;

import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.apache.commons.lang3.RandomStringUtils;
Expand All @@ -14,13 +16,16 @@
public class ToBeOrNotToBe {

private static final String SEARCHED_STRING = "To be or not to be";
private static interface A {

}

public static void main(String[] args) {
System.out.println(
Evolution.of(
Population.generate(ToBeOrNotToBe::generatePhrase, 10),
ToBeOrNotToBe::aptitudeMeter)
.selector(Selectors.truncatedSelection(0.50))
Population.generate(ToBeOrNotToBe::generatePhrase, 200),
ToBeOrNotToBe::aptitudeMeter,
0.005)
.crosser(ToBeOrNotToBe::crosser)
.mutator(ToBeOrNotToBe::mutator)
.evolveUntil(
Expand Down

0 comments on commit 62c2768

Please sign in to comment.