forked from tinvukhac/spatialdatagenerators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.rb
248 lines (223 loc) · 7.15 KB
/
generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env ruby
# Copyright 2019 University of California, Riverside
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Base class for the generator
class Generator
# Initialize the random number generator and the two common parameters
def initialize(card, d = 2)
@random = Random.new
@card = card
@d = d
end
# Generates a random value in the range [0, 1)
def rnd
@random.rand
end
# Generates either 0 or 1 from a Bernoulli distribution with parameter p
def bernoulli(p)
rnd <= p ? 1 : 0
end
# Generates a number from a uniform distribution U(a, b)
def uniform(a, b)
(b - a) * rnd + a
end
# Generates a number from the normal (Gaussian) distribution with mean mu and standard deviation sigma
def normal(mu, sigma)
mu + sigma * Math::sqrt(-2 * Math::log(rnd))*Math::sin(2 * Math::PI * rnd)
end
end
# Abstract class for the point generators (the first five generators)
class PointGenerator < Generator
# Initialize one of the first five generators that are based on points
# In addition to the two common parameters, the first two specific parameters are always maxWidth and maxHeight for generating rectangles
def initialize(card, d, maxWidth, maxHeight)
super(card, d)
@maxWidth = maxWidth
@maxHeight = maxHeight
end
# Generates all the rectangles by first generating points and then generating rectangles around these points
def generate
g = []
i = 0
prevPoint = nil
while (i < @card)
# Call the abstract generatePoint function
x, y = generatePoint(prevPoint, i)
if (pointInSpace(x, y))
prevPoint = [x, y]
w = uniform(0, @maxWidth)
h = uniform(0, @maxHeight)
g << [x - w / 2, y - h / 2, w, h]
i += 1
end
end
g
end
def pointInSpace(x, y)
x >= 0.0 && x <= 1.0 && y >= 0.0 && y <= 1.0
end
end
# Generates points from the uniform distribution in the space [(0,0), (1,1)]
class UniformGenerator < PointGenerator
def generatePoint(prevPoint, i)
[rnd, rnd]
end
end
# Generates points from the diagonal distribution
class DiagonalGenerator < PointGenerator
def initialize(card, d, maxWidth, maxHeight, percentage, buffer)
super(card, d, maxWidth, maxHeight)
@percentage = percentage
@buffer = buffer
end
# Generate a point from a diagonal distribution
def generatePoint(prevPoint, i)
if (bernoulli(@percentage) == 1)
# Generate a point exactly on the diagonal
x = y = uniform(0, 1)
else
# Deviate a little bit from the diagonal
c = uniform(0, 1)
d = normal(0, @buffer / 5)
x = c + d / Math::sqrt(2)
y = c - d / Math::sqrt(2)
end
return x, y
end
end
# Generates points from the Gaussian distribution
class GaussianGenerator < PointGenerator
# Generate a point from the Gaussian distribution
def generatePoint(prevPoint, i)
x = normal(0.5, 0.1)
y = normal(0.5, 0.1)
return x, y
end
end
# Generates points from the Sierpinsky distribution
class SierpinskyGenerator < PointGenerator
# Generate a point using the Sierpinsky's triangle
def generatePoint(prevPoint, i)
case i
when 0
[0.0, 0.0]
when 1
[1.0, 0.0]
when 2
[0.5, Math::sqrt(3) / 2]
else
case dice(5)
when 1..2
middlePoint(prevPoint, [0.0, 0.0])
when 3..4
middlePoint(prevPoint, [1.0, 0.0])
when 5
middlePoint(prevPoint, [0.5, Math::sqrt(3) / 2])
end
end
end
# Generates a random integer number in the range [1, n]
def dice(n)
(rnd * n).floor + 1
end
# Computes the middle point between two points
def middlePoint(point1, point2)
[(point1[0] + point2[0]) / 2.0, (point1[1] + point2[1]) / 2.0]
end
end
# Generates points from the bit distribution
class BitGenerator < PointGenerator
def initialize(card, d, maxWidth, maxHeight, p, digits)
super(card, d, maxWidth, maxHeight)
@p = p
@digits = digits.to_i
end
def generatePoint(prevPoint, i)
[generateBit, generateBit]
end
def generateBit
n = 0
i = 1
@digits.times do
c = bernoulli(@p)
n += c * 1.0 / (1 << i)
i += 1
end
n
end
end
class ParcelGenerator < Generator
def initialize(card, d, r, alpha)
super(card, d)
@r = r
@alpha = alpha
end
def generate
# Initial box
box = [0.0, 0.0, 1.0, 1.0]
g = [box]
# Generate the initial parcels by splitting the box @card - 1 times
while g.length < @card
box = g.slice!(0)
# if box width > box height
if (box[2] > box[3])
split_size = box[2] * uniform(@r, 1-@r)
box1 = [box[0], box[1], split_size, box[3]]
box2 = [box[0] + split_size, box[1], box[2] - split_size, box[3]]
else
split_size = box[3] * uniform(@r, 1-@r)
box1 = [box[0], box[1], box[2], split_size]
box2 = [box[0], box[1] + split_size, box[2], box[3] - split_size]
end
g << box1
g << box2
end
# Add noise using dithering
g.map do |box|
[box[0], box[1], box[2] * (1-uniform(0, @alpha)), box[3] * (1- uniform(0, @alpha))]
end
end
end
if __FILE__ == $0
if (ARGV.length < 5)
$stderr.puts "Usage: #{__FILE__} <distribution> <cardinality> <dimensions> [distribution specific parameters]"
$stderr.puts "The available distributions are: {uniform, diagonal, gaussian, sierpinsky, bit, parcel}"
$stderr.puts "cardinality: The number of records to generate"
$stderr.puts "dimensions: The dimensionality of the generated geometries. Currently, on two-dimensional data is supported."
$stderr.puts "Refer to the gem description for the model specific parmaeters"
exit 1
end
# Command line interface (CLI)
generatorType = ARGV.slice!(0)
# Convert all remaining parameters to floating point
ARGV.map!(&:to_f)
generator = case generatorType
when "uniform"
UniformGenerator.new(*ARGV.slice!(0..3))
when "diagonal"
DiagonalGenerator.new(*ARGV.slice!(0..5))
when "gaussian"
GaussianGenerator.new(*ARGV.slice!(0..3))
when "sierpinsky"
SierpinskyGenerator.new(*ARGV.slice!(0..3))
when "bit"
BitGenerator.new(*ARGV.slice!(0..5))
when "parcel"
ParcelGenerator.new(*ARGV.slice!(0..5))
end
geometries = generator.generate
for geometry in geometries
x1, y1, x2, y2 = geometry[0], geometry[1], geometry[0] + geometry[2], geometry[1] + geometry[3]
puts ("POLYGON ((%f %f, %f %f, %f %f, %f %f, %f %f))" % [
x1, y1, x2, y1, x2, y2, x1, y2, x1, y1
])
end
end