Class: KnapsackSolver::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_solver/cli.rb

Overview

This class implements a command-line interface for the 0/1 knapsack problem solver.

Constant Summary collapse

RESULTS_FILNEMAE_SUFFIX =

Suffix of a text file which will containg results of dataset solving (price, knapsack things presence, cpu time, wall clock time, relative_error).

'.results'.freeze
STATS_FILNEMAE_SUFFIX =

Suffix of a text file which will containg statistic data (average price, execution times, relative error)

'.stats'.freeze

Class Method Summary collapse

Class Method Details

Prints output of datasets solving. Results and statistics are printed to stdout or to a text files. Graphs of statistic values can be created.

Parameters:

  • results (Hash)

    results of dataset solvings

  • stats (Hash)

    statistics from the results of dataset solvings

  • options (Hash)

    Command-line line options supplied to the CLI

  • args (Array)

    array of the positional command-line arguments



42
43
44
45
46
47
# File 'lib/knapsack_solver/cli.rb', line 42

def self.print_results(results, stats, options, args)
  OutputPrinter.new(args, RESULTS_FILNEMAE_SUFFIX, results).print(options[:output_dir])
  OutputPrinter.new(args, STATS_FILNEMAE_SUFFIX, stats).print(options[:output_dir])
  return unless options[:graphs_dir]
  GraphPrinter.new(args, stats, options[:graphs_dir]).print
end

.run(args) ⇒ Object

Processes command-line arguments. If no option is given, converts arabic number to roman number and prints it to stdout.

Parameters:

  • args (Array)

    the command-line arguments



24
25
26
27
28
29
30
31
32
33
# File 'lib/knapsack_solver/cli.rb', line 24

def self.run(args)
  options = CliOptionParser.parse(args)
  return if options.nil?
  datasets = args.each_with_object([]) do |file, sets|
    sets << Dataset.parse(File.new(file))
  end
  s = Solver.new(options, datasets)
  results = s.run
  print_results(results, s.stats(results), options, args)
end