Class: Nodes
- Inherits:
-
Object
- Object
- Nodes
- Defined in:
- lib/appswarm/routing/routing.rb
Defined Under Namespace
Classes: Path
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #computeDistances ⇒ Object
-
#computeRoutingMatrix(myNode, nodes = nil) ⇒ Object
R*v=P v is (0,..,0,1,0,…,0) vector having 1 at pos i P is the vector (over j) describing the probability for reaching node i over neighbor j example: to compute the probabilities to reach node 4, compute R*(0,0,0,0,1,0,…) result may be (0.5,0,0.5) - this means that you can reach node 4 by either crossing neighbor 0 or 2 (counting from 0).
- #connectUntilAllConnected(connectivity) ⇒ Object
- #createDistances(fromArray, toNode) ⇒ Object
- #createMatrix(nodes = nil) ⇒ Object
-
#initialize(n = 10, connectivity = 2) ⇒ Nodes
constructor
A new instance of Nodes.
- #shortestPath(from, to) ⇒ Object
- #weight(i) ⇒ Object
Constructor Details
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
65 66 67 |
# File 'lib/appswarm/routing/routing.rb', line 65 def nodes @nodes end |
Instance Method Details
#computeDistances ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/appswarm/routing/routing.rb', line 82 def computeDistances puts "Compute distances..." @nodes.each{|n| cur=[] cur+=n.neighbors while cur.length>0 c=cur.shift (c.neighbors-[n]).each{|x| nd=n.distance(c)+1 if n.setDistance(x,nd) cur<<x end } end } puts "Ready" end |
#computeRoutingMatrix(myNode, nodes = nil) ⇒ Object
R*v=P v is (0,..,0,1,0,…,0) vector having 1 at pos i P is the vector (over j) describing the probability for reaching node i over neighbor j example: to compute the probabilities to reach node 4, compute
R*(0,0,0,0,1,0,...)
result may be (0.5,0,0.5) - this means that you can reach node 4 by either crossing neighbor 0 or 2 (counting from 0)
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 |
# File 'lib/appswarm/routing/routing.rb', line 188 def computeRoutingMatrix(myNode,nodes=nil) #nodes||=(@nodes-myNode) nodes||=@nodes #assert{ !nodes.member?(myNode) } neighbors=myNode.neighbors #puts "---",neighbors,"--" i=[[0]*nodes.length]*neighbors.length #nodes.each{|x|puts "#{x} #{myNode.distance(x)}"} matrix=Linalg::DMatrix[* i] nodes.each{|curNode| # assert{curNode!=myNode} curArr={} neighbors.each{|neighbor| d=neighbor.distance(curNode)+myNode.distance(neighbor) myd=myNode.distance(curNode) if d<=myd curArr[neighbor]=d end } sum=curArr.values.inject(0){|a,b|a+b} #assert{sum>0} if sum>0 curArr.each{|k,v| y,x=nodes.index(curNode),neighbors.index(k) matrix[x,y]=v.to_f/sum } end } matrix end |
#connectUntilAllConnected(connectivity) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/appswarm/routing/routing.rb', line 72 def connectUntilAllConnected(connectivity) @nodes.each{|n| some=(@nodes-[n]).shuffle some[0...connectivity].each{|o| o.add(n) n.add(o) } } end |
#createDistances(fromArray, toNode) ⇒ Object
175 176 177 178 179 |
# File 'lib/appswarm/routing/routing.rb', line 175 def createDistances(fromArray,toNode) fromArray.map{|a| weight(a.distance(toNode)) } end |
#createMatrix(nodes = nil) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/appswarm/routing/routing.rb', line 161 def createMatrix(nodes=nil) nodes||=@nodes x=nodes.map{|a| nodes.map{|b| weight(a.distance(b)) } } end |
#shortestPath(from, to) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/appswarm/routing/routing.rb', line 136 def shortestPath(from,to) paths=[Path.new(from)] loop do currentPath=paths.shift last=currentPath.last return currentPath if last==to last.neighbors.each{|n| nPath=currentPath.clone nPath << n if nPath.value < MYMAX paths << nPath end #puts currentPath,nPath #exit } paths.sort!{|a,b|a.value<=>b.value} #puts paths,"--" # puts paths.length end return nil end |
#weight(i) ⇒ Object
170 171 172 173 |
# File 'lib/appswarm/routing/routing.rb', line 170 def weight(i) i #1-Math::atan(i)/Math::PI end |