source: cpp/frams/model/similarity/simil_match.cpp @ 934

Last change on this file since 934 was 869, checked in by Maciej Komosinski, 6 years ago

Added another, improved way of calculating dissimilarity of two creatures/models. Details and comparisons in https://doi.org/10.1007/978-3-030-16692-2_8

  • Property svn:eol-style set to native
File size: 7.0 KB
RevLine 
[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#include <assert.h>
6#include <stdio.h>
7#include "simil_match.h"
8
9/** Creates an empty matching for two objects of specified size.
[869]10                @param Obj0Size Size of the first object. Must be positive.
11                @param Obj1Size Size of the second object. Must be positive.
[349]12 */
13SimilMatching::SimilMatching(int Obj0Size, int Obj1Size)
14{
[869]15        // assure that sizes of objects are positive
16        assert(Obj0Size > 0);
17        assert(Obj1Size > 0);
[349]18
[869]19        // create necessary vectors
20        m_apvMatched[0] = new std::vector<int>(Obj0Size);
21        m_apvMatched[1] = new std::vector<int>(Obj1Size);
[349]22
[869]23        // assure that vectors are created
24        assert(m_apvMatched[0] != NULL);
25        assert(m_apvMatched[1] != NULL);
[349]26
[869]27        // fill vectors with "unmatched" indicator
[361]28        for (unsigned int i = 0; i < m_apvMatched[0]->size(); i++)
[869]29        {
30                m_apvMatched[0]->operator[](i) = -1;
31        }
[361]32        for (unsigned int i = 0; i < m_apvMatched[1]->size(); i++)
[869]33        {
34                m_apvMatched[1]->operator[](i) = -1;
35        }
[349]36}
37
38/** A copying constructor.
[869]39                @param Source The object to be copied.
[349]40 */
41SimilMatching::SimilMatching(const SimilMatching &Source)
42{
[869]43        // copy the vectors of the actual matching
44        m_apvMatched[0] = new std::vector<int>(*(Source.m_apvMatched[0]));
45        m_apvMatched[1] = new std::vector<int>(*(Source.m_apvMatched[1]));
[349]46
[869]47        // assure that vectors are created
48        assert(m_apvMatched[0] != NULL);
49        assert(m_apvMatched[1] != NULL);
[349]50}
51
52/** Destroys a matching object.
53 */
54SimilMatching::~SimilMatching()
55{
[869]56        // delete vectors of matching
57        delete m_apvMatched[0];
58        delete m_apvMatched[1];
[349]59}
60
61/** Gets size of the specified object.
[869]62                @param Index of an object (must be 0 or 1).
63                @return Size of the object (in elements).
[349]64 */
65int SimilMatching::GetObjectSize(int Obj)
66{
[869]67        // check parameter
68        assert((Obj == 0) || (Obj == 1));
[349]69
[869]70        // return the result
71        return m_apvMatched[Obj]->size();
[349]72}
73
74/** Matches elements given by indices in the given objects.
[869]75                @param Obj0 Index of the first object. Must be 0 or 1.
76                @param index0 Index of element in the first object. Must be a valid index
77                ( >= 0 and < size of the object).
78                @param Obj1 Index of the second object. Must be 0 or 1 and different from Obj0.
79                @param Index1 index of element in the second object. Must be a valid index
80                ( >= 0 and < size of the object).
[349]81
82 */
83void SimilMatching::Match(int Obj0, int Index0, int Obj1, int Index1)
84{
[869]85        // check parameters of object 0
86        assert((Obj0 == 0) || (Obj0 == 1));
87        assert((Index0 >= 0) && (Index0 < (int)m_apvMatched[Obj0]->size()));
88        // check parameters of object 1
89        assert(((Obj1 == 0) || (Obj1 == 1)) && (Obj0 != Obj1));
90        assert((Index1 >= 0) && (Index1 < (int)m_apvMatched[Obj1]->size()));
[349]91
[869]92        // match given elements
93        // matching_Obj0(Index0) = Index1
94        m_apvMatched[Obj0]->operator[](Index0) = Index1;
95        // matching_Obj1(Index1) = Index0
96        m_apvMatched[Obj1]->operator[](Index1) = Index0;
[349]97}
98
99/** Checks if the given element in the given object is already matched.
[869]100                @param Obj Index of an object (must be 0 or 1).
101                @param Index Index of an element in the given object. Must be a valid index
102                ( >=0 and < size of the object).
103                @return true if the given element is matched, false otherwise.
[349]104 */
105bool SimilMatching::IsMatched(int Obj, int Index)
106{
[869]107        // check parameters
108        assert((Obj == 0) || (Obj == 1));
109        assert((Index >= 0) && (Index < (int)m_apvMatched[Obj]->size()));
[349]110
[869]111        // check if the element is matched
112        if (m_apvMatched[Obj]->operator[](Index) >= 0)
113        {
114                return true;
115        }
116        else
117        {
118                return false;
119        }
[349]120}
121
122/** Gets index of the element thet is matched in the other object withe the element given
[869]123                by parameters.
124                @param Obj Index of an object (must be 0 or 1).
125                @param Index Index of checked element in the given object.
126                @return Index of the element (in the other organism) that is matched with the given
127                element. WARNING! If the given element is not matched, the result may be smaller than 0
128                (check IsMatched() before using GetMatchedIndex()).
[349]129 */
130int SimilMatching::GetMatchedIndex(int Obj, int Index)
131{
[869]132        // check parameters
133        assert((Obj == 0) || (Obj == 1));
134        assert((Index >= 0) && (Index < (int)m_apvMatched[Obj]->size()));
[349]135
[869]136        // return the index of the matched element
137        return m_apvMatched[Obj]->operator[](Index);
[349]138}
139
[869]140/** Checks if the matching is already full, i.e. if the smaller object already has all its
141                elements matched.
142                @return true if matching is full, false otherwise.
[349]143 */
144bool SimilMatching::IsFull()
145{
[869]146        // assume that the matching is full
147        bool bResult = true;
148        // index of the smallest object
149        int nObj;
[349]150
[869]151        // find the smallest object (its index)
152        if (m_apvMatched[0]->size() < m_apvMatched[1]->size())
153        {
154                nObj = 0;
155        }
156        else
157        {
158                nObj = 1;
159        }
[349]160
[869]161        // check if all elements of the smallest object are matched
[361]162        for (unsigned int nElem = 0; nElem < m_apvMatched[nObj]->size(); nElem++)
[869]163        {
164                if (m_apvMatched[nObj]->operator[](nElem) < 0)
165                {
166                        // if any element is not matched, the result is false
167                        bResult = false;
168                        break;
169                }
170        }
[349]171
[869]172        // return the result
173        return bResult;
[349]174}
175
176/** Checks if the matching is empty (i.e. none of elements is matched).
[869]177        @return true if matching is empty, otherwise - false.
[349]178 */
179bool SimilMatching::IsEmpty()
180{
[869]181        // result - assume that matching is empty
182        bool bResult = true;
[349]183
[869]184        // matching is empty if either of objects has only unmatched elements
185        // so it may be first object
186        int nObj = 0;
[361]187        for (unsigned int nElem = 0; nElem < m_apvMatched[nObj]->size(); nElem++)
[869]188        {
189                if (m_apvMatched[nObj]->operator[](nElem) >= 0)
190                {
191                        // if any element of the object is matched (unmatched objects have (-1))
192                        bResult = false;
193                        break;
194                }
195        }
[349]196
[869]197        // return the result from the loop
198        return bResult;
[349]199}
200
201/** Makes the matching completely empty. After a call to this method IsEmpty() should return true.
202 */
203void SimilMatching::Empty()
204{
[869]205        for (int iObj = 0; iObj < 2; iObj++) // a counter of objects
206        {
207                // for each object in the matching
[361]208                for (unsigned int iElem = 0; iElem < m_apvMatched[iObj]->size(); iElem++) // a counter of objects' elements
[869]209                {
210                        // for each element iElem for the object iObj
211                        // set it as unmatched (marker: -1)
212                        m_apvMatched[iObj]->operator[](iElem) = -1;
213                }
214        }
[349]215
[869]216        // the exit condition
217        assert(IsEmpty() == true);
[349]218}
219
220/** Prints the current state of the matching
221 */
222void SimilMatching::PrintMatching()
223{
[869]224        int nBigger;
[349]225
[869]226        // check which object is bigger
227        if (m_apvMatched[0]->size() >= m_apvMatched[1]->size())
228        {
229                nBigger = 0;
230        }
231        else
232        {
233                nBigger = 1;
234        }
[349]235
[869]236        // print first line - indices of objects
237        printf("[ ");
[361]238        for (unsigned int i = 0; i < m_apvMatched[nBigger]->size(); i++)
[869]239        {
240                printf("%2d ", i);
241        }
242        printf("]\n");
[349]243
[869]244        // print second line and third - indices of elements matched with elements of the objects
245        for (int nObj = 0; nObj < 2; nObj++)
246        {
247                // for both objects - print out lines of matched elements
248                printf("[ ");
[361]249                for (unsigned int i = 0; i < m_apvMatched[nObj]->size(); i++)
[869]250                {
251                        if (IsMatched(nObj, i))
252                        {
253                                // if the element is matched - print the index
254                                printf("%2d ", GetMatchedIndex(nObj, i));
255                        }
256                        else
257                        {
258                                // if the element is not matched - print "X"
259                                printf(" X ");
260                        }
261                }
262                printf("]\n");
263        }
[349]264}
Note: See TracBrowser for help on using the repository browser.