Skip to content

Instantly share code, notes, and snippets.

@seanstickle
Created October 27, 2018 23:00
Show Gist options
  • Select an option

  • Save seanstickle/6aa9032d9f81c1b5e1b0a720b172afb0 to your computer and use it in GitHub Desktop.

Select an option

Save seanstickle/6aa9032d9f81c1b5e1b0a720b172afb0 to your computer and use it in GitHub Desktop.

Revisions

  1. seanstickle created this gist Oct 27, 2018.
    72 changes: 72 additions & 0 deletions best-wing-prices.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/usr/bin/env ruby

    require "ruby-cbc"
    require "csv"

    wings = {
    4 => 4.55,
    5 => 5.70,
    6 => 6.80,
    7 => 7.95,
    8 => 9.10,
    9 => 10.20,
    10 => 11.35,
    11 => 12.50,
    12 => 13.60,
    13 => 14.75,
    14 => 15.90,
    15 => 17.00,
    16 => 18.15,
    17 => 19.30,
    18 => 20.40,
    19 => 21.55,
    20 => 22.70,
    21 => 23.80,
    22 => 24.95,
    23 => 26.10,
    24 => 27.25,
    25 => 27.80,
    26 => 28.95,
    27 => 30.10,
    28 => 31.20,
    29 => 32.35,
    30 => 33.50,
    35 => 39.15,
    40 => 44.80,
    45 => 50.50,
    50 => 55.60,
    60 => 67.00,
    70 => 78.30,
    75 => 83.45,
    80 => 89.10,
    90 => 100.45,
    100 => 111.25,
    125 => 139.00,
    150 => 166.85,
    200 => 222.50
    }

    CSV.open("best_combo.csv", "w") do |csv|
    csv << ["wings", "price"]
    best_combos = (wings.keys.min .. wings.keys.max).each do |x|
    m = Cbc::Model.new
    vars = Array.new(wings.size)
    vars = m.int_var_array(wings.size, 0..Cbc::INF)
    vars = vars.each_with_index.map do |var,i|
    {
    var: var,
    num_wings: Array(wings)[i][0],
    price: Array(wings)[i][1]
    }
    end
    m.minimize(vars.map{|x| x[:price] * x[:var]}.reduce(:+))
    m.enforce(vars.map{|x| x[:num_wings] * x[:var]}.reduce(:+) == x)
    p = m.to_problem
    p.solve
    if p.proven_infeasible?
    csv << [x, nil]
    else
    csv << [x, vars.map{|x| p.value_of(x[:var]) * x[:price]}.reduce(:+)]
    end
    end
    end