Skip to content

Commit

Permalink
unification of random seed / string conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Dec 22, 2019
1 parent 82e2aa9 commit 1645518
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,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.16` is the current version of `aitoa-code`.
Here, `0.8.17` 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.16</version>
<version>0.8.17</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.16</version>
<version>0.8.17</version>
<packaging>jar</packaging>
<name>aitoa-code</name>
<description>Example Source Codes from the Book "Introduction to Optimization Algorithms"</description>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/aitoa/structure/_BlackBoxProcessData.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ void _printInfos(final Appendable out) throws IOException {
LogFormat.doubleToStringForLog(this.m_goalF)));
out.append(System.lineSeparator());
out.append(LogFormat.mapEntry(LogFormat.RANDOM_SEED,
LogFormat.RANDOM_SEED_PREFIX
+ RandomUtils.randSeedToString(this.m_randSeed)));
RandomUtils.randSeedToString(this.m_randSeed)));
out.append(System.lineSeparator());
out.append(LogFormat.asComment(LogFormat.END_SETUP));
out.append(System.lineSeparator());
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/aitoa/utils/RandomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Arrays;
import java.util.Random;

import aitoa.structure.LogFormat;

/** Utilities for random number generation */
public final class RandomUtils {

Expand Down Expand Up @@ -152,15 +154,46 @@ public final class RandomUtils {
* @return the string
*/
public static final String randSeedToString(final long seed) {
final char[] str = new char[16];
final int l = LogFormat.RANDOM_SEED_PREFIX.length();
final char[] str = new char[l + 16];
LogFormat.RANDOM_SEED_PREFIX.getChars(0, l, str, 0);
long cmp = seed;
for (int i = 16; (--i) >= 0;) {
for (int i = (l + 16); (--i) >= l;) {
str[i] = RandomUtils.CHOOSE[(int) (cmp & 0xfL)];
cmp >>>= 4L;
}
return String.valueOf(str);
}

/**
* Parse a random seed string to a {@code long}
*
* @param randSeedString
* the random seed string
* @return the {@code long} representing the string
*/
public static final long
stringToRandSeed(final String randSeedString) {
if ((!randSeedString
.startsWith(LogFormat.RANDOM_SEED_PREFIX))
|| (randSeedString.length() < 3)) {
throw new IllegalArgumentException(
"Random seed must start with '" //$NON-NLS-1$
+ LogFormat.RANDOM_SEED_PREFIX
+ "' and contain at least one hexadecimal digit, but is "//$NON-NLS-1$
+ randSeedString);
}
try {
return Long.parseUnsignedLong(randSeedString
.substring(LogFormat.RANDOM_SEED_PREFIX.length()), 16);
} catch (final NumberFormatException nfe) {
throw new IllegalArgumentException(
"Invalid random seed: " + //$NON-NLS-1$
randSeedString,
nfe);
}
}

/**
* Randomize a sub-sequence of an array or permutation of
* {@code java.lang.Objects}. After this procedure, the
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/aitoa/utils/logs/EndResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

import aitoa.structure.LogFormat;
import aitoa.utils.RandomUtils;

/**
* A line of the end results table, as created by
Expand Down Expand Up @@ -94,21 +94,7 @@ public EndResult(final String _algorithm,
}

this.seed = _seed.trim();
if (this.seed.isEmpty() || (!this.seed
.startsWith(LogFormat.RANDOM_SEED_PREFIX))) {
throw new IllegalArgumentException(
"Cannot have '" + _seed + //$NON-NLS-1$
"' as random seed.");//$NON-NLS-1$
}
try {
this.seedVal = Long.parseUnsignedLong(this.seed
.substring(LogFormat.RANDOM_SEED_PREFIX.length()), 16);
} catch (final Throwable error) {
throw new IllegalArgumentException(
"Invalid random seed: '" + _seed + //$NON-NLS-1$
"'.", //$NON-NLS-1$
error);
}
this.seedVal = RandomUtils.stringToRandSeed(this.seed);

this.bestF = _bestF;
if (!Double.isFinite(this.bestF)) {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/aitoa/utils/logs/EndResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import aitoa.utils.Configuration;
import aitoa.utils.ConsoleIO;
import aitoa.utils.IOUtils;
import aitoa.utils.RandomUtils;

/**
* This class allows you to create a (potentially large) csv
Expand Down Expand Up @@ -375,20 +376,7 @@ public static final void parseEndResultsTable(final Path path,
nextSemi = line.indexOf(LogFormat.CSV_SEPARATOR_CHAR, //
++lastSemi);
seed = line.substring(lastSemi, nextSemi).trim();
if ((seed.length() < 3) || (!seed
.startsWith(LogFormat.RANDOM_SEED_PREFIX))) {
throw new IllegalArgumentException(
"Random seed invalid, must start with " + //$NON-NLS-1$
LogFormat.RANDOM_SEED_PREFIX);
}
try {
Long.parseUnsignedLong(seed.substring(2), 16);
} catch (final Throwable error2) {
throw new IllegalArgumentException(
"Invalid random seed: '" + seed + //$NON-NLS-1$
"'.", //$NON-NLS-1$
error2);
}
RandomUtils.stringToRandSeed(seed);
lastSemi = nextSemi;

nextSemi = line.indexOf(LogFormat.CSV_SEPARATOR_CHAR, //
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/aitoa/utils/logs/LogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import aitoa.structure.LogFormat;
import aitoa.utils.IOUtils;
import aitoa.utils.RandomUtils;

/**
* This class allows for efficient parsing of the log files
Expand Down Expand Up @@ -394,17 +395,8 @@ public static final void parseLogFile(final Path file,
}
case LogFormat.RANDOM_SEED: {
randSeedString = value;
if ((!value.startsWith(
LogFormat.RANDOM_SEED_PREFIX))
|| (value.length() < 3)) {
throw new IllegalArgumentException(
"Random seed must start with '" //$NON-NLS-1$
+ LogFormat.RANDOM_SEED_PREFIX
+ "' and contain at least one hexadecimal digit, but is "//$NON-NLS-1$
+ value);
}
randSeedLong = Long.parseUnsignedLong(
value.substring(2), 16);
randSeedLong = RandomUtils
.stringToRandSeed(randSeedString);
break;
}
default: {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/aitoa/utils/logs/SetupData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Objects;
import java.util.TreeMap;

import aitoa.structure.LogFormat;
import aitoa.utils.RandomUtils;

/** The setup information from a log file */
public final class SetupData implements Comparable<SetupData> {
Expand Down Expand Up @@ -51,8 +51,8 @@ public final class SetupData implements Comparable<SetupData> {
this.randSeedLong = _randSeedLong;

try {
final long l = Long.parseUnsignedLong(this.randSeedString
.substring(LogFormat.RANDOM_SEED_PREFIX.length()), 16);
final long l =
RandomUtils.stringToRandSeed(this.randSeedString);
if (l != this.randSeedLong) {
throw new IllegalStateException("Rand seed string (" + //$NON-NLS-1$
this.randSeedString + '=' + l
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/aitoa/utils/ExperimentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void testLogFile() throws IOException {
Assert.assertTrue(Files.exists(test));
Assert.assertTrue(Files.isRegularFile(test));
Assert.assertEquals(test.getFileName().toString(),
"2d3_sdfsfdg_0000000000000001.txt");//$NON-NLS-1$
"2d3_sdfsfdg_0x0000000000000001.txt");//$NON-NLS-1$

final Path inst = test.getParent();
Assert.assertTrue(Files.exists(inst));
Expand Down

0 comments on commit 1645518

Please sign in to comment.