Skip to content

Commit

Permalink
several bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Mar 11, 2020
1 parent c3f4b03 commit 2a1d435
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 22 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.40` is the current version of `aitoa-code`.
Here, `0.8.41` 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.40</version>
<version>0.8.41</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions 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.40</version>
<version>0.8.41</version>
<packaging>jar</packaging>
<name>aitoa-code</name>
<description>Example Source Codes from the Book "Introduction to Optimization Algorithms"</description>
Expand Down Expand Up @@ -203,7 +203,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/aitoa/algorithms/EAWithPruning.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
// we switch the two arrays here so the rest is the same as EA
makeUnique: for (final Individual<X> ind : P2) {
++done;
if ((unique <= 0)
|| (ind.quality > P[unique - 1].quality)) {
if ((unique <= 0) || //
(ind.quality > P[unique - 1].quality)) {
P[unique] = ind;
if ((++unique) >= this.mu) { // we are done and can
System.arraycopy(P2, done, P, unique, // copy the
Expand All @@ -184,7 +184,8 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
}

final Individual<X> dest = P[index];
final Individual<X> sel = P[(++p1) % unique];
p1 = (p1 + 1) % unique;
final Individual<X> sel = P[p1];
if ((unique >= 2) && (random.nextDouble() <= this.cr)) {
int p2; // to hold index of second selected record
do { // find a second, different record
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 @@ -156,7 +156,7 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
if (newQuality < ind.quality) { // better?
ind.quality = newQuality; // store quality
searchSpace.copy(point, ind.x); // store point
return (true); // exit to next loop
return true; // exit to next loop
} // if we get here, point is not better
return process.shouldTerminate();
}); // repeat this until no improvement or time is
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/aitoa/algorithms/MAWithPruning.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
if (newQuality < ind.quality) { // better?
ind.quality = newQuality; // store quality
searchSpace.copy(point, ind.x); // store
return (true); // exit to next loop
return true; // exit to next loop
} // if we get here, point is not better
return process.shouldTerminate();
}); // repeat this until no improvement or time
// is up
}); // repeat until no improvement or time up
if (process.shouldTerminate()) { // we return
return; // best solution is stored in process
}
Expand All @@ -198,8 +197,8 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
// we switch the two arrays here so the rest is the same as EA
makeUnique: for (final Individual<X> ind : P2) {
++done;
if ((unique <= 0)
|| (ind.quality > P[unique - 1].quality)) {
if ((unique <= 0) || //
(ind.quality > P[unique - 1].quality)) {
P[unique] = ind;
if ((++unique) >= this.mu) { // we are done and can
System.arraycopy(P2, done, P, unique, // copy the
Expand All @@ -224,7 +223,8 @@ public final void solve(final IBlackBoxProcess<X, Y> process) {
return; // best solution is stored in process
}
final Individual<X> dest = P[index];
final Individual<X> sel = P[(++p1) % unique];
p1 = (p1 + 1) % unique;
final Individual<X> sel = P[p1];
// to hold index of second selected record
do { // find a second, different record
p2 = random.nextInt(unique);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/aitoa/examples/jssp/EJSSPExperimentStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,20 @@ public void configureBuilderForProblem(
() -> new MA<>(64, 64, 100), //
() -> new MAWithPruning<>(64, 64, 100), //
//
() -> new MA<>(256, 256, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(256, 256, Integer.MAX_VALUE), //
() -> new MA<>(256, 256, 10), //
() -> new MAWithPruning<>(256, 256, 10), //
() -> new MA<>(256, 256, 100), //
() -> new MAWithPruning<>(64, 64, 100), //
//
() -> new MA<>(1024, 1024, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(1024, 1024, Integer.MAX_VALUE), //
() -> new MA<>(1024, 1024, 10), //
() -> new MAWithPruning<>(1024, 1024, 10), //
() -> new MA<>(1024, 1024, 100), //
() -> new MAWithPruning<>(1024, 1024, 100), //
//
() -> new MA<>(4096, 4096, Integer.MAX_VALUE), //
() -> new MAWithPruning<>(4096, 4096, Integer.MAX_VALUE), //
() -> new MA<>(4096, 4096, 10), //
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/aitoa/examples/jssp/JSSPUnaryOperator1SwapU.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class JSSPUnaryOperator1SwapU
implements IUnarySearchOperator<int[]> {
// end relevant
/** the indexes */
private final int[] m_indexes;
final int[] m_indexes;

/**
* create the representation
Expand Down Expand Up @@ -127,13 +127,13 @@ public final boolean enumerate(final Random random,
// start relevant
System.arraycopy(x, 0, dest, 0, dest.length); // copy x

// We move along the index-pair array and shuffle the indices
// on the way
// We move along the index-pair array and shuffle the indices on
// the way
for (int i = 0, start = -1; i < pairCount; i++) {

// Get "a" and "b": the next, randomly chosen index pair.
// What we do here is basically an iterative version of the
// Fisher-Yates shuffle.
// Get "a" and "b": the next, randomly chosen index pair.
// What we do here is basically an iterative version of the
// Fisher-Yates shuffle.
int swapWith = (i + random.nextInt(pairCount - i)) << 1;
final int a = indexes[swapWith];
indexes[swapWith] = indexes[++start];
Expand All @@ -146,8 +146,8 @@ public final boolean enumerate(final Random random,
final int job_j = dest[b];// the job at second index

if (job_i != job_j) {
dest[b] = job_j; // then we swap the values
dest[a] = job_i; // and will then call the visitor
dest[a] = job_j; // then we swap the values
dest[b] = job_i; // and will then call the visitor
if (visitor.test(dest)) {
return true; // visitor says: stop -> return true
} // visitor did not say stop, so we need to
Expand Down
184 changes: 184 additions & 0 deletions src/test/java/aitoa/examples/jssp/TestJSSPUnaryOperator1SwapU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package aitoa.examples.jssp;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

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

import aitoa.structure.ISpace;
import aitoa.structure.IUnarySearchOperator;
import aitoa.structure.IUnarySearchOperatorTest;

/** test the unary 1-swap search operator */
public class TestJSSPUnaryOperator1SwapU
extends IUnarySearchOperatorTest<int[]> {

/** the space we use */
private static final JSSPInstance PROBLEM =
new JSSPInstance("yn2"); //$NON-NLS-1$

/** the space we use */
private static final JSSPSearchSpace SPACE =
new JSSPSearchSpace(TestJSSPUnaryOperator1SwapU.PROBLEM);

/** the operator we use */
private static final JSSPUnaryOperator1SwapU OP =
new JSSPUnaryOperator1SwapU(
TestJSSPUnaryOperator1SwapU.PROBLEM);

/** {@inheritDoc} */
@Override
protected ISpace<int[]> getSpace() {
return TestJSSPUnaryOperator1SwapU.SPACE;
}

/** {@inheritDoc} */
@Override
protected IUnarySearchOperator<int[]>
getOperator(final ISpace<int[]> space) {
return TestJSSPUnaryOperator1SwapU.OP;
}

/** {@inheritDoc} */
@Override
protected boolean equals(final int[] a, final int[] b) {
return Arrays.equals(a, b);
}

/** {@inheritDoc} */
@Override
protected int[] createValid() {
return JSSPTestUtils
.createValidX(TestJSSPUnaryOperator1SwapU.PROBLEM);
}

/**
* check that all index pairs are unique
*
* @param op
* the operator
* @param test
* the test set
*/
private static final void __checkUnique(
final JSSPUnaryOperator1SwapU op,
final HashSet<Long> test) {
test.clear();
for (int k = 0; k < op.m_indexes.length;) {
final long v = op.m_indexes[k++];
final long w = op.m_indexes[k++];
final long key =
(v < w) ? ((v << 32L) | w) : ((w << 32L) | v);
Assert.assertTrue(test.add(Long.valueOf(key)));
}
}

/** test the application to the canonical instance */
@SuppressWarnings("static-method")
@Test(timeout = 3600000)
public final void testCanonical() {
final Random random = ThreadLocalRandom.current();
new HashSet<>();
for (final JSSPInstance inst : JSSPTestUtils.INSTANCS) {
final JSSPUnaryOperator1SwapU op =
new JSSPUnaryOperator1SwapU(inst);
TestJSSPUnaryOperator1SwapU.__checkUnique(op,
new HashSet<>());
final int[] x = new int[inst.m * inst.n];
final int[] c = new int[inst.m * inst.n];
JSSPTestUtils.canonicalX(c, inst);
final int[] c2 = c.clone();
JSSPTestUtils.assertX(c2, inst);
for (int i = 1000; (--i) >= 0;) {
op.apply(c, x, random);
JSSPTestUtils.assertX(x, inst);
Assert.assertArrayEquals(c, c2);
}
}
}

/** test the application to random instances */
@SuppressWarnings("static-method")
@Test(timeout = 3600000)
public final void testRandom() {
final Random random = ThreadLocalRandom.current();
for (final JSSPInstance inst : JSSPTestUtils.INSTANCS) {
final int[] x = new int[inst.m * inst.n];
final int[] c = new int[inst.m * inst.n];
final JSSPUnaryOperator1SwapU op =
new JSSPUnaryOperator1SwapU(inst);
TestJSSPUnaryOperator1SwapU.__checkUnique(op,
new HashSet<>());

for (int i = 1000; (--i) >= 0;) {
JSSPTestUtils.randomX(c, inst);
JSSPTestUtils.assertX(c, inst);
op.apply(c, x, random);
JSSPTestUtils.assertX(x, inst);
}
}
}

/** test the number of unique outcomes */
@SuppressWarnings("static-method")
@Test(timeout = 3600000)
public void testNumberOfUniqueOutcomes() {
final Random random = ThreadLocalRandom.current();
final JSSPInstance inst = new JSSPInstance("demo"); //$NON-NLS-1$
final int[] in = new int[inst.n * inst.m];
final int[] out = new int[in.length];
final int[][] unique = new int[(inst.n * inst.m
* (inst.n - 1) * inst.m) >>> 1][out.length];

new JSSPNullaryOperator(inst).apply(in, random);
final JSSPUnaryOperator1SwapU op =
new JSSPUnaryOperator1SwapU(inst);
int count = 0;
outer: for (int i = (50 * unique.length * unique.length);
(--i) >= 0;) {
op.apply(in, out, random);
for (int j = count; (--j) >= 0;) {
if (Arrays.equals(unique[j], out)) {
continue outer;
}
}
System.arraycopy(out, 0, unique[count++], 0, out.length);
}

Assert.assertEquals(unique.length, count);
}

/**
* test that the
* {@link IUnarySearchOperator#enumerate(java.util.Random, Object, Object, java.util.function.Predicate)}
* method works correctly and respects the return values of the
* visitor
*/
@SuppressWarnings({ "static-method", "unchecked" })
@Test(timeout = 3600000)
public void testEnumerate2() {
final JSSPSearchSpace space =
TestJSSPUnaryOperator1SwapU.SPACE;

final JSSPUnaryOperator1SwapU op =
TestJSSPUnaryOperator1SwapU.OP;
final Random random = ThreadLocalRandom.current();

final int[] src = this.createValid();
final int[] dest = space.create();

final int[] copy = space.create();
space.copy(src, copy);

for (int i = 10; (--i) >= 0;) {
Assert.assertFalse(
op.enumerate(random, src, dest, (x) -> false));
final HashSet<Long> set = new HashSet<>();
TestJSSPUnaryOperator1SwapU.__checkUnique(op, set);
Assert.assertTrue(this.equals(src, copy));
}
}
}

0 comments on commit 2a1d435

Please sign in to comment.