Class ExternalProblem

java.lang.Object
org.moeaframework.problem.ExternalProblem
All Implemented Interfaces:
AutoCloseable, Problem

public abstract class ExternalProblem extends Object implements 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 the Variable.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 provide moeaframework.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.

  • Field Details

    • DEFAULT_PORT

      public static final int DEFAULT_PORT
      The default port used by the MOEA Framework to connect to remote evaluation processes via sockets.
      See Also:
    • instance

      protected final ExternalProblem.Instance instance
      The instance backing this external problem, which manages the underlying resources including the process, socket, and streams.
  • Constructor Details

    • ExternalProblem

      @Deprecated public ExternalProblem(String... command) throws IOException
      Deprecated.
      Use the ExternalProblem(Builder) constructor
      Constructs an external problem using new ProcessBuilder(command).start(). If the command contains arguments, the arguments should be passed in as separate strings, such as
         new ExternalProblem("command", "arg1", "arg2");
       
      Parameters:
      command - a specified system command
      Throws:
      IOException - if an I/O error occured
    • ExternalProblem

      @Deprecated public ExternalProblem(String host, int port) throws IOException, UnknownHostException
      Deprecated.
      Use the ExternalProblem(Builder) constructor
      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; or null to use the local host
      port - the port number
      Throws:
      UnknownHostException - if the IP address of the specified host could not be determined
      IOException - if an I/O error occurred
    • ExternalProblem

      @Deprecated public ExternalProblem(InetAddress address, int port) throws IOException
      Deprecated.
      Use the ExternalProblem(Builder) constructor
      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 system
      port - the port number
      Throws:
      IOException - if an I/O error occurred
    • ExternalProblem

      public ExternalProblem(ExternalProblem.Builder builder)
      Constructs an external problem using the specified builder.
      Parameters:
      builder - the builder that defines the process and/or socket address
    • ExternalProblem

      @Deprecated protected ExternalProblem(Process process)
      Deprecated.
      Use the ExternalProblem(Builder) constructor
      Constructs an external problem using the specified process.
      Parameters:
      process - the process used to evaluate solutions
    • ExternalProblem

      @Deprecated protected ExternalProblem(InputStream input, OutputStream output)
      Deprecated.
      Use the ExternalProblem(Builder) constructor
      Constructs an external problem using the specified input and output streams.
      Parameters:
      input - the input stream
      output - the output stream
  • Method Details

    • setDebugStream

      @Deprecated public void setDebugStream(OutputStream stream)
      Deprecated.
      Enable debugging by calling ExternalProblem.Builder.withDebugging()
      Sets the output stream used to write debugging information. If null, 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 of evaluate are permitted.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Problem
    • evaluate

      public void evaluate(Solution solution) throws ProblemException
      Evaluates the specified solution using the process defined by this class' constructor.
      Specified by:
      evaluate in interface Problem
      Parameters:
      solution - the solution to evaluate
      Throws:
      ProblemException