Skip to content

Commit

Permalink
Update to MOEAFramework 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Apr 6, 2024
1 parent 3c27db8 commit 224dad5
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 90 deletions.
4 changes: 3 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
Expand All @@ -22,9 +23,10 @@
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add the following dependency to your `pom.xml`:
<dependency>
<groupId>org.moeaframework</groupId>
<artifactId>pisa-plugin</artifactId>
<version>1.0.3</version>
<version>2.0.0</version>
</dependency>
```

Expand All @@ -27,9 +27,10 @@ MOEA Framework, use the table below to identify which version of this plugin to

MOEA Framework Version | Compatible PISA-Plugin Version
---------------------- | ------------------------------
**`>= 3.8`** | **`1.0.3`**
`3.7` | `1.0.2`
`<= 3.6` | Not available
**`>= 4.0`** | **`2.0.0`**
`>= 3.8` | `1.0.3`
`3.7` | `1.0.2`
`<= 3.6` | Not available

## Usage

Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.moeaframework</groupId>
<artifactId>pisa-plugin</artifactId>
<version>1.0.3</version>
<version>2.0.0</version>
<packaging>jar</packaging>

<name>PISA Plugin for the MOEA Framework</name>
Expand Down Expand Up @@ -31,15 +31,15 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.moeaframework</groupId>
<artifactId>moeaframework</artifactId>
<version>3.8</version>
<version>4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
74 changes: 30 additions & 44 deletions src/main/java/org/moeaframework/algorithm/pisa/PISAAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.text.StringTokenizer;
import org.moeaframework.algorithm.AbstractAlgorithm;
import org.moeaframework.algorithm.AlgorithmException;
import org.moeaframework.algorithm.pisa.installer.PISAInstaller;
Expand All @@ -41,32 +42,25 @@
import org.moeaframework.core.NondominatedPopulation;
import org.moeaframework.core.PRNG;
import org.moeaframework.core.Problem;
import org.moeaframework.core.Settings;
import org.moeaframework.core.Solution;
import org.moeaframework.core.Variation;
import org.moeaframework.core.operator.RandomInitialization;
import org.moeaframework.core.initialization.RandomInitialization;
import org.moeaframework.util.TypedProperties;
import org.moeaframework.util.io.FileUtils;
import org.moeaframework.util.io.RedirectStream;

/**
* Algorithm for interfacing with an external PISA selector. The PISA
* framework is a platform and programming language independent interface for
* search algorithms. PISA separates search algorithms into <em>selector</em>s,
* describing the optimization algorithm, and <em>variator</em>s, describing
* the optimization problem. PISA uses a file-based communication channel
* between selectors and variators, which may result in excessive
* communication costs, file system bottlenecks and file name collisions.
* See the PISA homepage for detailed instructions.
* Algorithm for interfacing with an external PISA selector. The PISA framework is a platform and programming language
* independent interface for search algorithms. PISA separates search algorithms into <em>selector</em>s, describing
* the optimization algorithm, and <em>variator</em>s, describing the optimization problem. PISA uses a file-based
* communication channel between selectors and variators, which may result in excessive communication costs, file
* system bottlenecks and file name collisions. See the PISA homepage for detailed instructions.
* <p>
* Note that some PISA selectors parse the command line arguments using sscanf
* Note that some PISA selectors parse the command line arguments using sscanf:
* <pre>
* sscanf(argv[2], "%s", filenamebase);
* </pre>
* On some operating systems, this will not work if the files used by PISA
* contain whitespace in the filename. It may be necessary to set the JVM
* property {@code java.io.tmpdir} to a folder with no whitespace in the
* filename.
* On some operating systems, this will not work if the files used by PISA contain whitespace in the filename. It may
* be necessary to set the JVM property {@code java.io.tmpdir} to a folder with no whitespace in the filename.
*
* @see <a href="http://sop.tik.ee.ethz.ch/pisa/">PISA Homepage</a>
*/
Expand Down Expand Up @@ -189,7 +183,7 @@ public PISAAlgorithm(String name, Problem problem, Variation variation, TypedPro

//construct the command line call to start the PISA selector
selector = new ProcessBuilder(ArrayUtils.addAll(
Settings.parseCommand(command),
parseCommand(command),
configuration,
filePrefix,
Double.toString(pollRate/(double)1000)));
Expand Down Expand Up @@ -235,8 +229,7 @@ protected void initialize() {
public void terminate() {
super.terminate();

//guard against attempting to access the non-existent state file if
//this algorithm is not yet initialized
//guard against attempting to access the non-existent state file if this algorithm is not yet initialized
if (!isInitialized()) {
return;
}
Expand Down Expand Up @@ -293,10 +286,14 @@ public NondominatedPopulation getResult() {
result.addAll(solutions.values());
return result;
}

private String[] parseCommand(String command) {
return new StringTokenizer(command).setQuoteChar('\"').getTokenArray();
}

/**
* Clears the specified file. Some selector implementations may block until
* the {@code sel} and {@code arc} files are cleared.
* Clears the specified file. Some selector implementations may block until the {@code sel} and {@code arc} files
* are cleared.
*
* @param file the file to clear
* @throws IOException if an I/O error occurred
Expand All @@ -308,8 +305,7 @@ private void clearFile(File file) throws IOException {
}

/**
* Updates the population, retaining only those solutions with the
* specified identifiers.
* Updates the population, retaining only those solutions with the specified identifiers.
*
* @param ids the identifiers to retain
*/
Expand All @@ -324,8 +320,7 @@ private void updatePopulation(int[] ids) {
}

/**
* Adds the specified solution to the population, returning its assigned
* identifier.
* Adds the specified solution to the population, returning its assigned identifier.
*
* @param solution the solution
* @return the assigned identifier for the solution
Expand Down Expand Up @@ -378,11 +373,6 @@ private void state0() throws IOException {
*/
private void state4() throws IOException {
int[] archivedIds = readList(new File(filePrefix + "arc"));

// if (archivedIds.length != alpha) {
// throw new IOException("invalid archive length");
// }

updatePopulation(archivedIds);
}

Expand Down Expand Up @@ -430,8 +420,7 @@ private void state2() throws IOException {
}

/**
* Reads either the {@code sel} or {@code arc} files, returning the list
* of identifiers contained in the file.
* Reads either the {@code sel} or {@code arc} files, returning the list of identifiers contained in the file.
*
* @param file the {@code sel} or {@code arc} file
* @return the list of identifiers contained in the file
Expand Down Expand Up @@ -470,8 +459,7 @@ private int[] readList(File file) throws IOException {
}

/**
* Writes either the {@code ini} or {@code var} file with the specified
* identifiers.
* Writes either the {@code ini} or {@code var} file with the specified identifiers.
*
* @param file the {@code ini} or {@code var} file
* @param ids the identifiers of solutions written to the file
Expand All @@ -497,20 +485,18 @@ private void writePopulation(File file, int[] ids) throws IOException {
}

/**
* Removes any existing PISA communication files, creating a new {@code cfg}
* file with the appropriate settings.
* Removes any existing PISA communication files, creating a new {@code cfg} file with the appropriate settings.
*
* @throws IOException if an I/O error occurred
*/
private void configure() throws IOException {
FileUtils.delete(new File(filePrefix + "arc"));
FileUtils.delete(new File(filePrefix + "cfg"));
FileUtils.delete(new File(filePrefix + "ini"));
FileUtils.delete(new File(filePrefix + "sel"));
FileUtils.delete(new File(filePrefix + "sta"));

try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(
new File(filePrefix + "cfg"))))) {
new File(filePrefix + "arc").delete();
new File(filePrefix + "cfg").delete();
new File(filePrefix + "ini").delete();
new File(filePrefix + "sel").delete();
new File(filePrefix + "sta").delete();

try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(new File(filePrefix + "cfg"))))) {
writer.print("alpha ");
writer.println(alpha);
writer.print("mu ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static boolean getPISAAllowInstall() {
}

/**
* Returns the path to install the PISA binaries. If relative, will be created relative to the
* working directory when running Java.
* Returns the path to install the PISA binaries. If relative, will be created relative to the working directory
* when running Java.
*
* @return the path to install the PISA binaries
*/
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/moeaframework/algorithm/pisa/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public class State {
private static final long pollRate = PISASettings.getPISAPollRate();

/**
* The number of times this class will attempt to write to the state file
* until propagating the error. Failures primarily result from PISA
* selectors locking the state file.
* The number of times this class will attempt to write to the state file until propagating the error. Failures
* primarily result from PISA selectors locking the state file.
*/
private static final int numberOfRetries = 5;

Expand Down Expand Up @@ -79,8 +78,7 @@ public int get() throws IOException {
*
* @param state the state
* @throws IOException if an I/O error occurred
* @throws InterruptedException if {@link Thread#sleep(long)} was
* interrupted
* @throws InterruptedException if {@link Thread#sleep(long)} was interrupted
*/
public void set(int state) throws IOException, InterruptedException {
int retriesRemaining = numberOfRetries;
Expand All @@ -106,8 +104,7 @@ public void set(int state) throws IOException, InterruptedException {
*
* @param state the state to wait for
* @throws IOException if an I/O error occurred
* @throws InterruptedException if {@link Thread#sleep(long)} was
* interrupted
* @throws InterruptedException if {@link Thread#sleep(long)} was interrupted
*/
public void waitFor(int state) throws IOException, InterruptedException {
while (!file.exists() || (get() != state)) {
Expand All @@ -116,14 +113,12 @@ public void waitFor(int state) throws IOException, InterruptedException {
}

/**
* Blocks while the state remains at the specified value, returning the
* new state value when it changes.
* Blocks while the state remains at the specified value, returning the new state value when it changes.
*
* @param state the state to wait on
* @return the new state
* @throws IOException if an I/O error occurred
* @throws InterruptedException if {@link Thread#sleep(long)} was
* interrupted
* @throws InterruptedException if {@link Thread#sleep(long)} was interrupted
*/
public int waitWhile(int state) throws IOException, InterruptedException {
int current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +38,7 @@ public abstract class AbstractPISAInstaller implements PISAInstaller {

protected void register(String algorithm, String remoteUrl) {
try {
files.put(getCanonicalName(algorithm), new URL(remoteUrl));
files.put(getCanonicalName(algorithm), URI.create(remoteUrl).toURL());
} catch (MalformedURLException e) {
throw new FrameworkException(e);
}
Expand Down Expand Up @@ -150,7 +151,8 @@ protected String[] buildExtractCommand(File zipFile, File destinationPath) {
return new String[] {
"pwsh",
"-Command",
"Expand-Archive -Path \"" + zipFile.getAbsolutePath() + "\" -DestinationPath \"" + destinationPath.getAbsolutePath() + "\" -Force"
"Expand-Archive -Path \"" + zipFile.getAbsolutePath() + "\" -DestinationPath \"" +
destinationPath.getAbsolutePath() + "\" -Force"
};
} else {
return new String[] {
Expand All @@ -160,7 +162,8 @@ protected String[] buildExtractCommand(File zipFile, File destinationPath) {
destinationPath.getAbsolutePath()
};
}
} else if (extension.equalsIgnoreCase("tar") || extension.equalsIgnoreCase("gz") || extension.equalsIgnoreCase("tar.gz")) {
} else if (extension.equalsIgnoreCase("tar") || extension.equalsIgnoreCase("gz") ||
extension.equalsIgnoreCase("tar.gz")) {
return new String[] {
"tar",
"-x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.moeaframework.util.io.RedirectStream;

/**
* Downloads, compiles, and configures the PISA selector from source code. This is experimental and may
* not work on all systems.
* Downloads, compiles, and configures the PISA selector from source code. This is experimental and may not work on
* all systems.
*/
public class SourceInstaller extends AbstractPISAInstaller {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -32,11 +34,9 @@
import org.moeaframework.core.spi.ProblemFactory;
import org.moeaframework.core.spi.ProviderNotFoundException;
import org.moeaframework.util.TypedProperties;
import org.moeaframework.util.io.FileUtils;

/**
* Tests the {@link PISAAlgorithms} class using the old, static configuration
* files.
* Tests the {@link PISAAlgorithms} class using the old, static configuration files.
*/
public class PISAAlgorithmsStaticConfigurationTest {

Expand Down Expand Up @@ -136,13 +136,11 @@ public void testIBEA() throws IOException {
@Test
@Ignore("need to make design file an argument, otherwise can't parallelize")
public void testMSOPS() throws IOException {
FileUtils.copy(
new File("./pisa/msops_win/msops_weights/" +
properties.getString("populationSize", "") +
"/space-filling-" +
realProblem.getNumberOfObjectives() + "dim.des"),
new File("space-filling-" +
realProblem.getNumberOfObjectives() + "dim.des"));
Files.copy(new File("./pisa/msops_win/msops_weights/" + properties.getString("populationSize", "") +
"/space-filling-" + realProblem.getNumberOfObjectives() + "dim.des").toPath(),
new File("space-filling-" + realProblem.getNumberOfObjectives() + "dim.des").toPath(),
StandardCopyOption.REPLACE_EXISTING);

run("msops", realProblem);
}

Expand Down
Loading

0 comments on commit 224dad5

Please sign in to comment.