netlogoagent-based-modeling

NetLogo: How to make turtles (1) move to a patch with patch variable matching condition based on turtle variable and (2) take patch id as variable


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

Solution

  • 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