문제 설명
Netlogo는 특정 xycor를 설정합니다. (Netlogo set specific xycor)
이것은 나의 첫 번째 Netlogo 스크립트이며 나는 완전한 초보자이며 보트인 거북이의 xy 좌표를 설정하는 데 도움이 필요합니다. 보트가 세계 상단에 위치하기를 바랍니다(30 x 30 세계에서 원점이 왼쪽 하단 모서리에 있음). 보트의 수는 1에서 4까지의 슬라이더로 선택됩니다. 다른 질문 & 답변, 다음 코드를 시도했지만 "예상 닫는 대괄호" 또는 "FOREACH는 적어도 2개의 입력, 목록 및 익명 명령을 예상함" errors.
create‑boats N‑boats ;; create the boats
[set color red ;; give them a color, a size and shape
set size 1
set shape "arrow"
(foreach [ 0 1 2 3 ] [6 12 18 24] [28 28 28 28] [ xy ‑> boats [ setxy item 0 xy item 1 xy ] ] )
set heading 180]]]
"set xcor one‑of [6 12]"도 시도했지만 때때로 보트가 서로의 위에 일렬로 늘어서서 정확한 좌표를 지정하고 싶습니다. 감사합니다.
참조 솔루션
방법 1:
Do they have to be equally spaced? Or just anywhere along the top? If anywhere along the top, you are better off choosing 4 random patches in that row and getting each of them to sprout a boat. Something like (not tested):
ask n‑of N‑boats patches with [pycor = max‑pycor]
[ sprout 1
[ set color red
set heading 180
set shape "arrow"
]
]
Just as a general newbie tip with NetLogo, if you are using foreach
then you should immediately think about whether what you really want is some sort of agentset. It's particularly common for people coming from some other coding background to try and do things in for
loops that are better handled as agentsets in NetLogo.
방법 2:
First, to answer your syntax issues:
Remember that the code in brackets after CREATE‑TURTLES is run by a single turtle. After all the turtles are created, each turtle runs the code, one‑at‑a‑time. It looks like you're trying to position all the turtles. That's probably not what you meant.
Second: look at the FOREACH.
( foreach [ 0 1 2 3 ] [6 12 18 24] [28 28 28 28] [ xy ‑> boats [ setxy item 0 xy item 1 xy ] ] )
- You've got the foreach in ( and ) ‑‑ that's good.
- You've got THREE separate lists as input ‑‑ that's fine, too, but looks like maybe you meant that to be one list? Not sure.
- Your anonymous procedure has only ONE input: xy. That's a problem, since you have THREE lists as input.
- You are referring to ITEM 0 and ITEM 1... which doesn't make a lot of sense at that point, unless you passed a list of lists, but no... still not.
- Finally, it looks like you meant to
ask boats
but if you did, since this is inside the create‑turtles code block, this is already being excuted by a boat. So, this boat will ask all boats to do something. If this was otherwise correct, you'd just do the boat commands‑‑you are essentially already inside an "ask".
So, this is not ideal, in any case. Let's start at the top.
Three Ways to Set Turtle Location
If you want to put turtles at specific x and y coordinates, you have a few choices. Here's three:
- Use Math based on the value of WHO to calculate the coordinates.
- (Or a counter, if you want to keep your model "who‑agnostic")
- Use a set of patches (probably created using Math, too.) The patches
SPROUT
the turtles. - Use a List of coordinates. Each turtle pulls its location from the list.
A Note on the Examples.
All the below examples focus on setting the position. After the boats are created, the boats are asked to run a procedure called
apply‑boat‑properties
, which is where things like color, heading, size, and other properties would be set. This is to remove clutter from the examples. It also makes it quite easy to find where the properties are being set.to apply‑boat‑properties ;; run by a boat set heading 180 set shape "arrow" set color gray end
Math (aka "calculated position")
You can do this is the locations are in a pattern that can be calculated. This can be easy to do, but can also be hard and require a lot of tweaking and experimentation to get just right. The weird NetLogo world geometry can sometimes be confusing.
Simple Example of Calculated Positioning
to make‑fleet [ #fleet‑count ]
create‑boats #fleet‑count
[ setxy (min‑pxcor + 6 + who * 6) max‑pycor
apply‑boat‑properties
]
Elaborate Example of Calculated Positioning
- Varies the gap between boats automatically.
- The boats are located without regard to patch centers, unless "$center‑on‑patches?" is set to true
WHO
is not used in the calculations. This ensures the math comes out right, even if other turtles already exist in the model.
to make‑fleet [ $fleet‑count $center‑on‑patches? ]
;; requires breed [ boats boat ]
set‑default‑shape boats "arrow"
let $boat‑size 1
;; this is the real "0" in terms of even positioning
let $far‑left‑edge‑xcor min‑pxcor ‑ 0.5
let $total‑width‑of‑all‑boats ($boat‑size * $fleet‑count)
let $gap‑between‑boat‑edges (world‑width ‑ $total‑width‑of‑all‑boats) / ($fleet‑count + 1)
let $gap‑between‑boat‑centers $gap‑between‑boat‑edges + $boat‑size
let $gap‑between‑left‑edge‑and‑first‑boat‑center $gap‑between‑boat‑edges + ($boat‑size / 2)
;; I use boat‑number, rather than who,
;;so this procedure will work even if there are other turtles
let $boat‑number 0 ;; 0 to (#fleet‑count ‑ 1)
create‑boats $fleet‑count
[ let $x 0
if‑else ( $center‑on‑patches? )
[ set $x min‑pxcor
+ floor ( $gap‑between‑left‑edge‑and‑first‑boat‑center )
+ ceiling ( $boat‑number * $gap‑between‑boat‑centers )
]
[ ;; position without regard to patch alignment
set $x $far‑left‑edge‑xcor
+ $gap‑between‑left‑edge‑and‑first‑boat‑center
+ $boat‑number * $gap‑between‑boat‑centers
]
let $y max‑pycor
setxy $x $y
set $boat‑number $boat‑number + 1
]
ask boats [ apply‑boat‑properties ]
end
Using a Set of Patches
This is a lot like the previous method, but we create a set of patches, then ask the patches to make the turtles. This is useful for creating a set of turtles on a specific area of the world, like on an edge, or in a specific pattern of patches, or filling an area with a randomly scattered set of turtles.
to make‑fleet [ $boat‑gap ]
let $locations patches with
[
;; rules to find the patches
pycor = max‑pycor ;; top row
and
(pxcor ‑ min‑pxcor) mod $boat‑gap = 0
]
ask $locations [ sprout 1 [ set breed boats ] ]
ask boats [ apply‑boat‑properties ]
end
Using A List of Coordinates
If the required coordinates are few, or are can't be calculated, you can put the coordinates in a list. The created turtles can pull the coordinates from the list.
to make‑fleet [ $fleet‑count ]
;; A list of x y coordinate pairs.
;; The coordinate pairs are also in a list
let $coordinates
[
; x y ;
[ 6 0 ]
[ 12 0 ]
[ 18 0 ]
[ 24 0 ]
]
;; initialize the counter uses as the list index
let $boat‑number 0
;; make boats
create‑boats $fleet‑count
[ ;; get the coordinate pair
let $xy item $boat‑number $coordinates
;; get the x and y from the pair
let $x first $xy ;; aka item 0 $xy
let $y last $xy ;; aka item 1 $xy
;; apply the coordinate
setxy $x $y
;; increment the index number
set $boat‑number $boat‑number + 1
]
ask boats [ apply‑boat‑properties ]
Note that the above will make errors if $fleet‑count
is more than 4, because the coordinate list has only 4 items.
You could use FOREACH
on the list, and create 1 turtle for each coordinate. This is handy when a large list of coordinates have been read from a file.
to make‑fleet [ $coordinates ]
;; assume $coordinates is a list of pairs of coordinates.
;; like [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]
;; we are going to ask the patch at each of the coordinates
;; to create a turtle. Then we will set up the turtle.
( foreach $coordinates
[ [ $xy ] ‑>
ask patch first $xy last $xy
[ sprout 1 [ set breed boats ] ]
]
)
ask boats [ apply‑boat‑properties ]
end
Finally
There are other ways, too, and many other formulas for calculated positions.
(by Afada、JenB、TurtleZero)