Partners Example

Partners Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 304 times • Downloaded 20 times • Run 2 times
Download the 'Partners Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own
[
  partner   ;;  the turtle that is our partner, or "nobody" if we don't have one
]

;;  This procedure will setup 150 turtles, by randomly placing them around the world.

to setup
  clear-all
  create-turtles 150
  [
    ;;  turtles get random coordinates, and they are
    ;;  light gray (to show they don't have partners yet).
    setxy random-xcor random-ycor
    set color gray + 2
    set partner nobody
  ]
  reset-ticks
end 

;; have each turtle move until they find a partner at which point they turn red

to find-partners
  let singles turtles with [partner = nobody]
  if not any? singles [ stop ]

  ;;  In this example, only turtles that haven't found a partner can move around.
  ask singles
  [
    ;;  Randomly move about the world
    lt random 40
    rt random 40
    fd 1
  ]

  ;;  Now that all unpartnered turtles have moved, ask the unpartnered turtles to check if they
  ;;  are on a patch with a turtle that also don't have a partner.
  ask turtles
  [
    if (partner = nobody) and (any? other turtles-here with [partner = nobody])
    [
      ;;  There could be more than one partner to choose from, so we use one-of
      ;;  to randomly choose one of the turtle without a partner
      set partner one-of other turtles-here with [partner = nobody]

      ;;  We graphically show that we have a partner by turning red.
      set color red

      ask partner [
        ;; Ask our partner to partner with ourselves.
        set partner myself
        ;;  Color my partner red too
        set color red
      ]
    ]
  ]

  ;;  As a check on the correctness of the code, let's make sure that
  ;;  the partner assignments are consistent.
  if any? turtles with [partner != nobody and [partner] of partner != self]
  [
    ask one-of turtles with [[partner] of partner != self]
    [
      user-message (word "Oops! Partner mismatch: " self
                         " is not partnered to " myself)
    ]
    stop
  ]
  tick
end 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 9 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 10 years ago Updated version tag Download this version
Uri Wilensky over 10 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 12 years ago Partners Example Download this version
Uri Wilensky over 12 years ago Partners Example Download this version

Attached files

File Type Description Last updated
Partners Example.png preview Preview for 'Partners Example' over 9 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.