Error using "n-of" command with a breed in Netlogo

Viewed 63

I'm running into trouble with using the "ask n-of # " command.. got an "expected number here, rather than a list or block" error message.

Added the main chunks of code that pertain to this error. Wondering if anyone can help?

Goal: Looking to have solar power charge only 6 of squad1 at a time (squad1 is my breed of 10 turtles). Annotated below...

    breed [squad1 squad1s]

    turtles-own
    [individual-power
    solar-delay
    power-count1
    count-recharge1]

Setup

    to setup 
    clear-all
    setup-patches
    setup-turtles
    reset-ticks
    end

    to setup-turtles
    create-squad1 10
    ask squad1
    [
    set color blue
    set shape "person"
    set size 1.7
    set individual-power 100
    set heading 90
    set solar-delay 0
    setxy random-xcor random-ycor
    ]
    end

To Go

    to go
    ask squad1 [
    if individual-power >= 15
    [forward 1
    set label individual-power
    set individual-power individual-power - 1
    set solar-delay solar-delay + 1]

    if recharge-options1 = "external-solar1" [solar-battery1]
    ]
    tick
    end

Solar-Battery1 Code

**This is chunk of code I need help with. I get an "expected number here, rather than a list or block" message where Netlogo highlights everything right after "ask n-of 6 squad1s ["

    to solar-battery1
    ask n-of 6 squad1s [ 
    if solar-delay >= 85
    [set individual-power 15]
    if solar-delay >= 91 ; increase in 6 ticks..
    [set individual-power 100
    set individual-power individual-power - 1
    set solar-delay 0]
    ]
    end

Update... Having a similar issue again (n-of command isn't working) but have my turtle plurals ARE correct this time (hah). Any ideas? When I try to run this code, all five turtles perform the function, not the random 2 that I want.

    turtles-own
    [ battery_1
    ]

    to setup
    ca

    reset-ticks
    create-turtles 5
    ask turtles [
    set color blue
    set shape "default"
    set size 1.5
    set xcor 0
    set ycor who
    set battery_1 100 
    ]
    end

   to go
   turtles-use-power
   if recharge-options = "refresh-combat-load" [refresh-combat-load] 
   tick
   end


   to turtles-use-power
   ask turtles [
   if battery_1 >= 15
   [set battery_1 battery_1 - 4 * round random-float 1
   set label battery_1 ]
   ]
   end


   to refresh-combat-load ;;
   ask n-of 2 turtles [
   if battery_1 <= 15
   [set battery_1 100 ]]
   end
1 Answers

I think you just have your breed declaration backwards- it should be plural then singular forms. Try this toy model:

breed [squad1s squad1]

to setup
  ca
  create-squad1s 10 
  reset-ticks
end

to go
  ask n-of 6 squad1s [
    fd 1
  ]
end

You should see only 6 turtles moving per call to go. Note that if you change to this more common structure, you will need to update your other related calls from ask squad1 [ ... to ask squad1s.

If, however, you intended squad1 to be the plural, then your call to n-of should read as ask n-of 6 squad1 [....

Edit

I have slightly modified your code to check whether all turtles are 'refreshing combat load' and I'm not showing that behavior- only two turtles per tick are refreshing the load. With this (very slightly) modified version of your edited code:

turtles-own [ battery_1 ]

to setup
  ca
  reset-ticks
  create-turtles 5
  ask turtles [
    set color blue
    set shape "default"
    set size 1.5
    set xcor 0
    set ycor who
    set battery_1 100 
  ]
end

to go
  print word "Current tick: " ticks
  turtles-use-power
  refresh-combat-load
  tick
end


to turtles-use-power
  ask turtles [
    if battery_1 >= 15
    [set battery_1 battery_1 - 4 * round random-float 1
      set label battery_1 ]
  ]
end

to refresh-combat-load ;;
  ask n-of 2 turtles [
    show "I am refreshing combat load"
    if battery_1 <= 15 [
      set battery_1 100 
    ]
  ]
end

When I run go with the new print statement, only two turtles are being queried by refresh-combat-load:

enter image description here

Can you explain a little further what behavior you're expecting to see that is not occurring here?

Related