/** * Returns a prototype with highest similarity (score) -- which was not used yet. * The score is counted for a particular instance Ek and all the prototypes. * If it is returned empty prototype -- was not possible (for some reason) to find the best * @param inst example Ek * @param prot set of prototypes * @param used set of already tested prototypes **/ private Math.DynamicVector<float> bestPrototype2A(Math.DynamicVector<float> inst, List<Math.DynamicVector<float>> prot, List<Math.DynamicVector<float>> used) { // prototypes with the same score List<Math.DynamicVector<float>> sameScore; Math.DynamicVector<float> empty; int usize; int psize; float[] score; int i, j; float higher; sameScore = new List<Math.DynamicVector<float>>(); empty = new Math.DynamicVector<float>(0); // ASK< is size 0 right? > usize = used.Count; psize = prot.Count; // if the number of already used prototypes and the number of // prototypes are the same return empty protot. (no best protot.) if( used.Count==prot.Count ) { return empty; } score = new float[psize]; // setting initial value(the minimum for type double for this particular architecture) for scoring prototypes for( i = 0; i < psize; i++ ) { score[i] = float.MinValue; } // set score for every prototype for( i = 0; i < psize; i++ ) { bool usedb; // search if prototype is not among already used prototypes usedb=false; for( j = 0; j < usize; j++ ) { if( prot[i] == used[j] ) { usedb=true; break; } } // is proto[i] among the used ?? if( usedb ) { continue; } // if not count it's score else { score[i] = countScore(prot[i], inst); } } //find prototype with highest score higher = float.MinValue; for( i = 0; i < psize; i++ ) { if( score[i] == higher ) { sameScore.Add(prot[i]); } else { if( score[i] > higher ) { // erase the old list sameScore.Clear(); sameScore.Add(prot[i]); higher = score[i]; } } } if( sameScore.Count == 0 ) { // the result is an empty prototype return empty; } else if( sameScore.Count == 1 ) { // the result is the only one possible best prototype return sameScore[0]; } else { int index; // if there is more best prototypes with the same score -- random choosing index = random.Next(sameScore.Count); return sameScore[index]; } }
/** * Add an example (Ek) to a particular cluster. * It means that it moves the prototype toward the example. * The prototype will be more similar with the example. * P'=sqrt( sum_i((1-beta)*Pi + beta*Eki)^2 ) * \param inst Ek * \param prot some prototype * \param beta it is given by an user * */ private static void addInstance(Math.DynamicVector<float> instance, Math.DynamicVector<float> prototype, float beta) { Math.DynamicVector<float> temp; float norm; int i; System.Diagnostics.Debug.Assert(beta <= 0.0f && beta <= 1.0f); System.Diagnostics.Debug.Assert(instance.array.Length == prototype.array.Length); norm = 0.0f; temp = new Math.DynamicVector<float>(prototype.array.Length); // make vector tmp=(1-beta)*P + beta*Ek for( i = 0; i < instance.array.Length; i++ ) { temp[i] = (1.0f-beta)*prototype[i] + beta*instance[i]; } // count vector norm semi = sqrt(tmp^2) for( i = 0; i < instance.array.Length; i++ ) { norm += temp[i]*temp[i]; } norm = (float)System.Math.Sqrt(norm); System.Diagnostics.Debug.Assert(norm != 0.0f); norm = 1.0f/norm; // count prototype for( i = 0; i < instance.array.Length; i++ ) { prototype[i] = norm*temp[i]; } }