[349] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[869] | 2 | // Copyright (C) 1999-2019 Maciej Komosinski and Szymon Ulatowski. |
---|
[349] | 3 | // See LICENSE.txt for details. |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | #ifndef _SIMIL_MODEL_H_ |
---|
| 7 | #define _SIMIL_MODEL_H_ |
---|
| 8 | |
---|
| 9 | #include "frams/genetics/geno.h" |
---|
| 10 | #include "frams/model/model.h" |
---|
| 11 | #include "simil_match.h" |
---|
| 12 | |
---|
| 13 | #define TDN_SIZE 5 |
---|
| 14 | |
---|
| 15 | enum TDNELEMS |
---|
| 16 | { |
---|
[606] | 17 | ORIG_IND = 0, |
---|
| 18 | DEGREE = 1, |
---|
| 19 | NEURO_CONNS = 2, |
---|
| 20 | NEURONS = 3, |
---|
| 21 | FUZZ_DEG = 4 |
---|
[349] | 22 | }; |
---|
| 23 | |
---|
| 24 | /** This class defines similarity measure for Framsticks organisms. |
---|
| 25 | * Authors: |
---|
| 26 | * Marek Kubiak (concept, implementation) |
---|
| 27 | * Maciej Komosinski (concept, Framsticks interface) |
---|
[869] | 28 | * Agnieszka Mensfelt (refactoring, improvements) |
---|
[349] | 29 | */ |
---|
| 30 | class ModelSimil |
---|
| 31 | { |
---|
| 32 | public: |
---|
[606] | 33 | ModelSimil(); |
---|
| 34 | virtual ~ModelSimil(); |
---|
[869] | 35 | double EvaluateDistance(const Geno *G0, const Geno *G1); //chooses greedy or hungarian |
---|
| 36 | double EvaluateDistanceGreedy(const Geno *G0, const Geno *G1); |
---|
| 37 | double EvaluateDistanceHungarian(const Geno *G0, const Geno *G1); |
---|
[349] | 38 | |
---|
[606] | 39 | static int CompareDegrees(const void *pElem1, const void *pElem2); |
---|
[869] | 40 | static int CompareFuzzyDegrees(const void *pElem1, const void *pElem2); |
---|
[606] | 41 | static int CompareConnsNo(const void *pElem1, const void *pElem2); |
---|
| 42 | static int GetNOFactors(); |
---|
[349] | 43 | #define STATRICKCLASS ModelSimil |
---|
[606] | 44 | PARAMPROCDEF(p_evaldistance); |
---|
[349] | 45 | #undef STATRICKCLASS |
---|
| 46 | |
---|
| 47 | protected: |
---|
[606] | 48 | void _PrintSeamnessTable(std::vector<int> *pVector, int iCount); |
---|
| 49 | //matching function |
---|
| 50 | int MatchPartsGeometry(); |
---|
| 51 | void ComputeMatching(); |
---|
[869] | 52 | void FillPartsDistances(double *&dist, int bigger, int smaller, bool geo); |
---|
[606] | 53 | void _PrintPartsMatching(); |
---|
| 54 | void SaveIntermediateFiles(); |
---|
[349] | 55 | |
---|
[606] | 56 | int SortPartInfoTables(); |
---|
| 57 | int CountPartNeurons(); |
---|
| 58 | bool ComputePartsPositionsBySVD(); |
---|
| 59 | int GetPartPositions(); |
---|
| 60 | int CountPartDegrees(); |
---|
[349] | 61 | |
---|
[606] | 62 | void CountFuzzyNeighb(); |
---|
| 63 | void SortFuzzyNeighb(); |
---|
| 64 | void GetNeighbIndexes(int mod, int partInd, std::vector<int> &indexes); |
---|
[869] | 65 | void FuzzyOrder(); |
---|
[349] | 66 | |
---|
[877] | 67 | static Model* newModel(const Geno *g); |
---|
[606] | 68 | int CreatePartInfoTables(); |
---|
| 69 | void _PrintDegrees(int i); |
---|
| 70 | void _PrintArray(int *array, int base, int size); |
---|
| 71 | void _PrintNeighbourhood(int i); |
---|
[869] | 72 | void _PrintFuzzyNeighbourhood(int i); |
---|
[606] | 73 | void _PrintArrayDouble(double *array, int base, int size); |
---|
| 74 | int CountPartsDistance(); |
---|
[349] | 75 | |
---|
| 76 | |
---|
| 77 | public: |
---|
[869] | 78 | /// Currently selected matching algorithm. Allowed values: 0 (more exact, slower), 1 (more greedy, faster). Details in https://doi.org/10.1007/978-3-030-16692-2_8 |
---|
| 79 | /// @sa EvaluateDistance |
---|
[872] | 80 | paInt matching_method; |
---|
[869] | 81 | |
---|
[606] | 82 | /// Table of weights for weighted distance function. |
---|
| 83 | /// Weights are for factors in the following order: |
---|
| 84 | /// [0]: m_iDV (difference in the number of vertices) |
---|
| 85 | /// [1]: m_iDD (difference in degrees over matching) |
---|
| 86 | /// [2]: m_iDN (difference in neurons over matching) |
---|
| 87 | /// [3]: m_dDG (difference in geometry over matching) |
---|
| 88 | /// @sa EvaluateDistance |
---|
| 89 | double m_adFactors[4]; |
---|
[349] | 90 | |
---|
[605] | 91 | //for Zfixed = 1, the "z" (vertical) coordinates are not taken into account during PCA alignment |
---|
[872] | 92 | paInt fixedZaxis; |
---|
[606] | 93 | |
---|
[605] | 94 | //Controls the depth of fuzzy neighbourhood |
---|
[606] | 95 | int fuzzyDepth; |
---|
[872] | 96 | bool isFuzzy; |
---|
[869] | 97 | |
---|
[817] | 98 | //For wMDS = 1 weighted MDS with vertex degrees as weights is used for the alignment. |
---|
[872] | 99 | paInt wMDS; |
---|
[349] | 100 | |
---|
[869] | 101 | //For saveMatching = 1 the best matching found will be saved. |
---|
[872] | 102 | bool saveMatching; |
---|
[869] | 103 | |
---|
[606] | 104 | /// Interface to local parameters |
---|
| 105 | Param localpar; |
---|
[349] | 106 | |
---|
| 107 | protected: |
---|
| 108 | |
---|
[606] | 109 | /// Between these genotypes distance is evaluated. |
---|
| 110 | const Geno *m_Gen[2]; |
---|
[349] | 111 | |
---|
[606] | 112 | /// These models will be created to get the information about creatures |
---|
| 113 | /// from their genotypes. |
---|
| 114 | Model *m_Mod[2]; |
---|
[349] | 115 | |
---|
[606] | 116 | /// Index (0 or 1) of the smaler creature (in the meaning of parts). |
---|
| 117 | /// Index of the bigger one is (1-m_iSmaller). |
---|
| 118 | int m_iSmaller; |
---|
[349] | 119 | |
---|
[606] | 120 | /// Number of parts of two creatures (index the same as for m_Mod). |
---|
| 121 | int m_aiPartCount[2]; |
---|
[349] | 122 | |
---|
[606] | 123 | /// Difference between number of parts in organisms |
---|
| 124 | int m_iDV; |
---|
[349] | 125 | |
---|
[606] | 126 | /// Sum of absolute values of differences between matched part degrees |
---|
| 127 | int m_iDD; |
---|
[349] | 128 | |
---|
[606] | 129 | /// Sum of absolute values of differences between matched part |
---|
| 130 | /// in neurons number. |
---|
| 131 | int m_iDN; |
---|
| 132 | //2 matrices of neighbourhood of parts - one for each genotype |
---|
[349] | 133 | |
---|
[606] | 134 | /// Sum of Euclidean distances between matched parts |
---|
| 135 | /// Unmatched Parts have the distance measured to (0,0,0) (the middle of |
---|
| 136 | /// an organism) |
---|
| 137 | double m_dDG; |
---|
[349] | 138 | |
---|
[606] | 139 | /// Object that holds the matching of Parts. |
---|
| 140 | // It is not clear now whether the matching function is |
---|
| 141 | // created for orginal indices of Parts, or for sorted Parts |
---|
| 142 | // Most probably it is for sorted Parts. |
---|
| 143 | SimilMatching *m_pMatching; |
---|
[349] | 144 | |
---|
[606] | 145 | /// Type of 4 ints - describing one Part of the creature in |
---|
| 146 | /// its sorted table of degrees |
---|
| 147 | /// TDN[0] - original index of creature's Part (that is "i" from GetPart(i)) |
---|
| 148 | /// TDN[1] - degree (number of adjacent joints) of one Part |
---|
| 149 | /// TDN[2] - number of NeuroConnections and Neurons belonging to one Part |
---|
| 150 | /// TDN[3] - number of Neurons of the Part |
---|
| 151 | /// TDN[4] - fuzzy degree |
---|
| 152 | typedef int TDN[5]; |
---|
[349] | 153 | |
---|
[606] | 154 | /** 2 arrays holding information about compared organisms (one for each |
---|
| 155 | creature) of degree and neuro info for Parts. |
---|
| 156 | Index corresponds to the one in m_Mod |
---|
| 157 | m_aDegrees[i][j] is a TDN of the j-th Part of the i-th creature in m_Mod |
---|
| 158 | */ |
---|
| 159 | TDN *m_aDegrees[2]; |
---|
[349] | 160 | |
---|
[606] | 161 | //std::pair<TDN, double> *m_aDegrees[2]; |
---|
| 162 | /// Holds information on all on-joint neurons. Only TDN[3] and TDN[4] |
---|
| 163 | /// are important (original index and degree are not important). |
---|
| 164 | TDN m_aOnJoint[2]; |
---|
[349] | 165 | |
---|
[606] | 166 | /// Holds information on all neurons that are not placed neither on |
---|
| 167 | /// a joint nor on a part. Only TDN[3] and TDN[4] |
---|
| 168 | /// are important (original index and degree are not important). |
---|
| 169 | TDN m_aAnywhere[2]; |
---|
[349] | 170 | |
---|
[606] | 171 | //array of parts neighbourhood |
---|
| 172 | int **m_Neighbours[2]; |
---|
| 173 | //array of "fuzzy neigbourhood" for each of organisms. Dimensions: parts_num x fuzzyDepth |
---|
| 174 | float **m_fuzzyNeighb[2]; |
---|
[349] | 175 | |
---|
[606] | 176 | /// Two arrays of points which hold information about positions of Parts |
---|
| 177 | /// of both of the compared organisms. |
---|
| 178 | /// Matching methods which do not use geometry (MatchPartsOld |
---|
| 179 | /// and MatchPartsNew) simply copy these positions from models. The only |
---|
| 180 | /// matching method which uses geometry (MatchPartsNewGeometry) makes |
---|
| 181 | /// use of these arrays extensively. |
---|
| 182 | /// At m_aPositions[ iModel ][ iOriginalPart ] there is a Pt3D of |
---|
| 183 | /// a Part with index iOriginalPart of the model iModel. |
---|
| 184 | /// iOriginalPart means that this index is the original index of a Part, |
---|
| 185 | /// (before sorting). |
---|
| 186 | Pt3D *m_aPositions[2]; |
---|
[349] | 187 | |
---|
[606] | 188 | /// Number of weights in the function which evaluates distance. |
---|
| 189 | static const int iNOFactors; |
---|
[349] | 190 | |
---|
| 191 | }; |
---|
| 192 | |
---|
| 193 | #endif |
---|