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 ``example3.cpp''
22 
23 #include <cstdio>
24 #include <cstdlib>
25 #include <sdpa_call.h>
26 
27 /*
28 example1.dat:
29 
30 "Example 1: mDim = 3, nBLOCK = 1, {2}"
31    3  =  mDIM
32    1  =  nBLOCK
33    2  = bLOCKsTRUCT
34 {48, -8, 20}
35 { {-11,  0}, { 0, 23} }
36 { { 10,  4}, { 4,  0} }
37 { {  0,  0}, { 0, -8} }
38 { {  0, -8}, {-8, -2} }
39 
40 example1.ini:
41 {0.0, -4.0, 0.0}
42 { {11.0, 0.0}, {0.0, 9.0} }
43 { {5.9,  -1.375}, {-1.375, 1.0} }
44 
45 */
46 
47 void printVector(double* ele, int dim, char* printFormat,
48 		 FILE* fpout);
49 void printMatrix(double* ele, int dim, char* printFormat,
50 		 FILE* fpout);
51 void printDimacsError(double dimacs_error[7],char* printFormat,
52 		      FILE* fpout);
53 
main()54 int main ()
55 {
56   SDPA::printSDPAVersion(stdout);
57   SDPA	Problem1;
58   Problem1.setDisplay(stdout);
59 
60   // All parameteres are renewed
61   Problem1.setParameterType(SDPA::PARAMETER_DEFAULT);
62 
63   // If necessary, each parameter can be set independently
64 
65   // Problem1.setParameterMaxIteration(100);
66   // Problem1.setParameterEpsilonStar(1.0e-7);
67   // Problem1.setParameterLambdaStar(1.0e+2);
68   // Problem1.setParameterOmegaStar(2.0);
69   // Problem1.setParameterLowerBound(-1.0e+5);
70   // Problem1.setParameterUppwerBound(1.0e+5);
71   // Problem1.setParameterBetaStar(0.1);
72   // Problem1.setParameterBetaBar(0.2);
73   // Problem1.setParameterGammaStar(0.9);
74   // Problem1.setParameterEpsilonDash(1.0e-7);
75   // Problem1.setParameterPrintXVec((char*)"%+8.3e" );
76   // Problem1.setParameterPrintXMat((char*)"%+8.3e" );
77   // Problem1.setParameterPrintYMat((char*)"%+8.3e" );
78   // Problem1.setParameterPrintInformation((char*)"%+10.16e");
79 
80   Problem1.printParameters(stdout);
81 
82   int mDIM   = 3;
83   int nBlock = 1;
84   Problem1.inputConstraintNumber(mDIM);
85   Problem1.inputBlockNumber(nBlock);
86   Problem1.inputBlockSize(1,2);
87   Problem1.inputBlockType(1,SDPA::SDP);
88 
89   Problem1.initializeUpperTriangleSpace();
90 
91   Problem1.inputCVec(1,48);
92   Problem1.inputCVec(2,-8);
93   Problem1.inputCVec(3,20);
94 
95   Problem1.inputElement(0, 1, 1, 1, -11);
96   Problem1.inputElement(0, 1, 2, 2,  23);
97 
98   Problem1.inputElement(1, 1, 1, 1,  10);
99   Problem1.inputElement(1, 1, 1, 2,   4);
100 
101   Problem1.inputElement(2, 1, 2, 2,  -8);
102 
103   Problem1.inputElement(3, 1, 1, 2,  -8);
104   Problem1.inputElement(3, 1, 2, 2,  -2);
105 
106   Problem1.initializeUpperTriangle();
107 
108   Problem1.setInitPoint(true);
109 
110   Problem1.inputInitXVec(1, 0.0);
111   Problem1.inputInitXVec(2,-4.0);
112   Problem1.inputInitXVec(3, 0.0);
113 
114   Problem1.inputInitXMat(1,1,1, 11.0);
115   Problem1.inputInitXMat(1,2,2,  9.0);
116 
117   Problem1.inputInitYMat(1,1,1,  5.9);
118   Problem1.inputInitYMat(1,1,2, -1.375);
119   Problem1.inputInitYMat(1,2,2,  1.0);
120 
121   Problem1.initializeSolve();
122 
123   // if necessary, dump input data and initial point
124   // Problem1.writeInputSparse((char*)"tmp.dat-s",(char*)"%+8.3e");
125   // Problem1.writeInitSparse((char*)"tmp.ini-s",(char*)"%+8.3e");
126 
127   Problem1.solve();
128 
129   fprintf(stdout, "\nStop iteration = %d\n",
130 	  Problem1.getIteration());
131   char phase_string[30];
132   Problem1.getPhaseString(phase_string);
133   fprintf(stdout, "Phase          = %s\n", phase_string);
134   fprintf(stdout, "objValPrimal   = %+10.6e\n",
135 	  Problem1.getPrimalObj());
136   fprintf(stdout, "objValDual     = %+10.6e\n",
137 	  Problem1.getDualObj());
138   fprintf(stdout, "p. feas. error = %+10.6e\n",
139 	  Problem1.getPrimalError());
140   fprintf(stdout, "d. feas. error = %+10.6e\n\n",
141 	  Problem1.getDualError());
142 
143 
144   fprintf(stdout, "xVec = \n");
145   // Problem1.printResultXVec();
146   printVector(Problem1.getResultXVec(),
147 	      Problem1.getConstraintNumber(), (char*)"%+8.3e",
148 	      stdout);
149 
150   fprintf(stdout, "xMat = \n");
151   // Problem1.printResultXMat();
152   for (int l=0; l<Problem1.getBlockNumber(); ++l) {
153     if (Problem1.getBlockType(l+1) == SDPA::SDP) {
154       printMatrix(Problem1.getResultXMat(l+1),
155 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
156 		  stdout);
157     }
158     else if (Problem1.getBlockType(l+1) == SDPA::SOCP) {
159       printf("current version does not support SOCP\n");
160     }
161     if (Problem1.getBlockType(l+1) == SDPA::LP) {
162       printVector(Problem1.getResultXMat(l+1),
163 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
164 		  stdout);
165     }
166   }
167 
168   fprintf(stdout, "yMat = \n");
169   // Problem1.printResultYMat();
170   for (int l=0; l<Problem1.getBlockNumber(); ++l) {
171     if (Problem1.getBlockType(l+1) == SDPA::SDP) {
172       printMatrix(Problem1.getResultYMat(l+1),
173 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
174 		  stdout);
175     }
176     else if (Problem1.getBlockType(l+1) == SDPA::SOCP) {
177       printf("current version does not support SOCP\n");
178     }
179     if (Problem1.getBlockType(l+1) == SDPA::LP) {
180       printVector(Problem1.getResultYMat(l+1),
181 		  Problem1.getBlockSize(l+1), (char*)"%+8.3e",
182 		  stdout);
183     }
184   }
185 
186   double dimacs_error[7];
187   Problem1.getDimacsError(dimacs_error);
188   printDimacsError(dimacs_error,(char*)"%+8.3e",stdout);
189 
190   // Problem1.printComputationTime(stdout);
191 
192   Problem1.terminate();
193   exit(0);
194 };
195 
printVector(double * ele,int dim,char * printFormat,FILE * fpout)196 void printVector(double* ele, int dim, char* printFormat, FILE* fpout)
197 {
198   fprintf(fpout,"[ ");
199   for (int k=0; k<dim-1; ++k) {
200     fprintf(fpout,printFormat,ele[k]);
201     fprintf(fpout," ");
202   }
203   fprintf(fpout,printFormat,ele[dim-1]);
204   fprintf(fpout,"]; \n");
205 }
206 
printMatrix(double * ele,int dim,char * printFormat,FILE * fpout)207 void printMatrix(double* ele, int dim, char* printFormat, FILE* fpout)
208 {
209   fprintf(fpout,"[\n");
210   for (int i=0; i<dim; ++i) {
211     fprintf(fpout,"[ ");
212     for (int j=0; j<dim-1; ++j) {
213       fprintf(fpout,printFormat,ele[i+dim*j]);
214       fprintf(fpout," ");
215     }
216     fprintf(fpout,printFormat,ele[i+dim*(dim-1)]);
217     fprintf(fpout,"]; \n");
218   }
219   fprintf(fpout,"]; \n");
220 }
221 
printDimacsError(double dimacs_error[7],char * printFormat,FILE * fpout)222 void printDimacsError(double dimacs_error[7],char* printFormat,
223 		      FILE* fpout)
224 {
225   fprintf(fpout,  "\n");
226   fprintf(fpout,  "* DIMACS_ERRORS * \n");
227   fprintf(fpout,  "err1 = ");
228   fprintf(fpout,  printFormat, dimacs_error[1]);
229   fprintf(fpout, "  [||Ax-b|| / (1+||b||_1)]\n");
230   fprintf(fpout,  "err2 = ");
231   fprintf(fpout,  printFormat, dimacs_error[2]);
232   fprintf(fpout, "  [max(0, -lambda(x)/(1+||b||_1))]\n");
233   fprintf(fpout,  "err3 = ");
234   fprintf(fpout,  printFormat, dimacs_error[3]);
235   fprintf(fpout, "  [||A^Ty + z - c || / (1+||c||_1)]\n");
236   fprintf(fpout,  "err4 = ");
237   fprintf(fpout,  printFormat, dimacs_error[4]);
238   fprintf(fpout, "  [max(0, -lambda(z)/(1+||c||_1))]\n");
239   fprintf(fpout,  "err5 = ");
240   fprintf(fpout,  printFormat, dimacs_error[5]);
241   fprintf(fpout, "  [(<c,x> - <b,y>) / (1 + |<c,x>| + |<b,y>|)]\n");
242   fprintf(fpout,  "err6 = ");
243   fprintf(fpout,  printFormat, dimacs_error[6]);
244   fprintf(fpout, "  [<x,z> / (1 + |<c,x>| + |<b,y>|)]\n");
245   fprintf(fpout,  "\n");
246 }
247 
248