E. Renders, Listeners

List of all renders and listeners in JCOP

In table 1 is a list of all renders and listeners bundled with JCOP, along with destination media, type and how they format results.

Name Type Format description Media
CSVRender Render Creates CSV using JavaCSV library. java.io.OutputStream
java.io.File
System.out (console)
ExceptionRender Render Prints only exceptions for results, but with their full stack trace. System.out (console)
JFreeChartRender Listener Creates chart with on-the-fly results, each solver iteration has its own plot line. JFrame
SimpleCompareRender Render Sorts result entries by fitness (descending) and by optimize counter (ascending), prints two tables with problem, algorithm and either fitness or optimize counter. Useful for quick comparison of different algorithms/settings. java.io.PrintStream
java.io.File
System.out (console)
SimpleRender Render Prints various levels of text information about result entries. java.io.PrintStream
java.io.File
System.out (console)
XMLRender Render Creates XML file with rich information about each result entry. java.io.File

Table 1: List of renders and listeners in JCOP.

CSVRender

CSVRender creates CSV output, one line with headers and then one line for every result entry. Note that by default it uses comma (",") as delimiter and utf-8 encoding, both incompatible with Microsoft Office Excel. Both can be changed either by CSVRender#setCharset(Charset) and CSVRender#setDelimiter(char). Also keep in mind that even Open Office cannot have more than 1024 columns (Microsoft Office Excel 2003 has only 256, Microsoft Office Excel 2007 should be able to read about 15681 columns) so operation history might me trimmed if it is too long. See figure 1 for demo output.

CSVRender output opened in Open Office Calc

Figure 1: CSVRender output opened in Open Office Calc.

Below is example how to use CSVRender:

Solver solver = /* ... */

// Output to System.out
solver.addRender(new CSVRender());

// Output to file
solver.addRender(new CSVRender(new File("output-file.csv")));

// Output to directly specified OutputStream
solver.addRender(new CSVRender(System.out));

// ISO-8859-1 charset, ';' delimiter
CSVRender csvRender = new CSVRender();
csvRender.setDelimiter(';');
csvRender.setCharset(Charset.forName("ISO-8859-1"));

ExceptionRender

ExceptionRender creates detailed report only about exceptions raised in solver iteration. Writes to console only and uses Throwable#printStackTrace() to print results. See below for example output.

Problem Knapsack [line=9000 4 100 18 114 42 136 88 192 3 223]/Algorithm BreadthFirstSearch [] got Exception CannotContinueException
cz.cvut.felk.cig.jcop.algorithm.CannotContinueException: No more items in queue
	at cz.cvut.felk.cig.jcop.algorithm.graphsearch.GraphSearch.optimize(GraphSearch.java:44)
	at cz.cvut.felk.cig.jcop.solver.BaseSolver.optimize(BaseSolver.java:160)
	at cz.cvut.felk.cig.jcop.solver.SimpleSolver.run(SimpleSolver.java:44)
	at demopackage.DemoTemp.main(DemoTemp.java:135)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)

Example 1: ExceptionRender displaying exception in BFS while solving Knapsack problem.

Example how to use ExceptionRender:

Solver solver = /* ... */

solver.addRender(new ExceptionRender());

JFreeChartRender

JFreeChartRender listens Solver for any changes of best solution. If any is found (message MessageBetterConfigurationFound) it adds one point to chart. Every solver iteration has its own plot line. X axis holds number of optimizations, Y axis fitness of best solution. JFreeChartRender uses JFreeChart library. See figure 2 for how results from this render looks like.

JFreeChartRender output when benchmarking 10 settings of Simulated Annealing on SAT problem

Figure 2: JFreeChartRender output when benchmarking 10 settings of Simulated Annealing on SAT problem.

Currently JFreeChartRender requires only a title as a parameter, co its usage is just this snippet:

Solver solver = /* ... */

solver.addListener(new JFreeChartRender("Demo JFreeChartRender"));

SimpleCompareRender

SimpleCompareRender makes simple comparison of results by their fitness and number of optimizations. This is useful to be able to see quickly which algorithm (or problem) performed better and which worse. It can write to console, java.io.File or any other java.io.PrintStream. Result look like two tables, one sorted by fitness, the other by optimization counter. Below is example of render's output.

Results sorted by fitness (DESC):
fitness    algorithm                               problem
7,0        SimulatedAnnealing [T=10.0, A=0.9]      SAT [valid-standard.cnf]
-2,0       SimulatedAnnealing [T=10.0, A=0.999]    SAT [valid-standard.cnf]
-3,0       BreadthFirstSearch []                   SAT [valid-standard.cnf]
-3,0       SimulatedAnnealing [T=100.0, A=0.999]   SAT [valid-standard.cnf]
-4,0       DepthFirstSearch []                     SAT [valid-standard.cnf]

Results sorted by optimizations (ASC):
optimiz.   algorithm                               problem
725        BreadthFirstSearch []                   SAT [valid-standard.cnf]
756        DepthFirstSearch []                     SAT [valid-standard.cnf]
2981       SimulatedAnnealing [T=10.0, A=0.999]    SAT [valid-standard.cnf]
3636       SimulatedAnnealing [T=10.0, A=0.9]      SAT [valid-standard.cnf]
3930       SimulatedAnnealing [T=100.0, A=0.999]   SAT [valid-standard.cnf]

Example 2: SimpleCompareRender rendering usage of different algorithms on SAT problem with 100ms timeout.

SimpleCompareRender can be added to solver this way:

Solver solver = /* ... */

// Output to console
solver.addRender(new SimpleCompareRender());

// Output to file
solver.addRender(new SimpleCompareRender(new File("output-file.txt")));

// Output to directly specified PrintStream
solver.addRender(new SimpleCompareRender(System.out));

SimpleRender

SimpleRender is basic render of JCOP. It provides output to console, java.io.File or any other java.io.PrintStream. It has three levels of details, SimpleRender#OUTPUT_STANDARD (which prints almost everything except operation history), SimpleRender#OUTPUT_MINI (which prints just time, number of optimizations, fitness, exception and best solution) and SimpleRender#OUTPUT_FULL (printing almost everything, including full result history). Below are examples of these three formats.

=== Algorithm DepthFirstSearch [] used on problem SAT [valid-standard.cnf] ===
  CPU Time:                     110 [ms]
  System Time:                    1 [ms]
  User Time:                    109 [ms]
  Clock Time:                   125 [ms]
  Optimize counter:             511 [-]
  Optimize/sec (CPU):          4645 [1/s]
  Optimize/sec (Clock):        4088 [1/s]
  Best solution:         Configuration{attributes=[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1], operationHistory={247 items}}
  Depth:                        247 [-]
  Fitness:                     -4,0 [-]
  Ended without exception

Example 3: SimpleRender using SimpleRender#OUTPUT_STANDARD output level to render DFS on SAT with 100ms timeout.

=== (DepthFirstSearch [], SAT [valid-standard.cnf]) ===
  CPU Time:     110 ms. Optimizations:     511. Fitness:    -4,0. Best solution: Configuration{attributes=[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1], operationHistory={247 items}}. Ended without exception.

Example 4: SimpleRender using SimpleRender#OUTPUT_MINI output level to render DFS on SAT with 100ms timeout.


=== Algorithm DepthFirstSearch [] used on problem SAT [valid-standard.cnf] ===
  CPU Time:                     110 [ms]
  System Time:                    1 [ms]
  User Time:                    109 [ms]
  Clock Time:                   125 [ms]
  Optimize counter:             511 [-]
  Optimize/sec (CPU):          4645 [1/s]
  Optimize/sec (Clock):        4088 [1/s]
  Best solution: [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1]
  Depth:                        247 [-]
  Fitness:                     -4,0 [-]
  Operation history:
           0. Empty SAT created
           1. SetTrueOperation{index=19}
           2. SetTrueOperation{index=18}
           3. SetTrueOperation{index=17}
           4. SetFalseOperation{index=19}
           5. SetTrueOperation{index=16}
           ... ... ...

Example 5: SimpleRender using SimpleRender#OUTPUT_FULL output level to render DFS on SAT with 100ms timeout.

SimpleRender can be added to solver this way:

Solver solver = /* ... */

// Output standard info to console
solver.addRender(new SimpleRender(SimpleRender.OUTPUT_STANDARD));

// Output minimal info to specified PrintStream
solver.addRender(new SimpleRender(SimpleRender.OUTPUT_MINI, System.out));

// Output full into to file
solver.addRender(new SimpleRender(SimpleRender.OUTPUT_FULL, new File("output-file.txt")));

XMLRender

XMLRender creates XML file with all possible information about results. Useful for automatized processing. Requires java.io.File to write into. Below is examples of result XML file (reformatted for easied read).

<?xml version="1.0" standalone="yes"?>
<result>
  <experiment date='Wed Mar 17 16:47:11 CET 2010'></experiment>
  <entry exception='CannotContinueException' clock-time='16' system-time='1' user-time='16' algorithm='BreadthFirstSearch []' cpu-time='15' optimize-counter='16' problem='Knapsack [line=9000 4 100 18 114 42 136 88 192 3 223]'>
    <best-solution fitness='473.0'>
      <operations>
        <operation index='0' label='Empty knapsack created'></operation>
        <operation index='1' label='AddOperation{knapsackItem=KnapsackItem{index=0, weight=18, price=114}}'></operation>
        <operation index='2' label='AddOperation{knapsackItem=KnapsackItem{index=1, weight=42, price=136}}'></operation>
        <operation index='3' label='AddOperation{knapsackItem=KnapsackItem{index=3, weight=3, price=223}}'></operation>
      </operations>
      <attributes>
        <attribute index='0' value='1' human-readable-value='1'></attribute>
        <attribute index='1' value='1' human-readable-value='1'></attribute>
        <attribute index='2' value='0' human-readable-value='0'></attribute>
        <attribute index='3' value='1' human-readable-value='1'></attribute>
      </attributes>
    </best-solution>
  </entry>
</result>

Example 6: XMLRender creates XML file for BFS used on Knapsack.

XMLRender is used as shown in following snippet:

Solver solver = /* ... */

solver.addRender(new XMLRender(new File("output-file.xml")));