From 205890c09fc63830d40c18acf219283422ab65b3 Mon Sep 17 00:00:00 2001 From: tweise Date: Thu, 23 Apr 2020 05:19:17 +0800 Subject: [PATCH] cleanup of comments (e.g., removal of unnecessary "relevant" tags) --- src/main/java/aitoa/algorithms/EA1p1.java | 11 +++-------- .../java/aitoa/algorithms/EA1p1WithFitness.java | 3 --- .../java/aitoa/algorithms/EAWithRestarts.java | 2 -- .../java/aitoa/algorithms/EDAWithFitness.java | 9 +-------- .../java/aitoa/algorithms/HillClimber2.java | 9 +-------- src/main/java/aitoa/algorithms/HybridEDA.java | 8 -------- .../aitoa/algorithms/HybridEDAWithFitness.java | 10 ---------- src/main/java/aitoa/algorithms/MA.java | 10 ++++++---- .../java/aitoa/algorithms/MAWithClearing.java | 14 +------------- .../java/aitoa/algorithms/MAWithFitness.java | 17 ++--------------- 10 files changed, 14 insertions(+), 79 deletions(-) diff --git a/src/main/java/aitoa/algorithms/EA1p1.java b/src/main/java/aitoa/algorithms/EA1p1.java index 2c875a6..31f6d8b 100644 --- a/src/main/java/aitoa/algorithms/EA1p1.java +++ b/src/main/java/aitoa/algorithms/EA1p1.java @@ -28,9 +28,8 @@ * @param * the solution space */ -// start relevant public final class EA1p1 implements IMetaheuristic { -// end relevant + /** create */ public EA1p1() { super(); @@ -38,10 +37,9 @@ public EA1p1() { /** {@inheritDoc} */ @Override -// start relevant public final void solve(final IBlackBoxProcess process) { // init local variables x_cur, x_best, nullary, unary, random -// end relevant + final X x_cur = process.getSearchSpace().create(); final X x_best = process.getSearchSpace().create(); final INullarySearchOperator nullary = @@ -49,7 +47,7 @@ public final void solve(final IBlackBoxProcess process) { final IUnarySearchOperator unary = process.getUnarySearchOperator(); // get unary op final Random random = process.getRandom();// get random gen -// start relevant + // create starting point: a random point in the search space nullary.apply(x_best, random); // put random point in x_best double f_best = process.evaluate(x_best); // map & evaluate @@ -66,7 +64,6 @@ public final void solve(final IBlackBoxProcess process) { } // otherwise, i.e., f_cur > f_best: just forget x_cur } // until time is up } // process will have remembered the best candidate solution -// end relevant /** {@inheritDoc} */ @Override @@ -101,6 +98,4 @@ public final void printSetup(final Writer output) return IMetaheuristic.getSetupNameWithUnaryOperator(this, builder); } -// start relevant } -// end relevant diff --git a/src/main/java/aitoa/algorithms/EA1p1WithFitness.java b/src/main/java/aitoa/algorithms/EA1p1WithFitness.java index 5028d96..c94e8a7 100644 --- a/src/main/java/aitoa/algorithms/EA1p1WithFitness.java +++ b/src/main/java/aitoa/algorithms/EA1p1WithFitness.java @@ -29,13 +29,11 @@ * @param * the solution space */ -// start relevant public final class EA1p1WithFitness implements IMetaheuristic { /** the fitness assignment process */ public final FitnessAssignmentProcess fitness; -// end relevant /** * create * @@ -51,7 +49,6 @@ public EA1p1WithFitness( /** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") -// start relevant public final void solve(final IBlackBoxProcess process) { // init local variables pop, nullary, unary, random // end relevant diff --git a/src/main/java/aitoa/algorithms/EAWithRestarts.java b/src/main/java/aitoa/algorithms/EAWithRestarts.java index 8f81638..c6a2098 100644 --- a/src/main/java/aitoa/algorithms/EAWithRestarts.java +++ b/src/main/java/aitoa/algorithms/EAWithRestarts.java @@ -141,11 +141,9 @@ public final void solve(final IBlackBoxProcess process) { final X x = searchSpace.create(); nullary.apply(x, random); population[i] = new Individual<>(x, process.evaluate(x)); -// end relevant if (process.shouldTerminate()) { return; } -// start relevant } while (nonImprovedGen < this.generationsUntilRestart) { diff --git a/src/main/java/aitoa/algorithms/EDAWithFitness.java b/src/main/java/aitoa/algorithms/EDAWithFitness.java index c23e5df..d07d32f 100644 --- a/src/main/java/aitoa/algorithms/EDAWithFitness.java +++ b/src/main/java/aitoa/algorithms/EDAWithFitness.java @@ -22,10 +22,8 @@ * @param * the solution space */ -// start relevant public final class EDAWithFitness implements IMetaheuristic { -// end relevant /** the number of solution to be selected */ public final int mu; @@ -100,9 +98,7 @@ public final String toString() { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override -// start relevant public final void solve(final IBlackBoxProcess process) { -// end relevant // create local variables final Random random = process.getRandom(); final ISpace searchSpace = process.getSearchSpace(); @@ -113,7 +109,7 @@ public final void solve(final IBlackBoxProcess process) { final FitnessIndividual[] P = new FitnessIndividual[this.lambda]; this.fitness.initialize(); -// start relevant + // local variable initialization omitted for brevity Model.initialize(); // initialize model=uniform distribution @@ -122,11 +118,9 @@ public final void solve(final IBlackBoxProcess process) { final X x = searchSpace.create(); nullary.apply(x, random); P[i] = new FitnessIndividual<>(x, process.evaluate(x)); -// end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant } for (;;) {// each iteration: update model, sample model @@ -146,4 +140,3 @@ public final void solve(final IBlackBoxProcess process) { } // the end of the main loop } } -// end relevant diff --git a/src/main/java/aitoa/algorithms/HillClimber2.java b/src/main/java/aitoa/algorithms/HillClimber2.java index 49e9d24..b83539c 100644 --- a/src/main/java/aitoa/algorithms/HillClimber2.java +++ b/src/main/java/aitoa/algorithms/HillClimber2.java @@ -29,11 +29,9 @@ * @param * the solution space */ -// start relevant public final class HillClimber2 implements IMetaheuristic { -// end relevant /** create */ public HillClimber2() { super(); @@ -41,11 +39,9 @@ public HillClimber2() { /** {@inheritDoc} */ @Override -// start relevant public final void solve(final IBlackBoxProcess process) { // init local variables x_cur, x_best, nullary, unary, random, // f_best, improved: omitted here for brevity -// end relevant final X x_cur = process.getSearchSpace().create(); final X x_best = process.getSearchSpace().create(); final INullarySearchOperator nullary = @@ -54,7 +50,7 @@ public final void solve(final IBlackBoxProcess process) { process.getUnarySearchOperator(); // get unary op final Random random = process.getRandom();// get random gen boolean improved = true; -// start relevant + // create starting point: a random point in the search space nullary.apply(x_best, random); // put random point in x_best final double[] f_best = { process.evaluate(x_best) }; // evaluate @@ -79,7 +75,6 @@ public final void solve(final IBlackBoxProcess process) { } } // process will have remembered the best candidate solution -// end relevant /** {@inheritDoc} */ @Override @@ -94,6 +89,4 @@ public final String toString() { return IMetaheuristic.getSetupNameWithUnaryOperator(this, builder); } -// start relevant } -// end relevant diff --git a/src/main/java/aitoa/algorithms/HybridEDA.java b/src/main/java/aitoa/algorithms/HybridEDA.java index 46930a7..129f20b 100644 --- a/src/main/java/aitoa/algorithms/HybridEDA.java +++ b/src/main/java/aitoa/algorithms/HybridEDA.java @@ -27,10 +27,8 @@ * @param * the solution space */ -// start relevant public final class HybridEDA implements IMetaheuristic { -// end relevant /** the number of solution to be selected */ public final int mu; @@ -120,9 +118,7 @@ public final String toString() { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override -// start relevant public final void solve(final IBlackBoxProcess process) { -// end relevant // create local variables final Random random = process.getRandom(); final ISpace searchSpace = process.getSearchSpace(); @@ -135,7 +131,6 @@ public final void solve(final IBlackBoxProcess process) { final Individual[] P = new Individual[this.lambda]; final X temp = searchSpace.create(); -// start relevant // the initialization of local variables is omitted for brevity Model.initialize(); // initialize model=uniform distribution @@ -144,11 +139,9 @@ public final void solve(final IBlackBoxProcess process) { final X x = searchSpace.create(); nullary.apply(x, random); P[i] = new Individual<>(x, process.evaluate(x)); -// end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant } for (;;) {// each iteration: LS, update model, then sample @@ -187,4 +180,3 @@ public final void solve(final IBlackBoxProcess process) { } // the end of the main loop } } -// end relevant diff --git a/src/main/java/aitoa/algorithms/HybridEDAWithFitness.java b/src/main/java/aitoa/algorithms/HybridEDAWithFitness.java index f8322d0..ac381ae 100644 --- a/src/main/java/aitoa/algorithms/HybridEDAWithFitness.java +++ b/src/main/java/aitoa/algorithms/HybridEDAWithFitness.java @@ -24,10 +24,8 @@ * @param * the solution space */ -// start relevant public final class HybridEDAWithFitness implements IMetaheuristic { -// end relevant /** the number of solution to be selected */ public final int mu; @@ -127,9 +125,7 @@ public final String toString() { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override -// start relevant public final void solve(final IBlackBoxProcess process) { -// end relevant // create local variables final Random random = process.getRandom(); final ISpace searchSpace = process.getSearchSpace(); @@ -144,7 +140,6 @@ public final void solve(final IBlackBoxProcess process) { final X temp = searchSpace.create(); this.fitness.initialize(); -// start relevant // the initialization of local variables is omitted for brevity Model.initialize(); // initialize model=uniform distribution @@ -153,11 +148,9 @@ public final void solve(final IBlackBoxProcess process) { final X x = searchSpace.create(); nullary.apply(x, random); P[i] = new FitnessIndividual<>(x, process.evaluate(x)); -// end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant } for (;;) {// each iteration: LS, update model, then sample @@ -194,12 +187,9 @@ public final void solve(final IBlackBoxProcess process) { Model.sample(dest.x, random); dest.quality = process.evaluate(dest.x); } // the end of the new points generation -// end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant } // the end of the main loop } } -// end relevant diff --git a/src/main/java/aitoa/algorithms/MA.java b/src/main/java/aitoa/algorithms/MA.java index 14d4f66..a9003af 100644 --- a/src/main/java/aitoa/algorithms/MA.java +++ b/src/main/java/aitoa/algorithms/MA.java @@ -142,12 +142,13 @@ public final void solve(final IBlackBoxProcess process) { while (!process.shouldTerminate()) { // main loop for (final LSIndividual ind : P) { +// If ind is not known to be local optimum, refine it with local +// search à la HillClimber2 for a given number of maximum steps +// (code omitted for brevity). +// end relevant if (ind.isOptimum) { continue; } -// refine ind with local search à la HillClimber2 (code omitted) -// for a given number of maximum steps -// end relevant int steps = this.maxLSSteps; do { // local search in style of HillClimber2 improved = unary.enumerate(random, ind.x, temp, // @@ -189,9 +190,10 @@ public final void solve(final IBlackBoxProcess process) { } while (p2 == p1); // perform recombination of the two selected individuals binary.apply(sel.x, P[p2].x, dest.x, random); -// map to solution/schedule and evaluate quality dest.quality = process.evaluate(dest.x); +// end relevant dest.isOptimum = false; +// start relevant } // the end of the offspring generation } // the end of the main loop } diff --git a/src/main/java/aitoa/algorithms/MAWithClearing.java b/src/main/java/aitoa/algorithms/MAWithClearing.java index 89893a7..92b394f 100644 --- a/src/main/java/aitoa/algorithms/MAWithClearing.java +++ b/src/main/java/aitoa/algorithms/MAWithClearing.java @@ -49,10 +49,8 @@ * @param * the solution space */ -// start relevant public final class MAWithClearing implements IMetaheuristic { -// end relevant /** the number of selected parents */ public final int mu; @@ -128,12 +126,7 @@ public final String toString() { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override -// start relevant public final void solve(final IBlackBoxProcess process) { -// omitted: initialize local variables random, searchSpace, -// nullary, unary, binary, and arrays P and P2 of length -// mu+lambda, and array T to null -// end relevant // create local variables final Random random = process.getRandom(); final ISpace searchSpace = process.getSearchSpace(); @@ -149,18 +142,16 @@ public final void solve(final IBlackBoxProcess process) { P2 = new LSIndividual[P.length]; boolean improved = false; int p2 = -1; -// start relevant + restart: while (!process.shouldTerminate()) { // first generation: fill population with random individuals for (int i = P.length; (--i) >= 0;) { final X x = searchSpace.create(); nullary.apply(x, random); P[i] = new LSIndividual<>(x, process.evaluate(x)); -// end relevant if (process.shouldTerminate()) { return; } -// start relevant } while (!process.shouldTerminate()) { // main loop @@ -236,7 +227,6 @@ public final void solve(final IBlackBoxProcess process) { } // the end of the main loop } // end of the restart loop } -// end relevant /** {@inheritDoc} */ @Override @@ -245,6 +235,4 @@ public final void solve(final IBlackBoxProcess process) { return IMetaheuristic.getSetupNameWithUnaryAndBinaryOperator(// this, builder); } -// start relevant } -// end relevant diff --git a/src/main/java/aitoa/algorithms/MAWithFitness.java b/src/main/java/aitoa/algorithms/MAWithFitness.java index 85d91f9..b90eddd 100644 --- a/src/main/java/aitoa/algorithms/MAWithFitness.java +++ b/src/main/java/aitoa/algorithms/MAWithFitness.java @@ -24,10 +24,8 @@ * @param * the solution space */ -// start relevant public final class MAWithFitness implements IMetaheuristic { -// end relevant /** the number of selected parents */ public final int mu; @@ -123,10 +121,7 @@ public final String toString() { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override -// start relevant public final void solve(final IBlackBoxProcess process) { -// the initialization of local variables is omitted for brevity -// end relevant // create local variables final Random random = process.getRandom(); final ISpace searchSpace = process.getSearchSpace(); @@ -143,18 +138,15 @@ public final void solve(final IBlackBoxProcess process) { final LSFitnessIndividual[] P = new LSFitnessIndividual[this.mu + this.lambda]; this.fitness.initialize(); -// start relevant // first generation: fill population with random individuals for (int i = P.length; (--i) >= 0;) { // set P[i] = random individual (code omitted) -// end relevant final X x = searchSpace.create(); nullary.apply(x, random); P[i] = new LSFitnessIndividual<>(x, process.evaluate(x)); if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant } while (!process.shouldTerminate()) { // main loop @@ -164,7 +156,6 @@ public final void solve(final IBlackBoxProcess process) { } int steps = this.maxLSSteps; // refine P[i] with local search à la HillClimber2 (code omitted) -// end relevant do { // local search in style of HillClimber2 improved = unary.enumerate(random, ind.x, temp, // (point) -> { @@ -183,7 +174,7 @@ public final void solve(final IBlackBoxProcess process) { } while (improved && ((--steps) > 0)); ind.isOptimum = !improved; // is it a local optimum? } // end of 1 ls iteration: we have refined 1 solution -// start relevant + // sort the population: mu best individuals at front are selected this.fitness.assignFitness(P); Arrays.sort(P, this.fitness); @@ -193,11 +184,10 @@ public final void solve(final IBlackBoxProcess process) { // override the worse lambda solutions with new offsprings for (int index = P.length; (--index) >= this.mu;) { -// end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } -// start relevant + final LSFitnessIndividual dest = P[index]; final LSFitnessIndividual sel = P[(++p1) % this.mu]; @@ -211,12 +201,9 @@ public final void solve(final IBlackBoxProcess process) { dest.isOptimum = false; } // the end of the offspring generation - // end relevant if (process.shouldTerminate()) { // we return return; // best solution is stored in process } - // start relevant } // the end of the main loop } } -// end relevant