Exemplo n.º 1
0
	int GradientBoostingForest::LearnNewInstance()
	{
		if(!m_pconfig->IsLearnNewInstances)	
		{
			Comm::LogErr("GradientBoostingForest::LearnNewInstance IsLearnNewInstances is 0");
			return -1;
		}
		//	std::vector< std::vector<int> > vecTmpInstances(m_pInstancePool->Size());
		int LeafCnt = 0;
		for(int i=0;i<m_Forest.size();i++)
		{
			DecisionTree * pTree = m_Forest[i];
			for(int j=0;j<pTree->Size();j++)
			{
			//	printf("SizeSize %d\n",pTree->Size());
				DecisionTreeNode & node = pTree->GetNode(j);
				if(LEAF == node.m_status)
				{
					/*
					if(node.m_InstancesHashCode == 1150390567)
					{
						printf("f**k debug");
						node.print();
					}
					*/
					if(NULL == node.m_ppInstances)
					{
						Comm::LogErr("GradientBoostingForest::LearnNewInstance fail m_ppInstances is NULL DebugStr = %s",node.DebugStr().c_str());
						return -1;
					}
					node.m_LeafIndex = LeafCnt;
					LeafCnt++;
					node.m_ppInstances = Comm::Free(node.m_ppInstances);
				}
			}
		}
		
		m_TotLeafCnt = LeafCnt;
			
		if(m_pconfig->OutputNewInstancesFilePath == "null")
		{
			Comm::LogInfo("GradientBoostingForest::LearnNewInstance OutputNewInstancesFilePath is null");
			return 0;
		}
		std::ostringstream oss;
		oss.clear();
		oss.str("");
		FILE * fp = fopen(m_pconfig->OutputNewInstancesFilePath.c_str(),"w");
		if(NULL == fp)
		{
			Comm::LogErr("GradientBoostingForest::LearnNewInstance fail! open %s fail!",m_pconfig->OutputNewInstancesFilePath.c_str());
			return -1;
		}

		//IsPushBackOgX
		if(m_pconfig->IsPushBackOgX)
			oss<<"n_feature,"<<LeafCnt + m_pconfig->FeatureNum<<"\n";
		else 
			oss<<"n_feature,"<<LeafCnt<<"\n";
		
		int ret = fputs(oss.str().c_str(),fp);
		if(ret < 0)
		{
			Comm::LogErr("GradientBoostingForest::LearnNewInstance fail!fputs %s fail!",oss.str().c_str());
			fclose(fp);
			return -1;
		}

		for(int i=0;i<m_pInstancePool->Size();i++)
		{
			Instance newInstance;
			ret = GetNewInstance(m_pInstancePool->GetInstance(i), newInstance);
			if(ret != 0)
			{
				Comm::LogErr("GradientBoostingForest::LearnNewInstance GetNewInstance i = %d fail!",i);
				fclose(fp);
				return -1;
			}
			std::string str = newInstance.ToString() + "\n";
			ret = fputs(str.c_str(),fp);
			if(ret < 0)
			{
				Comm::LogErr("GradientBoostingForest::LearnNewInstance fputs %s fail!",str.c_str());
				fclose(fp);
				return -1;
			}
		}

		fclose(fp);
		return 0;
	}