Unit 2: Building a Simple Model
I'm trying to do the Heroes and Cowards example. I'm working on Windows 11 pro, NetLogo 6.4.0 (November 14, 2023). I think setxy isn't working. I'll put my current code at the end (is there a way to include a screen print? I wanted to include a print of what my UI looks like after setup). When I hit setup, rather than seeing the turtles at random places on the screen they are all grouped together at 0,0. When I click on Go (regardless of whether I choose the hero or coward personality) I get the following error:
"OF expected input to be a turtle agentset or turtle but got the number 0 instead.
error while turtle 65 running OF
called by procedure ACT-COWARDLY
called by procedure GO
called by Button 'go'"When I click on the small blob in the center I get a menu of all 100 turtles and when I inspect any of them their xpos and ypos are both set to 0. I think what is happening is they aren't getting initialized so the default value shows up but when I go to set the value it is an error because the property hasn't been initialized. Here is my code:
turtles-own [ friend enemy ]
to setup
clear-all
ask patches [set pcolor white] ;; create blank background
create-turtles 100 [
setxy random-xcor random-ycor
if (personalities = "cowards" ) [set color blue ]
if (personalities = "heroes" ) [set color red ]
set friend one-of other turtles
set enemy one-of other turtles
]
reset-ticks
endto go
ask turtles [
if (color = blue) [act-cowardly]
if (color = red) [act-bravely]
]
tick
endto act-cowardly
;; move away from the enemy
facexy [xcor] of friend + ([xcor] of friend - [xcor] of enemy)
[ycor] of friend + ([ycor] of friend - [ycor] of enemy)
fd 0.1
endto act-bravely
;; Move toward midpoint of your friend and enemy
facexy ([xcor] of friend + [xcor] of enemy) / 2
([ycor] of friend + [ycor] of enemy) / 2
fd 0.1
endI figured it out. It wasn't a problem with the code but with the UI. When I created the "setup" button I set the title rather than the command so when I clicked it nothing happened. It works now.
Well done for figuring it out! The little things can drive me up the wall with frustration. :)
1. Do we use [set pcolor red] or [set color red] to set colors to turtles?
2. I tried Netlogo with random-seed 29 but I cannot see the results as the test showed.1. [set pcolor red] will turn the patch the turtle is on red if the command is being directed at turtle(s). [set color red] will turn the turtle itself red.
2. where are you using random-seed 29? if you enter it in the text box below the Command Center in the Interface tab, then things will display in the Command Center window. random-seed 29 only sets the seed. You then need to enter a command to generate a random number from 0 to 10 to have a random number then display in the Command CenterQ 2. after you have set the "random-seed 29" in the "observer>" input, the Command Center box above it should show the command. Then type in the observer> text entry "random 10". The Command box will show first: "show random 10" and then on the next line the value, e.g. "3". Repeat the "random 10" in the observer input and the Command Center should keep adding the 2 lines, with your command and the value below it. Are you seeing both the "Command Center" display box and below it the "Observer" input? - Alex Tolley
Subforums
- Introduce Yourself
- Class Announcements
- Study Groups and Meetups
- Technical Issues
- General Discussion
- Course Feedback
- Course Materials to Share
- Unit 1: What is Agent-Based Modeling and Why Should You Use It?
- Unit 2: Building a Simple Model
- Unit 3: Extending Models
- Unit 4: Creating Agent-Based Models
- Unit 5: The Components of an Agent-Based Model
- Unit 6: Analyzing Agent-Based Models
- Unit 7: Verification, Validation, and Replication
- Unit 8: History of ABM and Classic Models
- Unit 9: Advanced ABM