LCOV - code coverage report
Current view: top level - analysis - FarthestPointSampling.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 31 32 96.9 %
Date: 2026-03-30 13:16:06 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "LandmarkSelectionBase.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "tools/Random.h"
      25             : 
      26             : //+PLUMEDOC LANDMARKS LANDMARK_SELECT_FPS
      27             : /*
      28             : Select a set of landmarks using farthest point sampling.
      29             : 
      30             : \par Examples
      31             : 
      32             : */
      33             : //+ENDPLUMEDOC
      34             : 
      35             : namespace PLMD {
      36             : namespace analysis {
      37             : 
      38             : class FarthestPointSampling : public LandmarkSelectionBase {
      39             : private:
      40             :   unsigned seed;
      41             : public:
      42             :   static void registerKeywords( Keywords& keys );
      43             :   explicit FarthestPointSampling( const ActionOptions& ao );
      44             :   void selectLandmarks() override;
      45             : };
      46             : 
      47       13787 : PLUMED_REGISTER_ACTION(FarthestPointSampling,"LANDMARK_SELECT_FPS")
      48             : 
      49           5 : void FarthestPointSampling::registerKeywords( Keywords& keys ) {
      50           5 :   LandmarkSelectionBase::registerKeywords(keys);
      51          10 :   keys.add("compulsory","SEED","1234","a random number seed");
      52           5 : }
      53             : 
      54           1 : FarthestPointSampling::FarthestPointSampling( const ActionOptions& ao ):
      55             :   Action(ao),
      56           1 :   LandmarkSelectionBase(ao) {
      57           1 :   if( !dissimilaritiesWereSet() ) {
      58           0 :     error("dissimilarities have not been calcualted in input action");
      59             :   }
      60           1 :   parse("SEED",seed);
      61           1 : }
      62             : 
      63           1 : void FarthestPointSampling::selectLandmarks() {
      64           1 :   std::vector<unsigned> landmarks( getNumberOfDataPoints() );
      65             : 
      66             :   // Select first point at random
      67           1 :   Random random;
      68           1 :   random.setSeed(-seed);
      69           1 :   double rand=random.RandU01();
      70           1 :   landmarks[0] = std::floor( my_input_data->getNumberOfDataPoints()*rand );
      71           1 :   selectFrame( landmarks[0] );
      72             : 
      73             :   // Now find distance to all other points (N.B. We can use squared distances here for speed)
      74           1 :   Matrix<double> distances( getNumberOfDataPoints(), my_input_data->getNumberOfDataPoints() );
      75          14 :   for(unsigned i=0; i<my_input_data->getNumberOfDataPoints(); ++i) {
      76          13 :     distances(0,i) = my_input_data->getDissimilarity( landmarks[0], i );
      77             :   }
      78             : 
      79             :   // Now find all other landmarks
      80           3 :   for(unsigned i=1; i<getNumberOfDataPoints(); ++i) {
      81             :     // Find point that has the largest minimum distance from the landmarks selected thus far
      82             :     double maxd=0;
      83          28 :     for(unsigned j=0; j<my_input_data->getNumberOfDataPoints(); ++j) {
      84          26 :       double mind=distances(0,j);
      85          39 :       for(unsigned k=1; k<i; ++k) {
      86          13 :         if( distances(k,j)<mind ) {
      87             :           mind=distances(k,j);
      88             :         }
      89             :       }
      90          26 :       if( mind>maxd ) {
      91             :         maxd=mind;
      92           5 :         landmarks[i]=j;
      93             :       }
      94             :     }
      95           2 :     selectFrame( landmarks[i] );
      96          28 :     for(unsigned k=0; k<my_input_data->getNumberOfDataPoints(); ++k) {
      97          26 :       distances(i,k) = my_input_data->getDissimilarity( landmarks[i], k );
      98             :     }
      99             :   }
     100           1 : }
     101             : 
     102             : }
     103             : }

Generated by: LCOV version 1.16