Class ResultFileWriter

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

public class ResultFileWriter extends Object implements OutputWriter
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.

An entry contains two pieces of data: 1) properties which are defined on lines starting with // in the same format as TypedProperties; and 2) lines containing a sequence of floating-point numbers listing, in order, the real-valued decision variables and objectives. Each decision variable is separated by one or more whitespace characters. Decision variables that can not be encoded appear as -. The writer will attempt to output the data in a human-readable format, but falls back on Base64 encoded serialized objects to store serializable variables.

Complete entries are always terminated by a line starting with the # character. Incomplete entries, such as those with the incorrect number of decision variables or objectives, are automatically removed.

When appending is enabled, this will attempt to recover any valid records from the previous file. Query the getNumberOfEntries() method to determine how many valid entries were recovered.

See Also:
  • Constructor Details

    • ResultFileWriter

      public ResultFileWriter(Problem problem, File file, ResultFileWriter.ResultFileWriterSettings settings) 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
      settings - the settings to use when writing the result file
      Throws:
      IOException - if an I/O error occurred
  • Method Details

    • 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 interface OutputWriter
      Returns:
      the number of entries written to the result file thus far
    • append

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

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • encode

      public String encode(Variable variable)
      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
    • append

      public static ResultFileWriter append(Problem problem, File file) throws IOException
      Opens the result file in append mode. If the file already exists, this writer will validate the contents, remove any invalid entries at the end of the file, and report 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
    • overwrite

      public static ResultFileWriter overwrite(Problem problem, File file) throws IOException
      Opens the result file in overwrite mode. Any existing file will be deleted.
      Parameters:
      problem - the problem
      file - the file
      Returns:
      the result file writer
      Throws:
      IOException - if an I/O error occurred