Class ExternalProblem
- All Implemented Interfaces:
AutoCloseable
,Problem
Standard I/O Mode
Standard I/O is the easiest mode to setup and run. First, an executable program on the computer is launched by invoking the constructor with the program name (and any optional arguments):new ExternalProblem("./problem.exe", "arg1", "arg2") { ... }Then, solutions are sent to the process on its standard input (stdin) stream, and the objectives and constraints are read from its standard output (stdout) stream. Writing or reading any other content from these streams will interfere with the communication. Consider using sockets, as discussed below, if the program already uses standard input / output for other purposes.
Socket Mode
Socket mode is more complicated to setup, but is more flexible and robust. It has the ability to not only evaluate the problem on the host computer, but can be spread across a computer network. To use sockets, use either theExternalProblem(String, int)
or ExternalProblem(InetAddress, int)
constructor.
C/C++ Interface
A C/C++ interface is provided for implementing problems. This interface supports both modes of communication, depending on which initialization routine is invoked. See themoeaframework.c
and moeaframework.h
files in the examples/
folder for details. This interface conforms to the communication protocol described
below.
The communication protocol consists of sending decision variables to the external process, and the process responding with the objectives and constraints. First, the MOEA Framework writes a line containing the decision variables separated by whitespace and terminated by the newline character. The program should read this line from stdin, then write the objectives and constraints, if any, to stdout. The objectives and constraints must also appear on a single line separated by whitespace.
The program must continue processing lines until the input stream is closed. In addition, it should always flush the output stream after writing each line. Otherwise, some systems may buffer the data causing the program to stall.
Whitespace is one or more spaces, tabs or any combination thereof. The newline is either the line feed ('\n'), carriage return ('\r') or a carriage return followed immediately by a line feed ("\r\n").
It is critical that the close()
method be invoked to ensure the external process is shutdown cleanly.
Failure to do so could leave the process running in the background.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default port used by the MOEA Framework to connect to remote evaluation processes via sockets. -
Constructor Summary
ModifierConstructorDescriptionprotected
ExternalProblem
(InputStream input, OutputStream output) Constructs an external problem using the specified input and output streams.protected
ExternalProblem
(Process process) Constructs an external problem using the specified process.ExternalProblem
(String... command) Constructs an external problem usingnew ProcessBuilder(command).start()
.ExternalProblem
(String host, int port) Constructs an external problem that connects to a remote process via sockets.ExternalProblem
(InetAddress address, int port) Constructs an external problem that connects to a remote process via sockets.protected
ExternalProblem
(Socket socket) Constructs an external problem using the specified socket. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the connection to the process.void
Evaluates the specified solution using the process defined by this class' constructor.void
setDebugStream
(OutputStream stream) Sets the output stream used to write debugging information.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.moeaframework.core.Problem
getName, getNumberOfConstraints, getNumberOfObjectives, getNumberOfVariables, isType, newSolution
-
Field Details
-
DEFAULT_PORT
public static final int DEFAULT_PORTThe default port used by the MOEA Framework to connect to remote evaluation processes via sockets.- See Also:
-
-
Constructor Details
-
ExternalProblem
Constructs an external problem usingnew ProcessBuilder(command).start()
. If the command contains arguments, the arguments should be passed in as separate strings, such asnew ExternalProblem("command", "arg1", "arg2");
- Parameters:
command
- a specified system command- Throws:
IOException
- if an I/O error occured
-
ExternalProblem
Constructs an external problem that connects to a remote process via sockets. The remote process should be instantiated and already listening to the designated port number prior to invoking this constructor.- Parameters:
host
- the host name of the remote system; ornull
to use the local hostport
- the port number- Throws:
UnknownHostException
- if the IP address of the specified host could not be determinedIOException
- if an I/O error occurred
-
ExternalProblem
Constructs an external problem that connects to a remote process via sockets. The remote process should be instantiated and already listening to the designated port number prior to invoking this constructor.- Parameters:
address
- the IP address of the remote systemport
- the port number- Throws:
IOException
- if an I/O error occurred
-
ExternalProblem
Constructs an external problem using the specified socket.- Parameters:
socket
- the socket used to send solutions to be evaluated- Throws:
IOException
- if an I/O error occurred
-
ExternalProblem
Constructs an external problem using the specified process.- Parameters:
process
- the process used to evaluate solutions
-
ExternalProblem
Constructs an external problem using the specified input and output streams.- Parameters:
input
- the input streamoutput
- the output stream
-
-
Method Details
-
setDebugStream
Sets the output stream used to write debugging information. Ifnull
, disables debugging. The debug stream is not closed by this class and must be managed by the caller.- Parameters:
stream
- the output stream
-
close
public void close()Closes the connection to the process. No further invocations ofevaluate
are permitted.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceProblem
-
evaluate
Evaluates the specified solution using the process defined by this class' constructor.- Specified by:
evaluate
in interfaceProblem
- Parameters:
solution
- the solution to evaluate- Throws:
ProblemException
-