testca.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/wangjia184/sortedset"
  5. "git.wecise.com/wecise/common/alg/ca"
  6. "math"
  7. )
  8. type SimpleClusterable struct {
  9. position float64
  10. }
  11. func (s SimpleClusterable) Distance(c interface{}) float64 {
  12. distance := math.Abs(c.(SimpleClusterable).position - s.position)
  13. return distance
  14. }
  15. func (s SimpleClusterable) String() string {
  16. return fmt.Sprint( s.position )
  17. }
  18. func main() {
  19. clusterList := sortedset.New()
  20. clusterList.AddOrUpdate("1", 10, SimpleClusterable{10} )
  21. clusterList.AddOrUpdate("2", 5, SimpleClusterable{5} )
  22. clusterList.AddOrUpdate("3", 42, SimpleClusterable{42} )
  23. clusterList.AddOrUpdate("4", 0, SimpleClusterable{0} )
  24. clusterList.AddOrUpdate("5", 50, SimpleClusterable{50} )
  25. clusterList.AddOrUpdate("6", 40, SimpleClusterable{40} )
  26. clusterList.AddOrUpdate("7", 41, SimpleClusterable{41} )
  27. clusterList.AddOrUpdate("8", 102, SimpleClusterable{102} )
  28. clusterList.AddOrUpdate("9", 43, SimpleClusterable{43} )
  29. clusterList.AddOrUpdate("10", 200, SimpleClusterable{200} )
  30. clusterList.AddOrUpdate("11", 40, SimpleClusterable{40} )
  31. clusterList.AddOrUpdate("12", 40, SimpleClusterable{40} )
  32. clusterList.AddOrUpdate("13", 101, SimpleClusterable{101} )
  33. clusterList.AddOrUpdate("14", 45, SimpleClusterable{45} )
  34. clusterList.AddOrUpdate("15", 103, SimpleClusterable{103} )
  35. clusterList.AddOrUpdate("16", 200, SimpleClusterable{200} )
  36. clusterList.AddOrUpdate("17", 400, SimpleClusterable{400} )
  37. clusterList.AddOrUpdate("18", 200, SimpleClusterable{200} )
  38. clusterList.AddOrUpdate("19", 53, SimpleClusterable{53} )
  39. clusterList.AddOrUpdate("20", 63, SimpleClusterable{63} )
  40. alg := ca.New(2, 10, true)
  41. clusters := alg.Clusterize(clusterList)
  42. fmt.Println("total cluster len==>", len(clusters))
  43. for _, cluster := range clusters {
  44. nodes := cluster.GetByRankRange(-1, 1, false)
  45. fmt.Printf(" cluster len %d : ==>\n", cluster.GetCount())
  46. for i := 0; i < len(nodes); i++ {
  47. fmt.Printf(" %s - %s\n", nodes[i].Value, nodes[i].Key)
  48. //fmt.Print(reflect.TypeOf(nodes[i].Value))
  49. }
  50. }
  51. }