static Evaluation twoPhaseSatKrn(const Params &params, const Evaluation& Sw)
    {
        if (Sw <= params.krnSpline().xMin())
            return Evaluation(params.krnSpline().yFirst());
        if (Sw >= params.krnSpline().xMax())
            return Evaluation(params.krnSpline().yLast());

        return params.krnSpline().eval(Sw);
    }
    static Evaluation twoPhaseSatPcnw(const Params &params, const Evaluation& Sw)
    {
        // this assumes that the capillary pressure is monotonically decreasing
        if (Sw <= params.pcnwSpline().xMin())
            return Evaluation(params.pcnwSpline().yFirst());
        if (Sw >= params.pcnwSpline().xMax())
            return Evaluation(params.pcnwSpline().yLast());

        return params.pcnwSpline().eval(Sw);
    }
    static Evaluation twoPhaseSatKrnInv(const Params &params, const Evaluation& krn)
    {
        static const Evaluation nil(0.0);

        if (krn >= params.krnSpline().yFirst())
            return Evaluation(params.krnSpline().xMin());
        if (krn <= params.krnSpline().yLast())
            return Evaluation(params.krnSpline().xMax());

        return params.krnSpline().intersect(/*a=*/nil, /*b=*/nil, /*c=*/nil, /*d=*/krn);
    }
    static Evaluation twoPhaseSatPcnwInv(const Params &params, const Evaluation& pcnw)
    {
        static const Evaluation nil(0.0);

        // this assumes that the capillary pressure is monotonically decreasing
        if (pcnw >= params.pcnwSpline().yFirst())
            return Evaluation(params.pcnwSpline().xMin());
        if (pcnw <= params.pcnwSpline().yLast())
            return Evaluation(params.pcnwSpline().xMax());

        // the intersect() method of splines is a bit slow, but this code path is not too
        // time critical...
        return params.pcnwSpline().intersect(/*a=*/nil, /*b=*/nil, /*c=*/nil, /*d=*/pcnw);
    }
Exemplo n.º 5
0
    static Evaluation liquidEnthalpy(const Evaluation& temperature, const Evaluation& pressure)
    {
        // Gauss quadrature rule:
        // Interval: [0K; temperature (K)]
        // Gauss-Legendre-Integration with variable transformation:
        // \int_a^b f(T) dT  \approx (b-a)/2 \sum_i=1^n \alpha_i f( (b-a)/2 x_i + (a+b)/2 )
        // with: n=2, legendre -> x_i = +/- \sqrt(1/3), \apha_i=1
        // here: a=0, b=actual temperature in Kelvin
        // \leadsto h(T) = \int_0^T c_p(T) dT
        //                 \approx 0.5 T * (cp( (0.5-0.5*\sqrt(1/3)) T) + cp((0.5+0.5*\sqrt(1/3)) T))
        //                = 0.5 T * (cp(0.2113 T) + cp(0.7887 T) )

        // enthalpy may have arbitrary reference state, but the empirical/fitted heatCapacity function needs Kelvin as input
        return 0.5*temperature*(liquidHeatCapacity(Evaluation(0.2113*temperature), pressure)
                                + liquidHeatCapacity(Evaluation(0.7887*temperature), pressure));
    }
Exemplo n.º 6
0
double Min(State * s_in, int depth, double alpha, double bata)
{
	if(TerminalTest(s_in) || depth <= 0)
	{
		return Evaluation(s_in);
	}
	std::vector<Action> actions = s_in->ActionList();
	double best_v = -infinity;
	Action dummy_out;
	for( std::vector<Action>::iterator a = actions.begin(); a != actions.end(); a++)
	{
		State *s2 = s_in->NewState(*a);
		double V = Max(s2, dummy_out, depth-1, alpha, beta);
		if(V < best_v)
		{
			beta = V;
			best_v = V;
		}
		if(V < alpha)
		{
			break;
		}
		delete s2;
		s2 = 0;
	}
	return best_v;
}
Exemplo n.º 7
0
double Max(State * s_in, Action & action_out, int depth, double alpha, double beta)
{
	if(TerminalTest(s_in) || depth <= 0)
	{
		action_out = Action::none;
		return Evaluation(s_in); //allways evaluate from our prespective not the oppenents
	}
	std::vector<Action> actions = s_in->ActionList();
	double best_v = -infinity;
	for( std::vector<Action>::iterator a = actions.begin(); a != actions.end(); a++)
	{
		State *s2 = s_in->NewState(*a);
		double V = Min(s2, depth-1, alpha, beta);
		if(V > best_v)
		{
			alpha = V;
			best_v = V;
			action_out = *a;
		}
		if(V > beta)
		{	
			break;
		}
		delete s2;
		s2 = 0;
	}
	return best_v;
}
Exemplo n.º 8
0
    static Evaluation krn(const Params &params,
                          const FluidState &fluidState)
    {
        typedef MathToolbox<Evaluation> Toolbox;
        typedef MathToolbox<typename FluidState::Scalar> FsToolbox;

        Scalar Swco = params.Swl();

        Evaluation Sw =
            Toolbox::max(Evaluation(Swco),
                         FsToolbox::template decay<Evaluation>(fluidState.saturation(waterPhaseIdx)));
        Evaluation Sg = FsToolbox::template decay<Evaluation>(fluidState.saturation(gasPhaseIdx));

        Evaluation Sw_ow = Sg + Sw;
        Evaluation So_go = 1.0 - Sw_ow;
        const Evaluation& kro_ow = OilWaterMaterialLaw::twoPhaseSatKrn(params.oilWaterParams(), Sw_ow);
        const Evaluation& kro_go = GasOilMaterialLaw::twoPhaseSatKrw(params.gasOilParams(), So_go);

        // avoid the division by zero: chose a regularized kro which is used if Sw - Swco
        // < epsilon/2 and interpolate between the oridinary and the regularized kro between
        // epsilon and epsilon/2
        const Scalar epsilon = 1e-5;
        if (Toolbox::scalarValue(Sw_ow) - Swco < epsilon) {
            Evaluation kro2 = (kro_ow + kro_go)/2;;
            if (Toolbox::scalarValue(Sw_ow) - Swco > epsilon/2) {
                Evaluation kro1 = (Sg*kro_go + (Sw - Swco)*kro_ow)/(Sw_ow - Swco);
                Evaluation alpha = (epsilon - (Sw_ow - Swco))/(epsilon/2);
                return kro2*alpha + kro1*(1 - alpha);
            }

            return kro2;
        }
        else
            return (Sg*kro_go + (Sw - Swco)*kro_ow)/(Sw_ow - Swco);
    }
void 
AsynchronousApplication::
perform_evaluation_impl( const utilib::Any &domain,
                         const AppRequest::request_map_t &requests,
                         utilib::seed_t &seed,
                         AppResponse::response_map_t &responses )
{
   Any evalID = spawn_evaluation(domain, requests, seed);

   while ( true )
   {
      AppResponse::response_map_t tmp_responses;
      utilib::seed_t tmp_seed;
      Any tmp_evalId;

      tmp_evalId = async_collect_evaluation(tmp_responses, tmp_seed);
      try {
         if ( evalID.references_same_data_as(tmp_evalId) ||
              evalID == tmp_evalId )
         {
            responses = tmp_responses;
            seed = tmp_seed;
            return;
         }
         else
         {
            evaluatedBuffer.push_back
               ( Evaluation(tmp_evalId, tmp_seed, tmp_responses) );
         }
      } catch ( utilib::any_not_comparable& ) {
         // silently ignore non-comparable EvalIDs
      }
   }
}
Exemplo n.º 10
0
ToolState::ToolState() : current_evaluation(Evaluation()){
  dm = states::Single_Image;
  ds = states:: Enabled_Datasets;
  display_modifier = states::No_Modifier;

  current_image_page = 0;
  enabled_classifier = 0;
  enabled_feature = 0;

  ims_per_page = 16;
  parameter_panel = 0;
  current_classifier = 0;
  selected_class_listbox = 0;
  main_gui = 0;
  classes = 0;
  stats = 0;
  current_single_image_texture = 0;
  singleDp = 0;
}
Exemplo n.º 11
0
 static Evaluation gasDensity(const Evaluation& temperature, const Evaluation& pressure)
 { return IdealGas<Scalar>::density(Evaluation(molarMass()), temperature, pressure); }
Exemplo n.º 12
0
int * Simulate(NODE *graph,int Max,int fault_node,int type_of_fault,int npi)
{
LIST *fanin,*temp;
LIST *frontier=NULL;
LIST *event = NULL;
LIST *improve = NULL;
char line[Mlin],str1[Mlin];
int i,j,length,output_number ,k,index;
int inp_value,inp_node;
int node_id,value,input_value,line_number,next_value;
int test,check,obj,eof_flag;
int f_node,f_value,detected,new_input,error,flag;
//error reports
int coverage,event_null,nodes_number;
coverage = event_null = nodes_number = 0;

	//init_node(graph , Max);
	depth(graph,Max);
controllability(graph,Max);

clock_t begin,end;
begin = clock();

int * vector = (int *) malloc(npi*sizeof(int));

nodes_number++;
	detected = 0;
	error = 0;
	f_node = node_id =fault_node;
	f_value = value = type_of_fault;
	while(detected != 1 && error != 1){
	new_input = 0;
	controllability(graph,Max);
	obj = objective(graph,Max,&node_id,&value,f_node,f_value,&frontier);
	backtrace(graph,node_id,value,&inp_value,&inp_node,&new_input);
	push (&event,inp_node);
	detected = Evaluation(graph,Max,inp_node,inp_value,f_node,f_value);
	d_frontier(graph,Max,&frontier);
		if(detected == 1){
			 //printf("\n------------------------DETECTED-----------------\n");
			for(j=0;j<=Max;j++){
				if(graph[j].Type == INPT){
					vector[j-1] = graph[j].Cval;
					//fprintf(fres,"%d",input_value);	
				}
			}
			coverage++;
		} else {
			if(obj == 1 && frontier == NULL){
				 if(detected != 1)
					//printf("\n----------NOT DETECTED--------------(path blocked)");
			error = 1;
			}
			if(obj == 3) {
				error = 1;
				//printf("\n----------NOT DETECTED--------------(fault masked)");
			} 
			if(new_input ==0 ) {
				error = 1;
				//printf("\n----------NOT NEW INPUT -----------");
			}
			while(error == 1){
				//printf("\n------------trying to backtrack-------------");
				inp_node = pop(&event);
				inp_value = invert(inp_value);
				frontier=NULL;
				detected = Evaluation(graph,Max,inp_node,inp_value,f_node,f_value);
				d_frontier(graph,Max,&frontier); 
				if(event == NULL){
					error = 0;
					detected = 1;
					//printf("\ncase event null");
					event_null++;
					push (&improve,f_node); 
				}else if(detected != 1 && frontier != NULL) {
					error = 0;
					//printf("\ncase continue");
				}else if(detected == 1){
					error = 0;
					detected = 1;
					for(j=0;j<=Max;j++){
						if(graph[j].Type == INPT){
							vector[j-1] = graph[j].Cval;
							//fprintf(fres,"%d",input_value);	
						}
					}
					//printf("\n------------------------DETECTED-----------------\n");
					coverage++;
				}else if(frontier == NULL) {
					error = 1;
					//printf("\ncase next value from event list");
				}
			}
		}
	}
	//init_node(graph , Max);
	if(frontier!=NULL) frontier=NULL; event = NULL;

/*
//try to improve the coverage
//printf("\n-----TRY to improve the coverage");
temp = improve;
while(temp!=NULL){
detected = 0;
error = 0;
f_node = node_id =temp->id;
f_value = value = type_of_fault;
	while(detected != 1 && error != 1){
	new_input = 0;
	controllability(graph,Max);
	obj = objective(graph,Max,&node_id,&value,f_node,f_value,&frontier);
	backtrace(graph,node_id,value,&inp_value,&inp_node,&new_input);
	push (&event,inp_node);
	detected = Evaluation(graph,Max,inp_node,inp_value,f_node,f_value);
	d_frontier(graph,Max,&frontier);
	if(detected == 1){
		//printf("\n------------------------DETECTED AT FIRST BACKTRACK-----------------\n");
			for(j=0;j<=Max;j++){
				if(graph[j].Type == INPT){
					vector[j-1] = graph[j].Cval;
					//fprintf(fres,"%d",input_value);	
				}
			}
		coverage++;
	} else {
		if(obj == 1 && frontier == NULL){
			 if(detected != 1) {
				//printf("\n----------NOT DETECTED--------------(path blocked)");
			} 
		error = 1;
		}
		if(obj == 3) {
			error = 1;
			//printf("\n----------NOT DETECTED--------------(fault masked)");
			} 
		if(new_input ==0 ) {
			error = 1;
			//printf("\n----------NOT NEW INPUT -----------");
			}
		if(error == 1){
			//printf("\n------------trying to backtrack-----------------");
			inp_node = pop(&event);
			graph[inp_node].Cval = 2;
			inp_node = pop(&event);
			inp_value = invert(inp_value);
			frontier=NULL;
			detected = Evaluation(graph,Max,inp_node,inp_value,f_node,f_value);
			d_frontier(graph,Max,&frontier); 
			if(event == NULL){
				error = 0;
				detected = 1;
				//printf("\ncase event null"); 
			}else if(detected != 1 && frontier != NULL) {
				error = 0;
				//printf("\ncase continue");
			}else if(detected == 1){
				error = 0;
				detected = 1;
				//printf("\n------------------------DETECTED AT FIRST BACKTRACK-----------------\n");
				for(j=0;j<=Max;j++){
					if(graph[j].Type == INPT){
						vector[j-1] = graph[j].Cval;
						//fprintf(fres,"%d",input_value);	
					}
				}
				coverage++;
			}else if(frontier == NULL) {
				error = 0;
			}
		}
	}
	}
	//init_node(graph , Max);
	if(frontier!=NULL) frontier=NULL; event = NULL;
temp = temp->next;
}*/
printf("\nTotal number of nodes %d and faults detected %d and event list is empty %d and not detected %d \n",nodes_number,coverage,event_null,nodes_number-coverage);
printf("------------------------RESULTS---------------------------\n\tTOTAl FAULTS->%d\n\tDETECTED->%d\n\tPERCENTAGE->%g\r\n",nodes_number,coverage,(((double)coverage)/nodes_number*100));
end = clock();
printf("\nTIME SPENT TO GENERATE THE PATTERNS %f\n",(double)(end-begin)/CLOCKS_PER_SEC);
return vector;
}//end of Simulate function
Exemplo n.º 13
0
void fit(struct rec *t_func,struct data *L_data){
	
	int i,j,k=0,Nm,Np;
	double *w;
	double sum_p=0,sum_m=0;
	double u_w_x=0,u_w_y=0,s_y=0,t_y;
	double *jm_x_p,*jm_x_m,*jm_y_p,*jm_y_m,*sepa,*alpha;
	double db_x_p,db_x_m,db_y_p,db_y_m;	
	double thore;
	double min;
	double best;
	int best_x_p,best_x_m,best_y_p,best_y_m,*s_flag;

        w=(double *)malloc(sizeof(double)*L_data[0].num);
	jm_x_p=(double *)malloc(sizeof(double)*L_data[0].num);	
	jm_x_m=(double *)malloc(sizeof(double)*L_data[0].num);
	jm_y_p=(double *)malloc(sizeof(double)*L_data[0].num);
	jm_y_m=(double *)malloc(sizeof(double)*L_data[0].num);
	sepa=(double *)malloc(sizeof(double)*L_data[0].num);
	s_flag=(int *)malloc(sizeof(int)*L_data[0].num);
	alpha=(double *)malloc(sizeof(double)*L_data[0].num);
	printf("1\n");

	Nm = 0;
	Np = 0;
	for(i=0;i<L_data[0].num;i++)
	{
		if(L_data[i].qul == 1)
		{
			Np++;
		}
		if(L_data[i].qul == -1)
                {
                        Nm++;
                }
	}
	
	for(i=0;i<L_data[0].num;i++)
        {
	
        	w[i]=1.0/(2*Np);
		
        }
Label:
        for(i=0;i<L_data[0].num-1;i++)
        {
               jm_x_p[i]=0.0;
               jm_y_p[i]=0.0;
               jm_x_m[i]=0.0;
               jm_y_m[i]=0.0;
        }

	printf("2\n");
//thoreshold x


	printf("loop %d\n",k);	
	for(i=0;i<L_data[0].num-1;i++)
        {
	 for(j=0;j<L_data[0].num;j++)
        	{
		thore = t_func[i].x;
		jm_x_p[i] += w[j] * w_func_x_p(thore,L_data,j);
		jm_x_m[i] += w[j] * w_func_x_m(thore,L_data,j);
		u_w_x += w[j];
//		printf("w_x_p %lf w_x_m %lf w_f_x_p %lf w_f_x_m %lf\n",w_x_p[i][j],w_x_m[i][j],w_func_x_p(thore,L_data,j),w_func_x_m(thore,L_data,j));
//		printf("x %lf y %lf label %d thore_x %lf jm_x_p %lf jm_x_m %lf\n",L_data[j].x,L_data[j].y,L_data[j].qul,t_func[i].x,jm_x_p[i],jm_x_m[i]);
		}
		jm_x_p[i] /= u_w_x;
		jm_x_m[i] /= u_w_x;
		printf("jm_x_p %lf jm_x_m %lf\n",jm_x_p[i],jm_x_m[i]);
                u_w_x = 0;
//		printf("thore_x %lf number%d sum_p %lf sum_m %lf\n",t_func[i].x,i,sum_p,sum_m);
        }

	min = jm_x_p[0];
	best_x_p = 0;
        printf("0 rec_x_p %lf rec_x_l %lf\n",t_func[0].x,jm_x_p[0]);

	for(i=1;i<L_data[0].num-1;i++)
        {
		printf("%d rec_x_p %lf rec_x_l %lf\n",i,t_func[i].x,jm_x_p[i]);
		if(min > jm_x_p[i])
		{
			min = jm_x_p[i];
			best_x_p = i;
		}
	}
        printf("%d learn %d min_rec_x_p %lf\n",k,best_x_p,min);

        min = jm_x_m[0];
        best_x_m = 0;
        printf("0 rec_x_m %lf rec_x_m_l %lf\n",t_func[0].x,jm_x_m[0]);
	
        for(i=1;i<L_data[0].num-1;i++)
        {
                printf("%d rec_x_m %lf rec_x_l %lf\n",i,t_func[i].x,jm_x_m[i]);
                if(min > jm_x_m[i])
                {
                        min = jm_x_m[i];
                        best_x_m = i;
                }
        }
	printf("%d learn %d min_rec_x_m %lf\n",k,best_x_m,min);


//thoreshold y
        for(i=0;i<L_data[0].num-1;i++)
        {
         for(j=0;j<L_data[0].num;j++)
                {
                thore = t_func[i].y;
                jm_y_p[i] += w[j] * w_func_y_p(thore,L_data,j);       
		jm_y_m[i] += w[j] * w_func_y_m(thore,L_data,j);
		u_w_y += w[j];
//		printf("w_y_m %lf w_y_f_m %lf\n",w_y_m[i][j], w_func_y_m(thore,L_data,j));
		 }
//		printf("jm_y_p %lf jm_y_m %lf\n",jm_y_p[i],jm_y_m[i] );
		jm_y_p[i] /= u_w_y;
                jm_y_m[i] /= u_w_y;
//        	printf("jm_y_p %lf jm_y_m %lf\n",jm_y_p[i],jm_y_m[i] );
		printf("u_w_y %lf\n",u_w_y);
		u_w_y = 0;
	}

        min = jm_y_p[0];
	best_y_p = 0;
	printf("0 rec_y_p %lf rec_y_l %lf\n",t_func[0].y,jm_y_p[0]);
        
	for(i=1;i<L_data[0].num-1;i++)
        {
        	printf("%d rec_y_p %lf rec_y_l %lf\n",i,t_func[i].y,jm_y_p[i]);
	        if(min > jm_y_p[i])
                {
                        min = jm_y_p[i];
                        best_y_p = i;
                }
        }
        printf("%d learn %d min_rec_y_p %lf\n",k,best_y_p,min);	

    	min = jm_y_m[0];
        best_y_m = 0;
        printf("0 rec_y_m %lf rec_y_l %lf\n",t_func[0].y,jm_y_m[0]);
        
	for(i=1;i<L_data[0].num-1;i++)
        {
                printf("%d rec_y_m %lf rec_y_l %lf\n",i,t_func[i].x,jm_y_m[i]);
                if(min > jm_y_m[i])
                {
                        min = jm_y_m[i];
                        best_y_m = i;
                }
        }
        printf("%d learn %d min_rec_y_m %lf\n",k,best_y_m,min);

// best function search
	printf("3\n");
	
//	printf("x_%d %d\n",k,best_x);
	sepa[k] = t_func[best_x_p].x;
	s_flag[k] = 1;
	alpha[k] = log((1-jm_x_p[best_x_p])/jm_x_p[best_x_p]);
	db_x_p = alpha[k];
//	printf("best_x_p %d rec_x_p %lf jm_x_p %lf\n",best_x_p,sepa[k],jm_x_p[best_x_p]);
//	printf("alpha %lf\n",alpha[k]);
//	printf("s_flag %d\n",s_flag[k]);	
	
	best = jm_x_p[best_x_p];
	
	if(best > jm_x_m[best_x_m] )
	{
		best = jm_x_m[best_x_m];
//		printf("y_%d %d\n",k,best_y);
		sepa[k] = t_func[best_x_m].x;
		s_flag[k] = 2; 
	        alpha[k] = log((1-jm_x_m[best_x_m])/jm_x_m[best_x_m]);
  		
//		printf("rec_x_m %lf\n",sepa[k]);
//		printf("alpha %lf\n",alpha[k]);
//		printf("s_flag %d\n",s_flag[k]);
	      	
	
       	}
	db_x_m =  log((1-jm_x_m[best_x_m])/jm_x_m[best_x_m]);

        if(best > jm_y_p[best_y_p] )
        {
                best = jm_y_p[best_y_p];
//              printf("y_%d %d\n",k,best_y);
                sepa[k] = t_func[best_y_p].y;
                s_flag[k] = 3; 
                alpha[k] = log((1-jm_y_p[best_y_p])/jm_y_p[best_y_p]);

//                printf("rec_y_p %lf\n",sepa[k]);
//                printf("alpha %lf\n",alpha[k]);
//		printf("s_flag %d\n",s_flag[k]);
                
		thore = t_func[best_y_p].y;

        }
	db_y_p =  log((1-jm_y_p[best_y_p])/jm_y_p[best_y_p]);

        if(best > jm_y_m[best_y_m] )
        {
                best = jm_y_m[best_y_m];
//              printf("y_%d %d\n",k,best_y);
                sepa[k] = t_func[best_y_m].y;
                s_flag[k] = 4; 
                alpha[k] = log((1-jm_y_m[best_y_m])/jm_y_m[best_y_m]);

//                printf("rec_y_m %lf\n",sepa[k]);
//                printf("alpha %lf\n",alpha[k]);
//		printf("s_flag %d\n",s_flag[k]);


        }
	db_y_m =  log((1-jm_y_m[best_y_m])/jm_y_m[best_y_m]);

        printf("%d alpha_x_p %lf alpha_x_m %lf alpha_y_p %lf alpha_y_m %lf\n",k,db_x_p,db_x_m,db_y_p,db_y_m);
	
	printf("alpha_best %lf\n",alpha[k]);

	if(best == jm_x_p[best_x_p])
        {
	thore = t_func[best_x_p].x;
	for(j=0;j<L_data[0].num;j++)
        	{
        	w[j] = w[j] * exp(alpha[k] * w_func_x_p(thore,L_data,j));
//        	printf("%lf\n",w[best_x][j]);
        	}
	}
	
        if(best == jm_x_m[best_x_m])
        {
        thore = t_func[best_x_m].x;
        for(j=0;j<L_data[0].num;j++)
                {
                w[j] = w[j] * exp(alpha[k] * w_func_x_m(thore,L_data,j));
//              printf("%lf\n",w[best_x][j]);
                }
        }

        if(best == jm_y_p[best_y_p])
        {
        thore = t_func[best_y_p].y;
        for(j=0;j<L_data[0].num;j++)
                {
                w[j] = w[j] * exp(alpha[k] * w_func_y_p(thore,L_data,j));
//              printf("%lf\n",w[best_x][j]);
                }
        }

        if(best == jm_y_m[best_y_m])
        {
        thore = t_func[best_y_m].y;
        for(j=0;j<L_data[0].num;j++)
                {
                w[j] = w[j] * exp(alpha[k] * w_func_y_m(thore,L_data,j));
//              printf("%lf\n",w[best_x][j]);
                }
        }

	k++;
// Evaluation
	if(k>=L_data[0].num-1)
	{
		printf("4\n");
		Evaluation(s_flag,sepa,alpha,L_data);
		return;

	}

	goto Label;

}
Exemplo n.º 14
0
 static Evaluation gasDensity(const Evaluation& temperature, const Evaluation& pressure)
 {
     // Assume an ideal gas
     return IdealGas::density(Evaluation(molarMass()), temperature, pressure);
 }
Exemplo n.º 15
0
/// evaluate best possible move up to a certain level
/// this is done recursively by a form of the NegaMax algorithm with alpha-beta pruning
Evaluation Game::_evaluateMoves(int level, float alpha, float beta, bool initialCall) {
  KBX::Logger log("evaluation");
  if ((level == 0) || (this->getWinner() != NONE_OF_BOTH)) {
    return Evaluation(this->_rate(this->_nextPlayer));
  }
  // get rating, either directly or by recursive call
  float rating;
  // container for best move candidates
  std::priority_queue< Evaluation, std::vector< Evaluation >, Evaluation::less > candidates;
  // limit indices to significant color
  size_t from, to;
  if (this->_nextPlayer == WHITE) {
    from = 0;
    to = 8;
  } else {
    from = 9;
    to = 17;
  }
  // iterate over all dice of a color
  for (size_t d = from; d <= to; d++) {
    // get value of current die
    size_t value = this->_dice[d].getValue();
    // iterate over max number of moves for given dice value (stored in state-array)
    for (size_t i = 0; i < DieState::nPossibleMoves[value]; i++) {
      // abort evaluation if cancelled
      if (this->cancelled()) {
        return Evaluation(0.0f);
      }
      // check if this specific move is valid
      RelativeMove move = DieState::possibleMoves[value][i];
      if (this->moveIsValid(Move(d, move))) {
        // store all data to undo move later
        RelativeMove moveBack = move.invert();
        // kill the die lying on the target field
        int idDieOnTarget = this->_fields[this->_dice[d].x() + move.dx][this->_dice[d].y() + move.dy];
        // perform move
        this->makeMove(Move(d, move), false);
        // recursive call for next step (negative weighting, since it is opponent's turn)
        rating = - this->_strategy.patience * this->_evaluateMoves(level - 1, -beta, -alpha, false).rating;
        // undo move
        this->makeMove(Move(d, moveBack), false);
        // revive killed die on target field
        if (idDieOnTarget != CLEAR) {
          this->reviveDie(idDieOnTarget);
        }
        // alpha-beta pruning
        if (rating >= beta) {
          return Evaluation(rating);
        }
        if (rating > alpha) {
          alpha = rating;
          if (initialCall == true) {
            // add move to candidate list
            candidates.push(Evaluation(rating, Move(d, move)));
          }
        }
      }
    }
  }
  if (initialCall == true) {
    if ( ! candidates.empty()) {
      float topRating = candidates.top().rating;
      std::vector<Evaluation> topCandidates;
      log.info(stringprintf("top rating: %0.4f", topRating));
      while ((candidates.top().rating >= topRating) && ( ! candidates.empty())) {
        topCandidates.push_back(candidates.top());
        candidates.pop();
      }
      log.info(stringprintf("next rating: %0.4f", candidates.top().rating));
      log.info(stringprintf("no. of top candidates: %d", topCandidates.size()));
      // randomly select from equally rated top candidates
      std::size_t iTop = randomIndex(0, topCandidates.size()-1);
      return topCandidates[iTop];
    }
  }
  return Evaluation(alpha);
}