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

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

Introduced a shared function to avoid code duplication; more informative error message; frees memory in case of error

  • 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        static Model* newModel(const Geno *g);
68        int CreatePartInfoTables();
69        void _PrintDegrees(int i);
70        void _PrintArray(int *array, int base, int size);
71        void _PrintNeighbourhood(int i);
72        void _PrintFuzzyNeighbourhood(int i);
73        void _PrintArrayDouble(double *array, int base, int size);
74        int CountPartsDistance();
75
76
77public:
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
80        paInt matching_method;
81
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];
90
91        //for Zfixed = 1, the "z" (vertical) coordinates are not taken into account during PCA alignment
92        paInt fixedZaxis;
93
94        //Controls the depth of fuzzy neighbourhood
95        int fuzzyDepth;
96        bool isFuzzy;
97
98        //For wMDS = 1 weighted MDS with vertex degrees as weights is used for the alignment.
99        paInt wMDS;
100
101        //For saveMatching = 1 the best matching found will be saved.
102        bool saveMatching;
103
104        /// Interface to local parameters
105        Param localpar;
106
107protected:
108
109        /// Between these genotypes distance is evaluated.
110        const Geno *m_Gen[2];
111
112        /// These models will be created to get the information about creatures
113        /// from their genotypes.
114        Model *m_Mod[2];
115
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;
119
120        /// Number of parts of two creatures (index the same as for m_Mod).
121        int m_aiPartCount[2];
122
123        /// Difference between number of parts in organisms
124        int m_iDV;
125
126        /// Sum of absolute values of differences between matched part degrees
127        int m_iDD;
128
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
133
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;
138
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;
144
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];
153
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];
160
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];
165
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];
170
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];
175
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];
187
188        /// Number of weights in the function which evaluates distance.
189        static const int iNOFactors;
190
191};
192
193#endif
Note: See TracBrowser for help on using the repository browser.