Skip to content

Commit

Permalink
Add Radar problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Dec 31, 2023
1 parent 1351b61 commit 630dda4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependency-reduced-pom.xml
*.so
*.exe
*.dll
*.lib

native/HBV/bin/hbv
native/LRGV/bin/lrgv
Expand All @@ -21,4 +22,5 @@ native/WDS/bin/NYT
native/WDS/bin/PES
native/WDS/bin/TLN
native/WDS/bin/TRN
native/WDS/bin/reportFile.rpt
native/WDS/bin/reportFile.rpt
native/Radar/bin/testpris.p
4 changes: 4 additions & 0 deletions moeaframework.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ org.moeaframework.analysis.diagnostics.problems = GAA, HBV, \
## The Radar Waveform Design problem requires Matlab. By default, the command
## 'matlab' is used, but a custom path can be specified below if required.
#matlab.path = /usr/global/matlab/R2013a/bin/matlab

## Time to wait, in milliseconds, after launching Matlab before attempting to
## establish a connection.
#matlab.sleep = 30000
21 changes: 15 additions & 6 deletions src/main/java/org/moeaframework/benchmarks/BenchmarkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@
package org.moeaframework.benchmarks;

import org.moeaframework.benchmarks.WDS.WDSInstance;
import org.moeaframework.core.FrameworkException;
import org.moeaframework.core.spi.RegisteredProblemProvider;

public class BenchmarkProvider extends RegisteredProblemProvider {

public BenchmarkProvider() {
super();

register("GAA", () -> new GAA(), "./pf/GAA.reference");
register("CarSideImpact", () -> new CarSideImpact(), "./pf/CarSideImpact.reference");
register("ElectricMotor", () -> new ElectricMotor(), "./pf/ElectricMotor.reference");
register("HBV", () -> new HBV(), "./pf/HBV.reference");
register("LRGV", () -> new LRGV(), null);
register("LakeProblem", () -> new LakeProblem(), "./pf/LakeProblem.reference");
register("GAA", GAA::new, "./pf/GAA.reference");
register("CarSideImpact", CarSideImpact::new, "./pf/CarSideImpact.reference");
register("ElectricMotor", ElectricMotor::new, "./pf/ElectricMotor.reference");
register("HBV", HBV::new, "./pf/HBV.reference");
register("LRGV", LRGV::new, null);
register("LakeProblem", LakeProblem::new, "./pf/LakeProblem.reference");

for (WDSInstance variant : WDSInstance.values()) {
register("WDS(" + variant.getName() + ")", () -> new WDS(variant), "./pf/WDS/" + variant.getName() + ".reference");
}

register("Radar", () -> {
try {
return new Radar();
} catch (Exception e) {
throw new FrameworkException("failed to start Radar problem", e);
}
}, null);
}

}
18 changes: 7 additions & 11 deletions src/main/java/org/moeaframework/benchmarks/Radar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.commons.lang3.SystemUtils;
import org.moeaframework.core.FrameworkException;
import org.moeaframework.core.PRNG;
import org.moeaframework.core.Settings;
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.ExternalProblem;
import org.moeaframework.problem.OsType;
import org.moeaframework.util.io.RedirectStream;

public class Radar extends ExternalProblem {

public static final String PATH = "./native/Radar/bin/";

public Radar() throws IOException {
public Radar() throws Exception {
super((String)null, startProcess());
}

public static int startProcess() throws IOException {
public static int startProcess() throws Exception {
int port = PRNG.nextInt(10000, 65536);
String command = Settings.PROPERTIES.contains("matlab.path") ?
Settings.PROPERTIES.getString("matlab.path", "matlab") :
SystemUtils.IS_OS_WINDOWS ? "matlab.exe" : "matlab";
String command = Settings.PROPERTIES.getString("matlab.path",
OsType.getOsType() == OsType.WINDOWS ? "matlab.exe" : "matlab");

validate();

Expand All @@ -36,11 +36,7 @@ public static int startProcess() throws IOException {
RedirectStream.redirect(process.getInputStream(), System.out);
RedirectStream.redirect(process.getErrorStream(), System.err);

try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// do nothing
}
Thread.sleep(Settings.PROPERTIES.getInt("matlab.sleep", 30000));

return port;
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/moeaframework/problem/NativeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class NativeCommand {
private final String[] arguments;

private final File workingDirectory;

public NativeCommand(String executableName, String[] arguments) {
this(executableName, arguments, null);
}

public NativeCommand(String executableName, String[] arguments, File workingDirectory) {
super();
Expand All @@ -38,13 +42,18 @@ public Process exec() throws IOException {

public ProcessBuilder toProcessBuilder(OsType osType) {
List<String> command = new ArrayList<String>();
String executableName = getExecutableName();

switch (osType) {
case WINDOWS:
command.add(new File(getWorkingDirectory(), getExecutableName() + ".exe").getPath());
if (!executableName.toLowerCase().endsWith(".exe")) {
executableName += ".exe";
}

command.add(new File(getWorkingDirectory(), executableName).getPath());
break;
case POSIX:
command.add(new File(".", getExecutableName()).getPath());
command.add(new File(".", executableName).getPath());
break;
default:
throw new IllegalArgumentException("Unsupported OS " + osType);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/moeaframework/problem/NativeProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public NativeCommand getCommand() {

private static final Process startProcessOrThrow(NativeCommand command) {
try {
return command.toProcessBuilder(OsType.getOsType()).start();
return command.exec();
} catch (IOException e) {
throw new FrameworkException("Failed to start native process", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
*/
package org.moeaframework.benchmarks;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.moeaframework.Executor;
import org.moeaframework.core.spi.ProblemFactory;
import org.moeaframework.problem.NativeCommand;

/**
* Tests to ensure each benchmark problem can be instantiated with the MOEA
Expand All @@ -42,6 +46,22 @@ protected void test(String problemName, boolean hasReferenceSet) {
}
}

protected void requires(NativeCommand command) {
try {
Process process = command.exec();
process.waitFor(15, TimeUnit.SECONDS);

if (process.isAlive()) {
process.destroy();
Assume.assumeTrue("Process did not terminate within configured timeout", false);
}

Assume.assumeTrue("Process exited with non-zero result code", process.exitValue() == 0);
} catch (Exception e) {
Assume.assumeNoException("Caught exception when invoking process", e);
}
}

@Test
public void testCarSideImpact() {
test("CarSideImpact", true);
Expand Down Expand Up @@ -73,8 +93,8 @@ public void testLRGV() {
}

@Test
@Ignore("requires matlab")
public void testRadar() {
requires(new NativeCommand("matlab", new String[] { "-h" }));
test("Radar", false);
}

Expand Down

0 comments on commit 630dda4

Please sign in to comment.