コード例 #1
0
ファイル: mvnsub.c プロジェクト: cran/mprobit
void isamp(int x[], int n)
/* random permutation of size n without replacement */
{ int j,k,temp,rndgen(int, int);
  for(j=1;j<=n;j++) x[j]=j;
  for(j=1; j<=n; j++)
  {  k=rndgen(j,n);	/*uniform on [j,n]*/
     temp=x[j]; x[j]=x[k]; x[k]=temp;
  }
}
コード例 #2
0
ファイル: ex_mt.cpp プロジェクト: bruneli/optimize
int main(int argc, char* argv[])
{
  // Number of threads
  unsigned int n_threads = boost::thread::hardware_concurrency();
  std::cout << "Number of available threads " << n_threads << std::endl;

  likelihood l(5, 5, 9);
  double nexp[5][5] = {{1.,  1.6,  0.6, 0.2,  0.0},
                       {0., 15.4,  2.3, 0.0,  0.0},
                       {0.,  2.5,  4.1, 0.0,  0.0},
                       {0.,  3.3,  0.0, 1.1,  0.0},
                       {0.,  9.5, 10.0, 0.7, 39.2}};
  std::cout << "n(regions) = " << l.nr()
            << ", n(processes) = " << l.np()
            << ", n(systematics) = " << l.ns() << std::endl;
  for (unsigned int ir = 0; ir < l.nr(); ir++)
    for (unsigned int ip = 0; ip < l.np(); ip++) {
      l.set_nexp(ir, ip, nexp[ir][ip]);
      for (unsigned int is = 0; is < l.ns(); is++) {
        double dsyst = 0.;
        if (is < 2 && ip != 4) // Jet/MET
          dsyst = (0.1 - 0.05 * is) * (1. + ir * 0.05);
        else if (is < 7 && (is - 2 == ip)) // Theory
          dsyst = (ip == 4) ? 1. : 0.5;
        else if (is == 7 && (ir == 1 || ir == 2)) // b-tagging
          dsyst = (-1. + (ir - 1) * 2.) * 0.1;
        else if (is == 8) // Photon id efficiency
          dsyst = 0.05;
        l.set_dsyst(ir, ip, is, dsyst);
      }      
    }

  // Setup minimizer
  unsigned int nparams = l.np()+l.ns();
  boost::scoped_array<double> p0(new double[nparams]);
  boost::scoped_array<double> popt(new double[nparams]);
  for (unsigned int i = 0; i < nparams; i++)
    p0[i] = (i < l.np()) ? 1. : 0.;
  boost::function<double (double * const)> feval;
  feval = boost::bind(&likelihood::eval, &l, _1);
  optimize::minimizer_nm   mle_nm(feval, l.np()+l.ns());
  optimize::minimizer_bfgs mle_bfgs(feval, l.np()+l.ns());

  // Setup random generation of poisson distributed numbers
  boost::mt19937 gen;
  typedef boost::poisson_distribution<unsigned int> t_poisson;
  typedef boost::scoped_ptr< t_poisson > t_ptr_poisson;
  typedef boost::variate_generator< boost::mt19937&, t_poisson > t_gen;
  typedef boost::scoped_ptr< t_gen > t_ptr_gen;
  boost::scoped_array< t_ptr_poisson > pdist(new t_ptr_poisson[l.nr()]);
  boost::scoped_array< t_ptr_gen > rndgen(new t_ptr_gen[l.ns()]);
  for (unsigned int ir = 0; ir < l.nr(); ir++) {
    std::cout << "region" << ir << " nexp = " 
              << l.nexp(ir, p0.get()) << std::endl;
    pdist[ir].reset(new t_poisson(l.nexp(ir, p0.get())));
    rndgen[ir].reset(new t_gen(gen, *(pdist[ir])));
  }

  boost::timer time_monitor;
  double dt[3] = {0., 0., 0.};
  unsigned int ntoys = 10;
  for (unsigned int iexp = 0; iexp < ntoys; iexp++) {
    std::cout << "Starting experiment " << iexp << std::endl;

    // Generate pseudo-experiment random numbers
    for (unsigned int ir = 0; ir < l.nr(); ir++) {
      l.set_nobs(ir, (*(rndgen[ir]))());
      std::cout << "  region" << ir
                << " nobs = " << l.nobs(ir)
                << " nexp = " << l.nexp(ir, p0.get()) << std::endl;
    }

    std::cout << "  Perform a minimization " << std::endl;
    
    // Minimize with Nelder-Mean without mt
    mle_nm.set_is_mt(false);
    time_monitor.restart();
    mle_nm.minimize(p0.get());
    dt[0] = time_monitor.elapsed();
    std::cout << "  min = (";
    for (size_t i = 0; i < nparams; i++)
       popt[i] = mle_nm.get_opt_var(i).value();
    for (size_t i = 0; i < nparams; i++) {
      if (i < l.nr())
        std::cout << l.nexp(i, popt.get());
      else
        std::cout << mle_nm.get_opt_var(i).value();
      if (i < nparams - 1) std::cout << ", ";
    }
    std::cout << ") fmin = " << mle_nm.get_fmin() << std::endl;

    // Minimize with Nelder-Mean with mt
    mle_nm.set_is_mt(true);
    time_monitor.restart();
    mle_nm.minimize(p0.get());
    dt[1] = time_monitor.elapsed();
    std::cout << "  min = (";
    for (size_t i = 0; i < nparams; i++)
       popt[i] = mle_nm.get_opt_var(i).value();
    for (size_t i = 0; i < nparams; i++) {
      if (i < l.nr())
        std::cout << l.nexp(i, popt.get());
      else
        std::cout << mle_nm.get_opt_var(i).value();
      if (i < nparams - 1) std::cout << ", ";
    }
    std::cout << ") fmin = " << mle_nm.get_fmin() << std::endl;

    // Minimize with BFGS
    time_monitor.restart();
    mle_bfgs.minimize(p0.get());
    dt[2] = time_monitor.elapsed();
    std::cout << "  min = (";
    for (size_t i = 0; i < nparams; i++)
       popt[i] = mle_bfgs.get_opt_var(i).value();
    for (size_t i = 0; i < nparams; i++) {
      if (i < l.nr())
        std::cout << l.nexp(i, popt.get());
      else
        std::cout << mle_bfgs.get_opt_var(i).value();
      if (i < nparams - 1) std::cout << ", ";
    }
    std::cout << ") fmin = " << mle_bfgs.get_fmin() << std::endl;

    std::cout << "  timing " << dt[0] << " " << dt[1] << " " << dt[2] 
              << std::endl;
    std::cout << "  ... done." << std::endl;
  }

  return 0;
}
コード例 #3
0
ファイル: server.cpp プロジェクト: a-sf-mirror/gusanos
void Server::ZCom_cbDataReceived( ZCom_ConnID  _id, ZCom_BitStream &_data) 
{
	Network::NetEvents event = (Network::NetEvents) _data.getInt(8);
	switch( event )
	{
		case Network::PLAYER_REQUEST:
		{
			std::string name = _data.getStringStatic();
			int colour = _data.getInt(24);
			int team = _data.getSignedInt(8);
			unsigned int uniqueID = static_cast<unsigned int>(_data.getInt(32));

			BaseWorm* worm = game.addWorm(true);
			if ( NetWorm* netWorm = dynamic_cast<NetWorm*>(worm) )
			{
				netWorm->setOwnerId(_id);
			}
			BasePlayer* player = game.addPlayer ( Game::PROXY );
			
			let_(i, savedScores.find(uniqueID));
			if(i != savedScores.end())
			{
				player->stats = i->second;
				player->getOptions()->uniqueID = uniqueID;
			}
			else
			{
				do
				{
					uniqueID = rndgen();
				} while(!uniqueID);
				
				player->getOptions()->uniqueID = uniqueID;
				savedScores[uniqueID] = player->stats;
			}
			
			player->colour = colour;
			player->team = team;
			player->localChangeName( name );
			console.addLogMsg( "* " + player->m_name + " HAS JOINED THE GAME");
			player->assignNetworkRole(true);
			player->setOwnerId(_id);
			player->assignWorm(worm);
		}
		break;
		case Network::RConMsg:
		{
			char const* passwordSent = _data.getStringStatic();
			if ( !game.options.rConPassword.empty() && game.options.rConPassword == passwordSent )
			{
				//console.addQueueCommand(_data.getStringStatic());
				console.parseLine(_data.getStringStatic());
			}
		}
		break;
		
		case Network::ConsistencyInfo:
		{
			int clientProtocol = _data.getInt(32);
			if(clientProtocol != Network::protocolVersion)
			{
				network.disconnect(_id, Network::IncompatibleProtocol);
			}
			
			if(!game.checkCRCs(_data) && network.checkCRC) // We call checkCRC anyway so that the stream is advanced
				network.disconnect(_id, Network::IncompatibleData);
			
		}
		break;
	}
}