Class: Doodl::ISOMLayout

Inherits:
Layout
  • Object
show all
Defined in:
lib/layout/isom_layout.rb

Instance Attribute Summary

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) ⇒ ISOMLayout

Returns a new instance of ISOMLayout.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/layout/isom_layout.rb', line 10

def initialize(view)
  super(view)
  @interval = 0
  @finished = false
  @max_epoch = 700
  @initial_adaption = 0.9
  @min_adaption = 0
  @cooling_factor = 2.0
  @finished = false

  @epoch = 1
  @interval = 100
  @radius = 5
  @minimal_radius = 1
  @adaption = @initial_adaption
end

Instance Method Details

#adjust(graph) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/layout/isom_layout.rb', line 59

def adjust(graph)
  rloc = Location.new(rand(@view.width), rand(@view.height))
  winner = graph.nodes.min { |a, b|  @locations[a].dist(rloc) <=> @locations[b].dist(rloc) }
  dfs = DepthFirstSearch.new(graph, winner)
  graph.each_node do |node|
    nloc = @locations[node]
    distance = dfs.dist[node]
    if (distance < @radius)
      delta_x = 2 ** (-distance) * @adaption*(nloc.x-rloc.x)
      delta_y = 2 ** (-distance) * @adaption*(nloc.y-rloc.y)
      delta = Location.new(delta_x, delta_y)
      @locations[node] -= delta
    end
  end
end

#calc_adaption(graph) ⇒ Object



55
56
57
# File 'lib/layout/isom_layout.rb', line 55

def calc_adaption(graph)
  @adaption = [@min_adaption, @initial_adaption* Math.exp(-1 * @cooling_factor.to_f*(@epoch.to_f/@max_epoch))].max
end

#dolayout(graph) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/layout/isom_layout.rb', line 27

def dolayout(graph)
  $LOG.info "Started ISOMLayout" if $LOG
  setup(graph)
  changed
  notify_observers(self)
  while (@epoch <= @max_epoch)
    step(graph)
    @epoch += 1
    changed
    notify_observers(self)
  end
  $LOG.info "Finished ISOMLayout" if $LOG
end

#setup(graph) ⇒ Object



41
42
43
44
45
46
# File 'lib/layout/isom_layout.rb', line 41

def setup(graph)
  random = RandomLayout.new(@view)
  random.graph=(graph)
  random.layout
  @locations = random.locations
end

#step(graph) ⇒ Object



48
49
50
51
52
53
# File 'lib/layout/isom_layout.rb', line 48

def step(graph)
  adjust(graph)
  calc_adaption(graph)
  @iterations += 1
  @radius -= 1 if (@epoch % @interval == 0)
end