chatGTP and GEMINI code errors

I keep receiving errors (forever the same type or errors) in the code wrote by chatGTP and GEMINI about this simple simulation (a simplified version of Isobenefit Urbanism growth). Someone can help me please?

BASIC REQUIREMENTS

  1. Initialization:
  • All patches are green (land-use = "green").
  • One single white patch in the center (land-use = "centrality").
  • One black patch (land-use = "building") adjacent to the centrality.
  1. Subsequent steps (with go):
  • Each step adds ONE new black patch (building) randomly, only if:
    • 2a) It is within distance D (in patches) from at least one centrality (white).
    • 2b) It is within distance D from at least one green area (green).
    • 2c) It is adjacent (Moore or Von Neumann neighborhood) to at least one other building (black).
    • 2d) each green cell must be adjacent (Moore or Von Neumann neighborhood) to at least one green cell.
  • No new green cells are added.
  • A white cell (centrality) is added (at a distance D from the building cell farthest from the previous centrality) if no new building can be added due to condition 2a.
  • If no more patches satisfy all the conditions → stop.
  1. Colors:
  • Green: green
  • Building: black
  • Centrality: white
  1. Cell size = 100m x 100m
    → Therefore, D = 10 equals 1 km

:radio_button: Slider – D
• Name: D
• Min: 1
• Max: 50
• Initial: 10
• Increment: 1
• Unit: 100m

Yeah, since that there isn’t that much NetLogo code online to train on, LLMs are still pretty bad at NetLogo.

Please attach your NetLogo model and someone can help with whatever errors you are getting.

(Also, I’m going to move this to the New to NetLogo catory)

1 Like

thanks a lot! here the code from chatGTP but is full or errors:

(I edited your post to your post in a code block so it is easier to read as code. See tip #6 on this post)

A few things:

  • Replace all “ and ” with straight quotes ".
  • pxcor-of farthest-building should be [pxcor] of farthest-building

The end of the setup procedure needs to be inside of an ask patches like this:

After that, though you’ll likely get an error for that code, because adjacent-patch might be nobody. So, you’ll need to change it to:

That seems to solve all the errors.

it doesnt work since the very beggining: af the first ask: keyword expected

Could you attach your actual model file? (you can just drag it onto the message box to attach). Then I can make sure I’m seeing exactly what you are.

new users cannot
but I copy and paste here the code:
now the check is fine but when I click go another error appear…

globals [
D ; distance in patches (e.g., 10 for 1 km)
]

patches-own [
land-use ; “green”, “building”, or “centrality”
]

to setup
clear-all
set D 10 ; you can replace this with a slider if preferred

; Initialize all patches as green
ask patches [
set land-use “green”
set pcolor green
]

; Set the central patch as centrality (white)
let center patch 0 0
ask center [
set land-use “centrality”
set pcolor white
]

; Set one adjacent patch as building (black)
ask patches [
; Set one adjacent patch as building (black)
let adjacent-patch one-of neighbors4 with [land-use = “green”]
ask adjacent-patch [
set land-use “building”
set pcolor black
]
]
end

to go
; Try to find a new valid patch for building
let candidate-patches patches with [
land-use = “green” and
any? patches with [land-use = “centrality” and distance myself <= D] and
any? patches with [land-use = “green” and distance myself <= D] and
any? neighbors4 with [land-use = “building”]
]

; Ensure all green patches are adjacent to at least one other green
set candidate-patches candidate-patches with [
count neighbors4 with [land-use = “green”] > 0
]

; If at least one valid patch, add a building
if any? candidate-patches [
ask one-of candidate-patches [
set land-use “building”
set pcolor black
]
]

; If no valid patch and some building is too far from all centralities → add a new centrality
if not any? candidate-patches [
let buildings patches with [land-use = “building”]
let centralities patches with [land-use = “centrality”]

; Find the building farthest from any centrality
let max-distance 0
let farthest-building nobody

foreach sort buildings [
  b ->
  let min-dist min [distance b] of centralities
  if min-dist > max-distance [
    set max-distance min-dist
    set farthest-building b
  ]
]

; If the farthest building is beyond D, add a centrality at distance D from it
if max-distance > D [
  let dir random-float 360
  let new-x round ([pxcor] of farthest-building + D * cos dir)
  let new-y round ([pxcor] of farthest-building + D * sin dir)

  if patch new-x new-y != nobody and [land-use] of patch new-x new-y = "green" [
    ask patch new-x new-y [
      set land-use "centrality"
      set pcolor white
    ]
  ]
]

]

; If still no patch can be added, stop
if not any? patches with [
land-use = “green” and
any? patches with [land-use = “centrality” and distance myself <= D] and
any? patches with [land-use = “green” and distance myself <= D] and
any? neighbors4 with [land-use = “building”] and
count neighbors4 with [land-use = “green”] > 0
] [
stop
]

tick
end

You should be able to upload the file now. Can you do that and then I’ll see what the issue is?