LCOV - code coverage report
Current view: top level - analysis - FarthestPointSampling.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 26 26 100.0 %
Date: 2024-10-11 08:09:47 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       10421 : PLUMED_REGISTER_ACTION(FarthestPointSampling,"LANDMARK_SELECT_FPS")
      48             : 
      49           2 : void FarthestPointSampling::registerKeywords( Keywords& keys ) {
      50           2 :   LandmarkSelectionBase::registerKeywords(keys);
      51           4 :   keys.add("compulsory","SEED","1234","a random number seed");
      52           2 : }
      53             : 
      54           1 : FarthestPointSampling::FarthestPointSampling( const ActionOptions& ao ):
      55             :   Action(ao),
      56           1 :   LandmarkSelectionBase(ao)
      57             : {
      58           1 :   if( !dissimilaritiesWereSet() ) error("dissimilarities have not been calcualted in input action");
      59           1 :   parse("SEED",seed);
      60           1 : }
      61             : 
      62           1 : void FarthestPointSampling::selectLandmarks() {
      63           1 :   std::vector<unsigned> landmarks( getNumberOfDataPoints() );
      64             : 
      65             :   // Select first point at random
      66           1 :   Random random; random.setSeed(-seed); double rand=random.RandU01();
      67           1 :   landmarks[0] = std::floor( my_input_data->getNumberOfDataPoints()*rand );
      68           1 :   selectFrame( landmarks[0] );
      69             : 
      70             :   // Now find distance to all other points (N.B. We can use squared distances here for speed)
      71           1 :   Matrix<double> distances( getNumberOfDataPoints(), my_input_data->getNumberOfDataPoints() );
      72          14 :   for(unsigned i=0; i<my_input_data->getNumberOfDataPoints(); ++i) distances(0,i) = my_input_data->getDissimilarity( landmarks[0], i );
      73             : 
      74             :   // Now find all other landmarks
      75           3 :   for(unsigned i=1; i<getNumberOfDataPoints(); ++i) {
      76             :     // Find point that has the largest minimum distance from the landmarks selected thus far
      77             :     double maxd=0;
      78          28 :     for(unsigned j=0; j<my_input_data->getNumberOfDataPoints(); ++j) {
      79          26 :       double mind=distances(0,j);
      80          39 :       for(unsigned k=1; k<i; ++k) {
      81          13 :         if( distances(k,j)<mind ) { mind=distances(k,j); }
      82             :       }
      83          26 :       if( mind>maxd ) { maxd=mind; landmarks[i]=j; }
      84             :     }
      85           2 :     selectFrame( landmarks[i] );
      86          28 :     for(unsigned k=0; k<my_input_data->getNumberOfDataPoints(); ++k) distances(i,k) = my_input_data->getDissimilarity( landmarks[i], k );
      87             :   }
      88           1 : }
      89             : 
      90             : }
      91             : }

Generated by: LCOV version 1.15