O. L
O. L

Reputation: 53

Netlogo while loop only one time

I have a small problem in Netlogo, which I began to work on only a few days ago, I try to make a maze with two adventurers, and I make them go from two different starting locations, to one final location. All the beginning of my code works fine, to draw my maze, but when I want to make the adventurers go, only one of them goes in the right direction and find the exit, and the second doesn't even go in the asked direction (East). I think the problem is in my GO procedure, but I can't reach to find a solution... Here is my code, I work on Netlogo 5.2

to createaventurier
create-aventuriers pointsdepart
[set shape "person"
set color pink
set size 1 
move-to one-of patches with [pcolor = green]
ask patch-here
[set pcolor blue]
set beta ycor
]
show count aventuriers                                   
end

Here the program does what it's supposed to do.

to go
set i 0
createaventurier
while [i < pointsdepart] 
[show count aventuriers
ask one-of aventuriers
[set heading 90
  execute]
set i i + 1
]
show count pas
end

And it's here that the program return that there are no adventurers (no agents or agentsets) while the observer returns me that there are two of them (when I want two adventurers). I Breed-ed them at the beginning of the code, and I used a lot of while loops in other procedures, which worked perfectly. I'm not at ease with the software, I'm just looking for a simple explanation, (I'm not so good in english too). If you need some other parts of my program I can post it, but I don't think they'll be needed. If you need more informations I can also post it, but I hope I have been clear enough.

I thank you in advance.

Upvotes: 5

Views: 3449

Answers (1)

JenB
JenB

Reputation: 17678

Here is a simplified version of your code. I have changed adventurers into turtles so I didn't need breeds and hard-coded the number 2 for your variable pointsdepart. It works fine, in the sense that there are always 2 turtles.

to setup
  clear-all
  ask n-of 20 patches [set pcolor green]
  reset-ticks
end

to make-agents
  create-turtles 2
  [ set shape "person"
    set color pink
    set size 1
    move-to one-of patches with [pcolor = green]
    ask patch-here [ set pcolor blue]
  ]
  show count turtles                                   
end

to go
  let i 0
  make-agents
  while [ i < 2 ]
  [ show count turtles
    ask one-of turtles
    [ set heading 90
      forward 1
    ]
    set i i + 1
    ]
  show count turtles
end

This suggests that the problem is in your execute function (which I replaced with forward 1).

Running my code will demonstrate a logical problem. You are looping through (twice in this example) and running ask one-of in each loop. one-of selects a random turtle, so you might get them each to run your execute code once, or you might have the same turtle chosen each time. It is very likely you want code that looks more like this:

to go
  make-agents             ; note - should really be in setup, not go
  ask turtles
  [ set heading 90
    forward 1
  ]
  show count turtles
end

Also, you would generally have a tick command at the end of the go procedure to advance the clock and then the go procedure is run again so the turtles continue to move etc. This is why I have commented that the call to create the adventurers should really be in the setup procedure, otherwise another 2 adventurers will be created each time the clock advances.

The setup procedure is for everything that needs to be in place for the beginning of the simulation (eg creating adventurers, setting up your maze, giving initial resources to adventurers). The go procedure is for the actual process that is being simulated (eg moving, obtaining resources from the environment, depleting energy).

Upvotes: 2

Related Questions