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.

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 the ExternalProblem(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 the moeaframework.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 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:
  • Constructor Details

    • ExternalProblem

      public ExternalProblem(String... command) throws IOException
      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

      public ExternalProblem(String host, int port) throws IOException, UnknownHostException
      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

      public ExternalProblem(InetAddress address, int port) throws IOException
      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

      protected ExternalProblem(Socket socket) throws IOException
      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

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

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

    • setDebugStream

      public void setDebugStream(OutputStream stream)
      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