Ejemplo n.º 1
0
void visita(Nodo** head)
{
    Nodo* punter=*head;
    Nodo* pp=punter;
    punter->status=2;
    printf("%d ",punter->valnome);

    if(punter->SX!=NULL)
    {
        pp=punter->SX;
    }

    if(punter->SX=!NULL)
    {
        pp=punter->SX;
        visita(pp);
    }

    if(punter->DX!=NULL)
    {
        pp=punter->DX;
        visita(pp);
    }

    return 0;
}
Ejemplo n.º 2
0
void preorden(int nodo){
    if(nodo == -1){
        return;
    }
    visita(nodo);
    preorden(izq[nodo]);
    preorden(der[nodo]);
}
Ejemplo n.º 3
0
Archivo: E.c Proyecto: jpbat/aed2011
void emOrdem (nodePtr leaf)
{
	if (leaf != NULL)
	{
		emOrdem(leaf->left);
		visita (leaf);
		emOrdem(leaf->right);
	}
}
Ejemplo n.º 4
0
int main()
{
    printf("Hello world!\n");
    Nodo* radice=malloc(sizeof(Nodo));
    radice->SX=NULL;
    radice->DX=NULL;
    radice->valnome=0;

    Creanodo(&radice,1);
    Creanodo(&radice,2);
    Creanodo(&radice,3);
    Creanodo(&radice,4);
    Creanodo(&radice,5);
    Creanodo(&radice,6);

    visita(&radice);
    return 0;
}
Ejemplo n.º 5
0
void buscaemprofundidade(grafo *G, int *d, int *f, int *pai){
	int cor[G->vertices], u, tempo;
	no *q;
	aresta *p;
	q=G->inicio;
	while(q!=NULL){
		cor[q->vertice]=0;
		pai[q->vertice]=-1;
		q=q->prox;
	}
	tempo=0;
	q=G->inicio;
	while(q!=NULL){
		if(cor[q->vertice]==0){
			u=q->vertice;
			visita(G,u,d,f,pai,cor,&tempo);
		}
		q=q->prox;
	}
}
Ejemplo n.º 6
0
void visita(grafo *G, int u, int *d, int *f, int *pai, int *cor, int *tempo){
	aresta *p;
	no *q;
	cor[u]=1;
	*tempo=*tempo+1;
	d[u]=*tempo;

	q=buscavertice(G,u);
	p=q->prox_aresta;
	while(p!=NULL){
		if(cor[p->arestas]==0){
			pai[p->arestas]=q->vertice;
			u=p->arestas;
			visita(G,u,d,f,pai,cor,tempo);
		}
		p=p->prox_aresta;
	}
	cor[q->vertice]=2;
	*tempo=*tempo+1;
	f[q->vertice]=*tempo;
}
Ejemplo n.º 7
0
void simet(nodo *pt) {
	if (pt->esq != NULL) simet(pt->esq);
	visita(pt);
	if (pt->dir != NULL) simet(pt->dir);
}
Ejemplo n.º 8
0
int Engine::startSearch()
{
    if (isVerbose())
    {
        Rprintf("Starting...\n");
    }
    double temQa;
    double visit, a, b;
    int itNew = 0, itDev;
    double s1, s, t1, t2, r, pqa, pqa1;
    bool inConstraint = false;
    bool eMini_NotChanged = true;
    double eMiniMarkov = 0;
    int indexNoEminiUpdate = 0;
    int indexTolEminiUpdate = 1000;
    dVec xMiniMarkov(x_.size());

    if (getIsSimpleFunction())
    {
        indexTolEminiUpdate = x_.size();
    }

    //	if (x_.size() <= 2)
    //	{
    //		indexTolEminiUpdate = 3;
    //	}
    //	else if (x_.size() > 2 && x_.size() <= 4)
    //	{
    //		indexTolEminiUpdate = 4 * x_.size();
    //	}
    //	else if (x_.size() > 4 && x_.size() <= 10)
    //	{
    //		indexTolEminiUpdate = 4 * x_.size();
    //	}

    startTime_ = clock();
    eMini_ = etot_;
    xMini_ = x_;
    etot0_ = etot_;

    if (isVerbose())

    {
        Rprintf("first time, ind_trace is: %i\n", indTrace_);
    }
    // Initialize etot0 and temp
    if (!lsEnd_)
    {
        etot_ = lsEnergy(x_);
        if (etot_ < eMini_)
        {
            eMini_ = etot_;
            xMini_ = x_;
        }
        ++indTrace_;
        // Do the tracing here
        tracer_.addValue("currentEnergy", etot0_);
        tracer_.addValue("minEnergy", eMini_);
        tracer_.addValue("nSteps", itNew);
        tracer_.addValue("temperature", temSta_);
    }
    if (etot_ < eMini_)
    {
        eMini_ = etot_;
        xMini_ = x_;
    }
    etot0_ = etot_;
    tem_ = temSta_;

    if (isVerbose())
    {
        Rprintf("The transformed xinitial x: \n");
        printVect(x_);
        Rprintf("The energy of  transformed initial x = %.10g\n", etot_);
    }

    if (isVerbose())
    {
        Rprintf("At the beginning, etot0= %.10g\n", etot0_);
        Rprintf("Emini= %.10g\n", eMini_);
        Rprintf("Current x: ");
        printVect(x_);
        Rprintf("Current xmini: ");
        printVect(xMini_);
    }
    if (checkStoping())
    {
        stopSearch();
        return 0;
    }

    if (isVerbose())
    {
        Rprintf("Number of function call: %i\n", nbFctCall_);
    }
    int stepRecord = 0;
L2435:

    // Main loop
    for (int i = 0; i < maxStep_; ++i)
    {
        itNew = i + 1;
        s1 = (double) itNew;
        s = s1 + 1.;
        t1 = exp((qv_ - 1.) * log(2.)) - 1.;
        t2 = exp((qv_ - 1.) * log(s)) - 1.;
        tem_ = temSta_ * t1 / t2;
        stepRecord += 1;
        if (stepRecord == maxStep_)
        {
            break;
        }
        if (tem_ < temRestart_)
        {
            //printf("*\n");
            goto L2435;
        }
        temQa = tem_ / (double) itNew;

        indexNoEminiUpdate++;

        // Markov loop
        for (unsigned int j = 0; j < (unsigned) markovLength_; ++j)
        {
            if (j == 0)
            {
                eMini_NotChanged = true;
            }
            if (j == 0 && i == 0)
            {
                eMini_NotChanged = false;
            }

            xBackup_ = x_;
            inConstraint = false;
            while (!inConstraint)
            {
                // Loop on coordinates
                if (j < x_.size())
                {
                    for (unsigned int k = 0; k < x_.size(); ++k)
                    {
                        if (isVerbose())
                        {
                            Rprintf("IDUM before visit: %d\n", idum_);
                        }
                        visit = visita(&qv_, &tem_, &idum_);
                        if (visit > 1.e8)
                        {
                            visit = 1.e8 * Utils::ran2(&idum_);
                        }
                        else if (visit < -1.e8)
                        {
                            visit = -1.e8 * Utils::ran2(&idum_);
                        }

                        x_[k] = visit + xBackup_[k];
                        a = x_[k] - lower_[k];
                        b = Utils::dMod(&a, &xRange_[k]) + xRange_[k];
                        x_[k] = Utils::dMod(&b, &xRange_[k]) + lower_[k];
                        if (fabs(x_[k] - lower_[k]) < 1.e-10)
                        {
                            x_[k] += 1.e-10;
                        }
                        if (isVerbose())
                        {
                            Rprintf(
                                    "visit: %.10g a: %.10g b: %.10g idum: %d x: %.10g\n",
                                    visit, a, b, idum_, x_[k]);
                        }
                    } // end coordinates loop
                }
                else
                {
                    // Now change only one component at a time
                    visit = visita(&qv_, &tem_, &idum_);
                    if (visit > 1.e8)
                    {
                        visit = 1.e8 * Utils::ran2(&idum_);
                    }
                    else if (visit < -1.e8)
                    {
                        visit = -1.e8 * Utils::ran2(&idum_);
                    }
                    int index = j - x_.size();
                    x_[index] = visit + xBackup_[index];
                    a = x_[index] - lower_[index];
                    b = Utils::dMod(&a, &xRange_[index]) + xRange_[index];
                    x_[index] = Utils::dMod(&b, &xRange_[index])
                        + lower_[index];
                    if (fabs(x_[index] - lower_[index]) < 1.e-10)
                    {
                        x_[index] += 1.e-10;
                    }
                }
                if (isVerbose())
                {
                    Rprintf("\ntem: %.10g temqa: %.10g itnew: %d markov j=%d\n",
                            tem_, temQa, itNew, j);
                    Rprintf("fx are: ");
                    printVect(xBackup_);
                    Rprintf("x are: ");
                    printVect(x_);
                }

                if (hasConstraint_)
                {
                    inConstraint = judgeConstraint();
                }
                else
                {
                    inConstraint = true;
                }
                if (inConstraint)
                {
                    if (lsEnd_)
                    {
                        if (isVerbose())
                        {
                            Rprintf("Calling energy\n");
                        }
                        energy(x_);
                    }
                    else
                    {
                        if (isVerbose())
                        {
                            Rprintf("Calling lsEnergy\n");
                        }
                        etot_ = lsEnergy(x_);
                    }
                    if (isVerbose())
                    {
                        Rprintf("Before judge, etot0= %.10g etot= %.10g\n",
                                etot0_, etot_);
                    }
                    if (etot_ < etot0_)
                    {
                        etot0_ = etot_;
                        if (isVerbose())
                        {
                            Rprintf("etot is smaller than etot0\n");
                        }
                        if (etot_ < eMini_)
                        {
                            eMini_ = etot_;
                            xMini_ = x_;
                            eMini_NotChanged = false;
                            indexNoEminiUpdate = 0;
                        }
                    }
                    else
                    {
                        r = Utils::ran2(&idum_);
                        pqa1 = (qa_ - 1.) * (etot_ - etot0_) / temQa + 1.;
                        /* write(*,*)' etot0=',etot0,', etot=',etot,', pqa1=',pqa1 ! r */
                        if (pqa1 < 0.)
                        {
                            pqa = 0.;
                        }
                        else
                        {
                            pqa = exp(log(pqa1) / (1. - qa_));
                        }
                        if (isVerbose())
                        {
                            Rprintf("pqa= %.10g r= %.10g \n", pqa, r);
                        }
                        if (r > pqa)
                        {
                            x_ = xBackup_;
                        }
                        else
                        {
                            etot0_ = etot_;
                        }
                    } // endif etot_ < eMini_
                    tracer_.addValue("currentEnergy", etot0_);
                    tracer_.addValue("minEnergy", eMini_);
                    tracer_.addValue("nSteps", itNew);
                    tracer_.addValue("temperature", tem_);
                    if (checkStoping())
                    {
                        stopSearch();
                        return 0;
                    }
                } // end else hasConstraint
            } // end while !inconstraint
            if (indexNoEminiUpdate >= indexTolEminiUpdate - 1)
            {
                if (j == 0)
                {
                    eMiniMarkov = etot0_;
                    std::copy(x_.begin(), x_.end(), xMiniMarkov.begin());
                }
                else
                {
                    if (etot0_ < eMiniMarkov)
                    {
                        eMiniMarkov = etot0_;
                        std::copy(x_.begin(), x_.end(), xMiniMarkov.begin());
                    }
                }
            }
        } // end markov chain loop
        if (lsEnd_)
        {
            if (!eMini_NotChanged)
            {
                dVec temp(x_.size());
                std::copy(xMini_.begin(), xMini_.end(), temp.begin());
                //Rprintf("Xmini:\n");
                //printVect(xMini_);
                //				if (isUserVerbose())
                //				{
                //					Rprintf("Before lsEnergy, itNew: %d eTemp: %.15g x: %.15g %15g\n", itNew, eMini_, temp[0], temp[1]);
                //				}
                double eTemp = lsEnergy(temp);
                //				if (isUserVerbose())
                //				{
                //					Rprintf("lsEnergy called, itNew: %d eTemp: %.15g x: %.15g %.15g\n", itNew, eTemp, temp[0], temp[1]);
                //				}

                if (eTemp < eMini_)
                {
                    if (isUserVerbose())
                    {
                        Rprintf("It: %d, obj value: %.10g\n", itNew, eTemp);
                    }
                    std::copy(temp.begin(), temp.end(), xMini_.begin());
                    eMini_ = eTemp;
                    indexNoEminiUpdate = 0;
                    tracer_.updateLastValue("currentEnergy", etot0_);
                    tracer_.updateLastValue("minEnergy", eMini_);
                    tracer_.updateLastValue("nSteps", itNew);
                    tracer_.updateLastValue("temperature", tem_);
                }
            }
            if (indexNoEminiUpdate >= indexTolEminiUpdate)
            {
                //Rprintf("Before lsEnergy, itNew: %d x: %.15g %.15g\n", itNew, xMiniMarkov[0], xMiniMarkov[1]);
                eMiniMarkov = lsEnergy(xMiniMarkov);
                //Rprintf("lsEnergy called, itNew: %d eMiniMarkov: %.15g x: %.15g %.15g\n", itNew, eMiniMarkov, xMiniMarkov[0], xMiniMarkov[1]);
                if (isUserVerbose())
                {
                    Rprintf(".");
                }
                indexNoEminiUpdate = 0;
                indexTolEminiUpdate = x_.size();
                if (eMiniMarkov < eMini_)
                {
                    std::copy(xMiniMarkov.begin(), xMiniMarkov.end(),
                            xMini_.begin());
                    eMini_ = eMiniMarkov;
                    tracer_.updateLastValue("currentEnergy", etot0_);
                    tracer_.updateLastValue("minEnergy", eMini_);
                    tracer_.updateLastValue("nSteps", itNew);
                    tracer_.updateLastValue("temperature", tem_);
                    if (isUserVerbose())
                    {
                        Rprintf("\nIt: %d, obj value: %.10g\n", itNew, eMini_);
                    }
                    if (checkStoping())
                    {
                        stopSearch();
                        return 0;
                    }
                }
            }
        }

        itDev = itNew % interval_;
        if (0 == itDev)
        {
            if (isVerbose())
            {
                Rprintf(">After one search %d %.10g %.10g %.10g <-- Emini\n",
                        itNew, tem_, etot0_, eMini_);
                Rprintf("Current x: ");
                printVect(x_);
                Rprintf("\nCurrent xmini: ");
                printVect(xMini_);
                Rprintf(
                        "\n__________________________________________________\n");
            }
        }
    } // end step loop
    stopSearch();
    return 0;
}