Sadaf
Sadaf

Reputation: 163

Rules of use of chooser in Netlogo Model

I am writing a model and want to add a button of chooser to assess alternative management options. Model was working fine as some variables graphs disappeared as I have added chooser for around all of turtles. I have also tried changing parameters from Turtle-own to global and vice versa and not rectified the error. I couldn't figure out the issus. Problem arise when i add chooser in irrigate procedure. Codes are bit longer, please bear with me. I will be grateful for the solution. Please help me to get out of difficult situation. I have also shared before and after image of interface.

Before

Before

and after

after

Thanks

Globals [farmlocations  farmers-list   water eva  IWR-perday ASW maxgwater usedflow  wdemand wtdmean wtdsd av-wdischarge wdischarge wd totannual-wdischarge current-demand wtd canal-wdischarge water-right alpha beta]
Breed [Farmers farmer]
farmers-own [ irrigation-turn season1-profit  swh  season2-profit yield myflow DWT total-gw-cost watercosts total-sw-cost raqba sw benefits evapotrans totaldemand swcost gwateruse  wneed logging gwcost salinity aw  totalcosts dws  resources    irrigation-turn  gw   AGW mywaterloss] ;swh= surface water height aw= old surface water height
Patches-own [gw-level]
links-own [flow]
to setup
  clear-all
  setup-farmers
  import-data
  reset-ticks
end
to setup-farmers
    create-farmers 5 [ set irrigation-turn [0]
    ask farmer 0 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
    ask farmer 1 [ setxy min-pxcor + 1 0 set irrigation-turn 2]
    ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
    ask farmer 3 [ setxy min-pxcor + 3 2 set irrigation-turn 3]
    ask farmer 4 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
                          ]
                      set farmers-list sort turtles
    set eva 0.3 
    ask farmers [set label who + 1
    set size 1
    set myflow 0
    If land = "small farmers"  [ set raqba random 5 + 3 ]
    If land = "small and large farmers" [ ifelse random 3 = 1 [set raqba  10 + random 20 ] [ set raqba  5 + random 5]]
    set total-sw-cost 500 * raqba
    Set resources 1
    set DWS  5 + 1 * (who) 
    set evapotrans  5 + 1 * (who)
    set water-right 4.5 * raqba
    Set logging 500 / DWS
    Set salinity  DWS / 5
    set maxgwater 200    
    set wtdmean 10
    set wtdSD 5              
    set sw 0                 
    set swh 0
    set aw 0 
    set gw-level  100 / dws
    set gw 50000 + random 1000
     Depth-to-Water-Table
    ground-water-travel
    water-losses
    set alpha 1  
    set beta 0.7 

    ]
     set av-wdischarge  4.5
    end
to Depth-to-Water-Table
 set  DWT Water-Table-Depth  + DWS / 10 
  end
to ground-water-travel
  set  gw gw +  10 / (random-float 1 + DWT) * DWS
                             set AGW gw + (sum [gw-level] of patches in-radius 2)
                              set pcolor scale-color blue AGW 1 max [gw-level] of patches in-radius 2
end                      
to water-losses
   Set mywaterloss  waterloss + 5 * DWS ; 5 + 1* who
    end
to go
tick
if ticks = 630 [ stop ] 
calculate-sw
evaporate
irrigate
 ask turtles [set color scale-color blue swh 30 3]
 calculate-profits
 update-output
 
end
to
calculate-sw
   let list-element floor ticks / 5
  ask item (ticks mod 5) farmers-list [
  set canal-wdischarge item list-element totannual-wdischarge
  set wdischarge canal-wdischarge - (canal-wdischarge * discharge-reduction)]
end
to evaporate
  let list-element floor ticks / 5
  ask item (ticks mod 5) farmers-list [
  set current-demand item list-element IWR-perday
  set Aw  swh
  if aw >= current-demand [set wneed 0]
  set wneed 0
  set wneed wneed + current-demand - aw
  ask farmers [  set totaldemand current-demand * raqba + evapotrans]
  if wneed < 0 [set wneed 0]
  set swh swh - swh * eva 
  if swh < 0 [set swh 0]
  ]

end
to irrigate
          If water-use-mangement =  "Business as usual" [
          ask turtles with [irrigation-turn = (ticks mod count farmers)]
          [ set color red
           set shape "person"
            set pcolor brown + who
             set ASW (Wdischarge - myWaterloss)* raqba 
            if ASW < 0 [set ASW 0]
            set myflow ASW
            ifelse wneed > 0 [
                              set swh myflow / raqba + aw 
                              ifelse swh > current-demand [set swh current-demand ] [use-gw] 
                              ifelse swh > aw [set usedflow (swh - aw) * raqba 
                              if usedflow < 0 [ set usedflow 0]
                              set ASW ASW - usedflow
                              if ASW < 0 [ set ASW 0]
                                         update-gw-use ]
                                           [set usedflow 0]]                                                                                                                                                                                          [
                                                                    set myflow 0
                                                                    set swh aw + ((myflow ) / raqba)
                                                                     if swh > current-demand [set swh current-demand ]]]]    
                                                                                                                    end

to use-gw
Ask farmers [ let excess-demand (current-demand - swh) * raqba
              if AGW > excess-demand [ ;set excess-demand AGW
                                       set gwateruse agw - excess-demand
                                       set dwt dwt + (0.2 + who / 20)
                                       Ifelse DWT > 8 [ set gwcost excess-demand * 17.5] [set gwcost excess-demand * 5]
                                       set agw agw - excess-demand
                                       If AGW < 0 [ set AGW 0]
                                       Ifelse excess-demand > 0.5 * current-demand [ifelse random 4 = 1 [ set salinity salinity + 0.1 
                                                                                   set logging logging - 0.05]
                                                                                   [set salinity salinity + .05 
                                                                                    set logging logging + 0.05 ]]                                                                                                                                                                                       [set salinity salinity - 0.05 
                                                                                     set logging   logging + 0.1]]]
                                                                                                   set gw gw - 1
                                                                                                              end
to  update-gw-use
   set salinity salinity + .04
   set logging logging + 0.01
   set gw gw - 1
   set total-gw-cost total-gw-cost + gwcost
end

to calculate-profits

 ask farmers [ if ticks mod 630 = 252 [ set total-sw-cost raqba * 500    
                           set watercosts total-gw-cost +  total-sw-cost
                           ;update-yield
                           calculate-season1-profit
                           ]]

 ask farmers [ if ticks mod 630 = 0 [ set total-sw-cost raqba * 500      
                           set watercosts total-gw-cost +  total-sw-cost
                           calculate-season2-profit]]   
end

to calculate-season1-profit
 ask farmers [ set yield yield +  ((usedflow / totaldemand ) * alpha + ( gwateruse / totaldemand)* beta) * 55  - 0.1 * (55 * salinity / 12) - 0.1 *(55 * logging / 12) ; 55mon/acre market price
  let price 1350 ; price/ maund
  set season1-profit yield * price - watercosts
  ]
end
to calculate-season2-profit
 ask Farmers[  set yield yield +  ((usedflow / totaldemand ) * alpha  + (gwateruse / totaldemand)* beta ) * 100 - 0.1 * (100 * salinity / 18) - 0.1 * (100 * logging / 18) ; 100 mauns/acre market price
  let price 2500 ; price/ maund
  set season2-profit yield * price - watercosts]
end
to update-output

  set-current-plot "Available-surface-water"
  set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [swh] of turtle 0] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [swh] of turtle 1] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [swh] of turtle 2] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [swh] of turtle 3] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [swh] of turtle 4] [plot 0]
  
  set-current-plot "Logging"
 set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [logging] of turtle 0] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [logging] of turtle 1] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [logging] of turtle 2] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [logging] of turtle 3] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [logging] of turtle 4] [plot 0]
 
   set-current-plot "Salinity"
 set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [salinity] of turtle 0] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [salinity] of turtle 1] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [salinity] of turtle 2] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [salinity] of turtle 3] [plot 0]
  set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [salinity] of turtle 4] [plot 0]
  end
to import-data
   ifelse ( file-exists? "totannual-wdischarge.txt" )
  [
    set totannual-wdischarge []
    file-open "totannual-wdischarge.txt"
    while [ not file-at-end? ]
    [
      set totannual-wdischarge sentence totannual-wdischarge (list file-read)
    ]
    file-close
  ]
  [ user-message "There is no totannual-wdischarge.txt file in current directory!" ]


   ;; Import water demand
  ifelse ( file-exists? "IWR-perday.txt" )
  [
    set IWR-perday []
    file-open "IWR-perday.txt"
    while [ not file-at-end? ]
    [
      set IWR-perday sentence IWR-perday (list file-read)
    ]
    file-close
  ]
  [ user-message "There is no IWR-perday.txt file in current directory!" ]

end

to-report _clarify-duplicate-plot-pen-name [ name ]
  let name-map [["Min" "Min"] ["MIn" "MIn_1"]]
  let replacement filter [ rename -> first rename = name] name-map
  let reported-name name
  if not empty? replacement [
    set reported-name item 1 replacement
  ]
  report reported-name
end

Upvotes: 0

Views: 950

Answers (1)

JenB
JenB

Reputation: 17678

The plots are working, it's just that they are showing 0. My best guess is that the text you have inside the chooser for the options is different than the text you are testing in your if statement, so the if statement is always false.

Try something like this:

ifelse water-use-mangement =  "Business as usual"
[ ; all your irrigation code that you already have
]
[ print "ERROR: Management case not found"
]

This will print an error message if the text doesn't match.

Upvotes: 1

Related Questions