Improved K-means Clustering Model

No preview image

1 collaborator

Default-person Tânia Duarte (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.1 • Viewed 246 times • Downloaded 10 times • Run 0 times
Download the 'Improved K-means Clustering Model' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

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

Click to Run Model

breed [datapoints datapoint]
breed [centroids centroid]

globals [any-centroids-moved?]

to setup
  clear-all
  set-default-shape datapoints "circle"
  set-default-shape centroids "x"
  generate-datapoints
  reset-centroids
end 

to setup2
  clear-all
  set-default-shape datapoints "circle"
  set-default-shape centroids "x"
  generate-datapoints
  reset-centroids3
  reset-ticks
end 

to generate-datapoints
  repeat num-datapoints [
    let center-x random-xcor / 1.5
    let center-y random-ycor / 1.5
    create-datapoints num-datapoints [
      setxy center-x center-y
      set heading random 360
        ]]
end 

to reset-centroids
  set any-centroids-moved? true
  ask datapoints [ set color grey ]

  let colors base-colors
  ask centroids [die]
  create-centroids num-centroids [
    move-to one-of datapoints
    set size 5
    set color last colors + 1
    set colors butlast colors
  ]
  ;;clear-all-plots
  reset-ticks
end 

to reset-centroids2
  set any-centroids-moved? true
  ask datapoints [ set color grey ]

  let colors base-colors
  ask centroids [die]
  create-centroids num-centroids [
    move-to one-of datapoints
    set size 5
    set color last colors + 1
    set colors butlast colors
  ]
end 

to reset-centroids3
  set any-centroids-moved? true
  ask datapoints [ set color grey ]

  let colors base-colors
  ask centroids [die]
  create-centroids 1 [
    move-to one-of datapoints
    set size 5
    set color last colors + 1
    set colors butlast colors
  ]
end 

to go
  if not any-centroids-moved? [stop]
  set any-centroids-moved? false
  assign-clusters
  update-clusters
  tick
end 

to go2
  set num-centroids (ticks + 1)
  reset-centroids2
  while[any-centroids-moved?][
    set any-centroids-moved? false
    assign-clusters
    update-clusters2
  ]
  tick
end 

to assign-clusters
  ask datapoints [set color ([color] of (closest-centroid) - 2)]
end 

to update-clusters
  let movement-threshold 0.1
  ask centroids [
    let my-points datapoints with [ shade-of? color [ color ] of myself ]
    if any? my-points [
      let new-xcor mean [ xcor ] of my-points
      let new-ycor mean [ ycor ] of my-points
      if distancexy new-xcor new-ycor > movement-threshold [
        set any-centroids-moved? true
      ]
      setxy new-xcor new-ycor
    ]
  ]
  update-plots
end 

to update-clusters2
  let movement-threshold 0.1
  ask centroids [
    let my-points datapoints with [ shade-of? color [ color ] of myself ]
    if any? my-points [
      let new-xcor mean [ xcor ] of my-points
      let new-ycor mean [ ycor ] of my-points
      if distancexy new-xcor new-ycor > movement-threshold [
        set any-centroids-moved? true
      ]
      setxy new-xcor new-ycor
    ]
  ]
end 

to-report closest-centroid
  report min-one-of centroids [ distance myself ]
end 

to-report square-deviation
  report sum [ (distance myself) ^ 2 ] of datapoints with [ closest-centroid = myself ]
end 

to-report mean-square-deviation
  report mean [ square-deviation ] of centroids
end 



; Copyright 2014 Uri Wilensky.
; See Info tab for full copyright and license.

There is only one version of this model, created 10 months ago by Tânia Duarte.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.