Wessel K
Wessel K

Reputation: 55

Netlogo: repeat code with different values

My question is regarding to netlogo.

My variable has to be changed every time it gets bigger than a certain value. However the code is somewhat long and only the threshold values change.

Is there a way to right this code only once and pick the numbers from a list. Like the first time it goes through the code it takes the first list number the second time the second etc.

this is the code I need it for:

if Low-amount > 0.025 * number-of-companies and generationL = 1 [
    set generationL generationL + 1
    set Low-multiplier Low-multiplier * 0.8 ]
if medium-amount > 0.025 * number-of-companies and generationm = 1 [
    set generationm generationm + 1
    set Medium-multiplier Medium-multiplier * 0.8 ]
if high-amount > 0.025 * number-of-companies and generationh = 1 [
    set generationh generationh + 1
    set high-multiplier High-multiplier * 0.8 ]

the only things that change are the threshold variable 0.025 into 0.125 0.25 0.5 0.75 and 1 and the generation variable in the if.

I prefer to not write this code 6 times, even though it takes not a lot of time it just looks ugly and can be done easier. I looked into foreach and map but could not find the right way.

Upvotes: 0

Views: 75

Answers (1)

Wessel K
Wessel K

Reputation: 55

I found it out with foreach. the code looks like this now

(foreach [0.025 0.16 0.5 0.84 1] [1 2 3 4 5] [0.8 0.64 0.512 0.4096 0.32768] [ [a b c] ->
  if Low-amount > a * count companies with [t-needed = "low"] and generationL = b [
    set generationL generationL + 1
    set Low-multiplier c ] 
  if medium-amount > a * number-of-companies and generationm = b [
    set generationm generationm + 1
    set Medium-multiplier c ]
  if high-amount > a * number-of-companies and generationh = b [
    set generationh generationh + 1
    set high-multiplier c ]
])

Upvotes: 1

Related Questions