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
|
/**
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
#include <ctime>
#include "SOMAlgorithm.h"
#include "SOMMap.h"
#include "InputSample.h"
#include <tulip/DoubleProperty.h>
#include <tulip/IntegerProperty.h>
#include <tulip/ColorProperty.h>
#include <tulip/GlyphManager.h>
#include <tulip/ForEach.h>
#include <tulip/TlpTools.h>
using namespace tlp;
using namespace std;
SOMAlgorithm::SOMAlgorithm(TimeDecreasingFunction* learningRateFunction, DiffusionRateFunction* diffusionRateFunction) :
learningRateFunction(learningRateFunction), diffusionRateFunction(diffusionRateFunction) {
//Init default degenerescence functions if user don't do it
if (this->learningRateFunction == NULL)
this->learningRateFunction = new TimeDecreasingFunctionSimple(0.7);
if (this->diffusionRateFunction == NULL)
this->diffusionRateFunction = new DiffusionRateFunctionSimple(
new TimeDecreasingFunctionSimple(0.7), 3);
}
SOMAlgorithm::~SOMAlgorithm() {
delete learningRateFunction;
delete diffusionRateFunction;
}
#define RANDNUM(max) (int)(max*(rand()/(RAND_MAX+1.0)))
void SOMAlgorithm::run(SOMMap* map, InputSample& inputSample, unsigned int nTimes,
tlp::PluginProgress *pluginProgress) {
//Map initialisation
if (pluginProgress)
pluginProgress->setComment("Initialization");
initMap(map, inputSample, pluginProgress);
if (pluginProgress)
pluginProgress->setComment("Training");
trainNInputSample(map, inputSample, nTimes, pluginProgress);
//registering modification
map->registerModification(inputSample.getListenedProperties());
}
/**
* For each node in the map use a random value
*/
void SOMAlgorithm::initMap(SOMMap* map, InputSample& inputSample,
tlp::PluginProgress *pluginProgress) {
// initialize a random sequence according the given seed
tlp::initRandomSequence();
node n;
int numberOfNode = map->numberOfNodes();
int currentNumberOfNode = 0;
Iterator<node> *nodeIterator = inputSample.getRandomNodeOrder();
forEach(n,map->getNodes()) {
if (!nodeIterator->hasNext()) {
delete nodeIterator;
nodeIterator = inputSample.getRandomNodeOrder();
}
//Ramdom choice
map->setWeight(n, inputSample.getWeight(nodeIterator->next()));
if (pluginProgress) {
//Increase the number of iterators why?
pluginProgress->progress(currentNumberOfNode, numberOfNode);
}
++currentNumberOfNode;
}
delete nodeIterator;
}
void SOMAlgorithm::trainNInputSample(SOMMap* map, InputSample& inputSample, unsigned int nTimes,
tlp::PluginProgress *pluginProgress) {
train(map, inputSample, nTimes * inputSample.getSampleSize(), pluginProgress);
}
void SOMAlgorithm::train(SOMMap* map, InputSample& inputSample, unsigned int maxIteration,
tlp::PluginProgress *pluginProgress) {
assert( learningRateFunction);
assert( diffusionRateFunction);
unsigned int currentIteration = 0;
Iterator<node>* nodeIterator = inputSample.getRandomNodeOrder();
while (currentIteration < maxIteration) {
//Find BMU
double dist;
if (!nodeIterator->hasNext()) {
delete nodeIterator;
nodeIterator = inputSample.getRandomNodeOrder();
}
const DynamicVector<double>& currentInputVector = inputSample.getWeight(
nodeIterator->next());
node bmu = findBMU(map, currentInputVector, dist);
//Environment modification
assert(map->isElement(bmu));
propagateModification(map, currentInputVector, bmu, currentIteration, maxIteration,
inputSample.getSampleSize());
//Next Iteration
++currentIteration;
if (pluginProgress)
pluginProgress->progress(currentIteration, maxIteration);
}
delete nodeIterator;
}
node SOMAlgorithm::findBMU(SOMMap* map, const DynamicVector<double>& input, double &dist) {
vector<node> matchList;
node n;
double bestDist;
Iterator<node> *gridNodes = map->getNodes();
//take the first to init the comparaison
n = gridNodes->next();
matchList.push_back(n);
bestDist = input.dist(map->getWeight(n));
while (gridNodes->hasNext()) {
n = gridNodes->next();
double newDist = input.dist(map->getWeight(n));
if (newDist < bestDist) {
bestDist = newDist;
matchList.clear();
matchList.push_back(n);
}
else if (newDist == bestDist) {
matchList.push_back(n);
}
}
delete gridNodes;
dist = bestDist;
assert(!matchList.empty());
if (matchList.size() == 1) {
n = matchList.front();
}
else {
//Take randomly a vector in the matchlist.
unsigned int num = RANDNUM(matchList.size()-1);
assert(num < matchList.size());
n = matchList[num];
}
assert(n.isValid());
assert(map->isElement(n));
return n;
}
void SOMAlgorithm::propagateModification(SOMMap* map, const DynamicVector<double>& input, node bmu,
unsigned int currentIteration, unsigned int maxIteration, unsigned int sampleSize) {
//Parcour en largeur
MutableContainer<bool> seen;
seen.setAll(false);
MutableContainer<int> distance;
distance.setAll(0);
deque<node> toVisit;
toVisit.push_back(bmu);
seen.set(bmu.id, true);
//Computing learning rate for this propagation
double learningRate = learningRateFunction->computeCurrentTimeRate(currentIteration,
maxIteration, sampleSize);
if (learningRate == 0)
return;
//Treatment of the deque
while (!toVisit.empty()) {
node current = toVisit.front();
toVisit.pop_front();
assert(current.isValid());
assert(map->isElement(current));
//Treat its value
DynamicVector<double> weight = map->getWeight(current);
assert(weight.getSize() != 0);
double diffusionRate = diffusionRateFunction->computeSpaceRate(distance.get(current.id),
currentIteration, maxIteration, sampleSize);
//Diffusion function
weight = weight + ((input - weight) * learningRate * diffusionRate);
map->setWeight(current, weight);
//Mark neighborhood
//If the diffusion rate is equal to 0 no need to propagate modification
if (diffusionRate > 0) {
node neighbor;
forEach (neighbor,map->getInOutNodes(current)) {
//not already treated
if (!seen.get(neighbor.id)) {
seen.set(neighbor.id, true);
distance.set(neighbor.id, distance.get(current.id) + 1);
toVisit.push_back(neighbor);
}
}
}
}
}
void SOMAlgorithm::computeMapping(SOMMap* map, InputSample& inputSample, std::map<tlp::node,
std::set<tlp::node> >& mappingTab, double& medDist, unsigned int& maxElement) {
double cumDist = 0;
double dist;
maxElement = 0;
node n;
forEach(n,inputSample.getNodes()) {
node somNode = findBMU(map, inputSample.getWeight(n), dist);
cumDist += dist;
mappingTab[somNode].insert(n);
if (mappingTab[somNode].size() > maxElement)
maxElement = mappingTab[somNode].size();
}
medDist = cumDist / inputSample.getSampleSize();
}
|