Class: MainCalculations

Inherits:
Object
  • Object
show all
Extended by:
CLI::Task
Defined in:
lib/MainCalculations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ MainCalculations

Returns a new instance of MainCalculations.



13
14
15
16
17
# File 'lib/MainCalculations.rb', line 13

def initialize(file_name)
  @file_name = file_name
  @all_times = Array.new
  read_from_file
end

Instance Attribute Details

#all_timesObject

Returns the value of attribute all_times.



11
12
13
# File 'lib/MainCalculations.rb', line 11

def all_times
  @all_times
end

#file_nameObject

Returns the value of attribute file_name.



11
12
13
# File 'lib/MainCalculations.rb', line 11

def file_name
  @file_name
end

Instance Method Details

#basic_stats(params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/MainCalculations.rb', line 48

def basic_stats(params)
  p '----------------------------'
  p '----------------------------'
  p '-------- STATISTICS --------'
  p 'Mean of the solvetimes   : %0.2f' % @main_vector.mean
  p 'Median of the solvetimes : %0.2f' % @main_vector.median
  p 'Best solvetime           : %0.2f' % @main_vector.min
  p 'Worst solvetime          : %0.2f' % @main_vector.max
  p 'Mode solvetime           : %0.2f' % @main_vector.mode
end

#build_graph_of_last_few_solve_times(params) ⇒ Object

Raises:

  • (ArgumentError)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/MainCalculations.rb', line 143

def build_graph_of_last_few_solve_times(params)
  raise ArgumentError, "What is the number of solves to computer average for?" unless params.count >= 1
  num_solves = params[0].to_i
  Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|

      plot.title  "Last #{num_solves} solvetimes (Total of #{@all_times.count} solves)"
      plot.xlabel "Time"
      plot.ylabel "Solvetime"

      #start_index = @all_times.count - num_solves
      #end_index = @all_times.count-1

      y = @all_times[@all_times.count-num_solves..@all_times.count-1]
      x = (@all_times.count-num_solves..@all_times.count-1).to_a

      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "linespoints"
        ds.notitle
      end
    end
  end
end

#build_graph_of_solve_times(params) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/MainCalculations.rb', line 121

def build_graph_of_solve_times(params)
  Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|

      plot.title  "Solvetime evolution over time (Total of #{@all_times.count} solves)"
      plot.xlabel "Time"
      plot.ylabel "Solvetimes"

      y = @all_times
      x = (1..@all_times.count).to_a

      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "linespoints"
        ds.notitle
      end
    end
  end
end

#build_hist_of_time_distribution(params) ⇒ Object

Raises:

  • (ArgumentError)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/MainCalculations.rb', line 169

def build_hist_of_time_distribution(params)
  raise ArgumentError, "What is the number of solves to computer average for?" unless params.count >= 3
  start_time = params[0].to_f
  end_time = params[1].to_f
  min_distance = params[2].to_f

  main_hash = Hash.new(0)
  num_bins = ((end_time - start_time) / min_distance).to_f.ceil

  printf "Number of bins is %d\n", num_bins

  @all_times.each do |time|
    if time >= start_time and time <= end_time
      main_hash[((time - start_time) / min_distance).floor] += 1
    end
  end

  puts main_hash.count
  puts main_hash.to_s

  data = Array.new

  for i in main_hash
    data.push([i, main_hash[i]])
  end

  Gnuplot.open do |gp|
    Gnuplot::Plot.new(gp) do |plot|

      plot.title  "Time Distribution (Total of #{@all_times.count} solves)"
      plot.style  "data histograms"
      plot.xtics "nomirror rotate"
      plot.boxwidth "0.5"
      # plot.xtics  "nomirror rotate by +45"

      x = Array.new
      y = Array.new

      (0..num_bins-1).to_a.each do |index|
        x.push((start_time + index * min_distance).to_s + "-" + (start_time + (index+1) * min_distance).to_s)
        y.push(main_hash[index])
      end

      plot.yrange "[0:#{main_hash.max_by{|k, v| v}[1] * 1.25}]"

      plot.data = [
        Gnuplot::DataSet.new( [x, y] ) { |ds|
          ds.using = "2:xtic(1)"
          ds.with = "boxes fill solid 0.8"
          # ds.with = "candlesticks"
          ds.title = "Number of solves"
        },
        Gnuplot::DataSet.new( [x, y] ) { |ds|
          ds.using = "0:(10 + $2):2 with labels"
          ds.title = ""
        }
      ]
    end
  end
end

#build_history_of_averages(params) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/MainCalculations.rb', line 61

def build_history_of_averages(params)
  raise ArgumentError, "What is the number of solves to computer average for?" unless params.count >= 1
  num_solves = params[0].to_i
  num_datapoints = (@all_times.count / num_solves).to_i
  all_means = Array.new
  for i in 0..num_datapoints
    this_mean = @all_times[i*num_solves..(i+1)*num_solves].to_vector.mean
    all_means.push(this_mean)
  end
  Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|

      plot.title  "Average of #{num_solves} versus time (Total of #{@all_times.count} solves)"
      plot.xlabel "n-th set of #{num_solves} solves"
      plot.ylabel "Average of #{num_solves}"
      plot.xrange "[0:#{all_means.count+2}]"

      y = all_means
      x = (1..all_means.count).to_a

      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "linespoints"
        ds.notitle
      end
    end
  end
end

#build_history_of_best_solves(params) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/MainCalculations.rb', line 91

def build_history_of_best_solves(params)
  raise ArgumentError, "What is the number of solves to computer average for?" unless params.count >= 1
  num_solves = params[0].to_i
  num_datapoints = (@all_times.count / num_solves).to_i
  all_best_times = Array.new
  for i in 0..num_datapoints
    this_min = @all_times[i*num_solves..(i+1)*num_solves].to_vector.min
    all_best_times.push(this_min)
  end
  Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|

      plot.title  "Best of #{num_solves} versus time (Total of #{@all_times.count} solves)"
      plot.xlabel "n-th set of #{num_solves} solves"
      plot.ylabel "Best of #{num_solves}"
      plot.xrange "[0:#{all_best_times.count+2}]"

      y = all_best_times
      x = (1..all_best_times.count+2).to_a

      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "linespoints"
        ds.notitle
      end
    end
  end
end

#hello(params) ⇒ Object



21
22
23
24
# File 'lib/MainCalculations.rb', line 21

def hello(params)
  puts "Hello, what's up?"
  puts "You gave me #{params.inspect} as parameters"
end

#read_from_fileObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/MainCalculations.rb', line 26

def read_from_file
  start_reading = false
  # read the input file
  File.open(@file_name, "r") do |filin|
    while(line = filin.gets)
      if not start_reading and /Time\sList/.match(line)
        start_reading = true
      end

      if start_reading
        matches = line.scan(/\d{2}.\d{2}/)
        matches.each do |match|
          @all_times.push(match.to_f)
        end
      end
    end
  end
  @main_vector = @all_times.to_vector(:scale)
end