ColPack
|
00001 // An example for using GraphColoringInterface to generate the seed matrix for Hessian 00002 /* How to compile this driver manually: 00003 To compile the code, replace the Main.cpp file in Main directory with this file 00004 and run "make" in ColPack installation directory. Make will generate "ColPack.exe" executable 00005 Run "ColPack.exe" 00006 //*/ 00007 00008 #include "ColPackHeaders.h" 00009 00010 using namespace ColPack; 00011 using namespace std; 00012 00013 int main() 00014 { 00015 double*** dp3_Seed = new double**; 00016 int *ip1_SeedRowCount = new int; 00017 int *ip1_SeedColumnCount = new int; 00018 int i_RowCount, i_MaxNonZerosInRows; 00019 00020 //populate the Hessian. Uncomment one of the 2 matrices below 00021 00022 /* 1x1 matrix 00023 i_RowCount = 1; 00024 i_MaxNonZerosInRows = 1; 00025 unsigned int **uip2_HessianSparsityPattern = new unsigned int *[i_RowCount];//[1][1] 00026 for(int i=0;i<1;i++) uip2_HessianSparsityPattern[i] = new unsigned int[i_MaxNonZerosInRows + 1]; 00027 uip2_HessianSparsityPattern[0][0] = 1; uip2_HessianSparsityPattern[0][1] = 0; 00028 //*/ 00029 00030 //* 5x5 matrix 00031 i_RowCount = 5; 00032 i_MaxNonZerosInRows = 2; 00033 unsigned int **uip2_HessianSparsityPattern = new unsigned int *[i_RowCount];//[5][5] 00034 for(int i=0;i<5;i++) uip2_HessianSparsityPattern[i] = new unsigned int[i_MaxNonZerosInRows + 1]; 00035 uip2_HessianSparsityPattern[0][0] = 1; uip2_HessianSparsityPattern[0][1] = 1; 00036 uip2_HessianSparsityPattern[1][0] = 2; uip2_HessianSparsityPattern[1][1] = 0; uip2_HessianSparsityPattern[1][2] = 2; 00037 uip2_HessianSparsityPattern[2][0] = 2; uip2_HessianSparsityPattern[2][1] = 1; uip2_HessianSparsityPattern[2][2] = 3; 00038 uip2_HessianSparsityPattern[3][0] = 2; uip2_HessianSparsityPattern[3][1] = 2; uip2_HessianSparsityPattern[3][2] = 4; 00039 uip2_HessianSparsityPattern[4][0] = 1; uip2_HessianSparsityPattern[4][1] = 3; 00040 //*/ 00041 00042 00043 //Step 1: Read the sparsity pattern of the given Hessian matrix (compressed sparse rows format) 00044 //and create the corresponding graph 00045 GraphColoringInterface * g = new GraphColoringInterface(SRC_MEM_ADOLC, uip2_HessianSparsityPattern, i_RowCount); 00046 00047 //Step 2: Color the bipartite graph with the specified ordering 00048 g->Coloring("SMALLEST_LAST", "STAR"); 00049 00050 //Step 3: From the coloring information, create and return the seed matrix 00051 (*dp3_Seed) = g->GetSeedMatrix(ip1_SeedRowCount, ip1_SeedColumnCount); 00052 /* Notes: 00053 In stead of doing step 1-3, you can just call the bellow function: 00054 g->GenerateSeedHessian(uip2_HessianSparsityPattern, i_RowCount, dp3_Seed, ip1_SeedRowCount, ip1_SeedColumnCount, "STAR", "SMALLEST_LAST"); // this function is inside GraphColoringInterface class 00055 */ 00056 cout<<"Finish GenerateSeed()"<<endl; 00057 00058 //this SECTION is just for displaying the result 00059 g->PrintGraphStructure(); 00060 g->PrintVertexColors(); 00061 g->PrintVertexColoringMetrics(); 00062 double **Seed = *dp3_Seed; 00063 int rows = g->GetVertexCount(); 00064 int cols = g->GetVertexColorCount(); 00065 cout<<"Seed matrix: ("<<rows<<","<<cols<<")"<<endl; 00066 for(int i=0; i<rows; i++) { 00067 for(int j=0; j<cols; j++) { 00068 cout<<setw(6)<<Seed[i][j]; 00069 } 00070 cout<<endl; 00071 } 00072 //END SECTION 00073 00074 Pause(); 00075 00076 //GraphColoringInterface * g = new GraphColoringInterface(); 00077 delete g; 00078 g = NULL; 00079 00080 //double*** dp3_Seed = new double**; 00081 delete dp3_Seed; 00082 dp3_Seed = NULL; 00083 Seed = NULL; 00084 00085 //int *ip1_SeedRowCount = new int; 00086 delete ip1_SeedRowCount; 00087 ip1_SeedRowCount = NULL; 00088 00089 //int *ip1_SeedColumnCount = new int; 00090 delete ip1_SeedColumnCount; 00091 ip1_SeedColumnCount = NULL; 00092 00093 //unsigned int **uip2_HessianSparsityPattern = new unsigned int *[i_RowCount];//[5][5] 00094 free_2DMatrix(uip2_HessianSparsityPattern, i_RowCount); 00095 uip2_HessianSparsityPattern = NULL; 00096 00097 return 0; 00098 }