/
TrainSelector.java
1015 lines (840 loc) · 26.9 KB
/
TrainSelector.java
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.livingforsom.com/license.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package lfsom.experiment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import lfsom.data.LFSData;
import lfsom.data.LFSDataCSVWriter;
import lfsom.layers.LFSGrowingLayer;
import lfsom.layers.LFSUnit;
import lfsom.layers.quality.LFSQuantizationError;
import lfsom.models.LFSGrowingSOM;
import lfsom.properties.LFSExpProps;
import lfsom.properties.LFSSOMProperties;
import lfsom.util.LFSPCA;
/**
*
* This class is called from the client-side: The client gives the params to
* train the SOMs. This class combines the values of these params and organizes
* the trainings.
*
* @author Vicente Buendia
* @version $Id: $
*/
public class TrainSelector {
/**
* To know if it's still executing
*/
private boolean Calculando = false;
/**
* Version to show in client-side
*/
private String versionprog = "v1.4.3";
/**
* Progress
*/
private int Progreso = 0;
/**
* The best indicators to select best SOM to be saved.
*/
private double MedidaKaski = Double.POSITIVE_INFINITY;
private double MedidaTopo = Double.POSITIVE_INFINITY;
private double MedidaQuan = Double.POSITIVE_INFINITY;
/**
* Best SOM of each best indicator
*/
private LFSGrowingSOM mapaMejorKaski;
private LFSGrowingSOM mapaMejorQuan;
private LFSGrowingSOM mapaMejorTopo;
/**
* Needed to multiprocess
*/
private CountDownLatch doneSignal;
private final Semaphore sema = new Semaphore(2);
/**
* Total number of trainings, depending on parameters
*/
private int numIter = 0;
/**
* The results of each training (input parameters and resulting indicators)
* will be saved in a csv file.
*
* datosResul contains the attribute names to save.
* matrixResults[indiceResul] contains the current values of each training.
*
*/
private LFSData datosResul = null;
private double[][] matrixResults;
private int indiceResul = 0;
public static String[] labelsTrain = new String[] { "Err.Topo", "Err.Quan",
"Kaski and Lagus", "LearnRate", "0=online/1=batch", "Sigma",
"Init (10=Rand 20=Interval 30=Vector 40=PCA)",
"Neigh. Func. (10=Gauss 20=Bubble 30=Cut Gauss)", "Neigh. Width",
"Growing" };
/**
* The different initializations are precalculated and saved, so they won't
* be needed to be calculated in each training.
*/
private LFSUnit[][] unitsPCA, unitsVector, unitsInterval;
private LFSUnit[][] unitsPCABatch, unitsVectorBatch, unitsIntervalBatch;
/**
* Name of the experiment
*/
private String expName = "SOM";
/**
* If it is cancelled
*/
private boolean cancelado = false;
/**
* Current training
*/
private long iteact = 0;
/**
* Minimum number of data to generate a SOM. Required for hierarchical SOM.
*/
private int minDatosExp = 300;
/**
* Current and max deep allowed
*/
private int prof = 1;
private int maxProf = 6;
public TrainSelector() {
}
private TrainSelector(int profun) {
prof = profun;
}
/**
* Depending on parameters, calculates heuristic dimensions
*
* @param data
* @param x
* @param y
* @param isHier
* @param isBatch
* @param nWidth
* @return
*/
private int[] calculaDimen(LFSData data, int x, int y, boolean isHier,
boolean isBatch, int nWidth) {
int[] dimen = new int[2];
if (x == 0 || y == 0) {
if (isHier) {
dimen[1] = 2;
dimen[0] = 2;
} else {
double numceldas = Math.pow(data.getData().length, 0.54321);
double ratio = Math.sqrt(data.getPCA().getFirstAxisIndex()
/ data.getPCA().getSecondAxisIndex());
dimen[1] = (int) Math.min(numceldas,
Math.round(Math.sqrt(numceldas / ratio)));
dimen[0] = (int) (numceldas / dimen[1]);
}
} else {
dimen[1] = y;
dimen[0] = x;
}
if (isBatch && dimen[1] < 2 * nWidth) {
dimen[1] = 2 * nWidth;
}
if (isBatch && dimen[0] < 2 * nWidth) {
dimen[0] = 2 * nWidth;
}
return dimen;
}
/**
* Precalc of Interval and Vector initializatons
*
* @param data
* @param xSize
* @param ySize
*/
private void calculaInitLayers(LFSData data, int xSize, int ySize) {
Random rand = new Random();
unitsInterval = new LFSUnit[xSize][ySize];
unitsVector = new LFSUnit[xSize][ySize];
for (int j = 0; j < ySize; j++) {
for (int i = 0; i < xSize; i++) {
unitsInterval[i][j] = new LFSUnit(data, i, j, data.dim(), rand,
true, LFSUnit.INIT_INTERVAL_INTERPOLATE);
unitsVector[i][j] = new LFSUnit(data, i, j, data.dim(), rand,
true, LFSUnit.INIT_VECTOR);
}
}
}
/**
* Precalc of initializations, for batch mode (dim can be different)
*
* @param data
* @param xSize
* @param ySize
*/
private void calculaInitLayersBatch(LFSData data, int xSize, int ySize) {
Random rand = new Random();
unitsIntervalBatch = new LFSUnit[xSize][ySize];
unitsVectorBatch = new LFSUnit[xSize][ySize];
for (int j = 0; j < ySize; j++) {
for (int i = 0; i < xSize; i++) {
unitsIntervalBatch[i][j] = new LFSUnit(data, i, j, data.dim(),
rand, true, LFSUnit.INIT_INTERVAL_INTERPOLATE);
unitsVectorBatch[i][j] = new LFSUnit(data, i, j, data.dim(),
rand, true, LFSUnit.INIT_VECTOR);
}
}
}
/**
* PCA initialization precalculation
*
* @param data
* @param xSize
* @param ySize
* @return
*/
private LFSUnit[][] calculaUnitsPCA(LFSData data, int xSize, int ySize) {
double[][] dataArray = data.getData();
double[][] projectedDataArray = new double[data.numVectors()][2];
int dim = dataArray[0].length;
LFSUnit[][] uPCA = new LFSUnit[xSize][ySize];
//
// project the data points
//
LFSPCA pca = data.getPCA();
for (int i = 0; i < data.numVectors(); i++) {
float xProj = 0.f;
for (int j = 0; j < dim; j++) {
xProj += dataArray[i][j] * pca.U[pca.getFirstAxisIndex()][j];
}
projectedDataArray[i][0] = xProj;
float yProj = 0.f;
for (int j = 0; j < dim; j++) {
yProj += dataArray[i][j] * pca.U[pca.getSecondAxisIndex()][j];
}
projectedDataArray[i][1] = yProj;
}
// find minX,minY,maxX,maxY
double minX = Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
double maxX = Double.MIN_VALUE;
double maxY = Double.MIN_VALUE;
for (int i = 0; i < data.numVectors(); i++) {
if (projectedDataArray[i][0] < minX) {
minX = projectedDataArray[i][0];
}
if (projectedDataArray[i][1] < minY) {
minY = projectedDataArray[i][1];
}
if (projectedDataArray[i][0] > maxX) {
maxX = projectedDataArray[i][0];
}
if (projectedDataArray[i][1] > maxY) {
maxY = projectedDataArray[i][1];
}
}
double diffX = maxX - minX;
double diffY = maxY - minY;
double cellSizeX = diffX / xSize;
double cellSizeY = diffY / ySize;
for (int j = 0; j < ySize; j++) {
for (int i = 0; i < xSize; i++) {
// find the closes point in the data point cloud
int closestPointIndex = -1;
double closesPointDist = Double.MAX_VALUE;
for (int curPoint = 0; curPoint < data.numVectors(); curPoint++) {
double[] curCellCoords = new double[2];
curCellCoords[0] = i * cellSizeX + cellSizeX / 2;
curCellCoords[1] = j * cellSizeY + cellSizeY / 2;
double curPointDist = Math.sqrt(Math.pow(
projectedDataArray[curPoint][0] - curCellCoords[0],
2)
+ Math.pow(projectedDataArray[curPoint][1]
- curCellCoords[1], 2));
if (curPointDist < closesPointDist) {
closesPointDist = curPointDist;
closestPointIndex = curPoint;
}
}
double[] closesPointVec = new double[dim];
for (int l = 0; l < dim; l++) {
closesPointVec[l] = dataArray[closestPointIndex][l];
}
uPCA[i][j] = new LFSUnit(i, j, closesPointVec);
}
}
return uPCA;
}
/**
* Trains a SOM according to SOMProperties parameters If it's better than on
* of the bests, it will be saved.
*
* @param datos1
* @param propsMapa
* @param xmulti
*/
private void cuerpoTrain(LFSData datos1, LFSSOMProperties propsMapa,
double xmulti) {
try {
LFSSOMProperties props = propsMapa.copia();
if (!this.cancelado) {
LFSGrowingSOM mapaActivo2 = new LFSGrowingSOM(
props.getExpName());
if (!propsMapa.batchSom()) {
switch (props.getInitializationMode()) {
case LFSUnit.INIT_INTERVAL_INTERPOLATE:
mapaActivo2.initLayer(true, props, datos1,
unitsInterval);
break;
case LFSUnit.INIT_VECTOR:
mapaActivo2.initLayer(true, props, datos1, unitsVector);
break;
case LFSUnit.INIT_PCA:
mapaActivo2.initLayer(true, props, datos1, unitsPCA);
break;
default:
mapaActivo2.initLayer(true, props, datos1, null);
}
} else {
switch (props.getInitializationMode()) {
case LFSUnit.INIT_INTERVAL_INTERPOLATE:
mapaActivo2.initLayer(true, props, datos1,
unitsIntervalBatch);
break;
case LFSUnit.INIT_VECTOR:
mapaActivo2.initLayer(true, props, datos1,
unitsVectorBatch);
break;
case LFSUnit.INIT_PCA:
mapaActivo2.initLayer(true, props, datos1,
unitsPCABatch);
break;
default:
mapaActivo2.initLayer(true, props, datos1, null);
}
}
mapaActivo2.train(datos1, props);
// InputData subMuestra = datos1.getSubMuestra(3000);
mapaActivo2.getLayer().mapCompleteDataAfterTraining(datos1);
mapaActivo2.getLayer().calcQuality(datos1);
double errActualQuan = mapaActivo2.getLayer()
.getQualityMeasure("QError").getMapQuality("mqe");
double errActualTopo = mapaActivo2.getLayer()
.getQualityMeasure("TError").getMapQuality("TE_Map");
double errActualKaski = mapaActivo2.getLayer()
.getQualityMeasure("KError").getMapQuality("ID_Map");
try {
sema.acquire();
double[] arrRes = new double[] { errActualTopo,
errActualQuan, errActualKaski, props.learnrate(),
props.batchSom() ? 1 : 0, props.sigma(),
props.getInitializationMode(),
props.getNeighbourFunc(), props.pcNeighbourWidth(),
props.isGrowing() ? 1 : 0 };
matrixResults[indiceResul++] = arrRes;
if (MedidaKaski > errActualKaski
&& 2 * MedidaQuan > errActualQuan) {
MedidaKaski = errActualKaski;
mapaMejorKaski = mapaActivo2;
}
if (MedidaQuan > errActualQuan) {
MedidaQuan = errActualQuan;
mapaMejorQuan = mapaActivo2;
}
if (MedidaTopo > errActualTopo
&& 2 * MedidaQuan > errActualQuan) {
MedidaTopo = errActualTopo;
mapaMejorTopo = mapaActivo2;
}
} finally {
sema.release();
}
}
iteact++;
Progreso = (int) (iteact / xmulti);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Multiprocess caller
*
* @author vicente
*
*/
private class UpdaterThread implements Runnable {
private LFSData datos1;
private LFSSOMProperties props;
private double xmulti;
@Override
public void run() {
try {
System.out.println("PROPS: useBatch: " + props.batchSom()
+ " bucleLearnRate " + props.learnrate()
+ " NeighFunc " + props.getNeighbourFunc()
+ " Init Func " + props.getInitializationMode()
+ " Sigma " + props.sigma());
cuerpoTrain(datos1, props, xmulti);
} finally {
doneSignal.countDown();
}
}
private UpdaterThread(LFSData datos1, LFSSOMProperties props,
double xmulti) {
this.datos1 = datos1;
this.props = props;
this.xmulti = xmulti;
}
}
public long getIteAct() {
return iteact;
}
/**
* Call from client side to calculate
*
* @param exprops
* @throws Exception
*/
public void LanzaExperimento(LFSExpProps exprops) throws Exception {
LFSData datos1 = new LFSData(exprops.getFicheroEntrada());
LanzaExperimento(datos1, exprops);
}
/**
* Gets a sample and an object containing the properties of the experiment
* and generates desired SOMs.
*
* @param datos1
* @param exprops
*/
public void LanzaExperimento(LFSData datos1, LFSExpProps exprops) {
// Parameter loading
boolean isGCHSOM = exprops.isGCHSOM();
boolean isHier = exprops.isHier() && !exprops.isGCHSOM();
String criterioSu = "mqe";
boolean isGrowing = (exprops.isGrowing() || isGCHSOM)
&& !exprops.isHier();
int[] dimSOM = null;
if (isGrowing) { // Los growing y hier tienen size automatico
exprops.setWidthSOM(0);
exprops.setHeightSOM(0);
}
int widthSOM = exprops.getWidthSOM();
int heightSOM = exprops.getHeightSOM();
if (widthSOM == 0 || heightSOM == 0) {
dimSOM = calculaDimen(datos1, widthSOM, heightSOM, isHier, false, 0);
} else {
dimSOM = new int[] { exprops.getWidthSOM(), exprops.getHeightSOM() };
}
widthSOM = dimSOM[0];
heightSOM = dimSOM[1];
int lambda = exprops.getLambda();
int numRepe = exprops.getNumRepe();
int numCPUs = exprops.getNumCPUs();
double[] bucleLearnRate = exprops.getBucleLearnRate();
boolean[] bucleUseBatch = exprops.getBucleUseBatch();
float[] bucleSigma = exprops.getBucleSigma();
int[] bucleInitializationMode = exprops.getBucleInitializationMode();
int[] bucleNeighFunc = exprops.getBucleNeighFunc();
float[] buclePcNeighWidth = exprops.getBuclePcNeighWidth();
String nameExp = exprops.getExpName();
String dataPath = exprops.getDataPath();
String rootPath = exprops.getRootPath();
double sensiCluster = exprops.getSensiCluster();
boolean isSub = exprops.isSubred();
// String celdasSubnet = exprops.getStrSubredOrigen();
String fPadre = exprops.getFPadre();
// String fDatosPadre = exprops.getFDatosPadre();
try {
// Initializations
MedidaKaski = Double.POSITIVE_INFINITY;
MedidaTopo = Double.POSITIVE_INFINITY;
MedidaQuan = Double.POSITIVE_INFINITY;
Calculando = true;
cancelado = false;
Progreso = 0;
double tau = exprops.getTau();
double tau2 = exprops.getTau2();
long seed = 1;
int trainingCycles = exprops.getCycles();
int trainingIterations = 1;
String metric = null;
long startTime = System.currentTimeMillis();
int nThreads = numCPUs;
double qRef = Double.POSITIVE_INFINITY;
double qRef0 = 0;
if (isHier || isGCHSOM) {
// If it's hierarchical, calculates mqeRef from a 1 cell net
LFSSOMProperties props = new LFSSOMProperties(1, 1, seed,
trainingCycles, trainingIterations, 1, 1, tau, metric,
false, false, LFSUnit.INIT_RANDOM,
LFSGrowingLayer.NEIGH_BUBBLE, 1, nameExp, false, 0.0,
false, false, false, 1);
props.setDataPath(dataPath);
LFSGrowingSOM mapa1Celda = new LFSGrowingSOM(props.getExpName());
LFSUnit[][] units1Celda = new LFSUnit[1][1];
units1Celda[0][0] = new LFSUnit(0, 0, datos1.getMeanVector()
.toArray());
mapa1Celda.initLayer(true, props, datos1, units1Celda);
mapa1Celda.getLayer().mapCompleteDataAfterTraining(datos1);
mapa1Celda.getLayer()
.setQError(
new LFSQuantizationError(mapa1Celda.getLayer(),
datos1));
qRef0 = mapa1Celda.getLayer().getQualityMeasure("QError")
.getMapQuality(criterioSu);
if (exprops.getMqeIni() == -1) {
qRef = qRef0;
} else {
qRef = exprops.getMqeIni();
}
}
// Initialization precalcs
unitsPCA = calculaUnitsPCA(datos1, widthSOM, heightSOM);
calculaInitLayers(datos1, widthSOM, heightSOM);
// Calculate number of nets to train
int numItera = 0;
for (int element : bucleNeighFunc) {
if (element == LFSGrowingLayer.NEIGH_GAUSS
|| element == LFSGrowingLayer.NEIGH_MH) {
// GAUSS
// doesn't
// use
// neighwidth
numItera += numRepe * bucleLearnRate.length
* bucleUseBatch.length * bucleSigma.length
* bucleInitializationMode.length;
}
if (element == LFSGrowingLayer.NEIGH_BUBBLE) { // Bubble doesn't
// use sigma
numItera += numRepe * bucleLearnRate.length
* bucleUseBatch.length
* bucleInitializationMode.length
* buclePcNeighWidth.length;
}
if (element == LFSGrowingLayer.NEIGH_CUTGAUSS) {
numItera += numRepe * bucleLearnRate.length
* bucleUseBatch.length * bucleSigma.length
* bucleInitializationMode.length
* buclePcNeighWidth.length;
}
}
setNumIter(numItera);
double xmulti = getNumIter() / 100;
// Setup csv to save results of all trainings
datosResul = new LFSData(labelsTrain);
matrixResults = new double[getNumIter() + nThreads][labelsTrain.length];
// Start multiprocess
ExecutorService e = null;
if (nThreads > 1) {
e = Executors.newFixedThreadPool(nThreads);
doneSignal = new CountDownLatch(getNumIter());
}
// Bucle containing all parameter combinations
int nbSigma = 0;
int nbNeighWidth = 0;
for (boolean bBatch : bucleUseBatch) {
nbNeighWidth = 0;
for (float bNeighWidth : buclePcNeighWidth) {
int wSOM = widthSOM;
int hSOM = heightSOM;
if (bBatch) {
int maxSize = wSOM > hSOM ? wSOM : hSOM;
int neighWidth = (int) (maxSize * bNeighWidth);
int[] dimSOMBatch = calculaDimen(datos1, wSOM, hSOM,
isHier, true, neighWidth);
wSOM = dimSOMBatch[0];
hSOM = dimSOMBatch[1];
unitsPCABatch = calculaUnitsPCA(datos1, wSOM, hSOM);
calculaInitLayersBatch(datos1, wSOM, hSOM);
}
for (int bNeighFunc : bucleNeighFunc) {
for (double bLearnRate : bucleLearnRate) {
for (int bInitializationMode : bucleInitializationMode) {
boolean usePCA = bInitializationMode == LFSUnit.INIT_PCA;
nbSigma = 0;
for (float bSigma : bucleSigma) {
boolean ejecuta = true;
// Si es Bubble, solo se ejecuta para el
// primer Sigma
if (nbSigma > 0
&& bNeighFunc == LFSGrowingLayer.NEIGH_BUBBLE) {
ejecuta = false;
}
// Si es Gauss, solo se ejecuta una vez para
// un Neighwidth
if (nbNeighWidth > 0
&& (bNeighFunc == LFSGrowingLayer.NEIGH_MH || bNeighFunc == LFSGrowingLayer.NEIGH_GAUSS)) {
ejecuta = false;
}
if (ejecuta) {
LFSSOMProperties props = new LFSSOMProperties(
wSOM, hSOM, seed,
trainingCycles,
trainingIterations, bLearnRate,
bSigma, tau, metric, usePCA,
bBatch, bInitializationMode,
bNeighFunc, bNeighWidth,
nameExp, isGrowing, qRef,
isSub, isHier, isGCHSOM, lambda);
props.setDataPath(dataPath);
if (nThreads > 1) {
for (int i = 0; i < numRepe; i++) {
e.execute(new UpdaterThread(
datos1, props, xmulti));
}
} else {
for (int i = 0; i < numRepe; i++) {
cuerpoTrain(datos1, props,
xmulti);
}
}
}
nbSigma++;
}
}
}
}
nbNeighWidth++;
}
}
if (nThreads > 1) {
doneSignal.await();
e.shutdown();
}
// Save results
if (!this.cancelado) {
new File(dataPath).mkdirs();
String fiche = null;
int numClusters = isGCHSOM && !exprops.isSubred() ? 4 : -1;
mapaMejorTopo.getLayer().mapCompleteDataAfterTraining(datos1);
mapaMejorTopo.clusteriza(numClusters, nThreads, sensiCluster);
fiche = dataPath + "/topo.xml";
mapaMejorTopo.EscribeXML(fiche, datos1.getMaxValues(),
datos1.getMinValues());
mapaMejorTopo.escribeProps(fiche + "props");
mapaMejorQuan.getLayer().mapCompleteDataAfterTraining(datos1);
mapaMejorQuan.clusteriza(numClusters, nThreads, sensiCluster);
// String fich = getfTopo();
fiche = dataPath + "/quan.xml";
mapaMejorQuan.EscribeXML(fiche, datos1.getMaxValues(),
datos1.getMinValues());
mapaMejorQuan.escribeProps(fiche + "props");
mapaMejorKaski.getLayer().mapCompleteDataAfterTraining(datos1,
true);
mapaMejorKaski.clusteriza(numClusters, nThreads, sensiCluster);
// String fich = getfTopo();
fiche = dataPath + "/kaski.xml";
mapaMejorKaski.EscribeXML(fiche, datos1.getMaxValues(),
datos1.getMinValues());
mapaMejorKaski.escribeProps(fiche + "props");
exprops.clearNets();
exprops.setFPadre(fPadre);
// Best nets
exprops.addNet("Kaski - Lagus", "kaski.xml");
exprops.addNet("Quantization Err.", "quan.xml");
exprops.addNet("Topographic Err.", "topo.xml");
exprops.EscribeXML(dataPath + "/ExpProps.xml");
// Save the data used to train the net
String ficheroEntrada = exprops.getFicheroEntrada();
if (!ficheroEntrada.equals(dataPath + "/data.csv")
&& !ficheroEntrada.equals("noMatter")) {
copyFile(new File(ficheroEntrada), new File(dataPath
+ "/data.csv"));
}
datosResul.setMatrix(matrixResults);
LFSDataCSVWriter.writeAsCSV(datosResul, indiceResul, dataPath
+ "/results.csv");
// If it's hierarchical, generate new sons
if ((isHier || isGCHSOM) && prof < maxProf) {
if (isGCHSOM) {
lanza_experimento_clusters(tau2,
mapaMejorKaski.getLayer(), dataPath, rootPath,
datos1, qRef0);
} else if (isHier) {
lanza_experimento_units(tau2,
mapaMejorKaski.getLayer(), dataPath, rootPath,
datos1, qRef0);
}
}
}
Progreso = 100;
System.out.println("Tiempo "
+ java.lang.String.valueOf(System.currentTimeMillis()
- startTime));
Progreso = 101;
Calculando = false;
cancelado = false;
System.out.println("FIN ");
} catch (Exception e) {
e.printStackTrace();
}
}
// Prepares a new experiment with data from clusters
private void lanza_experimento_clusters(double tau2,
LFSGrowingLayer mMejor, String dataPath, String rootPath,
LFSData datos1, Double mqeRef) {
// Se calcula el numero de clusters que hay
int nClusters = mMejor.getNClusterMax();
long iteini = iteact;
// Para cada cluster se comprueba si en promedio cumple con tau2xmqeRef
for (int z = 0; z <= nClusters; z++) {
int numDatos = mMejor.getNDatosCluster(z);
if (numDatos > minDatosExp) {
ArrayList<Integer> arrInc = mMejor.getLabelCluster(z);
double quality = mMejor.getMqeCluster(z);
if (quality > tau2 * mqeRef) {
// No cumple, se envia a entrenar el cluster completo
generaExp(mMejor, dataPath, rootPath, datos1, arrInc,
iteini, z);
}
}
}
}
private void generaExp(LFSGrowingLayer mMejor, String dataPath,
String rootPath, LFSData datos1, ArrayList<Integer> arrInc,
long iteini, int z) {
try {
LFSExpProps nexprops = new LFSExpProps(dataPath + "/ExpProps.xml");
String directorio = dataPath + "/n" + z;
mMejor.getgSOM().saveMapCSVParcial(datos1, arrInc, directorio,
directorio + "/data.csv");
nexprops.setRootPath(rootPath);
nexprops.setIsSubred(true);
nexprops.setMqeIni(mMejor.devuelve_mqe_units(arrInc));
nexprops.setFicheroEntrada(directorio + "/data.csv");
nexprops.setSubredOrigen(arrInc);
nexprops.setFPadre("kaski.xml");
nexprops.setDataPath(directorio);
nexprops.setExpName(nexprops.getExpName() + "-" + z);
TrainSelector nuevoExp = new TrainSelector(prof + 1);
iteact = iteini + z;
nuevoExp.LanzaExperimento(nexprops);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Generates a new experiment with data mapped to a unit
private void lanza_experimento_units(double tau2, LFSGrowingLayer mMejor,
String dataPath, String rootPath, LFSData datos1, Double qRef0) {
ArrayList<LFSUnit> ExpUnits = mMejor.getExpandedUnits(
mMejor.getQualityMeasure("QError"), "mqe", tau2, qRef0,
minDatosExp, datos1.numVectors());
// Se lanza un experimento para cada uno de ellos
numIter = numIter + ExpUnits.size();
long iteini = iteact;
try {
for (int z = 0; z < ExpUnits.size(); z++) {
ArrayList<Integer> arrInc = new ArrayList<Integer>();
LFSUnit unidad = ExpUnits.get(z);
arrInc.add(unidad.getYPos() * mMejor.getXSize()
+ unidad.getXPos());
generaExp(mMejor, dataPath, rootPath, datos1, arrInc, iteini, z);
}
} catch (Exception e) {
}
}
public void sendCancel() {
cancelado = true;
}
/**
* @return Returns the progress.
*/
public int getProgreso() {
return Progreso;
}
/**
* @param progreso
* The progreso to set.
*/
public void setProgreso(int progreso) {
Progreso = progreso;
}
/**
* @return To know if it's executing.
*/
public boolean isCalculando() {
return Calculando;
}
/**
* @param calculando
* The calculando to set.
*/
public void setCalculando(boolean calculando) {
Calculando = calculando;
}
private static void copyFile(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
/**
* @return Returns the name of the experiment.
*/
public String getExpName() {
return expName;
}
/**
* @param expName
* The expName to set.
*/
public void setExpName(String expName) {
this.expName = expName;
}
/**
* @return Returns the number of iterations of train.
*/
public int getNumIter() {
return numIter;
}
/**
* @param numIter
* The numIter to set.
*/
public void setNumIter(int numIter) {
this.numIter = numIter;
}
/**