source: cpp/frams/model/similarity/simil_model.h @ 872

Last change on this file since 872 was 872, checked in by Maciej Komosinski, 5 years ago

Fixed error messages, changed int to paInt (for 32/64-bit safety), changed two int variables to bool as they should be

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