Question

I am expanding a model looking at the effect of different types of fruit and fragmentation on a population of monkeys. The model reads a landsat file made up of 1's and 0s (representing forested and deforested areas) I need to reclassify the 1's into three kinds of trees. At the moment in the code I have the three types of trees randomly set, now I want them to be divided into percentages i.e 30 percent of the forested patches are blue, 50 percent are yellow, 20 are green. I have this atm: I think I have to change the:

[ if value = 1 [ set value one-of tree-types ]

I tried with n-of but I can't seem to make it work.

Any suggestions?

to load-forest 
file-open "patch46.txt" 
let tree-types [ 1   2   3 ] 
let tree-colors [ 0 15 65 45] 
let tree-fruits [ 0 25 50 100 ] 

foreach n-values world-height [ min-pycor + ? ] 
[let num-lines ? 
foreach n-values world-width [ min-pxcor + ? ] 
[let num-cols ? 
    ask patch num-lines num-cols 
    [set value file-read 
    ] 
  ] 
] 
file-close 

ask patches 
 [ if value = 1 [ set value one-of tree-types ] 
   set pcolor item value tree-colors 
   set fruit item value tree-fruits 
 ] 
end 
Was it helpful?

Solution

One possibility for you would be to use the rnd:weighted-one-of primitive from the NetLogo Rnd extension.

It works a bit like one-of, but the probability of selecting each item can be different (i.e., weighted).

Supposing you have a list of probabilities for your different types of trees:

let probabilities [ 30 50 20 ]

You can use it as:

set value rnd:weighted-one-of tree-types [ item (? - 1) probabilities ]

Note that this will give you on average 30% of blue trees, 50% of yellow trees and 20% of green trees, but it might vary from run to run. Since you were already using one-of, which has the same effect (i.e., on average 33% of each type) I assume you are OK with that. If you wanted exact percentages every run, the answer would be very different.

Side note:

Your tree-colors and tree-fruits lists have a "dummy" 0 item just so you can use the tree type value directly as an index for those lists. That's confusing, and bad practice for a variety of reasons. For example, show length tree-fruits will print 4. It also means that you cannot use tree-types and the other lists in the same foreach or map construct, e.g.: (foreach tree-types tree-fruits [...]). Those things may not seem like a big deal now, but they add up in the end and make your program more difficult to understand.

You should either bite the bullet and use value - 1 as an index, or renumber your tree types to be [ 0 1 2 ].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top