Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TQpDataDens.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 "TQpDataDens.h"
44
45////////////////////////////////////////////////////////////////////////////////
46///
47/// \class TQpDataDens
48///
49/// Data for the dense 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 TMatrixD &A_in, TVectorD &bA_in,
73 TMatrixD &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() > 0) {
98 fC.Use(C_in);
99 fMz = fC.GetNrows();
100 } else
101 fMz = 0;
102}
103
104
105////////////////////////////////////////////////////////////////////////////////
106/// Copy constructor
107
109{
110 *this = another;
111}
112
113
114////////////////////////////////////////////////////////////////////////////////
115/// calculate y = beta*y + alpha*(fQ*x)
116
118{
119 y *= beta;
120 if (fQ.GetNoElements() > 0)
121 y += alpha*(fQ*x);
122}
123
124
125////////////////////////////////////////////////////////////////////////////////
126/// calculate y = beta*y + alpha*(fA*x)
127
129{
130 y *= beta;
131 if (fA.GetNoElements() > 0)
132 y += alpha*(fA*x);
133}
134
135
136////////////////////////////////////////////////////////////////////////////////
137/// calculate y = beta*y + alpha*(fC*x)
138
140{
141 y *= beta;
142 if (fC.GetNoElements() > 0)
143 y += alpha*(fC*x);
144}
145
146
147////////////////////////////////////////////////////////////////////////////////
148/// calculate y = beta*y + alpha*(fA^T*x)
149
151{
152 y *= beta;
153 if (fA.GetNoElements() > 0)
154 y += alpha*(TMatrixD(TMatrixD::kTransposed,fA)*x);
155}
156
157
158////////////////////////////////////////////////////////////////////////////////
159/// calculate y = beta*y + alpha*(fC^T*x)
160
162{
163 y *= beta;
164 if (fC.GetNoElements() > 0)
165 y += alpha*(TMatrixD(TMatrixD::kTransposed,fC)*x);
166}
167
168
169////////////////////////////////////////////////////////////////////////////////
170/// Return the largest component of several vectors in the data class
171
173{
174 Double_t norm = 0.0;
175
176 Double_t componentNorm = fG.NormInf();
177 if (componentNorm > norm) norm = componentNorm;
178
179 TMatrixDSym fQ_abs(fQ);
180 componentNorm = (fQ_abs.Abs()).Max();
181 if (componentNorm > norm) norm = componentNorm;
182
183 componentNorm = fBa.NormInf();
184 if (componentNorm > norm) norm = componentNorm;
185
186 TMatrixD fA_abs(fQ);
187 componentNorm = (fA_abs.Abs()).Max();
188 if (componentNorm > norm) norm = componentNorm;
189
190 TMatrixD fC_abs(fQ);
191 componentNorm = (fC_abs.Abs()).Max();
192 if (componentNorm > norm) norm = componentNorm;
193
194 R__ASSERT(fXloBound.MatchesNonZeroPattern(fXloIndex));
195 componentNorm = fXloBound.NormInf();
196 if (componentNorm > norm) norm = componentNorm;
197
198 R__ASSERT(fXupBound.MatchesNonZeroPattern(fXupIndex));
199 componentNorm = fXupBound.NormInf();
200 if (componentNorm > norm) norm = componentNorm;
201
202 R__ASSERT(fCloBound.MatchesNonZeroPattern(fCloIndex));
203 componentNorm = fCloBound.NormInf();
204 if (componentNorm > norm) norm = componentNorm;
205
206 R__ASSERT(fCupBound.MatchesNonZeroPattern(fCupIndex));
207 componentNorm = fCupBound.NormInf();
208 if (componentNorm > norm) norm = componentNorm;
209
210 return norm;
211}
212
213
214////////////////////////////////////////////////////////////////////////////////
215/// Print all class members
216
217void TQpDataDens::Print(Option_t * /*opt*/) const
218{
219 fQ.Print("Q");
220 fG.Print("c");
221
222 fXloBound.Print("xlow");
223 fXloIndex.Print("ixlow");
224
225 fXupBound.Print("xupp");
226 fXupIndex.Print("ixupp");
227
228 fA.Print("A");
229 fBa.Print("b");
230 fC.Print("C");
231
232 fCloBound.Print("clow");
233 fCloIndex.Print("iclow");
234
235 fCupBound.Print("cupp");
236 fCupIndex.Print("icupp");
237}
238
239
240////////////////////////////////////////////////////////////////////////////////
241/// Insert the Hessian Q into the matrix M at index (row,col) for the fundamental
242/// linear system
243
245{
246 m.SetSub(row,col,fQ);
247}
248
249
250////////////////////////////////////////////////////////////////////////////////
251/// Insert the constraint matrix A into the matrix M at index (row,col) for the fundamental
252/// linear system
253
255{
256 m.SetSub(row,col,fA);
257}
258
259
260////////////////////////////////////////////////////////////////////////////////
261/// Insert the constraint matrix C into the matrix M at index (row,col) for the fundamental
262/// linear system
263
265{
266 m.SetSub(row,col,fC);
267}
268
269
270////////////////////////////////////////////////////////////////////////////////
271/// Return in vector dq the diagonal of matrix fQ (Quadratic part of Objective function)
272
274{
275 const Int_t n = TMath::Min(fQ.GetNrows(),fQ.GetNcols());
276 dq.ResizeTo(n);
277 dq = TMatrixDDiag(fQ);
278}
279
280
281////////////////////////////////////////////////////////////////////////////////
282/// Return value of the objective function
283
285{
286 TVectorD tmp(fG);
287 this->Qmult(1.0,tmp,0.5,vars->fX);
288
289 return tmp*vars->fX;
290}
291
292
293////////////////////////////////////////////////////////////////////////////////
294/// Choose randomly a QP problem
295
297{
298 Double_t ix = 3074.20374;
299
300 TVectorD xdual(fNx);
302 TVectorD sprime(fMz);
304
305 fQ.RandomizePD(0.0,1.0,ix);
306 fA.Randomize(-10.0,10.0,ix);
307 fC.Randomize(-10.0,10.0,ix);
308 y .Randomize(-10.0,10.0,ix);
309
310 fG = xdual;
311 fG -= fQ*x;
312
315
316 fBa = fA*x;
317 s = fC*x;
318
319 // Now compute the real q = s-sprime
320 const TVectorD q = s-sprime;
321
322 // Adjust fCloBound and fCupBound appropriately
323 Add(fCloBound,1.0,q);
324 Add(fCupBound,1.0,q);
325
326 fCloBound.SelectNonZeros(fCloIndex);
327 fCupBound.SelectNonZeros(fCupIndex);
328}
329
330
331////////////////////////////////////////////////////////////////////////////////
332/// Assignment operator
333
335{
336 if (this != &source) {
338 fQ.ResizeTo(source.fQ); fQ = source.fQ;
339 fA.ResizeTo(source.fA); fA = source.fA;
340 fC.ResizeTo(source.fC); fC = source.fC;
341 }
342 return *this;
343}
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
TMatrixTSym< Double_t > TMatrixDSym
TMatrixTDiag< Double_t > TMatrixDDiag
TMatrixT< Double_t > TMatrixD
Definition TMatrixDfwd.h:23
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 DataNorm() override
Return the largest component of several vectors in the data class.
void Qmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fQ*x)
void Cmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fC*x)
void GetDiagonalOfQ(TVectorD &dQ) override
Return in vector dq the diagonal of matrix fQ (Quadratic part of Objective function).
void DataRandom(TVectorD &x, TVectorD &y, TVectorD &z, TVectorD &s) override
Choose randomly a QP problem.
TMatrixD fC
Definition TQpDataDens.h:70
void Amult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fA*x)
TMatrixDSym fQ
Definition TQpDataDens.h:68
Double_t ObjectiveValue(TQpVar *vars) override
Return value of the objective function.
TMatrixD fA
Definition TQpDataDens.h:69
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...
void Print(Option_t *opt="") const override
Print all class members.
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 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 ATransmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fA^T*x)
TQpDataDens & operator=(const TQpDataDens &source)
Assignment operator.
void CTransmult(Double_t beta, TVectorD &y, Double_t alpha, const TVectorD &x) override
calculate y = beta*y + alpha*(fC^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