Class: LocalRouting

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/routing/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(baseNode, network, neighborDepth = 2) ⇒ LocalRouting

Returns a new instance of LocalRouting.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/appswarm/routing/routing.rb', line 223

def initialize(baseNode,network,neighborDepth=2)
  puts "init local routing for #{baseNode}"
  @baseNode=baseNode
  @network=network
  @depth=neighborDepth
  @neighbors=@baseNode.neighborHood(@depth)+[@baseNode]
  
  @distMatrix=@network.createMatrix(@neighbors)
  @distMatrixInv=Linalg::DMatrix[*@distMatrix].pseudo_inverse
  
  @base=PCA::Base.new(PCA::Input.new(@distMatrix))
    
  @base.reduce([@base.dimension,8].min)
  
  @routing=@network.computeRoutingMatrix(@baseNode,@neighbors) #-[@baseNode])
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



222
223
224
# File 'lib/appswarm/routing/routing.rb', line 222

def base
  @base
end

#neighborsObject (readonly)

Returns the value of attribute neighbors.



222
223
224
# File 'lib/appswarm/routing/routing.rb', line 222

def neighbors
  @neighbors
end

Instance Method Details

#computePosForDistances(distHash) ⇒ Object



280
281
282
283
# File 'lib/appswarm/routing/routing.rb', line 280

def computePosForDistances(distHash)
  distVec=@neighbors.map{|n|distHash[n]}
  @base.compress(distVec)
end

#distance(from, to) ⇒ Object



285
286
287
288
# File 'lib/appswarm/routing/routing.rb', line 285

def distance(from,to)
  assert{[from,to]-@neighbors==[]}
  from.distance(to)
end

#getDistancesForPos(toNodePos) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/appswarm/routing/routing.rb', line 271

def getDistancesForPos(toNodePos)
  distVec=base.decompress(toNodePos).to_a[0]
  nodes={}
  0.upto(distVec.length-1){|i|
    nodes[@neighbors[i]]=distVec[i]
  }
  nodes
end

#getNode(i) ⇒ Object



255
256
257
# File 'lib/appswarm/routing/routing.rb', line 255

def getNode(i)
  neighbors[i]
end

#getNodePos(neighbor) ⇒ Object



240
241
242
243
244
245
# File 'lib/appswarm/routing/routing.rb', line 240

def getNodePos(neighbor)
  assert{@neighbors.member?(neighbor)}
  distVec=@distMatrix[@neighbors.index(neighbor)]
  #pp distVec
  @base.compress(distVec)
end

#getRouteOrder(nodePos) ⇒ Object



259
260
261
262
263
264
# File 'lib/appswarm/routing/routing.rb', line 259

def getRouteOrder(nodePos)
  rv=routeVector(nodePos)
  ns=@baseNode.neighbors
  #puts "RV",rv,"neigbors:",ns
  ns.sort{|a,b| rv[ns.index(b)]<=>rv[ns.index(a)]}
end

#routeVector(nodePos) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/appswarm/routing/routing.rb', line 247

def routeVector(nodePos)
  #puts "ROUTEV",nodePos
  r0=(@distMatrixInv*@base.decompress(nodePos).transpose)
  #puts "r0",r0
  #puts @routing
  @routing*r0
end

#routeVectorDirect(neighbor) ⇒ Object



266
267
268
269
# File 'lib/appswarm/routing/routing.rb', line 266

def routeVectorDirect(neighbor)
  id=@neighbors.index(neighbor)
  @routing*Linalg::DMatrix[[0]*(id)+[1]+[0]*(@neighbors.length-id-1)].transpose
end