cluster.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Cluster Analysis of a set of objects with a distance measure
  21. (declare (usual-integrations))
  22. (define (cluster objects cluster-separation distance)
  23. (if (null? objects)
  24. '()
  25. (let merge-lp ((clusters (map make-singleton-cluster objects)))
  26. (if (null? (cdr clusters))
  27. clusters
  28. (let ((candidates '()) (min-d 1e307)) ;+infinity
  29. (let scan-lp-1 ((c1 clusters))
  30. (if (null? (cdr c1))
  31. (merge-lp (cons (merge-2-clusters candidates distance)
  32. (multiset-difference clusters candidates)))
  33. (let scan-lp-2 ((c2 (cdr c1)))
  34. (let ((d (cluster-separation (car c1) (car c2))))
  35. (if (< d min-d)
  36. (begin (set! candidates (list (car c1) (car c2)))
  37. (set! min-d d)))
  38. (if (null? (cdr c2))
  39. (scan-lp-1 (cdr c1))
  40. (scan-lp-2 (cdr c2))))))))))))
  41. (define (multiset-difference s1 s2)
  42. (if (null? s2)
  43. s1
  44. (multiset-difference (remove-one (car s2) s1)
  45. (cdr s2))))
  46. (define (remove-one x s)
  47. (cond ((null? s) '())
  48. ((eq? x (car s)) (cdr s))
  49. (else
  50. (cons (car s)
  51. (remove-one x (cdr s))))))
  52. ;;; A cluster has: elements, a diameter, the subclusters it was made from.
  53. (define (merge-2-clusters clusters distance)
  54. (let ((c1s (cluster-elements (car clusters)))
  55. (c2s (cluster-elements (cadr clusters))))
  56. (make-a-cluster (append c1s c2s)
  57. (max (apply max
  58. (apply append
  59. (map (lambda (c1)
  60. (map (lambda (c2)
  61. (distance c1 c2))
  62. c2s))
  63. c1s)))
  64. (cluster-diameter (car clusters))
  65. (cluster-diameter (cadr clusters)))
  66. clusters)))
  67. (define (make-a-cluster elements diameter subclusters)
  68. (list elements diameter subclusters))
  69. (define (cluster-elements cluster) (car cluster))
  70. (define (cluster-diameter cluster) (cadr cluster))
  71. (define (cluster-subclusters cluster) (caddr cluster))
  72. (define (make-singleton-cluster el)
  73. (make-a-cluster (list el) 0 '()))
  74. (define (set-separation element-distance)
  75. (lambda (cl1 cl2)
  76. (let ((c1s (cluster-elements cl1))
  77. (c2s (cluster-elements cl2)))
  78. (apply min
  79. (apply append
  80. (map (lambda (c1)
  81. (map (lambda (c2)
  82. (element-distance c1 c2))
  83. c2s))
  84. c1s))))))