scagbackbone
scagbackbone

Reputation: 791

Netlogo - how to create paths?

I'm trying to model particular intersection in NetLogo. Here is the code (in early stages of the development):

breed [ trams tram ]
breed [ cars car ]



to setup
  clear-all
  set-default-shape trams "bug"
  set-default-shape cars "car"
  setup-patches
  reset-ticks
end

to go
  make-new-car frequency-SN-1 1 -20 0
  make-new-car frequency-SN-2 2 -20 0
  make-new-car frequency-NS-3 -3 20 180
  setup-trams tram-frequency
  move-trams
  want-turn
  move-cars
  tick
end

to setup-trams [ freq ]
  if (random-float 100 < freq) and not any? turtles-on patch 0 -20 [
    create-trams 1 [
      setxy 0 -20
      set heading 0
      set color black
    ]
  ]
end

to make-new-car [freq x y head ]
  if (random-float 100 < freq) and not any? turtles-on patch x y [
     create-cars 1 [
       setxy x y
       set heading head
       set color one-of base-colors
     ]
  ]
end

to move-cars
  ask cars [
    ifelse not can-move? 1
      [ die ]
      [ fd 1 ]
  ]
end

to want-turn
  ask cars-on patch 2 -7 [
    ifelse (random-float 100 < 100) and not any? turtles-on patch 3 -7
      [ rt 90 fd 1 
        lt 90 fd 4
        rt 90 fd 2
        lt 90 fd 2
        rt 90 fd 1]
      [ fd 1 ]
  ]
end

to move-trams
  ask trams [
    ifelse not can-move? 1
      [ die ]
      [ fd 1 ]
  ]
end

to setup-patches
  ask patches [
    ifelse abs pxcor <= 3 or abs pycor <= 3
      [ set pcolor black ]     
      [ set pcolor green - 1 ]
    ifelse abs pxcor <= 0.5
      [ set pcolor red ]
      [   ]
  ask patch 3 -2 [ set pcolor red ]
  ask patch -2 -1 [ set pcolor red ]
  ask patch -1 -1 [ set pcolor red ]
  if pycor = -3 and pxcor > 8
    [ set pcolor green - 1 ]
  if pycor <= 3 and pycor >= -1 and pxcor < -3
    [ set pcolor green - 1 ]
  if pxcor = 3 and pycor <= -8
    [ set pcolor green - 1 ]
  ]
end

What causes me problems is the want-turn procedure. I want some cars to move right and head eastwards. However i cannot, up to now(just started with NetLogo) to do it differently - but just to make car disappear on particular patch and reappear on the other (it is caused exactly by want-turn procedure which runs in to-go procedure. It is run each tick, yet inside want-turn I move car many patches so it seems as teleport).

Can I somehow create road there? So instead of manually moving car to another patch I would just ask if this car meets the condition on the particular patch (in this case: patch 2 -7) and if yes - it would move the alternative road.

I'm just discovering NetLogo and any ideas are welcome

Upvotes: 1

Views: 794

Answers (1)

Luke C
Luke C

Reputation: 10301

One way to do this is to use rules based on the colors of patches ahead of the turning cars. For a way that will strictly work with the pattern I think you're trying to reproduce, you can make the cars turn left if there is a green patch ahead of them and right when there is a red one ahead. That allows you to control their heading based on patches as they move.

I would also recommend building into their movement procedure that they don't ever move forward if there is a car ahead of them (as in the Traffic models in the models library), just as you did with your turning procedure. That way, they will never cross into a patch that is already occupied by another car.

You should also check out patch-set to create patch agentsets so you don't have to repeat commands like set pcolor red; you can also do this with or.

Have a look at the modifications that I made to your code for some implemented examples of the above:

breed [ cars car ]

to setup
  ca
  resize-world -20 20 -20 20 
  set-default-shape cars "car"
  setup-patches
  reset-ticks
end

to go
  make-new-car 10 2 -20 0 blue
  make-new-car 10 1 -20 0 yellow
  make-new-car 10 -3 20 180 white
  make-new-car 10 -1 20 180 pink
  make-new-car 10 -20 -3 90 orange 
  want-turn
  move-cars
  tick
end

to make-new-car [freq x y head col ]
  if (random-float 100 < freq) and not any? turtles-on patch x y [
    create-cars 1 [
      setxy x y
      set heading head
      set color col
    ]
  ]
end

to move-cars
  ask cars [
    ifelse not can-move? 1
      [ 
        die 
      ]
      [ 
        if patch-ahead 1 != nobody [
          if [pcolor] of patch-ahead 1 = green - 1 [
            rt -90
          ]
          if [pcolor] of patch-ahead 1 = red [
            rt 90
          ]
          if ( [count turtles-here] of patch-ahead 1 = 0 ) and
            ([pcolor] of patch-ahead 1 = black) [
              fd 1
            ]
        ]
      ]
  ]

end

to want-turn
  ask cars-on patch 2 -7 [
    if (random-float 100 < 50) [ 
      rt 90 
    ]
  ]
end

to setup-patches
  ask patches [
    ifelse abs pxcor <= 3 or abs pycor <= 3
      [ set pcolor black ]     
      [ set pcolor green - 1 ]
    if abs pxcor <= 0.5
      [ set pcolor red ]
    ask ( patch-set patch 3 -2 patch -2 -1 patch -1 -1 patch 8 -1 ) [
      set pcolor red 
    ]

    if ( pycor = -3 and pxcor > 8 ) or (pycor <= 3 and pycor >= -1 and pxcor < -3) or (pxcor = 3 and pycor <= -8) [
      set pcolor green - 1 
    ]
  ]
end

Upvotes: 1

Related Questions