Changeset 877


Ignore:
Timestamp:
05/16/19 22:08:18 (5 years ago)
Author:
Maciej Komosinski
Message:

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

Location:
cpp/frams/model/similarity
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/model/similarity/simil_model.cpp

    r872 r877  
    8686double ModelSimil::EvaluateDistanceGreedy(const Geno *G0, const Geno *G1)
    8787{
    88         double dResult = 0;
     88        double dResult = 0.0;
    8989
    9090        m_Gen[0] = G0;
    9191        m_Gen[1] = G1;
    9292
    93         // check whether pointers are not NULL
    94         if (m_Gen[0] == NULL || m_Gen[1] == NULL)
    95         {
    96                 logPrintf("ModelSimil", "EvaluateDistanceGreedy", LOG_ERROR, "NULL genotype pointer(s)");
     93        // create models of objects to compare
     94        m_Mod[0] = newModel(m_Gen[0]);
     95        m_Mod[1] = newModel(m_Gen[1]);
     96
     97        if (m_Mod[0] == NULL || m_Mod[1] == NULL)
    9798                return 0.0;
    98         }
    99         // create models of objects to compare
    100         m_Mod[0] = new Model(*(m_Gen[0]));
    101         m_Mod[1] = new Model(*(m_Gen[1]));
    102 
    103         // validate models
    104         if (m_Mod[0] == NULL || m_Mod[1] == NULL || !(m_Mod[0]->isValid()) || !(m_Mod[1]->isValid()))
    105         {
    106                 logPrintf("ModelSimil", "EvaluateDistanceGreedy", LOG_ERROR, "NULL or invalid model pointer(s)");
    107                 return 0.0;
    108         }
    10999
    110100        // difference in the number of vertices (Parts) - positive
     
    537527}
    538528
     529Model* ModelSimil::newModel(const Geno *g)
     530{
     531        if (g == NULL)
     532        {
     533                logPrintf("ModelSimil", "newModel", LOG_ERROR, "NULL genotype pointer");
     534                return NULL;
     535        }
     536        Model *m = new Model(*g);
     537        if (!m->isValid())
     538        {
     539                logPrintf("ModelSimil", "newModel", LOG_ERROR, "Invalid model for the genotype of '%s'", g->getName().c_str());
     540                delete m;
     541                return NULL;
     542        }
     543        return m;
     544}
     545
     546
    539547/** Creates arrays holding information about organisms' Parts (m_aDegrees) andm_Neigh
    540548        fills them with initial data (original indices and zeros).
     
    542550        - Models (m_Mod) are created and available.
    543551        */
    544 int ModelSimil::CreatePartInfoTables()
     552int ModelSimil::ModelSimil::CreatePartInfoTables()
    545553{
    546554        // check assumptions about models
     
    20702078double ModelSimil::EvaluateDistanceHungarian(const Geno *G0, const Geno *G1)
    20712079{
    2072         double dResult = 0;
     2080        double dResult = 0.0;
    20732081
    20742082        m_Gen[0] = G0;
    20752083        m_Gen[1] = G1;
    20762084
    2077         // check whether pointers are not NULL
    2078         if (m_Gen[0] == NULL || m_Gen[1] == NULL)
    2079         {
    2080                 logPrintf("ModelSimil", "EvaluateDistanceHungarian", LOG_ERROR, "NULL genotype pointer(s)");
     2085        // create models of objects to compare
     2086        m_Mod[0] = newModel(m_Gen[0]);
     2087        m_Mod[1] = newModel(m_Gen[1]);
     2088
     2089        if (m_Mod[0] == NULL || m_Mod[1] == NULL)
    20812090                return 0.0;
    2082         }
    2083         // create models of objects to compare
    2084         m_Mod[0] = new Model(*(m_Gen[0]));
    2085         m_Mod[1] = new Model(*(m_Gen[1]));
    2086 
    2087         // validate models
    2088         if (m_Mod[0] == NULL || m_Mod[1] == NULL || !(m_Mod[0]->isValid()) || !(m_Mod[1]->isValid()))
    2089         {
    2090                 logPrintf("ModelSimil", "EvaluateDistanceHungarian", LOG_ERROR, "NULL or invalid model pointer(s)");
    2091                 return 0.0;
    2092         }
    20932091
    20942092        //Get information about vertex degrees, neurons and 3D location
    20952093        if (!CreatePartInfoTables())
    2096                 return 0;
     2094                return 0.0;
    20972095        if (!CountPartDegrees())
    2098                 return 0;
     2096                return 0.0;
    20992097        if (!GetPartPositions())
    2100                 return 0;
     2098                return 0.0;
    21012099        if (!CountPartNeurons())
    2102                 return 0;
     2100                return 0.0;
    21032101
    21042102        m_iSmaller = m_Mod[0]->getPartCount() <= m_Mod[1]->getPartCount() ? 0 : 1;
     
    21162114                if (!ComputePartsPositionsBySVD())
    21172115                {
    2118                         return 0;
     2116                        return 0.0;
    21192117                }
    21202118
     
    21772175                        std::copy(minAssignment.begin(), minAssignment.end(), assignment);
    21782176        }
    2179 
    21802177        else
    21812178        {
  • cpp/frams/model/similarity/simil_model.h

    r872 r877  
    6565        void FuzzyOrder();
    6666
     67        static Model* newModel(const Geno *g);
    6768        int CreatePartInfoTables();
    6869        void _PrintDegrees(int i);
Note: See TracChangeset for help on using the changeset viewer.