octaviomarote
octaviomarote

Reputation: 1

Netlogo adding patches by grid percentage

I have added 4 different user input sliders into net-logo. Yellow, red and green should represent percentages of the total patches grid, whilst the blue one is on a set amount of 0-10 (non-percentage). The % sliders vary in value for both green red and yellow. When I'm setting up the grid with the following code, only the blue patches are being inserted in the grid. I'm also looking to continuously have the same % of each colored patch throughout the simulation. Any help is appreciated as I've been trying this one for a good couple hours without any luck googling.

  let total 961
  let p-yellow yellow-patches-amount-% * 100 / total
  let p-green (green-patches-amount-% * 100) / total
  let p-red  (red-patches-amount-% * 100) / total
  let p-blue blue-patches-amount
  
  let patch-yellow p-yellow
  
  ask patches with [pcolor = black] [
    if count patches with [pcolor = blue] < p-blue [
  ask n-of p-blue patches [set pcolor blue]
    ]
  ]
  
    ask patches with [pcolor = black] [
    if count patches with [pcolor = yellow] < p-yellow [
  ask n-of p-yellow patches [set pcolor yellow]
    ]
  ]
end

Upvotes: 0

Views: 181

Answers (1)

LeirsW
LeirsW

Reputation: 2305

Your problem is how you define p-yellow, and how you use your percentage. Let's say yellow-patches-amount-% is 50. Multiply that by 100 and you get 5000. divide that by 941 and you get 5.3 yellow patches. That number is then rounded down by n-of. The correct way of using percentages here is:

let p-yellow yellow-patches-amount-% / 100 * total

Then there are a few problems with how you recolor your patches. I will address them one by one.

In your code, you have a ask patches [ ask patches [...]] construction. This is almost always a horrible idea, since it means that every patch can be asked to do something patch number of times, instead of just once. Letting one agent ask all agents to do something can be okay. Letting all agents ask one agent to do something can be okay. But letting all agents ask all agents to do something will almost always lead to code not doing what you want. I simplified the code you were using to illustrate this.

to setup
  ca
  
  let total count patches ;1089 in this case
  let p-yellow 5 * total / 100 ;54.45 in this case
  
  ask patches with [pcolor = black] [
    if count patches with [pcolor = yellow] < p-yellow [
      ask n-of p-yellow patches [set pcolor yellow]
      show count patches with [pcolor = yellow] ;first 54, next 108 or a bit lower
    ]
  ]
end

With this setup, you would want 54 patches to become yellow. The first black patch that you call upon will now count all yellow patches, which is 0. Because of that, it now asks p-yellow, rounded down, random patches to turn yellow. Now the second black patch comes up and counts all yellow patches. It comes up with 54, which is lower than 54.45, so again it asks 54 random patches to turn yellow. By now you have more than 54.45 yellow patches so all subsequent patches count them but no longer ask any patches to change color. But that still means you turned a patch yellow 108 times instead of 54 times.

Even though you turned a patch yellow 108 times, you didn't necessarily turn 108 different patches yellow. If you ask n-of p-yellow patches [set pcolor yellow] once, then each patch can only be turned yellow once. This breaks down if you do it multiple times, since now ask n-of p-yellow patches [set pcolor yellow] can also ask a patch that you previously turned yellow to turn yellow again. This can be avoided by using ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow]. It is especially important since you are working with multiple colors.

Finally, the if count patches with [pcolor = yellow] < p-yellow construction makes sense if you are turning patches yellow one by one until you have >= p-yellow patches. The moment you turn more than 1 patch yellow at the same time, this sort of construction can easily let you overshoot your target.

With that in mind, I have two different suggestions for you

to setup-1
  ca
  
  let total count patches ;1089 in this case
  let p-yellow 5 * total / 100 ;54.45 in this case

  ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow] ;54 in this case

end

to setup-2
  ca
  
  let total count patches ;1089 in this case
  let p-yellow 5 * total / 100 ;54.45 in this case
  
  
  while [count patches with [pcolor = yellow] < p-yellow] [
    ask one-of patches with [pcolor = black] [set pcolor yellow]
  ] ;55 in this case
  
end

setup-1 simply turns the required number of patches yellow all at once and is done with it.

setup-2 turns black patches yellow one by one, until the count of yellow patches exceeds p-yellow. Note that this will get you 55 yellow patches instead of 54, since p-yellow > 54. If you would want it to be rounded down instead, you could use floor, which rounds a value down to the nearest integer: [count patches with [pcolor = yellow] < floor p-yellow]

Upvotes: 1

Related Questions