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