2

The following code creates 2 cars and then adds carts behind them (called 'followers'). The code aims to spread the followers out evenly behind the cars, one after the other and then link and tie them together. Then when the cars move they should pull the followers with them. But only the first tie (the car and nearest follower) seems to be working.

globals [small  len_small ]
breed [cars car]
breed [followers follower]

cars-own [reg no_foll  len]
followers-own [reg]

to setup
  clear-all
   set small 2
   set len_small 7
 
  ask patches with [pycor >= 5 or pycor <=  1][set pcolor red]
  ask patches with [pycor = 4 ][set pcolor blue]
   ask patches with [ pycor = 2][set pcolor blue + 1]
  ask patches with [pycor = 3][set pcolor white]
  make_small
  reset-ticks

end

to go
  ask cars [forward 2]
  
end

to make_small
 create-cars  small  [move-to one-of patches with [pcolor = white  ]
    set shape "circle" set color red  set reg who set no_foll 0  set len len_small
    set heading one-of  [90 270]]
  
   create-followers (len_small - 1) * small [ set shape "car" set color red move-to one-of cars with [color = red and no_foll < (len_small - 1)]
    set heading [heading] of one-of cars-here + 180 set reg [reg] of one-of cars-here ask one-of cars-here [set no_foll no_foll + 1]]
  form_small_car
end

to form_small_car
  ask followers with [color = red ] [while [any? other turtles-here with [reg = [reg] of myself]][forward 1]]
  ask cars with [color = red] [let foll_behind_list followers-on patch-ahead -1 let foll_behind one-of foll_behind_list with [reg = [reg] of myself] create-link-to foll_behind [tie]]
  ask followers with [color = red and any? followers-on patch-ahead -1] [let foll_behind_list followers-on patch-ahead -1 let foll_behind one-of foll_behind_list with [reg = [reg] of myself ] 
  create-link-to foll_behind [tie]]
 end

Where am I going wrong? [BTW the selection of followers is over-done in this example code, but in the bigger model needs to be in place so that only followers attached to the same car link]

Thanks,

Kevin

Kevin A
  • 71
  • 7

1 Answers1

3

Think I got it - essentially needed to ensure only picked correct car and also patch-ahead 1 and not -1.

globals [small  len_small ]
breed [cars car]
breed [followers follower]

cars-own [reg no_foll  len]
followers-own [reg]

;;white is the road and blue are parking ranks (red is the border)
to setup
  clear-all
   set small 2
   set len_small 7

  ask patches with [pycor >= 5 or pycor <=  1][set pcolor red]
  ask patches with [pycor = 4 ][set pcolor blue]
   ask patches with [ pycor = 2][set pcolor blue + 1]
  ask patches with [pycor = 3][set pcolor white]
  make_small
  reset-ticks

end

to go
  ask cars [forward 2]

end

to make_small
 create-cars  small  [move-to one-of patches with [pcolor = white  ]
    set shape "circle" set color red  set reg who set no_foll 0  set len len_small
    set heading one-of  [90 270]]

   create-followers (len_small - 1) * small [ set shape "car" set color red move-to one-of cars with [color = red and no_foll < (len_small - 1)]
    set heading [heading] of one-of cars-here + 180 set reg [reg] of one-of cars-here ask one-of cars-here [set no_foll no_foll + 1]]
 form_small_car
end

to form_small_car
  ask followers with [color = red ] [while [any? other turtles-here with [reg = [reg] of myself]][forward 1]]
  ask cars with [color = red] [let foll_behind_list followers-on patch-ahead -1 let foll_behind one-of foll_behind_list with [reg = [reg] of myself] create-link-to foll_behind [tie]]
  ask followers with [color = red and any? followers-on patch-ahead 1] [let foll_behind_list followers-on patch-ahead 1 if any? foll_behind_list with [reg = [reg] of myself]
    [let foll_behind one-of foll_behind_list with [reg = [reg] of myself] print who print [who] of foll_behind create-link-to foll_behind [tie]]
     ]
 end
Kevin A
  • 71
  • 7