weighted_select.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package utils
  17. import (
  18. "math/rand"
  19. )
  20. type (
  21. // WeightedRandomSelect is capable of weighted random selection from a set of items
  22. WeightedRandomSelect struct {
  23. root *wrsNode
  24. idx map[WrsItem]int
  25. wfn WeightFn
  26. }
  27. WrsItem interface{}
  28. WeightFn func(interface{}) uint64
  29. )
  30. // NewWeightedRandomSelect returns a new WeightedRandomSelect structure
  31. func NewWeightedRandomSelect(wfn WeightFn) *WeightedRandomSelect {
  32. return &WeightedRandomSelect{root: &wrsNode{maxItems: wrsBranches}, idx: make(map[WrsItem]int), wfn: wfn}
  33. }
  34. // Update updates an item's weight, adds it if it was non-existent or removes it if
  35. // the new weight is zero. Note that explicitly updating decreasing weights is not necessary.
  36. func (w *WeightedRandomSelect) Update(item WrsItem) {
  37. w.setWeight(item, w.wfn(item))
  38. }
  39. // Remove removes an item from the set
  40. func (w *WeightedRandomSelect) Remove(item WrsItem) {
  41. w.setWeight(item, 0)
  42. }
  43. // IsEmpty returns true if the set is empty
  44. func (w *WeightedRandomSelect) IsEmpty() bool {
  45. return w.root.sumWeight == 0
  46. }
  47. // setWeight sets an item's weight to a specific value (removes it if zero)
  48. func (w *WeightedRandomSelect) setWeight(item WrsItem, weight uint64) {
  49. idx, ok := w.idx[item]
  50. if ok {
  51. w.root.setWeight(idx, weight)
  52. if weight == 0 {
  53. delete(w.idx, item)
  54. }
  55. } else {
  56. if weight != 0 {
  57. if w.root.itemCnt == w.root.maxItems {
  58. // add a new level
  59. newRoot := &wrsNode{sumWeight: w.root.sumWeight, itemCnt: w.root.itemCnt, level: w.root.level + 1, maxItems: w.root.maxItems * wrsBranches}
  60. newRoot.items[0] = w.root
  61. newRoot.weights[0] = w.root.sumWeight
  62. w.root = newRoot
  63. }
  64. w.idx[item] = w.root.insert(item, weight)
  65. }
  66. }
  67. }
  68. // Choose randomly selects an item from the set, with a chance proportional to its
  69. // current weight. If the weight of the chosen element has been decreased since the
  70. // last stored value, returns it with a newWeight/oldWeight chance, otherwise just
  71. // updates its weight and selects another one
  72. func (w *WeightedRandomSelect) Choose() WrsItem {
  73. for {
  74. if w.root.sumWeight == 0 {
  75. return nil
  76. }
  77. val := uint64(rand.Int63n(int64(w.root.sumWeight)))
  78. choice, lastWeight := w.root.choose(val)
  79. weight := w.wfn(choice)
  80. if weight != lastWeight {
  81. w.setWeight(choice, weight)
  82. }
  83. if weight >= lastWeight || uint64(rand.Int63n(int64(lastWeight))) < weight {
  84. return choice
  85. }
  86. }
  87. }
  88. const wrsBranches = 8 // max number of branches in the wrsNode tree
  89. // wrsNode is a node of a tree structure that can store WrsItems or further wrsNodes.
  90. type wrsNode struct {
  91. items [wrsBranches]interface{}
  92. weights [wrsBranches]uint64
  93. sumWeight uint64
  94. level, itemCnt, maxItems int
  95. }
  96. // insert recursively inserts a new item to the tree and returns the item index
  97. func (n *wrsNode) insert(item WrsItem, weight uint64) int {
  98. branch := 0
  99. for n.items[branch] != nil && (n.level == 0 || n.items[branch].(*wrsNode).itemCnt == n.items[branch].(*wrsNode).maxItems) {
  100. branch++
  101. if branch == wrsBranches {
  102. panic(nil)
  103. }
  104. }
  105. n.itemCnt++
  106. n.sumWeight += weight
  107. n.weights[branch] += weight
  108. if n.level == 0 {
  109. n.items[branch] = item
  110. return branch
  111. }
  112. var subNode *wrsNode
  113. if n.items[branch] == nil {
  114. subNode = &wrsNode{maxItems: n.maxItems / wrsBranches, level: n.level - 1}
  115. n.items[branch] = subNode
  116. } else {
  117. subNode = n.items[branch].(*wrsNode)
  118. }
  119. subIdx := subNode.insert(item, weight)
  120. return subNode.maxItems*branch + subIdx
  121. }
  122. // setWeight updates the weight of a certain item (which should exist) and returns
  123. // the change of the last weight value stored in the tree
  124. func (n *wrsNode) setWeight(idx int, weight uint64) uint64 {
  125. if n.level == 0 {
  126. oldWeight := n.weights[idx]
  127. n.weights[idx] = weight
  128. diff := weight - oldWeight
  129. n.sumWeight += diff
  130. if weight == 0 {
  131. n.items[idx] = nil
  132. n.itemCnt--
  133. }
  134. return diff
  135. }
  136. branchItems := n.maxItems / wrsBranches
  137. branch := idx / branchItems
  138. diff := n.items[branch].(*wrsNode).setWeight(idx-branch*branchItems, weight)
  139. n.weights[branch] += diff
  140. n.sumWeight += diff
  141. if weight == 0 {
  142. n.itemCnt--
  143. }
  144. return diff
  145. }
  146. // choose recursively selects an item from the tree and returns it along with its weight
  147. func (n *wrsNode) choose(val uint64) (WrsItem, uint64) {
  148. for i, w := range n.weights {
  149. if val < w {
  150. if n.level == 0 {
  151. return n.items[i].(WrsItem), n.weights[i]
  152. }
  153. return n.items[i].(*wrsNode).choose(val)
  154. }
  155. val -= w
  156. }
  157. panic(nil)
  158. }