Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TQpDataSparse.cxx
Go to the documentation of this file.
1// @(#)root/quadp:$Id$
2// Author: Eddy Offermann May 2004
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/*************************************************************************
13 * Parts of this file are copied from the OOQP distribution and *
14 * are subject to the following license: *
15 * *
16 * COPYRIGHT 2001 UNIVERSITY OF CHICAGO *
17 * *
18 * The copyright holder hereby grants you royalty-free rights to use, *
19 * reproduce, prepare derivative works, and to redistribute this software*
20 * to others, provided that any changes are clearly documented. This *
21 * software was authored by: *
22 * *
23 * E. MICHAEL GERTZ gertz@mcs.anl.gov *
24 * Mathematics and Computer Science Division *
25 * Argonne National Laboratory *
26 * 9700 S. Cass Avenue *
27 * Argonne, IL 60439-4844 *
28 * *
29 * STEPHEN J. WRIGHT swright@cs.wisc.edu *
30 * Computer Sciences Department *
31 * University of Wisconsin *
32 * 1210 West Dayton Street *
33 * Madison, WI 53706 FAX: (608)262-9777 *
34 * *
35 * Any questions or comments may be directed to one of the authors. *
36 * *
37 * ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES OF *
38 * ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, AND *
39 * OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A CONTRACT *
40 * WITH THE DEPARTMENT OF ENERGY. *
41 *************************************************************************/
42
43#include "TQpDataSparse.h"
44
45////////////////////////////////////////////////////////////////////////////////
46///
47/// \class TQpDataSparse
48///
49/// Data for the sparse QP formulation
50///
51////////////////////////////////////////////////////////////////////////////////
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor
56
58: TQpDataBase(nx,my,mz)
59{
60 fQ.ResizeTo(fNx,fNx);
61 fA.ResizeTo(fMy,fNx);
62 fC.ResizeTo(fMz,fNx);
63}
64
65
66////////////////////////////////////////////////////////////////////////////////
67/// Constructor
68
70 TVectorD &xlow_in,TVectorD &ixlow_in,
71 TVectorD &xupp_in,TVectorD &ixupp_in,
72 TMatrixDSparse &A_in, TVectorD &bA_in,
73 TMatrixDSparse &C_in,
74 TVectorD &clow_in,TVectorD &iclow_in,
75 TVectorD &cupp_in,TVectorD &icupp_in)
76{
77 fG .ResizeTo(c_in) ; fG = c_in;
78 fBa .ResizeTo(bA_in) ; fBa = bA_in;
79 fXloBound.ResizeTo(xlow_in) ; fXloBound = xlow_in;
80 fXloIndex.ResizeTo(ixlow_in); fXloIndex = ixlow_in;
81 fXupBound.ResizeTo(xupp_in) ; fXupBound = xupp_in;
82 fXupIndex.ResizeTo(ixupp_in); fXupIndex = ixupp_in;
83 fCloBound.ResizeTo(clow_in) ; fCloBound = clow_in;
84 fCloIndex.ResizeTo(iclow_in); fCloIndex = iclow_in;
85 fCupBound.ResizeTo(cupp_in) ; fCupBound = cupp_in;
86 fCupIndex.ResizeTo(icupp_in); fCupIndex = icupp_in;
87
88 fNx = fG.GetNrows();
89 fQ.Use(Q_in);
90
91 if (A_in.GetNrows() > 0) {
92 fA.Use(A_in);
93 fMy = fA.GetNrows();
94 } else
95 fMy = 0;
96
97 if (C_in.GetNrows()) {
98 fC.Use(C_in);
99 fMz = fC.GetNrows();
100 } else
101 fMz = 0;
102 // fQ.Print();
103 // fA.Print();
104 // fC.Print();
105 // printf("fNx: %d\n",fNx);
106 // printf("fMy: %d\n",fMy);
107 // printf("fMz: %d\n",fMz);
108}
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Copy constructor
113
115{
116 *this = another;
117}
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Allocate space for the appropriate number of non-zeros in the matrices
122
124{
125 fQ.SetSparseIndex(nnzQ);
126 fA.SetSparseIndex(nnzA);
127 fC.SetSparseIndex(nnzC);
128}
129
130
131////////////////////////////////////////////////////////////////////////////////
132/// calculate y = beta*y + alpha*(fQ*x)
133
135{
136 y *= beta;
137 if (fQ.GetNoElements() > 0)
138 y += alpha*(fQ*x);
139}
140
141
142////////////////////////////////////////////////////////////////////////////////
143/// calculate y = beta*y + alpha*(fA*x)
144
146{
147 y *= beta;
148 if (fA.GetNoElements() > 0)
149 y += alpha*(fA*x);
150}
151
152
153////////////////////////////////////////////////////////////////////////////////
154/// calculate y = beta*y + alpha*(fC*x)
155
157{
158 y *= beta;
159 if (fC.GetNoElements() > 0)
160 y += alpha*(fC*x);
161}
162
163
164////////////////////////////////////////////////////////////////////////////////
165/// calculate y = beta*y + alpha*(fA^T*x)
166
168{
169 y *= beta;
170 if (fA.GetNoElements() > 0)
172}
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// calculate y = beta*y + alpha*(fC^T*x)
177
179{
180 y *= beta;
181 if (fC.GetNoElements() > 0)
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Return the largest component of several vectors in the data class
188
190{
191 Double_t norm = 0.0;
192
193 Double_t componentNorm = fG.NormInf();
194 if (componentNorm > norm) norm = componentNorm;
195
196 TMatrixDSparse fQ_abs(fQ);
197 componentNorm = (fQ_abs.Abs()).Max();
198 if (componentNorm > norm) norm = componentNorm;
199
200 componentNorm = fBa.NormInf();
201 if (componentNorm > norm) norm = componentNorm;
202
203 TMatrixDSparse fA_abs(fA);
204 componentNorm = (fA_abs.Abs()).Max();
205 if (componentNorm > norm) norm = componentNorm;
206
207 TMatrixDSparse fC_abs(fC);
208 componentNorm = (fC_abs.Abs()).Max();
209 if (componentNorm > norm) norm = componentNorm;
210
211 R__ASSERT(fXloBound.MatchesNonZeroPattern(fXloIndex));
212 componentNorm = fXloBound.NormInf();
213 if (componentNorm > norm) norm = componentNorm;
214
215 R__ASSERT(fXupBound.MatchesNonZeroPattern(fXupIndex));
216 componentNorm = fXupBound.NormInf();
217 if (componentNorm > norm) norm = componentNorm;
218
219 R__ASSERT(fCloBound.MatchesNonZeroPattern(fCloIndex));
220 componentNorm = fCloBound.NormInf();
221 if (componentNorm > norm) norm = componentNorm;
222
223 R__ASSERT(fCupBound.MatchesNonZeroPattern(fCupIndex));
224 componentNorm = fCupBound.NormInf();
225 if (componentNorm > norm) norm = componentNorm;
226
227 return norm;
228}
229
230
231////////////////////////////////////////////////////////////////////////////////
232/// Print class members
233
234void TQpDataSparse::Print(Option_t * /*opt*/) const
235{
236 fQ.Print("Q");
237 fG.Print("c");
238
239 fXloBound.Print("xlow");
240 fXloIndex.Print("ixlow");
241
242 fXupBound.Print("xupp");
243 fXupIndex.Print("ixupp");
244
245 fA.Print("A");
246 fBa.Print("b");
247 fC.Print("C");
248
249 fCloBound.Print("clow");
250 fCloIndex.Print("iclow");
251
252 fCupBound.Print("cupp");
253 fCupIndex.Print("icupp");
254}
255
256
257////////////////////////////////////////////////////////////////////////////////
258/// Insert the Hessian Q into the matrix M at index (row,col) for the fundamental
259/// linear system
260
262{
263 m.SetSub(row,col,fQ);
264}
265
266
267////////////////////////////////////////////////////////////////////////////////
268/// Insert the constraint matrix A into the matrix M at index (row,col) for the fundamental
269/// linear system
270
272{
273 m.SetSub(row,col,fA);
274}
275
276
277////////////////////////////////////////////////////////////////////////////////
278/// Insert the constraint matrix C into the matrix M at index (row,col) for the fundamental
279/// linear system
280
282{
283 m.SetSub(row,col,fC);
284}
285
286
287////////////////////////////////////////////////////////////////////////////////
288/// Return in vector dq the diagonal of matrix fQ
289
291{
292 const Int_t n = TMath::Min(fQ.GetNrows(),fQ.GetNcols());
293 dq.ResizeTo(n);
295}
296
297
298////////////////////////////////////////////////////////////////////////////////
299/// Return value of the objective function
300
302{
303 TVectorD tmp(fG);
304 this->Qmult(1.0,tmp,0.5,vars->fX);
305
306 return tmp*vars->fX;
307}
308
309
310////////////////////////////////////////////////////////////////////////////////
311/// Choose randomly a QP problem
312
314{
315 Double_t ix = 3074.20374;
316
317 TVectorD xdual(fNx);
319
320 TVectorD sprime(fMz);
322
323 fQ.RandomizePD(0.0,1.0,ix);
324 fA.Randomize(-10.0,10.0,ix);
325 fC.Randomize(-10.0,10.0,ix);
326 y .Randomize(-10.0,10.0,ix);
327
328 // fG = - fQ x + A^T y + C^T z + xdual
329
330 fG = xdual;
331 fG -= fQ*x;
332
335
336 fBa = fA*x;
337 s = fC*x;
338
339 // Now compute the real q = s-sprime
340 const TVectorD q = s-sprime;
341
342 // Adjust fCloBound and fCupBound appropriately
343 Add(fCloBound,1.0,q);
344 Add(fCupBound,1.0,q);
345
346 fCloBound.SelectNonZeros(fCloIndex);
347 fCupBound.SelectNonZeros(fCupIndex);
348}
349
350
351////////////////////////////////////////////////////////////////////////////////
352/// Assignment operator
353
355{
356 if (this != &source) {
358 fQ.ResizeTo(source.fQ); fQ = source.fQ;
359 fA.ResizeTo(source.fA); fA = source.fA;
360 fC.ResizeTo(source.fC); fC = source.fC;
361 }
362 return *this;
363}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
float * q
TMatrixTBase< Double_t > TMatrixDBase
TMatrixTSparse< Double_t > TMatrixDSparse
TMatrixTSparseDiag< Double_t > TMatrixDSparseDiag
TVectorT< Double_t > TVectorD
Definition TVectorDfwd.h:23
Int_t GetNrows() const
virtual TMatrixTBase< Element > & Abs()
Take an absolute value of a matrix, i.e. apply Abs() to each element.
TQpDataBase & operator=(const TQpDataBase &source)
Assignment operator.
TVectorD fXupIndex
Definition TQpDataBase.h:80
TVectorD fXloBound
Definition TQpDataBase.h:81
TVectorD fCloIndex
Definition TQpDataBase.h:86
TVectorD fCupIndex
Definition TQpDataBase.h:84
TVectorD fG
Definition TQpDataBase.h:77
TVectorD fXupBound
Definition TQpDataBase.h:79
TQpDataBase()
Default constructor.
TVectorD fXloIndex
Definition TQpDataBase.h:82
TVectorD fCupBound
Definition TQpDataBase.h:83
TVectorD fCloBound
Definition TQpDataBase.h:85
TVectorD fBa
Definition TQpDataBase.h:78
static void RandomlyChooseBoundedVariables(TVectorD &x, TVectorD &dualx, TVectorD &blx, TVectorD &ixlow, TVectorD &bux, TVectorD &ixupp, Double_t &ix, Double_t percentLowerOnly, Double_t percentUpperOnly, Double_t percentBound)
Randomly choose x and its boundaries.
Double_t ObjectiveValue(TQpVar *vars) override
Return value of the objective function.
Double_t DataNorm() override
Return the largest component of several vectors in the data class.
void PutAIntoAt(TMatrixDBase &M, Int_t row, Int_t col) override
Insert the constraint matrix A into the matrix M at index (row,col) for the fundamental linear system...
void Amult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fA*x)
void PutQIntoAt(TMatrixDBase &M, Int_t row, Int_t col) override
Insert the Hessian Q into the matrix M at index (row,col) for the fundamental linear system.
void DataRandom(TVectorD &x, TVectorD &y, TVectorD &z, TVectorD &s) override
Choose randomly a QP problem.
void Print(Option_t *opt="") const override
Print class members.
void PutCIntoAt(TMatrixDBase &M, Int_t row, Int_t col) override
Insert the constraint matrix C into the matrix M at index (row,col) for the fundamental linear system...
TQpDataSparse & operator=(const TQpDataSparse &source)
Assignment operator.
TMatrixDSparse fC
TMatrixDSparse fQ
void SetNonZeros(Int_t nnzQ, Int_t nnzA, Int_t nnzC)
Allocate space for the appropriate number of non-zeros in the matrices.
void Cmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fC*x)
void Qmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fQ*x)
void GetDiagonalOfQ(TVectorD &dQ) override
Return in vector dq the diagonal of matrix fQ.
TMatrixDSparse fA
void CTransmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fC^T*x)
void ATransmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fA^T*x)
Class containing the variables for the general QP formulation.
Definition TQpVar.h:60
TVectorD fX
Definition TQpVar.h:91
TVectorT< Element > & ResizeTo(Int_t lwb, Int_t upb)
Resize the vector to [lwb:upb] .
Definition TVectorT.cxx:294
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.
TMarker m
Definition textangle.C:8