0

I'm trying to do the following:

  1. Ask turtles (households here) to check whether there are any patches that have (1) is-home = true and (2) cost less than the household's money;
  2. Take as my-home the patch id of the first patch that meets these qualifications;
  3. Pass the household id to the patch's patch-owner variable; and
  4. Move to that patch.
  5. If they don't find a patch that meets the criteria, the household dies and the displaced counter increments by 1.

UPDATE: Thanks for the assists so far. Now after adding the condition and not any? households-here to prevent households from occupying the same patches, pressing Go triggers the error MOVE-TO expected input to be an agent but got NOBODY instead. Sorry to reopen this question, but can anyone provide further guidance?

breed [ households household ]

globals [
  homes
  num-households
  number-displaced
]

households-own
[ 
  my-home
  money
  owner?
]

patches-own
[
  is-home?
  cost
  occupied?
  patch-owner
]

to-report random-exponential-in-bounds [mid mmin mmax]
  let result random-exponential mid
  if result < mmin or result > mmax
    [ report random-exponential-in-bounds mid mmin mmax ]
  report result
end

to setup-households
  ask households 
  [ set money round random-exponential-in-bounds 50000 0 1000000 ]
end

to setup-patches
  ask n-of 100 patches
  [ set is-home? one-of [ true false ]
    set cost 500 ];random-normal 173000 43000 ] 
end

to setup
  ca
  set-default-shape households "person"
  set num-households 100
  create-households num-households
  setup-households
  setup-patches
  reset-ticks
end

to choose-house
  ifelse any? patches with [ is-home? = true and cost <= [money] of myself and not any? households-here ]
  [ ask households [ move-to one-of patches 
    with [ is-home? = true and cost <= [money] of myself and not any? households-here ]  
    set owner? true
    set my-home patch-here
  ] 
    set occupied? TRUE
    set patch-owner households-here
  ]
  [ set number-displaced number-displaced + 1
    die ]
end

to go
  ask households [ choose-house ] 
  tick
end
Abe
  • 393
  • 2
  • 13
  • You should `reset-ticks` at the end of `setup`, not the beginning. And at the end of `go`, you should add `tick`. – Seth Tisue Feb 27 '23 at 23:07

1 Answers1

1

I see a few problems with your code.

setup-households: You never actually create any turtles. You only ask the (nonexistent) turtles to change their money.

setup-patches: I'm not sure what you are doing here. You ask 100 patches to maybe become a home and maybe not become a home. Why not just ask 50 patches to become a home? Or ask all patches to become a home but adjust your world size to the number of homes you want? I would also suggest to use scale-color, in order to visualise the cost of all patches.

choose-house: You are trying to check the money of myself, without actually having a myself available (currently it is called by the observer). Moving ask households to the start of the procedure fixes this. I should note that currently multiple households can move into the same house. Finally, you misspelled turtles-here

to choose-house
  ask households [ 
    ifelse any? patches with [ is-home? = true and cost <= [money] of myself ] [ 
      
      move-to one-of patches with [ is-home? = true and cost <= [money] of myself ]
      set owner? true
      set my-home patch-here
      set my-owner turtles-here
    ] [
      set number-displaced number-displaced + 1
      die 
    ]
  ]
end
LeirsW
  • 2,070
  • 4
  • 18
  • thank you for the response and edits. I realized after posting the error of missing the `ask households` command. Do you have any recommendations for what to add to ensure that only one household can occupy a patch at a time? – Abe Feb 27 '23 at 05:05
  • @Abe You could add another condition that they only move to patches where there isn't already a household, using `not any? households-here`. Or you could work with an extra patch variable, called occupied or something, that is changed when someone moves in – LeirsW Feb 27 '23 at 11:28