0

したがって、データセットが小さい場合に機能するように、このメソッドが機能するようになりました。でも、もう少しだけ大きくなると……。

このスクリプトの目的は、可能なすべてのセットの組み合わせを重複なしで見つけることです。それらをデータベーステーブルに保存できるようにします。

set 1: [701,744,410,646,723,434]
set 2: [701,744,410,646,723,435]
set 3: etc..

また、元のキーとの関係を維持する必要があることにも注意してください。そのため、type1 のアイテムを他のタイプに移動することはできません。それが理にかなっていることを願っています。

Collecting pieces...
  pieces[type1] = [701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722]
  pieces[type2] = [744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765]
  pieces[type3] = [410, 412, 413, 414, 415, 419, 422, 424, 426, 427, 429, 372, 374, 376, 378, 380, 382, 385, 395, 397, 399, 401]
  pieces[type4] = [646, 647, 649, 651, 653, 655, 657, 671, 672, 673, 674, 679, 681, 684, 686, 688, 691, 695, 697, 698, 699, 700]
  pieces[type5] = [723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743]
  pieces[type6] = [434, 435, 438, 440, 443, 446, 447, 462, 464, 467, 469, 484, 485, 486, 487, 488, 489, 490, 491, 492, 494, 496]
Took 0.4265 seconds to collect.

Generating possibilities...
/Projects/my_project/lib/tasks/possibilities.rake:109: [BUG] Segmentation fault
ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]

はい、セグメンテーション違反です。

これを実現するために使用しているコードを次に示します。

def permutations!(input)
  permutations_start = Time.now
  puts "Generating possibilities..."
  input.each do |key, possibilities|
    possibilities.map!{|p| {key => p} }
  end

  digits = input.keys.map!{|key| input[key] }

  # This is the line that seems to want to cry.
  result = digits.shift.product(*digits)

  puts "# of generated possibilities: #{result.length}"
  puts "Took #{(Time.now - permutations_start).round(4)} seconds to generate.\n\n"

  return result
end

pieces = {}
pieces['type1'] = [701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722]
pieces['type2'] = [744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765]
pieces['type3'] = [410, 412, 413, 414, 415, 419, 422, 424, 426, 427, 429, 372, 374, 376, 378, 380, 382, 385, 395, 397, 399, 401]
pieces['type4'] = [646, 647, 649, 651, 653, 655, 657, 671, 672, 673, 674, 679, 681, 684, 686, 688, 691, 695, 697, 698, 699, 700]
pieces['type5'] = [723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743]
pieces['type6'] = [434, 435, 438, 440, 443, 446, 447, 462, 464, 467, 469, 484, 485, 486, 487, 488, 489, 490, 491, 492, 494, 496]
possibilities = permutations!(pieces)
4

1 に答える 1

0

記憶の限りでは大丈夫そうです。CPU は以前と同じように固定されています。

現在、大部分の時間はレコードをデータベースに保存しています。activerecord-import または一括挿入を使用してより速く処理できればと思いますが、グループを保存する前に計算を行う必要があります。そのため、モデルで処理されるように before_save フックとして設定しました。

現在の速度では、データベース内のすべてのデータを取得するのに約数か月かかります。

def generate(input)
  input.each do |key, possibilities|
    possibilities.map!{|p| {key => p} }
  end

  digits = input.keys.map!{ |key| input[key] }

  i = 1
  shifted = digits.shift
  shifted.each do |item|
    puts "Generating groups #{i} of #{shifted.length}..."
    permutations_start = Time.now
    results = [item].product(*digits)
    puts "# of generated groups in the set number - #{i}: #{results.length}"
    puts "Took #{(Time.now - permutations_start).round(4)} seconds to generate.\n\n"

    # Storing the groups
    puts "Storing groups..."
    storing_start = Time.now
    results.each { |item| Group.create!(item.reduce({}, :update)) }
    puts "Took #{(Time.now - storing_start).round(4)} seconds to store.\n\n"

    i = i + 1
  end
end

出力例:

Collecting pieces...
  possibilities['type1'] = [701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722]
  possibilities['type2'] = [744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765]
  possibilities['type3'] = [410, 412, 413, 414, 415, 419, 422, 424, 426, 427, 429, 372, 374, 376, 378, 380, 382, 385, 395, 397, 399, 401]
  possibilities['type4'] = [646, 647, 649, 651, 653, 655, 657, 671, 672, 673, 674, 679, 681, 684, 686, 688, 691, 695, 697, 698, 699, 700]
  possibilities['type5'] = [723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743]
  possibilities['type6'] = [434, 435, 438, 440, 443, 446, 447, 462, 464, 467, 469, 484, 485, 486, 487, 488, 489, 490, 491, 492, 494, 496]
Took 0.4248 seconds to collect.

Generating groups 1 of 22...
There were 4,919,376 groups in the set number 1.
Took 1.819 seconds to generate.

Storing Groups...
250 items took 11.7158 seconds
250 items took 11.5094 seconds
250 items took 11.6994 seconds
250 items took 11.5678 seconds
250 items took 11.5529 seconds
于 2012-11-13T01:30:57.867 に答える