randommodelnetlogopolicyeuropean-data-format

NetLogo Plots not being updated in the interface


i have been writing code for simulating the new CSDDD policy from EU for my research paper. But the code doesn't run properly and there's always some error. i solved some of them but then there's a new one. I am new to Netlogo and would be grateful if someone can help me please. Thank you!

Problem: The plots i have created on the netlogo interface aren't being updated with the visuals. I changed the code a lot of time but still no good. Here's the updated code:

globals [compliance-threshold-level penalty-rate num-compliant]

turtles-own [sustainability-score is-company? inspected]

to setup
  clear-all
  set-default-shape turtles "circle"

  ;; Create companies
  create-turtles num-companies [
    set is-company? true
    set color blue
    setxy random-xcor random-ycor
  ]

  ;; Create suppliers
  create-turtles num-suppliers [
    set is-company? false
    set sustainability-score random 101
    set color green
    setxy random-xcor random-ycor
  ]

  ;; Set parameters
  set compliance-threshold-level 70
  set penalty-rate 10
  set num-compliant 0

  reset-ticks
end

to go
  ask turtles with [is-company?] [
    inspect-suppliers
    check-compliance
  ]
  update-visuals
  tick
end

to update-visuals
  ;; Update the compliance plot
  set-current-plot "Compliance Over Time"
  clear-plot
  plot num-compliant
  
  ;; Update the average sustainability score plot
  set-current-plot "Average Sustainability Score"
  clear-plot
  let total-score sum [sustainability-score] of turtles with [not is-company?]
  let total-suppliers count turtles with [not is-company?]
  let average-score ifelse-value (total-suppliers > 0) [total-score / total-suppliers] [0]
  plot average-score

  ;; Update the supplier score distribution histogram
  set-current-plot "Supplier Score Distribution"
  clear-plot
  histogram [sustainability-score] of turtles with [not is-company?]
end

to inspect-suppliers
  let suppliers turtles with [not is-company?]
  let num-suppliers count suppliers

  ;; Determine how many suppliers to inspect
  let num-to-inspect min (list 5 num-suppliers)

  if num-to-inspect > 0 [
    let to-inspect n-of num-to-inspect suppliers
    ask to-inspect [
      set inspected true
    ]
  ]
end

to check-compliance
  let inspected-suppliers turtles with [inspected = true and not is-company?]

  if any? inspected-suppliers [
    let total-score sum [sustainability-score] of inspected-suppliers
    let count-inspected count inspected-suppliers

    ;; Calculate average score
    let average-score ifelse-value (count-inspected > 0) [total-score / count-inspected] [0]

    ;; Compliance check
    if average-score >= compliance-threshold-level [
      set num-compliant num-compliant + 1
      ask turtles with [is-company?] [set color green]
    ]
    if average-score < compliance-threshold-level [
      ask turtles with [is-company?] [set color red]
      ;; Apply penalty ensuring sustainability score doesn't go below 0
      ask inspected-suppliers [
        set sustainability-score max (list 0 (sustainability-score - penalty-rate))
      ]
    ]

    ;; Reset inspected flag
    ask inspected-suppliers [
      set inspected false
    ]
  ]
end

Solution

  • I assume your problem is not, that the plots are not being updated, but that you use clear-plot in each tick and thereby remove the data of the previous ticks. If you want to plot your metrics over time, omit the clear-plot!