easy-box-packer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/easy-box-packer.rb +82 -26
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fdf9c87fb9465a372621274314c24d467848dcc
4
- data.tar.gz: 328700b5ffd7860761c832d43ade508b01749cac
3
+ metadata.gz: 29750bc45104f8bddc2dbf0117bb41a5dbeae216
4
+ data.tar.gz: 6b696a68d044e4bdb34f31b02410c3597e2fbadc
5
5
  SHA512:
6
- metadata.gz: 181a58237b38d18dd3672e01aa66bc93583de44c3be03258e9505edec538c9ab58d529a60dea4a7ff9dd4155747d008507baa9d475a13dfd62f942f0f0e8f54c
7
- data.tar.gz: ffbf053c8ac2aa0213f32df3b4c8a87276f15ab7159dcfa6fe63d71a44fc88e92790422d3b2fdcbff3a9008a0682341b8b46747f7c0396cb50a2d6d6323cebaf
6
+ metadata.gz: cf3d9d27612e1bf412452eca360d2d792262ccf7137bd0f47dc1ce57d2f56477ab7901a3f7269a54958d6c71dabd0ff46494133b551e6122cde25c231262d2e1
7
+ data.tar.gz: c87f00e751a2c6b45096b9517f24c775ca93b0066596503ee842bee2cac8213aa18d2a4d6d2cb75160acbef7f5318e7329b87017f9f31cd22a775957bf1677de
data/easy-box-packer.rb CHANGED
@@ -65,6 +65,88 @@ module EasyBoxPacker
65
65
  { packings: packings, errors: errors }
66
66
  end
67
67
 
68
+ def find_smallest_container(items:)
69
+ possible_containers = []
70
+ invalid_containers = []
71
+
72
+ min_vol = items.map { |h| h[:dimensions].inject(&:*) }.inject(&:+)
73
+ # order items from biggest to smallest
74
+ sorted_items = items.sort_by { |h| h[:dimensions].sort }.reverse
75
+
76
+ # base_container = sorted_items.first
77
+ based_container = sorted_items.first
78
+
79
+ find_possible_container(
80
+ possible_containers: possible_containers,
81
+ invalid_containers: invalid_containers,
82
+ container: based_container[:dimensions],
83
+ items: sorted_items.map {|i| i[:dimensions]},
84
+ item_index: 1,
85
+ min_vol: min_vol)
86
+
87
+ possible_containers.map { |a| a.sort }.sort_by { |a| [a.inject(&:*), a.inject(&:+)] }.each do |c|
88
+ packing = pack(
89
+ container: { dimensions: c },
90
+ items: items)
91
+ return c if packing[:packings].size == 1
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def std(contents)
98
+ n = contents.size
99
+ contents.map!(&:to_f)
100
+ mean = contents.reduce(&:+)/n
101
+ sum_sqr = contents.map {|x| x * x}.reduce(&:+)
102
+ Math.sqrt((sum_sqr - n * mean * mean)/(n-1))
103
+ end
104
+
105
+ def find_possible_container(possible_containers:,invalid_containers:,container:, items:, item_index:, min_vol:)
106
+ return unless items[item_index]
107
+ c_length, c_width, c_height = container.sort.reverse
108
+ b_length, b_width, b_height = items[item_index].sort.reverse
109
+ c_permutations = [
110
+ [c_width, c_height, c_length],
111
+ [c_length, c_width, c_height],
112
+ [c_length, c_height, c_width]
113
+ ]
114
+ b_permutations = [
115
+ [b_width, b_height, b_length],
116
+ [b_length, b_width, b_height],
117
+ [b_length, b_height, b_width]
118
+ ]
119
+
120
+ tmp_possible_containers = []
121
+ # (1) loops base_container 6 rotations
122
+ c_permutations.each do |c_perm|
123
+ # (2) try to puts items to 3 points, then it will create 6 different possible containers
124
+ b_permutations.each do |b_perm|
125
+ tmp_possible_containers << [ c_perm[0] + b_perm[0], [c_perm[1], b_perm[1]].max, [c_perm[2], b_perm[2]].max]
126
+ tmp_possible_containers << [ [c_perm[0], b_perm[0]].max, c_perm[1] + b_perm[1], [c_perm[2], b_perm[2]].max]
127
+ tmp_possible_containers << [ [c_perm[0], b_perm[0]].max, [c_perm[1], b_perm[1]].max, c_perm[2]+ b_perm[2]]
128
+ end
129
+ end
130
+ removed_tried_container = tmp_possible_containers.map { |a| a.sort }.uniq - possible_containers - invalid_containers
131
+ return unless removed_tried_container.any?
132
+ # (3) loop all container from smallest spaces to biggest space
133
+ removed_tried_container.sort_by { |a| [a.inject(&:*), a.inject(&:+)] }.each do |cont|
134
+ # (4) next unless l * w * h >= minimum_space
135
+ if cont.inject(&:*) >= min_vol
136
+ possible_containers << cont
137
+ else
138
+ # puts "invalid: #{cont}"
139
+ # invalid_containers << cont
140
+ # find_possible_container(possible_containers: possible_containers, invalid_containers: invalid_containers, container: cont, items: items, item_index: item_index + 1, min_vol: min_vol)
141
+ end
142
+ end
143
+ # minimum_space = (removed_tried_container).sort_by { |a| [a.inject(&:*), a.inject(&:+)] }.first
144
+ minimum_std = removed_tried_container.sort_by { |a| [std(a), a.inject(&:*), a.inject(&:+)] }.first
145
+ [minimum_std].uniq.compact.each do |cont|
146
+ find_possible_container(possible_containers: possible_containers, invalid_containers: invalid_containers, container: cont, items: items, item_index: item_index + 1, min_vol: min_vol)
147
+ end
148
+ end
149
+
68
150
  def place(item, space)
69
151
  item_width, item_height, item_length = item[:dimensions].sort.reverse
70
152
 
@@ -139,31 +221,5 @@ module EasyBoxPacker
139
221
  }
140
222
  ]
141
223
  end
142
-
143
- def find_smallest_container(items:)
144
- array_of_lwh = items.map { |i| i[:dimensions].sort.reverse }
145
- items_max_length = array_of_lwh.max { |x, y| x[0] <=> y[0] }[0]
146
- items_max_width = array_of_lwh.max { |x, y| x[1] <=> y[1] }[1]
147
- items_min_height = array_of_lwh.max { |x, y| x[2] <=> y[2] }[2]
148
- items_total_height = array_of_lwh.inject(0) { |sum, x| sum + x[2] }.round(1)
149
- miminum_box = {}
150
- (items_min_height..items_total_height.ceil).step(0.1).to_a.bsearch do |i|
151
- packing = pack(
152
- container: { dimensions: [items_max_length, items_max_width, i] },
153
- items: array_of_lwh.map { |a| { dimensions: a }})
154
-
155
- if packing[:packings].size == 1
156
- miminum_box = {
157
- length: items_max_length,
158
- width: items_max_width,
159
- height: i
160
- }
161
- true
162
- else
163
- false
164
- end
165
- end
166
- miminum_box
167
- end
168
224
  end
169
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-box-packer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aloha Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-21 00:00:00.000000000 Z
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec