Help with stopping my turtle

402 views
Skip to first unread message

Linn Douven

unread,
Mar 9, 2021, 2:39:15 PM3/9/21
to netlogo-users
Hello everybody,

I am making an extended model of Langton's ant. I'm trying to stop my ant when it touches the wall but nothing works.
Does anybody have any ideas?

Aaron Andre Brandes

unread,
Mar 11, 2021, 1:22:23 PM3/11/21
to Linn Douven, netlogo-users

Hi,

Here is some code for a model in which  breeds of turtles move randomly within their home regions but if their next step would take them out of the world boundaries (no wrapping) or across a region boundary they stop moving. (I’ve also included the model since it incorporates some settings.)

 

globals [ half-width half-height agentset-size

          region1 region2 region3 region4 ]

 

breed [ breed1s breed1 ]

breed [ breed2s breed2 ]

breed [ breed3s breed3 ]

breed [ breed4s breed4 ]

 

turtles-own [ home-region stuck]

to setup

  clear-all

  set agentset-size 20

  set half-width 15

  set half-height 10

  set-default-shape breed1s "airplane"

  set-default-shape breed2s "bug"

  set-default-shape breed3s "butterfly"

  set-default-shape breed4s "turtle"

 

  resize-world 1 - half-width half-width 1 - half-height half-height

  set region1 patches with [ pxcor <= 0 and pycor >= 1 ]

  ask region1 [ set pcolor yellow ]

  set region2 patches with [ pxcor >= 1 and pycor >= 1 ]

  ask region2 [ set pcolor orange ]

  set region3 patches with [ pxcor >= 1 and pycor <= 0 ]

  ask region3 [ set pcolor pink ]

  set region4 patches with [ pxcor <= 0 and pycor <= 0 ]

  ask region4 [ set pcolor red ]

  ask n-of agentset-size region1 [ sprout-breed1s 1 [ set color blue set heading random 360

  set home-region region1 ]]

  ask n-of agentset-size region2 [ sprout-breed2s 1 [ set color pink set heading random 360

  set home-region region2 ]]

  ask n-of agentset-size region3 [ sprout-breed3s 1 [ set color cyan set heading random 360

  set home-region region3 ]]

  ask n-of agentset-size region4 [ sprout-breed4s 1 [ set color magenta set heading random 360

  set home-region region4 ]]

  ask turtles [ set stuck false ]

  reset-ticks

end

 

to go

  tick

  ask breed1s [ move 1 ]

  ask breed2s [ move 1.5 ]

  ask breed3s [ move 2 ]

  ask breed4s [ move 5 ]

end

 

to move [ dist ]

  ; if the patch ahead does not exist or is not part of home region do not move

  ; because turtle is at a boundary

  let target-patch patch-ahead dist

  ifelse target-patch = nobody or not member? target-patch home-region

  []

  [ ; plan to make a random move, but do not execute if at boundary

    let turn-angle (random-float 60) - 30

    ; if world does not wrap target patch could be nobody

    ifelse target-patch = nobody or not member? target-patch home-region

    []

    [ rt turn-angle fd dist ]

  ]

end

 

I hope this is helpful.

 

Aaron

 

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/2336440c-adf1-4fb5-8041-4e9d2426d16cn%40googlegroups.com.

turtles-stop-at-region-boundaries.nlogo

Aaron Andre Brandes

unread,
Mar 11, 2021, 2:13:59 PM3/11/21
to Linn Douven, netlogo-users

Hi Linn,

In case you want the ant to turn around at an obstacle, here is a simpler example without multiple breeds.

 

globals [ num-turtles obstacle ]

 

to setup

  clear-all

  set num-turtles 20

  set obstacle patches with [pycor < -7 and pycor > -12 and pxcor > -15 and pxcor < 15 ]

  ask obstacle [ set pcolor red ]

  ask n-of num-turtles patches with [ pcolor != red ] [ sprout 1 [ set color blue set heading random 360 ]]

 

;; ask turtles [ set stuck false ]

  reset-ticks

end

 

to go

  tick

  ask turtles [ move 1 ]

end

 

to move [ dist ]

  let turn-angle (random-float 60) - 30

  ; if world does not wrap target patch could be nobody

  let target-patch patch-right-and-ahead turn-angle dist

 

  ifelse target-patch != nobody and  not member? target-patch obstacle

  [ rt turn-angle ]

  [ rt turn-angle - 180 ]

 

  ; turtle could still wind up outside region, so then don't move

  set target-patch patch-right-and-ahead 0 dist

  if target-patch != nobody and not member? target-patch obstacle

  [ fd dist ]

end

 

Aaron

-- 

Aaron Brandes, Software Developer

Center for Connected Learning and Computer-Based Modeling

 

 

From: <netlog...@googlegroups.com> on behalf of Linn Douven <linn...@gmail.com>
Date: Tuesday, March 9, 2021 at 2:39 PM
To: netlogo-users <netlog...@googlegroups.com>
Subject: [netlogo-users] Help with stopping my turtle

 

Hello everybody,

 

I am making an extended model of Langton's ant. I'm trying to stop my ant when it touches the wall but nothing works.

Does anybody have any ideas?

--

turtles-avoid-obstacle.nlogo
Reply all
Reply to author
Forward
0 new messages