ColPack
SampleDrivers/Basic/Generate_seed_matrix_for_Jacobian.cpp
Go to the documentation of this file.
00001 // An example for using BipartiteGraphPartialColoringInterface to generate the seed matrix for Jacobian
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_ColumnCount, i_MaxNonZerosInRows;
00019 
00020         //populate the Jacobian. Uncomment one of the 2 matrices below
00021         /* 1x1 matrix
00022         i_RowCount = 1;
00023         i_ColumnCount = 1;
00024         i_MaxNonZerosInRows = 1;
00025         unsigned int **uip2_JacobianSparsityPattern = new unsigned int *[i_RowCount];//[1][1]
00026         for(int i=0;i<i_RowCount;i++) uip2_JacobianSparsityPattern[i] = new unsigned int[i_MaxNonZerosInRows + 1];
00027         uip2_JacobianSparsityPattern[0][0] = 1;         uip2_JacobianSparsityPattern[0][1] = 0;
00028         //*/
00029 
00030         //* 32x9 matrix
00031         i_RowCount = 32;
00032         i_ColumnCount = 9;
00033         i_MaxNonZerosInRows = 3;
00034         unsigned int **uip2_JacobianSparsityPattern = new unsigned int *[i_RowCount];//[32][9]
00035         for(int i=0;i<i_RowCount;i++) uip2_JacobianSparsityPattern[i] = new unsigned int[i_MaxNonZerosInRows + 1];
00036         uip2_JacobianSparsityPattern[0][0] = 0;
00037         uip2_JacobianSparsityPattern[1][0] = 1;         uip2_JacobianSparsityPattern[1][1] = 0;
00038         uip2_JacobianSparsityPattern[2][0] = 1;         uip2_JacobianSparsityPattern[2][1] = 1;
00039         uip2_JacobianSparsityPattern[3][0] = 1;         uip2_JacobianSparsityPattern[3][1] = 2;
00040         uip2_JacobianSparsityPattern[4][0] = 1;         uip2_JacobianSparsityPattern[4][1] = 0;
00041         uip2_JacobianSparsityPattern[5][0] = 3;         uip2_JacobianSparsityPattern[5][1] = 0;         uip2_JacobianSparsityPattern[5][2] = 1;         uip2_JacobianSparsityPattern[5][3] = 3;
00042         uip2_JacobianSparsityPattern[6][0] = 3;         uip2_JacobianSparsityPattern[6][1] = 1;         uip2_JacobianSparsityPattern[6][2] = 2;         uip2_JacobianSparsityPattern[6][3] = 4;
00043         uip2_JacobianSparsityPattern[7][0] = 2;         uip2_JacobianSparsityPattern[7][1] = 2;         uip2_JacobianSparsityPattern[7][2] = 5;
00044         uip2_JacobianSparsityPattern[8][0] = 1;         uip2_JacobianSparsityPattern[8][1] = 3;
00045         uip2_JacobianSparsityPattern[9][0] = 3;         uip2_JacobianSparsityPattern[9][1] = 3;         uip2_JacobianSparsityPattern[9][2] = 4;         uip2_JacobianSparsityPattern[9][3] = 6;
00046         uip2_JacobianSparsityPattern[10][0] = 3;        uip2_JacobianSparsityPattern[10][1] = 4;                uip2_JacobianSparsityPattern[10][2] = 5;                uip2_JacobianSparsityPattern[10][3] = 7;
00047         uip2_JacobianSparsityPattern[11][0] = 2;        uip2_JacobianSparsityPattern[11][1] = 5;                uip2_JacobianSparsityPattern[11][2] = 8;
00048         uip2_JacobianSparsityPattern[12][0] = 1;        uip2_JacobianSparsityPattern[12][1] = 6;
00049         uip2_JacobianSparsityPattern[13][0] = 2;        uip2_JacobianSparsityPattern[13][1] = 6;                uip2_JacobianSparsityPattern[13][2] = 7;
00050         uip2_JacobianSparsityPattern[14][0] = 2;        uip2_JacobianSparsityPattern[14][1] = 7;                uip2_JacobianSparsityPattern[14][2] = 8;
00051         uip2_JacobianSparsityPattern[15][0] = 1;        uip2_JacobianSparsityPattern[15][1] = 8;
00052         uip2_JacobianSparsityPattern[16][0] = 1;        uip2_JacobianSparsityPattern[16][1] = 0;
00053         uip2_JacobianSparsityPattern[17][0] = 2;        uip2_JacobianSparsityPattern[17][1] = 0;                uip2_JacobianSparsityPattern[17][2] = 1;
00054         uip2_JacobianSparsityPattern[18][0] = 2;        uip2_JacobianSparsityPattern[18][1] = 1;                uip2_JacobianSparsityPattern[18][2] = 2;
00055         uip2_JacobianSparsityPattern[19][0] = 1;        uip2_JacobianSparsityPattern[19][1] = 2;
00056         uip2_JacobianSparsityPattern[20][0] = 2;        uip2_JacobianSparsityPattern[20][1] = 0;                uip2_JacobianSparsityPattern[20][2] = 3;
00057         uip2_JacobianSparsityPattern[21][0] = 3;        uip2_JacobianSparsityPattern[21][1] = 1;                uip2_JacobianSparsityPattern[21][2] = 3;                uip2_JacobianSparsityPattern[21][3] = 4;
00058         uip2_JacobianSparsityPattern[22][0] = 3;        uip2_JacobianSparsityPattern[22][1] = 2;                uip2_JacobianSparsityPattern[22][2] = 4;                uip2_JacobianSparsityPattern[22][3] = 5;
00059         uip2_JacobianSparsityPattern[23][0] = 1;        uip2_JacobianSparsityPattern[23][1] = 5;
00060         uip2_JacobianSparsityPattern[24][0] = 2;        uip2_JacobianSparsityPattern[24][1] = 3;                uip2_JacobianSparsityPattern[24][2] = 6;
00061         uip2_JacobianSparsityPattern[25][0] = 3;        uip2_JacobianSparsityPattern[25][1] = 4;                uip2_JacobianSparsityPattern[25][2] = 6;                uip2_JacobianSparsityPattern[25][3] = 7;
00062         uip2_JacobianSparsityPattern[26][0] = 3;        uip2_JacobianSparsityPattern[26][1] = 5;                uip2_JacobianSparsityPattern[26][2] = 7;                uip2_JacobianSparsityPattern[26][3] = 8;
00063         uip2_JacobianSparsityPattern[27][0] = 1;        uip2_JacobianSparsityPattern[27][1] = 8;
00064         uip2_JacobianSparsityPattern[28][0] = 1;        uip2_JacobianSparsityPattern[28][1] = 6;
00065         uip2_JacobianSparsityPattern[29][0] = 1;        uip2_JacobianSparsityPattern[29][1] = 7;
00066         uip2_JacobianSparsityPattern[30][0] = 1;        uip2_JacobianSparsityPattern[30][1] = 8;
00067         uip2_JacobianSparsityPattern[31][0] = 0;
00068         //*/
00069 
00070         //Step 1: Read the sparsity pattern of the given Jacobian matrix (compressed sparse rows format)
00071         //and create the corresponding bipartite graph
00072         BipartiteGraphPartialColoringInterface * g = new BipartiteGraphPartialColoringInterface(SRC_MEM_ADOLC, uip2_JacobianSparsityPattern, i_RowCount, i_ColumnCount);
00073 
00074         //Step 2: Do Partial-Distance-Two-Coloring the bipartite graph with the specified ordering
00075         g->PartialDistanceTwoColoring( "SMALLEST_LAST", "COLUMN_PARTIAL_DISTANCE_TWO");
00076 
00077         //Step 3: From the coloring information, create and return the seed matrix
00078         (*dp3_Seed) = g->GetSeedMatrix(ip1_SeedRowCount, ip1_SeedColumnCount);
00079         /* Notes:
00080         In stead of doing step 1-3, you can just call the bellow function:
00081                 g->GenerateSeedJacobian(uip2_JacobianSparsityPattern, i_RowCount,i_ColumnCount, dp3_Seed, ip1_SeedRowCount, ip1_SeedColumnCount, "COLUMN_PARTIAL_DISTANCE_TWO", "SMALLEST_LAST"); // compress columns. This function is inside BipartiteGraphPartialColoringInterface class
00082         */
00083         cout<<"Finish GenerateSeed()"<<endl;
00084 
00085         //this SECTION is just for displaying the result
00086         g->PrintBipartiteGraph();
00087         g->PrintColumnPartialColors();
00088         g->PrintColumnPartialColoringMetrics();
00089         double **RSeed = *dp3_Seed;
00090         int rows = g->GetColumnVertexCount();
00091         int cols = g->GetRightVertexColorCount();
00092         cout<<"Right Seed matrix: ("<<rows<<","<<cols<<")"<<endl;
00093         for(int i=0; i<rows; i++) {
00094                 for(int j=0; j<cols; j++) {
00095                         cout<<setw(6)<<RSeed[i][j];
00096                 }
00097                 cout<<endl;
00098         }
00099         //END SECTION
00100 
00101 
00102         Pause();
00103 
00104         //GraphColoringInterface * g = new GraphColoringInterface();
00105         delete g;
00106         g = NULL;
00107 
00108         //double*** dp3_Seed = new double**;
00109         delete dp3_Seed;
00110         dp3_Seed = NULL;
00111         RSeed = NULL;
00112 
00113         //int *ip1_SeedRowCount = new int;
00114         delete ip1_SeedRowCount;
00115         ip1_SeedRowCount = NULL;
00116 
00117         //int *ip1_SeedColumnCount = new int;
00118         delete ip1_SeedColumnCount;
00119         ip1_SeedColumnCount = NULL;
00120 
00121         //unsigned int **uip2_HessianSparsityPattern = new unsigned int *[i_RowCount];//[5][5]
00122         free_2DMatrix(uip2_JacobianSparsityPattern, i_RowCount);
00123         uip2_JacobianSparsityPattern = NULL;
00124 
00125         return 0;
00126 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines