Module: Result

Defined in:
lib/result.rb

Class Method Summary collapse

Class Method Details

.get_data(type, csv = false) ⇒ Object

Transforms the result into a more useable format.

The result to be transformed will be read from the global variable $RESULT.

If the csv paramter is set the output is a string containing the result as comma seperated values.

The return value of this method depends on the type parameter:

  • :best

    Returns only the best candidate. If the csv parameter is false the result is structured like { :value => best_value, :candidate => best_candidate }

  • :min

    Returns an Array containing one Array per Generation with only the minimal value in it. Like [[ min1 ], [ min2 ]]

  • :max

    Returns an Array containing one Array per Generation with only the maximal value in it. Like [[ max1 ], [ max2 ]]

  • :average

    Returns an Array containing one Array per Generation with only the avgerage value in it. Like [[ avg1 ], [ avg2 ]]

  • :median

    Returns an Array containing one Array per Generation with only the median value in it. Like [[ med1 ], [ med2 ]]

  • :min_max_average_median

    Returns an Array containing one Array per Generation with the minimum, maximum, average and median value in it. Like [[ min1, max1, avg1, med1 ], [ min2, max2, avg2, med2 ]]

  • :all

    Returns an Array of Arrays with all values of candidates of a generation. Like [[ val1, val2 ], [ val3, val4 ]]

Parameters:

  • type (:min, :max, :average, :median, :min_max_average_median, :all)

    The type of output requested.

  • csv (Boolean) (defaults to: false)

    If the result should be a CSV.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/result.rb', line 21

def Result.get_data(type, csv=false)
  result = beautify_data $RESULT


  if type == :beautify then
    return result
  end

  if type == :best then
    best_value = Float::INFINITY
    best_candidate = nil
    result[:results].each do |generation|
      generation_values = generation.map do |candidate|
        candidate[:result]
      end
      generation_candidates = generation.map do |candidate|
        candidate[:flag_state]
      end
      generation_values.each_with_index do |value, index|
        if value < best_value then
          best_value = value
          best_candidate = generation_candidates[index]
        end
      end
    end
    if csv then
      header = %w{value flags}
      return [header [best_value, best_candidate]]
    else
      return { :value => best_value,
               :candidate => best_candidate }
    end
  end


  aggreated_results = result[:results].map do |generation|
    generation = generation.map do |candidate|
      candidate[:result]
    end

    min, max = generation.minmax
    average = generation.reduce(&:+) / generation.size
    median = generation.size % 2 == 1 ? generation.sort[generation.size / 2] :
      (generation.sort[generation.size / 2 - 1] +
       generation.sort[generation.size / 2]) / 2

    case type
    when :min_max_average_median
      [min, max, average, median]
    when :min
      [min]
    when :max
      [max]
    when :average
      [average]
    when :median
      [median]
    when :all
      generation
    end
  end

  if csv then
    case type
    when :min_max_average_median
      header = %w{min max average median}
    when :min
      header = %w{min}
    when :max
      header = %w{max}
    when :average
      header = %w{average}
    when :median
      header = %w{median}
    when :all
      header = []
    end

    header = header.join ?\,

    header + ?\n + (aggreated_results.map do |x|
      x.join ?\,
    end.join ?\n)
  else
    aggreated_results
  end
end