easy-box-packer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -16
  3. data/easy-box-packer.rb +8 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29750bc45104f8bddc2dbf0117bb41a5dbeae216
4
- data.tar.gz: 6b696a68d044e4bdb34f31b02410c3597e2fbadc
3
+ metadata.gz: 9d7ee6c881d4342f0daaf9c3a87b838a6094c9e5
4
+ data.tar.gz: 2edf4d7bda53d69bd0e8f127124cf31619b940a1
5
5
  SHA512:
6
- metadata.gz: cf3d9d27612e1bf412452eca360d2d792262ccf7137bd0f47dc1ce57d2f56477ab7901a3f7269a54958d6c71dabd0ff46494133b551e6122cde25c231262d2e1
7
- data.tar.gz: c87f00e751a2c6b45096b9517f24c775ca93b0066596503ee842bee2cac8213aa18d2a4d6d2cb75160acbef7f5318e7329b87017f9f31cd22a775957bf1677de
6
+ metadata.gz: c4720460af7baf0828a12661358cec38e1ec1e4a565295d7604c879043bd268f9a6954bff6fb2d211c05ffef2c7ad8c2dd08a69a14318a6a06285066313f96e5
7
+ data.tar.gz: 30c42440ccad0c65b874166c523e4d5945250182a3a0cef5b992505772af2493d50a55846c4af72bcfade997a0991b825a7f8aa7de7542c0dfb00046f22b5d23
data/README.md CHANGED
@@ -14,10 +14,12 @@ gem 'easy-box-packer'
14
14
 
15
15
  ## Usage
16
16
 
17
+ ### Check all items can be packed in a container
18
+
17
19
  ``` ruby
18
20
  require 'easy-box-packer'
19
21
 
20
- packings = BoxPacker.pack(
22
+ cont = EasyBoxPacker.pack(
21
23
  container: { dimensions: [15, 20, 13], weight_limit: 50 },
22
24
  items: [
23
25
  { dimensions: [2, 3, 5], weight: 47 },
@@ -27,19 +29,28 @@ packings = BoxPacker.pack(
27
29
  ]
28
30
  )
29
31
 
30
- packings.length # 3
31
- packings[0][:weight] # 47
32
- packings[0][:placements].length # 1
33
- packings[0][:placements][0][:dimensions] # [2, 3, 5]
34
- packings[0][:placements][0][:position] # [0, 0, 0]
35
- packings[1][:weight] # 47
36
- packings[1][:placements].length # 1
37
- packings[1][:placements][0][:dimensions] # [2, 3, 5]
38
- packings[1][:placements][0][:position] # [0, 0, 0]
39
- packings[2][:weight] # 31
40
- packings[2][:placements].length # 2
41
- packings[2][:placements][0][:dimensions] # [1, 3, 3]
42
- packings[2][:placements][0][:position] # [0, 0, 0]
43
- packings[2][:placements][1][:dimensions] # [4, 1, 1]
44
- packings[2][:placements][1][:position] # [3, 0, 0]
32
+ cont[:packings].length # 3
33
+ cont[:packings][0][:weight] # 47
34
+ cont[:packings][0][:placements].length # 1
35
+ cont[:packings][0][:placements][0][:dimensions] # [2, 3, 5]
36
+ cont[:packings][0][:placements][0][:position] # [0, 0, 0]
37
+ cont[:packings][1][:weight] # 47
38
+ cont[:packings][1][:placements].length # 1
39
+ cont[:packings][1][:placements][0][:dimensions] # [2, 3, 5]
40
+ cont[:packings][1][:placements][0][:position] # [0, 0, 0]
41
+ cont[:packings][2][:weight] # 31
42
+ cont[:packings][2][:placements].length # 2
43
+ cont[:packings][2][:placements][0][:dimensions] # [1, 3, 3]
44
+ cont[:packings][2][:placements][0][:position] # [0, 0, 0]
45
+ cont[:packings][2][:placements][1][:dimensions] # [4, 1, 1]
46
+ cont[:packings][2][:placements][1][:position] # [3, 0, 0]
47
+ ```
48
+
49
+ ### Get a reasonable smallest container by given boxes
50
+
51
+ ```
52
+ container = EasyBoxPacker.find_smallest_container(
53
+ items: Array.new(1000) {{ dimensions: [1, 1, 1] }}
54
+ )
55
+ container # [10, 10, 10]
45
56
  ```
@@ -76,6 +76,10 @@ module EasyBoxPacker
76
76
  # base_container = sorted_items.first
77
77
  based_container = sorted_items.first
78
78
 
79
+ if sorted_items.size == 1
80
+ return based_container[:dimensions]
81
+ end
82
+
79
83
  find_possible_container(
80
84
  possible_containers: possible_containers,
81
85
  invalid_containers: invalid_containers,
@@ -118,9 +122,9 @@ module EasyBoxPacker
118
122
  ]
119
123
 
120
124
  tmp_possible_containers = []
121
- # (1) loops base_container 6 rotations
125
+ # (1) loops base_container 3 rotations
122
126
  c_permutations.each do |c_perm|
123
- # (2) try to puts items to 3 points, then it will create 6 different possible containers
127
+ # (2) try to puts items to 3 points, then it will create 3 different possible containers
124
128
  b_permutations.each do |b_perm|
125
129
  tmp_possible_containers << [ c_perm[0] + b_perm[0], [c_perm[1], b_perm[1]].max, [c_perm[2], b_perm[2]].max]
126
130
  tmp_possible_containers << [ [c_perm[0], b_perm[0]].max, c_perm[1] + b_perm[1], [c_perm[2], b_perm[2]].max]
@@ -128,13 +132,14 @@ module EasyBoxPacker
128
132
  end
129
133
  end
130
134
  removed_tried_container = tmp_possible_containers.map { |a| a.sort }.uniq - possible_containers - invalid_containers
135
+
131
136
  return unless removed_tried_container.any?
132
137
  # (3) loop all container from smallest spaces to biggest space
133
138
  removed_tried_container.sort_by { |a| [a.inject(&:*), a.inject(&:+)] }.each do |cont|
134
139
  # (4) next unless l * w * h >= minimum_space
135
140
  if cont.inject(&:*) >= min_vol
136
141
  possible_containers << cont
137
- else
142
+ # else
138
143
  # puts "invalid: #{cont}"
139
144
  # invalid_containers << cont
140
145
  # find_possible_container(possible_containers: possible_containers, invalid_containers: invalid_containers, container: cont, items: items, item_index: item_index + 1, min_vol: min_vol)
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.3
4
+ version: 0.0.4
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-23 00:00:00.000000000 Z
11
+ date: 2017-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec