Esempio n. 1
0
  void QUESO_Basic_Class::SolveInverseProblem()
  {
    VerifyInit();  

    // Launch the Markov Chain 

    m_ip->solveWithBayesMetropolisHastings(NULL,*m_queso_var_ini,m_CovMatrix);
  }
Esempio n. 2
0
  void QUESO_Basic_Class::Likelihood_Register(double (*fp)(double *) )
  {

    VerifyInit();

    printf("--> Setting prior and post vectors...\n");

    m_priorRV = new uqUniformVectorRVClass <basicV, basicM> ("prior_",*m_paramDomain);
    m_postRV  = new uqGenericVectorRVClass <basicV, basicM> ("post_" ,*m_paramSpace );
    
    m_likelihoodObj = new uqGenericScalarFunctionClass 
      <basicV,basicM> ("like_",*m_paramDomain,Likelihood_Wrapper,NULL,true);
    
    m_user_likelihood_func = fp;
    
    // define the inverse (calibration) problem
    
    printf("--> Defining inverse problem...\n");
    
    m_ip = new uqStatisticalInverseProblemClass <basicV,basicM> ("",NULL,*m_priorRV,*m_likelihoodObj,*m_postRV);
    
    // Default covariance matrix for now - default assumption assumes
    // 6-sigma distribution range falls over 1/3 of the max parameter range.
    //
    // koomie TODO: store relevant constants regarding this default
    // assumption in input file and register as default values, but
    // allow the savvy user to override
    
    double cov_param = 1/(3.*6.);	// 6-sigma over 1/3 of the range
    double param_range = 0.0;
    
    printf("--> Defining default covariance matrix...\n");
    
    m_CovMatrix = m_postRV->imageSet().vectorSpace().newProposalMatrix(NULL,
								       m_queso_var_ini);

    for(int i=0;i<m_num_params;i++)
      {
	param_range = (*m_queso_var_max)[i] - (*m_queso_var_min)[i];
	(*m_CovMatrix)(i,i) = (cov_param*param_range)*(cov_param*param_range);
      }
  }
Esempio n. 3
0
void BStar::Search()
{
	int Cancelar = 0;
	int i,NExpandFD = 0;
	int LastIteration = -1;
	Cancelar = 0;
	DumpTree *dt;
	TreeNode *CurNode = NULL;		// last move expanded
	TreeNode *CurBestNode = NULL;	// last best
	if(DumpData)
	{
		dt = new DumpTree();
	}

	TreeNode *a = NULL;
	TreeNode *SelectedNode = NULL;
	ColorRoot = TreeNode::GetRootNode()->Color;

	NExpandFD = 18;
	for(i=1;i < FixedDepth;i++)
	{
		NExpandFD *= 3; // Branch factor 3
	}
	if(FixedDepth)
	{
		SelectNodes = (NExpandFD * 6)/10;
		VerifyNodes = NExpandFD - SelectNodes;
	}

	for(;;)
	{
		if(LastIteration == TotalExpand)
			break;						// avoid cicle without expands.
		LastIteration = TotalExpand;
		if(Cancel||Cancelar)
			break;
//		printf("SELECT Step %d\n",TotalExpand);
		while (GetRealValBestAtRoot() <= OptValAnyOtherMoveAtRoot())
		{
			if(BestMoveAtRoot != CurBestNode)
			{
				if(CurBestNode != NULL)
					PrintPV();
				CurBestNode = BestMoveAtRoot;
				LastChange = TotalExpand;
				PrintPV();
			}
			if(TreeNode::GetRootNode()->RealVal > (MATE-50)
				|| TreeNode::GetRootNode()->RealVal < (50-MATE)
				)
			{
				Cancelar= 1;
				// switch to verify
				break;
			}


			TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2ndBest)/2;
			
			// it is unlikely that any other move can achieve as good a RealVal ?
			if( SecondRootOptPrb() < MinAct)
			{
				if(GetRealValBestAtRoot() >= (RealVal2ndBstMoveAtRoot() + 1))
//					goto salida;
				break;
			}
			if(DumpData )
			{
				dt->Print("SELECT STEP Color %d Best %d Target Value %d\n",ColorRoot,BestMoveAtRoot->RealVal,TargetVal);
				dt->Write();
			}
//			Select Root Node with greatest OptPrb;
			a = GetBestOptPrb(TreeNode::GetRootNode());
			if(!a)
				break;
			if(a != CurNode)
			{
				CurNode = a;
			}
//			Trace down the child subtree selecting
//			For Player-to-Move nodes, child with largest OptPrb
//			For Opponent-to-Move nodes, child with best RealVal
//			Until arriving at a leaf node;
			SelectedNode = a->TraceDownNotStopper(true);
			if(DumpData)
			{
				dt->Print("Selected node:");
				dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
				dt->Print("Color root %d\n",TreeNode::GetRootNode()->Color);
				dt->DumpRoot();
			}
			if(SelectedNode->MoveCount != 0) // already expanded, mate pos?
			{
				break;
			}
//			Get RealVal for each Child Node of this leaf;
//			If it is a Player-to-Move node get OptVals for each Child;
			if(Cancelar||Cancel)
				break;
			Expand(SelectedNode,PLAYER);
			// search path down to new expanded
			SelectedNode = a->TraceDown(true);
			a = SelectedNode;

			if(DumpData)
			{
				dt->Print("Backup Node :");
				dt->DumpPath(a,TreeNode::GetRootNode());
			}

//			Back up Values;
			if(
				SelectedNode->RealVal == 0 
				&& SelectedNode->OptVal == 0
				&& SelectedNode->MoveCount == -1
				)
				Backup(PLAYER,a->MoveTo(PARENT));
			else
				Backup(PLAYER,a);

			// best move at root can change after a Backup
			GetRealValBestAtRoot();
			TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2Best())/2;
			
			BackupPrb(a);

			if(TotalExpand > MaxExpand)
			{
				Cancelar = 1;
				break;
			}
			if(FixedDepth)
			{
				if(TotalExpand > NExpandFD ) //18*(3^FixedDepth))
				{
					Cancelar = 1;
					break; // EffortLimitsExceeded
				}
			}
			else
			if((TimeElapsed()-ini) >= TimeLimit )
			{
				Cancelar = 1;
				break; // EffortLimitsExceeded
			}
			if ((ExpandCount > SelectNodes)) 
			{
				ExpandCount = 0;
				break; // EffortLimitsExceeded
			}

		}
		if(Cancel||Cancelar)
			break;


		TargetVal = RealVal2ndBstMoveAtRoot() + 1;

		if(TargetVal == (-UNDEFINED+1))
			goto salida;
//		printf("VERIFY  Step %d\n",TotalExpand);

		if(DumpData)
		{
			dt->Print("VERIFY Step\n");
			dt->Print("Target Value %d\n",TargetVal);
		}
		
		if(!FixedDepth && (TimeElapsed()-ini) >= TimeLimit )
		{
			break;
		}
		if(FixedDepth)
		{
			if(TotalExpand > NExpandFD) //18*(3^FixedDepth))
			{
				break; // EffortLimitsExceeded
			}
		}

		VerifyInit();
		if(!FixedDepth && (TimeElapsed()-ini) >= TimeLimit )
			break;		
		GetRealValBestAtRoot() ;
		if(BestMoveAtRoot->RealVal < TargetVal && GetRealValBestAtRoot() > OptValAnyOtherMoveAtRoot())
			break; 
		
		if(BestMoveAtRoot->RealVal >= MATE-50 || BestMoveAtRoot->RealVal <= 50-MATE)
			break;

		ExpandCount = 0;
		while(BestMoveAtRoot->RealVal >= TargetVal)
		{
			if(BestMoveAtRoot != CurBestNode)
			{
				CurBestNode = BestMoveAtRoot;
				LastChange = TotalExpand;
				// print info
				PrintPV();
			}

			if(DumpData)
			{
				dt->Print("Verify 1:");
			}

//			Select reply Node with Greatest RealVal
			a = BestMoveAtRoot;
			if(DumpData)
				dt->Print("BestMove Real %d Opt %d Pess %d\n",a->RealVal,a->OptVal,a->PessVal);
//			Trace down the child subtree selecting
//			For Opponent-to-Move nodes, child with largest OptPrb
//			For Player-to-Move nodes, child with best RealVal
//			Until arriving at a leaf node;
			SelectedNode = TraceDownVerify(a);
			if(DumpData)
			{
				dt->Print("Verify Selected node:");
				dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
			}
			if(DumpData)
			{
				dt->Print("Verify 2:");
			}
// TODO
//			if(IsOver(a))
//				return;	// Solved
			if(SelectedNode->RealVal == MATE || SelectedNode->RealVal == -MATE)
				break;
//				goto salida; 
//			Get RealVal for each Child Node of this leaf;
//			If it is an Opponent-to-Move node get OptVals for each Child;
			Expand(SelectedNode,OPPONENT);
			if(SelectedNode->MoveCount > 0)
			{
				SelectedNode = TraceDownVerify(SelectedNode); // Search leaf node 
				Backup(OPPONENT,SelectedNode);			    // and Back up Values;
				if(DumpData)
				{
					dt->Print("Verify Selected node:");
					dt->DumpPath(SelectedNode,TreeNode::GetRootNode());
					dt->DumpRoot();
				}
			}
			else
			{
				if(
					SelectedNode->RealVal == 0 
					&& SelectedNode->OptVal == 0
					&& SelectedNode->MoveCount == -1
					)
					break;
			}
			if(DumpData)
			{
				dt->Print("Verify 3:");
			}
			if(TotalExpand > MaxExpand)
			{
				Cancelar = 1;
				break;
			}
			if(FixedDepth)
			{
				if(TotalExpand > NExpandFD) // 18*(3^FixedDepth))
				{
					goto salida;
					break; // EffortLimitsExceeded
				}
			}
			else
			if (((TimeElapsed()-ini) >= TimeLimit && ExpandCount > 3 )|| (ExpandCount > VerifyNodes))
			{
				ExpandCount = 0;
				if((TimeElapsed()-ini) >= TimeLimit )
				{
					goto salida;
				}
				break;
			}
		}
		if(DumpData)
		{
			int TargetVal = ((BestMoveAtRoot->RealVal) + OptVal2Best())/2;
			dt->Print("Target %d\n",TargetVal);
			dt->Write();
		}
	}
salida:
	PrintPV();
	if(DumpData)
	{
		delete dt;
	}

}
Esempio n. 4
0
  void QUESO_Basic_Class:: DefineParameterSpace()
  {
    double *param_min;		// min value of each parameter
    double *param_max;		// max value of each parameter
    double *param_ini;		// initial value of each parameter

    int     ierr = 1;

    VerifyInit();

    // Verify presence of required input file

    ierr *= grvy_input_fopen(m_inputfile->c_str());

    if(ierr == 0)
      QUESO_fatal("Unable to access QUESO input file");

    // Derive the UQ parameters from input file and create QUESO parameter vector

    ierr *= grvy_input_fread_int("queso/parameters/num_params",&m_num_params);

    if(ierr == 0)
      QUESO_fatal("Unable to read num_params from input file.");

    grvy_printf(GRVY_INFO,"%s: Num params defined\n",log_prefix,m_num_params);
    
    printf("--> Setup parameter space variables/ranges...\n");
    m_paramSpace  = new uqVectorSpaceClass <basicV,basicM> (*m_env,"queso_basic_",m_num_params,NULL);
    
    param_min = (double *) calloc(m_num_params,sizeof(double));
    param_max = (double *) calloc(m_num_params,sizeof(double));
    param_ini = (double *) calloc(m_num_params,sizeof(double));
  
    if(param_min == NULL || param_max == NULL || param_ini == NULL)
      QUESO_fatal("Unable to allocate memory for desired parameter space");
  
    ierr *= grvy_input_fread_double_vec("queso/parameters/param_mins",  param_min, m_num_params);
    ierr *= grvy_input_fread_double_vec("queso/parameters/param_maxs",  param_max, m_num_params);
    ierr *= grvy_input_fread_double_vec("queso/parameters/param_inits", param_ini, m_num_params);

    printf("max = %f\n",param_max[0]);

    if(ierr == 0)
      QUESO_fatal("Unable to read parameter min/max/init values");

    for(int i = 0;i<m_num_params;i++)
      {
	grvy_printf(GRVY_INFO,"%s: min  value = \n",log_prefix,param_min[i]);
	grvy_printf(GRVY_INFO,"%s: max  value = \n",log_prefix,param_max[i]);
	grvy_printf(GRVY_INFO,"%s: init value = \n",log_prefix,param_ini[i]);
      }

    m_queso_var_min = new basicV ( m_paramSpace->zeroVector() );
    m_queso_var_max = new basicV ( m_paramSpace->zeroVector() );
    m_queso_var_ini = new basicV ( m_paramSpace->zeroVector() );
    
    for(int i = 0;i<m_num_params;i++)
      {
	(*m_queso_var_min)[i] = param_min[i];
	(*m_queso_var_max)[i] = param_max[i];
	(*m_queso_var_ini)[i] = param_ini[i];
      }
    
    printf("--> Setup parameter search box...\n");
    m_paramDomain = new uqBoxSubsetClass<basicV, basicM> ("queso_basic_",*m_paramSpace,
							  *m_queso_var_min,*m_queso_var_max);
    // un poquito de clean up.

    free(param_min);
    free(param_max);
    free(param_ini);

    grvy_input_fclose();

  }