Skip to content

Commit

Permalink
added (1+1)-EA experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Oct 8, 2020
1 parent 1eb4be1 commit 12aeb40
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,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.68` is the current version of `aitoa-code`.
Here, `0.8.69` 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.68</version>
<version>0.8.69</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.68</version>
<version>0.8.69</version>
<packaging>jar</packaging>
<name>aitoa-code</name>
<description>Example Source Codes from the Book "Introduction to Optimization Algorithms"</description>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/aitoa/algorithms/EA1p1.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
* @param <Y>
* the solution space
*/
// start relevant
public final class EA1p1<X, Y> extends Metaheuristic1<X, Y> {

// end relevant
/**
* Create the (1+1) EA
*
Expand All @@ -45,8 +46,9 @@ public EA1p1(final INullarySearchOperator<X> pNullary,

/** {@inheritDoc} */
@Override
// start relevant
public void solve(final IBlackBoxProcess<X, Y> process) {
// init local variables xCur, xBest, nullary, unary, random
// initialize local variables xCur, xBest, random
final X xCur = process.getSearchSpace().create();
final X xBest = process.getSearchSpace().create();
final Random random = process.getRandom();// get random gen
Expand All @@ -60,13 +62,14 @@ public void solve(final IBlackBoxProcess<X, Y> process) {
this.unary.apply(xBest, xCur, random);
// map xCur from X to Y and evaluate candidate solution
final double fCur = process.evaluate(xCur);
if (fCur <= fBest) { // we found a better solution
if (fCur <= fBest) { // we found a not-worse solution
// remember best objective value and copy xCur to xBest
fBest = fCur;
process.getSearchSpace().copy(xCur, xBest);
} // otherwise, i.e., fCur > fBest: just forget xCur
} // until time is up
} // process will have remembered the best candidate solution
// end relevant

/** {@inheritDoc} */
@Override
Expand Down Expand Up @@ -95,4 +98,6 @@ public void printSetup(final Writer output)
output.write(LogFormat.mapEntry("restarts", false)); //$NON-NLS-1$
output.write(System.lineSeparator());
}
// start relevant
}
// end relevant
2 changes: 1 addition & 1 deletion src/main/java/aitoa/algorithms/HillClimber.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public HillClimber(final INullarySearchOperator<X> pNullary,
@Override
// start relevant
public void solve(final IBlackBoxProcess<X, Y> process) {
// init local variables xCur, xBest, random
// initialize local variables xCur, xBest, random
// end relevant
final X xCur = process.getSearchSpace().create();
final X xBest = process.getSearchSpace().create();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/aitoa/algorithms/HillClimber2.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
* @param <Y>
* the solution space
*/
// start relevant
public final class HillClimber2<X, Y>
extends Metaheuristic1<X, Y> {

// end relevant
/**
* Create the hill climber
*
Expand All @@ -51,9 +52,9 @@ public HillClimber2(final INullarySearchOperator<X> pNullary,

/** {@inheritDoc} */
@Override
// start relevant
public void solve(final IBlackBoxProcess<X, Y> process) {
// init local variables xCur, xBest, n random, fBest, improved:
// omitted here for brevity
// init local variables xCur, xBest, n random, fBest, improved
final X xCur = process.getSearchSpace().create();
final X xBest = process.getSearchSpace().create();
final Random random = process.getRandom();// get random gen
Expand Down Expand Up @@ -81,13 +82,15 @@ public void solve(final IBlackBoxProcess<X, Y> process) {
});
// repeat until time is up or no further improvement possible
}

} // process will have remembered the best candidate solution
// end relevant

/** {@inheritDoc} */
@Override
public String toString() {
return Experiment.nameFromObjectsMerge("hc2f", //$NON-NLS-1$
this.unary);
}
// start relevant
}
// end relevant
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public HillClimber2WithRestarts(
// start relevant
public void solve(final IBlackBoxProcess<X, Y> process) {
// initialization of local variables xCur, xBest, random omitted
// for brevety
// for brevity
// end relevant
final X xCur = process.getSearchSpace().create();
final X xBest = process.getSearchSpace().create();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/aitoa/algorithms/HillClimberWithRestarts.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public void solve(final IBlackBoxProcess<X, Y> process) {
final Random random = process.getRandom();// get random gen

// start relevant
// omitted: initialize local variables xCur, xBest, random,
// failsBeforeRestart, and failCounter=0
// omitted for brevity initialize local variables xCur, xBest,
// random, failsBeforeRestart, and failCounter=0
while (!(process.shouldTerminate())) { // outer loop: restart
this.nullary.apply(xBest, random); // start=random solution
double fBest = process.evaluate(xBest); // evaluate it
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/aitoa/examples/jssp/EJSSPExperimentStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.stream.Stream;

import aitoa.algorithms.EA;
import aitoa.algorithms.EA1p1;
import aitoa.algorithms.EAWithClearing;
import aitoa.algorithms.EDA;
import aitoa.algorithms.EDAWithClearing;
Expand Down Expand Up @@ -439,6 +440,36 @@ public enum EJSSPExperimentStage implements
}
return list.stream();
}
},

/**
* The tenth stage: 1+1-EA with FFA
*/
STAGE_10 {

/**
* Get a stream of algorithm suppliers for a given problem
*
* @param problem
* the problem
* @return the stream of suppliers
*/
@Override
public
Stream<Supplier<
IMetaheuristic<int[], JSSPCandidateSolution>>>
getAlgorithms(//
final JSSPMakespanObjectiveFunction problem) {

return Stream.of(
() -> new EA1p1<>(
new JSSPNullaryOperator(problem.instance),
new JSSPUnaryOperator1Swap()),
() -> new EA1p1<>(
new JSSPNullaryOperator(problem.instance),
new JSSPUnaryOperatorNSwap()));

}
};

/** the instances to be used */
Expand Down

0 comments on commit 12aeb40

Please sign in to comment.