Jacky Gu

常用NetLogo指令

03 Sep 2023 Share to

Netlogo详细文档:https://ccl.northwestern.edu/netlogo/docs/

  • 设置全局变量
globals [
	变量1
	变量2
]
  • 初始化时清空
clear-all
简称:ca
  • 返回在调用主体一定距离以内,且属于某集合的turtle所构成的集合
<集合> in-radius <距离>
  • 统计对象数量
count <值>
  • 随机数
random 随机数范围(从0开始);随机整数
random-float 随机数范围   ;随机小数
  • 在Command Center显示内容
show ...
print ...
type ...
  • 设置随机数分布
random-normal
random-poisson
random-exponential
random-gamma
  • tick计步器
to setup
	...
  reset-ticks
end
 
to go
	...
  tick
end
  • 颜色设置
set color red ;设置turtle颜色
set pcolor green ;设置patch颜色
  • breed语句定义对象
breed [<复数形式> <单数形式>]
如:
breed [wolves wolf]
ask dogs [ show count wolves-here ]

使得自定义对象可以像turtles/turtle一样使用

定义对象后,可以使用:

  1. create-wolves 5创建对象
  2. wolves-own [属性]定义对象属性
  • 创建对象
reate-<对象复数形式> 数量 [
    set color red
    setxy random-pxcor random-pycor
]
  • 设置数值
let <变量名> <值>
set <变量名> <值>
  • 移动指令
fd n ;前进n步
left 角度 ;左转
right 角度 ;右转
back n ;后退n步
move-to 
  • 设置形状/颜色/大小
set shape "wolf"
set size 3
set color
  • 随机走动
  ask turtles [
    right random 360
    fd 1
  ]
  • 位置相关
neighbors  ;相邻8个方块
patch-here  ;turtle下方patch
turtle-here
turtle-at
turtle-on
  • move-to使用
move-to turtle 5
;; turtle moves to same point as turtle 5
move-to one-of patches
;; turtle moves to the center of a random patch
move-to max-one-of turtles [size]
;; turtle moves to same point as biggest turtle
  • one-of相关
ask n-of 50 patches [ set pcolor green ]
;; 50 randomly chosen patches turn green

ask up-to-n-of 50 patches [ set pcolor green ]
;; 50 randomly chosen patches turn green
;; if less than 50 patches exist, they all turn green

;; assume the world is 11 x 11
show max-n-of 5 patches [pxcor]
;; shows 5 patches with pxcor = max-pxcor
;; min-n-of 相同

show max-n-of 5 patches with [pycor = 0] [pxcor]
;; shows an agentset containing:
;; (patch 1 0) (patch 2 0) (patch 3 0) (patch 4 0) (patch 5 0)

ask one-of patches [ set pcolor green ]
;; a random patch turns green

ask patches with [any? turtles-here] [ show one-of turtles-here ]
;; for each patch containing turtles, prints one of those turtles

show one-of mylist
;; suppose mylist is [1 2 3 4 5 6], prints a value randomly chosen from the list

show max-one-of patches [count turtles-here]
;; prints the first patch with the most turtles on it
;; min-one-of 相同
  • of相关用法
show [pxcor] of patch 3 5
;; prints 3

show [pxcor] of one-of patches
;; prints the value of a random patch's pxcor variable

show [who * who] of turtle 5
=> 25

show [count turtles in-radius 3] of patch 0 0
;; prints the number of turtles located within a three-patch radius of the origin
  • other用法
show count turtles-here
=> 10
show count other turtles-here
=> 9
  • neibours用法
show sum [count turtles-here] of neighbors
;; prints the total number of turtles on the eight
;; patches around this turtle or patch

show count turtles-on neighbors
;; a shorter way to say the same thing

ask neighbors4 [ set pcolor red ]
;; turns the four neighboring patches red
  • nobody用法
set target one-of other turtles-here
if target != nobody [ ask target [ set color red ] ]
  • crt numbers [commands] = create-turtles numbers [commands]

  • 显示时间

show date-and-time
  • 数值
show floor 4.5
show round 5.2
show ceiling 4.5
show precision 1.23456789 3
show precision 3834 -3
  • 取值范围
show range 5
=> [0 1 2 3 4]
show (range 2 5)
=> [2 3 4]
show (range 2 5 0.5)
=> [2 2.5 3 3.5 4 4.5]
show (range 10 0 -1)
=> [10 9 8 7 6 5 4 3 2 1]
show sqrt 4
=> 2
show sum [1 2 3 4]
=> 10
show sum [energy] of turtles
show 5 mod 2
  • 操作list数据(相当于数组)

turtles, patches,以及字符串都是list

length list ;;长度
last list   ;;最后一个
first list  ;;第一个
but-first   ;;除了第一个,bf, butfirst同
but-last    ;;除了最后一个,bl, butlast同 
empty? list ;;判断是否为空
fput ele list ;;在list头上插入ele
lput ele list ;;在list尾巴插入ele
max list    ;;取最大值
min list    ;;取最小值
n-of n      ;;随机取n个
position element list  ;;获取element在list中的位置,如果没有返回false,同样方法也可以用于字符串中子字符串查找
insert-item pos list ele ;;在list的pos位置前插入元素ele
remove-item pos list      ;;删除位于pos的元素
replace-item pos list ele ;;在list的pos位置替换为ele
reverse list ;;倒置list
sort 
sort-on [key] list ;;对象排序,根据key排序,如果倒叙,则用 sort-on [(-key)] list
sort-by < list     ;;顺序排序
sort-by > list     ;;倒叙排序
sublist list/string start-pos end-pos  ;;相当于substring,适用于list和string

foreach  ;;遍历元素并处理
如:
foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]

map reporter list ;;对list中每个元素,使用report方法处理
如:
- map [ i -> i + 1] [1 2 3] => [2 3 4]

filter report list ;;根据reporter设定的条件过滤list,返回经过滤后的新的list
如:
- show filter [ i -> i < 3 ] [1 3 2] => [1 2]
- show filter [ s -> first s != "t" ] ["hi" "there" "everyone"] => ["hi" "everyone"]
  • while 循环
while report [do something]
如:
- while [any? other turtles-here] [ fd 1 ]
- while [ length return-list < 7 ] [ set return-list lput 0 return-list ]
  • repeat重复执行某条指令
repeat 次数 [指令]
  • 使用循环
foreach [1.1 2.2 2.6] show
=> 1.1
=> 2.2
=> 2.6

foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3

foreach [1 2 3] [2 4 6] [ [a b] -> show word "the sum is: " (a + b) ]
=> "the sum is: 3"
=> "the sum is: 6"
=> "the sum is: 9"

foreach list (turtle 1) (turtle 2) [3 4] [ [the-turtle num-steps] -> ask the-turtle [ fd num-steps ] ]
;; turtle 1 moves forward 3 patches
;; turtle 2 moves forward 4 patches
  • map用法
show map round [1.1 2.2 2.7]
=> [1 2 3]

show map [ i -> i * i ] [1 2 3]
=> [1 4 9]

show (map + [1 2 3] [2 4 6])
=> [3 6 9]

show (map [ [a b c] -> a + b = c ] [1 2 3] [2 4 6] [3 5 9])
=> [true false true]
  • 连接字符串
show word "tur" "tle"
=> "turtle"

word "a" 6
=> "a6"
  • 孵化
hatch numbers [commands]
hatch-<breed> numbers [commands]
  • home = setxy 0 0

  • 关于turtles的label

ask turtles [ 
	set label-color red
	set label word "id: " who 
]
  • 关于patch的label
ask patches [ 
	set plabel-color red
	set plabel (word "id: " pxcor pycor)
]
  • 画随机产生的正态分布图
to draw-random
  let test-list []
  let i 0
  while [i < 10000] [
    set test-list lput (random-normal 10.1 5.1) test-list ;;期望值为10.1,方差5.1
    set i i + 1
  ]

  set-current-plot "Test"
  set-current-plot-pen "default" 
  set-histogram-num-bars 10
  ;;set-plot-y-range 0 10
  set-plot-x-range round (min test-list - 1) round (max test-list + 1)
  histogram test-list
end

random-normal 10.1 5.1换成random-exponential 2绘制指数函数