Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

netlogo - Change the color of the patch based on income level

Hi, this is a continuation of my previous question. Basically I'm trying to differentiate the patches by its income level and once I have established this I will set a monitor to count them. However, when I run the model the patches are not updating. Where am I going wrong? Appreciate your assistance.

 turtles-own [wealth]
patches-own [income]



to setup
  ca
  setup-turtles
  setup-patches
  reset-ticks

end

to setup-turtles
  create-turtles num-turtles
  ask turtles
  [
    set shape "person"
    set size 1
    setxy random-xcor random-ycor
    set wealth 100
  ]
end

to setup-patches
  ask n-of 2000 patches [ set pcolor green ] ;; to identify patches that will accumulate income from turtles
end


to go
  if not any? turtles with [wealth > 0] [stop]
  move-turtles
  spend
  tick
end

to move-turtles  ;; turtles to stop once they are spend all their wealth
  ask turtles [
   ifelse wealth > 0 
    [rt random 360 forward 1]
    [stop]

  ]

end

to color-patches  ;; patch colors to change based on their income level
  ask patches [
    ifelse income > 0 and income < 100 [
      set pcolor 15
    ] [
      ifelse income > 100 and income < 200 [
        set pcolor 45
      ] [
        ifelse income > 200 [
          set pcolor 64
        ] [
          set pcolor green
        ]
      ]
    ]
  ]

end

to spend
  ask turtles with [wealth > 0] [
    if pcolor = green [
      set wealth wealth - 1
      set income income + 1
    ]
   ]
end
question from:https://stackoverflow.com/questions/66057427/change-the-color-of-the-patch-based-on-income-level

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer is that you created the procedure to update patch colours but you never told NetLogo to run it. You probably want to add a line to your go procedure, I think this order:

to go
  if not any? turtles with [wealth > 0] [stop]
  move-turtles
  spend
  color-patches               ; this is what you are missing
  tick
end

This is not relevant to your question, but I also noticed that you have this code for moving:

to move-turtles  ;; turtles to stop once they are spend all their wealth
  ask turtles [
   ifelse wealth > 0 
    [rt random 360 forward 1]
    [stop]
  ]
end

You don't need to ask every turtle and then use stop to exit for some of them, instead it is generally easier to filter the turtles using with. So that code could be replaced with:

to move-turtles
  ask turtles with [wealth > 0] [
    rt random 360
    forward 1
  ]
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...