Skip to content

Commit

Permalink
Implement Population with truncate api
Browse files Browse the repository at this point in the history
  • Loading branch information
chen0040 committed Jun 15, 2017
1 parent 5115b19 commit d882f4b
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 3 deletions.
60 changes: 60 additions & 0 deletions src/main/java/com/github/chen0040/moea/components/Population.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.chen0040.moea.components;


import com.github.chen0040.moea.utils.QuickSort;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
* Created by xschen on 15/6/2017.
*/
@Getter
@Setter
public class Population implements Serializable {

private static final long serialVersionUID = -7818958192871058630L;

@Setter(AccessLevel.NONE)
protected final List<Solution> solutions = new ArrayList<>();
protected Mediator mediator = new Mediator();
protected int generation = 0;
protected int populationSize = 1000;

public void initialize(){

solutions.clear();
for(int i=0; i < populationSize; ++i){
Solution s = new Solution();
s.initialize(mediator);
solutions.add(s);
}
}

public void add(Population population){
solutions.addAll(population.solutions);
}

public void truncate(int size, Comparator<Solution> comparator) {

int current_size = solutions.size();
if(current_size > size) {
QuickSort.sort(solutions, comparator);
for(int i= current_size - 1; i >= size; --i){
solutions.remove(i);
}
}

}


public int size() {
return solutions.size();
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/github/chen0040/moea/utils/ArrayListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ public static List<Double> zeros(int size) {
}


public static List<Double> asArray(double value, int size) {
public static List<Double> asList(double value, int size) {
List<Double> result = new ArrayList<>();
for(int i=0; i < size; ++i) {
result.add(value);
}
return result;
}


public static List<Integer> range(int upper) {
List<Integer> result = new ArrayList<>();
for(int i=0; i < upper; ++i) {
result.add(i);
}
return result;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/github/chen0040/moea/utils/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.chen0040.moea.utils;


import java.util.Comparator;
import java.util.List;


/**
* Created by xschen on 15/6/2017.
*/
public class InsertionSort {
public static <S> void sort(List<S> a, int lo, int hi, Comparator<S> comparator) {
for(int i=lo; i <= hi; ++i){
for(int j=i; j > lo; --j){
if(SortUtils.less(a.get(j), a.get(j-1), comparator)){
SortUtils.exchange(a, j, j-1);
} else {
break;
}
}
}
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/github/chen0040/moea/utils/KnuthShuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.chen0040.moea.utils;


import com.github.chen0040.moea.components.RandomGenerator;

import java.util.List;


/**
* Created by xschen on 15/6/2017.
*/
public class KnuthShuffle {
public static <S> void shuffle(List<S> a, RandomGenerator randomGenerator) {
if(a.size() < 2) return;
for(int j=1; j < a.size(); ++j) {
int i = randomGenerator.nextInt(j+1);
SortUtils.exchange(a, i, j);
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/github/chen0040/moea/utils/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.chen0040.moea.utils;


import java.util.Comparator;
import java.util.List;


/**
* Created by xschen on 15/6/2017.
*/
public class QuickSort {
public static <S> void sort(List<S> a, Comparator<S> comparator) {
sort(a, 0, a.size()-1, comparator);
}

private static <S> void sort(List<S> a, int lo, int hi, Comparator<S> comparator) {
if(lo >= hi) return;

if(hi - lo < 7) {
InsertionSort.sort(a, lo, hi, comparator);
return;
}

int j = partition(a, lo, hi, comparator);
sort(a, lo, j-1, comparator);
sort(a, j+1, hi, comparator);
}

private static <S> int partition(List<S> a, int lo, int hi, Comparator<S> comparator) {
int i=lo, j = hi+1;
S v = a.get(lo);
while(true) {
while(SortUtils.less(a.get(++i), v, comparator)){
if(i >= hi) break;
}
while(SortUtils.less(v, a.get(--j), comparator)) {
if( j <= lo) break;
}

if(i >= j) break;

SortUtils.exchange(a, i, j);
}
SortUtils.exchange(a, lo, j);
return j;
}


}
44 changes: 44 additions & 0 deletions src/main/java/com/github/chen0040/moea/utils/SortUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.chen0040.moea.utils;


import java.util.Comparator;
import java.util.List;


/**
* Created by xschen on 15/6/2017.
*/
public class SortUtils {

public static <S> boolean less(S a1, S a2, Comparator<S> comparator) {
return comparator.compare(a1, a2) < 0;
}

public static <S> void exchange(List<S> a, int i, int j){
S temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}

public static <S> boolean isSorted(List<S> a, Comparator<S> comparator) {
for(int i=1; i < a.size(); ++i) {
int j = i-1;
if(comparator.compare(a.get(j), a.get(i)) > 0) {
return false;
}
}
return true;
}


public static void print(List<Integer> a) {
System.out.print("[");
for(int i=0; i < a.size(); ++i) {
if(i != 0) {
System.out.print(", ");
}
System.out.print(a.get(i));
}
System.out.println("]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.chen0040.moea.components;


import com.github.chen0040.moea.utils.ArrayListUtils;
import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Comparator;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.testng.Assert.*;


/**
* Created by xschen on 15/6/2017.
*/
public class PopulationUnitTest {

private static final int dimension = 1000;

private Mediator mediator;
@BeforeMethod
public void setUp(){
mediator = new Mediator();
mediator.setDimension(dimension);
mediator.setLowerBounds(ArrayListUtils.zeros(dimension));
mediator.setUpperBounds(ArrayListUtils.asList(1.0, dimension));
mediator.setObjectiveCount(3);
mediator.setMutationRate(0.2);

mediator = Mockito.spy(mediator);
Mockito.when(mediator.evaluate(any(), eq(0))).thenReturn(10.0);
Mockito.when(mediator.evaluate(any(), eq(1))).thenReturn(20.0);
Mockito.when(mediator.evaluate(any(), eq(2))).thenReturn(30.0);
}

@Test
public void test_truncate(){
Population p = new Population();
p.setPopulationSize(1000);
p.initialize();

for(int i=0; i < p.size(); ++i){
Solution s = p.getSolutions().get(i);
s.getCosts().add(mediator.nextDouble());
s.getCosts().add(mediator.nextDouble());
s.getCosts().add(mediator.nextDouble());
}

assertThat(p.size()).isEqualTo(1000);

p.truncate(500, Comparator.comparingDouble(s -> s.getCosts().get(0)));

assertThat(p.size()).isEqualTo(500);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.github.chen0040.moea.utils.ArrayListUtils;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand All @@ -24,7 +23,7 @@ public void setUp(){
mediator = new Mediator();
mediator.setDimension(dimension);
mediator.setLowerBounds(ArrayListUtils.zeros(dimension));
mediator.setUpperBounds(ArrayListUtils.asArray(1.0, dimension));
mediator.setUpperBounds(ArrayListUtils.asList(1.0, dimension));
mediator.setObjectiveCount(3);
mediator.setMutationRate(0.2);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.chen0040.moea.utils;


import com.github.chen0040.moea.components.RandomGeneratorImpl;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

import static org.testng.Assert.*;


/**
* Created by xschen on 15/6/2017.
*/
public class QuickSortUnitTest {

@Test
public void test_sort(){
List<Integer> a = Arrays.asList(10, 3, 5, 2, 5, 6,10, 11);

QuickSort.sort(a, Integer::compareTo);

assertTrue(SortUtils.isSorted(a, Integer::compareTo));

SortUtils.print(a);
}

@Test
public void test_sort_large(){
List<Integer> a = ArrayListUtils.range(100);
KnuthShuffle.shuffle(a, new RandomGeneratorImpl());

assertFalse(SortUtils.isSorted(a, Integer::compareTo));

QuickSort.sort(a, Integer::compareTo);

assertTrue(SortUtils.isSorted(a, Integer::compareTo));

SortUtils.print(a);
}
}

0 comments on commit d882f4b

Please sign in to comment.