设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10606|回复: 0

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

[复制链接]
发表于 2012-6-20 08:59:43 | 显示全部楼层 |阅读模式
新手,在看netlogo自带的例子时,有几行程序看不懂,希望高手能够指点。! {- d, @9 n+ D% L
netlogo自带的social science--traffic grid这一例子当中,
, a" X" a: ~! n/ y8 oglobals
9 m! }. u2 D2 [* [6 S7 ~6 Y[' R- A0 ?  J5 T( a- D
  grid-x-inc               ;; the amount of patches in between two roads in the x direction
0 O5 g+ x* J9 H" x3 S9 c3 V8 Z  grid-y-inc               ;; the amount of patches in between two roads in the y direction
. d4 |* p' M5 O! n1 F' w! T  acceleration             ;; the constant that controls how much a car speeds up or slows down by if
5 h: {8 V5 l, `6 C                           ;; it is to accelerate or decelerate- v+ f+ b. `) S, n( U0 p0 b7 ]) L
  phase                    ;; keeps track of the phase$ R2 Q( q* d; _1 A0 F& o
  num-cars-stopped         ;; the number of cars that are stopped during a single pass thru the go procedure8 F. _: ]! M! X8 [# B& E8 B! u+ h6 [
  current-light            ;; the currently selected light
# f8 c1 A2 I5 l6 X  B. U
' v2 P# i$ _  ]) [! ~8 n0 k  ;; patch agentsets7 @7 D/ ~5 m7 z  N
  intersections ;; agentset containing the patches that are intersections
2 m  N* \, j# E0 ~- X  roads         ;; agentset containing the patches that are roads
9 f  D$ g1 v; {3 f" l8 t) C3 F], G; L2 J& x( ]  }) P

$ i; K% k5 U9 ?! w) T( mturtles-own
  W+ u9 y$ d7 u8 Q. v( F* y[
3 ~/ Y9 P1 q9 Q+ G  speed     ;; the speed of the turtle
9 S. x: |) I( x  up-car?   ;; true if the turtle moves downwards and false if it moves to the right: l3 S9 D6 J, _% j
  wait-time ;; the amount of time since the last time a turtle has moved
0 \8 [0 p) o4 ~8 o]  p! Z! V" ^8 c  z1 a
/ w& h5 |1 j1 X/ j9 w8 X7 T( Y
patches-own
. Y3 {6 r" t" l. z' d[# e$ K0 e7 [6 j3 U
  intersection?   ;; true if the patch is at the intersection of two roads
' c% |  [5 R$ L# c" f/ Q# e  green-light-up? ;; true if the green light is above the intersection.  otherwise, false.0 d$ _  b$ [5 m& r' w
                  ;; false for a non-intersection patches.
6 d$ @1 D' q7 l% W$ y( b  my-row          ;; the row of the intersection counting from the upper left corner of the- P& {' t  P5 m/ \2 ]1 q
                  ;; world.  -1 for non-intersection patches.4 q) q9 A1 @0 c+ [% l* E# _1 d3 K% H
  my-column       ;; the column of the intersection counting from the upper left corner of the
% W6 }- `! ?$ G5 s; u, B3 F                  ;; world.  -1 for non-intersection patches.% G/ Y$ ^3 l( a  b: A, E
  my-phase        ;; the phase for the intersection.  -1 for non-intersection patches.* a  q' `2 {1 D/ k: a" a7 g
  auto?           ;; whether or not this intersection will switch automatically.* [, u% ]# O# ?. h$ h, E8 O
                  ;; false for non-intersection patches.
: g9 y- C5 [! c( Z* o]4 e. N: s) z/ |3 A) g
2 b4 X7 r* k6 a( x3 h
4 b3 Z+ Y3 j1 i0 K2 B. F
;;;;;;;;;;;;;;;;;;;;;;" `$ i9 f! R! W9 g. Z
;; Setup Procedures ;;
) I8 o3 M' Z) K, v;;;;;;;;;;;;;;;;;;;;;;& e' D  |6 o9 z# X. ]. u! u4 T5 k

5 a# N4 i: \+ g0 B, j;; Initialize the display by giving the global and patch variables initial values.
4 ?; G; a- n/ O  ~% E8 A. G- Y5 i9 L; x! [;; Create num-cars of turtles if there are enough road patches for one turtle to
) w1 s7 {& |6 g& h" p3 G$ o;; be created per road patch. Set up the plots.
+ R; b* @9 N. x7 ?+ o. qto setup
) v4 T( L4 y. F9 R& h8 ~" h  ca% C4 m" A6 z: v0 {. r+ r. M% g3 i
  setup-globals
; z* K. ]1 }8 P' A' ?- D: ~5 R9 q( g  X/ B+ K
  ;; First we ask the patches to draw themselves and set up a few variables
2 N+ j. s1 I! _" u+ }; n  setup-patches
- a5 @+ M+ T: J& G. |0 _3 b  make-current one-of intersections
* @: R: x4 p6 k, e  label-current$ ?7 J1 u2 F; b* a& T2 o
, f0 [: i9 K* P  F# b
  set-default-shape turtles "car"! q( R- X" d: B2 d
6 W7 [3 \% X5 G/ G/ _
  if (num-cars > count roads)
% b; ^  f8 U- H' ~3 E# d' t' }  [. L& r" @* d0 w3 [* H
    user-message (word "There are too many cars for the amount of "9 L" \: Q# y% C
                       "road.  Either increase the amount of roads "8 ?, I1 |- T3 m3 B
                       "by increasing the GRID-SIZE-X or "
1 V2 {, A- h2 g& {' B3 ]2 w                       "GRID-SIZE-Y sliders, or decrease the "
) U' Z& x# e4 B1 F6 Z7 S                       "number of cars by lowering the NUMBER slider.\n"" G8 x. o- M$ Q% g; q
                       "The setup has stopped.")# \3 S- F8 N; Z  m% z, F6 O
    stop
  L3 a& o5 f7 g6 k" w  ]
% m8 g- o" b- ^: X/ M  t# n
3 u# u  {6 J; P% b2 M  ;; Now create the turtles and have each created turtle call the functions setup-cars and set-car-color
. T, Z% E* W& `6 S+ e  crt num-cars5 x% A5 B6 |! c4 n
  [. E/ C% b* I$ \/ y
    setup-cars' v8 P. m5 \4 F$ L6 B% m( [
    set-car-color
( e/ E5 b& `) d9 \- J    record-data
$ @% \8 L# _9 c3 s2 o2 Q6 e  ]
7 |0 Y2 Z9 g1 ]1 i6 Z# `* C6 Q) ?* g  h
  ;; give the turtles an initial speed) ?9 \, n$ e+ \6 A7 V$ }/ b- ]
  ask turtles [ set-car-speed ]
( m; m- q( b- m4 h: V! u  F( h- K$ t0 K9 e0 w% X& \- l
  reset-ticks
0 v5 G! j8 {2 l+ q& _+ \end! [4 Q+ x4 _% f% U1 ]% R

3 l: l; _& j3 Q! @2 Z- n;; Initialize the global variables to appropriate values
1 G/ G- z# B$ Y' q8 L+ r4 |9 t0 wto setup-globals+ |7 L  E# ^' o
  set current-light nobody ;; just for now, since there are no lights yet- L, R# M3 _" }/ f, l6 R
  set phase 0) {; A* P, Y9 `, c/ x! p# B
  set num-cars-stopped 09 b5 ^5 a  _% o: u% {! C" P
  set grid-x-inc world-width / grid-size-x7 x5 p& N, x: Z5 F& |! W1 K
  set grid-y-inc world-height / grid-size-y
/ t" o! w+ U  T) L+ ?- P
; S" T" U+ K( T7 g, d  ;; don't make acceleration 0.1 since we could get a rounding error and end up on a patch boundary! O: i/ I0 h/ I# t5 f9 n
  set acceleration 0.099  `- f- [% w6 M. C) I) P
end5 T3 p! y4 Q. r1 _) Y

4 \6 [/ q9 l# ^9 B# i0 Q;; Make the patches have appropriate colors, set up the roads and intersections agentsets,
( x3 e5 q& ~# ]9 ]8 D;; and initialize the traffic lights to one setting3 n' k0 Q6 n. E* W$ K, D2 y- d& Z1 L2 F
to setup-patches
3 ?% b* |* ], K$ {6 A/ r  ;; initialize the patch-owned variables and color the patches to a base-color0 W! M  L) o; p, U# b. t
  ask patches+ `2 K. u! O9 T) R3 k2 x
  [8 e- ~) I: [8 D  j3 Y
    set intersection? false% X3 q' ], a2 A; T2 ^( Y7 H
    set auto? false  @6 i4 W- Y+ Q' ]% u" G' \3 k
    set green-light-up? true
  @; {8 g4 b/ v) w* T    set my-row -1
2 G) t5 N! n" v& A. g/ v    set my-column -1( d. x4 \+ c) f
    set my-phase -13 W% @+ n# n7 Q# q% m% m5 P$ ^
    set pcolor brown + 3
: O3 a9 ^! Q3 O1 q3 Q9 K9 L1 R  ]# u% i! z4 J) O

/ @9 S3 j$ A# @" }& k  ;; initialize the global variables that hold patch agentsets/ z" D+ s! C) r; l  [6 r% w  `6 y
  set roads patches with
4 t( M! u  N! z' n% O2 K/ o+ I- b    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or9 P7 t$ V/ ^8 Z- X* m/ X! U5 k1 h* N
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]. a8 {8 Y% ~* r' {& a
  set intersections roads with7 w5 `1 R! H7 X# p
    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
7 r! b5 y& d* w4 p% H    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]
& }+ V, |2 o: T0 D- @
/ c" C4 p1 a* K$ D/ h, _  ask roads [ set pcolor white ]
& J! }# ^" w$ x! d& A$ K% B+ \    setup-intersections0 A$ \9 _& L# L8 B1 S
end. {6 `& Q# [. W8 _0 S* H2 F
其中定义道路的句子,如下所示,是什么意思啊?
  H$ u6 j! _, P# g+ ^ set roads patches with! H7 S3 ]2 A: M& [! A4 `; T
    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or: p1 j' X8 w. g
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]) G8 ]4 E- o7 B
谁能帮我解释一下[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0)是什么意思吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-16 09:39 , Processed in 0.013953 second(s), 14 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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