Skip to content

Commit

Permalink
Merge pull request #377 from kusumotolab/move-constant-in-GA
Browse files Browse the repository at this point in the history
GA内の定数をConfigに移動
  • Loading branch information
tanimon authored Oct 31, 2018
2 parents bedfbb9 + 98aff53 commit 7d1e51f
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 84 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ $ java -jar path/to/kGenProg.jar
| `-o`, `--out-dir` | kGenProg が結果の出力を行うディレクトリへのパス.指定ディレクトリ直下に実行時のタイムスタンプを名前とするディレクトリが生成され,そのディレクトリに結果が出力されます. | カレントディレクトリ直下に `kgenprog-out` というディレクトリが作成される |
| `-v`, `--verbose` | 詳細なログを出力する | `false` |
| `-q`, `--quiet` | エラー出力のみを行う | `false` |
| `--siblings-count` | 遺伝的アルゴリズムの変異操作によって1つの個体から1世代に生成する個体の数 | 10 |
| `--mutation-generating-count` | 遺伝的アルゴリズムの変異操作によって1つの世代に生成する個体の数 | 10 |
| `--crossover-generating-count` | 遺伝的アルゴリズムの交叉操作によって1つの世代に生成する個体の数 | 10 |
| `--headcount` | 遺伝的アルゴリズムの選択操作によって1世代に残される個体の最大数 | 100 |
| `--max-generation` | 遺伝的アルゴリズムを打ち切る世代数 | 10 |
| `--time-limit` | 遺伝的アルゴリズムを打ち切る時間(秒) | 60 |
Expand Down
4 changes: 4 additions & 0 deletions example/BuildSuccess08/withCrossoverGeneratingCount.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root-dir = "./"
src = ["src/"]
test = ["test/"]
crossover-generating-count = 50
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
root-dir = "./"
src = ["src/"]
test = ["test/"]
siblings-count = 50

mutation-generating-count = 50
7 changes: 5 additions & 2 deletions example/CloseToZero01/kgenprog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ test = ["src/example/CloseToZeroTest.java"]
# Selects the log level to be shown at least.
#log-level = ("OFF" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE" | "ALL")

# Specifies how many variants are generated from a parent in a generation.
#siblings-count = <num>
# Specifies how many variants are generated in a generation by a mutation.
#mutation-generating-count = <num>

# Specifies how many variants are generated in a generation by a crossover.
#crossover-generating-count = <num>

# Specifies how many variants survive in a generation.
#headcount = <num>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/jp/kusumotolab/kgenprog/CUILauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public void launch(final Configuration config) {
final RouletteStatementSelection rouletteStatementSelection =
new RouletteStatementSelection(random);
final Mutation mutation =
new RandomMutation(config.getSiblingsCount(), random, rouletteStatementSelection);
final Crossover crossover = new SinglePointCrossover(random);
new RandomMutation(config.getMutationGeneratingCount(), random, rouletteStatementSelection);
final Crossover crossover = new SinglePointCrossover(random,
config.getCrossoverGeneratingCount());
final SourceCodeGeneration sourceCodeGeneration = new DefaultSourceCodeGeneration();
final SourceCodeValidation sourceCodeValidation = new DefaultCodeValidation();
final VariantSelection variantSelection = new DefaultVariantSelection(config.getHeadcount());
Expand Down
56 changes: 41 additions & 15 deletions src/main/java/jp/kusumotolab/kgenprog/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class Configuration {

// region Fields
public static final int DEFAULT_MAX_GENERATION = 10;
public static final int DEFAULT_SIBLINGS_COUNT = 10;
public static final int DEFAULT_MUTATION_GENERATING_COUNT = 10;
public static final int DEFAULT_CROSSOVER_GENERATING_COUNT = 10;
public static final int DEFAULT_HEADCOUNT = 100;
public static final int DEFAULT_REQUIRED_SOLUTIONS_COUNT = 1;
public static final Duration DEFAULT_TIME_LIMIT = Duration.ofSeconds(60);
Expand All @@ -59,7 +60,8 @@ public class Configuration {
private final List<String> executionTests;
private final Path workingDir;
private final Path outDir;
private final int siblingsCount;
private final int mutationGeneratingCount;
private final int crossoverGeneratingCount;
private final int headcount;
private final int maxGeneration;
private final Duration timeLimit;
Expand All @@ -77,7 +79,8 @@ private Configuration(final Builder builder) {
executionTests = builder.executionTests;
workingDir = builder.workingDir;
outDir = builder.outDir;
siblingsCount = builder.siblingsCount;
mutationGeneratingCount = builder.mutationGeneratingCount;
crossoverGeneratingCount = builder.crossoverGeneratingCount;
headcount = builder.headcount;
maxGeneration = builder.maxGeneration;
timeLimit = builder.timeLimit;
Expand Down Expand Up @@ -106,8 +109,12 @@ public Path getOutDir() {
return outDir;
}

public int getSiblingsCount() {
return siblingsCount;
public int getMutationGeneratingCount() {
return mutationGeneratingCount;
}

public int getCrossoverGeneratingCount() {
return crossoverGeneratingCount;
}

public int getHeadcount() {
Expand Down Expand Up @@ -195,9 +202,13 @@ public static class Builder {
@Conversion(PathToString.class)
private Path outDir = DEFAULT_OUT_DIR;

@com.electronwill.nightconfig.core.conversion.Path("siblings-count")
@com.electronwill.nightconfig.core.conversion.Path("mutation-generating-count")
@PreserveNotNull
private int mutationGeneratingCount = DEFAULT_MUTATION_GENERATING_COUNT;

@com.electronwill.nightconfig.core.conversion.Path("crossover-generating-count")
@PreserveNotNull
private int siblingsCount = DEFAULT_SIBLINGS_COUNT;
private int crossoverGeneratingCount = DEFAULT_CROSSOVER_GENERATING_COUNT;

@PreserveNotNull
private int headcount = DEFAULT_HEADCOUNT;
Expand Down Expand Up @@ -328,10 +339,17 @@ public Builder setOutDir(final Path outDir) {
return this;
}

public Builder setSiblingsCount(final int siblingsCount) {
log.debug("enter setSiblingsCount(int)");
public Builder setMutationGeneratingCount(final int mutationGeneratingCount) {
log.debug("enter setMutationGeneratingCount(int)");

this.siblingsCount = siblingsCount;
this.mutationGeneratingCount = mutationGeneratingCount;
return this;
}

public Builder setCrossoverGeneratingCount(final int crossoverGeneratingCount) {
log.debug("enter setCrossoverGeneratingCount(int)");

this.crossoverGeneratingCount = crossoverGeneratingCount;
return this;
}

Expand Down Expand Up @@ -564,12 +582,20 @@ private void setOutDirFromCmdLineParser(final String outDir) {
this.outDir = Paths.get(outDir);
}

@Option(name = "--siblings-count", metaVar = "<num>",
usage = "Specifies how many variants are generated from a parent in a generation.",
@Option(name = "--mutation-generating-count", metaVar = "<num>",
usage = "Specifies how many variants are generated in a generation by a mutation.",
depends = {"-r", "-s", "-t"})
private void setMutationGeneratingCountFromCmdLineParser(final int mutationGeneratingCount) {
log.debug("enter setMutationGeneratingCountFromCmdLineParser(int)");
this.mutationGeneratingCount = mutationGeneratingCount;
}

@Option(name = "--crossover-generating-count", metaVar = "<num>",
usage = "Specifies how many variants are generated in a generation by a crossover.",
depends = {"-r", "-s", "-t"})
private void setSiblingsCountFromCmdLineParser(final int siblingsCount) {
log.debug("enter setSiblingsCountFromCmdLineParser(int)");
this.siblingsCount = siblingsCount;
private void setCrossOverGeneratingCountFromCmdLineParser(final int crossoverGeneratingCount) {
log.debug("enter setCrossOverGeneratingCountFromCmdLineParser(int)");
this.crossoverGeneratingCount = crossoverGeneratingCount;
}

@Option(name = "--headcount", metaVar = "<num>",
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/jp/kusumotolab/kgenprog/PerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ private static KGenProgMain createMain(final Path rootPath, final Path productPa
final Random random = new Random(config.getRandomSeed());
final CandidateSelection statementSelection = new RouletteStatementSelection(random);
final Mutation mutation = new RandomMutation(10, random, statementSelection);
final Crossover crossover = new SinglePointCrossover(random);
final Crossover crossover = new SinglePointCrossover(random,
config.getCrossoverGeneratingCount());
final SourceCodeGeneration sourceCodeGeneration = new DefaultSourceCodeGeneration();
final SourceCodeValidation sourceCodeValidation = new DefaultCodeValidation();
final VariantSelection variantSelection = new GenerationalVariantSelection();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jp/kusumotolab/kgenprog/ga/Mutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public abstract class Mutation {
private static Logger log = LoggerFactory.getLogger(Mutation.class);

protected final Random random;
protected final int numberOfBase;
protected final int mutationGeneratingCount;
protected final CandidateSelection candidateSelection;

public Mutation(final int numberOfBase, final Random random,
public Mutation(final int mutationGeneratingCount, final Random random,
final CandidateSelection candidateSelection) {
this.random = random;
this.numberOfBase = numberOfBase;
this.mutationGeneratingCount = mutationGeneratingCount;
this.candidateSelection = candidateSelection;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jp/kusumotolab/kgenprog/ga/RandomMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class RandomMutation extends Mutation {

private static final Logger log = LoggerFactory.getLogger(RandomMutation.class);

public RandomMutation(final int numberOfBase, final Random random,
public RandomMutation(final int mutationGeneratingCount, final Random random,
final CandidateSelection candidateSelection) {
super(numberOfBase, random, candidateSelection);
super(mutationGeneratingCount, random, candidateSelection);
}

@Override
Expand All @@ -37,7 +37,7 @@ public List<Variant> exec(final VariantStore variantStore) {
return Double.isNaN(value) ? 0 : value + 1;
}, random);

for (int i = 0; i < numberOfBase; i++) {
for (int i = 0; i < mutationGeneratingCount; i++) {
final Variant variant = variantRoulette.exec();
final List<Suspiciousness> suspiciousnesses = variant.getSuspiciousnesses();
final Function<Suspiciousness, Double> weightFunction = susp -> Math.pow(susp.getValue(), 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public SinglePointCrossover(final Random random, final int crossoverGeneratingCo
this.crossoverGeneratingCount = crossoverGeneratingCount;
}

public SinglePointCrossover(final Random random) {
this(random, 10);
}

@Override
public List<Variant> exec(final VariantStore variantStore) {
log.debug("enter exec(List<>)");
Expand Down
Loading

0 comments on commit 7d1e51f

Please sign in to comment.