void NaiveSampleGenerator::getSample(base::DataVector& sample) {
  // generate random sample with dimensionality corresponding to the
  // size of the given datavector (in 0 to 1)
  for (size_t i = 0; i < sample.getSize(); i++) {
    sample[i] = uniformRealDist(rng);
  }
}
void StratifiedSampleGenerator::getSample(SGPP::base::DataVector& dv) {
  // Check for correct dimension of the parameter vector
  if (dv.getSize() != dimensions) return;

  // Choose a random number inside the stratum selected for this dimension
  for (size_t i = 0; i < dimensions; i++) {
    dv[i] = (static_cast<float_t>(currentStrata[i]) + uniformRealDist(rng)) * sizeOfStrata[i];
  }

  // continue to the next stratum used for the next sample
  getNextStrata();
}
Ejemplo n.º 3
0
NeuralNetwork::NeuralNetwork(const NeuronPointerList::size_type neuronCount, int seed, double j_ee, double j_ie, double j_ei, double j_ii) :
  J_EE(j_ee),
  J_IE(j_ie),
  J_EI(j_ei),
  J_II(j_ii),
  //rng(world.rank()), this compiles but does it work? does not seem to work
  rng(),
  //uniformDist(rng),
  uniformRealDist(0.0, 1.0),
  spikeDelayDistribution(tau_d - s_d, tau_d + s_d),
  distributionJ_EE(J_EE, 0.1*J_EE),
  distributionJ_IE(J_IE, 0.1*J_IE),
  distributionJ_EI(-J_EI, 0.1*J_EI),
  distributionJ_II(-J_II, 0.1*J_II),
  neuronList(),
  localNeuronList(),
  maxSpikeDelay(int(tau_d + s_d)),
  localSpikeList(),
  remoteSpikeList(),
  totalSpikingNeuronCount(0) {

    std::cout << "process " << world.rank() << " using seed " << seed << std::endl;
    rng.seed(seed); // this works

    std::cout << "maximum spike delay: " << maxSpikeDelay << std::endl;
    std::cout << "creating local spike list" << std::endl;
    for ( int i = 0; i < maxSpikeDelay; i++ ) {
      localSpikeList.push_back(new std::vector<double>());
    }

    // only need one remote spike list
    std::cout << "creating remote spike list" << std::endl;
    for ( int r = 0; r < 1; r++ ) {
      remoteSpikeList.push_back(new std::vector<double>());
    }

    externalInputDistMean = connectionProbability * neuronCount * excitatoryFraction * nu_E_ms * dt;

    ExcitatoryNeuron::Jexternal = J_EE;
    InhibitoryNeuron::Jexternal = J_IE;
    ExcitatoryNeuron::decayCoefficient = 1.0 - dt/tau_E_ms;
    InhibitoryNeuron::decayCoefficient = 1.0 - dt/tau_I_ms;

    std::cout << "creating " << neuronCount << " neurons" << std::endl;    
    excitatoryNeuronCount = 0;
    inhibitoryNeuronCount = 0;
    for ( int i = 0; i < neuronCount; i++ ) {
      // use the mpi process size and rank to determine which neurons are local
      // use i % mpiProcessSize
      //  i   i % mpiProcessSize=1  i % mpiProcessSize=2  i % mpiProcessSize=3
      //  0     0                     0                     0
      //  1     0                     1                     1
      //  2     0                     0                     2
      //  9     0                     1                     0
      // 10     0                     0                     1
      // if i % mpiProcessSize == (mpiProcessRank-1) then neuron i is local
      // the process rank of neuron i is easy to predict
      //   processRankNeuronI = (i % mpiProcessSize)
      //   if processRankNeuronI == mpiProcessRank then neuron i is local

      int mpiProcessRankOfNeuronI = (i % world.size());
      //if ( i < 10 ) {
      //  std::cout << "neuron " << i << " has mpiProcessRank " << mpiProcessRankOfNeuronI << std::endl;
      //}
      if ( mpiProcessRankOfNeuronI == world.rank() ) {
        // neuron i is local
        //if ( i < 10 ) {
          //std::cout << "neuron " << i << " in process with rank " << world.rank() << " is local" << std::endl;
        //}
        if ( uniformRealDist(rng) < excitatoryFraction ) {
          ExcitatoryNeuron* en = new ExcitatoryNeuron(i, mpiProcessRankOfNeuronI, &localSpikeList);
          neuronList.push_back(en);
          localNeuronList.push_back(en);
          excitatoryNeuronCount++;
        }
        else {
          InhibitoryNeuron* in = new InhibitoryNeuron(i, mpiProcessRankOfNeuronI, &localSpikeList);
          neuronList.push_back(in);
          localNeuronList.push_back(in);
          inhibitoryNeuronCount++;
        }
      }
      else {
        //if (i < 10) {
        //  std::cout << "neuron " << i << " in process with rank " << world.rank() << " is remote" << std::endl;
        //}
        // neuron i is remote
        RemoteNeuron* rn = new RemoteNeuron(i, mpiProcessRankOfNeuronI, &remoteSpikeList);
        neuronList.push_back(rn);
      }
    }

    // send the type id of all local neurons to the other processes
    std::vector<int> neuronTypeIdBuffer;
    for ( int i = 0; i < localNeuronList.size(); i++ ) {
      neuronTypeIdBuffer.push_back(localNeuronList.at(i)->getId());
      neuronTypeIdBuffer.push_back(localNeuronList.at(i)->getTypeId());
    }
    std::vector< std::vector<int> > remoteNeuronTypeIdBufferList;
    boost::mpi::all_gather(world, neuronTypeIdBuffer, remoteNeuronTypeIdBufferList);
    std::vector< std::vector<int> >::const_iterator x = remoteNeuronTypeIdBufferList.begin();
    for ( int allgather_rank = 0; allgather_rank < world.size(); allgather_rank++ ) {
      std::cout << "process " << world.rank()
                << " reading remote neuron type id buffer from mpi process with rank " 
                << allgather_rank << std::endl;
      // do not do anything with this process's neurons
      if ( allgather_rank == world.rank() ) {
        std::cout << "skipping local neurons in process " << world.rank() << std::endl;
      }
      else {
        std::vector<int> remoteNeuronTypeIdBuffer = remoteNeuronTypeIdBufferList[allgather_rank];
        std::vector<int>::const_iterator y = remoteNeuronTypeIdBuffer.begin();
        while ( y != remoteNeuronTypeIdBuffer.end() ) {
          int remoteNeuronId = *y;
          y++;
          int remoteNeuronTypeId = *y;
          y++; 
          //std::cout << "process with rank " << world.rank()
          //          << " setting remote neuron " << remoteNeuronId 
          //          << " type to " << remoteNeuronTypeId
          //          << std::endl;
          RemoteNeuron* remoteNeuron = (RemoteNeuron*) neuronList[remoteNeuronId];
          neuronList[remoteNeuronId]->setTypeId(remoteNeuronTypeId);
        }
      }
    }

    std::cout << "creating connections" << std::endl;
    std::cout << localNeuronList.size() << " local neurons" << std::endl;
    std::cout << neuronList.size() << " total neurons" << std::endl;
    RandomNormalDistribution* distributionJ_xx[2][2];
    distributionJ_xx[0][0] = &distributionJ_EE;
    distributionJ_xx[1][0] = &distributionJ_IE;
    distributionJ_xx[0][1] = &distributionJ_EI;
    distributionJ_xx[1][1] = &distributionJ_II;
    int connectionCounts[2][2];
    connectionCounts[0][0] = 0;
    connectionCounts[1][0] = 0;
    connectionCounts[0][1] = 0;
    connectionCounts[1][1] = 0;

    connectionCount = 0;
    int localConnectionCount = 0;
    int remoteConnectionCount = 0;
    for (int i = 0; i < localNeuronList.size(); i++) {
      LocalNeuron* sourceNeuron = localNeuronList[i];
      for (int j = 0; j < neuronList.size(); j++) {
        Neuron* targetNeuron = neuronList[j];
        if (sourceNeuron->getId() == targetNeuron->getId()) {
          // do not connect a neuron to itself
        }
        else if ( uniformRealDist(rng) < connectionProbability ) {
          connectionCount++;

          int delay = spikeDelayDistribution(rng);
          int targetNeuronTypeId = targetNeuron->getTypeId();
          int sourceNeuronTypeId = sourceNeuron->getTypeId();
          connectionCounts[targetNeuronTypeId][sourceNeuronTypeId]++;
          RandomNormalDistribution* efficacyDistribution = distributionJ_xx[targetNeuron->getTypeId()][sourceNeuron->getTypeId()];
          double efficacy = (*efficacyDistribution)(rng);
          sourceNeuron->addConnection(
            new Connection(sourceNeuron, targetNeuron, efficacy, delay)
          );
          if ( sourceNeuron->getMpiRank() == targetNeuron->getMpiRank() ) {
            localConnectionCount++;
          }
          else {
            remoteConnectionCount++;
          }
        }
        else {
         // no connection between these neurons
        }
      }
    }

    std::cout << "process " << world.rank() << " : " << localConnectionCount << " local connections" << std::endl;
    std::cout << "process " << world.rank() << " : " << remoteConnectionCount << " remote connections" << std::endl;

    std::cout << "process " << world.rank() << " : " << connectionCounts[0][0] << " ee connections" << std::endl;
    std::cout << "process " << world.rank() << " : " << connectionCounts[1][0] << " ie connections" << std::endl;
    std::cout << "process " << world.rank() << " : " << connectionCounts[0][1] << " ei connections" << std::endl;
    std::cout << "process " << world.rank() << " : " << connectionCounts[1][1] << " ii connections" << std::endl;
    std::cout << "process " << world.rank() << " NeuralNetwork initialized" << std::endl;

}
Ejemplo n.º 4
0
static void
test_for_replica_exchange(FILE                 *fplog,
                          const gmx_multisim_t *ms,
                          struct gmx_repl_ex   *re,
                          const gmx_enerdata_t *enerd,
                          real                  vol,
                          gmx_int64_t           step,
                          real                  time)
{
    int                                  m, i, j, a, b, ap, bp, i0, i1, tmp;
    real                                 delta = 0;
    gmx_bool                             bPrint, bMultiEx;
    gmx_bool                            *bEx      = re->bEx;
    real                                *prob     = re->prob;
    int                                 *pind     = re->destinations; /* permuted index */
    gmx_bool                             bEpot    = FALSE;
    gmx_bool                             bDLambda = FALSE;
    gmx_bool                             bVol     = FALSE;
    gmx::ThreeFry2x64<64>                rng(re->seed, gmx::RandomDomain::ReplicaExchange);
    gmx::UniformRealDistribution<real>   uniformRealDist;
    gmx::UniformIntDistribution<int>     uniformNreplDist(0, re->nrepl-1);

    bMultiEx = (re->nex > 1);  /* multiple exchanges at each state */
    fprintf(fplog, "Replica exchange at step %" GMX_PRId64 " time %.5f\n", step, time);

    if (re->bNPT)
    {
        for (i = 0; i < re->nrepl; i++)
        {
            re->Vol[i] = 0;
        }
        bVol               = TRUE;
        re->Vol[re->repl]  = vol;
    }
    if ((re->type == ereTEMP || re->type == ereTL))
    {
        for (i = 0; i < re->nrepl; i++)
        {
            re->Epot[i] = 0;
        }
        bEpot              = TRUE;
        re->Epot[re->repl] = enerd->term[F_EPOT];
        /* temperatures of different states*/
        for (i = 0; i < re->nrepl; i++)
        {
            re->beta[i] = 1.0/(re->q[ereTEMP][i]*BOLTZ);
        }
    }
    else
    {
        for (i = 0; i < re->nrepl; i++)
        {
            re->beta[i] = 1.0/(re->temp*BOLTZ);  /* we have a single temperature */
        }
    }
    if (re->type == ereLAMBDA || re->type == ereTL)
    {
        bDLambda = TRUE;
        /* lambda differences. */
        /* de[i][j] is the energy of the jth simulation in the ith Hamiltonian
           minus the energy of the jth simulation in the jth Hamiltonian */
        for (i = 0; i < re->nrepl; i++)
        {
            for (j = 0; j < re->nrepl; j++)
            {
                re->de[i][j] = 0;
            }
        }
        for (i = 0; i < re->nrepl; i++)
        {
            re->de[i][re->repl] = (enerd->enerpart_lambda[(int)re->q[ereLAMBDA][i]+1]-enerd->enerpart_lambda[0]);
        }
    }

    /* now actually do the communication */
    if (bVol)
    {
        gmx_sum_sim(re->nrepl, re->Vol, ms);
    }
    if (bEpot)
    {
        gmx_sum_sim(re->nrepl, re->Epot, ms);
    }
    if (bDLambda)
    {
        for (i = 0; i < re->nrepl; i++)
        {
            gmx_sum_sim(re->nrepl, re->de[i], ms);
        }
    }

    /* make a duplicate set of indices for shuffling */
    for (i = 0; i < re->nrepl; i++)
    {
        pind[i] = re->ind[i];
    }

    rng.restart( step, 0 );

    /* PLUMED */
    int plumed_test_exchange_pattern=0;
    if(plumed_test_exchange_pattern && plumed_hrex) gmx_fatal(FARGS,"hrex not compatible with ad hoc exchange patterns");
    /* END PLUMED */

    if (bMultiEx)
    {
        /* multiple random switch exchange */
        int nself = 0;


        for (i = 0; i < re->nex + nself; i++)
        {
            // For now this is superfluous, but just in case we ever add more
            // calls in different branches it is safer to always reset the distribution.
            uniformNreplDist.reset();

            /* randomly select a pair  */
            /* in theory, could reduce this by identifying only which switches had a nonneglibible
               probability of occurring (log p > -100) and only operate on those switches */
            /* find out which state it is from, and what label that state currently has. Likely
               more work that useful. */
            i0 = uniformNreplDist(rng);
            i1 = uniformNreplDist(rng);
            if (i0 == i1)
            {
                nself++;
                continue;  /* self-exchange, back up and do it again */
            }

            a  = re->ind[i0]; /* what are the indices of these states? */
            b  = re->ind[i1];
            ap = pind[i0];
            bp = pind[i1];

            bPrint = FALSE; /* too noisy */
            /* calculate the energy difference */
            /* if the code changes to flip the STATES, rather than the configurations,
               use the commented version of the code */
            /* delta = calc_delta(fplog,bPrint,re,a,b,ap,bp); */
            delta = calc_delta(fplog, bPrint, re, ap, bp, a, b);

            /* we actually only use the first space in the prob and bEx array,
               since there are actually many switches between pairs. */

            if (delta <= 0)
            {
                /* accepted */
                prob[0] = 1;
                bEx[0]  = TRUE;
            }
            else
            {
                if (delta > PROBABILITYCUTOFF)
                {
                    prob[0] = 0;
                }
                else
                {
                    prob[0] = exp(-delta);
                }
                // roll a number to determine if accepted. For now it is superfluous to
                // reset, but just in case we ever add more calls in different branches
                // it is safer to always reset the distribution.
                uniformRealDist.reset();
                bEx[0] = uniformRealDist(rng) < prob[0];
            }
            re->prob_sum[0] += prob[0];

            if (bEx[0])
            {
                /* swap the states */
                tmp      = pind[i0];
                pind[i0] = pind[i1];
                pind[i1] = tmp;
            }
        }
        re->nattempt[0]++;  /* keep track of total permutation trials here */
        print_allswitchind(fplog, re->nrepl, pind, re->allswaps, re->tmpswap);
    }
    else
    {
        /* standard nearest neighbor replica exchange */

        m = (step / re->nst) % 2;
        /* PLUMED */
        if(plumedswitch){
          int partner=re->repl;
          plumed_cmd(plumedmain,"getExchangesFlag",&plumed_test_exchange_pattern);
          if(plumed_test_exchange_pattern>0){
            int *list;
            snew(list,re->nrepl);
            plumed_cmd(plumedmain,"setNumberOfReplicas",&(re->nrepl));
            plumed_cmd(plumedmain,"getExchangesList",list);
            for(i=0; i<re->nrepl; i++) re->ind[i]=list[i];
            sfree(list);
          }

          for(i=1; i<re->nrepl; i++) {
            if (i % 2 != m) continue;
            a = re->ind[i-1];
            b = re->ind[i];
            if(re->repl==a) partner=b;
            if(re->repl==b) partner=a;
          }
          plumed_cmd(plumedmain,"GREX setPartner",&partner);
          plumed_cmd(plumedmain,"GREX calculate",NULL);
          plumed_cmd(plumedmain,"GREX shareAllDeltaBias",NULL);
        }
        /* END PLUMED */
        for (i = 1; i < re->nrepl; i++)
        {
            a = re->ind[i-1];
            b = re->ind[i];

            bPrint = (re->repl == a || re->repl == b);
            if (i % 2 == m)
            {
                delta = calc_delta(fplog, bPrint, re, a, b, a, b);
                /* PLUMED */
                if(plumedswitch){
                  real adb,bdb,dplumed;
                  char buf[300];
                  sprintf(buf,"GREX getDeltaBias %d",a); plumed_cmd(plumedmain,buf,&adb);
                  sprintf(buf,"GREX getDeltaBias %d",b); plumed_cmd(plumedmain,buf,&bdb);
                  dplumed=adb*re->beta[a]+bdb*re->beta[b];
                  delta+=dplumed;
                  if (bPrint)
                    fprintf(fplog,"dplumed = %10.3e  dE_Term = %10.3e (kT)\n",dplumed,delta);
                }
                /* END PLUMED */
                if (delta <= 0)
                {
                    /* accepted */
                    prob[i] = 1;
                    bEx[i]  = TRUE;
                }
                else
                {
                    if (delta > PROBABILITYCUTOFF)
                    {
                        prob[i] = 0;
                    }
                    else
                    {
                        prob[i] = exp(-delta);
                    }
                    // roll a number to determine if accepted. For now it is superfluous to
                    // reset, but just in case we ever add more calls in different branches
                    // it is safer to always reset the distribution.
                    uniformRealDist.reset();
                    bEx[i] = uniformRealDist(rng) < prob[i];
                }
                re->prob_sum[i] += prob[i];

                if (bEx[i])
                {
                  /* PLUMED */
                  if(!plumed_test_exchange_pattern) {
                    /* standard neighbour swapping */
                    /* swap these two */
                    tmp       = pind[i-1];
                    pind[i-1] = pind[i];
                    pind[i]   = tmp;
                    re->nexchange[i]++;  /* statistics for back compatibility */
                  } else {
                    /* alternative swapping patterns */
                    tmp       = pind[a];
                    pind[a]   = pind[b];
                    pind[b]   = tmp;
                    re->nexchange[i]++;  /* statistics for back compatibility */
                  }
                  /* END PLUMED */
                }
            }
            else
            {
                prob[i] = -1;
                bEx[i]  = FALSE;
            }
        }
        /* print some statistics */
        print_ind(fplog, "ex", re->nrepl, re->ind, bEx);
        print_prob(fplog, "pr", re->nrepl, prob);
        fprintf(fplog, "\n");
        re->nattempt[m]++;
    }

    /* PLUMED */
    if(plumed_test_exchange_pattern>0) {
      for (i = 0; i < re->nrepl; i++)
      {
          re->ind[i] = i;
      }
    }
    /* END PLUMED */

    /* record which moves were made and accepted */
    for (i = 0; i < re->nrepl; i++)
    {
        re->nmoves[re->ind[i]][pind[i]] += 1;
        re->nmoves[pind[i]][re->ind[i]] += 1;
    }
    fflush(fplog); /* make sure we can see what the last exchange was */
}