blf 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/blf.gemspec +28 -0
- data/example/example.rb +23 -0
- data/lib/blf.rb +9 -0
- data/lib/blf/block.rb +37 -0
- data/lib/blf/test.rb +17 -0
- data/lib/blf/version.rb +3 -0
- data/lib/blf/world.rb +126 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e54c1065e6517f9f6e64c0e3994ef812b1373d9c
|
4
|
+
data.tar.gz: 1a4c38daa0c783db021e1190793c429c6f379959
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 392df518a812c00ca6fafd8c1f419cd714360cc323da699500649be50e0db04c59cacee60d82649b2da394525e0c29bf598e72ff54a01e322f6c8bbfb8caea54
|
7
|
+
data.tar.gz: acee7b8cc51cb9394d0a409b0f333d1480ad5858da571e046ad7f7b1dd4816770e1c9c0a0263a761e853cbf4255343d6edb96a458fc75f2dc9c08a614e1c8fd1
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0-preview2
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Masami Yonehara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# BLF
|
2
|
+
|
3
|
+
詰め込み問題のBLF法の実装
|
4
|
+
|
5
|
+
This is an implementation of Bottom-Left method for 2-Dimensional Strip Packing Problem.
|
6
|
+
|
7
|
+

|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'blf'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install blf
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "blf"
|
27
|
+
|
28
|
+
# create the world.
|
29
|
+
world = BLF.create_world width: 500, height: 500
|
30
|
+
|
31
|
+
# add a block with coordinates.
|
32
|
+
world.add_block_with_location x: 60, y: 0, width: 50, height: 50
|
33
|
+
|
34
|
+
(1..50).each do |n|
|
35
|
+
w = Random.new(Random.new_seed).rand * 100 + 10
|
36
|
+
h = Random.new(Random.new_seed).rand * 100 + 10
|
37
|
+
# add a block without coordinates. this becomes a target of allocation.
|
38
|
+
world.add_block width: w, height: h
|
39
|
+
end
|
40
|
+
|
41
|
+
# do allocation to all the blocks without coordinates.
|
42
|
+
world.allocate_all
|
43
|
+
|
44
|
+
world.draw
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/blf.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "blf"
|
8
|
+
spec.version = Blf::VERSION
|
9
|
+
spec.authors = ["Masami Yonehara"]
|
10
|
+
spec.email = ["zeitdiebe@gmail.com"]
|
11
|
+
spec.description = %q{This is an implementation of Bottom-Left method to 2-Dimensional Strip Packing Problem.}
|
12
|
+
spec.summary = %q{This is an implementation of Bottom-Left method to 2-Dimensional Strip Packing Problem.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "rmagick", "~> 2.0"
|
22
|
+
spec.add_runtime_dependency "activesupport"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
data/example/example.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "blf"
|
2
|
+
require "pp"
|
3
|
+
|
4
|
+
# create the world.
|
5
|
+
world = BLF.create_world width: 500, height: 500
|
6
|
+
|
7
|
+
# add a block with coordinates.
|
8
|
+
world.add_block_with_location x: 60, y: 0, width: 50, height: 50
|
9
|
+
|
10
|
+
(1..50).each do |n|
|
11
|
+
w = Random.new(Random.new_seed).rand * 100 + 10
|
12
|
+
h = Random.new(Random.new_seed).rand * 100 + 10
|
13
|
+
# add a block without coordinates. this becomes a target of allocation.
|
14
|
+
world.add_block width: w, height: h
|
15
|
+
end
|
16
|
+
|
17
|
+
# do allocation to all the blocks without coordinates.
|
18
|
+
world.allocate_all
|
19
|
+
|
20
|
+
# `world.placed_blocks` return the coordinates of the allocated blocks.
|
21
|
+
pp world.placed_blocks[0]
|
22
|
+
|
23
|
+
world.draw
|
data/lib/blf.rb
ADDED
data/lib/blf/block.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "active_support/all"
|
2
|
+
|
3
|
+
|
4
|
+
module BLF
|
5
|
+
class Block
|
6
|
+
attr_accessor :start_x, :start_y, :width, :height, :end_x, :end_y, :world
|
7
|
+
|
8
|
+
def initialize(args)
|
9
|
+
@start_x = args[:start_x] || 0
|
10
|
+
@start_y = args[:start_y] || 0
|
11
|
+
@width = args[:width] || 0
|
12
|
+
@height = args[:height] || 0
|
13
|
+
@world = args[:world]
|
14
|
+
end
|
15
|
+
|
16
|
+
def end_x
|
17
|
+
(@start_x + @width).presence || 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def end_y
|
21
|
+
(@start_y + @height).presence || 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def draw
|
25
|
+
dr = Draw.new
|
26
|
+
dr.stroke = "#ccddff"
|
27
|
+
dr.fill = "rgb(#{random*255}, #{random*255}, #{random*255})"
|
28
|
+
dr.stroke_width 1
|
29
|
+
dr.rectangle @start_x, @start_y, end_x, end_y
|
30
|
+
dr.draw @world.image
|
31
|
+
end
|
32
|
+
|
33
|
+
def random
|
34
|
+
Random.new(Random.new_seed).rand
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/blf/test.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# ruby-1.9.3
|
2
|
+
|
3
|
+
input_times = gets.to_i
|
4
|
+
|
5
|
+
result = input_times.times.map do
|
6
|
+
input = gets.to_i
|
7
|
+
input.match /((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])[.]){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])/
|
8
|
+
end
|
9
|
+
|
10
|
+
r = result.map do |e|
|
11
|
+
case
|
12
|
+
when e == true then 'True'
|
13
|
+
when e == false then 'False'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
r.each {|e| print e}
|
data/lib/blf/version.rb
ADDED
data/lib/blf/world.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'rmagick'
|
2
|
+
include Magick
|
3
|
+
|
4
|
+
|
5
|
+
module BLF
|
6
|
+
class World
|
7
|
+
attr_reader :width, :height, :blocks, :image, :bl_stable_point_list, :unplaced_blocks, :placed_blocks
|
8
|
+
|
9
|
+
def initialize(args)
|
10
|
+
@width = args[:width]
|
11
|
+
@height = args[:height]
|
12
|
+
@unplaced_blocks = []
|
13
|
+
@placed_blocks = []
|
14
|
+
@bl_stable_point_list = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_block(args)
|
18
|
+
@unplaced_blocks << Block.new(width: args[:width], height: args[:height], world: self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_block_with_location(args)
|
22
|
+
@placed_blocks << Block.new(start_x: args[:x], start_y: args[:y], width: args[:width], height: args[:height], world: self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_bl_stable_point_candidates(new_block)
|
26
|
+
@bl_stable_point_list << { x: new_block.end_x, y: 0, width: 0, height: new_block.start_y }
|
27
|
+
@bl_stable_point_list << { x: 0, y: new_block.end_y, width: new_block.start_x, height: 0 }
|
28
|
+
|
29
|
+
@placed_blocks.each_with_index do |placed_block, index|
|
30
|
+
if (placed_block.end_x <= new_block.start_x) && (placed_block.end_y > new_block.end_y)
|
31
|
+
@bl_stable_point_list << { x: placed_block.end_x, y: new_block.end_y, width: new_block.start_x - placed_block.end_x, height: placed_block.start_y - new_block.end_y }
|
32
|
+
|
33
|
+
elsif (new_block.end_x <= placed_block.start_x) && (new_block.end_y > placed_block.end_y)
|
34
|
+
@bl_stable_point_list << { x: new_block.end_x, y: placed_block.end_y, width: placed_block.start_x - new_block.end_x, height: new_block.start_y - placed_block.end_y }
|
35
|
+
|
36
|
+
elsif (new_block.end_x < placed_block.end_x) && (placed_block.end_y <= new_block.start_y)
|
37
|
+
@bl_stable_point_list << { x: new_block.end_x, y: placed_block.end_y, width: placed_block.start_x - new_block.end_x, height: new_block.start_y - placed_block.end_y }
|
38
|
+
|
39
|
+
elsif (placed_block.end_x < new_block.end_x) && (new_block.end_y <= placed_block.start_y)
|
40
|
+
@bl_stable_point_list << { x: placed_block.end_x, y: new_block.end_y, width: new_block.start_x - placed_block.end_x, height: placed_block.start_y - new_block.end_x }
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def allocate new_block
|
47
|
+
@bl_stable_point_list.each do |point|
|
48
|
+
if stable?(point, new_block) &&
|
49
|
+
!overlapped?(start_x: point[:x], start_y: point[:y], width: new_block.width, height: new_block.height) &&
|
50
|
+
!beyond_area?(start_x: point[:x], start_y: point[:y], width: new_block.width, height: new_block.height)
|
51
|
+
|
52
|
+
new_block.start_x = point[:x]
|
53
|
+
new_block.start_y = point[:y]
|
54
|
+
@placed_blocks << new_block
|
55
|
+
break
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def allocate_all
|
61
|
+
# 左上端をブロックの組み合わせごとに追加するのは無駄なので、ここで一回だけ追加する。
|
62
|
+
@bl_stable_point_list << { x: 0, y: 0, width: 0, height: 0 }
|
63
|
+
|
64
|
+
@placed_blocks.each do |block|
|
65
|
+
add_bl_stable_point_candidates block
|
66
|
+
end
|
67
|
+
|
68
|
+
@unplaced_blocks.length.times do
|
69
|
+
# BL候補点の配列の順番通りに配置を試すので、Bottom-Leftの規則に沿って、yが小さい順に並べ替える。
|
70
|
+
@bl_stable_point_list.sort! {|a, b| a[:y] <=> b[:y] }
|
71
|
+
|
72
|
+
block = @unplaced_blocks.shift
|
73
|
+
allocate block
|
74
|
+
add_bl_stable_point_candidates block
|
75
|
+
self.draw
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def stable?(point, current_block)
|
80
|
+
if point[:width] <= 0
|
81
|
+
current_block.height >= point[:height]
|
82
|
+
elsif point[:height] <= 0
|
83
|
+
current_block.width >= point[:width]
|
84
|
+
else
|
85
|
+
current_block.width >= point[:width] && current_block.height >= point[:height]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def overlapped?(args)
|
90
|
+
start_x = args[:start_x]
|
91
|
+
start_y = args[:start_y]
|
92
|
+
end_x = args[:start_x] + args[:width]
|
93
|
+
end_y = args[:start_y] + args[:height]
|
94
|
+
|
95
|
+
@placed_blocks.map do |block|
|
96
|
+
block.start_x < end_x &&
|
97
|
+
block.start_y < end_y &&
|
98
|
+
start_x < block.end_x &&
|
99
|
+
start_y < block.end_y
|
100
|
+
end.any?
|
101
|
+
end
|
102
|
+
|
103
|
+
def beyond_area?(args)
|
104
|
+
end_x = args[:start_x] + args[:width]
|
105
|
+
end_y = args[:start_y] + args[:height]
|
106
|
+
!(end_x <= self.width && end_y <= self.height)
|
107
|
+
end
|
108
|
+
|
109
|
+
def draw
|
110
|
+
@image = Image.new(@width, @height) do
|
111
|
+
self.background_color = 'white'
|
112
|
+
end
|
113
|
+
|
114
|
+
@placed_blocks.each {|block| block.draw }
|
115
|
+
|
116
|
+
@bl_stable_point_list.compact.each do |point|
|
117
|
+
dr = Draw.new
|
118
|
+
dr.fill = "black"
|
119
|
+
dr.ellipse(point[:x], point[:y], 1, 1, 0, 360)
|
120
|
+
dr.draw @image
|
121
|
+
end
|
122
|
+
|
123
|
+
@image.write("example.jpg")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masami Yonehara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: This is an implementation of Bottom-Left method to 2-Dimensional Strip
|
98
|
+
Packing Problem.
|
99
|
+
email:
|
100
|
+
- zeitdiebe@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".ruby-version"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- blf.gemspec
|
112
|
+
- example/example.rb
|
113
|
+
- lib/blf.rb
|
114
|
+
- lib/blf/block.rb
|
115
|
+
- lib/blf/test.rb
|
116
|
+
- lib/blf/version.rb
|
117
|
+
- lib/blf/world.rb
|
118
|
+
homepage: ''
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.0
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: This is an implementation of Bottom-Left method to 2-Dimensional Strip Packing
|
142
|
+
Problem.
|
143
|
+
test_files: []
|