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 : #ifndef __PLUMED_dimred_SketchMapBase_h 23 : #define __PLUMED_dimred_SketchMapBase_h 24 : 25 : #include "tools/SwitchingFunction.h" 26 : #include "DimensionalityReductionBase.h" 27 : 28 : namespace PLMD { 29 : namespace dimred { 30 : 31 : class SketchMapBase : public DimensionalityReductionBase { 32 : private: 33 : /// To save us retyping switching functions many times the code will reuse the 34 : /// ones from previous sketch-map objects 35 : bool reuse_hd, reuse_ld; 36 : /// Was previous action in chain a sketch-map action 37 : SketchMapBase* smapbase; 38 : /// Switching functions for low and high dimensional space 39 : SwitchingFunction lowdf, highdf; 40 : /// This is used within calculate stress to hold the target distances and the 41 : /// target values for the high dimensional switching function 42 : std::vector<double> dtargets, ftargets, pweights; 43 : /// Stress normalization (sum_ij w_i w_j) 44 : double normw; 45 : protected: 46 : /// This holds the target distances and target transformed distances 47 : Matrix<double> distances, transformed; 48 : /// The fraction of pure distances to mix in when optimising 49 : double mixparam; 50 : public: 51 : static void registerKeywords( Keywords& keys ); 52 : explicit SketchMapBase( const ActionOptions& ); 53 : /// This starts the process of calculating the projections 54 : void calculateProjections( const Matrix<double>&, Matrix<double>& ) override; 55 : /// This finishes the process of calculating the prjections 56 : virtual void minimise( Matrix<double>& )=0; 57 : /// Apply the low dimensional switching function to the value val 58 : double transformLowDimensionalDistance( const double& val, double& df ) const ; 59 : /// Apply the high dimensional switching function to the value val 60 : double transformHighDimensionalDistance( const double& val, double& df ) const ; 61 : /// Set the target distance and from it calculate the target value for the switching function 62 : /// This target vector is used when we use calculateStress when finding the projections of individual points. 63 : /// For example this function is used in PLMD::dimred::ProjectOutOfSample 64 : void setTargetDistance( const unsigned& idata, const double& dist ) override; 65 : /// Calculate the pointwise stress on one point when it is located at p. 66 : /// This function makes use of the distance data in dtargets and ftargets 67 : /// It is used in PLMD::dimred::ProjectOutOfSample and in pointwise optimisation 68 : double calculateStress( const std::vector<double>& p, std::vector<double>& d ) override; 69 : /// Calculate the total stress when the projections are placed at point p. Notice 70 : /// this is a vectorized version of the matrix of projections 71 : double calculateFullStress( const std::vector<double>& p, std::vector<double>& d ); 72 : }; 73 : 74 : inline 75 26384110 : double SketchMapBase::transformLowDimensionalDistance( const double& val, double& df ) const { 76 26384110 : if( reuse_ld ) return smapbase->transformLowDimensionalDistance( val, df ); 77 21473710 : double ans=1.0 - lowdf.calculate( val, df ); df*=-val; return ans; 78 : } 79 : 80 : inline 81 341661 : double SketchMapBase::transformHighDimensionalDistance( const double& val, double& df ) const { 82 341661 : if( reuse_hd ) return smapbase->transformHighDimensionalDistance( val, df ); 83 316711 : double ans=1.0 - highdf.calculate( val, df ); df*=-val; return ans; 84 : } 85 : 86 : inline 87 145000 : void SketchMapBase::setTargetDistance( const unsigned& idata, const double& dist ) { 88 145000 : double df; dtargets[idata]=dist; ftargets[idata]=transformHighDimensionalDistance( dist, df ); 89 145000 : } 90 : 91 : } 92 : } 93 : #endif