int main(int argc, char **argv){ MPI_Init(&argc, &argv); int procid, nprocs; MPI_Comm_rank(MPI_COMM_WORLD, &procid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); structMesh *mesh = ParallelMeshCreate(); physics *phys = PhysicsCreate(); double **c = ConcentrationCreate(mesh, phys); double finalTime = 2.5; PrintTotalError(mesh, phys, c, 0, procid); ConvectionSolve2d(mesh, phys, c, finalTime); PrintTotalError(mesh, phys, c, finalTime, procid); char filename[200]; snprintf(filename, 200, "result-%d-%d.txt", procid, nprocs); // printf("procid=%d, filename=%s\n", procid, filename); SaveMatrix(filename, c, mesh->ndim1, mesh->ndim2); snprintf(filename, 200, "y-%d-%d.txt", procid, nprocs); SaveMatrix(filename, mesh->y, mesh->ndim1, mesh->ndim2); snprintf(filename, 200, "x-%d-%d.txt", procid, nprocs); SaveMatrix(filename, mesh->x, mesh->ndim1, mesh->ndim2); ConcentrationFree(c); ParallelMeshFree(mesh); PhysicsFree(phys); MPI_Finalize(); return 0; }
void Camera::Mirror( const Plane & plane) { SaveMatrix(); // SetProjTransform( nearPlane, farPlane + 10.0f, fov); Matrix matrix = WorldMatrix(); matrix.Mirror( plane); SetWorldAll( matrix); }
void CHierarchicalDP::SaveResult( const char *dir ) { char dirname[256]; char filename[256]; strcpy( dirname , dir ); int len = (int)strlen( dir ); if( len==0 || dir[len-1] != '\\' || dir[len-1] != '/' ) strcat( dirname , "\\" ); CreateDirectory( dirname , NULL ); std::vector< std::vector<double> > Pz = GetPz_dk(); sprintf( filename , "%sPz.txt" , dirname ); SaveMatrix( Pz , (int)Pz[0].size() , (int)Pz.size() , filename ); std::vector<int> classRes = GetClassificationResult_d(); sprintf( filename , "%sClusteringResult.txt" , dirname ); SaveArray( classRes , (int)classRes.size() , filename ); }
/** * Construct an ELM * @param * elm_type - 0 for regression; 1 for (both binary and multi-classes) classification */ void ELMTrain(int elm_type) { double starttime,endtime; double TrainingAccuracy; int i,j,k = 0; float **input,**weight,*biase,**tranpI,**tempH,**H; float **PIMatrix; float **train_set; float **T,**Y; float **out; train_set = (float **)calloc(DATASET,sizeof(float *)); tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *)); input = (float **)calloc(DATASET,sizeof(float *)); /*datasize * INPUT_NEURONS*/ weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * INPUT_NEURONS*/ biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); /*HIDDEN_NEURONS*/ tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * datasize*/ PIMatrix = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); H = (float **)calloc(DATASET,sizeof(float *)); T = (float **)calloc(DATASET,sizeof(float *)); Y = (float **)calloc(DATASET,sizeof(float *)); out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); for(i=0;i<DATASET;i++){ train_set[i] = (float *)calloc(NUMROWS,sizeof(float)); input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); } for(i=0;i<DATASET;i++) { T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); } for(i=0;i<INPUT_NEURONS;i++) tranpI[i] = (float *)calloc(DATASET,sizeof(float)); for(i=0;i<HIDDEN_NEURONS;i++) { weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); tempH[i] = (float *)calloc(DATASET,sizeof(float)); out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); PIMatrix[i] = (float *)calloc(DATASET,sizeof(float)); } printf("begin to random weight and biase...\n"); /*得到随机的偏置和权重*/ RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS); RandomBiase(biase,HIDDEN_NEURONS); SaveMatrix(weight,"./result/weight",HIDDEN_NEURONS,INPUT_NEURONS); SaveMatrix_s(biase,"./result/biase",1,HIDDEN_NEURONS); /*加载数据集到内存*/ printf("begin to load input from the file...\n"); if(LoadMatrix(train_set,"./sample/frieman",DATASET,NUMROWS) == 0){ printf("load input file error!!!\n"); return; } /*将数据集划分成输入和输出*/ if(elm_type == 0){ //regression /* the first column is the output of the dataset */ for(i = 0;i < DATASET ;i++) { T[k++][0] = train_set[i][0]; for(j = 1;j <= INPUT_NEURONS;j++) { input[i][j-1] = train_set[i][j]; } } }else{ //classification /* the last column is the lable of class of the dataset */ InitMatrix(T,DATASET,OUTPUT_NEURONS,-1); for(i = 0;i < DATASET ;i++) { //get the last column T[k++][0] = train_set[i][0] - 1;//class label starts from 0,so minus one for(j = 1;j <= INPUT_NEURONS;j++) { input[i][j-1] = train_set[i][j]; } } for(i = 0;i < DATASET ;i++) { for(j = 0;j < OUTPUT_NEURONS;j++) { k = T[i][0]; if(k < OUTPUT_NEURONS && k >= 0){ T[i][k] = 1; } if(k != 0){ T[i][0] = -1; } } } } /*ELM*/ printf("begin to compute...\n"); starttime = omp_get_wtime(); TranspositionMatrix(input,tranpI,DATASET,INPUT_NEURONS); printf("begin to compute step 1...\n"); MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,DATASET,tempH); printf("begin to compute setp 2...\n"); AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,DATASET); printf("begin to compute step 3...\n"); SigmoidHandle(tempH,HIDDEN_NEURONS,DATASET); printf("begin to compute step 4...\n"); TranspositionMatrix(tempH,H,HIDDEN_NEURONS,DATASET); PseudoInverseMatrix(H,DATASET,HIDDEN_NEURONS,PIMatrix); MultiplyMatrix(PIMatrix,HIDDEN_NEURONS,DATASET,T,DATASET,OUTPUT_NEURONS,out); //SaveMatrix(H,"./result/H",DATASET,HIDDEN_NEURONS); //SaveMatrix(PIMatrix,"./result/PIMatrix",HIDDEN_NEURONS,DATASET); printf("begin to compute step 5...\n"); endtime = omp_get_wtime(); //保存输出权值 SaveMatrix(out,"./result/result",HIDDEN_NEURONS,OUTPUT_NEURONS); MultiplyMatrix(H,DATASET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y); printf("use time :%f\n",endtime - starttime); printf("train complete...\n"); if(elm_type == 0){ //检测准确率 double MSE = 0; for(i = 0;i< DATASET;i++) { MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]); } TrainingAccuracy = sqrt(MSE/DATASET); printf("Regression/trainning accuracy :%f\n",TrainingAccuracy); }else{ float MissClassificationRate_Training=0; double maxtag1,maxtag2; int tag1 = 0,tag2 = 0; for (i = 0; i < DATASET; i++) { maxtag1 = Y[i][0]; tag1 = 0; maxtag2 = T[i][0]; tag2 = 0; for (j = 1; j < OUTPUT_NEURONS; j++) { if(Y[i][j] > maxtag1){ maxtag1 = Y[i][j]; tag1 = j; } if(T[i][j] > maxtag2){ maxtag2 = T[i][j]; tag2 = j; } } if(tag1 != tag2) MissClassificationRate_Training ++; } TrainingAccuracy = 1 - MissClassificationRate_Training*1.0f/DATASET; printf("Classification/training accuracy :%f\n",TrainingAccuracy); } FreeMatrix(train_set,DATASET); FreeMatrix(tranpI,INPUT_NEURONS); FreeMatrix(input,DATASET); FreeMatrix(weight,HIDDEN_NEURONS); free(biase); FreeMatrix(tempH,HIDDEN_NEURONS); FreeMatrix(PIMatrix,HIDDEN_NEURONS); FreeMatrix(H,DATASET); FreeMatrix(T,DATASET); FreeMatrix(Y,DATASET); FreeMatrix(out,HIDDEN_NEURONS); }
void SGMExporter::ExportCam(IGameNode *node, BinaryWriter *bw) { IGameObject *gameObject = node ->GetIGameObject(); assert(gameObject != NULL); if (gameObject ->GetIGameType() == IGameObject::IGAME_CAMERA) { camsCount++; IGameCamera *gameCam = (IGameCamera*)gameObject; bw->Write((int)node->GetNodeID()); bw->Write(StringUtils::ToNarrow(node->GetName())); GMatrix viewMatrix = gameCam->GetIGameObjectTM().Inverse(); SaveMatrix(bw, viewMatrix); IGameProperty *fov = gameCam->GetCameraFOV(); IGameProperty *trgDist = gameCam->GetCameraTargetDist(); IGameProperty *nearClip = gameCam->GetCameraNearClip(); IGameProperty *farClip = gameCam->GetCameraFarClip(); // FOV if (fov != NULL && fov->IsPropAnimated()) { IGameControl *gameCtrl = fov->GetIGameControl(); IGameKeyTab keys; if (gameCtrl->GetTCBKeys(keys, IGAME_FLOAT)) { bw->Write((bool)true); bw->Write(keys.Count()); for (int i = 0; i < keys.Count(); i++) { bw->Write(TicksToSec(keys[i].t)); bw->Write(keys[i].tcbKey.fval); } } else { Log::LogT("warning: node '%s' doesn't have TCB controller for FOV, animation won't be exported", StringUtils::ToNarrow(node->GetName()).c_str()); bw->Write((bool)false); float fovVal; fov->GetPropertyValue(fovVal); bw->Write(fovVal); } } else { bw->Write((bool)false); float fovVal; fov->GetPropertyValue(fovVal); bw->Write(fovVal); } ///////DISTANCE if (trgDist != NULL && trgDist->IsPropAnimated()) { IGameControl *gameCtrl = trgDist->GetIGameControl(); IGameKeyTab keys; if (gameCtrl->GetTCBKeys(keys, IGAME_FLOAT)) { bw->Write((bool)true); bw->Write(keys.Count()); for (int i = 0; i < keys.Count(); i++) { bw->Write(TicksToSec(keys[i].t)); bw->Write(keys[i].tcbKey.fval); } } else { Log::LogT("warning: node '%s' doesn't have TCB controller for target distance, animation won't be exported", StringUtils::ToNarrow(node->GetName()).c_str()); bw->Write((bool)false); float val; trgDist->GetPropertyValue(val); bw->Write(val); } } else { bw->Write((bool)false); float fovVal; //trgDist->GetPropertyValue(fovVal); bw->Write(10.0f); } float nearClipValue; if (nearClip != NULL) nearClip->GetPropertyValue(nearClipValue); else nearClipValue = 0.1f; float farClipValue; if (farClip != NULL) farClip->GetPropertyValue(farClipValue); else farClipValue = 0.1f; bw->Write(nearClipValue); bw->Write(farClipValue); } node ->ReleaseIGameObject(); for (int i = 0; i < node ->GetChildCount(); i++) ExportCam(node ->GetNodeChild(i), bw); }
//回归 void ELMTrain() { double starttime,endtime; int i,j,k = 0; double MSE = 0,TrainingAccuracy; float **input,**weight,*biase,**tranpI,**tempH,**H; float **PIMatrix; float **train_set; float **T,**Y; float **out; train_set = (float **)calloc(DATASET,sizeof(float *)); tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *)); input = (float **)calloc(DATASET,sizeof(float *)); /*datasize * INPUT_NEURONS*/ weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * INPUT_NEURONS*/ biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); /*HIDDEN_NEURONS*/ tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * datasize*/ PIMatrix = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); H = (float **)calloc(DATASET,sizeof(float *)); T = (float **)calloc(DATASET,sizeof(float *)); Y = (float **)calloc(DATASET,sizeof(float *)); out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); for(i=0;i<DATASET;i++){ train_set[i] = (float *)calloc(NUMROWS,sizeof(float)); input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); } for(i=0;i<DATASET;i++) { T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); } for(i=0;i<INPUT_NEURONS;i++) tranpI[i] = (float *)calloc(DATASET,sizeof(float)); for(i=0;i<HIDDEN_NEURONS;i++) { weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); tempH[i] = (float *)calloc(DATASET,sizeof(float)); out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); PIMatrix[i] = (float *)calloc(DATASET,sizeof(float)); } printf("begin to random weight and biase...\n"); /*得到随机的偏置和权重*/ RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS); RandomBiase(biase,HIDDEN_NEURONS); /*加载数据集到内存*/ printf("begin to load input from the file...\n"); if(LoadMatrix(train_set,"../TrainingDataSet/covtype",DATASET,NUMROWS,1) == 0){ printf("load input file error!!!\n"); return; } /*将数据集划分成输入和输出*/ for(i = 0;i < DATASET ;i++) { T[k++][0] = train_set[i][0]; for(j = 1;j <= INPUT_NEURONS;j++) { input[i][j-1] = train_set[i][j]; } } SaveMatrix(input,"./result/input",DATASET,INPUT_NEURONS); /*ELM*/ printf("begin to compute...\n"); starttime = omp_get_wtime(); TranspositionMatrix(input,tranpI,DATASET,INPUT_NEURONS); printf("begin to compute step 1...\n"); MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,DATASET,tempH); printf("begin to compute setp 2...\n"); AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,DATASET); printf("begin to compute step 3...\n"); SigmoidHandle(tempH,HIDDEN_NEURONS,DATASET); printf("begin to compute step 4...\n"); TranspositionMatrix(tempH,H,HIDDEN_NEURONS,DATASET); PseudoInverseMatrix(H,DATASET,HIDDEN_NEURONS,PIMatrix); MultiplyMatrix(PIMatrix,HIDDEN_NEURONS,DATASET,T,DATASET,OUTPUT_NEURONS,out); //SaveMatrix(H,"./result/H",DATASET,HIDDEN_NEURONS); //SaveMatrix(PIMatrix,"./result/PIMatrix",HIDDEN_NEURONS,DATASET); printf("begin to compute step 5...\n"); endtime = omp_get_wtime(); //保存输出权值 SaveMatrix(out,"./result/result",HIDDEN_NEURONS,OUTPUT_NEURONS); //检测准确率 MultiplyMatrix(H,DATASET,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y); for(i = 0;i< DATASET;i++) { MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]); } SaveMatrix(T,"./result/t",DATASET,OUTPUT_NEURONS); SaveMatrix(Y,"./result/y",DATASET,OUTPUT_NEURONS); TrainingAccuracy = sqrt(MSE/DATASET); printf("use time :%f\n",endtime - starttime); printf("trainning accuracy :%f\n",TrainingAccuracy); printf("train complete...\n"); //print(PIMatrix,DATASET,HIDDEN_NEURONS); }
int main(int argc,char **argv){ int i,j,k = 0; double MSE = 0,TrainingAccuracy; float **input,**weight,*biase,**tranpI,**tempH,**H; float **tranpw; float **train_set; float **T,**Y; float **out; train_set = (float **)calloc(TESTDATA,sizeof(float *)); tranpI = (float **)calloc(INPUT_NEURONS,sizeof(float *)); input = (float **)calloc(TESTDATA,sizeof(float *)); /*datasize * INPUT_NEURONS*/ weight = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * INPUT_NEURONS*/ biase = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); /*HIDDEN_NEURONS*/ tempH = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); /*HIDDEN_NEURONS * datasize*/ tranpw = (float **)calloc(INPUT_NEURONS,sizeof(float *)); H = (float **)calloc(TESTDATA,sizeof(float *)); T = (float **)calloc(TESTDATA,sizeof(float *)); Y = (float **)calloc(TESTDATA,sizeof(float *)); out = (float **)calloc(HIDDEN_NEURONS,sizeof(float *)); for(i=0;i<TESTDATA;i++){ train_set[i] = (float *)calloc(NUMROWS,sizeof(float)); input[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); H[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); } for(i=0;i<TESTDATA;i++) { T[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); Y[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); } for(i=0;i<INPUT_NEURONS;i++){ tranpI[i] = (float *)calloc(TESTDATA,sizeof(float)); tranpw[i] = (float *)calloc(HIDDEN_NEURONS,sizeof(float)); } for(i=0;i<HIDDEN_NEURONS;i++) { weight[i] = (float *)calloc(INPUT_NEURONS,sizeof(float)); tempH[i] = (float *)calloc(TESTDATA,sizeof(float)); out[i] = (float *)calloc(OUTPUT_NEURONS,sizeof(float)); } printf("begin to random weight and biase...\n"); /*得到随机的偏置和权重*/ //RandomWeight(weight,HIDDEN_NEURONS,INPUT_NEURONS); //RandomBiase(biase,HIDDEN_NEURONS); if(LoadMatrix(tranpw,"../result/weight",INPUT_NEURONS,HIDDEN_NEURONS,0) == 0){ printf("load input file error!!!\n"); return 0; } TranspositionMatrix(tranpw,weight,INPUT_NEURONS,HIDDEN_NEURONS); if(LoadMatrix_s(biase,"../result/bias",1,HIDDEN_NEURONS,0) == 0){ printf("load input file error!!!\n"); return 0; } /*加载数据集到内存*/ printf("begin to load input from the file...\n"); if(LoadMatrix(train_set,"../sample/0",TESTDATA,NUMROWS,1) == 0){ printf("load input file error!!!\n"); return 0; } /*将数据集划分成输入和输出*/ for(i = 0;i < TESTDATA ;i++) { T[k++][0] = train_set[i][0]; for(j = 1;j <= INPUT_NEURONS;j++) { input[i][j-1] = train_set[i][j]; } } /*ELM*/ printf("begin to compute...\n"); TranspositionMatrix(input,tranpI,TESTDATA,INPUT_NEURONS); printf("begin to compute step 1...\n"); MultiplyMatrix(weight,HIDDEN_NEURONS,INPUT_NEURONS, tranpI,INPUT_NEURONS,TESTDATA,tempH); printf("begin to compute setp 2...\n"); AddMatrix_bais(tempH,biase,HIDDEN_NEURONS,TESTDATA); printf("begin to compute step 3...\n"); SigmoidHandle(tempH,HIDDEN_NEURONS,TESTDATA); printf("begin to compute step 4...\n"); TranspositionMatrix(tempH,H,HIDDEN_NEURONS,TESTDATA); printf("begin to load input from the file...\n"); if(LoadMatrix(out,"../result/result",HIDDEN_NEURONS,OUTPUT_NEURONS,0) == 0){ printf("load input file error!!!\n"); return 0; } //检测准确率 MultiplyMatrix(H,TESTDATA,HIDDEN_NEURONS,out,HIDDEN_NEURONS,OUTPUT_NEURONS,Y); SaveMatrix(Y,"../result/Y",TESTDATA,OUTPUT_NEURONS); for(i = 0;i< TESTDATA;i++) { MSE += (Y[i][0] - T[i][0])*(Y[i][0] - T[i][0]); } TrainingAccuracy = sqrt(MSE/TESTDATA); printf("trainning accuracy :%f\n",TrainingAccuracy); printf("test complete...\n"); //print(PIMatrix,TESTDATA,HIDDEN_NEURONS); return 0; }