00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "Riostream.h"
00044 #include "TQpDataSparse.h"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 ClassImp(TQpDataSparse)
00055
00056
00057 TQpDataSparse::TQpDataSparse(Int_t nx,Int_t my,Int_t mz)
00058 : TQpDataBase(nx,my,mz)
00059 {
00060
00061
00062 fQ.ResizeTo(fNx,fNx);
00063 fA.ResizeTo(fMy,fNx);
00064 fC.ResizeTo(fMz,fNx);
00065 }
00066
00067
00068
00069 TQpDataSparse::TQpDataSparse(TVectorD &c_in, TMatrixDSparse &Q_in,
00070 TVectorD &xlow_in,TVectorD &ixlow_in,
00071 TVectorD &xupp_in,TVectorD &ixupp_in,
00072 TMatrixDSparse &A_in, TVectorD &bA_in,
00073 TMatrixDSparse &C_in,
00074 TVectorD &clow_in,TVectorD &iclow_in,
00075 TVectorD &cupp_in,TVectorD &icupp_in)
00076 {
00077
00078
00079 fG .ResizeTo(c_in) ; fG = c_in;
00080 fBa .ResizeTo(bA_in) ; fBa = bA_in;
00081 fXloBound.ResizeTo(xlow_in) ; fXloBound = xlow_in;
00082 fXloIndex.ResizeTo(ixlow_in); fXloIndex = ixlow_in;
00083 fXupBound.ResizeTo(xupp_in) ; fXupBound = xupp_in;
00084 fXupIndex.ResizeTo(ixupp_in); fXupIndex = ixupp_in;
00085 fCloBound.ResizeTo(clow_in) ; fCloBound = clow_in;
00086 fCloIndex.ResizeTo(iclow_in); fCloIndex = iclow_in;
00087 fCupBound.ResizeTo(cupp_in) ; fCupBound = cupp_in;
00088 fCupIndex.ResizeTo(icupp_in); fCupIndex = icupp_in;
00089
00090 fNx = fG.GetNrows();
00091 fQ.Use(Q_in);
00092
00093 if (A_in.GetNrows() > 0) {
00094 fA.Use(A_in);
00095 fMy = fA.GetNrows();
00096 } else
00097 fMy = 0;
00098
00099 if (C_in.GetNrows()) {
00100 fC.Use(C_in);
00101 fMz = fC.GetNrows();
00102 } else
00103 fMz = 0;
00104 fQ.Print();
00105 fA.Print();
00106 fC.Print();
00107 printf("fNx: %d\n",fNx);
00108 printf("fMy: %d\n",fMy);
00109 printf("fMz: %d\n",fMz);
00110 }
00111
00112
00113
00114 TQpDataSparse::TQpDataSparse(const TQpDataSparse &another) : TQpDataBase(another)
00115 {
00116
00117
00118 *this = another;
00119 }
00120
00121
00122
00123 void TQpDataSparse::SetNonZeros(Int_t nnzQ,Int_t nnzA,Int_t nnzC)
00124 {
00125
00126
00127 fQ.SetSparseIndex(nnzQ);
00128 fA.SetSparseIndex(nnzA);
00129 fC.SetSparseIndex(nnzC);
00130 }
00131
00132
00133
00134 void TQpDataSparse::Qmult(Double_t beta,TVectorD &y,Double_t alpha,const TVectorD &x )
00135 {
00136
00137
00138 y *= beta;
00139 if (fQ.GetNoElements() > 0)
00140 y += alpha*(fQ*x);
00141 }
00142
00143
00144
00145 void TQpDataSparse::Amult(Double_t beta,TVectorD &y,Double_t alpha,const TVectorD &x)
00146 {
00147
00148
00149 y *= beta;
00150 if (fA.GetNoElements() > 0)
00151 y += alpha*(fA*x);
00152 }
00153
00154
00155
00156 void TQpDataSparse::Cmult(Double_t beta,TVectorD &y,Double_t alpha,const TVectorD &x)
00157 {
00158
00159
00160 y *= beta;
00161 if (fC.GetNoElements() > 0)
00162 y += alpha*(fC*x);
00163 }
00164
00165
00166
00167 void TQpDataSparse::ATransmult(Double_t beta,TVectorD &y,Double_t alpha,const TVectorD &x)
00168 {
00169
00170
00171 y *= beta;
00172 if (fA.GetNoElements() > 0)
00173 y += alpha*(TMatrixDSparse(TMatrixDSparse::kTransposed,fA)*x);
00174 }
00175
00176
00177
00178 void TQpDataSparse::CTransmult(Double_t beta,TVectorD &y,Double_t alpha,const TVectorD &x)
00179 {
00180
00181
00182 y *= beta;
00183 if (fC.GetNoElements() > 0)
00184 y += alpha*(TMatrixDSparse(TMatrixDSparse::kTransposed,fC)*x);
00185 }
00186
00187
00188
00189 Double_t TQpDataSparse::DataNorm()
00190 {
00191
00192
00193 Double_t norm = 0.0;
00194
00195 Double_t componentNorm = fG.NormInf();
00196 if (componentNorm > norm) norm = componentNorm;
00197
00198 TMatrixDSparse fQ_abs(fQ);
00199 componentNorm = (fQ_abs.Abs()).Max();
00200 if (componentNorm > norm) norm = componentNorm;
00201
00202 componentNorm = fBa.NormInf();
00203 if (componentNorm > norm) norm = componentNorm;
00204
00205 TMatrixDSparse fA_abs(fQ);
00206 componentNorm = (fA_abs.Abs()).Max();
00207 if (componentNorm > norm) norm = componentNorm;
00208
00209 TMatrixDSparse fC_abs(fQ);
00210 componentNorm = (fC_abs.Abs()).Max();
00211 if (componentNorm > norm) norm = componentNorm;
00212
00213 R__ASSERT(fXloBound.MatchesNonZeroPattern(fXloIndex));
00214 componentNorm = fXloBound.NormInf();
00215 if (componentNorm > norm) norm = componentNorm;
00216
00217 R__ASSERT(fXupBound.MatchesNonZeroPattern(fXupIndex));
00218 componentNorm = fXupBound.NormInf();
00219 if (componentNorm > norm) norm = componentNorm;
00220
00221 R__ASSERT(fCloBound.MatchesNonZeroPattern(fCloIndex));
00222 componentNorm = fCloBound.NormInf();
00223 if (componentNorm > norm) norm = componentNorm;
00224
00225 R__ASSERT(fCupBound.MatchesNonZeroPattern(fCupIndex));
00226 componentNorm = fCupBound.NormInf();
00227 if (componentNorm > norm) norm = componentNorm;
00228
00229 return norm;
00230 }
00231
00232
00233
00234 void TQpDataSparse::Print(Option_t * ) const
00235 {
00236
00237
00238 fQ.Print("Q");
00239 fG.Print("c");
00240
00241 fXloBound.Print("xlow");
00242 fXloIndex.Print("ixlow");
00243
00244 fXupBound.Print("xupp");
00245 fXupIndex.Print("ixupp");
00246
00247 fA.Print("A");
00248 fBa.Print("b");
00249 fC.Print("C");
00250
00251 fCloBound.Print("clow");
00252 fCloIndex.Print("iclow");
00253
00254 fCupBound.Print("cupp");
00255 fCupIndex.Print("icupp");
00256 }
00257
00258
00259
00260 void TQpDataSparse::PutQIntoAt(TMatrixDBase &m,Int_t row,Int_t col)
00261 {
00262
00263
00264
00265 m.SetSub(row,col,fQ);
00266 }
00267
00268
00269
00270 void TQpDataSparse::PutAIntoAt(TMatrixDBase &m,Int_t row,Int_t col)
00271 {
00272
00273
00274
00275 m.SetSub(row,col,fA);
00276 }
00277
00278
00279
00280 void TQpDataSparse::PutCIntoAt(TMatrixDBase &m,Int_t row,Int_t col)
00281 {
00282
00283
00284
00285 m.SetSub(row,col,fC);
00286 }
00287
00288
00289
00290 void TQpDataSparse::GetDiagonalOfQ(TVectorD &dq)
00291 {
00292
00293
00294 const Int_t n = TMath::Min(fQ.GetNrows(),fQ.GetNcols());
00295 dq.ResizeTo(n);
00296 dq = TMatrixDSparseDiag(fQ);
00297 }
00298
00299
00300
00301 Double_t TQpDataSparse::ObjectiveValue(TQpVar *vars)
00302 {
00303
00304
00305 TVectorD tmp(fG);
00306 this->Qmult(1.0,tmp,0.5,vars->fX);
00307
00308 return tmp*vars->fX;
00309 }
00310
00311
00312
00313 void TQpDataSparse::DataRandom(TVectorD &x,TVectorD &y,TVectorD &z,TVectorD &s)
00314 {
00315
00316
00317 Double_t ix = 3074.20374;
00318
00319 TVectorD xdual(fNx);
00320 this->RandomlyChooseBoundedVariables(x,xdual,fXloBound,fXloIndex,fXupBound,fXupIndex,ix,.25,.25,.25);
00321
00322 TVectorD sprime(fMz);
00323 this->RandomlyChooseBoundedVariables(sprime,z,fCloBound,fCloIndex,fCupBound,fCupIndex,ix,.25,.25,.5);
00324
00325 fQ.RandomizePD(0.0,1.0,ix);
00326 fA.Randomize(-10.0,10.0,ix);
00327 fC.Randomize(-10.0,10.0,ix);
00328 y .Randomize(-10.0,10.0,ix);
00329
00330
00331
00332 fG = xdual;
00333 fG -= fQ*x;
00334
00335 fG += TMatrixDSparse(TMatrixDSparse::kTransposed,fA)*y;
00336 fG += TMatrixDSparse(TMatrixDSparse::kTransposed,fC)*z;
00337
00338 fBa = fA*x;
00339 s = fC*x;
00340
00341
00342 const TVectorD q = s-sprime;
00343
00344
00345 Add(fCloBound,1.0,q);
00346 Add(fCupBound,1.0,q);
00347
00348 fCloBound.SelectNonZeros(fCloIndex);
00349 fCupBound.SelectNonZeros(fCupIndex);
00350 }
00351
00352
00353
00354 TQpDataSparse &TQpDataSparse::operator=(const TQpDataSparse &source)
00355 {
00356
00357
00358 if (this != &source) {
00359 TQpDataBase::operator=(source);
00360 fQ.ResizeTo(source.fQ); fQ = source.fQ;
00361 fA.ResizeTo(source.fA); fA = source.fA;
00362 fC.ResizeTo(source.fC); fC = source.fC;
00363 }
00364 return *this;
00365 }