1 /* -------------------------------------------------------------
2 
3 This file is a component of SDPA
4 Copyright (C) 2004-2012 SDPA Project
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 
20 ------------------------------------------------------------- */
21 // Here is the start of ``example6.cpp''
22 
23 #include <cstdio>
24 #include <cstdlib>
25 #include <sdpa_call.h>
26 
27 void printVector(double* ele, int dim, char* printFormat,
28                  FILE* fpout);
29 void printMatrix(double* ele, int dim, char* printFormat,
30                  FILE* fpout);
31 void printDimacsError(double dimacs_error[7],char* printFormat,
32                       FILE* fpout);
33 void printResult(SDPA& Problem1);
34 
main(int argc,char ** argv)35 int main(int argc, char** argv)
36 {
37   if (argc != 4) {
38     fprintf(stderr, "%s [Input] [Init] [Param] \n", argv[0]);
39     exit(EXIT_FAILURE);
40   }
41 
42   SDPA Problem1;
43   Problem1.setDisplay(stdout);
44 
45   Problem1.readParameter(argv[3],stdout);
46   // Note that readParameter should be called before readInput
47   // Otherwise initial point cannot be decided by lambdaStar
48   Problem1.readInput(argv[1],stdout,SDPA::AUTO);
49   Problem1.setInitPoint(true);
50   Problem1.readInit(argv[2],stdout,SDPA::AUTO);
51 
52   Problem1.initializeSolve();
53   Problem1.solve();
54   printResult(Problem1);
55   Problem1.terminate();
56   exit(0);
57 }
58 
printVector(double * ele,int dim,char * printFormat,FILE * fpout)59 void printVector(double* ele, int dim, char* printFormat, FILE* fpout)
60 {
61   fprintf(fpout,"[ ");
62   for (int k=0; k<dim-1; ++k) {
63     fprintf(fpout,printFormat,ele[k]);
64     fprintf(fpout," ");
65   }
66   fprintf(fpout,printFormat,ele[dim-1]);
67   fprintf(fpout,"]; \n");
68 }
69 
printMatrix(double * ele,int dim,char * printFormat,FILE * fpout)70 void printMatrix(double* ele, int dim, char* printFormat, FILE* fpout)
71 {
72   fprintf(fpout,"[\n");
73   for (int i=0; i<dim; ++i) {
74     fprintf(fpout,"[ ");
75     for (int j=0; j<dim-1; ++j) {
76       fprintf(fpout,printFormat,ele[i+dim*j]);
77       fprintf(fpout," ");
78     }
79     fprintf(fpout,printFormat,ele[i+dim*(dim-1)]);
80     fprintf(fpout,"]; \n");
81   }
82   fprintf(fpout,"]; \n");
83 }
84 
printDimacsError(double dimacs_error[7],char * printFormat,FILE * fpout)85 void printDimacsError(double dimacs_error[7],char* printFormat,
86                       FILE* fpout)
87 {
88   fprintf(fpout,  "\n");
89   fprintf(fpout,  "* DIMACS_ERRORS * \n");
90   fprintf(fpout,  "err1 = ");
91   fprintf(fpout,  printFormat, dimacs_error[1]);
92   fprintf(fpout, "  [||Ax-b|| / (1+||b||_1)]\n");
93   fprintf(fpout,  "err2 = ");
94   fprintf(fpout,  printFormat, dimacs_error[2]);
95   fprintf(fpout, "  [max(0, -lambda(x)/(1+||b||_1))]\n");
96   fprintf(fpout,  "err3 = ");
97   fprintf(fpout,  printFormat, dimacs_error[3]);
98   fprintf(fpout, "  [||A^Ty + z - c || / (1+||c||_1)]\n");
99   fprintf(fpout,  "err4 = ");
100   fprintf(fpout,  printFormat, dimacs_error[4]);
101   fprintf(fpout, "  [max(0, -lambda(z)/(1+||c||_1))]\n");
102   fprintf(fpout,  "err5 = ");
103   fprintf(fpout,  printFormat, dimacs_error[5]);
104   fprintf(fpout, "  [(<c,x> - <b,y>) / (1 + |<c,x>| + |<b,y>|)]\n");
105   fprintf(fpout,  "err6 = ");
106   fprintf(fpout,  printFormat, dimacs_error[6]);
107   fprintf(fpout, "  [<x,z> / (1 + |<c,x>| + |<b,y>|)]\n");
108   fprintf(fpout,  "\n");
109 }
110 
printResult(SDPA & Problem1)111 void printResult(SDPA& Problem1)
112 {
113   fprintf(stdout, "\nStop iteration = %d\n",
114 	  Problem1.getIteration());
115   char phase_string[30];
116   Problem1.getPhaseString(phase_string);
117   fprintf(stdout, "Phase          = %s\n", phase_string);
118   fprintf(stdout, "objValPrimal   = %+10.6e\n",
119 	  Problem1.getPrimalObj());
120   fprintf(stdout, "objValDual     = %+10.6e\n",
121 	  Problem1.getDualObj());
122   fprintf(stdout, "p. feas. error = %+10.6e\n",
123 	  Problem1.getPrimalError());
124   fprintf(stdout, "d. feas. error = %+10.6e\n\n",
125 	  Problem1.getDualError());
126 
127   #if 0
128 
129   fprintf(stdout, "xVec = \n");
130   // Problem1.printResultXVec();
131   printVector(Problem1.getResultXVec(),
132 	      Problem1.getConstraintNumber(), (char*)"%+8.3e",
133 	      stdout);
134 
135   fprintf(stdout, "xMat = \n");
136   // Problem1.printResultXMat();
137   for (int l=0; l<Problem1.getBlockNumber(); ++l) {
138     if (Problem1.getBlockType(l+1) == SDPA::SDP) {
139       printMatrix(Problem1.getResultXMat(l+1),
140 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
141 		  stdout);
142     }
143     else if (Problem1.getBlockType(l+1) == SDPA::SOCP) {
144       printf("current version does not support SOCP\n");
145     }
146     if (Problem1.getBlockType(l+1) == SDPA::LP) {
147       printVector(Problem1.getResultXMat(l+1),
148 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
149 		  stdout);
150     }
151   }
152 
153   fprintf(stdout, "yMat = \n");
154   // Problem1.printResultYMat();
155   for (int l=0; l<Problem1.getBlockNumber(); ++l) {
156     if (Problem1.getBlockType(l+1) == SDPA::SDP) {
157       printMatrix(Problem1.getResultYMat(l+1),
158 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
159 		  stdout);
160     }
161     else if (Problem1.getBlockType(l+1) == SDPA::SOCP) {
162       printf("current version does not support SOCP\n");
163     }
164     if (Problem1.getBlockType(l+1) == SDPA::LP) {
165       printVector(Problem1.getResultYMat(l+1),
166 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
167 		  stdout);
168     }
169   }
170   #endif
171 
172   double dimacs_error[7];
173   Problem1.getDimacsError(dimacs_error);
174   printDimacsError(dimacs_error,(char*)"%+8.3e",stdout);
175 
176 }
177