-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #793 from kusumotolab/fix-crossover-bug
crossoverのバグを修正
- Loading branch information
Showing
13 changed files
with
695 additions
and
154 deletions.
There are no files selected for viewing
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,10 @@ | ||
public class Foo { | ||
public void a(int n) { | ||
if (n) { | ||
n = 0; // loc0 | ||
} else { | ||
n = 1; // loc1 | ||
} | ||
n = 2; // loc2 | ||
} | ||
} |
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,13 @@ | ||
package example; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Test; | ||
|
||
public class FooTest { | ||
|
||
@Test | ||
public void test01() { | ||
assertEquals(1, 2); // always fail | ||
} | ||
|
||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/jp/kusumotolab/kgenprog/ga/crossover/CascadeCrossover.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,63 @@ | ||
package jp.kusumotolab.kgenprog.ga.crossover; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import jp.kusumotolab.kgenprog.ga.variant.Base; | ||
import jp.kusumotolab.kgenprog.ga.variant.CascadeCrossoverHistoricalElement; | ||
import jp.kusumotolab.kgenprog.ga.variant.Gene; | ||
import jp.kusumotolab.kgenprog.ga.variant.HistoricalElement; | ||
import jp.kusumotolab.kgenprog.ga.variant.Variant; | ||
import jp.kusumotolab.kgenprog.ga.variant.VariantStore; | ||
|
||
/** | ||
* 直列的な交叉を行うクラス. | ||
* | ||
* @author shinsuke | ||
*/ | ||
public class CascadeCrossover extends CrossoverAdaptor { | ||
|
||
/** | ||
* @param firstStrategy 1つ目の親を選ぶためのアルゴリズム | ||
* @param secondStrategy 2つ目の親を選ぶためのアルゴリズム | ||
*/ | ||
public CascadeCrossover(final FirstVariantSelectionStrategy firstStrategy, | ||
final SecondVariantSelectionStrategy secondStrategy) { | ||
super(firstStrategy, secondStrategy, 2); | ||
} | ||
|
||
|
||
@Override | ||
protected List<Variant> makeVariants(final List<Variant> variants, final VariantStore store) | ||
throws CrossoverInfeasibleException { | ||
final Variant v1 = getFirstVariantSelectionStrategy().exec(variants); | ||
final Variant v2 = getSecondVariantSelectionStrategy().exec(variants, v1); | ||
final HistoricalElement histElement = new CascadeCrossoverHistoricalElement(v1, v2); | ||
|
||
// create two variants from cascaded genes | ||
final Gene cascadeGene1 = createCascadeGene(v1, v2); | ||
final Gene cascadeGene2 = createCascadeGene(v2, v1); // NOSONAR: it's intentional. | ||
final Variant newVariant1 = store.createVariant(cascadeGene1, histElement); | ||
final Variant newVariant2 = store.createVariant(cascadeGene2, histElement); | ||
|
||
return Arrays.asList(newVariant1, newVariant2); | ||
} | ||
|
||
@Override | ||
protected List<Variant> filter(final List<Variant> variants) { | ||
return variants; // no filter necessary | ||
} | ||
|
||
private Gene createCascadeGene(final Variant v1, final Variant v2) { | ||
final List<Base> cascadeBases = Stream.of(v1, v2) | ||
.map(Variant::getGene) | ||
.map(Gene::getBases) | ||
.flatMap(Collection::stream) | ||
.distinct() // remove shared genes meaning these two variants have blood relation | ||
.collect(Collectors.toList()); | ||
return new Gene(cascadeBases); | ||
} | ||
|
||
} |
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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/jp/kusumotolab/kgenprog/ga/variant/CascadeCrossoverHistoricalElement.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,38 @@ | ||
package jp.kusumotolab.kgenprog.ga.variant; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* cascade交叉の記録をするクラス | ||
*/ | ||
public class CascadeCrossoverHistoricalElement implements HistoricalElement { | ||
|
||
private final Variant parentA; | ||
private final Variant parentB; | ||
|
||
/** | ||
* @param parentA 交叉した片方の親 | ||
* @param parentB もう片方の親 | ||
*/ | ||
public CascadeCrossoverHistoricalElement(final Variant parentA, final Variant parentB) { | ||
this.parentA = parentA; | ||
this.parentB = parentB; | ||
} | ||
|
||
/** | ||
* @return 親のリスト | ||
*/ | ||
@Override | ||
public List<Variant> getParents() { | ||
return Arrays.asList(parentA, parentB); | ||
} | ||
|
||
/** | ||
* @return 適用した操作の名前 | ||
*/ | ||
@Override | ||
public String getOperationName() { | ||
return "cascade-crossover"; | ||
} | ||
} |
Oops, something went wrong.