-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Population with truncate api
- Loading branch information
Showing
9 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/com/github/chen0040/moea/components/Population.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/chen0040/moea/utils/InsertionSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/java/com/github/chen0040/moea/utils/KnuthShuffle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/main/java/com/github/chen0040/moea/utils/QuickSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/main/java/com/github/chen0040/moea/utils/SortUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("]"); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/com/github/chen0040/moea/components/PopulationUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/com/github/chen0040/moea/utils/QuickSortUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |