-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpool.go
153 lines (128 loc) · 4.01 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package genetic
type pool struct {
random randomSource
items []*sequenceInfo
distinctItems map[string]bool
distinctItemFitnesses map[int]bool
addNewItem chan *sequenceInfo
maxPoolSize int
}
func NewPool(maxPoolSize int,
quit chan bool,
childFitnessIsSameOrBetter func(*sequenceInfo, *sequenceInfo) bool,
display chan *sequenceInfo) *pool {
p := pool{
maxPoolSize: maxPoolSize,
random: createRandomNumberGenerator(),
items: make([]*sequenceInfo, 0, maxPoolSize),
distinctItems: make(map[string]bool, maxPoolSize),
distinctItemFitnesses: make(map[int]bool, maxPoolSize),
addNewItem: make(chan *sequenceInfo, maxPoolSize),
}
go func() {
for {
select {
case <-quit:
quit <- true
return
case newItem := <-p.addNewItem:
if p.distinctItems[newItem.genes] {
continue
}
p.distinctItems[newItem.genes] = true
if len(p.items) < 1 {
p.items = append(p.items, newItem)
} else if childFitnessIsSameOrBetter(newItem, p.items[0]) {
if newItem.fitness != p.items[0].fitness {
go func() { display <- newItem }()
}
if len(p.items) < maxPoolSize {
p.items = append(p.items, newItem)
} else {
p.items[0], p.items[len(p.items)-1] = newItem, p.items[0]
}
insertionSort(p.items, childFitnessIsSameOrBetter, len(p.items)-1)
} else if len(p.items) < maxPoolSize {
p.items = append(p.items, newItem)
insertionSort(p.items, childFitnessIsSameOrBetter, len(p.items)-1)
} else if childFitnessIsSameOrBetter(newItem, p.items[len(p.items)-1]) {
p.items[len(p.items)-1] = newItem
insertionSort(p.items, childFitnessIsSameOrBetter, len(p.items)-1)
} else if len(p.distinctItemFitnesses) < 4 {
p.items[len(p.items)-1] = newItem
insertionSort(p.items, childFitnessIsSameOrBetter, len(p.items)-1)
} else {
continue
}
p.distinctItemFitnesses[newItem.fitness] = true
}
}
}()
return &p
}
func (p *pool) addAll(items []*sequenceInfo) {
for _, item := range items {
p.addNewItem <- item
}
}
func (p *pool) addItem(item *sequenceInfo) {
go func() { p.addNewItem <- item }()
}
func (p *pool) any() bool {
return len(p.items) > 0
}
func (p *pool) cap() int {
return p.maxPoolSize
}
func (p *pool) contains(item *sequenceInfo) bool {
return p.distinctItems[item.genes]
}
func (p *pool) getBest() *sequenceInfo {
return p.items[0]
}
func (p *pool) getRandomItem() (ret *sequenceInfo) {
index := p.random.Intn(len(p.items))
defer func() {
if r := recover(); r != nil {
ret = p.items[0]
}
}()
ret = p.items[index]
return
}
func (p *pool) getWorst() *sequenceInfo {
return p.items[len(p.items)-1]
}
func (p *pool) len() int {
return len(p.items)
}
func (p *pool) populatePool(nextChromosome chan string, geneSet string, numberOfChromosomes, numberOfGenesPerChromosome int, compareFitnesses func(*sequenceInfo, *sequenceInfo) bool, getFitness func(string) int, initialParent *sequenceInfo) {
itemGenes := generateParent(nextChromosome, geneSet, numberOfChromosomes, numberOfGenesPerChromosome)
initialStrategy := strategyInfo{name: "initial "}
p.addItem(initialParent)
max := p.cap()
for i := 0; i < 2*max; i++ {
itemGenes = generateParent(nextChromosome, geneSet, numberOfChromosomes, numberOfGenesPerChromosome)
sequence := sequenceInfo{genes: itemGenes, fitness: getFitness(itemGenes), strategy: initialStrategy}
sequence.parent = &sequence
p.addNewItem <- &sequence
}
}
func (p *pool) reset(item *sequenceInfo) {
p.items = p.items[:1]
p.resetDistinct()
p.addNewItem <- item
}
func (p *pool) resetDistinct() {
p.distinctItems = make(map[string]bool, p.maxPoolSize)
p.distinctItemFitnesses = make(map[int]bool, p.maxPoolSize)
for i := 0; i < len(p.items); i++ {
p.distinctItems[p.items[i].genes] = true
p.distinctItemFitnesses[p.items[i].fitness] = true
}
}
func (p *pool) truncateAndAddAll(items []*sequenceInfo) {
p.items = p.items[:min(20, len(p.items))]
p.resetDistinct()
p.addAll(items)
}