class KnapsackSolver::GraphPrinter

This class provides support for making graphs from statistics of datasets solving results. It uses Gnuplot and also generates a Gnuplot config file for each generated graph.

Public Class Methods

new(dataset_filenames, stats, out_dir) click to toggle source

Initializes printer for graph data (graphs, Gnuplot config files).

@param dataset_filenames [Array<String>] dataset filenames @param stats [Hash] statistics of results @param out_dir [String] statistics of results to print

# File lib/knapsack_solver/graph_printer.rb, line 13
def initialize(dataset_filenames, stats, out_dir)
  @dataset_basenames = file_basenames(dataset_filenames)
  @stats = stats
  @out_dir = out_dir
end

Public Instance Methods

print() click to toggle source

Create graphs from statistics and Gnuplot configuration files.

Protected Instance Methods

datasets(graphs, x_data) click to toggle source

Creates Gnuplot datasets from statistics.

@param graphs [Array] array of graph titles @param x_data [Array] array of X axis values

# File lib/knapsack_solver/graph_printer.rb, line 56
def datasets(graphs, x_data)
  graphs.each_with_object({}) do |g, gnuplot_datasets|
    @stats.each_value do |s|
      gnuplot_datasets[g.to_s] = s.each_key.with_object([]) do |method, o|
        o << plot_dataset(method.to_s, x_data, @stats.map { |_, v| v[method].first[g] })
      end
    end
  end
end
escape_gnuplot_special_chars(str) click to toggle source

Escapes Gnuplot special characters.

@param str [String] a string @return [Strnig] the string with Gnuplot special chars escaped

# File lib/knapsack_solver/graph_printer.rb, line 100
def escape_gnuplot_special_chars(str)
  # underscore means subscript in Gnuplot
  str.gsub('_', '\_')
end
file_basenames(paths) click to toggle source

Gets basenames of supplied file paths.

@param paths [Array<String>] path to files @return [Array<String>] basenames of the paths

# File lib/knapsack_solver/graph_printer.rb, line 109
def file_basenames(paths)
  paths.each_with_object([]) do |path, basenames|
    basenames << File.basename(path, File.extname(path))
  end
end
plot(title, data, filename) click to toggle source

Create graph.

@param title [String] title of the graph @param data [Array<Gnuplot::DataSet>] Gnuplot datasets to plot @param filename [String] path to the output image file

# File lib/knapsack_solver/graph_printer.rb, line 34
def plot(title, data, filename)
  Gnuplot.open do |gp|
    Gnuplot::Plot.new(gp, &plot_config(title, 'dataset', 'y', data, filename))
  end
  File.open(File.join(File.dirname(filename), File.basename(filename, '.png') + '.gnuplot'), 'w') do |gp|
    Gnuplot::Plot.new(gp, &plot_config(title, 'dataset', 'y', data, filename))
  end
end
plot_config(title, xlabel, ylabel, plot_datasets, out_file) click to toggle source

Creates Gnuplot plot configuration (configuration text lines).

@param title [String] graph title @param xlabel [String] label of X axis @param ylabel [String] label of Y axis @param plot_datasets [Array<Gnuplot::DataSet>] Gnuplot datasets for plotting @param out_file [String] output file @return [lambda] Lambda for setting plot configuration.

# File lib/knapsack_solver/graph_printer.rb, line 84
def plot_config(title, xlabel, ylabel, plot_datasets, out_file)
  lambda do |plot|
    plot.term('png')
    plot.output(out_file)
    plot.title("'#{escape_gnuplot_special_chars(title)}'")
    plot.ylabel("'#{escape_gnuplot_special_chars(ylabel)}'")
    plot.xlabel("'#{escape_gnuplot_special_chars(xlabel)}'")
    plot.key('outside')
    plot.data = plot_datasets
  end
end
plot_dataset(title, x_data, y_data) click to toggle source

Create dataset from provided title, X axis data and Y axis data.

@param title [String] Gnuplot dataset title @param x_data [Array] Array of X values @param y_data [Array] Array of Y values @return [Gnuplot::DataSet] Gnuplot dataset.

# File lib/knapsack_solver/graph_printer.rb, line 72
def plot_dataset(title, x_data, y_data)
  Gnuplot::DataSet.new([x_data, y_data]) { |ds| ds.title = escape_gnuplot_special_chars(title) }
end
stats_to_datasets() click to toggle source

Creates Gnuplot datasets from statistics.

@return [Array<Gnuplot::DataSet>] Gnuplot datasets created from the statistics.

# File lib/knapsack_solver/graph_printer.rb, line 46
def stats_to_datasets
  graphs = @stats.values.first.values.first.first.keys
  x_data = @stats.keys
  datasets(graphs, x_data)
end