00001
00002 #ifndef OnlineLearning_H
00003 #define OnlineLearning_H
00004
00005 #include <cstdlib>
00006
00007 #include "LearningAlgorithm.h"
00008 #include "util.h"
00009 #include "TopolParams.h"
00010 #include <math.h>
00011
00012
00013
00021 class OnlineLearning : public LearningAlgorithm {
00022
00023
00024
00025 public:
00027 OnlineLearning(const TopolParams& s):LearningAlgorithm(s) {};
00028
00030 int Learning ( NetParams& net );
00031
00032 const char* className();
00033
00034 };
00035
00036
00037 int
00038 OnlineLearning::Learning ( NetParams& net ) {
00039
00040 int i, i1, j, bmu, t, order_pos, mod, pick, temp;
00041 Value_Type h, r, alpha_init, alpha;
00042 Value_Type d = net.getData()->getDimension();
00043 int m = net.getMapcode()->getMapSize();
00044 int n = net.getData()->getDataSize();
00045
00046 int num_iterations = net.getNumIterations();
00047 int neighboor = net.getMapcode()->getNeighborType();
00048
00049 TMatrix entries = net.getData()->getEntries();
00050 SOMData * data = net.getData();
00051 TMatrix delta = net.getDelta();
00052
00053 TMatrix codebook = net.getMapcode()->getCodebook();
00054 Mapcode * mapcode = net.getMapcode();
00055
00056 BMUnode * winr;
00057
00058
00059 vector<int> order;
00060 for (int i = 0; i < n; i++) {
00061 order.push_back(i+1);
00062 }
00063 srand( (unsigned) time ( NULL ) );
00064
00065
00066 r = net.getInitNeighbor();
00067 alpha_init = net.getInitLearningRate();
00068 alpha = alpha_init;
00069
00070 for (t=1; t<= num_iterations; t++ ) {
00071
00072 setActualIteraction( t );
00073
00074 mod = n;
00075
00076 for (i = 1; i <= n; i++) {
00077 order_pos = ( rand() % mod );
00078 pick = order[order_pos];
00079
00080 winr = winner(data, mapcode, pick);
00081
00082 for (i1=1; i1<=m; i1++)
00083 {
00084 h = getTopology()->H( delta[i1][winr->i], r, neighboor );
00085
00086 for ( j=1; j<=d; j++)
00087 codebook[i1][j] += h*alpha*( entries[pick][j] - codebook[i1][j] );
00088 }
00089 mapcode->setCodebook( codebook );
00090
00091
00092 if (order_pos != mod)
00093 {
00094 temp = order[mod - 1];
00095 order[mod - 1] = order[order_pos];
00096 order[order_pos] = temp;
00097 }
00098 mod--;
00099 }
00100
00101
00102
00103 r = radius( r, num_iterations, t);
00104 alpha = LearningAlgorithm::alpha( alpha_init, num_iterations, t);
00105 }
00106 net.getMapcode()->setCodebook( codebook );
00107 MakeBMU(data,mapcode);
00108
00109 return 0;
00110 };
00111
00112 #endif