Some Complete Examples

Move According to Utility

globals [total-utility]
turtles-own [a b U]  ; Define variables that each turtle will own
patches-own [x y]  ; Define patch variables

; Define the Cobb-Douglas utility function
to-report cobb-douglas-utility [x1 x2 exp-a exp-b]
  ; Calculate the utility
  let utility (x1 ^ exp-a) * (x2 ^ exp-b)
  report utility
end

to setup
  clear-all
  ; Initialize patches with random values for x and y
  ask patches [
    set x random-float 10  ; Random float between 0 and 10 for x
    set y random-float 10  ; Random float between 0 and 10 for y
    
    let fertility x * y
    set pcolor scale-color green fertility 0 100

  ]
  ; Create turtles and assign values to a and b
  create-turtles 100 [
    set a random-float 1  ; Example exponent value for good x
    set b 1 - a ; Example exponent value for good y
    show a
    ; Set the turtle's initial position
    setxy random-xcor random-ycor
    set U 0
  ]
  reset-ticks
end

to go
  ask turtles [
    move-turtle
    consume
  ]
  deplete
  compute
  tick
end

to move-turtle
  ; Initialize variables to track the best utility and patch
  let best-patch nobody
  let best-utility U ; U of turtles #

  ; Ask each patch in the radius to compute the utility
  ask patches in-radius 1 [
    let utility cobb-douglas-utility x y [a] of myself [b] of myself  ; Compute utility

    ; Update best patch if this patch's utility is higher
    if utility > best-utility [
      set best-utility utility
      set best-patch self
    ]
  ]
  
 ; Move to the patch with the highest utility if one was found
  if best-patch != nobody [
    move-to best-patch
  ]
end

to consume
  ; Get the current values from the patch the turtle is on
  let current-patch patch-here
  let patch-x [x] of current-patch
  let patch-y [y] of current-patch

  ; Calculate utility using the turtle's `a` and `b` values
  let utility cobb-douglas-utility patch-x patch-y a b
  
  set U utility
end

to deplete
  ask patches [
    let n count turtles-here  ; Count the number of turtles on the patch
     ; Decrease resources by n, ensuring they do not go below 0
    set x x - n
    if x < 0 [ set x 0 ]  ; Check after subtraction
    set y y - n
    if y < 0 [ set y 0 ]  ; Check after subtraction

    
    ; Optionally update patch appearance based on depletion
    if (x + y) = 0 [set pcolor black]
  ]
end

to compute
  set total-utility sum [U] of turtles
  show total-utility
end

Move According to Utility (Self)

patches-own [QX QY] 

to-report utility [x y]
  report x * y
end

to setup
  clear-all
  create-turtles 10 [
    setxy random-xcor random-ycor
  ]
  ask patches [
    set QX random 10
    set QY random 10
    set pcolor scale-color green (QX * QY) 0 100 ; Ensure color is in a valid range
  ]
  reset-ticks
end

to go
  ask turtles [ move ]
  tick
end

to move
  let close-patches patches in-radius 1
  if any? close-patches [
    let eval max-one-of close-patches [ 
      utility [QX] of self [QY] of self 
    ]
    move-to eval
  ]
end