-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
385 lines (317 loc) · 7.55 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import "fmt"
import "math/rand"
import "time"
import "sort"
//import "reflect"
//import "strings"
//import "bytes"
const CROSSOVERRATE = 0.7
const MUTATIONRATE = 0.005
const POPULATION_SIZE = 200
const MAXGENERATIONS = 1000
const CHROM_LENGTH = 300
const NUMOPERATORS = 3
//const NUMBITS = 4*(NUMOPERATORS*2+1) //28 in the case of 3 operators
const GENE_LENGTH = 4
const RETURNONDIVZERO = 1000000 //TODO find some better solution
func Deb(v ...interface{}){
enableDebug := false
if enableDebug{
fmt.Println(v...)
}
}
func Log(v ...interface{}){
enableLog := true
if enableLog{
fmt.Println(v...)
}
}
func logBestFitness(input chan float64 ){
var best float64
var temp float64
for{
temp = <-input
if temp > best{
best = temp
Log("best fitness so far is: ",best)
}
}
}
const (
broken = iota
number
operator
)
const (
add = iota + 10
sub
mult
div
none
)
func argumentType(arg string)(ret int){
switch arg {
case "0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001": return number
case "1010","1011","1100","1101":return operator
default: return broken
}
return broken
}
func doMath(operator int, tempinput int, storage float64)(res float64){
input := float64(tempinput)
switch operator{
case add:return storage+input
case sub:return storage-input
case mult:return storage*input
case div:
if input == 0{
return RETURNONDIVZERO
}
return storage/input
default: return 99999.9 //Break
}
return 0.1 //TODO handle error????
}
func parseNumeric(arg string)(ret int){
switch arg {
case "0000":return 0
case "0001":return 1
case "0010":return 2
case "0011":return 3
case "0100":return 4
case "0101":return 5
case "0110":return 6
case "0111":return 7
case "1000":return 8
case "1001":return 9
}
return -1 //TODO handle error
}
func parseOperator(arg string)(ret int){
switch arg{
case "1010":return add
case "1011":return sub
case "1100":return mult
case "1101":return div
}
return none
}
func humanReadOperator(arg int)(ret string){
switch arg{
case add:return "+"
case sub:return "-"
case mult:return "*"
case div:return "/"
}
return "broken"
}
func generateOneChrom()(string){
var temp string
for i:=0;i<CHROM_LENGTH;i++{
tilf := rand.Float64()
if tilf < 0.5{
temp+="0"
}else{
temp+="1"
}
}
return temp
}
func generateNChroms(n int)([]string){
ret := make([]string,n)
//time.Now().Unix() //for new seed every program
rand.Seed(time.Now().Unix())
for i:=0;i<n;i++{
ret[i] = generateOneChrom()
}
return ret
}
//Parse the string 4 by 4 bit and calculate the expression
func evalExpression(chromStr string)(ret float64){
var currentval float64 = 0.0
currentOperator := add
next := number
tempOperator := none
tempNumeric := 0
for (len(chromStr) > 0) {
thisString := chromStr[0:GENE_LENGTH]
if next == number{
if argumentType(thisString) == number{
//calculate new currentval based on "prev operatoe" and "currentval
tempNumeric = parseNumeric(thisString)
currentval = doMath(currentOperator,tempNumeric,currentval)
Deb(tempNumeric)
next = operator
}
}else if next == operator{
//if arg type is operaotr, store it for use in possibly next numerical
if argumentType(thisString) == operator{
tempOperator = parseOperator(thisString)
currentOperator = tempOperator
Deb(humanReadOperator(tempOperator))
next = number
}
}
chromStr = chromStr[GENE_LENGTH:] // 4 ??
}
Deb("=")
return currentval
}
//The function to optimize
func calcFitness(inputVal, goal float64)(fitness float64,correct bool){
correct = false
if inputVal == goal{
correct = true
return
}
fitness = 1/abs((goal-inputVal))
return
}
func abs(in float64)(ret float64){
if in < 0{
return -in
}
return in
}
//TODO finalize
func mateOneGeneration(popIn []string,goal float64, logBestFitness chan float64)(popOut []string,done bool){
popOut = make([]string,len(popIn))
fitness := make([]float64,len(popIn))
var best float64
for i,chromIn := range popIn{
fitness[i],done = calcFitness(evalExpression(chromIn),goal)
if fitness[i] > best{
best = fitness[i]
}
if done{
return
}
}
logBestFitness<-best
//Log("best fitness is ",best)
// Log(prepareRoulette(fitness))
for i:=0;i<len(popIn);i+=2{
firstMateIndex := pickWinner(prepareRoulette(fitness))
secondMateIndex := pickWinner(prepareRoulette(fitness))
for secondMateIndex == firstMateIndex{
secondMateIndex = pickWinner(prepareRoulette(fitness))
}
popOut[i],popOut[i+1] = crossOver(popIn[firstMateIndex],popIn[secondMateIndex])
}
return
}
func crossOver(chromOne string, chromTwo string)(string, string){
//next line is test
//chromTest := string(chromOne[0])
//Log(reflect.TypeOf(chromTest))
//Log(chromTest)
crossOverCheck := rand.Float64()
if crossOverCheck < CROSSOVERRATE{
chosenGene := int((rand.Float64())*float64(len(chromOne)))
temp := chromOne
chromOne = chromOne[0:chosenGene] + chromTwo[chosenGene:]
chromTwo = chromTwo[0:chosenGene] + temp[chosenGene:]
}
chromOne = mutateString(chromOne)
chromTwo = mutateString(chromTwo)
return chromOne,chromTwo
}
//TODO verify this func
func mutateString(chrom string)(ret string){
ret = chrom
for i,_ := range chrom{
mutateCheck := rand.Float64()
if mutateCheck < MUTATIONRATE{
charTest := string(chrom[i])
if charTest == "0"{
ret = ret[0:i] + "1" + ret[i+1:]
}else{
ret = ret[0:i] + "0" + ret[i+1:]
}
}
}
return ret
}
//Picks a random chromosome based on fitness
func prepareRoulette(fitnessTable []float64)([]float64){
ret := make([]float64,len(fitnessTable))
ret[0] = fitnessTable[0]
for i,_ := range fitnessTable{
if i != 0{
ret[i] = fitnessTable[i]+ret[i-1]
}
}
//Log(winner)
//Log(winnerIndex)
return ret
}
func pickWinner(rouletteWheel []float64)(ret int){
largest := rouletteWheel[len(rouletteWheel)-1]
winner := rand.Float64()*largest
winnerIndex := sort.SearchFloat64s(rouletteWheel,winner)
return winnerIndex
}
//An attempt to run a genetic algorithm...
func main(){
chanLogger := make(chan float64)
go logBestFitness(chanLogger)
var target float64 = 1000
fmt.Println("Hello genetic algorithms!")
population := generateNChroms(POPULATION_SIZE)
goalReached := false
for i:=0;i<MAXGENERATIONS;i++{
population,goalReached = mateOneGeneration(population,target,chanLogger)
if goalReached{
Log("Evolution perfected after ",i," generations")
break
}
}
if !goalReached{
Log("The species stopped evolving after ",MAXGENERATIONS)
}
//Log(mutateString(lol[0]))
//lol[0],lol[1] = crossOver(lol[0],lol[1])
/*
for i:=0;i<POPULATION_SIZE;i++{
curr = evalExpression(lol[i])
currFit,goalReached = calcFitness(curr,target)
if !goalReached {
if currFit > bestFit{
bestFit = currFit
}
}else{
bestFit = currFit
Log("Evolution perfected!",target)
break;
}
}
if !goalReached{
Log("Population failed")
Log("Best loser is:")
Log(bestFit)
}
*/
//lol,goalReached = mateOneGeneration(lol,target)
//population := generateNChroms(POPULATION)
//fmt.Print(population)
//k := 8
//var test string
// test = "1010101010101010101010101010\n"
// test = test[0:4]+"0000"+test[8:28]
// fmt.Print(test[:])
//test := generateNChroms(20)
//fmt.Print(test)
}
/* TEST BLOCK
fmt.Println(argumentType("0010")) // 1
fmt.Println(argumentType("1011")) // 2
fmt.Println(argumentType("hei")) // 0
fmt.Println(parseNumeric("0011")) // 3
fmt.Println(parseNumeric("1111")) // broken -1
fmt.Println(parseOperator("1010")) // add 0
fmt.Println(parseOperator("lol")) // none 4
fmt.Println(calcFitness("lol")) // 1.0
*/