Package org.moeaframework.problem
Class ExternalProblem
java.lang.Object
org.moeaframework.problem.ExternalProblem
- All Implemented Interfaces:
AutoCloseable
,Problem
Evaluate solutions using an externally-defined problem. Two modes of operation are supported: standard I/O and
sockets.
Communication Protocol
Each solution is evaluated serially by writing the decision variables to the process or socket, waiting for a response, and reading the objectives and constraints. Values are formatted using theVariable.toString()
method and separated by whitespace, typically a single space character, and sent on a single line terminated by the
new line character (which depending on the platform can be "\n"
, "\r"
, or "\r\n"
). This
process repeats in a loop until all solutions are evaluated, at which point the stream is closed. We strongly
recommend flushing the output stream after writing each line to prevent buffering.
Standard I/O
When using Standard I/O, a process is started and the data is transmitted over the standard input/output streams. One limitation of this approach is the process can not use standard input/output for any other purpose, as that will interfere with the communication. Consider using sockets instead.Sockets
When using Sockets, data is transmitted over the network. Typically, this talks to a local process using a specific port. However, this can also connect to a remote process over a local-area network or the Internet.C/C++ Library
To assist in writing the function in a native language, we providemoeaframework.c
and
moeaframework.h
under the examples/
folder. This library handles setting up the connection using
either I/O or sockets, parsing decision variables, and writing the objectives and constraints. Furthermore, we can
use the BuildProblem
to generate a template using --language external
.
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder for defining the process or connection to the external problem.protected static class
Instance of the external problem. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default port used by the MOEA Framework to connect to remote evaluation processes via sockets.protected final ExternalProblem.Instance
The instance backing this external problem, which manages the underlying resources including the process, socket, and streams. -
Constructor Summary
ModifierConstructorDescriptionprotected
ExternalProblem
(InputStream input, OutputStream output) Deprecated.protected
ExternalProblem
(Process process) Deprecated.Use theExternalProblem(Builder)
constructorExternalProblem
(String... command) Deprecated.Use theExternalProblem(Builder)
constructorExternalProblem
(String host, int port) Deprecated.Use theExternalProblem(Builder)
constructorExternalProblem
(InetAddress address, int port) Deprecated.Use theExternalProblem(Builder)
constructorExternalProblem
(ExternalProblem.Builder builder) Constructs an external problem using the specified builder. -
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) Deprecated.Enable debugging by callingExternalProblem.Builder.withDebugging()
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:
-
instance
The instance backing this external problem, which manages the underlying resources including the process, socket, and streams.
-
-
Constructor Details
-
ExternalProblem
Deprecated.Use theExternalProblem(Builder)
constructorConstructs 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
Deprecated.Use theExternalProblem(Builder)
constructorConstructs 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
Deprecated.Use theExternalProblem(Builder)
constructorConstructs 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 builder.- Parameters:
builder
- the builder that defines the process and/or socket address
-
ExternalProblem
Deprecated.Use theExternalProblem(Builder)
constructorConstructs an external problem using the specified process.- Parameters:
process
- the process used to evaluate solutions
-
ExternalProblem
Deprecated.Use theExternalProblem(Builder)
constructorConstructs an external problem using the specified input and output streams.- Parameters:
input
- the input streamoutput
- the output stream
-
-
Method Details
-
setDebugStream
Deprecated.Enable debugging by callingExternalProblem.Builder.withDebugging()
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
-
ExternalProblem(Builder)
constructor