vector<ClusterDistribution> getClustersDistribution( list<BehaviourSequence> *data, vector<SimpleSkeleton> clusters){
 vector<ClusterDistribution> clusterDist;
 clusterDist.reserve(numClusters);
 
 //first create the clustDistribution vector from the clusters
 for(int i=0;i<numClusters;i++){
     ClusterDistribution newDist(clusters[i]);     
     clusterDist.push_back(newDist);
 }
 
 list<BehaviourSequence>::iterator itBeh;
 for (itBeh = data->begin(); itBeh != data->end(); itBeh++) {
 
     list<SimpleSkeleton>::iterator itSkel;
     for (itSkel = itBeh->skeletons.begin(); itSkel != itBeh->skeletons.end(); itSkel++) {         
         clusterDist[itSkel->idCluster].addObs(*itSkel);         
     }
 }
 
 //finally we compute the standar deviation
 for(int i=0;i<numClusters;i++){
     clusterDist[i].calcStandarDeviation();
 }
 
 return clusterDist;
}
Exemplo n.º 2
0
    GRA_tpCondRet GRA_BuscarCaminho( GRA_tppGrafo pGrafo , int idVerticeOrigem, int idVerticeDestino, LIS_tppLista * pLista ) {
        tpVertice * v = NULL;
        tpVertice * u = NULL;         
        tpVertice * origem1 = NULL;
        tpVertice * origem2 = NULL;
        int lenV = 0;
        LIS_tppLista Q = NULL; //FILA
        LIS_tppLista arestas = NULL;
        LIS_tppLista retorno = NULL;
        int t;
        int len = 0;
        int achou = 0;
        int ok = 0;
        int i,j,in;
        int lenD;
        int alt = 0;
        int* visitados = NULL; // Vetor de vertices visitados
        int* vizinhos = NULL;
        int* idAux = NULL;
        Dist** dists = NULL;
        Dist* dist = NULL; //aux;
        Dist* currDist = NULL;

        lenD = 1;

        v = get_by_id(pGrafo, idVerticeOrigem);
        u = get_by_id(pGrafo, idVerticeDestino);
        if(v == NULL || u == NULL) {
            return GRA_CondRetNaoEhVertice; 
        }

        origem1 = ObterOrigem(pGrafo, v);
        origem2 = ObterOrigem(pGrafo, u);
        if (origem1 != origem2) {
            return GRA_CondRetNaoEhConexo;
        }//Else: É conexo, devia retornar Ok.
        

        for (;;) {
            dists = (Dist**)calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1, sizeof(Dist*));
            if (dists == NULL) {break;}
            dists[0] = newDist(idVerticeOrigem, 0);

            retorno = LIS_CriarLista(free);
            if (retorno == NULL) { break; }
            else if (v == u) {
                if( LIS_InserirElementoApos(retorno, newInt(idVerticeOrigem)) == LIS_CondRetOK) {
                    *pLista = retorno;
                    return GRA_CondRetOK;
                } else {
                    break;
                }
            }

            visitados = (int*) calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1,sizeof(int));
            if (visitados == NULL) { break; }

            Q = LIS_CriarLista(free);
            if (Q == NULL) { break; }

            visitados[0] = idVerticeOrigem;
            lenV = 1;
            if (LIS_InserirElementoApos(Q, newInt(idVerticeOrigem)) != LIS_CondRetOK) { break;} //enque

            ok = 1;
            break;
        }
        if (!ok) {
            free(dists);
            LIS_DestruirLista(retorno);
            free(visitados);
            LIS_DestruirLista(Q);
            return GRA_CondRetFaltouMemoria;
        }

        while (LIS_NumeroDeElementos(Q) > 0) {
            //dequeue
            LIS_IrInicioLista(Q);
            t = getInt(LIS_ObterValor(Q));
            LIS_ExcluirElemento(Q);

            //Iterar sobre vizinhos
            GRA_ObterVizinhos(pGrafo, t, &arestas);
            vizinhos = converteListaParaVetorDeInteiros(arestas, &len);
            LIS_DestruirLista(arestas);
            arestas = NULL;

            currDist = getDist(dists, t);
            if(!currDist) {
                return GRA_CondRetFaltouMemoria;
            } else {
            }
            alt = currDist->dist + 1;
            for (i=0; i < len; i++) {
                
                in = 0;
                for (j=0; j < lenV; j++) {
                    if (visitados[j] == vizinhos[i]) {
                        in = 1;
                    }
                }
                if (!in) {
                    dist = getDist(dists, vizinhos[i]);
                    if (dist == NULL) { //infinity
                        dists[lenD] = newDist(vizinhos[i], alt);
                        dists[lenD]->prev = currDist;
                        dist = dists[lenD];
                        lenD++;
                    } else if (alt < dist->dist) {
                        dist->dist = alt;
                        dist->prev = currDist;
                    }
                    if (idVerticeDestino == vizinhos[i]) {
                        currDist = dist;
                        achou = 1;
                    }
                    visitados[lenV] = vizinhos[i];
                    lenV++;
                    LIS_InserirElementoAntes(Q, newInt(vizinhos[i]));
                }
            }
            free(vizinhos);
            if (achou) {
                currDist = dist;
                break;
            }
            if(lenV == LIS_NumeroDeElementos(pGrafo->vertices)) {
                break;
            }
        }
        
        if (achou) {
            //printf("\n");
            // for(i=0; i < lenD; i++) {
            //     printf("endr: %p, id: %d, dist: %d, prev: %p \n", *(dists+i), dists[i]->id, dists[i]->dist, dists[i]->prev);
            // }
            while (currDist) {
                LIS_InserirElementoAntes(retorno, newInt(currDist->id));
                currDist  = currDist->prev;
            }

        }

        

        


        //Limpando a memória        
        for (i=0; i < lenD; i++) {
            free(dists[i]);
        }
        free(dists);
        free(visitados);
        LIS_DestruirLista(Q);
        *pLista = retorno;
        return GRA_CondRetOK;
    }
Exemplo n.º 3
0
BinnedED
BinnedEDShrinker::ShrinkDist(const BinnedED& dist_) const{

    // No buffer no problem. FIXME: what about if all the values are zero?
    if (!fBuffers.size())
        return dist_;
    
    size_t nDims = dist_.GetNDims();

    // FIXME Add a check to see if the non zero entries of fBuffers are in the pdf and give warning

    // 1. Build new axes. ShrinkPdf method just makes a copy if buffer size is zero
    AxisCollection newAxes;
    const std::vector<size_t> distDataIndices = dist_.GetObservables().GetIndices();
    size_t dataIndex = 0;
    
    for(size_t i = 0; i < nDims; i++){
        dataIndex = distDataIndices.at(i);
        if (!fBuffers.count(dataIndex))
            newAxes.AddAxis(dist_.GetAxes().GetAxis(i));
        else
            newAxes.AddAxis(ShrinkAxis(dist_.GetAxes().GetAxis(i), 
                                       fBuffers.at(dataIndex).first,  
                                       fBuffers.at(dataIndex).second));
    }

    // 2. Initialise the new pdf with same data rep
    BinnedED newDist(dist_.GetName() + "_shrunk", newAxes);
    newDist.SetObservables(dist_.GetObservables());

    // 3. Fill the axes
    std::vector<size_t> newIndices(dist_.GetNDims());  // same as old, just corrected for overflow
    int   offsetIndex = 0; // note taking difference of two unsigneds
    size_t newBin = 0;     //  will loop over dims and use this to assign bin # corrected for overflow

    const AxisCollection& axes = dist_.GetAxes();
    double content = 0;
    // bin by bin of old pdf
    for(size_t i = 0; i < dist_.GetNBins(); i++){
        content = dist_.GetBinContent(i);
        if(!content) // no content no problem
            continue;

        // work out the index of this bin in the new shrunk pdf. 
        for(size_t j = 0; j < nDims; j++){
            offsetIndex = axes.UnflattenIndex(i, j);            // the index in old pdf
            if (fBuffers.count(distDataIndices.at(j)))          // offset by lower buffer if nonzero
                offsetIndex -= fBuffers.at(distDataIndices.at(j)).first;

            // Correct the ones that fall in the buffer regions
            // bins in the lower buffer have negative index. Put in first bin in fit region or ignore
            if (offsetIndex < 0){
                offsetIndex = 0;
                if(!fUsingOverflows)
                    content = 0;

            }

            // bins in the upper buffer have i > number of bins in axis j. Do the same
            if (offsetIndex >= newAxes.GetAxis(j).GetNBins()){
                offsetIndex = newAxes.GetAxis(j).GetNBins() - 1;

                if (!fUsingOverflows)
                    content = 0;
            }

            newIndices[j] = offsetIndex;
        }
        // Fill 
        newBin = newAxes.FlattenIndices(newIndices);
        newDist.AddBinContent(newBin, content);
    }

    return newDist;
}