Raj
Raj

Reputation: 1071

Dynamic turtle creation in netlogo 2 [contd..]

In the interface tab i have a slider whose value ranges between 2 & 10. Depending on the value defined by the user using this slider, that many number of turtles should be created.

I tried using multiple if statements but there is a problem in the succeeding steps.

if (slider-value = 2) [create2] 
if (slider-value = 3) [create3] 
if (slider-value = 4) [create4] 
if (slider-value = 5) [create5]

After creating the turtles using the above if conditions, i have to assign additional rules to each individual turtle like co-ordinate position, rules for how they should move etc. and i tried again using multiple if statements. But it doesn't seem to work.

for example the sub-query for create is as follows

to create2
  create-challengers 2
  ask turtle 0 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  ask turtle 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]

  ask turtles [ update-rule set old-mysize 0 set shape "default"]

end

for creating 3 turtles:

to create3
  create-challengers 3
  ask turtle 0 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  ask turtle 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]
  ask turtle 2 [set color green set label-color green set size 2 
               set xcor party3-left-right ]

  ask turtles [ update-rule set old-mysize 0 set shape "default"]

end

so on and so forth.

The main problem is even though program works irrespective of how many turtles the user has defined, all the 10 gets created but only user defined number of turtles move, i.e. if the user has assined 3 then when i run the program 10 turtles are created but only 3 move.

Is there a way to get around like in other programming languages where one can simply use an if-else statement.

Can someone suggest a way, would really appreciate the help.

Thanks in advance!

After the turtles are created i assign certain rules for them to move:

to update-rule  
  if (slider-values = 2) [update2]
  if (slider-values = 3) [update3]
  if (slider-values = 4) [update4]
  if (slider-values = 5) [update5]
end

And once again i create multiple sub-rule for update2, update3 which are basically for the each turtle depending on how many the user has defined:

If there are 2 turtles:

to update2 
  ask turtle 0 [set my-rule party1-rule] 
  ask turtle 1 [set my-rule party2-rule]
end

in case of 3 turtle:

to update3
  ask turtle 0 [set my-rule party1-rule] 
  ask turtle 1 [set my-rule party2-rule]
  ask turtle 2 [set my-rule party3-rule] 
end

Below are the movement rules

to adapt
  if (my-rule = "hunter") [hunt]
  ;;NB stickers do nothing
  if (my-rule = "aggregator") [aggregate]

end


to hunt                 
  ifelse (mysize > old-mysize) [jump 1] [set heading heading + 90 + random-float 180 jump 1]
  set old-mysize mysize 
end

to run-general
  create2 create3 create4 create5 create-voters update-support
  ask challengers [adapt] update-support

end

to go
  run-general

  tick

end

Upvotes: 0

Views: 139

Answers (3)

Geerten
Geerten

Reputation: 11

this if-else thing for a slider based setup is very nasty looking.

an alternative is to have a set of (global) lists for all properties that are set seperately for each individual. then you can use the following code

;assuming global_list1 and global_list2 exist

to setup
  let index 0
  create-turtles slidervalue [
                               set turtle_value1 item index global_list1
                               set turtle_value2 item index global_list2
                               set index index + 1
                             ]
end

then you only need to make sure that each turtles will know what to do when asked to do something

Geerten

Upvotes: 0

JenB
JenB

Reputation: 17678

Okay, try editing your code to look like this:

to adapt
  type "Hunters: " print count turtles with [my-rule = "hunter"]
  type "Aggregators: " print count turtles with [my-rule = "aggregator"]
  if (my-rule = "hunter") [hunt]
  if (my-rule = "aggregator") [aggregate]
end

This is a standard debugging trick. It will print the number of hunters and aggregators to the Command Center (bottom of the interface tab). This allows you to work out where code is going wrong. For example, in your case, it will let you know if the problem is with the movement code or with the code that assigns rules to turtles.

Another trick is to have something like print "Got to hunt procedure" if you are not sure whether a procedure is even being reached.

Upvotes: 1

JenB
JenB

Reputation: 17678

As noted by Seth, the problem is likely to be in the move part of the code since the turtles are being created. Just as general comments, it looks like your code for creating 3 turtles is identical to that for creating 2 turtles except for the additional turtle. If this is generally true, your code would be much easier if you used a format like:

to setup-challengers
  create-challengers 1 [set color blue set label-color blue set size 2 
                set xcor party1-left-right ]
  create-challengers 1 [set color red set label-color red set size 2 
                set xcor party2-left-right ]
  if slider-value >= 3
    [ create-challengers 1 [ set color green set label-color green set size 2
                           set xcor party3-left-right ] ]
  if slider-value >= 4
    [ create-challengers 1 [ <whatever you want number 4 to look like> ] ]

  ask challengers [<do stuff>]
end

This way you only need the code for each turtle to be written once so if you change your mind about colours or something, it is much easier to change.

Upvotes: 4

Related Questions