示例#1
0
文件: pnode.cpp 项目: chaozh/phxpaxos
int PNode :: ProposalMembership(
        SystemVSM * poSystemVSM, 
        const int iGroupIdx, 
        const NodeInfoList & vecNodeInfoList, 
        const uint64_t llVersion)
{
    string sOpValue;
    int ret = poSystemVSM->Membership_OPValue(vecNodeInfoList, llVersion, sOpValue);
    if (ret != 0)
    {
        return Paxos_SystemError;
    }

    SMCtx oCtx;
    int smret = -1;
    oCtx.m_iSMID = SYSTEM_V_SMID;
    oCtx.m_pCtx = (void *)&smret;

    uint64_t llInstanceID = 0;
    ret = Propose(iGroupIdx, sOpValue, llInstanceID, &oCtx);
    if (ret != 0)
    {
        return ret;
    }

    return smret;
}
示例#2
0
FenMenu::FenMenu()
{
    nomMenu = new QLabel;
    btnPropose = new QPushButton;
    btnAleatoire = new QPushButton;
    btnQuitter = new QPushButton;
    layout = new QVBoxLayout;

    nomMenu->setText(QString::fromUtf8("Le Mot Mystère"));
    btnPropose->setText(QString::fromUtf8("Choix du mot mystère"));
    btnAleatoire->setText(QString::fromUtf8("Mot mystère aléatoire"));
    btnQuitter->setText(QString::fromUtf8("Quitter"));

    layout->addWidget(nomMenu);
    layout->addWidget(btnPropose);
    layout->addWidget(btnAleatoire);
    layout->addWidget(btnQuitter);

    setLayout(layout);

    QObject::connect(btnPropose, SIGNAL(released()), this, SLOT(Propose()));
    QObject::connect(btnAleatoire, SIGNAL(released()), this, SLOT(Aleatoire()));
    QObject::connect(btnQuitter, SIGNAL(released()), qApp, SLOT(quit()));

}
short int ParameterUpdater::MCMC_Step(curr_par_obj* current_values, mach_dat_obj* data, global_par_obj* global_pars, int iteration_id, int print_step )
{
  double mcmc_ratio;
  double logalpha;
  is_accepted = 0;
  int i;
  PRINT_MCMC_STEP=print_step;
  if(PRINT_MCMC_STEP)
    {
      if(iteration_id >= 0)
	cout << iteration_id << " ";
      cout << par_name;
    }
  SetCurrentValue(current_values, global_pars);
  Propose(current_values, data, global_pars);
  if(PRINT_MCMC_STEP)
    {
      cout << ": current = " << current_value[0] << ", proposed = " << proposal_value[0] << " ";
    }
  if(proposal_value[0] != current_value[0])
    {
      mcmc_ratio = Logposterior(proposal_value[0], current_values, data, global_pars) 
	- Logposterior(current_value[0], current_values, data, global_pars)
	+ Logjump(current_value[0], proposal_value[0], current_values, data, global_pars) 
	- Logjump(proposal_value[0], current_value[0], current_values, data, global_pars);
      
      distributions->Simulate(3, 1, &logalpha, 0.0, 1.0);
      if(PRINT_MCMC_STEP)
	{
	  cout << " ratio = " << mcmc_ratio << ", logalpha = " << log(logalpha);
	}
      if(log(logalpha) < mcmc_ratio)
	{
	  is_accepted += 1;
	  SetCurrentProposed(current_values, global_pars);
	  if(PRINT_MCMC_STEP)
	    cout << " accepted " << endl;
	}
      else
	{
	  if(PRINT_MCMC_STEP)
	    cout << " rejected " << endl;
	}      
    }
  else
    {
      if(PRINT_MCMC_STEP)
	{
	  cout << " no move " << endl;
	}
    }
  
  return(is_accepted);
}
示例#4
0
	// update the value of the population parameter value using a robust adaptive metropolis algorithm
	virtual void Update() {
		// get current conditional log-posterior of population
		double logdens_current = std::accumulate(logdens.begin(), logdens.end(), 0.0);
		logdens_current += LogPrior(theta);
        
		// propose new value of population parameter
		svector proposed_theta = Propose();
        
        svector chi = p_Daug->GetChiVec();
        
		// calculate log-posterior of new population parameter
        double p_chi[pchi];
        for (int i=0; i<ndata; i++) {
            for (int j=0; j<pchi; j++) {
                p_chi[j] = chi[j * ndata + i];
            }
            proposed_logdens[i] = LogDensity(p_chi, proposed_theta);
        }
		double logdens_prop = std::accumulate(proposed_logdens.begin(), proposed_logdens.end(), 0.0);
        
		logdens_prop += LogPrior(proposed_theta);
        
		// accept the proposed value?
		double metro_ratio = 0.0;
		bool accept = AcceptProp(logdens_prop, logdens_current, metro_ratio);
		if (accept) {
			theta = proposed_theta;
            logdens = proposed_logdens;
			naccept++;
			current_logdens = logdens_prop;
		} else {
			current_logdens = logdens_current;
		}
        
		// adapt the covariance matrix of the proposals
		AdaptProp(metro_ratio);
		current_iter++;
	}