Class: BinPacking::Bin
- Inherits:
-
Object
- Object
- BinPacking::Bin
- Defined in:
- lib/bin_packing/bin.rb
Instance Attribute Summary collapse
-
#boxes ⇒ Object
readonly
Returns the value of attribute boxes.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#heuristic ⇒ Object
readonly
Returns the value of attribute heuristic.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #efficiency ⇒ Object
-
#initialize(width, height, heuristic = nil) ⇒ Bin
constructor
A new instance of Bin.
- #insert(box) ⇒ Object
- #insert!(box) ⇒ Object
- #insert_in_known_position(box) ⇒ Object
- #is_larger_than?(box) ⇒ Boolean
- #label ⇒ Object
- #score_for(box) ⇒ Object
Constructor Details
#initialize(width, height, heuristic = nil) ⇒ Bin
Returns a new instance of Bin.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/bin_packing/bin.rb', line 5 def initialize(width, height, heuristic = nil) @width = width @height = height @boxes = [] box = BinPacking::FreeSpaceBox.new(width, height) @free_rectangles = [box] @heuristic = heuristic || BinPacking::Heuristics::BestShortSideFit.new end |
Instance Attribute Details
#boxes ⇒ Object (readonly)
Returns the value of attribute boxes.
3 4 5 |
# File 'lib/bin_packing/bin.rb', line 3 def boxes @boxes end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
3 4 5 |
# File 'lib/bin_packing/bin.rb', line 3 def height @height end |
#heuristic ⇒ Object (readonly)
Returns the value of attribute heuristic.
3 4 5 |
# File 'lib/bin_packing/bin.rb', line 3 def heuristic @heuristic end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
3 4 5 |
# File 'lib/bin_packing/bin.rb', line 3 def width @width end |
Instance Method Details
#efficiency ⇒ Object
16 17 18 19 20 |
# File 'lib/bin_packing/bin.rb', line 16 def efficiency boxes_area = 0 @boxes.each { |box| boxes_area += box.area } boxes_area * 100 / area end |
#insert(box) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/bin_packing/bin.rb', line 22 def insert(box) return false if box.packed? @heuristic.find_position_for_new_node!(box, @free_rectangles) return false unless box.packed? insert_in_known_position(box) end |
#insert!(box) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/bin_packing/bin.rb', line 31 def insert!(box) unless insert(box) raise ArgumentError, "Could not insert box #{box.inspect} "\ "into bin #{inspect}." end self end |
#insert_in_known_position(box) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bin_packing/bin.rb', line 39 def insert_in_known_position(box) num_rectangles_to_process = @free_rectangles.size i = 0 while i < num_rectangles_to_process if split_free_node(@free_rectangles[i], box) @free_rectangles.delete_at(i) num_rectangles_to_process -= 1 else i += 1 end end prune_free_list @boxes << box true end |
#is_larger_than?(box) ⇒ Boolean
61 62 63 64 |
# File 'lib/bin_packing/bin.rb', line 61 def is_larger_than?(box) (@width >= box.width && @height >= box.height) || (@height >= box.width && @width >= box.height) end |
#label ⇒ Object
66 67 68 |
# File 'lib/bin_packing/bin.rb', line 66 def label "#{@width}x#{@height} #{efficiency}%" end |
#score_for(box) ⇒ Object
57 58 59 |
# File 'lib/bin_packing/bin.rb', line 57 def score_for(box) @heuristic.find_position_for_new_node!(box.clone, @free_rectangles) end |