1 /*
2  *	This file is part of qpOASES.
3  *
4  *	qpOASES -- An Implementation of the Online Active Set Strategy.
5  *	Copyright (C) 2007-2017 by Hans Joachim Ferreau, Andreas Potschka,
6  *	Christian Kirches et al. All rights reserved.
7  *
8  *	qpOASES is free software; you can redistribute it and/or
9  *	modify it under the terms of the GNU Lesser General Public
10  *	License as published by the Free Software Foundation; either
11  *	version 2.1 of the License, or (at your option) any later version.
12  *
13  *	qpOASES is distributed in the hope that it will be useful,
14  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *	See the GNU Lesser General Public License for more details.
17  *
18  *	You should have received a copy of the GNU Lesser General Public
19  *	License along with qpOASES; if not, write to the Free Software
20  *	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 
25 /**
26  *	\file testing/cpp/test_qrecipe.cpp
27  *	\author Andreas Potschka
28  *	\version 3.2
29  *	\date 2007-2017
30  *
31  *	QRECIPE example from the CUTEr test set with sparse matrices.
32  */
33 
34 
35 
36 #include <qpOASES.hpp>
37 #include <qpOASES/UnitTesting.hpp>
38 
39 #include "test_qrecipe_data.hpp"
40 
41 
42 
main()43 int main( )
44 {
45 	USING_NAMESPACE_QPOASES
46 
47 	long i;
48 	int_t nWSR;
49 	real_t tic, toc;
50 	real_t errP=0.0, errD=0.0;
51 	real_t *x1 = new real_t[180];
52 	real_t *y1 = new real_t[271];
53 	real_t *x2 = new real_t[180];
54 	real_t *y2 = new real_t[271];
55 
56 	/* create sparse matrices */
57 	SymSparseMat *H = new SymSparseMat(180, 180, H_ir, H_jc, H_val);
58 	SparseMatrix *A = new SparseMatrix(91, 180, A_ir, A_jc, A_val);
59 
60 	H->createDiagInfo();
61 
62 	real_t* H_full = H->full();
63 	real_t* A_full = A->full();
64 
65 	SymDenseMat *Hd = new SymDenseMat(180,180,180,H_full);
66 	DenseMatrix *Ad = new DenseMatrix(91,180,180,A_full);
67 
68 	/* solve with dense matrices */
69 	nWSR = 1000;
70 	QProblem qrecipeD(180, 91);
71 	tic = getCPUtime();
72 	qrecipeD.init(Hd, g, Ad, lb, ub, lbA, ubA, nWSR, 0);
73 	toc = getCPUtime();
74 	qrecipeD.getPrimalSolution(x1);
75 	qrecipeD.getDualSolution(y1);
76 
77 	fprintf(stdFile, "Solved dense problem in %d iterations, %.3f seconds.\n", (int)nWSR, toc-tic);
78 
79 	/* Compute KKT tolerances */
80 	real_t statD, feasD, cmplD;
81 	SolutionAnalysis analyzerD;
82 
83 	analyzerD.getKktViolation( &qrecipeD, &statD,&feasD,&cmplD );
84 	printf( "stat = %e\nfeas = %e\ncmpl = %e\n\n", statD,feasD,cmplD );
85 
86 
87 	/* solve with sparse matrices */
88 	nWSR = 1000;
89 	QProblem qrecipeS(180, 91);
90 	tic = getCPUtime();
91 	qrecipeS.init(H, g, A, lb, ub, lbA, ubA, nWSR, 0);
92 	toc = getCPUtime();
93 	qrecipeS.getPrimalSolution(x2);
94 	qrecipeS.getDualSolution(y2);
95 
96 	fprintf(stdFile, "Solved sparse problem in %d iterations, %.3f seconds.\n", (int)nWSR, toc-tic);
97 
98 	/* Compute KKT tolerances */
99 	real_t statS, feasS, cmplS;
100 	SolutionAnalysis analyzerS;
101 
102 	analyzerS.getKktViolation( &qrecipeS, &statS,&feasS,&cmplS );
103 	printf( "stat = %e\nfeas = %e\ncmpl = %e\n\n", statS,feasS,cmplS );
104 
105 	/* check distance of solutions */
106 	for (i = 0; i < 180; i++)
107 		if (getAbs(x1[i] - x2[i]) > errP)
108 			errP = getAbs(x1[i] - x2[i]);
109 	fprintf(stdFile, "Primal error: %9.2e\n", errP);
110 
111 	for (i = 0; i < 271; i++)
112 		if (getAbs(y1[i] - y2[i]) > errD)
113 			errD = getAbs(y1[i] - y2[i]);
114 	fprintf(stdFile, "Dual error: %9.2e (might not be unique)\n", errD);
115 
116 	delete H;
117 	delete A;
118 	delete[] H_full;
119 	delete[] A_full;
120 	delete Hd;
121 	delete Ad;
122 
123 	delete[] y2;
124 	delete[] x2;
125 	delete[] y1;
126 	delete[] x1;
127 
128 	QPOASES_TEST_FOR_TOL( statD,1e-14 );
129 	QPOASES_TEST_FOR_TOL( feasD,1e-14 );
130 	QPOASES_TEST_FOR_TOL( cmplD,1e-13 );
131 
132 	QPOASES_TEST_FOR_TOL( statS,1e-14 );
133 	QPOASES_TEST_FOR_TOL( feasS,1e-14 );
134 	QPOASES_TEST_FOR_TOL( cmplS,1e-13 );
135 
136 	QPOASES_TEST_FOR_TOL( errP,1e-13 );
137 
138 	return TEST_PASSED;
139 }
140 
141 
142 /*
143  *	end of file
144  */
145