void SOMAW<TNeuron, TSOMParameters>::updateNeuron(TNeuron &neuron, MatVector<float> &data, float alpha, float phi) { for (uint i = 0; i < data.size(); i++) { //update neuron weights float distance = fabs(data[i] - neuron.weights[i]); neuron.avgDistance[i] = neuron.avgDistance[i] + this->parameters.k1 * ((neuron.dsWeights[i] * distance) - neuron.avgDistance[i]); } float average = neuron.avgDistance.sum() / neuron.avgDistance.size(); //update neuron ds weights for (uint i = 0; i < data.size(); i++) { //update neuron weights float distance = fabs(data[i] - neuron.weights[i]); if (neuron.dsWeights[i] * distance > average) neuron.dsWeights[i] = this->parameters.k2 * neuron.dsWeights[i]; else neuron.dsWeights[i] = this->parameters.k3 * neuron.dsWeights[i]; } float sum = neuron.dsWeights.sum(); for (uint i = 0; i < data.size(); i++) { neuron.dsWeights[i] = neuron.dsWeights[i] / sum; } //update neuron weights for (uint i = 0; i < data.size(); i++) { neuron.weights[i] = neuron.weights[i] + phi * alpha * (data[i] - neuron.weights[i]); } }
float SOMAW<TNeuron, TSOMParameters>::presentPaternToNeuron(const MatVector<float> &data, TNeuron &neuron) { float distance = 0; for (uint i = 0; i < data.size(); i++) { distance += qrt(neuron.dsWeights[i]) * qrt(fabs(data[i] - neuron.weights[i])); } return (1 / (distance + 0.0000001)); }
double maxDistance(MatVector distancesTable, vector<locationNode> locationList) { double maxDist=0; int numNodes=distancesTable.size(); for (int i=0; i<numNodes; i++) { for (int j=0; j<numNodes; j++) { if (distancesTable[i][j]>maxDist) maxDist=distancesTable[i][j]; } } double maxArea=0; numNodes=locationList.size(); for (int i=0; i<numNodes; i++) { if (locationList[i].area>maxArea) maxArea=locationList[i].area; } return maxDist+maxArea; }
string buildEnvironment(vector<locationNode> &locationList, MatVector &distancesTable, int numNodes, string mapType, string probabilityDistribution, string object) { ostringstream stream2String; string msg; //Initialize random generators default_random_engine generator; uniform_real_distribution<double> distribution(0.0,1.0); //numbers in [0,1] //Pass the random numbers according the map number //so, choosing different map generates different random numbers for (int i=0; i<selectedMap*numNodes*numNodes*numNodes; i++) { double dump=distribution(generator); } vector<double> roomProbabilities; if (probabilityDistribution!="File") generateRoomProbabilities(probabilityDistribution, numNodes, roomProbabilities); //Generated with random else { loadRoomProbabilities(object, numNodes, roomProbabilities, "/home/izquierdocr/projects/internetQuery/data/experimentQueryWeightedProbabilityWebPb.txt"); //Loaded from files //loadRoomProbabilities(object, numNodes, roomProbabilities, "/home/izquierdocr/projects/internetQuery/data/bordaUserPb.txt"); //Loaded from users } for (int i=0; i<numNodes; i++) { locationNode location; location.ID=i; location.objectProbability=roomProbabilities[numNodes-i-1]; //Only for evaluating cross probabilities. This can be changed to [i]. Choosen only for having other order than 0,1,2... location.priority=0; locationList.push_back(location); } doubleVector rowMat; rowMat.assign(numNodes,0); for (int i=0; i<numNodes; i++) { distancesTable.push_back(rowMat); } if (mapType=="R") { //Type: House(H), Office (O), Random (R) for (int i=0; i<numNodes; i++) { locationList[i].area = ( DISTANCES_MEAN + DISTANCES_SIGMA * (distribution(generator)-0.5) )/2; //Assume areas represent in general 1/2 of distances between rooms (Here area defined as robot movement inside rooms } //Generate distances randomly for (int i=1; i<numNodes; i++) { for (int j=0; j<i; j++) { distancesTable[i][j]= DISTANCES_MEAN + DISTANCES_SIGMA * (distribution(generator)-0.5); distancesTable[j][i]=distancesTable[i][j]; } } } if (mapType=="H") { //Type: House(H), Office (O), Random (R) //Load distances from files //int numMap = MAX_MAPS*(double(rand()) / RAND_MAX)+1; //Random house map int numMap = selectedMap; loadMapData(numNodes, numMap, mapType, distancesTable); for (int i=0; i<numNodes; i++) { locationList[i].area = areas[i]; //locationList[i].area = 1; //Testing with equal areas } } if (mapType=="O") { //Type: House(H), Office (O), Random (R) //Not yet office maps } //Print node data cout << "Locations (ID,Priority,Probability,Area)" << endl; stream2String << "Locations (ID,Priority,Probability,Area)" << endl; for (int i=0; i<locationList.size(); i++) { cout << locationList[i].ID << ", "; cout << locationList[i].priority << ", "; cout << locationList[i].objectProbability << ", "; cout << locationList[i].area << endl; stream2String << locationList[i].ID << ","; stream2String << locationList[i].priority << ","; stream2String << locationList[i].objectProbability << ","; stream2String << locationList[i].area << endl; } //Print distances data cout << "Distances table" << endl; stream2String << "Distances table" << endl; for (int i=0; i<numNodes; i++) { for (int j=0; j<numNodes; j++) { cout << distancesTable[i][j] << ","; stream2String << distancesTable[i][j] << ","; } cout << endl; stream2String << endl; } msg = stream2String.str(); return msg; }