Skip to content

Commit

Permalink
fixed naming issues and documentation
Browse files Browse the repository at this point in the history
In EAs, it is actually not called pruning, but clearing ... Ooops
  • Loading branch information
thomasWeise committed Apr 8, 2020
1 parent 9930af0 commit cf30408
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 89 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ First, you need to add the following repository, which is a repository that can
```

Than you can add the dependency on our `aitoa-code` repository into your `dependencies` section.
Here, `0.8.50` is the current version of `aitoa-code`.
Here, `0.8.51` is the current version of `aitoa-code`.
Notice that you may have more dependencies in your `dependencies` section, say on `junit`, but here I just put the one for `aitoa-code` as example.

```xml
<dependencies>
<dependency>
<groupId>com.github.thomasWeise</groupId>
<artifactId>aitoa-code</artifactId>
<version>0.8.50</version>
<version>0.8.51</version>
</dependency>
</dependencies>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>aitoa</groupId>
<artifactId>aitoa-code</artifactId>
<version>0.8.50</version>
<version>0.8.51</version>
<packaging>jar</packaging>
<name>aitoa-code</name>
<description>Example Source Codes from the Book "Introduction to Optimization Algorithms"</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/EA.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", this.cr));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/EA1p1.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", 0));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/EA1p1WithFitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", 0));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* An {@linkplain aitoa.algorithms.EA evolutionary algorithm}
* which prunes the population from candidate solutions with
* which clears the population from candidate solutions with
* identical objective values before the reproduction step. This
* ensures that all "parents" from which new points in the search
* space are derived have a different solution quality. This, in
Expand All @@ -40,7 +40,7 @@
* the solution space
*/
// start relevant
public final class EAWithPruning<X, Y>
public final class EAWithClearing<X, Y>
implements IMetaheuristic<X, Y> {
// end relevant

Expand All @@ -61,7 +61,7 @@ public final class EAWithPruning<X, Y>
* @param _lambda
* the number of offspring to be created
*/
public EAWithPruning(final double _cr, final int _mu,
public EAWithClearing(final double _cr, final int _mu,
final int _lambda) {
super();
if ((_cr < 0d) || (_cr > 1d) || (!(Double.isFinite(_cr)))) {
Expand Down Expand Up @@ -99,7 +99,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", this.cr));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", true)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", true)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand All @@ -108,7 +108,7 @@ public final void printSetup(final Writer output)
/** {@inheritDoc} */
@Override
public final String toString() {
return ((((("eap_" + //$NON-NLS-1$
return ((((("eac_" + //$NON-NLS-1$
this.mu) + '+') + this.lambda) + '@') + this.cr);
}

Expand All @@ -117,9 +117,10 @@ public final String toString() {
@Override
// start relevant
public final void solve(final IBlackBoxProcess<X, Y> process) {
// omitted: initialize local variables random, searchSpace,
// omitted: Initialize local variables random, searchSpace,
// nullary, unary, binary, and arrays P and P2 of length
// mu+lambda, and array T to null
// mu+lambda, and array T to null. Then fill the population with
// random individuals (you already know this).
// end relevant
// create local variables
final Random random = process.getRandom();
Expand All @@ -134,70 +135,68 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
Individual<X>[] T = null,
P = new Individual[this.mu + this.lambda],
P2 = new Individual[P.length];
// start relevant

// 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 Individual<>(x, process.evaluate(x));
// end relevant
if (process.shouldTerminate()) { // we return
return; // best solution is stored in process
}
// start relevant
}

// start relevant
while (!process.shouldTerminate()) { // main loop
// shuffle P, so after sorting the order of unique recs is random
// Shuffle P: After sorting the order of unique recs is random.
RandomUtils.shuffle(random, P, 0, P.length);
// sort the population: mu best individuals at front are selected
// Sort the population: mu best individuals are at the front.
Arrays.sort(P);
// we now want to keep only the solutions with unique fitness
// We now want to keep only the solutions with unique quality.
int unique = 0, done = 0, end = P.length;
T = P; // since array P is sorted, so we can do this by
P = P2; // processing it from begin to end and copying
P2 = T; // these individuals to the start of another array
// we switch the two arrays here so the rest is the same as EA
T = P; // First switch the arrays. P2 is sorted. We process
P = P2; // it from begin to end and copy the unique records
P2 = T; // to the start of P, the rest to the end of P.
makeUnique: for (final Individual<X> ind : P2) {
++done;
if ((unique <= 0) || //
++done; // Increase number of processed in individuals.
if ((unique <= 0) || // two lines: check for uniqueness
(ind.quality > P[unique - 1].quality)) {
P[unique] = ind;
if ((++unique) >= this.mu) { // we are done and can
P[unique] = ind; // Individual unique -> copy to start
if ((++unique) >= this.mu) { // We got enough records:
System.arraycopy(P2, done, P, unique, // copy the
P.length - done); // remaining individuals
break makeUnique; // directly, they do not need to
} // be unique, as they will be overwritten anyway
} // be unique, as they will be overwritten anyway.
} else { // ind has an already-seen quality, so we copy
P[--end] = ind; // it to the end of the array, where
} // it will eventually be overwritten
} // it will eventually be overwritten.
}
// now we have 1 <= unique <= mu unique solutions
// shuffle the first unique solutions to ensure fairness
// Now we have 1 <= unique <= mu unique solutions.
// Shuffle the unique solutions again, to ensure fairness.
RandomUtils.shuffle(random, P, 0, unique);
int p1 = -1; // index to iterate over first parent

// override the worse (mu+lambda-unique) solutions
// Override the worse (mu + lambda - unique) solutions.
for (int index = P.length; (--index) >= unique;) {
if (process.shouldTerminate()) { // we return
return; // best solution is stored in process
if (process.shouldTerminate()) { // Finished.
return; // The best solution is stored in process.
}

final Individual<X> dest = P[index];
p1 = (p1 + 1) % unique;
final Individual<X> sel = P[p1];
final Individual<X> dest = P[index]; // offspring
p1 = (p1 + 1) % unique; // parent 1 index
final Individual<X> sel = P[p1]; // parent 1
if ((unique >= 2) && (random.nextDouble() <= this.cr)) {
int p2; // to hold index of second selected record
int p2; // p2 is the index of second selected parent.
do { // find a second, different record
p2 = random.nextInt(unique);
} while (p2 == p1);
// perform recombination of the two selected individuals
} while (p2 == p1); // Of course, can't be p1.
// Perform recombination of the two selected individuals.
binary.apply(sel.x, P[p2].x, dest.x, random);
} else {
// create modified copy of parent using unary operator
} else { // Otherwise: Mutation.
// Create modified copy of parent using unary operator.
unary.apply(sel.x, dest.x, random);
}
// map to solution/schedule and evaluate quality
// Map to candidate solution and evaluate quality.
dest.quality = process.evaluate(dest.x);
} // the end of the offspring generation
} // the end of the main loop
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/EAWithFitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", this.cr));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/EAWithRestarts.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", this.cr));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", true)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/MA.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", 1d));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
* always applies the binary operator to find new points in the
* search space and then refines them with a
* {@linkplain aitoa.algorithms.HillClimber2 first-improvement
* local search} based on a unary operator. This MA with pruning
* here also, well, prunes the population from candidate
* local search} based on a unary operator. This MA with clearing
* here also, well, clears the population from candidate
* solutions with identical objective values before the
* reproduction step.
* <p>
* All candidate solutions represented in the population will
* always be local optima before entering the binary operators.
* If these operators work well, they may jump close to other
* local optima. The pruning ensures that all "parents" from
* local optima. The clearing ensures that all "parents" from
* which new points in the search space are derived have a
* different solution quality. This, in turn, implies that they
* are different candidate solutions. These, in turn, must be the
Expand All @@ -50,7 +50,7 @@
* the solution space
*/
// start relevant
public final class MAWithPruning<X, Y>
public final class MAWithClearing<X, Y>
implements IMetaheuristic<X, Y> {
// end relevant

Expand All @@ -71,7 +71,7 @@ public final class MAWithPruning<X, Y>
* @param _maxLSSteps
* the maximum number of local search steps
*/
public MAWithPruning(final int _mu, final int _lambda,
public MAWithClearing(final int _mu, final int _lambda,
final int _maxLSSteps) {
super();
if ((_mu <= 1) || (_mu > 1_000_000)) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", 1d));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", true)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", true)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand All @@ -117,7 +117,7 @@ public final void printSetup(final Writer output)
/** {@inheritDoc} */
@Override
public final String toString() {
final String s = ((("map_" + //$NON-NLS-1$
final String s = ((("mac_" + //$NON-NLS-1$
this.mu) + '+') + this.lambda);
if (this.maxLSSteps >= Integer.MAX_VALUE) {
return s;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/MAWithFitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public final void printSetup(final Writer output)
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("cr", 1d));//$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("pruning", false)); //$NON-NLS-1$
output.write(LogFormat.mapEntry("clearing", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/aitoa/examples/jssp/EJSSPExperimentStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import java.util.stream.Stream;

import aitoa.algorithms.EA;
import aitoa.algorithms.EAWithPruning;
import aitoa.algorithms.EAWithClearing;
import aitoa.algorithms.EDA;
import aitoa.algorithms.HillClimber;
import aitoa.algorithms.HillClimber2;
import aitoa.algorithms.HillClimber2WithRestarts;
import aitoa.algorithms.HillClimberWithRestarts;
import aitoa.algorithms.MA;
import aitoa.algorithms.MAWithPruning;
import aitoa.algorithms.MAWithClearing;
import aitoa.algorithms.RandomSampling;
import aitoa.algorithms.SimulatedAnnealing;
import aitoa.algorithms.SingleRandomSample;
Expand Down Expand Up @@ -219,7 +219,7 @@ public void configureBuilderForProblem(
EJSSPExperimentStage._eas(
new int[] { 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 16384, 32768, 65536 },
new double[] { 0.05, 0.3 }, true));
new double[] { 0.05 }, true));
}

/**
Expand Down Expand Up @@ -611,7 +611,7 @@ public void configureBuilderForProblem(
? new double[] { 0d, 0.05d, 0.3d } : crossoverRates)) {
list.add(() -> new EA<>(cr, ps, ps));
if (withPruning) {
list.add(() -> new EAWithPruning<>(cr, ps, ps));
list.add(() -> new EAWithClearing<>(cr, ps, ps));
}
}
}
Expand Down Expand Up @@ -672,25 +672,25 @@ public void configureBuilderForProblem(
_ma() {
return Stream.of(//
() -> new MA<>(16, 16, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(16, 16, Integer.MAX_VALUE), //
() -> new MAWithClearing<>(16, 16, Integer.MAX_VALUE), //
() -> new MA<>(16, 16, 10), //
() -> new MAWithPruning<>(16, 16, 10), //
() -> new MAWithClearing<>(16, 16, 10), //
() -> new MA<>(16, 16, 100), //
() -> new MAWithPruning<>(16, 16, 100), //
() -> new MAWithClearing<>(16, 16, 100), //
//
() -> new MA<>(64, 64, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(64, 64, Integer.MAX_VALUE), //
() -> new MAWithClearing<>(64, 64, Integer.MAX_VALUE), //
() -> new MA<>(64, 64, 10), //
() -> new MAWithPruning<>(64, 64, 10), //
() -> new MAWithClearing<>(64, 64, 10), //
() -> new MA<>(64, 64, 100), //
() -> new MAWithPruning<>(64, 64, 100), //
() -> new MAWithClearing<>(64, 64, 100), //
//
() -> new MA<>(256, 256, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(256, 256, Integer.MAX_VALUE), //
() -> new MAWithClearing<>(256, 256, Integer.MAX_VALUE), //
() -> new MA<>(256, 256, 10), //
() -> new MAWithPruning<>(256, 256, 10), //
() -> new MAWithClearing<>(256, 256, 10), //
() -> new MA<>(256, 256, 100), //
() -> new MAWithPruning<>(64, 64, 100), //
() -> new MAWithClearing<>(64, 64, 100), //
//
() -> new MA<>(1024, 1024, Integer.MAX_VALUE), //
() -> new MA<>(1024, 1024, 10), //
Expand Down
Loading

0 comments on commit cf30408

Please sign in to comment.