void MainWindow::pull(double x1, double y1, double x2, double y2, double speed_factor)
{
    QString str;
    if (!ui->label_6->isVisible()){
        if (N_p == 0) {
            ui->label_2->setText("<FONT COLOR=#FF0000>Player 1</FONT>");
            ui->label_3->setText("<FONT COLOR=#000000>Player 2</FONT>");
        }

        if (N_p == 1) {
            ui->label_2->setText("<FONT COLOR=#000000>Player 1</FONT>");
            ui->label_3->setText("<FONT COLOR=#FF0000>Player 2</FONT>");
        }

        double sp = billiard->pull(x1, y1, x2, y2, speed_factor);
        str.setNum(sp);
        ui->textEdit->setText(str);
        ui->label_6->setVisible(1);
        while (!stop_cond() && !stop) {
            QApplication::processEvents();
            billiard->move();
            update_image();
        }

        if ((balls - billiard->left_balls) > 0) {
            p[N_p] += (balls - billiard->left_balls);
        } else {
            // N_p = ~N_p;
            if (N_p == 1)
                N_p = 0;
            else {
                if (N_p == 0)
                    N_p = 1;
            }
        }

        balls = billiard->left_balls;
        ui->label_6->setVisible(0);
        str.setNum(p[0]);
        ui->label_4->setText(str);
        str.setNum(p[1]);
        ui->label_5->setText(str);
        if (N_p == 0) {
            ui->label_2->setText("<FONT COLOR=#FF0000>Player 1</FONT>");
            ui->label_3->setText("<FONT COLOR=#000000>Player 2</FONT>");
        }

        if (N_p == 1) {
            ui->label_2->setText("<FONT COLOR=#000000>Player 1</FONT>");
            ui->label_3->setText("<FONT COLOR=#FF0000>Player 2</FONT>");
        }
    }
}
Example #2
0
static unsigned char rx (int nak, int send_stop) {
   unsigned char byte = 0;
   unsigned char bit;

   for(bit = 0; bit < 8; bit++) {
    byte <<= 1;      
    byte |= read_bit();      
   }
   write_bit(nak);
   if (send_stop) {
      stop_cond();
   }
   return byte;
}
Example #3
0
unsigned char rx(bool nak, bool send_stop)
{
	unsigned char byte = 0;
	unsigned bit;
 
	for (bit = 0; bit < 8; bit++) {
		byte |= read_bit();
		byte <<= 1;
	}
	write_bit(nak);
	if (send_stop)
		stop_cond();
	return byte;
}
Example #4
0
static unsigned char tx(int send_start, int send_stop, unsigned char byte) {
   unsigned char bit;
   unsigned char nack;
   if (send_start) {
      start_cond();
   }
   for (bit = 0; bit < 8; bit++) {
      write_bit(byte & 0x80);
      byte <<= 1;
   }
   nack = read_bit();
   if (send_stop) {
      stop_cond();
   }
   return nack;
}
Example #5
0
bool tx(bool send_start, bool send_stop, unsigned char byte)
{
	unsigned bit;
	unsigned nack;
 
	if (send_start)
		start_cond();
	for (bit = 0; bit < 8; bit++) {
		write_bit(byte & 0x80);
		byte <<= 1;
	}
	nack = read_bit();
	if (send_stop)
		stop_cond();
 
	return nack;
}
Example #6
0
int MCLR_SM::Active_Query()
{
	stop_training = false;

	// The max_info value and hence the algo converges if the user successively selects 
	//"I am not sure option" .. so whenever the user selects the "I am not sure option
	// delete that info value from max_info;
	if(current_label==0)
		max_info_vector.erase(max_info_vector.begin()+max_info_vector.size()-1);

	
	max_info = -1e9;
	int active_query = 0;
	//vnl_vector<double> diff_info_3_it(3,0);//difference in g vals for last 3 iterations
	//vnl_vector<double> g_3_it(3,0);	// g vals for the last three

	vnl_matrix<double> test_data_just_features =  test_data.transpose();
	//test_data_just_features = test_data_just_features.get_n_rows(0,test_data_just_features.rows()-1);
	vnl_matrix<double> prob = Test_Current_Model(test_data_just_features);
		

	vnl_matrix<double> test_data_bias = Add_Bias(test_data_just_features);
	vnl_vector<double> info_vector(test_data_just_features.cols());

	//Compute Information gain
	for(int i =0; i< test_data_just_features.cols();++i )
	{
		vnl_vector<double> temp_col_prob = prob.get_column(i);
		vnl_vector<double> temp_col_data = test_data_bias.get_column(i);		
		vnl_matrix<double> q = Kron(temp_col_prob,temp_col_data);//Kronecker Product
		vnl_diag_matrix<double> identity_matrix(no_of_classes,1); // Identity Matrix;
		double infoval = (q.transpose() * m.CRB.transpose() * q).get(0,0);
		vnl_matrix<double> info_matrix(no_of_classes,no_of_classes,infoval);
		info_matrix = identity_matrix + info_matrix ;
		info_vector(i) = log(vnl_determinant(info_matrix));
		if(info_vector(i)>max_info)
		{
			active_query = i;
			max_info = info_vector(i);
		}
	}

	max_info_vector.push_back(max_info);

//	std::cout<<max_info_vector.size() << "--" << max_info <<std::endl;

	if(max_info_vector.size()>1)
	  diff_info_3_it((max_info_vector.size()-1)%3) =  max_info_vector.at((max_info_vector.size()-1)) - max_info_vector.at((max_info_vector.size()-2));


	 info_3_it((max_info_vector.size()-1)%3) = max_info;	
	
	if (max_info_vector.size()>3) 
	{
		int sum = 0;
		for(int iter =0; iter < 3; iter++)
		{
			//std::cout<<fabs(diff_info_3_it(iter))/fabs(info_3_it(iter))<<std::endl;

			if(fabs(diff_info_3_it(iter))/fabs(info_3_it(iter)) < stop_cond(1))
			{
				sum = sum + 1;	
			}
		}
		// The algorithm is now confident about the problem
		// Training can be stopped
		if(sum==3)
			stop_training = true;
	}

	return active_query;
}
Example #7
0
MCLR_SM::model MCLR_SM::Get_Training_Model()
{	
	// Set the z matrix 
	z.set_size(no_of_classes,x.cols());// Used in gradient computation
	z.fill(0);
	 

	// Create the z matrix
	for(int i=0;i<x.cols();++i)
	{
		vnl_vector<double> temp_vector = z.get_column(i);
		temp_vector.put(y.get(i)-1,1);
		z.set_column(i,temp_vector);
	}


	vnl_vector<double> diff_g_3_it(3,0);//difference in g vals for last 3 iterations
	vnl_vector<double> g_4_it(4,0);	// g vals for the last three

	//std::cout<<x.cols()<<std::endl;
	//std::cout<<test_data.rows()<<std::endl;
	//std::cout<< x.get_n_columns(0,2)<< std::endl; 

	for(int i =0;i<1e10;++i)
	{	
		//Get the gradient
		Get_Gradient(Add_Bias(x));
		
		//Get the hessian
		Get_Hessian(Add_Bias(x));

		//ameliorate hessian conditions
		Ameliorate_Hessian_Conditions();	
		

		//Get the direction 
//		vnl_vector<double> dir = mclr->Newton_Direction(mclr->hessian,mclr->Column_Order_Matrix(mclr->gradient_w));
//		mclr->direction = mclr->Reshape_Vector(dir,mclr->no_of_features+1,mclr->no_of_classes);
		direction = Reshape_Vector(Newton_Direction(hessian,Column_Order_Matrix(gradient_w)),no_of_features+1,no_of_classes);

		double step = logit_stepsize();		
		if(step == -1)
			step = 1e-9;
		
		m.w = m.w + step * direction;

		g_4_it(i%4) = g;	

		if(i>1)
			diff_g_3_it(i%3) = g_4_it(i%4) - g_4_it((i-1)%4);
    
		if (i>3 && (diff_g_3_it.one_norm()/3)/(g_4_it.one_norm()/4) < stop_cond(0))
			break;
	}
	
	

	m.FIM = hessian*-1;
	m.CRB = vnl_matrix_inverse<double>(hessian);
	

	// quirk of vnl ...
	m.CRB = m.CRB*-1;
	//std::cout<<vnl_trace(m.CRB)<<std::endl;


 //   FILE * fpin1 = FDeclare2("C:\\Users\\rkpadman\\Documents\\MATLAB\\crb.txt", "", 'w');

	//for(int abc =0;abc<hessian.rows();++abc)
	//{	
	//	vnl_vector<double> temp = hessian.get_row(abc);
	//	for(int abcd =0;abcd<hessian.cols();++abcd)
	//	{
	//		fprintf(fpin1, "%lf    ", temp[abcd]);
	//	}
	//	fprintf(fpin1, "\n");
	//}



	//fclose(fpin1);
	top_features.clear();
	top_features = Get_Top_Features();

	return m;

}