Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Last active August 29, 2015 14:15
Show Gist options
  • Save mikecmpbll/a4081280937a1f94b180 to your computer and use it in GitHub Desktop.
Save mikecmpbll/a4081280937a1f94b180 to your computer and use it in GitHub Desktop.
races = Race.where(runs_at: (Date.today - 12.months)..Date.today)
variants_queue = # big queue!
workers = []
results = []
best_strike_rate = [0, nil]
best_avg = [100, nil]
mutex = Mutex.new
8.times do |i|
workers << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
loop do
break if variants_queue.empty?
v = variants_queue.pop
puts "Worker #{i+1} processing variant: #{v} (#{(variants.index(v).fdiv(variants.size)*100).round(2)}%). "
variant_results = []
races.each do |r|
prediction = predict(r)
result = r.results.find{|res| res.runner.name == predicted_winner}
variant_results << result.pos if result
end
strike_rate = variant_results.count{|j| j == "1"}.fdiv(variant_results.size)
finish_r = variant_results.reject(&:nil?).map(&:to_i).reject(&:zero?)
avg = finish_r.reduce(:+).fdiv(finish_r.length) rescue nil
mutex.synchronize do
best_avg = [avg, v] if avg < best_avg[0]
best_strike_rate = [strike_rate, v] if strike_rate > best_strike_rate[0]
results << [v, strike_rate, avg]
end
end
end
end
end
workers.each { |c| c.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment