Class ResultFileWriter

java.lang.Object
org.moeaframework.analysis.io.ResultWriter
org.moeaframework.analysis.io.ResultFileWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public class ResultFileWriter extends ResultWriter
Writes result files. A result file contains one or more entries consisting of a non-dominated population and optional properties. Entries are separated by one or more consecutive lines starting with the # character. Text contained on these lines after the "#" character are ignored.

Each entry consists of properties, on lines starting with "//", and solutions, on all remaining lines. Properties are stored using the same format as TypedProperties.save(Writer). The decision variables, objectives, and constraints for each solution are separated by whitespace. Each decision variable is encoded using Variable.encode(). In the event an error occurs while encoding a decision variable, "-" is written and a warning issued.

Complete entries are always terminated by a line starting with the # character.

See Also:
  • Constructor Details

    • ResultFileWriter

      public ResultFileWriter(Problem problem, File file) throws IOException
      Constructs a result file writer for writing the decision variables and objectives of a sequence of non-dominated populations to a file.
      Parameters:
      problem - the problem
      file - the file to which the results are stored
      Throws:
      IOException - if an I/O error occurred
    • ResultFileWriter

      public ResultFileWriter(Problem problem, Writer writer) throws IOException
      Constructs a result file writer for writing the decision variables and objectives of a sequence of non-dominated populations to a writer.
      Parameters:
      problem - the problem
      writer - the output writer
      Throws:
      IOException - if an I/O error occurred
  • Method Details

    • setExcludeVariables

      protected void setExcludeVariables(boolean excludeVariables)
      If set, excludes decision variables from the output. In general, excluding variables is not recommended as it is then impossible to recover the original solution.
      Parameters:
      excludeVariables - true to exclude decision variables; false otherwise
    • printHeader

      protected void printHeader(Problem problem) throws IOException
      Writes the header section to the output.
      Parameters:
      problem - the problem
      Throws:
      IOException - if an I/O error occurred
    • getErrorHandler

      public ErrorHandler getErrorHandler()
      Returns the error handler used by this reader.
      Returns:
      the error handler
    • getNumberOfEntries

      public int getNumberOfEntries()
      Returns the number of entries written to the result file. Querying this method immediately after the constructor in which the result file already existed returns the number of valid entries that were recovered.
      Specified by:
      getNumberOfEntries in class ResultWriter
      Returns:
      the number of entries written to the result file thus far
    • write

      public void write(ResultEntry entry) throws IOException
      Writes the decision variables, objectives and optional properties to the output file. Constraint violating solutions are not recorded.
      Specified by:
      write in class ResultWriter
      Parameters:
      entry - the entry to write
      Throws:
      IOException - if an I/O error occurred
    • close

      public void close()
    • encode

      public String encode(Variable variable) throws IOException
      Encodes the decision variable into a string representation that can be safely written to a result file. The resulting strings must not contain any whitespace characters. For decision variables that do not support a valid encoding, the string "-" will be returned and a warning message printed.
      Parameters:
      variable - the decision variable to encode
      Returns:
      the string representation of the decision variable
      Throws:
      IOException - if an error occurred during encoding and the error handler is configured to throw
    • open

      public static ResultFileWriter open(Problem problem, File file) throws IOException
      Opens the result file for writing. Any existing file will be overwritten.
      Parameters:
      problem - the problem
      file - the file
      Returns:
      the result file writer
      Throws:
      IOException - if an I/O error occurred
    • append

      public static ResultFileWriter append(Problem problem, File file) throws IOException
      Opens the result file in append mode. If the file already exists, any invalid entries will be removed by calling repair(Problem, File). Check getNumberOfEntries() to determine the number of valid entries in the file.
      Parameters:
      problem - the problem
      file - the file
      Returns:
      the result file writer
      Throws:
      IOException - if an I/O error occurred
    • repair

      public static int repair(File file) throws IOException
      Repairs the contents of the result file, removing any incomplete or invalid entries from the file.
      Parameters:
      file - the file
      Returns:
      the number of valid entries in the file
      Throws:
      IOException - if an I/O error occurred
    • repair

      public static int repair(Problem problem, File file) throws IOException
      Repairs the contents of the result file, removing any incomplete or invalid entries from the file.
      Parameters:
      problem - the problem, or null to derive the problem from the result file
      file - the file
      Returns:
      the number of valid entries in the file
      Throws:
      IOException - if an I/O error occurred