-
Notifications
You must be signed in to change notification settings - Fork 1
/
evolution.py
1356 lines (1276 loc) · 47 KB
/
evolution.py
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
PLANS - Planning Nature-based Solutions
Module description:
This module stores functions of genetic algortihmns.
Copyright (C) 2022 Iporã Brito Possantti
************ GNU GENERAL PUBLIC LICENSE ************
https://www.gnu.org/licenses/gpl-3.0.en.html
Permissions:
- Commercial use
- Distribution
- Modification
- Patent use
- Private use
Conditions:
- Disclose source
- License and copyright notice
- Same license
- State changes
Limitations:
- Liability
- Warranty
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import numpy as np
import pandas as pd
# utilitay routines for benchmarking
def get_moea_trivial_solution(show=True):
import matplotlib.pyplot as plt
# + sign
o1_tpl = ((2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(1, 1, 1, 1, 1, 1, 1, 1, 1),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2))
o1 = np.array(o1_tpl)
# x sign
o2_tpl = ((1, 2, 2, 2, 2, 2, 2, 2, 1),
(2, 1, 2, 2, 2, 2, 2, 1, 2),
(2, 2, 1, 2, 2, 2, 1, 2, 2),
(2, 2, 2, 1, 2, 1, 2, 2, 2),
(2, 2, 2, 2, 1, 2, 2, 2, 2),
(2, 2, 2, 1, 2, 1, 2, 2, 2),
(2, 2, 1, 2, 2, 2, 1, 2, 2),
(2, 1, 2, 2, 2, 2, 2, 1, 2),
(1, 2, 2, 2, 2, 2, 2, 2, 1))
# o sign
o2 = np.array(o2_tpl)
o3_tpl = ((2, 2, 2, 2, 2, 2, 2, 2, 2),
(2, 2, 2, 1, 1, 1, 2, 2, 2),
(2, 2, 1, 2, 2, 2, 1, 2, 2),
(2, 1, 2, 2, 2, 2, 2, 1, 2),
(2, 1, 2, 2, 2, 2, 2, 1, 2),
(2, 1, 2, 2, 2, 2, 2, 1, 2),
(2, 2, 1, 2, 2, 2, 1, 2, 2),
(2, 2, 2, 1, 1, 1, 2, 2, 2),
(2, 2, 2, 2, 2, 2, 2, 2, 2))
o3 = np.array(o3_tpl)
# mask
m0_tpl = ((1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 0, 1, 1),
(1, 1, 1, 1, 1, 1, 1, 1, 1))
m0 = np.array(m0_tpl)
if show:
plt.imshow(o1)
plt.show()
plt.imshow(o2)
plt.show()
plt.imshow(o3)
plt.show()
plt.imshow(m0)
plt.show()
return o1, o2, o3, m0
def get_trivial_solution(show=False):
"""
Get trivial image solution
:param show: boolean to control image display
:return: full solution 2d array and mask 2d array
"""
import matplotlib.pyplot as plt
answer = ((1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
(1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
(1, 1, 1, 1, 3, 3, 3, 2, 2, 2),
(1, 1, 1, 1, 3, 3, 3, 2, 2, 2),
(1, 1, 1, 1, 4, 3, 3, 2, 2, 2),
(1, 1, 1, 4, 4, 4, 3, 4, 2, 2),
(1, 1, 4, 4, 5, 4, 4, 4, 4, 4),
(4, 4, 4, 5, 5, 5, 4, 4, 4, 4),
(4, 4, 5, 5, 5, 5, 5, 4, 4, 4),
(4, 4, 4, 4, 4, 4, 4, 4, 4, 4))
solution_full = np.array(answer)
if show:
plt.imshow(solution_full, cmap='viridis')
plt.show()
mask = ((1, 1, 0, 0, 0, 0, 0, 0, 0, 0),
(1, 1, 0, 0, 0, 1, 1, 1, 1, 0),
(0, 0, 0, 0, 3, 3, 2, 2, 2, 0),
(1, 0, 0, 1, 3, 3, 3, 2, 2, 0),
(0, 0, 1, 1, 4, 3, 3, 2, 2, 0),
(0, 1, 1, 4, 4, 4, 3, 2, 2, 0),
(0, 1, 4, 4, 4, 4, 4, 2, 2, 0),
(0, 4, 4, 5, 5, 5, 4, 4, 4, 0),
(0, 4, 4, 5, 5, 5, 4, 4, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
mask_array = (np.array(mask) > 0) * 1
if show:
plt.imshow(solution_full * mask_array, cmap='viridis')
plt.show()
return solution_full, mask_array
def get_large_solution(seed=666, size=30, show=False):
"""
Get large image solutions
:param seed: int seed for random generator
:param size: int image size (square of size x size)
:param show: boolean control
:return: solution 2d array and mask 2d array
"""
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt
np.random.seed(seed)
m = np.random.random(size=(size, size))
m_smoth = gaussian_filter(m, sigma=2)
p = np.percentile(m_smoth, (80, 40, 60, 90))
mask = (m_smoth < p[0]) * 1
#plt.imshow(mask, cmap='Greys_r')
#plt.show()
m = np.random.random(size=(size, size))
#plt.imshow(m, cmap='viridis')
#plt.show()
m_smoth = gaussian_filter(m, sigma=3)
#plt.imshow(m_smoth, cmap='viridis')
#plt.show()
p = np.percentile(m_smoth, (10, 40, 60, 90))
#print(p)
sol = ((m_smoth < p[0]) * 1) + \
((m_smoth >= p[0]) * (m_smoth < p[1]) * 2) + \
((m_smoth >= p[1]) * (m_smoth < p[2]) * 3) + \
((m_smoth >= p[2]) * (m_smoth < p[3]) * 4) + \
((m_smoth >= p[3]) * 5)
if show:
plt.imshow(sol, cmap='viridis')
plt.show()
plt.imshow(sol * mask, cmap='viridis')
plt.show()
return sol, mask
def get_cost_surface():
from scipy.ndimage import gaussian_filter
size = 100
#np.random.seed(766)
np.random.seed(466)
im_rnd = np.random.random(size=(size, size))
im_field = gaussian_filter(im_rnd, sigma=5)
return im_field
# utilitary routines for plotting:
def plot_trace_generations(evolution, mask, sol, folder='.', step=1):
import matplotlib.pyplot as plt
gens_lst = list()
scores_gens_lst = list()
for i in range(0, len(evolution), step):
exp_lst = list()
# express genes
for j in range(36):
# print(np.shape(sol_array)[0])
lcl_exp = express_2darray_mask(evolution[i]['Parents'][j][0], mask)
# print(lcl_exp)
exp_lst.append(lcl_exp[:])
plot_generation(folder=folder, sol=sol * mask, gen=exp_lst, ids=evolution[i]['Ids'][:36],
scores=evolution[i]['Scores'][:36], nm=str(i + 1))
print('plot ' + str(i + 1))
scores_gens_lst.append(evolution[i]['Scores'][0])
# print(evolution[i]['Scores'][0])
gens_lst.append(i + 1)
plt.plot(gens_lst, scores_gens_lst, 'k-')
plt.ylabel('Score')
plt.xlabel('Generations')
plt.savefig(folder + '/convegence.png')
plt.close()
def plot_trace_gen_moea(evolution, mask, sols, folder='.', step=1, cmap='Greys_r'):
import matplotlib.pyplot as plt
gens_lst = list()
scores_gens_lst = list()
for i in range(0, len(evolution), step):
exp_lst = list()
# express genes
for j in range(36):
# print(np.shape(sol_array)[0])
lcl_exp = express_2darray_mask(evolution[i]['Parents'][j][0], mask)
# print(lcl_exp)
exp_lst.append(lcl_exp[:])
plot_generation_moea(folder=folder, sols=(sols[0] * mask, sols[1] * mask, sols[2] * mask),
gen=exp_lst, ids=evolution[i]['Ids'][:36], scores=evolution[i]['Scores'][:36],
nm=str(i + 1), cmap=cmap)
print('plot ' + str(i + 1))
scores_gens_lst.append(evolution[i]['Scores'][0])
# print(evolution[i]['Scores'][0])
gens_lst.append(i + 1)
plt.plot(gens_lst, scores_gens_lst, 'k-')
plt.ylabel('Score')
plt.xlabel('Generations')
plt.savefig(folder + '/convegence.png')
plt.close()
def plot_convergence(folder, gens, scores, colors, labels, nm='file'):
import matplotlib.pyplot as plt
for i in range(len(gens)):
plt.plot(gens[i], scores[i], c=colors[i], label=labels[i])
plt.legend()
plt.ylabel('Score')
plt.xlabel('Generations')
plt.savefig(folder + '/convergence_' + nm + '.png')
plt.close()
def plot_generation_moea(folder, sols, gen, ids, scores, nm, cmap='viridis'):
import matplotlib.pyplot as plt
import matplotlib as mpl
fig = plt.figure(figsize=(8, 9))
fig.suptitle('Generation {}'.format(nm), fontsize=12)
gs = mpl.gridspec.GridSpec(7, 6, wspace=0.2, hspace=0.4, top=0.90, bottom=0.1, left=0.1, right=0.95)
#
#
ind = 0
for i in range(7):
for j in range(6):
if i == 0 and j == 0:
plt.subplot(gs[i, j])
plt.title('Solution 1', fontsize=8, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(sols[0], cmap=cmap, vmin=np.min(sols), vmax=np.max(sols))
elif i == 0 and j == 1:
plt.subplot(gs[i, j])
plt.title('Solution 2', fontsize=8, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(sols[1], cmap=cmap, vmin=np.min(sols), vmax=np.max(sols))
elif i == 0 and j == 2:
plt.subplot(gs[i, j])
plt.title('Solution 3', fontsize=8, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(sols[2], cmap=cmap, vmin=np.min(sols), vmax=np.max(sols))
elif i == 0:
pass
else:
plt.subplot(gs[i, j])
plt.title(ids[ind] + ' S:' + str(round(scores[ind], 1)), fontsize=6, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(gen[ind], cmap=cmap, vmin=np.min(sols), vmax=np.max(sols))
ind = ind + 1
plt.savefig(folder + '/gen-' + nm + '.png', dpi=300)
plt.close(fig=fig)
def plot_generation(folder, sol, gen, ids, scores, nm, cmap='viridis'):
import matplotlib.pyplot as plt
import matplotlib as mpl
fig = plt.figure(figsize=(8, 9))
fig.suptitle('Generation {}'.format(nm), fontsize=12)
gs = mpl.gridspec.GridSpec(7, 6, wspace=0.2, hspace=0.4, top=0.95, bottom=0.1, left=0.1, right=0.95)
#
#
ind = 0
for i in range(7):
for j in range(6):
if i == 0 and j == 0:
plt.subplot(gs[i, j])
plt.title('Solution', fontsize=8, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(sol, cmap=cmap)
elif i == 0:
pass
else:
plt.subplot(gs[i, j])
plt.title(ids[ind] + ' S:' + str(round(scores[ind], 1)), fontsize=6, loc='left')
plt.xticks([])
plt.yticks([])
plt.imshow(gen[ind], cmap=cmap, vmin=np.min(sol), vmax=np.max(sol))
ind = ind + 1
plt.savefig(folder + '/gen-' + nm + '.png', dpi=300)
plt.close(fig=fig)
# general functions:
def express_string(gene, concat=''):
"""
Express a string-based gene
:param gene: gene iterable
:param concat: string concatenator - default is ''
:return: gene expression (string)
"""
aux_lst = list()
for i in range(len(gene)):
aux_lst.append(str(gene[i]))
expression = concat.join(aux_lst)
return expression
def express_intvalue(gene):
"""
Express a integer-based gene
:param gene: gene iterable
:return: integer
"""
expression = int(gene[0])
return expression
def express_floatvalue(gene):
"""
Express a float-based gene
:param gene: gene iterable
:return: float
"""
expression = float(gene[0])
return expression
def express_2darray(gene, rowlen=3):
"""
Express a 2d array based gene
:param gene: gene iterable
:param rowlen: int of row length
:return: 2d numpy array
"""
matrix = list()
for i in range(0, len(gene), rowlen):
lcl_row = gene[i: i + rowlen]
matrix.append(lcl_row[:])
return np.array(tuple(matrix))
def express_2darray_mask(gene, mask):
"""
Express a 2d array based gene considering a boolean mask array
:param gene: gene iterable
:param mask: pseudo-boolean (1 and 0) 2d array
:return: 2d numpy array
"""
matrix = list()
mask_1d = np.reshape(mask, np.size(mask))
gene_id = 0
for i in range(len(mask_1d)):
if mask_1d[i] == 1:
matrix.append(gene[gene_id])
gene_id = gene_id + 1
else:
matrix.append(0)
matrix_2d = np.array(matrix).reshape(np.shape(mask))
return matrix_2d
def express_1darray(gene):
"""
Express a 1d array-based gene
:param gene: gene iterable
:return: 1d numpy array
"""
expression = np.array(gene)
return expression
def express_path(gene, p0=0, pmin=0, pmax=100):
_a = express_1darray(gene=gene)
_path = np.zeros(shape=np.shape(_a))
_path[0] = p0
for i in range(1, len(_path)):
_path[i] = _path[i - 1] + _a[i - 1]
if _path[i] >= pmax:
_path[i] = pmax - 1
if _path[i] <= pmin:
_path[i] = pmin
return _path
def encode_string(string='text', concat=''):
"""
Encode a string-based phenotype
:param string: string phenotype
:param concat: string concatenator
:return: gene 1d tuple
"""
gene = string.split(concat)
return tuple(gene)
def encode_1darray(array):
"""
Encode a 1d array-based phenotype
:param array: 1d array phenotype
:return: gene 1d tuple
"""
return tuple(array)
def encode_2darray(array):
"""
Encode a 2d array-based phenotype
:param array: 2d array phenotype
:return: gene 1d tuple
"""
aux_lst = list()
for i in range(len(array)):
for j in range(len(array[i])):
aux_lst.append(array[i][j])
return tuple(aux_lst)
def encode_2darray_mask(array, mask):
"""
Encode a 2d array-based phenotype considering a boolean mask array
:param array: 2d array phenotype
:param mask: 2d array phenotype boolean mask
:return: gene 1d tuple
"""
aux_lst = list()
for i in range(len(array)):
for j in range(len(array[i])):
if mask[i][j] > 0:
aux_lst.append(array[i][j])
return tuple(aux_lst)
def generate_population(nucleotides, genesizes, popsize=100):
"""
genesis of a random population
:param nucleotides: tuple of nucleotides genes (tuple of tuples)
:param genesizes: tuple of gene sizes (tuple of ints)
:param popsize: population size (int)
:return: tuple of random new dnas
"""
pop_lst = list()
for i in range(popsize):
lcl_solution = generate_dna(nucleotides=nucleotides, genesizes=genesizes)
pop_lst.append(lcl_solution[:])
return tuple(pop_lst)
def generate_dna(nucleotides, genesizes):
"""
genesis of a random dna
:param nucleotides: tuple of nucleotides genes (tuple of tuples - 2d tuple)
:param genesizes: tuple of gene sizes (tuple of ints)
:return: tuple of genes (dna)
"""
#
def generate_gene(nucleo_set, size=3):
def_gene = list()
def_indexes = np.random.randint(0, high=len(nucleo_set), size=size)
for i in range(len(def_indexes)):
def_gene.append(nucleo_set[def_indexes[i]])
return tuple(def_gene)
#
def_dna = list()
for i in range(len(nucleotides)):
lcl_gene = generate_gene(nucleo_set=nucleotides[i], size=genesizes[i])
def_dna.append(lcl_gene)
return tuple(def_dna)
def reproduction(parenta, parentb, nucleotides, mutrate=0.05, puremutrate=0.10, cutfrac=0.2):
"""
DNA reproduction with crossover and mutation
:param parenta: Parent A DNA
:param parentb: Parent B DNA
:param nucleotides: tuple of nucleotides genes (tuple of tuples)
:param mutrate: float fraction of mutation rate
:return: Offspring A DNA and Offspring B DNA (two returns)
"""
offsp_a = list()
offsp_b = list()
# loop in dna genes
for i in range(len(parenta)):
# retrieve parent genes
parent_gene_a = parenta[i]
parent_gene_b = parentb[i]
# crossover
offsp_gene_a, offsp_gene_b = crossover(parent_gene_a, parent_gene_b, cutfrac=cutfrac)
# mutation
offsp_gene_a = mutation(offsp_gene_a, nucleotides[i], mutrate=mutrate, puremutrate=puremutrate)
offsp_gene_b = mutation(offsp_gene_b, nucleotides[i], mutrate=mutrate, puremutrate=puremutrate)
# appending
offsp_a.append(offsp_gene_a[:])
offsp_b.append(offsp_gene_b[:])
# return offspring dna
return tuple(offsp_a), tuple(offsp_b)
def crossover(genea, geneb, cutfrac=0.2):
"""
Crossover of Parent Gene A and Parent Gene B
:param genea: Gene A tuple
:param geneb: Gene B tuple
:param cutfrac: float fraction of cut in gene
:return: Offspring A and Offspring B genes
"""
#
cutsize = 1
if len(genea) * cutfrac < 1:
cutsize = 1
else:
cutsize = int(len(genea) * cutfrac)
# random cutpoint inside gene
cutpoint = np.random.randint(0, len(genea) - cutsize)
# extract cuts
cut_a = genea[cutpoint : cutpoint + cutsize]
cut_b = geneb[cutpoint : cutpoint + cutsize]
# convert to list
offs_a = list(genea[:])
offs_b = list(geneb[:])
# cross over:
offs_a[cutpoint : cutpoint + cutsize] = cut_b[:] # cut B goes in gene A
offs_b[cutpoint : cutpoint + cutsize] = cut_a[:] # cut A goes in gene B
# return tuples
return tuple(offs_a), tuple(offs_b)
def mutation(gene, gene_nucleo, mutrate=0.05, puremutrate=0.1):
"""
Mutation of a single gene (change of a single nucletide)
:param gene: tuple of gene
:param gene_nucleo: tuple of gene nucleotides
:param mutrate: float fraction of mutation rate
:param puremutrate: float less than 1 - fraction of pure mutation rate given a mutation event
:return: mutated gene tuple
"""
# generate a mutation event
mutevent = np.random.random(1)
# mutation rate filter:
if mutevent <= mutrate:
#print('Mutation! Type: ', end='\t')
mutsize = 1 # np.random.randint(1, len(gene)) # get number of mutations
gene_mut = list(gene) # convert gene tuple to a list
for i in range(mutsize):
# get mutation position in gene
gene_nucleoid = np.random.randint(0, len(gene))
muttype = np.random.random()
# pure random mutation:
if muttype < puremutrate:
#print('Pure')
# get the mutated nucleotide position in the list of nucleotides
mut_nucleoid = np.random.randint(0, len(gene_nucleo))
# neighborhood biased mutation:
else:
#print('Biased')
# get neighborhood nucleotides
if gene_nucleoid == 0:
neighbor_nucleos = (gene_mut[gene_nucleoid + 1], gene_mut[gene_nucleoid + 2])
elif gene_nucleoid == len(gene) - 1:
neighbor_nucleos = (gene_mut[gene_nucleoid - 1], gene_mut[gene_nucleoid - 2])
else:
neighbor_nucleos = (gene_mut[gene_nucleoid - 1], gene_mut[gene_nucleoid + 1])
# get the mutated nucleotide position in the list of biesed nucleotides
mut_nucleoid = np.random.randint(0, len(neighbor_nucleos))
#
# replace nucleotide in gene
gene_mut[gene_nucleoid] = gene_nucleo[mut_nucleoid]
return tuple(gene_mut)
else:
return gene
def generate_offspring(pop, nucleotides, offsize=100, mutrate=0.10, puremutrate=0.10, cutfrac=0.2):
"""
Genesis of new offspring DNA
:param pop: tuple of population DNA
:param nucleotides: tuple of genes nucleotides (2d tuple)
:param offsfrac: positive float - fraction of offspring related to population size
:param mutrate: float fraction of mutation rate
:return: tuple of Offspring DNA
"""
offsp_lst = list()
#print('>>> {}'.format(offsize))
count = 0
while True:
# get random order of mating pool
parents_ids = np.arange(len(pop))
np.random.shuffle(parents_ids)
parent_a_id = parents_ids[0]
parent_b_id = parents_ids[1]
# loop in mating pool
parent_a = pop[parent_a_id]
parent_b = pop[parent_b_id]
offsp_a, offsp_b = reproduction(parent_a, parent_b, nucleotides=nucleotides, mutrate=mutrate,
puremutrate=puremutrate, cutfrac=cutfrac)
offsp_lst.append(offsp_a)
count = count + 1
if count == offsize:
break
offsp_lst.append(offsp_b)
count = count + 1
if count == offsize:
break
#print(count)
return tuple(offsp_lst)
def recruitment(pop, offsp):
"""
Recruitment of Parents and Offspring
:param pop: tuple of parents DNA
:param offsp: tuple of offspring DNA
:return: tuple of recruited population DNA
"""
#
aux_lst = list(pop) + list(offsp)
return tuple(aux_lst)
def fitness_similarity(dna, solution):
"""
Benchmark global fitness score of a single DNA based on solution similarity
:param dna: DNA tuple
:param solution: Best DNA possible (this may be not be available!!)
:return: float global fitness score (the higher the better, 100 is perfect)
"""
fit_lst = list()
len_lst = list()
#
# loop in genes
for i in range(len(dna)):
lcl_gene = np.array(dna[i])
lcl_gene_solution = np.array(solution[i])
lcl_b = (lcl_gene == lcl_gene_solution) * 1 # pseudo boolean similarity array
lcl_len = len(lcl_gene_solution)
lcl_fit = 100 * np.sum(lcl_b)/lcl_len
#print('{}\t{}\t{}\t{}%'.format(lcl_gene, lcl_gene_solution, lcl_b, lcl_fit))
fit_lst.append(lcl_fit)
len_lst.append(lcl_len)
# global fit is the average of local fitness
gbl_fit = np.sum(np.array(fit_lst) * np.array(len_lst) / np.sum(len_lst))
return gbl_fit
def fitness_rmse(dna, solution):
"""
Benchmark global fitness score of a single DNA based on solution Root Mean Squared Error
:param dna: DNA tuple
:param solution: tuple of Solution - Best DNA possible (this may be not be available!!)
:return: float global fitness score (the higher the better, 0 is perfect)
"""
from analyst import rmse
fit_lst = list()
# loop in genes:
for i in range(len(dna)):
lcl_gene = np.array(dna[i])
lcl_gene_solution = np.array(solution[i])
lcl_fit = rmse(obs=lcl_gene_solution, sim=lcl_gene) * -1 # negative values of rmse
fit_lst.append(lcl_fit)
# global fit is the average of local fitness
return np.mean(fit_lst)
def fitness_moea(dna, solutions):
"""
Global fitness score of a single DNA MOEA (convergence to the
:param dna: DNA tuple
:param solutions: Best DNA possible solutions (this may be not be available!!)
:return: float global fitness score
"""
fit_lst = list()
#
# loop in genes
for i in range(len(dna)):
lcl_gene = np.array(dna[i])
gene_fit_lst = list()
len_lst = list()
# loop in gene multiples solutions:
lcl_gene_solutions = np.array(solutions[i])
for j in range(len(lcl_gene_solutions)):
# assess multiple solutions:
lcl_solution = lcl_gene_solutions[j]
lcl_b = (lcl_gene == lcl_solution) * 1 # boolean of matching nucleotides
lcl_len = len(lcl_solution) # number of nucleotides
lcl_gene_fit = 100 * np.sum(lcl_b)/lcl_len # give the % of matching nucleotides
gene_fit_lst.append(lcl_gene_fit)
len_lst.append(lcl_len)
# the weighted avg of gene solutions
gene_gbl_fit = np.sum(np.array(gene_fit_lst) * np.array(len_lst) / np.sum(len_lst))
fit_lst.append(gene_gbl_fit)
# global fit is the average of gene global fitness
gbl_fit = np.sum(np.array(fit_lst))/len(fit_lst)
return gbl_fit
def _evolve(pop0, nucleotides, solution, seed, generations=10, offsfrac=1, mutrate=0.20, puremutrate=0.10,
cutfrac=0.2, tracefrac=0.3, tracepop=False, fittype='similarity', tui=False):
"""
Benchmark Evolution of DNAs based on the NSGA-II approach but single-objective
:param pop0: initial population DNAs
:param nucleotides: tuple of genes nucleotides (tuple of tuples)
:param solution: tuple of gene solutions (tuple of objects) --- needed for fitness function
:param seed: int number for random state
:param mutrate: float - mutation rate (less than 1)
:param generations: int - number of generations
:param puremutrate: float - fraction of pure mutations (less than 1)
:param cutfrac: float - fraction of gene cut in cross over (less than 0.5)
:param tracefrac: float - fraction of traced dnas (less than 1)
:param fittype: string code for type of fittness function. Available: 'similarity' (default), 'rmse'
:return: list of traced generations
"""
from sys import getsizeof
#
np.random.seed(seed)
#
parents = pop0
trace = list()
if tracepop:
trace_pop = list()
for g in range(generations):
if tui:
print('\n\nGeneration {}\n'.format(g + 1))
# get offstring
offspring = generate_offspring(parents, offsfrac=offsfrac, nucleotides=nucleotides, mutrate=mutrate,
puremutrate=puremutrate, cutfrac=cutfrac)
# recruit new population
population = recruitment(parents, offspring)
if tui:
print('Population: {} KB'.format(getsizeof(population)))
# fit new population
ids_lst = list()
scores_lst = list()
pop_dct = dict()
if tracepop:
dnas_lst = list()
#
# loop in individuals
for i in range(len(population)):
#
# get local score and id:
lcl_dna = population[i] # local dna
#
#
#
# Get fitness score:
if fittype == 'similarity':
lcl_dna_score = fitness_similarity(lcl_dna, solution=solution)
elif fittype == 'rmse':
lcl_dna_score = fitness_rmse(lcl_dna, solution=solution)
#
#
lcl_dna_id = 'G' + str(g + 1) + '-' + str(i)
#
# store in retrieval system:
pop_dct[lcl_dna_id] = lcl_dna
ids_lst.append(lcl_dna_id)
scores_lst.append(lcl_dna_score)
if tracepop:
dnas_lst.append(lcl_dna)
#
# trace population
if tracepop:
trace_pop.append({'DNAs':dnas_lst[:], 'Ids':ids_lst[:], 'Scores':scores_lst[:]})
#
# rank new population (Survival)
df_population_rank = pd.DataFrame({'Id':ids_lst, 'Score':scores_lst})
df_population_rank.sort_values(by='Score', ascending=False, inplace=True)
#
# Selection of mating pool
df_parents_rank = df_population_rank.nlargest(len(pop0), columns=['Score'])
#
parents_ids = df_parents_rank['Id'].values # numpy array of string IDs
parents_scores = df_parents_rank['Score'].values # numpy array of float scores
#
parents_lst = list()
for i in range(len(parents_ids)):
parents_lst.append(pop_dct[parents_ids[i]])
parents = tuple(parents_lst) # parents DNAs
#
# printing
if tui:
for i in range(10):
print('{}'.format(round(parents_scores[i], 3)))
tr_len = int(len(pop0) * tracefrac)
#print('>>> {}'.format(tr_len))
#
# trace parents
trace.append({'DNAs':parents[:tr_len],
'Ids':parents_ids[:tr_len],
'Scores':parents_scores[:tr_len]})
if tui:
print('Trace size: {} KB'.format(getsizeof(trace)))
print('Trace len: {}'.format(len(trace)))
#if parents_scores[i] > 90:
#
# returns
if tracepop:
return trace, trace_pop
else:
return trace
def evolve(pop0, nucleotides, solution,
seed=666,
generations=10,
offsfrac=1,
mutrate=0.2,
puremutrate=0.1,
cutfrac=0.2,
tracefrac=0.3,
tracepop=False,
fittype='similarity',
tui=False):
"""
Modified evolution routine to optimize fitness over-operations
:param pop0: initial population DNAs
:param nucleotides: tuple of genes nucleotides (tuple of tuples)
:param solution: tuple of gene solutions (tuple of objects) --- needed for fitness function
:param seed: int number for random state
:param generations: int - number of generations
:param offsfrac:
:param mutrate: float - mutation rate (less than 1)
:param puremutrate: float - fraction of pure mutations (less than 1)
:param cutfrac: float - fraction of gene cut in cross over (less than 0.5)
:param tracefrac: float - fraction of traced dnas (less than 1)
:param tracepop: boolean to trace full population
:param fittype: string code for type of fittness function. Available: 'similarity' (default), 'rmse'
:param tui: boolean - display screen
:return:
"""
from sys import getsizeof
#
#
# 1) deploy random state
np.random.seed(seed)
#
parents = pop0
trace = list()
if tracepop:
trace_pop = list()
#
#
# 2) loop in generations
pop_dct = dict() # population dictionary
for g in range(generations):
if tui:
print('\n\nGeneration {}\n'.format(g))
#
#
# 3) REPRODUCE the fitting population
if g == 0:
population = parents
else:
# get offstring
offsize = int(offsfrac * len(pop0))
population = generate_offspring(parents, offsize=offsize, nucleotides=nucleotides, mutrate=mutrate,
puremutrate=puremutrate, cutfrac=cutfrac)
if tui:
print(len(population))
print('Population: {} KB'.format(getsizeof(population)))
#
#
# 4) FIT new population
ids_lst = list()
scores_lst = list()
if tracepop:
dnas_lst = list()
#
# loop in individuals
for i in range(len(population)):
#
# get local score and id:
lcl_dna = population[i] # local dna
#
#
#
# Get fitness score:
if fittype == 'similarity':
lcl_dna_score = fitness_similarity(lcl_dna, solution=solution)
elif fittype == 'rmse':
lcl_dna_score = fitness_rmse(lcl_dna, solution=solution)
#
#
lcl_dna_id = 'G' + str(g) + '-' + str(i)
#
# store in retrieval system:
pop_dct[lcl_dna_id] = lcl_dna
#
ids_lst.append(lcl_dna_id)
scores_lst.append(lcl_dna_score)
if tracepop:
dnas_lst.append(lcl_dna)
#
# trace population
if tracepop:
trace_pop.append({'DNAs': dnas_lst[:], 'Ids': ids_lst[:], 'Scores': scores_lst[:]})
#
#
# 5) RECRUIT new population
if g == 0:
df_parents_rank = pd.DataFrame({'Id': ids_lst, 'Score': scores_lst})
else:
df_offspring_rank = pd.DataFrame({'Id': ids_lst, 'Score': scores_lst})
if tui:
print('\nOffspring:')
print(df_offspring_rank.to_string())
print(len(df_offspring_rank))
# append to existing dataframe
df_parents_rank = df_parents_rank.append(df_offspring_rank, ignore_index=True)
#
#
# 6) RANK population
df_parents_rank.sort_values(by='Score', ascending=False, inplace=True, ignore_index=True)
#
#
# 7) SELECT mating pool
df_parents_rank = df_parents_rank.nlargest(len(pop0), columns=['Score'])
# printing
if tui:
print('\nNew parents:')
print(df_parents_rank.to_string())
#
parents_ids = df_parents_rank['Id'].values # numpy array of string IDs
parents_scores = df_parents_rank['Score'].values # numpy array of float scores
#
parents_lst = list()
for i in range(len(parents_ids)):
parents_lst.append(pop_dct[parents_ids[i]])
# recicle index
pop_dct = dict()
for i in range(len(parents_ids)):
pop_dct[parents_ids[i]] = parents_lst[i]
# new parents
parents = tuple(parents_lst) # parents DNAs
#
tr_len = int(len(pop0) * tracefrac)
# print('>>> {}'.format(tr_len))
#
# trace parents
trace.append({'DNAs': parents[:tr_len],
'Ids': parents_ids[:tr_len],
'Scores': parents_scores[:tr_len]})
if tui:
print('Trace size: {} KB'.format(getsizeof(trace)))
print('Index size: {} KB'.format(getsizeof(pop_dct)))
print('Trace len: {}'.format(len(trace)))
# if parents_scores[i] > 90:
#
# returns
if tracepop:
return trace, trace_pop
else:
return trace
def evolve_moea(pop0, nucleotides, solutions, seed, mutrate=0.10, generations=10):
"""
Evolution of DNAs
:param pop0: initial population DNAs
:param nucleotides: tuple of genes nucleotides (tuple of tuples)
:param solutions: 3d Tuple of tuple of gene solutions (tuple of tuple of objects) --- needed for fitness function
:param seed: int number for random state
:param mutrate: mutation rate (float fraction)
:param generations: number of generations
:return: list of traced generations
"""
from sys import getsizeof
#
np.random.seed(seed)