Method to get trained genome + anti-regression management + some stuff to do (output neurons issue) + manage `mutThr` decreasing + ...

This commit is contained in:
xdrm-brackets 2016-10-28 13:03:08 +02:00
parent ece00a7688
commit 7aefda0bf0
11 changed files with 348 additions and 1882 deletions

View File

@ -4,12 +4,14 @@
use filemanager\core\FileManager;
// FIXME: Propagation of outputNeurons from NeuralNetwork to Genome
class Genome implements \Serializable{
/************************************************
**** Constants ****
************************************************/
const MIN = 0;
const MIN = -1e9;
const MAX = 1e9;
/************************************************
@ -295,10 +297,8 @@
// echo "result: ${neurons[$n]}\n\n";
}
if( $l == 1 )
$neurons[$n] /= $this->inputN;
else
$neurons[$n] /= $this->neurons;
// if( $l == 1 ) $neurons[$n] /= $this->inputN;
// else $neurons[$n] /= $this->neurons;
}
@ -309,6 +309,7 @@
/* [3] Callback the output layer's values
=========================================================*/
echo $this->outputN;
call_user_func($this->callback, $input, array_slice($neurons, -$this->outputN) );
}

View File

@ -24,6 +24,7 @@
/************************************************
**** LOCAL ATTRIBUTES ****
************************************************/
private $maxFit; // Maximum fitness of the previous generation
public $gnr; // Current generation index
public $gnm; // Current genome index
private $genome; // Current genome instance
@ -123,7 +124,6 @@
$this->callback = function(){}; // default value
}
/************************************************
**** Attributes Setters ****
************************************************/
@ -315,6 +315,27 @@
}
/* RETURNS THE TRAINED GENOME IF IT EXISTS
*
* @return trained<Genome> The trained genome
*
*/
public function getTrainedGenome(){
/* (1) Read file */
$serialized = FileManager::readline($this->storage['ln']['filename'], 0);
/* (2) If no data */
if( is_null($serialized) )
throw new \Exception('No trained genome found.');
/* (3) Unserializes it */
$genome = new Genome(2, 2, 2, 2);
$genome->unserialize($serialized);
/* (4) Return genome */
return $genome;
}
/* INITIALIZES THE LEARNING ROUTINE
*
* @callback<Function> Callback function to display current state
@ -360,15 +381,20 @@
/* (1) Initializes data & storage */
$this->gnr = 0;
$this->gnm = 0;
$this->maxFit = null;
FileManager::write($this->storage['gn']['filename'], '');
FileManager::write($this->storage['ft']['filename'], '');
/* (2) Fetch learnt best genomes */
/* (2.1) Fetch learnt best genomes */
$loadedGenomes = [
FileManager::readline($this->storage['ln']['filename'], 0),
FileManager::readline($this->storage['ln']['filename'], 1)
];
/* (2.2) Checks content */
if( is_null($loadedGenomes[0]) || is_null($loadedGenomes[1]) )
return $this->initLearningRoutine($callback);
/* (3) Unserializes them */
$father = new Genome(2, 2, 2, 2);
$father->unserialize($loadedGenomes[0]);
@ -398,7 +424,6 @@
}
/* RETURNS THE CURRENT GENOME
*
* @return genome<Genome> Returns the current genome
@ -458,21 +483,47 @@
$ftRead = FileManager::read($this->storage['ft']['filename']);
$fitnesses = explode("\n", trim($ftRead) );
/* (3) Extract @mother & @father indexes */
/* (3) Checks if theres a fitness evolution */
$fitnessEvolution = is_null($this->maxFit) || max($fitnesses) > $this->maxFit;
/* (3.1) If evolution -> choose best + cross-over ... */
if( $fitnessEvolution ){
// {1} Extract @mother & @father indexes //
$iBest = $this->bestFitnesses($fitnesses);
/* (4) Extract best 2 genomes */
// {2} Extract best 2 genomes //
$sFather = FileManager::readline($this->storage['gn']['filename'], $iBest[0]);
$sMother = FileManager::readline($this->storage['gn']['filename'], $iBest[1]);
/* (5) Unserializes them */
// {3} Unserializes them //
$father = new Genome(2, 2, 2, 2);
$father->unserialize($sFather);
$mother = new Genome(2, 2, 2, 2);
$mother->unserialize($sMother);
/* (6) Create new generation */
$this->maxFit = max($fitnesses);
/* (3.2) If regression -> renew generation */
}else{
// {1} Extract 2 parents of the current generation //
$sFather = FileManager::readline($this->storage['gn']['filename'], 0);
$sMother = FileManager::readline($this->storage['gn']['filename'], 1);
// {3} Unserializes them //
$father = new Genome(2, 2, 2, 2);
$father->unserialize($sFather);
$mother = new Genome(2, 2, 2, 2);
$mother->unserialize($sMother);
/* (4) Stay on the current genertion */
$this->gnr--;
}
/* (7) Create new generation */
FileManager::write($this->storage['gn']['filename'], '');
FileManager::write($this->storage['ft']['filename'], '');
@ -495,14 +546,13 @@
}
/* (3) If end of process
---------------------------------------------------------*/
}else{
/* (1) Get the 2 best genomes */
$best = FileManager::readline($this->storage['ln']['filename'], 0);
$best.= FileManager::readline($this->storage['ln']['filename'], 1);
$best = FileManager::readline($this->storage['gn']['filename'], 0);
$best.= FileManager::readline($this->storage['gn']['filename'], 1);
/* (2) Stores data to learnt data */
FileManager::write($this->storage['ln']['filename'], $best);
@ -514,6 +564,8 @@
}
}
// TODO: Manage @mutThr decreasing to be more precise
/************************************************
**** Utility ****
************************************************/

View File

View File

@ -1,16 +1,16 @@
0,0,0;0,0
0,0,1;0,1
0,1,0;0,1
0,1,1;0,1
1,0,0;0,0
1,0,1;0,1
1,1,0;1,1
1,1,1;1,1
0,0,0;0,0
0,0,2;0,2
0,2,0;0,2
0,2,2;0,2
2,0,0;0,0
2,0,2;0,2
2,2,0;2,2
2,2,2;2,2
0,0,0;0
0,0,1;-1
0,1,0;1
0,1,1;0
1,0,0;1
1,0,1;0
1,1,0;2
1,1,1;1
0,0,0;0
0,0,2;-2
0,2,0;2
0,2,2;0
2,0,0;2
2,0,2;0
2,2,0;4
2,2,2;2

View File

@ -1,776 +1,67 @@
1503966.712809
1068720.8807691
50581.322614801
19445.885768699
498925.26240159
12716.437639977
3268.120241201
1209926.3483244
293721.40027881
811922.12715258
7382.3048353008
8498.9048423332
211248.2516164
529819.68198291
1272500.2181694
56509.07440623
31619.192408321
7386.8969551021
15901.371871418
42854.083749301
4449.7540659706
205842.62075801
118750.70761132
14827.64072793
436305.91575588
877054.353106
4406.0222916712
8991.6059517343
924652.54689666
416342.22263638
4594.5805227541
175848.67198755
20402.62946109
25839.882676464
29882.089777114
782067.88500628
1205.295551346
3644.1021100372
1144341.671424
592048.73135422
3259.8818667008
847174.1233754
3427.1909207399
467353.41917026
13117.344794609
8805.6021511117
219089.8290877
540196.88383416
5931.9253462153
30531.573763012
12156.69599649
1966.6380025538
654139.16925673
1317.9289427484
5397.4916579413
40011.423030561
411083.53309767
985565.67546946
1992.5255195875
867.27970093944
6686.2334143239
769.81344600742
4589.6651097979
14200.001583541
5213.7285901261
4272.8493406355
17320.744771089
6480.6805507954
2216.4197814729
635193.19989261
9964.9427674006
18659.845994967
7805.0614938383
225341.89299026
22252.57803151
2343.138151408
1698.7151059278
6610.4351271265
1178.1285977341
287392.60506109
55100.432793374
365285.70796836
1552.1394496442
711124.67580212
571512.29252394
77056.696312518
4126.6154762776
11068.168957456
8258.4059419301
15235.508980051
109644.96412441
113681.11666982
32221.926198074
578409.88916383
15556.539761091
3693.2463516294
4763.7886005265
17476.856477627
80886.326324518
941785.73146935
928079.99379673
494082.97344481
78522.415665071
185285.151485
2865.4769135985
1447068.0488982
5208.8567939543
771258.36827978
715971.15425806
13002.029607406
4374.22509377
17812.052464559
146152.16104364
1730.6252343779
750784.02101208
2901.7001119023
12773.00053369
611345.64076247
1546140.3348833
529407.4924862
10492.477363913
8923.7978220428
3816.1147972713
9116.3473860447
250340.80994845
23472.754133398
299777.60683267
16677.744780983
282166.34763632
331166.08621239
6392.827198579
4701.423616019
2018.8072431064
5390.8599539664
420434.62168811
2486.6313798878
327216.45012647
669476.70946247
18483.09846327
10814.35113459
939569.50458092
512481.54271008
14033.057544609
636005.6364225
1838.8965163121
650640.2877627
408415.58627249
24280.354273744
871.43237804393
16371.225084668
8966.820283932
2170.010901333
35334.088479739
938843.06168162
3304.4192927084
424645.21844662
363168.4065434
28307.488948292
498555.96320995
6653.4391222893
1282791.7293468
1085356.2703445
5111.0182440839
379316.60797885
15802.853983424
3346.6779329275
624129.61424556
21451.934075871
539978.00615637
7094.0702663757
125992.39738698
7747.7435351751
27248.011066495
72988.844502609
1027062.0344122
283080.85976041
2288.0901416122
2046.3792382041
4746.1216211833
455101.16553999
13159.469900165
293846.8896391
2354.9703005992
102062.59792206
12510.465733895
5515.9363384851
4135.2690670142
845924.0587252
813380.93249442
17737.02397429
148793.00412003
1044730.3043581
22830.189578394
846131.51252983
506916.33859883
4931.6880086071
1087389.6584224
133847.14860874
15173.726054336
569514.36031841
7861.6179460264
697760.69044369
368715.43908517
27900.067597739
247608.24485217
319379.33513821
22600.775359266
1419.8878555667
5686.7403381151
4085.8279123569
6157.5371871117
15922.494789485
1054762.4368377
193677.08609931
3533.428476413
539789.92707718
144047.66740182
437307.93812667
988719.877338
191823.58062154
9599.3903376852
27258.73122427
413045.87376624
4570.772067113
4699.4267011685
3942.0816075945
16599.817569962
726901.09899944
2703.7935889816
7700.1464480644
7163.1509495074
53459.984433767
485997.63179336
5001.15138465
9537.9192437909
20475.023635115
5645.1662665886
877677.01863477
9254.4364692907
7320.8207356157
6730.0381316782
688441.2048497
1042297.6159643
4880.1846931851
97968.948305701
347116.31748025
8713.5105118046
2103.3766857541
247495.92074737
292683.71847887
8471.5723850546
575986.252248
595795.69184191
13029.311582368
35450.60781314
26269.061431195
43103.763772897
281118.84395864
891616.56329339
13851.497006851
715569.06299981
1025052.7617297
2729.5439765657
968770.96410948
304795.42828106
316872.18159847
54523.280836136
4512.6755380592
722023.33255986
3276.5622436284
1121044.8916117
7396.6487694881
13733.05036298
33725.353512075
2003.3383729523
3466.8375178726
310243.52566571
2497.8985355753
555010.48559078
2076681.6166246
7994.7540594148
1041.7677049762
393172.89067839
665174.2131319
61914.235507567
574341.81693077
1298359.4366901
4687.2643525259
1253750.7446134
5945.5115154143
455656.75131794
5848.1189642498
24803.021487942
321979.2521915
458110.70232019
8682.7313809211
4697.9797707217
4734.8242082246
3058.6464211022
588101.59245838
12090.29611647
1839429.978746
14745.77982322
5704.7902115767
4480.8654233849
337601.29598701
24535.217286363
3451.9120933373
4296.8011045483
2202.0614658534
3959.2449567352
149984.68743923
766364.53749813
111857.92478814
4635.7856161537
28768.629169228
1579978.4588031
852621.24838613
600411.91520388
9270.350345121
332207.30101615
721803.48017372
1091.0839516598
21135.483527258
239513.84000304
340588.83484456
543537.64492022
5864.1205952681
970024.34894979
48610.26389935
2028.0852090862
4265.8034827129
2117.2136302446
2215.9515591889
2266.4020409861
13574.048229248
12636.03594223
3275.6266191994
8822.2047927369
7223.1646468667
6189.2694828541
601367.01012069
34187.817386726
4143.0834376163
3090.4403695064
278906.53435558
1488.6266070544
373707.18837323
10120.053363581
438572.19169775
6688.005705143
392866.57120695
69569.828473147
895921.52677283
2330.1918927169
194556.59706762
1772.3699313178
2110.828335661
1344996.3406127
24902.92432844
965.75865266983
3892.522581841
663562.74631362
47123.284084521
458444.26085829
10464.970970904
7605.7300788256
33722.931169907
170168.18803881
780510.32324015
1232356.3937976
403238.86163571
216383.92366236
586708.83323126
36257.67576296
478979.00525262
1485184.0571381
2271.4637706129
31436.093216786
619733.6438099
25214.135622113
508258.17817266
2808.6484016169
629292.3645335
797934.08041696
302890.81080187
1431128.1964969
30597.132309156
38972.434622337
1900198.7446892
236116.8286057
350983.06958131
5581.3921869579
3290.1690182801
1771335.7443773
924300.4149602
320706.85094681
616009.62725812
14886.487177182
21422.841051867
877557.23849379
7190.3583920668
907270.16650983
25829.181433737
36189.407419974
7635.2014877574
27511.888843197
10098.834644651
8130.3338936102
11424.637633948
462194.46170405
47714.988757952
1699279.4180955
2383.3772101894
54061.143253295
8440.1972008481
77506.999454607
11890.449857925
356013.41745075
1887666.5345828
15249.285310834
1068221.5104035
162320.3464632
5275.2771699731
343163.46330414
2892345.8855379
3363.1795758472
4312.5691608604
18721.216137266
57094.824908171
1188388.3876052
6165.9331961425
7781.1901121063
106218.87722447
33239.65654355
291764.04852727
704125.00357649
167855.09506206
474245.74834062
16372.238712543
1577449.5052397
65545.799470059
10971.468446015
50385.762576
1317581.8288717
297743.43269364
2429.2296518871
3444.5860612113
408873.78149809
26015.384424115
325928.46822069
3544.2855702669
372684.82777085
185860.42157747
997576.19502453
386204.58584421
28057.349902182
32827.059879157
2365.5943281876
3290.4157967042
1606512.9817915
24911.987668943
19123.237289734
5052.0977052453
24901.56617041
1498653.2867402
8722.3269811827
28360.660434395
950915.62222054
303963.20612752
4213.3814160244
1030889.0167807
17665.108590993
1671434.2075738
288661.53199153
179522.62860504
185395.38885295
2373.9451746204
539225.02140577
17476.273395486
707083.81297551
620102.18603384
1293729.1011519
134360.90759751
3699.8924389579
15665.884165672
5632.2608623459
5469.7233014882
95337.6228933
495600.49979176
4975.9066969693
642972.03894579
2942.4594523288
213535.16185625
3691.0811171585
6300.1364191143
15160.988550918
5082.219018404
17300.874069765
704178.51806288
2409.0494233664
4186.1748171758
24066.221580411
400039.03453399
478441.15533215
3655.6602834098
3069796.2791075
3020.4738337992
2308.1465231225
596443.57483204
2069.6905273202
2921.6981296277
25561.349333171
288069.73243991
140711.66289369
76028.702690138
2884430.9235928
15958.745845557
5733.8433297922
1741.0837293539
4683.4101589826
361009.28138141
1306460.1616392
14311.528413148
7827.6905340624
173929.98577419
1137.3208649461
29002.11304406
570123.17392581
80339.163762124
16545.844298484
436154.9292826
363662.88960342
530587.80198697
6806.7955762443
487271.16671032
28929.454949272
148900.60134611
177960.29840185
8299.048397482
710702.23086571
975870.95860661
72474.826355546
268330.24651832
777149.19117014
2330.4600010596
52260.948570803
224347.58198539
35462.590155391
980439.85022438
868806.12120577
720891.74508266
2427.1303744565
24745.506610283
628119.09071711
699778.15447366
4397.1315370018
540861.85665726
80310.241185524
238138.43700871
21086.311190516
9937.2918708988
6177.6448330148
222425.95212378
1001680.7447798
4403.7810747023
424914.6987222
8630.1910665528
5987.9715144471
1579.659014926
308868.91764228
5885.4495796232
37584.669942931
189855.78550108
59660.913132969
7658.949165027
2176666.0086483
7584.0045616857
496507.27920668
2153.6340070904
914.47399962715
446103.379179
1259.7695644977
16179.18492982
565692.37115034
2399.8466153963
2975.5441391014
2864901.2640867
2335.6157956852
16923.206083786
446351.55740281
345695.16507315
1012084.0082879
215886.09653843
204292.66557792
8703.0679734908
3208.2750488513
141844.52804578
993974.74579892
286605.63513784
10620.762302015
347118.99794518
12544.597570034
8389.2147474357
688719.09858913
123616.39930283
4318.8336399637
2560.6545817115
3778.6662253525
48175.950400609
2939.9138801758
311145.83517287
6192.252781238
7273.6252563826
702004.05581632
2384.0856531973
1040719.7929284
59826.363245486
832.82373364446
956336.66751797
247450.98801169
12664.720561413
7598.3865345005
1731.2559529421
606631.0089968
6885.5302224377
919653.98522501
1126496.0204504
587741.25215872
14767.588385056
434671.82455158
4497.7099422751
609300.72268622
631141.69397824
13694.687828079
127303.45335266
12212.556265912
1081.9467620123
3050.8435282523
751643.48288648
256193.71726099
38948.417355414
41481.18992655
18814.796489985
2286.2418700544
7243.484571757
570741.16212699
3078.6305257295
531802.82582759
8366.2991239218
963855.14678384
5878.0950973202
749795.84796837
10138.123394173
467136.78253303
590098.08197856
980482.74271637
34308.736260989
259300.00626816
57945.883110664
12667.499381634
4935.082149346
2150.1705577064
788536.02370618
4300.5808633596
74285.872358851
409741.35410456
3193.9662763306
30623.933618266
5089.7091358381
4168.1352509163
3428.7506150937
3651.1541263728
378218.86348293
460968.61368109
605156.10527424
23046.382939329
1727.5614931569
4119.2754639703
213213.83818465
32740.833633038
24519.891265612
2764.0266015432
3520.4908417154
626596.20396703
2983.812238522
1041.8619238928
6727.0693381596
17122.628720702
3566.2647754844
401686.03286279
3491.0875278019
28377.943183738
194144.58176966
714593.49882494
29972.404507814
18416.785776748
589921.1763952
509243.83194714
5188.3167507381
8367.877746735
3460.6412591116
401328.67758027
91792.305602593
353894.62182512
7518.5994297769
1649687.5393043
61806.161271895
1796.5368954566
3729.1619837528
7706.4471218507
237859.66546805
8157.909553844
4244.5019744555
2603.5467864558
168677.27803387
753311.45631428
12568.571178854
1657.9964486281
16471.753980569
273437.44828237
3778.9007197032
37022.412768297
301256.26634481
3919.5331619325
12441.214993626
4620.4917934806
140042.26424163
19885.808110774
901668.08698742
721005.18631527
405399.6975612
501192.46166846
170585.72247322
1032998.3769422
500565.57176182
1091077.0952211
1046675.3262274
188697.07585277
1476.5779017466
513639.73069778
476846.33350997
650119.52917025
3634853.7054124
880932.34514119
118649.93809137
369305.54992343
607069.83847438
87309.298120936
41990.403555945
44380.833932577
505734.44176155
2188.1088980416
7942.9407678976
753929.06981816
4693.4459964267
25611.124714649
3003.9600783587
34149.912741398
133116.94641351
7051.402042614
1414.7803108893
12171.691564839
1429.3815816289
166988.31818112
837148.44890922
2891.7608151147
403512.21750897
961843.32633155
7132.1522687055
751875.3768063
13125.024323867
614777.64236488
2910.2742456494
508798.92404313
227311.21217478
-0.26100705460266
-0.26332370388955
-0.25807040325881
-0.22872658728855
-0.2525469468111
-0.24537126426618
-0.24897551014889
-0.22877441599361
-0.2338669679939
-0.26291432434374
-0.24253701374514
-0.24379596365793
-0.23996414424896
-0.22123873931464
-0.27001852389648
-0.25111384165343
-0.24175301813362
-0.26529997159751
-0.23852883258585
-0.22210609410012
-0.22823595168029
-0.2300380623709
-0.23896167033627
-0.26509436775629
-0.26328307763079
-0.26247886873884
-0.21495589397895
-0.23967246175787
-0.23839653632849
-0.25995519705069
-0.24627053693291
-0.28207214320164
-0.22499059687071
-0.23338432454985
-0.21582926394003
-0.24272426226148
-0.22659467028497
-0.26097887720452
-0.2355457341177
-0.24979157249363
-0.23307403271554
-0.24097631233955
-0.25950346473092
-0.24422640236537
-0.21881174427133
-0.25138914226329
-0.27032074467422
-0.22096845560221
-0.27190881634849
-0.220023331219
-0.29367165210062
-0.26982749475642
-0.23299693741808
-0.26749006305528
-0.23971615242697
-0.24511407028025
-0.21672379113928
-0.27777722826163
-0.2534721449125
-0.2484361544963
-0.21306825458823
-0.24421454714016
-0.26239812741934
-0.27751629379262
-0.27253134534381
-0.23566019695354
-0.24144314658119

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
3,3,3,3;-0.673048732,-0.412463881,-0.736532526,0.829005319,-0.813510365,-0.502031047,0.950266852,0.818343199,0.03824395,0.219597033,-0.339935295,-0.181847521,-0.820436519,-0.762547916,0.165912707,0.799397635,-0.883427944,0.304251916,-0.414343129,0.25450569,-0.472308773,-0.804309556,0.137957493,0.576775783,-0.004366962,-0.494472245,-0.518802319,-0.819242309,0.447490839,0.506983895,0.452629618,-0.248047378,0.468150892,0.291926346,0.947744258,0.536874463,-0.73260191,0.834541621,0.177465943
3,3,3,3;-0.673048732,-0.412463881,-0.736532526,0.829005319,-0.813510365,-0.502031047,0.950266852,0.818343199,0.03824395,0.219597033,-0.339935295,-0.181847521,-0.820436519,-0.762547916,0.165912707,0.799397635,-0.883427944,0.304251916,-0.414343129,0.25450569,-0.472308773,-0.804309556,0.137957493,0.576775783,-0.004366962,-0.494472245,-0.518802319,-0.819242309,0.447490839,0.506983895,0.452629618,-0.248047378,0.468150892,0.291926346,0.947744258,0.536874463,-0.73260191,0.834541621,0.177465943

View File

@ -0,0 +1,2 @@
3,3,3,3;-0.673048732,-0.412463881,-0.736532526,0.829005319,-0.813510365,-0.502031047,0.950266852,0.818343199,0.03824395,0.219597033,-0.339935295,-0.181847521,-0.820436519,-0.762547916,0.165912707,0.799397635,-0.883427944,0.304251916,-0.414343129,0.25450569,-0.472308773,-0.804309556,0.137957493,0.576775783,-0.004366962,-0.494472245,-0.518802319,-0.819242309,0.447490839,0.506983895,0.452629618,-0.248047378,0.468150892,0.291926346,0.947744258,0.536874463,-0.73260191,0.834541621,0.177465943
3,3,3,3;-0.673048732,-0.412463881,-0.736532526,0.829005319,-0.813510365,-0.502031047,0.950266852,0.818343199,0.03824395,0.219597033,-0.339935295,-0.181847521,-0.820436519,-0.762547916,0.165912707,0.799397635,-0.883427944,0.304251916,-0.414343129,0.25450569,-0.472308773,-0.804309556,0.137957493,0.576775783,-0.004366962,-0.494472245,-0.518802319,-0.819242309,0.447490839,0.506983895,0.452629618,-0.248047378,0.468150892,0.291926346,0.947744258,0.536874463,-0.73260191,0.834541621,0.177465943

View File

@ -1 +1 @@
{"maxGnr":50,"maxGnm":1000,"mutThr":0.5,"fitEnd":1,"numHid":4,"numNeu":4,"inpNeu":3,"outNeu":2,"storage":{"nn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.nn","exists":false},"ex":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ex","exists":false},"gn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.gn","exists":false},"ft":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ft","exists":false},"ln":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ln","exists":false}}}
{"maxGnr":50,"maxGnm":100,"mutThr":0.005,"fitEnd":1,"numHid":3,"numNeu":3,"inpNeu":3,"outNeu":1,"storage":{"nn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.nn","exists":false},"ex":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ex","exists":false},"gn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.gn","exists":false},"ft":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ft","exists":false},"ln":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ln","exists":false}}}

View File

@ -7,10 +7,10 @@
use \filemanager\core\FileManager;
function behaviour($abc){
return [($abc[0] & $abc[1]), $abc[1] | $abc[2]];
return [($abc[0] + $abc[1] - $abc[2])];
}
if( true && 'test_creating_dataset' ){
if( false && 'learning_process' ){
$part = 1;
@ -29,13 +29,13 @@
=========================================================*/
}catch(\Exception $e){
$nn = NeuralNetwork::create(50, 1000);
$nn = NeuralNetwork::create(50, 100);
$nn->setHiddenLayersCount(4);
$nn->setHiddenLayerNeuronsCount(4);
$nn->setHiddenLayersCount(3);
$nn->setHiddenLayerNeuronsCount(3);
$nn->setInputLayerCount(3);
$nn->setOutputLayerCount(2);
$nn->setMutationThreshold(0.5);
$nn->setOutputLayerCount(1);
$nn->setMutationThreshold(0.3);
echo "$part. NeuralNetwork configured\n"; $part++;
@ -69,10 +69,7 @@
$max_fit = 0;
$nn->loadLearningRoutine(function($input, $output){
global $fitness;
$diff = [ abs($output[0] - behaviour($input)[0]), abs($output[1] - behaviour($input)[1]) ];
if( $diff[0] > 0 ) $fitness += 1 / $diff[0];
if( $diff[1] > 0 ) $fitness += 1 / $diff[1];
$fitness -= abs($output[0] - behaviour($input)[0]);
});
echo "$part. Learning routine initialized.\n"; $part++;
@ -80,18 +77,23 @@
/* [3] Learning through generations and genomes
=========================================================*/
/* (1) For each generation */
for( $gnr = 0 ; $gnr < 50 ; $gnr++ ){
$last_gnr = -1;
while( true ){
$max_fit = 0;
if( $nn->gnr > $last_gnr)
$start = microtime(true);
$last_gnr = $nn->gnr;
$max_fit = -1e9;
/* (2) For each genome */
for( $gnm = 0 ; $gnm < 1000 ; $gnm++ ){
while( true ){
$fitness = 0;
/* (2.1) Get current genome */
$g = $nn->getGenome();
echo "\r[x] genome ".($nn->gnm+1)."/1000 on generation ".($nn->gnr+1)."/50 - max fitness: $max_fit ";
echo "\r[x] gnm ".($nn->gnm+1)."/100 on gnr ".($nn->gnr+1)."/50 - max_fit: $max_fit ";
/* (2.2) Train genome with random samples */
for( $r = 0 ; $r < 100 ; $r++ )
@ -101,50 +103,66 @@
if( $fitness > $max_fit ) $max_fit = $fitness;
$g->setFitness($fitness);
if( $nn->gnm >= 100-1 )
break;
$nn->nextGenome();
}
$nn->nextGenome();
// If generation evolution, notify
if( $nn->gnr > $last_gnr)
echo "\n\t".((microtime(true)-$start))."s\n";
if( $nn->gnr == 50-1 )
break;
}
}
if( true && 'guessing_process' ){
$part = 1;
echo "Welcome to neural-network.php\n";
echo "-----------------------------\n\n";
if( false && 'load_neural_network' ){
/* [1] Trying to load neural network
=========================================================*/
try{
$nn = NeuralNetwork::load('test/test1');
echo "$part. NeuralNetwork loaded from 'test/test1'\n"; $part++;
/* [2] Else, creates it
=========================================================*/
}catch(\Exception $e){
echo "You must create/train your neural network before using it.\n";
exit();
}
if( false && 'test_genomes' ){
/* (1) Basic Creation */
$a = new Genome(2, 3, 3, 2); // 2 layers of 3 neurons each -> randomly filled
echo "A : ".$a->serialize()."\n";
/* [2] Fetch trained genome
=========================================================*/
$genome = $nn->getTrainedGenome();
$genome->setCallback(function($in, $out){
echo "callback input: ".implode(',', $in)."\n";
echo "callback output: ".round($out[0])."\n";
echo "callback result: ".implode(',', behaviour($in))."\n";
});
/* (2) Inheritance */
$b = new Genome($a); // Clone of @aecho "A neurons\n";
echo "cloning A to B\n";
echo "B : ".$b->serialize()."\n";
$genome->train([rand(0,10), rand(0,10), rand(0,10)]);
/* (3) Section Title */
$b->mutation(0.3); // @b has now mutated with a threshold of 30%
echo "mutate B\n";
echo "B : ".$b->serialize()."\n";
/* (4) Cross-over (father+mother) */
$c = new Genome($a, $b); // @c is a randomly-done mix of @a and @b
echo "crossover : A+B -> C\n";
echo "C : ".$c->serialize()."\n";
}
if( false ){
$g = new Genome(2, 3, 3, 2);