forked from CorticalComputer/DXNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modular_mutator.erl
1739 lines (1641 loc) · 65.2 KB
/
modular_mutator.erl
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
%% This source code and work is provided and developed by DXNN Research Group WWW.DXNNResearch.COM
%%
%Copyright (C) 2009 by Gene Sher, DXNN Research Group, CorticalComputer@gmail.com
%All rights reserved.
%
%This code is licensed under the version 3 of the GNU General Public License. Please see the LICENSE file that accompanies this project for the terms of use.
-module(modular_mutator).
-compile(export_all).
-include("records.hrl").
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mutagens %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-define(MUTATION_OPERATORS,[{add_Neuron,0.2},{neurolink_OutputSplice,0.2},{add_ONLink,0.2},{add_INLink,0.2},{add_Threshold,0.2}]). %change_ActivationFunction
mos()->?MUTATION_OPERATORS.
-define(SUBCORE_MUTAGENS,[
% add_SubCore,
% remove_SubCore,
% subCoreLink_Splice,
% subCoreLink_DeSplice,
% add_SubCoreLink,
% change_SubCorePlasticity,
% add_SubCoreModulator,
% remove_SubCoreModulator,
{add_Neuron,2}, %Can also add a link to a new sensor or actuator
% remove_Neuron,
{neurolink_OutputSplice,2},
% neurolink_DeSplice,
%neurolink_InputSplice,
% neurolink_DeInSplice,
{add_ONLink,2}, %Can also add a link to a new actuator
% remove_ONLink,
{add_INLink,2}, %Can also add a link to a new sensor
% remove_INLink,
% change_PlasticityFunction,
% change_ActivationFunction,
% reset_DWP,
% reset_Neuron,
% add_NeuroModulation,
% remove_NeuroModulation,
% increase_SubstrateResolution,
% decrease_SubstrateResolution,
% increase_SubstrateDepth,
% decrease_SubstrateDepth,
{add_SensorLink,1},
{add_ActuatorLink,1},
% add_Sensor,
% add_Actuator,
% remove_Threshold,
%{perturb_weights,485},
%{add_CircuitLayer,2},
%{add_CircuitNode,4},
%delete_CircuitNode,
{add_Threshold,1}
]).%change_Adapter,change_ActivationFunction,reset_DWP,reset_Neuron]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Modular_Mutator Parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-define(SCCTTypes,[single,block,block]).%[single,block,all],
-define(HYPERCUBE_CFTAGS,morphology:get_HCF(Cx#cortex.dimensions,Cx#cortex.plasticity,Cx#cortex.type)).
-define(HYPERCUBE_CTTAGS,morphology:get_HCT(Cx#cortex.dimensions,Cx#cortex.plasticity)).%TODO: Add standard coordinates
-define(ACTUATOR_TAGS,morphology:get_Actuators(DX#dx.morphology)).
-define(SENSOR_TAGS,morphology:get_Sensors(DX#dx.morphology)).
-define(EVO_STRAT,static). %static|evolving
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
test()->
F = fun()->
mutate(dx_test)
end,
mnesia:transaction(F).
mutate(DX_Id)->
{A,B,C} = now(),
random:seed(A,B,C),
[DX] = mnesia:read({dx,DX_Id}),
OldGeneration = DX#dx.generation,
NewGeneration = OldGeneration+1,
mnesia:write(DX#dx{
generation = NewGeneration,
evo_strat=agent_evo_strat:mutate(DX#dx.evo_strat),
brittleness=0,
robustness=0,
evolutionary_capacitance=0,
fitness=undefined,
main_fitness=undefined}
),
[Cx] = mnesia:read({cortex,DX#dx.cx_id}),
mnesia:write(Cx#cortex{generation = NewGeneration}),
apply_Mutagens(DX_Id,NewGeneration),
verify_network(DX_Id),%%%TODO:Should be Cx or Cx_Id rather than DX_Id
technome_constructor:update_Stats(DX_Id),
technome_constructor:reset_fitness(DX_Id),
find_AbroptEdges(DX_Id), %What is a NeuroEdge?
ok.
test(DX_Id,MutationOperator)->
F = fun()->
[DX] = mnesia:dirty_read({dx,DX_Id}),
Summary = DX#dx.summary,
Cx_Id = DX#dx.cx_id,
modular_mutator:MutationOperator(DX_Id,Cx_Id)
end,
mnesia:transaction(F).
%-record(agent_evo_strat,{
% strategies_mutation_prob = 0,%Probability of mutating an evolutionary strategies parameter, increments and decrements based on how far from the edge: 0-100%.
% tuning_mutation_range = math:pi(),%From -2Pi to Pi, starts of with a range of -Pi to Pi.
% tuning_annealing_flag = false,%true|false
% annealing_parameter = 0.5,%Standard: math:pi()*math:pow(0.5,Age) tuning_mutation_range*math:pow(anealing_parameter,Age), ranges from 0 to 1, where 1 is no anealing, and 0 is stop evolving.
% topological_mutation_prob = 1,%Standard: Range from 1 to 1/sqrt(Tot_Neurons), Multiplier range: 1 to sqrt(Tot_Neurons).
% topological_annealing_flag = false,%true|false
% neuron_perturbation_prob = 1,%Standard: 1/sqrt(TotNeurons) Probability of choosing a neuron for perturbation, multiplier: 1 to sqrt(TotNeurons)
% weight_perturbation_prob = 1,%Standard: 1/sqrt(TotWeights) Probability of choosing a weight for perturbation, multiplier: 1 to sqrt(TotWeights)
% active_neuron_selection_type = dynamic_random,%What type to use to select active neurons: %[dynamic|active|recent|current|all|dynamic_random|active_random|recent_random|current_random|all_random]
% active_neuron_selection_parameter = undefined %Augment the parameter of selection, dependent on type
%}).
apply_Mutagens(DX_Id,NewGeneration)->
[DX] = mnesia:read({dx,DX_Id}),
Summary = DX#dx.summary,
Cx_Id = DX#dx.cx_id,
Tot_Neurons = Summary#summary.tot_neurons,
case ?EVO_STRAT of
static ->
TotMutations = random:uniform(round(math:pow(Tot_Neurons,1/2))),
Mutation_Operators = ?SUBCORE_MUTAGENS;
evolving ->
EvoStrat = DX#dx.evo_strat,
%io:format("EvoStrat:~p~n",[EvoStrat]),
TotMutations = round(Tot_Neurons*EvoStrat#agent_evo_strat.topological_mutation_prob),
Mutation_Operators = EvoStrat#agent_evo_strat.mutation_operators,
io:format("TotMutations:~p~n",[TotMutations]);
dynamic ->
TotMutations = void,
Mutation_Operators = void
end,
apply_Mutagens(DX_Id,NewGeneration,Cx_Id,TotMutations,Mutation_Operators).
apply_Mutagens(_DX_Id,_NewGeneration,_Cx_Id,0,_Mutation_Operators)->
done;
apply_Mutagens(DX_Id,NewGeneration,Cx_Id,MutationIndex,Mutation_Operators)->
Result = apply_SCLevelMutagen(DX_Id,NewGeneration,Cx_Id,Mutation_Operators),
case Result of
{atomic,_} ->
apply_Mutagens(DX_Id,NewGeneration,Cx_Id,MutationIndex-1,Mutation_Operators);
Error ->
io:format("******** Error:~p~nRetrying with new Mutagen...~n",[Error]),
apply_Mutagens(DX_Id,NewGeneration,Cx_Id,MutationIndex,Mutation_Operators)
end.
apply_SCLevelMutagen(DX_Id,NewGeneration,Cx_Id,Mutagens)->
F = fun()->
Mutagen = get_RandomMutagen(Mutagens),
io:format("Mutagen:~p~n",[Mutagen]),
modular_mutator:Mutagen(DX_Id,Cx_Id)
end,
mnesia:transaction(F).
get_RandomMutagen(Mutagens)->
case Mutagens of
[{Mutagen,_PercentageSector}|_] ->
Tot=lists:sum([RelativeProbability||{Mutator,RelativeProbability} <- Mutagens]),
get_RandomMutagen(0,Mutagens,random:uniform(Tot));
_ ->
Tot_Mutagens = length(Mutagens),
lists:nth(random:uniform(Tot_Mutagens),Mutagens)
end.
get_RandomMutagen(Range_From,[{Mutagen,Prob}|Mutagens],Choice)->
Range_To = Range_From+Prob,
case (Choice >= Range_From) and (Choice =< Range_To) of
true ->
Mutagen;
false ->
get_RandomMutagen(Range_To,Mutagens,Choice)
end;
get_RandomMutagen(_Rage_From,[],_Choice)->
exit("********ERROR:get_RandomMutagen:: in get_RandomMutagen(Mutagens,Choice), Mutagens reached []").
find_AbroptEdges(DX_Id)->
[DX] = mnesia:read({dx,DX_Id}),
N_Ids = DX#dx.n_ids,
find_NeuroEdges(N_Ids,[],[]).
find_NeuroEdges([N_Id|N_Ids],ZeroIAcc,ZeroOAcc)->
[N] = mnesia:read({neuron,N_Id}),
I = N#neuron.i,
O = N#neuron.o,
case {length(I),length(O)} of
{0,0} -> find_NeuroEdges(N_Ids,[N_Id|ZeroIAcc],[N_Id|ZeroOAcc]);
{0,_} -> find_NeuroEdges(N_Ids,[N_Id|ZeroIAcc],ZeroOAcc);
{_,0} -> find_NeuroEdges(N_Ids,ZeroIAcc,[N_Id|ZeroOAcc]);
{_,_} -> find_NeuroEdges(N_Ids,ZeroIAcc,ZeroOAcc)
end;
find_NeuroEdges([],ZeroIAcc,ZeroOAcc)->
case (length(ZeroIAcc) =/= 0) or (length(ZeroOAcc) =/= 0) of
true ->
exit("********ERROR:find_NeuroEdges:: ZeroEdge Error");
false ->
done
end.
%%%================================================================SubCore Mutagens================================================================%%%
%%evo_hist: Evolutionary History in a list: [{Generation,TypeOfMutagen,Applied_On,From,To}...]
update_EvoHist(DX_Id,Mutagen,New_Id,Applied_On,From,To,Parameter)->
[DX] = mnesia:read({dx,DX_Id}),
Generation = DX#dx.generation,
EvoHist = DX#dx.evo_hist,
U_EvoHist = [{Generation,Mutagen,New_Id,Applied_On,From,To,Parameter}|EvoHist],
%io:format("EvoHist:~p U_EvoHist:~p~n",[EvoHist,U_EvoHist]),
mnesia:write(DX#dx{
evo_hist = U_EvoHist
}).
%--------------------------------insert_NewLayer--------------------------------
%%%Notes:
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
insert_NewLayer(SU_Id,NewLI)->
% io:format("SU_Id:~p NewLI:~p~n",[SU_Id,NewLI]),
case SU_Id of
{_,cortex} ->
[Cx] = mnesia:read({cortex,SU_Id}),
Pattern = Cx#cortex.pattern,
U_Pattern = add_LayerToPattern(NewLI,Pattern),
mnesia:write(Cx#cortex{pattern = U_Pattern})
end.
add_LayerToPattern(NewLI,Pattern)->
% io:format("add_LayerToPattern, NewLI:~p Pattern:~p~n",[NewLI,Pattern]),
[{FirstLI,FirstLL}|PTail] = Pattern,
[{LastLI,_}|_] = lists:reverse(Pattern),
case (NewLI > LastLI) or (NewLI < FirstLI) of
true ->
case NewLI > LastLI of
true ->
Pattern ++ [{NewLI,0}];
false ->
[{NewLI,0}] ++ Pattern
end;
false ->
insert_NewLayer(NewLI,PTail,[{FirstLI,FirstLL}])
end.
insert_NewLayer(NewLI,[{LI2,LL2}|Pattern],UpdatedPatternAcc)->
[{LI1,LL1}|_] = UpdatedPatternAcc,
% io:format("NewLI:~p LI2:~p LI1:~p~n",[NewLI,LI2,LI1]),
case (NewLI > LI1) and (NewLI < LI2) of
true ->
insert_NewLayer(NewLI,Pattern,[{LI2,LL2},{NewLI,0}|UpdatedPatternAcc]);
false ->
insert_NewLayer(NewLI,Pattern,[{LI2,LL2}|UpdatedPatternAcc])
end;
insert_NewLayer(_NewLI,[],UpdatedPatternAcc)->
lists:reverse(UpdatedPatternAcc).
add_UnitToPattern([{LI,LL}|Pattern],TL,U_Pattern)->
case LI == TL of
true ->
add_UnitToPattern(Pattern,TL,[{LI,LL+1}|U_Pattern]);
false ->
add_UnitToPattern(Pattern,TL,[{LI,LL}|U_Pattern])
end;
add_UnitToPattern([],_TL,U_Pattern)->
lists:reverse(U_Pattern).
get_NewLI(LI,LI,Direction,Pattern)->
exit("******** ERROR: get_NewLI FromLI == ToLI");
get_NewLI(FromLI,ToLI,Direction,Pattern)->
% io:format("FromLI:~p ToLI:~p Direction:~p Pattern:~p~n",[FromLI,ToLI,Direction,Pattern]),
NewLI = case Direction of
next ->
get_NextLI(Pattern,FromLI,ToLI);
prev ->
get_PrevLI(lists:reverse(Pattern),FromLI,ToLI)
end,
% io:format("FromLI:~p ToLI:~p Direction:~p Pattern:~p NewLI:~p~n",[FromLI,ToLI,Direction,Pattern,NewLI]),
NewLI.
get_NextLI([{LI,_LastLayerDensity}],LI,su)->
(LI+1)/2;
get_NextLI([{LI,_LayerDensity}|Pattern],FromLI,ToLI)->
case LI == FromLI of
true ->
[{NextLI,_NextLayerDensity}|_] = Pattern,
case NextLI == ToLI of
true ->
(FromLI + ToLI)/2;
false ->
NextLI
end;
false ->
get_NextLI(Pattern,FromLI,ToLI)
end.
get_PrevLI([{LI,_FirstLayerDensity}],LI,su)->
(-1+LI)/2;
get_PrevLI([{LI,_LayerDensity}|Pattern],FromLI,ToLI)->
case LI == FromLI of
true ->
[{PrevLI,_PrevLayerDensity}|_] = Pattern,
case PrevLI == ToLI of
true ->
(FromLI + ToLI)/2;
false ->
PrevLI
end;
false ->
get_PrevLI(Pattern,FromLI,ToLI)
end.
%--------------------------------get_TargetLayerIds--------------------------------
%%%Notes:
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
get_TargetLayerIds(SU_Id,0,_Ids,_TargetLayerIdsAcc)->
[SU_Id];
get_TargetLayerIds(SU_Id,TLI,[{{LI,UId},TL}|Ids],TargetLayerIdsAcc)->
case TLI =:= LI of
true ->
get_TargetLayerIds(SU_Id,TLI,Ids,[{{LI,UId},TL}|TargetLayerIdsAcc]);
false ->
get_TargetLayerIds(SU_Id,TLI,Ids,TargetLayerIdsAcc)
end;
get_TargetLayerIds(_SU_Id,_TLI,[],TargetLayerIdsAcc)->
lists:reverse(TargetLayerIdsAcc).
%--------------------------------remove_LayerFromPattern--------------------------------
remove_LayerFromPattern(LI,TLI,[LL|Pattern],UpdatedPatternAcc)->
case LI == TLI of
true ->
case LL == 0 of
true ->
remove_LayerFromPattern(LI+1,TLI,Pattern,UpdatedPatternAcc);
false ->
exit("********ERROR:remove_LayerFromPattern:: in remove_LayerFromPattern(), LL =/= 0 ~n")
end;
false ->
remove_LayerFromPattern(LI+1,TLI,Pattern,[LL|UpdatedPatternAcc])
end;
remove_LayerFromPattern(_LI,_TLI,[],UpdatedPatternAcc)->
lists:reverse(UpdatedPatternAcc).
%update_EvoHist(DX_Id,Mutagen,New_Id,Applied_On,From,To,Parameter)
%--------------------------------increase_SubstrateResolution--------------------------------
increase_SubstrateResolution(DX_Id,Cx_Id)->
[Cx] = mnesia:read({cortex,Cx_Id}),
case Cx#cortex.type of
neural ->
exit("******** increase_SubstrateResolution not applicable to Cx#cortex.type == neural~n");
aart ->
exit("******** increase_SubstrateResolution not applicable to Cx#cortex.type == aart~n");
hypercube ->
Old_Densities = Cx#cortex.densities,
[Old_Depth|Old_SubDensities] = Old_Densities,
%New_SubDensities = [Density+random:uniform(round(math:sqrt(Density))) || Density <- Old_SubDensities],
New_SubDensities = [Density+(random:uniform(5)-1) || Density <- Old_SubDensities],
New_Densities = [Old_Depth|New_SubDensities],
mnesia:write(Cx#cortex{
densities = New_Densities
}),
update_EvoHist(DX_Id,increase_SubstrateResolution,void,Cx_Id,void,void,void)
end.
%--------------------------------decrease_SubstrateResolution--------------------------------
decrease_SubstrateResolution(_DX_Id,_Cx_Id)->
done.%TODO
%--------------------------------increase_SubstrateDepth--------------------------------
increase_SubstrateDepth(DX_Id,Cx_Id)->
[Cx] = mnesia:read({cortex,Cx_Id}),
case Cx#cortex.type of
neural ->
exit("******** increase_SubstrateDepth not applicable to Cx#cortex.type == neural~n");
aart ->
exit("******** increase_SubstrateDepth not applicable to Cx#cortex.type == aart~n");
hypercube ->
Old_Densities = Cx#cortex.densities,
[Old_Depth|Old_SubDensities] = Old_Densities,
New_Depth = Old_Depth+1,
New_Densities = [New_Depth|Old_SubDensities],
mnesia:write(Cx#cortex{
densities = New_Densities
}),
update_EvoHist(DX_Id,increase_SubstrateDepth,void,Cx_Id,void,void,void)
end.
%--------------------------------decrease_SubstrateDepth--------------------------------
decrease_SubstrateDepth(_DX_Id,_Cx_Id)->
done.%TODO
%{add_CircuitLayer,3},
%{add_CircuitNode,3},
%delete_CircuitNode,
add_CircuitNode(DX_Id,Cx_Id)->
[DX] = mnesia:read({dx,DX_Id}),
[Cx] = mnesia:read({cortex,Cx_Id}),
io:format("Inside add_CircuitNode(DX_Id,Cx_Id)~n"),
NId_Pool=Cx#cortex.cids,
N_Id = lists:nth(random:uniform(length(NId_Pool)),NId_Pool),
[N] = mnesia:read({neuron,N_Id}),
case N#neuron.type of
standard ->
exit("******** ERROR: Not a circuit type neuron, can not execute: add_CircuitNode(DX_Id,Cx_Id)~n");
circuit ->
Circuit = N#neuron.dwp,
TotLayers = length(Circuit)-1,
LayerIndex = random:uniform(TotLayers),
VL = case LayerIndex == 1 of
true ->
N#neuron.ivl;
false ->
undefined
end,
U_Circuit=circuit:add_neurode(Circuit,LayerIndex,VL),
U_N = N#neuron{dwp=U_Circuit},
mnesia:write(U_N),
update_EvoHist(DX_Id,add_CircuitNode,void,N_Id,void,void,void)
end.
add_CircuitLayer(DX_Id,Cx_Id)->
[DX] = mnesia:read({dx,DX_Id}),
[Cx] = mnesia:read({cortex,Cx_Id}),
io:format("add_CircuitLayer(DX_Id,Cx_Id)~n"),
NId_Pool=Cx#cortex.cids,
N_Id = lists:nth(random:uniform(length(NId_Pool)),NId_Pool),
[N] = mnesia:read({neuron,N_Id}),
case N#neuron.type of
standard ->
exit("******** ERROR: Not a circuit type neuron, can not execute: add_CircuitLayer(DX_Id,Cx_Id)~n");
circuit ->
Circuit = N#neuron.dwp,
TotLayers = length(Circuit),
LayerIndex = random:uniform(TotLayers),
VL = case LayerIndex == TotLayers of
true ->
random:uniform(1);
false ->
random:uniform(3)
end,
U_Circuit=circuit:add_layer(Circuit,LayerIndex,VL),
U_N = N#neuron{dwp=U_Circuit},
mnesia:write(U_N),
update_EvoHist(DX_Id,add_layer,void,N_Id,void,void,void)
end.
%--------------------------------add_Sensor--------------------------------
%update_EvoHist(DX_Id,Mutagen,New_Id,Applied_On,From,To,Parameter)
add_SensorLink(DX_Id,Cx_Id)->%TODO We need to eventually make sure that sensor holds the most recent list of sensors, same for actuators.
[DX] = mnesia:read({dx,DX_Id}),
[Cx] = mnesia:read({cortex,Cx_Id}),
io:format("Inside add_Sensor()~n"),
case Cx#cortex.type of
neural ->
case Cx#cortex.cids--[N_Id|| {N_Id,_Filter_Tag}<-lists:flatten([NIdPs|| {_Sensor,NIdPs} <- Cx#cortex.ct])] of
[] ->
exit("******** ERROR: No unconnected Neurons in add_SensorLink(DX_Id,Cx_Id)~n");
NId_Pool ->
io:format("****************add_SensorLink(DX_Id,Cx_Id) NId_Pool:~p~n",[NId_Pool]),
IId_Pool = [Cx_Id],
N_Id = lists:nth(random:uniform(length(NId_Pool)),NId_Pool),
%add_INeuroLinks(DX_Id,N_Id,IId_Pool),
[link_FromElementToElement(DX_Id,From_Id,N_Id) || From_Id <-IId_Pool],
update_EvoHist(DX_Id,add_SensorLink,N_Id,Cx_Id,void,void,void)
end;
%exit("******** ERROR: No Neural type add_Sensor(DX_Id,Cx_Id) exists yet~n");
hypercube ->
CurrentSensors = Cx#cortex.sensors,
case ?SENSOR_TAGS--CurrentSensors of
[] ->
exit("******** ERROR: No new Sensors to add in add_SensorLink(DX_Id,Cx_Id)~n");
AvailableNew_Sensors ->
NewSensor = lists:nth(random:uniform(length(AvailableNew_Sensors)),AvailableNew_Sensors),
mnesia:write(Cx#cortex{
sensors = [NewSensor|CurrentSensors]
}),
update_EvoHist(DX_Id,add_SensorLink,NewSensor#sensor.name,Cx_Id,void,void,void)
end;
aart ->
CurrentSensors = Cx#cortex.sensors,
case ?SENSOR_TAGS--CurrentSensors of
[] ->
exit("******** ERROR: No new Sensors to add in add_SensorLink(DX_Id,Cx_Id)~n");
AvailableNew_Sensors ->
NewSensor = lists:nth(random:uniform(length(AvailableNew_Sensors)),AvailableNew_Sensors),
mnesia:write(Cx#cortex{
sensors = [NewSensor|CurrentSensors]
}),
update_EvoHist(DX_Id,add_SensorLink,NewSensor#sensor.name,Cx_Id,void,void,void)
end
end.
%--------------------------------add_Actuator--------------------------------
add_ActuatorLink(DX_Id,Cx_Id)->%TODO in the case of a Neural system, it links to an Actuator, sometimes new, sometimes the same.
[DX] = mnesia:read({dx,DX_Id}),
[Cx] = mnesia:read({cortex,Cx_Id}),
io:format("Inside add_Actuator()~n"),
case Cx#cortex.type of
neural ->
case Cx#cortex.cids--lists:flatten([NIds||{_Actuator,NIds} <-Cx#cortex.cf]) of
[] ->
exit("******** ERROR: No unconnected Neurons in add_ActuatorLink(DX_Id,Cx_Id)~n");
NId_Pool ->
io:format("****************add_ActuatorLink(DX_Id,Cx_Id) NId_Pool:~p~n",[NId_Pool]),
OId_Pool = [Cx_Id],
N_Id = lists:nth(random:uniform(length(NId_Pool)),NId_Pool),
%add_ONeuroLinks(DX_Id,N_Id,OId_Pool),
[link_FromElementToElement(DX_Id,N_Id,To_Id) || To_Id <- OId_Pool],
update_EvoHist(DX_Id,add_ActuatorLink,N_Id,Cx_Id,void,void,void)
end;
%exit("******** ERROR: No Neural type add_Actuator(DX_Id,Cx_Id) exists yet~n");
hypercube ->
CurrentActuators = Cx#cortex.actuators,
case ?ACTUATOR_TAGS--CurrentActuators of
[] ->
exit("******** ERROR: No new Actuators to add in add_ActuatorLink(DX_Id,Cx_Id)~n");
AvailableNew_Actuators ->
NewActuator = lists:nth(random:uniform(length(AvailableNew_Actuators)),AvailableNew_Actuators),
mnesia:write(Cx#cortex{
actuators = [NewActuator|CurrentActuators]
}),
update_EvoHist(DX_Id,add_ActuatorLink,NewActuator#actuator.name,Cx_Id,void,void,void)
end;
aart ->
CurrentActuators = Cx#cortex.actuators,
case ?ACTUATOR_TAGS--CurrentActuators of
[] ->
exit("******** ERROR: No new Actuators to add in add_ActuatorLink(DX_Id,Cx_Id)~n");
AvailableNew_Actuators ->
NewActuator = lists:nth(random:uniform(length(AvailableNew_Actuators)),AvailableNew_Actuators),
mnesia:write(Cx#cortex{
actuators = [NewActuator|CurrentActuators]
}),
update_EvoHist(DX_Id,add_ActuatorLink,NewActuator#actuator.name,Cx_Id,void,void,void)
end
end.
%--------------------------------NeuroSplice--------------------------------
%%%Notes:
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
neurolink_OutputSplice(DX_Id,Cx_Id)-> %TODO: Currently does only forward based splicing.
[DX] = mnesia:read({dx,DX_Id}),
Generation = DX#dx.generation,
[BeforeNL_Cx] = mnesia:read({cortex,Cx_Id}),
BeforeNL_Pattern = BeforeNL_Cx#cortex.pattern,
BeforeNL_CIds = BeforeNL_Cx#cortex.cids,
Tot_Neurons = length(BeforeNL_CIds),
N_Id = lists:nth(random:uniform(Tot_Neurons),BeforeNL_CIds),
[N] = mnesia:read({neuron,N_Id}),
%O_Ids = N#neuron.o,
O_IdPool = case above_LimitedCIds(Cx_Id,N_Id,N#neuron.o,[]) of
[] ->
exit("********ERROR:neurolink_OutputSplice:: NeuroLink_OutputSplice O_IdPool == []");
Ids ->
Ids
end,
%O_IdPool = N#neuron.o,
%O_IdPool = case different_LimitedCIds(SC_Id,N_Id,N#neuron.o,[]) of
% [] ->
% exit("********ERROR:neurolink_OutputSplice:: NeuroLink_OutputSplice O_IdPool == []");
% Ids ->
% Ids
%end,
io:format("neurolink_OutputSplice(DX_Id,Cx_Id)::N_Id~p, O_IdPool:~p~n",[N_Id,O_IdPool]),
Tot_OLinks = length(O_IdPool),
O_Id = lists:nth(random:uniform(Tot_OLinks),O_IdPool),
{{TLI,_},neuron} = N_Id,
{NewN_Id,NewLI} = case O_Id of
Cx_Id ->
NewLI = get_NewLI(TLI,su,next,BeforeNL_Pattern),
case lists:keymember(NewLI,1,BeforeNL_Pattern) of %To make sure that the NewLI is either the last layer or below, otherwise add new layer.
true ->
%insert_NewLayer(SC_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
insert_NewLayer(Cx_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI}
end;
{{OLI,NumId},neuron} ->
case OLI =< TLI of %In the case that recursive neurolink_OutputSplices are allowed, which is not in the current version.
true ->
NewLI = TLI,
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
NewLI = get_NewLI(TLI,OLI,next,BeforeNL_Pattern),
case lists:keymember(NewLI,1,BeforeNL_Pattern) of %If NewLI member, then np, if not add a new layer to pattern.
true->
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
insert_NewLayer(Cx_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI}
end
end
end,
[Cx] = mnesia:read({cortex,Cx_Id}),
Pattern = Cx#cortex.pattern,
CIds = Cx#cortex.cids,
technome_constructor:construct_Neuron(Cx_Id,Generation,NewN_Id,{0,[]},{1,[]},get_SpeCon(specie_id,DX#dx.specie_id),DX#dx.neural_type,DX#dx.heredity_type),
U_Pattern = add_UnitToPattern(Pattern,NewLI,[]),
mnesia:write(Cx#cortex{
pattern = U_Pattern,
cids = [NewN_Id|CIds]}),
[NewN] = mnesia:read({neuron,NewN_Id}),
mnesia:write(NewN#neuron{
ivl = NewN#neuron.ivl + 1,%What if input is different. this should support IVL.
i = [{N_Id,1}|NewN#neuron.i],
dwp = case NewN#neuron.type of
standard ->
[{N_Id,[technome_constructor:null_wt()]}|NewN#neuron.dwp];
circuit ->%The circuit will have a first layer whose neurodes have 1 weight each, to accept input from the neuron N_Id
[NeurodeLayer|Substrate]=NewN#neuron.dwp,
U_NeurodeLayer=[Neurode#neurode{weights=[random:uniform()-0.5||_<-lists:seq(1,1)]}||Neurode<-NeurodeLayer],
[U_NeurodeLayer|Substrate]
end,
o = [O_Id|NewN#neuron.o]
}),
[UN] = mnesia:read({neuron,N_Id}),
mnesia:write(UN#neuron{
o = [NewN_Id|UN#neuron.o] -- [O_Id]}),
case O_Id of
Cx_Id ->
[Cortex] = mnesia:read({cortex,Cx_Id}),
U_CxCF = update_CxCF_NId(N_Id,NewN_Id,Cortex#cortex.cf,[]),
io:format("neurolink_OutputSplice(DX_Id,Cx_Id)::CF:~p U_CF:~p~n",[Cortex#cortex.cf,U_CxCF]),
mnesia:write(Cortex#cortex{cf = U_CxCF});
_ ->
[ON] = mnesia:read({neuron,O_Id}),
mnesia:write(ON#neuron{
i = lists:keyreplace(N_Id,1,ON#neuron.i,{NewN_Id,1}),
dwp = case ON#neuron.type of
standard ->
{value,{N_Id,WeightsP}} = lists:keysearch(N_Id,1,ON#neuron.dwp),
lists:keyreplace(N_Id,1,ON#neuron.dwp,{NewN_Id,WeightsP});
circuit ->%By default a circuit has an output_VL ==1, so the ON was previously either connected to normal neuron or circuit neuron, in both cases output of that was 1, as is now, so nothing changes in dwp
%[ON_NeurodeLayer|ON_Substrate]=ON#neuron.dwp,
%U_ON_NeurodeLayer=[{Id,[random:uniform()-0.5||_<-lists:seq(1,1)]++Weights}||{Id,Weights}<-ON_NeurodeLayer],
%[U_ON_NeurodeLayer|ON_Substrate]
ON#neuron.dwp
end})
end,
update_EvoHist(DX_Id,neurolink_OutputSplice,NewN_Id,N_Id,N_Id,O_Id,void).
update_CxCF_NId(N_Id,NewN_Id,[{Tag,NIds}|CxCF],Acc)->
case lists:member(N_Id,NIds) of
true ->
U_NIds = replace(N_Id,NewN_Id,NIds,[]),
update_CxCF_NId(N_Id,NewN_Id,CxCF,[{Tag,U_NIds}|Acc]);
false ->
update_CxCF_NId(N_Id,NewN_Id,CxCF,[{Tag,NIds}|Acc])
end;
update_CxCF_NId(_N_Id,NewN_Id,[],Acc)->
lists:reverse(Acc).
replace(OldN_Id,NewN_Id,[N_Id|N_Ids],Acc)->
case OldN_Id == N_Id of
true ->
replace(OldN_Id,NewN_Id,N_Ids,[NewN_Id|Acc]);
false ->
replace(OldN_Id,NewN_Id,N_Ids,[N_Id|Acc])
end;
replace(_OldN_Id,_NewN_Id,[],Acc)->
lists:reverse(Acc).
neurolink_InputSplice(DX_Id,Cx_Id)->%CIds = ContainedWithinIds, the Ids contained within some module (Cx in this case).
[DX] = mnesia:read({dx,DX_Id}),
Generation = DX#dx.generation,
[BeforeNL_Cx] = mnesia:read({cortex,Cx_Id}),
BeforeNL_Pattern = BeforeNL_Cx#cortex.pattern,
BeforeNL_CIds = BeforeNL_Cx#cortex.cids,
Tot_Neurons = length(BeforeNL_CIds),
N_Id = lists:nth(random:uniform(Tot_Neurons),BeforeNL_CIds),
[N] = mnesia:read({neuron,N_Id}),
%I_Ids = [I_Id||{I_Id,_IVL}<-N#neuron.i],
I_IdPool = case below_LimitedCIds(Cx_Id,N_Id,[I_Id||{I_Id,_IVL}<-N#neuron.i],[]) of
[] ->
exit("********ERROR:neurolink_InputSplice:: NeuroLink_InputSplice I_IdPool == []");
Ids ->
Ids
end,
io:format("neurlink_InputSplice(DX_Id,Cx_Id)::N_Id~p, I_IdPool:~p~n",[N_Id,I_IdPool]),
Tot_ILinks = length(I_IdPool),
I_Id = lists:nth(random:uniform(Tot_ILinks),I_IdPool),
{{TLI,_},neuron} = N_Id,
io:format("neurlink_InputSplice(DX_Id,Cx_Id)::I_Id:~p~n",[I_Id]),
{NewN_Id,NewLI} = case I_Id of
Cx_Id ->
NewLI = get_NewLI(TLI,su,prev,BeforeNL_Pattern),
io:format("neurlink_InputSplice(DX_Id,Cx_Id)::NewLI:~p~n",[{NewLI,lists:keymember(NewLI,1,BeforeNL_Pattern)}]),
case lists:keymember(NewLI,1,BeforeNL_Pattern) of %To make sure that the NewLI is either the first layer or above, otherwise add new layer.
true ->
%insert_NewLayer(SC_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
insert_NewLayer(Cx_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI}
end;
{{ILI,NumId},neuron} ->
case ILI >= TLI of %In the case that recursive neurolink_InputSplices are allowed, which is not in the current version.
true ->
NewLI = TLI,
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
NewLI = get_NewLI(TLI,ILI,prev,BeforeNL_Pattern),
case lists:keymember(NewLI,1,BeforeNL_Pattern) of %If NewLI member, then np, if not add a new layer to pattern.
true->
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI};
false ->
insert_NewLayer(Cx_Id,NewLI),
{{{NewLI,technome_constructor:generate_UniqueId()},neuron},NewLI}
end
end
end,
[Cx] = mnesia:read({cortex,Cx_Id}),
Pattern = Cx#cortex.pattern,
CIds = Cx#cortex.cids,
technome_constructor:construct_Neuron(Cx_Id,Generation,NewN_Id,{0,[]},{1,[]},get_SpeCon(specie_id,DX#dx.specie_id),DX#dx.neural_type,DX#dx.heredity_type),
U_Pattern = add_UnitToPattern(Pattern,NewLI,[]),
mnesia:write(Cx#cortex{
pattern = U_Pattern,
cids = [NewN_Id|CIds]}),
[NewN] = mnesia:read({neuron,NewN_Id}),
{I_Id,I_VL} = lists:keyfind(I_Id, 1, N#neuron.i),
io:format("NewN#neuron.type:~p~n",[NewN#neuron.type]),
mnesia:write(NewN#neuron{
ivl = NewN#neuron.ivl + I_VL,
i = [{I_Id,I_VL}|NewN#neuron.i],
dwp = case NewN#neuron.type of
standard ->
[{I_Id,[technome_constructor:null_wt()||_<-lists:seq(1,I_VL)]}|NewN#neuron.dwp];
circuit ->%The new neuron will have a circuit whose first layer has I_VL number of weights.
[NeurodeLayer|Substrate]=NewN#neuron.dwp,
U_NeurodeLayer=[Neurode#neurode{weights=[random:uniform()-0.5||_<-lists:seq(1,I_VL)]}||Neurode<-NeurodeLayer],
[U_NeurodeLayer|Substrate]
end,
o = [N_Id|NewN#neuron.o]
}),
[UN] = mnesia:read({neuron,N_Id}),
mnesia:write(UN#neuron{
i = lists:keyreplace(I_Id,1,UN#neuron.i,{NewN_Id,1}),
dwp = case UN#neuron.type of
standard ->
{value,{I_Id,WeightsP}} = lists:keysearch(I_Id,1,UN#neuron.dwp),
lists:keyreplace(I_Id,1,UN#neuron.dwp,{NewN_Id,WeightsP});
circuit ->%Replace the previous IVL of weights with the IVL==1 of the new circuit neuron. First delete weights based on Index and IVL. Then add Weights based on Indexand IVL==1.
[UN_NeurodeLayer|UN_Substrate]=UN#neuron.dwp,
{TargetIndex,TargetVL}=find_index(I_Id,UN#neuron.i,1),
%[NeurodeLayer|Substrate] = ToDWP,
U_UN_NeurodeLayer=[Neurode#neurode{weights=circuit:delete_weights(TargetIndex,TargetVL,Neurode#neurode.weights)}||Neurode<-UN_NeurodeLayer],
U2_UN_NeurodeLayer=[Neurode#neurode{weights=circuit:add_weights(TargetIndex,1,Neurode#neurode.weights)}||Neurode<-U_UN_NeurodeLayer],%Adds an extra weight, 1 because the NewNis a neuron, with an output vector of length 1.
[U2_UN_NeurodeLayer|UN_Substrate]
end
}),
case I_Id of
Cx_Id ->
[Cortex] = mnesia:read({cortex,Cx_Id}),
U_CxCT = update_CxCT_NId(N_Id,NewN_Id,Cortex#cortex.ct,[]),
io:format("CT:~p U_CT:~p~n",[Cortex#cortex.ct,U_CxCT]),
mnesia:write(Cortex#cortex{ct = U_CxCT});
_ ->
[IN] = mnesia:read({neuron,I_Id}),
mnesia:write(IN#neuron{
o = replace(N_Id,NewN_Id,IN#neuron.o,[])
})
end,
update_EvoHist(DX_Id,neurolink_InputSplice,NewN_Id,N_Id,N_Id,I_Id,void).
update_CxCT_NId(N_Id,NewN_Id,[{CT_Tag,NIdPs}|CxCT],Acc)->%CT_Tag= Sensor|Geotag Filter=Link tag
case lists:keyfind(N_Id,1,NIdPs) of
{N_Id,Filter} ->
U_NIdPs = lists:keyreplace(N_Id,1,NIdPs,{NewN_Id,Filter}),
% U_NIds = replace(N_Id,NewN_Id,NIds,[]),
update_CxCT_NId(N_Id,NewN_Id,CxCT,[{CT_Tag,U_NIdPs}|Acc]);
false ->
update_CxCT_NId(N_Id,NewN_Id,CxCT,[{CT_Tag,NIdPs}|Acc])
end;
update_CxCT_NId(_N_Id,NewN_Id,[],Acc)->
lists:reverse(Acc).
filter_LinkType(LinkType,FilterType,SU_Id,TId,Id_Pool)->
% io:format("LinkType,FilterType,SU_Id,TId,Id_Pool:~p~n",[{LinkType,FilterType,SU_Id,TId,Id_Pool}]),
{{TLI,_UniqueId},_NodeType} = TId,
case LinkType of
recursive ->
Id_Pool;
feedforward ->
FF_IdPool = case FilterType of
above ->
filter_LT_above(TLI,SU_Id,Id_Pool,[]);
below ->
filter_LT_below(TLI,SU_Id,Id_Pool,[])
end,
FF_IdPool
end.
filter_LT_above(TLI,SU_Id,[Id|Id_Pool],Acc)->
case Id of
SU_Id ->
filter_LT_above(TLI,SU_Id,Id_Pool,[Id|Acc]);
{{LI,_UniqueId},NodeType} ->
case LI > TLI of
true ->
filter_LT_above(TLI,SU_Id,Id_Pool,[Id|Acc]);
false ->
filter_LT_above(TLI,SU_Id,Id_Pool,Acc)
end
end;
filter_LT_above(_TLI,_SU_Id,[],Acc)->
lists:reverse(Acc).
filter_LT_below(TLI,SU_Id,[Id|Id_Pool],Acc)->
case Id of
SU_Id ->
filter_LT_below(TLI,SU_Id,Id_Pool,[Id|Acc]);
{{LI,_UniqueId},NodeType}->
case LI < TLI of
true ->
filter_LT_below(TLI,SU_Id,Id_Pool,[Id|Acc]);
false ->
filter_LT_below(TLI,SU_Id,Id_Pool,Acc)
end
end;
filter_LT_below(_TLI,_SU_Id,[],Acc)->
lists:reverse(Acc).
%--------------------------------different_LimitedCIds--------------------------------
%Filters Ids such that all ids are those that do not blong to the same layer.
different_LimitedCIds(SU_Id,Target_Id,[Id|Ids],Limited_CIdsAcc)->
case Id of
SU_Id ->
different_LimitedCIds(SU_Id,Target_Id,Ids,[Id|Limited_CIdsAcc]);
_ ->
{{TLI,_},neuron} = Target_Id,
{{LI,_},neuron} = Id,
case LI =/= TLI of
true ->
different_LimitedCIds(SU_Id,Target_Id,Ids,[Id|Limited_CIdsAcc]);
false ->
different_LimitedCIds(SU_Id,Target_Id,Ids,Limited_CIdsAcc)
end
end;
different_LimitedCIds(_SU_Id,_Target_Id,[],Acc)->
Acc.
%--------------------------------Add Threshold--------------------------------
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
add_Threshold(DX_Id,Cx_Id)->
[Cx] = mnesia:read({cortex,Cx_Id}),
Cx_CIds = Cx#cortex.cids,
N_Id = lists:nth(random:uniform(length(Cx_CIds)),Cx_CIds),
add_Threshold(DX_Id,Cx_Id,N_Id).
add_Threshold(DX_Id,Cx_Id,N_Id)->
[DX] = mnesia:read({dx,DX_Id}),
Generation = DX#dx.generation,
[N] = mnesia:read({neuron,N_Id}),
DWP = N#neuron.dwp,
case N#neuron.type of
standard ->
case lists:keymember(threshold, 1, DWP) of
true ->
exit("********ERROR:add_Threshold:: This Neuron already has a threshold part.");
false ->
U_DWP = lists:append(DWP,[{threshold,[technome_constructor:weight_tuple()]}]),
mnesia:write(N#neuron{
dwp = U_DWP,
generation = Generation})
end;
circuit ->
TotWeights=lists:sum(lists:flatten([[length(Neurode#neurode.weights)||Neurode<-Layer]||Layer <-DWP])),
MP=1/TotWeights,
U_DWP=[[ant(Neurode,MP)|| Neurode<-Layer]||Layer<-DWP],
mnesia:write(N#neuron{
dwp = U_DWP,
generation = Generation
})
end,
update_EvoHist(DX_Id,add_Threshold,void,N_Id,void,void,void).
ant(N,MP)->
case N#neurode.bias of
undefined ->
case random:uniform() < MP of
true ->
N#neurode{bias=random:uniform()-0.5};
false ->
N
end;
_Bias ->
N
end.
%--------------------------------remove Thresholds--------------------------------
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
remove_Threshold(DX_Id,Cx_Id)->
[Cx] = mnesia:read({cortex,Cx_Id}),
Cx_CIds = Cx#cortex.cids,
N_Id = lists:nth(random:uniform(length(Cx_CIds)),Cx_CIds),
remove_Threshold(DX_Id,Cx_Id,N_Id).
remove_Threshold(DX_Id,Cx_Id,N_Id)->
[DX] = mnesia:read({dx,DX_Id}),
Generation = DX#dx.generation,
[N] = mnesia:read({neuron,N_Id}),
DWP = N#neuron.dwp,
case lists:keymember(threshold, 1, DWP) of
true ->
U_DWP = lists:keydelete(threshold,1,DWP),
mnesia:write(N#neuron{
dwp = U_DWP,
generation = Generation});
false->
exit("********ERROR:remove_Threshold:: This Neuron does not have a threshold part.")
end,
update_EvoHist(DX_Id,remove_Threshold,void,N_Id,void,void,void).
%--------------------------------Add Neurons--------------------------------
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
add_Neuron(DX_Id,Cx_Id)->
[Cx] = mnesia:read({cortex,Cx_Id}),
Cx_Pattern = Cx#cortex.pattern,
{TargetLayer,_} = lists:nth(random:uniform(length(Cx_Pattern)),Cx_Pattern),
add_Neurons(DX_Id,Cx_Id,TargetLayer,1).
%--------------------------------Add Neurons--------------------------------
%%%Notes:
%%%Function:
%%%Interface:Input: Output:
%%%MsgComunication:
add_Neurons(_DX_Id,_Cx_Id,_TLI,0)->
done;
add_Neurons(DX_Id,Cx_Id,TLI,NeuroIndex)->
add_Neuron(DX_Id,Cx_Id,TLI),
add_Neurons(DX_Id,Cx_Id,TLI,NeuroIndex-1).
proper_OIds(SU_Id,Id_Pool,PresentIds,Elements_Requested)->
case get_OIdPool(SU_Id,Id_Pool,PresentIds) of
[] ->
exit("********ERROR:proper_OIds:: Id_Pool is empty");
OId_Pool ->
get_UniqueIds(OId_Pool,Elements_Requested,[])
end.
get_OIdPool(SU_Id,Id_Pool,PresentIds)->
%io:format("get_OIdPool:: SU_Id:~p Id_Pool:~p PresentIds:~p~n",[SU_Id,Id_Pool,PresentIds]),
[Cx] = mnesia:read({cortex,SU_Id}),
[DX] = mnesia:read({dx,Cx#cortex.su_id}),
CF = Cx#cortex.cf,
Flag =case Cx#cortex.type of
neural ->
InvalidActuators = [Actuator || {Actuator,NIds}<-CF, Actuator#actuator.tot_vl == length(NIds)],
?ACTUATOR_TAGS -- InvalidActuators;
hypercube ->
InvalidTags = [SCF || {SCF,NIds}<-CF, SCF#sCF.tot_vl == length(NIds)],
?HYPERCUBE_CFTAGS -- InvalidTags;
aart ->
InvalidTags = [SCF || {SCF,NIds}<-CF, SCF#sCF.tot_vl == length(NIds)],
?HYPERCUBE_CFTAGS -- InvalidTags
end,
case Flag of
[] ->
%io:format("***** Tag_Pool -- CF_Tags == []~n"),
Id_Pool -- PresentIds;
_ ->
[SU_Id|Id_Pool] -- PresentIds
end.
get_UniqueIds(Id_Pool,Elements_Requested)->
get_UniqueIds(Id_Pool,Elements_Requested,[]).
get_UniqueIds(_Id_Pool,0,IdAcc)->
IdAcc;
get_UniqueIds([],_Id_Index,[])->
exit("********ERROR:get_UniqueIds:: Id_Pool == IdAcc == []");
get_UniqueIds([],_Id_Index,IdAcc)->
IdAcc;
get_UniqueIds(Id_Pool,Id_Index,IdAcc)->
UniqueId = lists:nth(random:uniform(length(Id_Pool)),Id_Pool),
% io:format("UniqueId:~p~n",[UniqueId]),
get_UniqueIds(Id_Pool--[UniqueId],Id_Index-1,[UniqueId|IdAcc]).
add_Neuron(DX_Id,Cx_Id,TargetLayer)-> %TODO: Crash/End if in last layer, feed forward, and Cx already fully connected to.
%Extract cointained Neuro Ids from SC and Pattern,
%Create new Neuron(s?) technome.
%Update Ids/I/O/RO for the Neuron and the Neurons/SubCore he is connected to.
%Cortex:{{Id,core},TotCFVL,CF:[{Id1,VL1},{Id2,VL2}...],TotCTVL,CT:[Id1,Id2...],Type,Version,Pattern,CIds,Mutagens}
%construct_NeuronTechnome(Technome,Cx_Id,N_Id,{N_TotIVL,N_I},{N_TotOVL,N_O})