设为首页收藏本站

最大的系统仿真与系统优化公益交流社区

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11668|回复: 0

[求助] 在看例子时有几个问题看不懂

[复制链接]
发表于 2012-6-20 08:59:43 | 显示全部楼层 |阅读模式
新手,在看netlogo自带的例子时,有几行程序看不懂,希望高手能够指点。
* K) M/ F) N2 G  k( v, \netlogo自带的social science--traffic grid这一例子当中,
& g, b7 B# h* g8 }globals
4 W. C% ~) ^1 K9 u9 e% A[6 L, ~6 z0 p8 u; p" u6 L( k
  grid-x-inc               ;; the amount of patches in between two roads in the x direction
2 \6 G( ^1 Y1 w9 E1 X  grid-y-inc               ;; the amount of patches in between two roads in the y direction. X' \+ s3 d: t
  acceleration             ;; the constant that controls how much a car speeds up or slows down by if/ ]1 C( z' f4 q. d& D* @( a
                           ;; it is to accelerate or decelerate: n3 Z& q% p3 Q8 |  v6 c/ e; P# V
  phase                    ;; keeps track of the phase
/ v4 W" U+ l( m% j( D0 S  num-cars-stopped         ;; the number of cars that are stopped during a single pass thru the go procedure
4 X' Z0 ^) s- R, [  current-light            ;; the currently selected light! E; b7 P' a" u. U( i$ i0 V; N
7 W4 I7 Y6 x2 X- s( N1 o
  ;; patch agentsets
& ~! H  [" O# E* e  intersections ;; agentset containing the patches that are intersections
+ ^' l& r% B8 s4 t) O. x" h  roads         ;; agentset containing the patches that are roads
  i; c3 x+ n% m' q9 n]
0 t. L; l: r8 s" [3 D) W( V1 r! a& g3 z& R( V! ]: E
turtles-own* `, U2 A( N0 a1 t' I
[
6 x( n1 v7 [8 w; A  speed     ;; the speed of the turtle6 V+ n# U. N& R& c- g  I
  up-car?   ;; true if the turtle moves downwards and false if it moves to the right  g- L! P2 C3 b1 j) ~# o: ~4 c2 F3 _
  wait-time ;; the amount of time since the last time a turtle has moved5 k* _& \+ u9 P# z' i" ~% K. R
]" d' C: s7 P2 k8 }! X& `6 u. I6 i

% j7 Y5 m5 ^/ O, Y0 ~6 Npatches-own
  g5 _2 R* }% B; i" w" |2 X[
  ~! [" X$ c7 M+ v  intersection?   ;; true if the patch is at the intersection of two roads/ Q4 @/ e9 ?. G' u# W
  green-light-up? ;; true if the green light is above the intersection.  otherwise, false.
3 m( T& F( ^& @" w2 Q                  ;; false for a non-intersection patches.
& W) j, A$ Z+ X$ q& V0 C" m  my-row          ;; the row of the intersection counting from the upper left corner of the/ a$ ]$ c5 y' o& r$ R! r, [+ B
                  ;; world.  -1 for non-intersection patches.% I" S2 e3 ?- p
  my-column       ;; the column of the intersection counting from the upper left corner of the
0 C* |- _3 ^) [& i                  ;; world.  -1 for non-intersection patches.! Q/ D- s' B! F" N  b
  my-phase        ;; the phase for the intersection.  -1 for non-intersection patches.; p* O- R3 a$ p9 Z& A6 l
  auto?           ;; whether or not this intersection will switch automatically.* }5 _' T7 @7 u2 }' G3 P1 K  @! }
                  ;; false for non-intersection patches.
9 a* @# G0 V3 h% `; |]
- Y5 q3 ~1 _9 E) U' x; z
7 M; z% ?- h; \6 f" r  r
3 L% X1 }2 O& w; w;;;;;;;;;;;;;;;;;;;;;;4 a, F6 p; S: u8 P+ r6 u! m2 E" r& `$ F
;; Setup Procedures ;;
& G2 C8 r: t. F, C* V, r1 x/ a& _;;;;;;;;;;;;;;;;;;;;;;, d! o# Z/ a  z  G4 T

2 F# p6 _. m& L' e: O;; Initialize the display by giving the global and patch variables initial values.& F! s8 N! d3 A# E- x
;; Create num-cars of turtles if there are enough road patches for one turtle to. ^3 [" t) ~3 c
;; be created per road patch. Set up the plots.  J. j+ v/ R" T0 b/ z
to setup4 a8 p3 J4 C- K7 b/ p6 V3 J: q
  ca
# ?( Z0 U: P. B( B  setup-globals
' p, Q) u- i. ?+ B1 e8 \2 x5 q0 k4 @4 y' d
  ;; First we ask the patches to draw themselves and set up a few variables
" P% R2 [7 k, G* j9 q! ?& _  setup-patches
' O) l' f$ R( S6 P2 F* Z  C  make-current one-of intersections
1 x; z) c; J' x1 @. _& a3 }9 d  label-current  C& N8 Q- V# d* f* A: I
3 l8 ]+ ^; a9 V) \8 O% p
  set-default-shape turtles "car"
* V+ X: I0 M. N& g* p# k8 N% i) A+ ^1 R" v6 U
  if (num-cars > count roads): v% P+ l$ i6 l4 q& c
  [
% Q! [+ E" v, q. K5 G$ F8 S    user-message (word "There are too many cars for the amount of ": J& v! P1 h) Q5 c9 C) N
                       "road.  Either increase the amount of roads "
# w3 i6 R! r3 m3 J' m; v0 D! H) [                       "by increasing the GRID-SIZE-X or "' r( L0 e4 h' P
                       "GRID-SIZE-Y sliders, or decrease the "
. s/ y; G, B; D- }; K                       "number of cars by lowering the NUMBER slider.\n"
$ a! o. b$ A6 e+ s1 C                       "The setup has stopped.")/ A! k" A9 v1 k7 w
    stop
  R6 Y1 G3 b! h  ]
! B7 M4 w- a+ |$ {
* j/ `3 B& g1 P" g! r  ;; Now create the turtles and have each created turtle call the functions setup-cars and set-car-color
. {% ^1 U0 \$ J- T8 \0 U: R  crt num-cars
+ I6 R+ H# U7 X# g  [
8 p0 n9 k9 `3 Y) t  I$ V# L, U5 y    setup-cars
7 E/ G  E5 X( n# C7 y2 k% m    set-car-color
' U5 f9 H2 r, g6 C    record-data- X$ U/ A# X! a' A+ Z9 Q
  ]
  {+ d' V' O& L, U% L3 M" N. n2 x0 J0 p( s# v
  ;; give the turtles an initial speed. i1 X  H6 T2 c- U
  ask turtles [ set-car-speed ]
% N8 M4 z/ O* m6 |. j  R0 l& Z+ x; H/ v' i5 R; q4 w# i9 `/ e
  reset-ticks
, F" b0 @( U3 v' L, d8 Oend: y( R# o8 P* g; K0 Q

8 F& ^# C; F: m6 L* v;; Initialize the global variables to appropriate values. X9 x: S# a" s& E1 r( ?0 Y3 T
to setup-globals
$ Y- g" f% _1 D, D) \) Y  set current-light nobody ;; just for now, since there are no lights yet7 |/ q- e6 f/ l# R3 C& V
  set phase 0* M- E9 C% L. l
  set num-cars-stopped 0; w4 h' P! A9 E! L% [. ^  {: X
  set grid-x-inc world-width / grid-size-x
9 A& D+ C5 y4 `% B0 g, ?4 O  set grid-y-inc world-height / grid-size-y
. N3 b& F: q5 K
: l+ e1 x3 I4 z  ;; don't make acceleration 0.1 since we could get a rounding error and end up on a patch boundary' T  K3 E" @6 M, J' O! m7 m
  set acceleration 0.099
" u' g! R! z* ?( oend
. u/ p5 ~7 ^! U  N9 g0 b: Y. E" _# }  u+ H8 N5 m
;; Make the patches have appropriate colors, set up the roads and intersections agentsets,
  O! i& S  ~/ B;; and initialize the traffic lights to one setting) T( u% @, w( |; y7 i
to setup-patches% p% Z# J, c, q, E: O. h
  ;; initialize the patch-owned variables and color the patches to a base-color
, v7 Y6 {* Z# ]2 g  o) o  ask patches
( F" V) W+ p( h  [
9 A1 ?7 g1 y4 R/ z$ Y- v    set intersection? false4 u' ^2 q; Z5 o, s# n$ G
    set auto? false& k! E7 K3 J: }# {7 `, s, t) O
    set green-light-up? true  r. W1 ^" f5 I) f" A
    set my-row -1
3 B: R3 I( y6 h% I2 P    set my-column -1
( d, W' X* Y/ x% I1 m    set my-phase -13 g4 V. f! f& G3 b% [! N0 Q
    set pcolor brown + 3
, [% E5 V, S. B' F0 W% g+ j7 s# A* ]8 Q  ]
% q* a5 M1 n2 ^1 Q, k, t5 e2 d/ P) Z6 T( C8 y- T
  ;; initialize the global variables that hold patch agentsets4 i* I9 |5 @3 z0 ]" g9 l5 h
  set roads patches with
: s5 \3 z8 N9 t$ z0 d5 \    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
! V9 q, D4 I1 t' k    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]2 X5 {" A# {- R6 K% K
  set intersections roads with
* p7 ~& S6 X7 Y+ b    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and8 n' o: g$ s% H
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]
8 v. z% z9 Q. K( Q* [0 T
. Z# }8 V( Y" w1 O  ask roads [ set pcolor white ]) q8 Q: L4 w9 j/ c3 c
    setup-intersections0 k. X: y: ?+ z1 N5 N2 R  A) K3 Y
end% N5 _5 ^3 U8 G& P
其中定义道路的句子,如下所示,是什么意思啊?
8 t$ G/ w  A" B3 U9 o% X* c set roads patches with
$ v9 }( n- s9 |" w5 X    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or) H0 H# p$ y6 ?
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]# ~2 Z/ T) t8 u- L) U
谁能帮我解释一下[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0)是什么意思吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|SimulWay 道于仿真   

GMT+8, 2026-8-9 21:14 , Processed in 0.014153 second(s), 14 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表