我新调试好的程序如下,应该是对的,供参考8 Y" E- I8 M4 t. u' o; ?) r
因为特殊要求,这里的边的长度是二维座标内的空间距离9 x+ x3 S& ?- O
9 \" ?7 x! B" N6 L4 s4 t: ?9 d' L: p6 G
to find-distance
) b0 y7 k. u- i8 B7 \& V9 V let i 0" q# _2 w/ U0 |. t1 l. y' i6 u" v
let j 0
; r/ K% L: q# {" D3 B let nodedistance 0
6 k; A# E7 b" V let linknumber 0+ V! `! P6 F; ^+ b; D c5 I- y
( M; t8 E! Y) Y6 S
while [j < count turtles]
3 h' N+ V& ^: C, G; N: B: N [- ^$ b6 W5 Z+ ?$ X: }. g/ L7 y3 }
while [i < count turtles]0 I8 R% v \8 o* X
[ if link-neighbor? turtle i. y3 w3 e9 @0 j& R, M4 I0 G
[ask turtle j [set nodedistance nodedistance + distance turtle i]
+ s- a" o, t H' ? n7 | ask turtle j [set linknumber linknumber + count [link-neighbors] of turtle i]
# \1 w5 u- u6 k; z ]. |' m( L% I& L1 J& A- g8 P9 `
set i i + 1
5 a x5 f; z& y" c ]
& I _6 J; S2 }: b$ u* h set j j + 1
7 M, R/ f [. U. v! t: A6 ? l+ L ]
z+ P3 H8 P" g1 k( D, U! w5 o set average-path-length nodedistance / (linknumber * 2)
+ j3 {3 S, N- wend |