Ejemplo n.º 1
0
std::vector<float> DestinNetworkAlt::rescaleRecursiveUp(int srcLayer, std::vector<float> selCen, int dstLayer)
{
    if(srcLayer == dstLayer)
    {
        //printFloatVector("---Result is:\n", selCen);
        return selCen;
        
    }
    else
    {
        Node * currNode = getNode(srcLayer, 0, 0);
        Node * parentNode = getNode(srcLayer+1, 0, 0);
        // Contain the final result
        std::vector<float> newSelCen;
        // Contain the 4 new made children
        std::vector<std::vector<float> > extendCen;

        // Display all centroids on source layer
        printFloatCentroids(srcLayer);
        // Display the selected centroid
        printFloatVector("---The selected centroid is:\n", selCen);

        if(srcLayer == 0)
        {
            // Generate the index for every quarter, specific for layer 0
            std::vector<std::vector<int> > vecIdx;
            // 0,1,4,5
            // 2,3,6,7
            // 8,9,12,13
            // 10,11,14,15
            for(int i=0; i<2; ++i)
            {
                for(int j=0; j<=2; j+=2)
                {
                    std::vector<int> vecTemp;
                    vecTemp.push_back(i*2*4 + j);
                    vecTemp.push_back(i*2*4 + j + 1);
                    vecTemp.push_back(i*2*4 + j + 4);
                    vecTemp.push_back(i*2*4 + j + 4 + 1);
                    vecIdx.push_back(vecTemp);
                }
            }

            // Generate the 4 new observations
            for(int i=0; i<vecIdx.size(); ++i)
            {
                std::vector<float> quaCen;

                for(int j=0; j<vecIdx[i].size(); ++j)
                {
                    for(int k=0; k<4; ++k)
                    {
                        quaCen.push_back(selCen[vecIdx[i][j]]);
                    }
                }
                for(int j=0; j<currNode->nb; ++j)
                {
                    quaCen.push_back(1/(float)currNode->nb);
                }
                for(int j=0; j<currNode->np; ++j)
                {
                    quaCen.push_back(1/(float)currNode->np);
                }
                extendCen.push_back(quaCen);
            }

            // Display the 4 new observations
            printf("---The generated 4 new observations:\n");
            for(int i=0; i<extendCen.size(); ++i)
            {
                for(int j=0; j<currNode->ni; ++j)
                {
                    printf("%f  ", extendCen[i][j]);
                }
                printf("\n");
                for(int j=0; j<currNode->nb; ++j)
                {
                    printf("%f  ", extendCen[i][j + currNode->ni]);
                }
                printf("\n");
                for(int j=0; j<currNode->np; ++j)
                {
                    printf("%f  ", extendCen[i][j + currNode->ni + currNode->nb]);
                }
                printf("\n");
                if(i != extendCen.size()-1)
                {
                    printf("---\n");
                }
            }
            printf("\n\n");/**/

            //normalizeChildrenPart(newSelCen, extendCen.size()*currNode->nb);
        }
        else
        {
            // Generate the 4 new observations
            int numParts = 4;
            Node * childNode = getNode(srcLayer-1, 0, 0);
            for(int i=0; i<numParts; ++i)
            {
                std::vector<float> quaCen;
                for(int j=0; j<numParts; ++j)
                {
                    for(int k=0; k<childNode->nb; ++k)
                    {
                        quaCen.push_back(selCen[i*childNode->nb + k]);
                    }
                }
                for(int j=0; j<currNode->nb; ++j)
                {
                    quaCen.push_back( 1/(float)currNode->nb );
                }
                for(int j=0; j<currNode->np; ++j)
                {
                    quaCen.push_back( 1/(float)currNode->np );
                }

                extendCen.push_back(quaCen);

            }
        }


        // Use the 4 new observations to construct the belief vector for layer 1
        // 4 children part
        for(int i=0; i<extendCen.size(); ++i)
        {
            float delta;
            float sumMal;
            for(int j=0; j<currNode->nb; ++j)
            {
                sumMal = 0;

                // TODO: decrease from 'ns' to 'ni';
                for(int k=0; k<currNode->ni; ++k)
                //for(int k=0; k<currNode->ns; ++k)
                {
                    delta = extendCen[i][k] - currNode->mu[j][k];

                    delta *= delta;

                    // Assume starv is 1
                    delta *= 1;

                    sumMal += delta/destin->uf_sigma[srcLayer][j][k];
                }

                sumMal = sqrt(sumMal);

                //newSelCen.push_back( ( sumMal < EPSILON ) ? MAX_INTERMEDIATE_BELIEF : (1.0 / sumMal) );
                newSelCen.push_back( 1 / (1+sumMal) );

                // Because the faked observations are too close to the centroids, the sumMal is
                // less than 1. Then the belief will be larger than 1 according to 1.0/sumMal.
                // In the original DeSTIN, the belief vector will make centroids 'move' even the
                // belief is larger than 1. But what we get directly is not centroid, but belief.
            }
        }
        // current part
        for(int i=0; i<parentNode->nb; ++i)
        {
            newSelCen.push_back( 1/(float)parentNode->nb );
        }
        // parent part
        for(int i=0; i<parentNode->np; ++i)
        {
            newSelCen.push_back( 1/(float)parentNode->np );
        }

        return rescaleRecursiveUp(srcLayer+1, newSelCen, dstLayer);
    }
}
Ejemplo n.º 2
0
void DestinNetworkAlt::rescaleRecursiveDown(int srcLayer, std::vector<float> selCen, int dstLayer)
{
    if(srcLayer == dstLayer)
    {
        printFloatVector("---Result is:\n", selCen);
        return;
    }
    else
    {
        Node * childNode = getNode(srcLayer-1, 0, 0);

        std::vector<float> newSelCen;
        std::vector<float> normSelCen;

        // Display all centroids on source layer
        printFloatCentroids(srcLayer);
        // Display the selected centroid
        printFloatVector("---The selected centroid is:\n", selCen);

        if(srcLayer == 1)
        {
            // Use every quarter as a single lower level centroid, then downsample
            //int level0 = 0;
            //Node * childNode = getNode(level0, 0, 0);

            // Normalize for every quarter
            // This is inspired by 'cent_image_gen.c';
            for(int i=0; i<4; ++i)
            {
                float sum = 0.0;
                for(int j=0; j<childNode->nb; ++j)
                {
                    sum += selCen[i*childNode->nb + j];
                }
                for(int j=0; j<childNode->nb; ++j)
                {
                    normSelCen.push_back(selCen[i*childNode->nb + j] / sum);
                }
            }

            // Downsampling method: pickping left-up one
            std::vector<int> vecIdx;
            vecIdx.push_back(0);
            vecIdx.push_back(2);
            vecIdx.push_back(8);
            vecIdx.push_back(10);

            for(int i=0; i<4; ++i)
            {
                std::vector<float> tempCen(childNode->ni, 0);
                for(int j=0; j<childNode->nb; ++j)
                {
                    for(int k=j*childNode->ns; k<j*childNode->ns+childNode->ni; ++k)
                    {
                        //
                        tempCen[k-j*childNode->ns] += normSelCen[i*childNode->nb+j] * childNode->mu[j][k-j*childNode->ns];
                    }
                }
                for(int j=0; j<vecIdx.size(); ++j)
                {
                    newSelCen.push_back(tempCen[vecIdx[j]]);
                }
            }
        }
        else
        {
            // Normalize for every quarter
            for(int i=0; i<4; ++i)
            {
                float sum = 0.0;
                for(int j=0; j<childNode->nb; ++j)
                {
                    sum += selCen[i*childNode->nb + j];
                }
                for(int j=0; j<childNode->nb; ++j)
                {
                    normSelCen.push_back(selCen[i*childNode->nb + j] / sum);
                }
            }

            for(int i=0; i<4; ++i)
            {
                std::vector<float> tempCen(childNode->ni, 0);
                for(int j=0; j<childNode->nb; ++j)
                {
                    for(int k=j*childNode->ns; k<j*childNode->ns+childNode->ni; ++k)
                    {
                        //
                        tempCen[k - j*childNode->ns] += normSelCen[i*childNode->nb + j] * childNode->mu[j][k-j*childNode->ns];
                    }
                }

                for(int j=0; j<childNode->ni/4; ++j)
                {
                    newSelCen.push_back(tempCen[j]);
                }
            }
        }

        for(int i=0; i<childNode->nb; ++i)
        {
            newSelCen.push_back(1 / (float)childNode->nb);
        }
        for(int i=0; i<childNode->np; ++i)
        {
            newSelCen.push_back(1 / (float)childNode->np);
        }

        rescaleRecursiveDown(srcLayer-1, newSelCen, dstLayer);
    }
}