normal_conjugate_gradient_method.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
35 
36 
38 
39 
40 
41 //
42 // PUBLIC MEMBER FUNCTIONS:
43 //
44 
45 
48 
49  index = 0;
50  nIndex = 0;
51  iResult = 0;
52 }
53 
54 
57 
58  int run1, run2;
59 
60  if( arg.nIndex != 0 ){
61  nIndex = new int[dim];
62  for( run1 = 0; run1 < dim; run1++ ){
63  nIndex[run1] = arg.nIndex[run1];
64  }
65  }
66  else nIndex = 0;
67 
68  if( arg.index != 0 ){
69  index = new int*[dim];
70  for( run1 = 0; run1 < dim; run1++ ){
71  index[run1] = new int[nIndex[run1]];
72  for( run2 = 0; run2 < nIndex[run1]; run2++ )
73  index[run1][run2] = arg.index[run1][run2];
74  }
75  }
76  else index = 0;
77 
78  if( arg.iResult != 0 ){
79  iResult = new double[dim];
80  for( run1 = 0; run1 < dim; run1++ ){
81  iResult[run1] = arg.iResult[run1];
82  }
83  }
84  else iResult = 0;
85 }
86 
87 
89 
90  int run1;
91 
92  if( nIndex != 0 ) delete[] nIndex;
93 
94  if( iResult != 0 ) delete[] iResult;
95 
96  if( index != 0 ){
97  for( run1 = 0; run1 < dim; run1++ )
98  delete[] index[run1];
99  delete[] index;
100  }
101 }
102 
103 
104 
106 
107  return new NormalConjugateGradientMethod(*this);
108 }
109 
110 
111 returnValue NormalConjugateGradientMethod::setIndices( const int *rowIdx_, const int *colIdx_ ){
112 
114 }
115 
116 
118 
119 
120  // CONSISTENCY CHECK:
121  // ------------------
122 
123  if( dim <= 0 ) return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
124  if( nDense <= 0 ) return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
125 
126 
127  // ALLOCATE MEMORY:
128  // ------------------
129 
130  if( nIndex != 0 ) delete[] nIndex;
131  nIndex = new int[dim];
132 
133  if( iResult != 0 ) delete[] iResult;
134  iResult = new double[dim];
135 
136 
137  // LOCAL AUXILIARY VARIABLE:
138  // -------------------------
139  int run1, run2;
140  int counter0 ;
141  int bound ;
142  int counter = 0;
143 
144 
145  // DETERMINE THE INDEX LENGHTS:
146  // ----------------------------
147 
148 
149  for( run1 = 0; run1 < dim; run1++ ){
150 
151  counter0 = counter;
152  bound = dim*(run1+1);
153 
154  while( counter < nDense ){
155 
156  if( indices[counter] >= bound ) break;
157  counter++;
158  }
159 
160  nIndex[run1] = counter - counter0;
161  }
162 
163  if( index != 0 ){
164  for( run1 = 0; run1 < dim; run1++ )
165  delete[] index[run1];
166  delete[] index;
167  }
168  index = new int*[dim];
169 
170  counter = 0;
171 
172  for( run1 = 0; run1 < dim; run1++ ){
173  index[run1] = new int[nIndex[run1]];
174  for( run2 = 0; run2 < nIndex[run1]; run2++ ){
175  index [run1][run2] = indices[counter];
176  counter++;
177  }
178  }
179 
180  return SUCCESSFUL_RETURN;
181 }
182 
183 
184 
185 
186 
187 
188 
189 
190 //
191 // PROTECTED MEMBER FUNCTIONS:
192 //
193 
194 
195 
196 void NormalConjugateGradientMethod::multiply( double *xx , double *result ){
197 
198 
199  int i,j,counter, offset;
200 
201  counter = 0;
202 
203  for( i = 0; i < dim; i++ ){
204  iResult[i] = 0.0;
205  result [i] = 0.0;
206  offset = dim*i;
207  for( j = 0; j < nIndex[i]; j++ ){
208  iResult[i] += A[counter]*xx[index[i][j]-offset];
209  counter++;
210  }
211  }
212 
213  counter = 0;
214 
215  for( i = 0; i < dim; i++ ){
216  offset = dim*i;
217  for( j = 0; j < nIndex[i]; j++ ){
218  result[index[i][j]-offset] += A[counter]*iResult[i];
219  counter++;
220  }
221  }
222 }
223 
224 
225 
226 
228 
229  int i,j,counter;
230 
231  for( i = 0; i < dim; i++ ){
232  condScale[i] = 0.0;
233  }
234 
235  int offset;
236  counter = 0;
237 
238  for( i = 0; i < dim; i++ ){
239  offset = dim*i;
240  for( j = 0; j < nIndex[i]; j++ ){
241  condScale[index[i][j]-offset] += A_[counter]*A_[counter];
242  counter++;
243  }
244  }
245 
246  for( i = 0; i < dim; i++ ){
247  condScale[i] = sqrt(condScale[i]);
248  }
249 
250  counter = 0;
251 
252  for( i = 0; i < dim; i++ ){
253  for( j = 0; j < nIndex[i]; j++ ){
254  A[counter] = A_[counter]/condScale[i];
255  counter++;
256  }
257  }
258 
259  return SUCCESSFUL_RETURN;
260 }
261 
262 
263 
265 
266  int i,j, counter;
267 
268  for( i = 0; i < dim; i++ )
269  iResult[i] = b[i]/condScale[i];
270 
271  counter = 0;
272  int offset;
273 
274  for( i = 0; i < dim; i++ )
275  r[i] = 0.0;
276 
277  for( i = 0; i < dim; i++ ){
278  offset = dim*i;
279  for( j = 0; j < nIndex[i]; j++ ){
280  r[index[i][j]-offset] += A[counter]*iResult[i];
281  counter++;
282  }
283  }
284  return SUCCESSFUL_RETURN;
285 }
286 
287 
288 
289 
291 
292  int run1;
293 
294  for( run1 = 0; run1 < dim; run1++ )
295  x_[run1] = x[run1];
296 
297  return SUCCESSFUL_RETURN;
298 }
299 
300 
301 
303 
304 
305 /*
306  * end of file
307  */
int counter
Definition: powerkite_c.cpp:40
IntermediateState sqrt(const Expression &arg)
Allows to pass back messages to the calling function.
#define CLOSE_NAMESPACE_ACADO
Implements a conjugate gradient method as sparse linear algebra solver for non-symmetric linear syste...
virtual void multiply(double *xx, double *result)
virtual returnValue computePreconditioner(double *A_)
virtual returnValue setIndices(const int *rowIdx_, const int *colIdx_)
virtual returnValue applyPreconditioner(double *b)
Implements a conjugate gradient method as sparse linear algebra solver.
Generic interface for sparse solvers to be coupled with ACADO Toolkit.
#define BEGIN_NAMESPACE_ACADO
virtual returnValue applyInversePreconditioner(double *x_)
#define ACADOERROR(retval)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:54