Ejemplo n.º 1
0
void UpdatePheromones(double **pheromones, int **ants)
{
	int rho = RHO;
	int q = Q;
	for (int i = 0; i < n ; ++i)
      {
        for (int j = i + 1; j < n; ++j)
        {
			for (int k = 0; k < ANT_NUMBER; ++k)
			  {
				double length = CycleLength(ants[k]);
				double decrease = (1.0 - rho) * pheromones[i][j];
				double increase = 0.0;
				if (IsEdgeInTrail(i, j, ants[k]) == true) increase = (q / length);

				pheromones[i][j] = decrease + increase;

				if (pheromones[i][j] < 0.0001)
				  pheromones[i][j] = 0.0001;
				else if (pheromones[i][j] > 100000.0)
				  pheromones[i][j] = 100000.0;

				pheromones[j][i] = pheromones[i][j];
			  }
        }
      }
}
Ejemplo n.º 2
0
int *ShortestPath(int **ants)
{
   double bestLength = CycleLength(ants[0]); 
   int idxBestLength = 0;
   for (int k = 1; k < ANT_NUMBER ; ++k)
   {
     double len = CycleLength(ants[k]);
     if (len < bestLength)
     {
       bestLength = len;
       idxBestLength = k;
     } 
   }

   int *bestPath = new int[n];
   for(int i=0; i<n; i++)
	   bestPath[i] = ants[idxBestLength][i];

   return bestPath;
}
Ejemplo n.º 3
0
										  /// ANT COLONY ALGORITHMS ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
           /////                       /////                       /////                       /////       
void ant_colony()
{
	srand(time(NULL));

	const int loop_number = 1000;
    
    int** ants = CreateAnts(); 

    int *best_path = ShortestPath(ants);
    
	double bestLength = CycleLength(best_path);

	double **pheromones = InitializePheromones();

    int counter = 0;
       
	while (counter < loop_number)
    {
       UpdateAnts(ants, pheromones);
       UpdatePheromones(pheromones, ants);
           
       int *currBestTrail = ShortestPath(ants);
       double currBestLength = CycleLength(currBestTrail);

	   if (currBestLength < bestLength)
	   {
		   delete best_path;
		   best_path = NULL;
		   bestLength = currBestLength;
           best_path = currBestTrail;
       }
       counter++;
     }

	cout<<bestLength;
	 
	getchar();
	getchar();
}
Ejemplo n.º 4
0
// ================================================================================================
// Problem 26
// ================================================================================================
sint32 Problem26() {
    uintn longest = 0;
    uintn longestlen = 0;

    for(uintn i = 2; i < kMax; ++i) {
        uintn len = CycleLength(i);
        if(len > longestlen) {
            longest = i;
            longestlen = len;
        }
    }

    CLog::Write("The number with the longest cycle length of " UintNFmt_ " is " UintNFmt_ "\n",
                longestlen, longest);

    Assert_(longest == kAnswer, "The answer should have been " UintNFmt_, kAnswer);

    return 0;
}
Ejemplo n.º 5
0
/*!
 * \fn bool OSG::Animation::update(const Time& ElapsedTime)
 *
 * \brief Update the animation with the time since the last update
 *
 * The result of the animation will also be applied to the
 * object it is connected to.
 *
 * \param[in] ElapsedTime The time, in seconds, since the previous call to
 * update.
 */
bool Animation::update(const Time& ElapsedTime)
{
    if(!_IsPlaying || _IsPaused)
    {
        return false;
    }

    //Increment the updated animations statistic
    StatIntElem *NAnimationsStatElem = StatCollector::getGlobalElem(statNAnimations);
    if(NAnimationsStatElem) { NAnimationsStatElem->inc(); }

    //Start the  animation update time statistic
    StatTimeElem *AnimUpdateTimeStatElem = StatCollector::getGlobalElem(statAnimUpdateTime);
    if(AnimUpdateTimeStatElem) { AnimUpdateTimeStatElem->start(); }

    _CurrentTime += getScale()*ElapsedTime;
    UInt32 PreUpdateCycleCount(getCycles());
    if(getCycling() < 0 || PreUpdateCycleCount < getCycling())
    {
        Real32 CycleLength(getCycleLength() * getScale());

        //Check if the Animation Time is past the end
        if(_CurrentTime >= CycleLength)
        {
            //Update the number of cycles completed
            setCycles( (CycleLength <= 0.0f) ? (0): (static_cast<UInt32>( osgFloor( _CurrentTime / CycleLength ) )) );
            //commitChanges();
        }
        Real32 t(_CurrentTime);

        if(getCycling() > 0 && getCycles() >= getCycling())
        {
            if(getSpan() > 0.0f)
            {
                t = getSpan();
            }
            t -= 0.0001f;
        }
        else
        {
            if(getSpan() > 0.0f)
            {
                t -= osgFloor(_CurrentTime/getSpan())*getSpan();
            }
        }
        t += getOffset();

        //Internal Update
        internalUpdate(t, _PrevTime);


        //If the number of cycles has changed
        if(getCycles() != PreUpdateCycleCount)
        {
            if(getCycling() > 0 && getCycles() >= getCycling())
            {
                //Animation has reached the end
                //Remove the Animation from it's update producer
                _UpdateEventConnection.disconnect();
                _IsPlaying = false;

                //Produce the Ended event
                produceAnimationEnded();
            }
            else
            {
                //Animation hasn't finished yet
                //Produce the Cycled event
                produceAnimationCycled();
            }
        }
    }

    _PrevTime = _CurrentTime;

    //Stp[ the  animation update time statistic
    if(AnimUpdateTimeStatElem) { AnimUpdateTimeStatElem->stop(); }

    //Return true if the animation has completed its number of cycles, false otherwise
    return (getCycling() > 0 && getCycles() >= getCycling());
}
bool AnimationGroup::update(const Time& ElapsedTime)
{
    if(!_IsPlaying || _IsPaused)
    {
        return false;
    }
    _CurrentTime += getScale()*ElapsedTime;

    UInt32 PreUpdateCycleCount(getCycles());
    if(getCycling() < 0 || PreUpdateCycleCount < getCycling())
    {
        Real32 CycleLength(getCycleLength() * getScale());
        
        //Check if the Animation Time is past the end
        if(_CurrentTime >= CycleLength)
        {
            //Update the number of cycles completed
            setCycles( (CycleLength <= 0.0f) ? (0): (static_cast<UInt32>( osgFloor( _CurrentTime / CycleLength ) )) );
            //commitChanges();
        }
        Real32 t(_CurrentTime);

        if(getCycling() > 0 && getCycles() >= getCycling())
        {
            if(getSpan() > 0.0f)
            {
                t = getSpan();
            }
            else
            {
                t = CycleLength;
            }
            t -= 0.0001f;
        }
        else
        {
            if(getSpan() > 0.0f)
            {
                t -= osgFloor(_CurrentTime/getSpan())*getSpan();
            }
        }
        t += getOffset();

        //Internal Update
        for(UInt32 i = 0; i < getMFAnimations()->size(); ++i)
        {
            getAnimations(i)->internalUpdate(t, _PrevTime);
        }

        //If the number of cycles has changed
        if(getCycles() != PreUpdateCycleCount)
        {
            if(getCycling() > 0 && getCycles() >= getCycling())
            {
                //Animation has reached the end
                //Remove the Animation from it's update producer
                _UpdateEventConnection.disconnect();
                _IsPlaying = false;

                //Produce the Ended event
                produceAnimationEnded();
            }
            else
            {
                //Animation hasn't finished yet
                //Produce the Cycled event
                produceAnimationCycled();
            }
        }
    }

    _PrevTime = _CurrentTime;

    //Return true if the animation has completed its number of cycles, false otherwise
    return (getCycling() > 0 && getCycles() >= getCycling());
}