Beispiel #1
0
void House::timeStep(const unsigned long time)
{
   // _goodStockList[G_WHEAT]._currentQty -= _d->currentHabitants;  // to do once every month!
   if( time % 16 == 0 )
   {
      // consume services
      for (int i = 0; i < S_MAX; ++i)
      {
         ServiceType service = (ServiceType) i;
         _d->serviceAccessMap[service] = std::max(_d->serviceAccessMap[service] - 1, 0);
      }

      cancelService( S_WORKERS_HUNTER );

      // consume goods
      for (int i = 0; i < G_MAX; ++i)
      {
         GoodType goodType = (GoodType) i;
         int qty = std::max(_d->goodStore.getCurrentQty(goodType) - 1, 0);
         _d->goodStore.setCurrentQty(goodType, qty);
      }
   }

   if( time % 64 == 0 )
   {
     bool validate = _d->houseLevelSpec.checkHouse(*this);
     if (!validate)
     {
       levelDown();
     }
     else
     {
       validate = _d->nextHouseLevelSpec.checkHouse(*this);
       if( validate && _d->currentHabitants > 0 )
       {
          levelUp();
       }
     }

     int homeless = math::clamp( _d->currentHabitants - _d->maxHabitants, 0, 0xff );

     if( homeless > 0 )
     {
       _d->currentHabitants = math::clamp( _d->currentHabitants, 0, _d->maxHabitants );

       CityPtr city = Scenario::instance().getCity();
       ImmigrantPtr im = Immigrant::create( city );
       im->setCapacity( homeless );
       im->send2City( getTile() );
     }
   }

   if( _d->currentHabitants > 0 )
   {
     Building::timeStep( time );
   }
}
void House::timeStep(const unsigned long time)
{
   if( _d->currentHabitants > 0 )
   {
     if( time % 16 == 0 )
     {
        // consume services
        for (int i = 0; i < Service::S_MAX; ++i)
        {
           Service::Type service = (Service::Type) i;
           _d->serviceAccess[service] = std::max(_d->serviceAccess[service] - 1, 0);
        }

        cancelService( Service::S_WORKERS_HUNTER );
        _d->updateHealthLevel();

        // consume goods
        for (int i = 0; i < Good::goodCount; ++i)
        {
           Good::Type goodType = (Good::Type) i;
           _d->goodStore.setCurrentQty( goodType, std::max( _d->goodStore.getCurrentQty(goodType) - 1, 0) );
        }
     }

     if( time % 64 == 0 )
     {
       bool validate = _d->houseLevelSpec.checkHouse( this );
       if (!validate)
       {
         levelDown();
       }
       else
       {
         _d->condition4Up = "";
         validate = _d->nextHouseLevelSpec.checkHouse( this, &_d->condition4Up );
         if( validate && _d->currentHabitants > 0 )
         {
            levelUp();
         }
       }

       int homeless = math::clamp( _d->currentHabitants - _d->maxHabitants, 0, 0xff );

       if( homeless > 0 )
       {
         _d->currentHabitants = math::clamp( _d->currentHabitants, 0, _d->maxHabitants );

         ImmigrantPtr im = Immigrant::create( _getCity() );
         im->setCapacity( homeless );
         im->send2City( getTile() );
       }
     }

     Building::timeStep( time );
   }
}
///////////////////////////////////////////////////////////
// uses particles, variance, ind
// uses newPoints, newIndices, trees, Ndens
void gibbs2(unsigned int _Ndens, const BallTreeDensity* _trees, 
            unsigned long Np, unsigned int Niter,
            double *_pts, BallTree::index *_ind,
            double *_randU, double* _randN)
{
  unsigned int i,j,l;
  unsigned long s, maxNp;

  Ndens = _Ndens;                       // SET UP GLOBALS
  trees = _trees;
  newPoints = _pts; newIndices = _ind;
  randU = _randU; randN = _randN;
  Ndim  = trees[0].Ndim();              // dimension of densities    
  maxNp = 0;                            // largest # of particles we deal with
  for (unsigned int j=0; j<Ndens; j++)  // compute Max Np over all densities
    if (maxNp < trees[j].Npts()) maxNp = trees[j].Npts();

  ind = new BallTree::index[Ndens];     // ALLOCATE GLOBALS
  p = new double[maxNp];
  
  Nlevels = (unsigned int) (log((double)maxNp)/log((double)2))+1;          // how many levels to a balanced binary tree?

  particles = new double[Ndim*Ndens];
  variance  = new double[Ndim*Ndens];

  dNpts = new unsigned long[Ndens];
  levelList = new BallTree::index*[Ndens];
  levelListNew = new BallTree::index*[Ndens];
  for (j=0;j<Ndens;j++) { 
    levelList[j] = new BallTree::index[maxNp];
    levelListNew[j] = new BallTree::index[maxNp];
  }

  for (s=0; s<Np; s++) {                       

    levelInit();
    initIndices();
    calcIndices();

    ///////////////////////////////////////////////////////////////
    // Perform Gibbs sampling only if multiple densities in product
    ///////////////////////////////////////////////////////////////
    samplePoint(newPoints);
    for (l=0;l<Nlevels;l++) {
      levelDown();
      for (i=0;i<Niter;i++) {
        sampleIndices(newPoints);
        samplePoint(newPoints);
    }}

    for (unsigned int j=0; j<Ndens; j++)              // save and
      newIndices[j] = trees[j].getIndexOf(ind[j])+1;  // return particle label
    newIndices += Ndens;                              // move pointers to next sample
    newPoints  += Ndim;
  }

  for (j=0;j<Ndens;j++) { delete[] levelList[j];  delete[] levelListNew[j]; }
  delete[] levelList; delete[] levelListNew;
  delete[] dNpts;

  delete[] ind; delete[] p; delete[] particles; delete[] variance;
};