예제 #1
0
/*
   read level & print its map
*/
void PrintLevel( BCINT episode, BCINT mission)
{
   ReadLevelData( episode, mission);
   /* define map center */
   MapCenterX = (MapMinX + MapMaxX) / 2;
   MapCenterY = (MapMinY + MapMaxY) / 2;

   /* initialize, scale & draw the page */
   InitPage( episode, mission, ( UserLvlNm != NULL ? UserLvlNm : LevelName ));

   InitScale( MapMaxX - MapMinX, MapMaxY - MapMinY,
	      ( UserLvlNm != NULL ? UserLvlNm : LevelName ));
   if (FlagAutoZoom && (FlagName || FlagLegend))
   {
      BCINT n;
      for (n = 0; n < NumVertexes; n++)
	 /* rough & fine check for covering name or legend areas */
	 if (Vertexes[ n].y > MapCenterY)
	    if (CheckScale( Vertexes[ n].x, Vertexes[ n].y))
	       break; /* for */
      if (n < NumVertexes)
	 AdjustScale( MapMaxX - MapMinX, MapMaxY - MapMinY);
   }

   PrintMap();
   TermPage();

   /* clean up & free space */
   ForgetLevelData();
}
void LogisticRegression::TrainModel()
{
	cout << "Train ...." << endl;
	//load data
	string line;
	ifstream fin(this->str_train_data_file_.c_str());
	if(!fin)
	{
		cerr << "Can not read train data file !" << endl;
		exit(0);
	}

	while(getline(fin, line))
	{
		vector<double> vd_data = split2double(line, "\t");
		int label = (int)vd_data.back();
		vd_data.pop_back();
		this->vec_train_label_.push_back(label);
		this->vec_train_data_.push_back(vd_data);
	}
	fin.close();

	//data scale
	InitScale(this->vec_train_data_);
	ScaleData(this->vec_train_data_);

	//train
	size_t i_round = 0;
	while(i_round < this->i_max_round)
	{
		
		cout << "Rount [" << i_round << "]....." << endl;
		vector<double> douVec_gradient(this->vec_train_data_[0].size(), 0);
		double dou_bias_gradient = 0;
		double dou_cost = 0;
		for(int i = 0; i < this->vec_train_data_.size(); i++)
		{
			
			double dou_h = Sigmoid(this->vec_train_data_[i]);
			
			//calculate cost for each data
			dou_cost += (this->vec_train_label_[i]*log(dou_h) + (1-this->vec_train_label_[i])*log(1-dou_h));

			//calculate gradient for each data
			for(int j = 0; j < douVec_gradient.size(); j++)
			{
				douVec_gradient[j] += (this->vec_train_label_[i] - dou_h)*this->vec_train_data_[i][j];

			}

			dou_bias_gradient += (this->vec_train_label_[i] - dou_h);
			
		}

		//L2 regularization
		double dou_regularization = 0;
		for(int i = 0; i < this->douVec_weights_.size(); i++)
		{
			dou_regularization += pow(this->douVec_weights_[i], 2);
		}

		//final cost 
		dou_cost = -1.0 * dou_cost / this->douVec_weights_.size() + this->dou_lambda_*dou_regularization/(2*this->douVec_weights_.size());
		cout << "Cost J = " << dou_cost << endl;
		
		//update
		string str_new_weights("new weights is [ ");
		string str_gradient("gradient is [");
		for(int k = 0; k < douVec_gradient.size(); k++)
		{
			this->douVec_weights_[k] += this->dou_step_*(douVec_gradient[k]+this->dou_lambda_*this->douVec_weights_[k])/this->vec_train_data_.size();
			str_new_weights = str_new_weights + double2string(this->douVec_weights_[k]) + " ";
			str_gradient =  str_gradient + double2string(douVec_gradient[k]) + " ";
		}
		
		this->dou_bias_ += this->dou_step_*dou_bias_gradient/this->vec_train_data_.size();
		str_new_weights = str_new_weights +  double2string(this->dou_bias_) + "]";
		str_gradient = str_gradient + double2string(dou_bias_gradient) +  "]";
		cout << str_new_weights << endl;
		cout << str_gradient << endl;

		i_round += 1;
	}


	//save model
	cout << "save model ..." << endl;
	ofstream fout("./logreg_model.txt");
	cout << this->douVec_weights_.size() << endl;
	for(int i = 0;  i < this->douVec_weights_.size(); i++)
	{
		fout << this->douVec_weights_[i] << " ";
	}
	fout << this->dou_bias_ << endl;
	fout.close();

}
예제 #3
0
int WeaponSysFire(List *list, GameInstance *instance)
{
    short int i;
    Vector2D weapvel;
    Vector2D weappos;
    float proj_vel;
    int proj_type;
    float proj_lifetime;
    float degrees = CONVERT;     //in degrees
    float degrees_adjust = 0.0f;
    int proj_num;

    // Add check player weapon type statement
    /////////////////////////////////////////
    switch (instance->WeaponType_)
    {
    case PLAYER_BASIC:
        proj_vel = SHIP_BUL_VELOCITY;
        proj_type = Player_Bullet;
        proj_lifetime = BUL_LIFETIME;
        degrees *= 0.0;
        proj_num = 1;
        break;
    case PLAYER_SECOND:
        proj_vel = SHIP_BUL_VELOCITY;
        proj_type = Enemy_Bullet;
        proj_lifetime = BUL_LIFETIME;
        degrees *= 20.0f;
        degrees_adjust = -20.0f;
        proj_num = 3;
        break;
    case ENEMY_BASIC:
        proj_vel = ENEMY_BUL_VELOCITY;
        proj_type = Enemy_Bullet;
        proj_lifetime = BUL_LIFETIME; //Temporary value, replace with whatever is best
        degrees *= 0.0;
        proj_num = 1;
        break;
    default:
        return 0;
    }

    if (instance->reload_ != NO_RELOAD)
    {
        if (instance->reload_counter > instance->reload_)
        {
            for (i = 0; i < proj_num; ++i)
            {
                Vector2DSet(&weapvel, proj_vel * sinf(degrees), proj_vel * cosf(degrees));
                Vector2DSet(&weappos, instance->position_.x, instance->position_.y);

                Create_Instance(list, proj_type);

                InitScale(1.0f, 2.0f);
                InitPosition(&weappos);
                InitVelocity(&weapvel);
                InitRotation(-degrees);
                InitLifetime(proj_lifetime);

                //FMOD_System_PlaySound(sys, sounds[0], 0, FALSE, &channel[0]);

                instance->reload_counter = 0;
                degrees += degrees_adjust * CONVERT;
            }
        }
        instance->reload_counter++;
    }
    return 1;
}