1 /*  _______________________________________________________________________
2 
3     DAKOTA: Design Analysis Kit for Optimization and Terascale Applications
4     Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Dakota directory.
7     _______________________________________________________________________ */
8 
9 //- Class:        MorseSmaleComplex
10 //- Description:  Class declaration for MorseSmaleComplex
11 //- Owner:        Dan Maljovec, Mohamed Ebeida
12 //- Version: $Id$
13 
14 #ifndef MS_COMPLEX_H
15 #define MS_COMPLEX_H
16 
17 #include "ANN/ANN.h"
18 #include "ANN/ANNperf.h"
19 #include "ANN/ANNx.h"
20 
21 #include <vector>
22 #include <cstdlib>
23 #include <cstdio>
24 
25 #pragma once
26 //using namespace std;
27 
28 #define LOCAL_MIN 0
29 #define LOCAL_MAX 1
30 #define SADDLE 2
31 #define REGULAR 3
32 
33 class KNN_Edge;
34 
35 class Vertex
36 {
37 public:
38 	Vertex(int n, double *p, double _val, int _id);
39 	void CreateANNpoint(ANNpoint &p);
40 	double GetXi(int i);
41 	void Union_Max(Vertex * v, Vertex * V[]);
42 	int Find_Max(Vertex * V[]);
43 	void Union_Min(Vertex * v, Vertex * V[]);
44 	int Find_Min(Vertex * V[]);
45 	int GetIthNeighbor(int i, KNN_Edge * E[]);
46 	void ResetExtrema();
47 	double Value();
48   double SDistance(Vertex *v);
49 	Vertex(Vertex &v);
50 	~Vertex();
51 
NeighborMax()52 	int NeighborMax() { return UF_max;}
NeighborMin()53 	int NeighborMin() { return UF_min;}
54 
55 	int e;
56 	int classification;		//0 = minimum, 1=maximmum, 2=saddle, 3=regular
57 	int ID;
58 	double persistence;
59 
60 private:
61 	double *x;
62 	double val;
63 	int d;
64 	int UF_max; //The neighboring maximumm
65 	int UF_min; //The neighboring minimum
66 	int PC_UF_max; //The MS-Cell-max
67 	int PC_UF_min; //The MS-Cell-min
68 };
69 
70 struct Saddle
71 {
72 	int idx;
73 	int more;
74 	int less;
75 	double persistence;
76 	Saddle *next;
77 };
78 
79 class KNN_Edge
80 {
81 public:
82 	KNN_Edge(Vertex * _start, Vertex * _end, KNN_Edge * E[], int id);
83 	int ID;
84 	int start,end;
85 	int nextKNN_Edge;
86 };
87 
88 class MS_Crystal
89 {
90 public:
91 	MS_Crystal(Vertex ** _V, int *_vIds, int _count, double _d);
92 	MS_Crystal(MS_Crystal *a, MS_Crystal *b, bool mergeMax, int persistence);
93 	MS_Crystal(MS_Crystal *a, bool mergeMax, int nuExtrema, int persistence);
94 	~MS_Crystal();
95 	int size();
96 	int GetVertexID(int i);
97 	bool Exists(double persistence);
98 
99 	int MaxV();
100 	int MinV();
101 
102 	double minP;
103 	double maxP;
104 
105 private:
106 	int minV;
107 	int maxV;
108 	Vertex **V;
109 	int *v;
110 	int count;
111 	int d;
112 };
113 
114 class MS_Complex
115 {
116 public:
117 	MS_Complex(double  *points, int dimension, int count, int _k=15, bool perturb=false);
118 	MS_Complex(MS_Complex &C, double *new_point);
119 	~MS_Complex();
120 	void Destroy();
121 	void KNN();
122 	void Compute();
123   void Print(std::ostream &out);
124 	Vertex * *V;
125 	KNN_Edge * *E;
126 	MS_Crystal * *C;
127 	//Saddle * *S;
128 	int szV;
129 	int szE;
130 	int numV;
131 	int numE;
132 	int numC;
133 	int numKneighbors;
134 	int d;
135 	int *V_to_C;
136 	int p_levels;
137 	int globalMinIdx;
138 	int globalMaxIdx;
139 	double *persistences;
140   double maxDist;
141 	int szP;
142 
143 #ifdef USING_GL
144 	void Draw(double gMin, double gMax,bool flatMode);
145 	void DrawBurst();
146 #endif
147 
148 	double CompareExtremaCount(MS_Complex &_C);
149   double CompareClassChange(MS_Complex &_C);
150   double ComparePersistence(MS_Complex &_C);
151 	double ComparePersistenceNoSaddles(MS_Complex &_C);
152   double CompareBottleneck(MS_Complex &_C);
153   double CompareLabels(MS_Complex &_C, double p);
154 	double CloseToExtrema(double *v, double p);
155 	double FarFromExtrema(double *v, double p);
156   double FarFromSaddle(double *v, double p);
157   double FarFromCP(double *v, double p);
158   int    GetIthHighestPersistence(int i);
159   int    GetIthHighestPersistenceSaddle(int i);
160   bool   IsCritical(int i);
161   bool   IsExtrema(int i);
IsMaximum(int i)162   bool   IsMaximum(int i)
163   {
164     return i < numV && i >= 0 && V[i]->classification == LOCAL_MAX;
165   }
166   bool   IsSaddle(int i);
167   Vertex * GetVertex(int i);
168 
169   int CountExtrema(double p=0);
170   int CountMaxima(double p=0);
171   int CountMinima(double p=0);
172   int CountSaddles(double p=0);
173 
174 private:
175   bool perturbed;
DoesEdgeExist(int v1,int v2,int maxIndex)176   bool DoesEdgeExist(int v1, int v2, int maxIndex)
177   {
178     for(int i = 0; i < maxIndex; i++)
179       if(E[i]->start == v1 && E[i]->end == v2)
180         return true;
181     return false;
182   }
183 	ANNkd_tree *SearchStructure;
184 };
185 double ScoreTOPOB(MS_Complex &C, double *x);
186 double ScoreTOPOP(MS_Complex &C, double *x);
187 std::vector<int> ScoreTOPOHP(int dimension, int knn,
188   double *training, double *trainingY, int n_training,
189   double *candidates, double *candidateY, int n_candidates);
190 
191 
192 #endif //MS_COMPLEX_H
193