From a627e791f8d119e5a4b9120fcbf84a6c919b8bc1 Mon Sep 17 00:00:00 2001 From: Thomas Weise Date: Sat, 31 Oct 2020 15:48:31 +0800 Subject: [PATCH] added list of available JSSP instances to JSSPInstance --- README.md | 4 +- pom.xml | 2 +- .../aitoa/examples/jssp/JSSPInstance.java | 62 +++++++++++++++++++ .../aitoa/examples/jssp/TestJSSPInstance.java | 18 ++++++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cfa870c..d7a5e03 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ 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.72` is the current version of `aitoa-code`. +Here, `0.8.73` 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 @@ -52,7 +52,7 @@ Notice that you may have more dependencies in your `dependencies` section, say o com.github.thomasWeise aitoa-code - 0.8.72 + 0.8.73 ``` diff --git a/pom.xml b/pom.xml index 5da949e..abae9fa 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ aitoa aitoa-code - 0.8.72 + 0.8.73 jar aitoa-code Example Source Codes from the Book "Introduction to Optimization Algorithms" diff --git a/src/main/java/aitoa/examples/jssp/JSSPInstance.java b/src/main/java/aitoa/examples/jssp/JSSPInstance.java index beead6b..6a80c4c 100644 --- a/src/main/java/aitoa/examples/jssp/JSSPInstance.java +++ b/src/main/java/aitoa/examples/jssp/JSSPInstance.java @@ -5,6 +5,10 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; import aitoa.utils.math.BigMath; @@ -264,6 +268,64 @@ public final double getScale() { .pow(this.m))); } + /** + * Get an unmodifiable list of the names of all JSSP instances. + * This list does not contain the {@code demo} instance. + * + * @return an unmodifiable list of the names of all JSSP + * instances + */ + public static final List getAllInstances() { + return Holder.NAMES; + } + + /** the internal holder class */ + private static final class Holder { + /** the names of all instances */ + static final List NAMES = Holder.loadNames(); + + /** + * the internal names loader + * + * @return the array of strings + */ + private static final List loadNames() { + final HashSet names = new HashSet<>(); + try ( + final InputStream is = JSSPInstance.class + .getResourceAsStream("instance_data.txt"); //$NON-NLS-1$ + final InputStreamReader isr = + new InputStreamReader(is); + final BufferedReader br = new BufferedReader(isr)) { + String line = null; + + loop: while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) { + continue loop; + } + if (line.startsWith("instance ")) { //$NON-NLS-1$ + line = line.substring(9).trim(); + if (line.isEmpty()) { + continue loop; + } + if (line.indexOf(' ') >= 0) { + continue loop; + } + names.add(line); + } + } + } catch (final Throwable error) { + throw new IllegalStateException( + "Could not load instances text.", //$NON-NLS-1$ + error); + } + final String[] all = + names.toArray(new String[names.size()]); + Arrays.sort(all); + return Collections.unmodifiableList(Arrays.asList(all)); + } + } // start relevant } // end relevant diff --git a/src/test/java/aitoa/examples/jssp/TestJSSPInstance.java b/src/test/java/aitoa/examples/jssp/TestJSSPInstance.java index b894168..0569761 100644 --- a/src/test/java/aitoa/examples/jssp/TestJSSPInstance.java +++ b/src/test/java/aitoa/examples/jssp/TestJSSPInstance.java @@ -1,5 +1,7 @@ package aitoa.examples.jssp; +import java.util.Collection; + import org.junit.Assert; import org.junit.Test; @@ -370,4 +372,20 @@ public void testLoadAllInstances() { TestTools.assertGreater(f.lowerBound(), 0d); } } + + /** test all instances */ + @SuppressWarnings({ "static-method", "unused" }) + @Test(timeout = 3600000) + public void testAllInstances() { + final Collection all = + JSSPInstance.getAllInstances(); + Assert.assertNotNull(all); + int size = all.size(); + TestTools.assertGreater(size, 0); + for (final String n : all) { + new JSSPInstance(n); + --size; + } + Assert.assertEquals(0, size); + } }