jeudi 17 octobre 2019

Netlogo: Nested if/ifelse statements

Building my first ABM using Netlogo and have a problem involving ifelse statements and how to use them. I'm modelling agents response to flooded properties. The concepts are as follows:

If an agent is flooded, they will consider adopting protective measures (if they haven't already). If an agent has adopted protective measures, and are flooded, the success of the measure is calculated.

My code is as follows:

    to process-property
  let $random-flood-number random-float 1
  ask properties [
    set flood-damage-list-consequent replace-item 1 flood-damage-list-consequent (item 1 flood-damage-list-initial * (1 - PLP-reduction))
    set flood-damage-list-consequent replace-item 2 flood-damage-list-consequent (item 2 flood-damage-list-initial * (1 - PLP-reduction))'
    ifelse $random-flood-number < probability-flooding
    [
      set flooded? TRUE
      set number-of-times-flooded (number-of-times-flooded + 1)
      if plp-adopted? != TRUE [
        calculate-adoption-intention
      ]
    ]
    [
      set flooded? FALSE
    ]
  ]

  ask properties with [plp-adopted? = TRUE] [
    plp-reliability-analysis
  ]

end

    to plp-reliability-analysis
  if plp-abandoned? = TRUE [stop]
  if flooded? = TRUE [
  if number-of-times-flooded > 1 [
    let plp-reliability-factor 0.77 ;;This variable represents the probability of success that Manual PLP will offer full reduction in damage. Taken from JBA (2012;2014).
    ifelse random-float 1 < plp-reliability-factor
      [
        set plp-deployed-successful? TRUE
        set PLP-reduction 0.25
        set successful-flood-damage-reduction (sum flood-damage-list-initial * PLP-reduction)
      ]
      [
        set plp-deployed-successful? FALSE
        set PLP-reduction 0.9
        set unsuccessful-flood-damage-reduction (sum flood-damage-list-initial * PLP-reduction)
        calculate-abandonment-intention
      ]
    ]
  ]
end

I have written the following code as an error check, which i keep getting:

if flooded? = FALSE and plp-deployed-successful? = TRUE [error["Properties should only deploy PLP when they are flooded"]]

I believe the problem lies in the ifelse statements in "plp-reliability-analysis" procedure. I'm new to Netlogo and am confused as to when to use an 'if' or 'ifelse' statement. If someone could explain and have a look at the above code i'd be very grateful.

Thanks, David

Aucun commentaire:

Enregistrer un commentaire