Skip to content

Commit

Permalink
fixed checkstyle error + tests for IExperimentStage
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Oct 31, 2020
1 parent 102eee9 commit edd679a
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 31 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.71` is the current version of `aitoa-code`.
Here, `0.8.72` 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.71</version>
<version>0.8.72</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.71</version>
<version>0.8.72</version>
<packaging>jar</packaging>
<name>aitoa-code</name>
<description>Example Source Codes from the Book "Introduction to Optimization Algorithms"</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import aitoa.structure.IObjectiveFunction;

/**
* The makespan objective function working directly on the
* order-based representation.
* A variant of the {@linkplain JSSPMakespanObjectiveFunction
* makespan objective function} working directly on the
* order-based representation, i.e., {@code int[]}.
*/
public final class JSSPMakespanObjectiveFunction2
implements IObjectiveFunction<int[]> {
Expand All @@ -16,8 +17,6 @@ public final class JSSPMakespanObjectiveFunction2

/** the current time at a given machine */
private final int[] mMachineTime;
/** the current step index at a given machine */
private final int[] mMachineState;
/** the step index of the current job */
private final int[] mJobState;
/** the time of the current job */
Expand All @@ -32,28 +31,27 @@ public final class JSSPMakespanObjectiveFunction2
/**
* create the representation
*
* @param _instance
* @param pinstance
* the problem instance
*/
public JSSPMakespanObjectiveFunction2(
final JSSPInstance _instance) {
final JSSPInstance pinstance) {
super();
this.instance = Objects.requireNonNull(_instance);
this.mJobs = _instance.jobs;
this.mJobState = new int[_instance.n];
this.mJobTime = new int[_instance.n];
this.mMachineTime = new int[_instance.m];
this.mMachineState = new int[_instance.m];
this.instance = Objects.requireNonNull(pinstance);
this.mJobs = pinstance.jobs;
this.mJobState = new int[pinstance.n];
this.mJobTime = new int[pinstance.n];
this.mMachineTime = new int[pinstance.m];
}

/**
* create
*
* @param _instance
* @param pinstance
* the instance
*/
public JSSPMakespanObjectiveFunction2(final String _instance) {
this(new JSSPInstance(_instance));
public JSSPMakespanObjectiveFunction2(final String pinstance) {
this(new JSSPInstance(pinstance));
}

/** {@inheritDoc} */
Expand All @@ -65,17 +63,14 @@ public String toString() {
/** {@inheritDoc} */
@Override
public double evaluate(final int[] y) {
final int[] machineState = this.mMachineState;
final int[] machineTime = this.mMachineTime;
final int[] jobState = this.mJobState;
final int[] jobTime = this.mJobTime;
Arrays.fill(machineState, 0);
Arrays.fill(jobState, 0);
Arrays.fill(machineTime, 0);
Arrays.fill(jobTime, 0);

// iterate over the jobs in the solution
int end = -1;
for (final int nextJob : y) {
// jobState tells us the index in this list for the next step to
// do, but since the list contains machine/time pairs, we
Expand All @@ -86,19 +81,19 @@ public double evaluate(final int[] y) {
final int[] jobSteps = this.mJobs[nextJob];
// so we know the machine where the job needs to go next
final int machine = jobSteps[jobStep]; // get machine
// start time is maximum of the next time when the machine
// becomes idle and the time we have already spent on the job
final int start =
Math.max(machineTime[machine], jobTime[nextJob]);
// the end time is simply the start time plus the time the job
// needs to spend on the machine
end = start + jobSteps[jobStep + 1]; // end time
// it holds for both the machine (it will become idle after end)
// The start time is maximum of the next time when the machine
// becomes idle and the time we have already spent on the job.
// The end time is simply the start time plus the time the job
// needs to spend on the machine.
// It holds for both the machine (it will become idle after end)
// and the job (it can go to the next station after end)
jobTime[nextJob] = machineTime[machine] = end;
jobTime[nextJob] = machineTime[machine] = //
Math.max(machineTime[machine], jobTime[nextJob]) //
+ jobSteps[jobStep + 1]; // end time
}

// compute the makespan
int end = -1;
for (final int v : (machineTime.length > jobTime.length)
? jobTime : machineTime) {
if (v > end) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/aitoa/examples/jssp/TestEJSSPExperimentStage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aitoa.examples.jssp;

import java.util.stream.Stream;

import aitoa.structure.IMetaheuristic;
import aitoa.utils.Experiment.IExperimentStage;
import aitoa.utils.TestExperimentStages;

/** Test the {@link EJSSPExperimentStage} */
public class TestEJSSPExperimentStage extends
TestExperimentStages<int[], JSSPCandidateSolution,
JSSPMakespanObjectiveFunction,
IMetaheuristic<int[], JSSPCandidateSolution>,
IExperimentStage<int[], JSSPCandidateSolution,
JSSPMakespanObjectiveFunction,
IMetaheuristic<int[], JSSPCandidateSolution>>> {

/** create */
public TestEJSSPExperimentStage() {
super();
}

/** {@inheritDoc} */
@Override
protected
Stream<IExperimentStage<int[], JSSPCandidateSolution,
JSSPMakespanObjectiveFunction,
IMetaheuristic<int[], JSSPCandidateSolution>>>
getInstance() {
return Stream.of(EJSSPExperimentStage.values());
}
}
34 changes: 34 additions & 0 deletions src/test/java/aitoa/examples/jssp/TestEJSSPExperimentStageACO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package aitoa.examples.jssp;

import java.util.stream.Stream;

import aitoa.examples.jssp.aco.JSSPACOMakespanObjectiveFunction;
import aitoa.examples.jssp.aco.JSSPACORecord;
import aitoa.structure.IMetaheuristic;
import aitoa.utils.Experiment.IExperimentStage;
import aitoa.utils.TestExperimentStages;

/** Test the {@link EJSSPExperimentStageACO} */
public class TestEJSSPExperimentStageACO extends
TestExperimentStages<JSSPACORecord, JSSPACORecord,
JSSPACOMakespanObjectiveFunction,
IMetaheuristic<JSSPACORecord, JSSPACORecord>,
IExperimentStage<JSSPACORecord, JSSPACORecord,
JSSPACOMakespanObjectiveFunction,
IMetaheuristic<JSSPACORecord, JSSPACORecord>>> {

/** create */
public TestEJSSPExperimentStageACO() {
super();
}

/** {@inheritDoc} */
@Override
protected
Stream<IExperimentStage<JSSPACORecord, JSSPACORecord,
JSSPACOMakespanObjectiveFunction,
IMetaheuristic<JSSPACORecord, JSSPACORecord>>>
getInstance() {
return Stream.of(EJSSPExperimentStageACO.values());
}
}
100 changes: 100 additions & 0 deletions src/test/java/aitoa/utils/TestExperimentStage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package aitoa.utils;

import java.io.CharArrayWriter;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import aitoa.ObjectTest;
import aitoa.TestTools;
import aitoa.structure.BlackBoxProcessBuilder;
import aitoa.structure.IMetaheuristic;
import aitoa.structure.IObjectiveFunction;
import aitoa.utils.Experiment.IExperimentStage;

/**
* Test an experiment stage
*
* @param <X>
* the search space type
* @param <Y>
* the solution space type
* @param <P>
* the problem type
* @param <M>
* the metaheuristic type
* @param <S>
* the experiment stage type
*/
@Ignore
public abstract class TestExperimentStage<X, Y,
P extends IObjectiveFunction<Y>,
M extends IMetaheuristic<X, Y>,
S extends IExperimentStage<X, Y, P, M>>
extends ObjectTest<S> {

/** create */
protected TestExperimentStage() {
super();
}

/**
* test the experiment stage via the
* {@link IExperimentStage#getProblems()} method
*/
@Test(timeout = 3600000)
public void testStage() {
final S stage = this.getInstance();
Assert.assertNotNull(stage);

final BlackBoxProcessBuilder<X, Y> builder =
new BlackBoxProcessBuilder<>();
stage.configureBuilder(builder);

final Stream<Supplier<P>> problems = stage.getProblems();
Assert.assertNotNull(problems);

problems.forEach(supplier -> {
Assert.assertNotNull(supplier);
final P problem = supplier.get();
Assert.assertNotNull(problem);

stage.configureBuilderForProblem(builder, problem);

TestTools.assertGreater(
Experiment.nameFromObjectPrepare(problem).length(), 0);
TestTools.assertGreaterOrEqual(problem.upperBound(),
problem.lowerBound());
final int runs = stage.getRuns(problem);
TestTools.assertGreaterOrEqual(runs, 0);
if (runs > 0) {
final Stream<Supplier<M>> algorithms =
stage.getAlgorithms(problem);
Assert.assertNotNull(algorithms);
algorithms.forEach((asupplier) -> {
Assert.assertNotNull(asupplier);
final M algorithm = asupplier.get();
Assert.assertNotNull(algorithm);
try (final CharArrayWriter caw =
new CharArrayWriter()) {
algorithm.printSetup(caw);
} catch (final Throwable error) {
throw new AssertionError(
"There should be no error here.", //$NON-NLS-1$
error);
}
});
}
});
}

/** {@inheritDoc} */
@Override
protected void runAllTests() {
super.runAllTests();
this.testStage();
}
}
65 changes: 65 additions & 0 deletions src/test/java/aitoa/utils/TestExperimentStages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aitoa.utils;

import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import aitoa.ObjectTest;
import aitoa.structure.IMetaheuristic;
import aitoa.structure.IObjectiveFunction;
import aitoa.utils.Experiment.IExperimentStage;

/**
* Test a stream of experiment stages
*
* @param <X>
* the search space type
* @param <Y>
* the solution space type
* @param <P>
* the problem type
* @param <M>
* the metaheuristic type
* @param <S>
* the experiment stage type
*/
@Ignore
public abstract class TestExperimentStages<X, Y,
P extends IObjectiveFunction<Y>,
M extends IMetaheuristic<X, Y>,
S extends IExperimentStage<X, Y, P, M>>
extends ObjectTest<Stream<S>> {

/** create */
protected TestExperimentStages() {
super();
}

/**
* test the experiment stage via the
* {@link IExperimentStage#getProblems()} method
*/
@Test(timeout = 3600000)
public void testStages() {
final Stream<S> stages = this.getInstance();
Assert.assertNotNull(stages);

stages.forEach(stage -> {
new TestExperimentStage<X, Y, P, M, S>() {
@Override
protected S getInstance() {
return stage;
}
}.runAllTests();
});
}

/** {@inheritDoc} */
@Override
protected void runAllTests() {
super.runAllTests();
this.testStages();
}
}

0 comments on commit edd679a

Please sign in to comment.