Class: Doodl::KKLayout
Constant Summary
collapse
- K =
1.0
- EPSILON =
0.1
- LENGHT_FACTOR =
0.9
- MAXITERATIONS =
2000
- DISCONNECTED_MULTIPLIER =
0.5
Instance Attribute Summary collapse
Attributes inherited from Layout
#graph, #iterations, #locations
Instance Method Summary
collapse
Methods inherited from Layout
#center_x, #center_y, #deep_copy, #finished?, #get_nearest_node, #get_nearest_node_within, #init, #layout, #move, #rotate, #set_location
Constructor Details
#initialize(view) ⇒ KKLayout
Returns a new instance of KKLayout.
19
20
21
22
23
|
# File 'lib/layout/kk_layout.rb', line 19
def initialize(view)
super(view)
@adjust_for_gravity = true
@exchange_vertices = true
end
|
Instance Attribute Details
#dm ⇒ Object
Returns the value of attribute dm.
11
12
13
|
# File 'lib/layout/kk_layout.rb', line 11
def dm
@dm
end
|
Instance Method Details
#adjust_for_gravity ⇒ Object
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/layout/kk_layout.rb', line 183
def adjust_for_gravity
gcenter = Location.new(0, 0)
@locations.each_value do |loc|
gcenter += loc
end
gcenter /= @locations.size
@locations.each_pair do |key, value|
@locations[key] = value + (@view.center - gcenter)
end
end
|
#calculate_delta_m(m) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/layout/kk_layout.rb', line 113
def calculate_delta_m(m)
dEdm = Location.new(0, 0)
@nodes.each do |i|
if (i != m)
dist = dm[[m,i]]
l_mi = @L * dist
k_mi = K / (dist * dist)
delta = locations[m] - locations[i]
d = delta.length
common = k_mi * (1 - l_mi / d)
dEdm += delta * common
end
end
return dEdm.length
end
|
#calculate_delta_xy(m) ⇒ Object
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
|
# File 'lib/layout/kk_layout.rb', line 130
def calculate_delta_xy(m)
dE_dxm = 0
dE_dym = 0
d2E_d2xm = 0
d2E_dxmdym = 0
d2E_dymdxm = 0
d2E_d2ym = 0
@nodes.each do |i|
if (i != m)
dist = dm[[m, i]]
l_mi = @L * dist
k_mi = K / (dist * dist)
d = locations[m] - locations[i]
dist = d.length
ddd = dist * dist * dist
dE_dxm += k_mi * (1 - l_mi / dist) * d.x
dE_dym += k_mi * (1 - l_mi / dist) * d.y
d2E_d2xm += k_mi * (1 - l_mi * d.y * d.y / ddd)
d2E_dxmdym += k_mi * l_mi * d.x * d.y / ddd
d2E_d2ym += k_mi * (1 - l_mi * d.x * d.x / ddd)
end
end
d2E_dymdxm = d2E_dxmdym
denomi = d2E_d2xm * d2E_d2ym - d2E_dxmdym * d2E_dymdxm
delta_x = (d2E_dxmdym * dE_dym - d2E_d2ym * dE_dxm) / denomi
delta_y = (d2E_dymdxm * dE_dxm - d2E_d2xm * dE_dym) / denomi
Location.new(delta_x, delta_y)
end
|
#calculate_energy ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/layout/kk_layout.rb', line 97
def calculate_energy
energy = 0
walk do |i, j|
dist = dm[[i, j]];
l_ij = @L * dist;
k_ij = K / (dist * dist)
delta = locations[i] - locations[j]
d = delta.length
energy += k_ij / 2 * (delta.x * delta.x + delta.y * delta.y + l_ij * l_ij -
2 * l_ij * d)
end
return energy
end
|
#calculate_energy_if_exchanged(p, q) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/layout/kk_layout.rb', line 163
def calculate_energy_if_exchanged(p, q)
energy = 0
walk do |i, j|
ii = i;
jj = j;
ii = q if (i == p)
jj = p if (j == q)
dist = dm[[i,j]]
l_ij = @L * dist
k_ij = K / (dist * dist)
delta = locations[ii] - locations[jj]
d = Math.sqrt(delta.x * delta.x + delta.y * delta.y)
energy += k_ij / 2 * (delta.x * delta.x + delta.y * delta.y + l_ij * l_ij -
2 * l_ij * d)
end
return energy
end
|
#dolayout(graph) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/layout/kk_layout.rb', line 25
def dolayout(graph)
$LOG.info "Started KKLayout" if $LOG
setup(graph)
while (not @stabilized and @iterations < MAXITERATIONS)
step(graph)
@iterations += 1
changed
notify_observers(self)
end
$LOG.info "Finished KKLayout" if $LOG
end
|
#setup(graph) ⇒ Object
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
|
# File 'lib/layout/kk_layout.rb', line 37
def setup(graph)
initial = CircleLayout.new(@view)
initial.graph = graph
initial.layout
@locations = initial.locations
changed
notify_observers(self)
$LOG.info "Finished Randomizing"
@stabilized = false
@nodes = graph.nodes
japsp = JohnsonAllPairShortestPaths.new(graph)
distance = japsp.dist
diameter = japsp.diameter
@L0 = [@view.width, @view.height].min
@L = @L0 / diameter * LENGHT_FACTOR
@dm = {}
walk do |i, j|
d_ij = distance[[i, j]]
d_ji = distance[[j, i]]
d = [diameter * DISCONNECTED_MULTIPLIER, d_ij, d_ji].min
@dm[[i, j]] = d
@dm[[j, i]] = d
end
end
|
#step(graph) ⇒ Object
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
|
# File 'lib/layout/kk_layout.rb', line 65
def step(graph)
energy = calculate_energy
old_locations = @locations.clone
pm = @nodes.max { |x, y| calculate_delta_m(x) <=> calculate_delta_m(y) }
i = 0
100.times do |i|
@locations[pm] = @locations[pm] + calculate_delta_xy(pm)
deltam = calculate_delta_m(pm)
break if (deltam < EPSILON)
end
adjust_for_gravity if @adjust_for_gravity
if (@exchange_vertices and calculate_delta_m(pm) < EPSILON)
energy = calculate_energy
walk do |i, j|
xenergy = calculate_energy_if_exchanged(i, j)
if (energy > xenergy)
li = locations[i]
lj = locations[j]
temp = li
li = lj
lj = temp
break
end
end
end
diff = 0
@locations.each_key { |key| diff += (@locations[key] - old_locations[key]).length }
@stabilized = true if (diff < graph.num_nodes)
end
|