TQpDataSparse.cxx

Go to the documentation of this file.
00001 // @(#)root/quadp:$Id: TQpDataSparse.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Eddy Offermann   May 2004
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 /*************************************************************************
00013  * Parts of this file are copied from the OOQP distribution and          *
00014  * are subject to the following license:                                 *
00015  *                                                                       *
00016  * COPYRIGHT 2001 UNIVERSITY OF CHICAGO                                  *
00017  *                                                                       *
00018  * The copyright holder hereby grants you royalty-free rights to use,    *
00019  * reproduce, prepare derivative works, and to redistribute this software*
00020  * to others, provided that any changes are clearly documented. This     *
00021  * software was authored by:                                             *
00022  *                                                                       *
00023  *   E. MICHAEL GERTZ      gertz@mcs.anl.gov                             *
00024  *   Mathematics and Computer Science Division                           *
00025  *   Argonne National Laboratory                                         *
00026  *   9700 S. Cass Avenue                                                 *
00027  *   Argonne, IL 60439-4844                                              *
00028  *                                                                       *
00029  *   STEPHEN J. WRIGHT     swright@cs.wisc.edu                           *
00030  *   Computer Sciences Department                                        *
00031  *   University of Wisconsin                                             *
00032  *   1210 West Dayton Street                                             *
00033  *   Madison, WI 53706   FAX: (608)262-9777                              *
00034  *                                                                       *
00035  * Any questions or comments may be directed to one of the authors.      *
00036  *                                                                       *
00037  * ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES OF   *
00038  * ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT, AND     *
00039  * OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A CONTRACT   *
00040  * WITH THE DEPARTMENT OF ENERGY.                                        *
00041  *************************************************************************/
00042 
00043 #include "Riostream.h"
00044 #include "TQpDataSparse.h"
00045 
00046 //////////////////////////////////////////////////////////////////////////
00047 //                                                                      //
00048 // TQpDataSparse                                                        //
00049 //                                                                      //
00050 // Data for the sparse QP formulation                                   //
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 // Constructor
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 // Constructor
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 // Copy constructor
00117 
00118    *this = another;
00119 }
00120 
00121 
00122 //______________________________________________________________________________
00123 void TQpDataSparse::SetNonZeros(Int_t nnzQ,Int_t nnzA,Int_t nnzC)
00124 {
00125 // Allocate space for the appropriate number of non-zeros in the matrices
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 // calculate y = beta*y + alpha*(fQ*x)
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 // calculate y = beta*y + alpha*(fA*x)
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 // calculate y = beta*y + alpha*(fC*x)
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 // calculate y = beta*y + alpha*(fA^T*x)
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 // calculate y = beta*y + alpha*(fC^T*x)
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 // Return the largest component of several vectors in the data class
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 * /*opt*/) const
00235 {
00236 // Print class members
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 // Insert the Hessian Q into the matrix M at index (row,col) for the fundamental
00263 // linear system
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 // Insert the constraint matrix A into the matrix M at index (row,col) for the fundamental
00273 // linear system
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 // Insert the constraint matrix C into the matrix M at index (row,col) for the fundamental
00283 // linear system
00284 
00285    m.SetSub(row,col,fC);
00286 }
00287 
00288 
00289 //______________________________________________________________________________
00290 void TQpDataSparse::GetDiagonalOfQ(TVectorD &dq)
00291 {
00292 // Return in vector dq the diagonal of matrix fQ
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 // Return value of the objective function
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 // Choose randomly a QP problem
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    // fG = - fQ x + A^T y + C^T z + xdual
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    // Now compute the real q = s-sprime
00342    const TVectorD q = s-sprime;
00343 
00344    // Adjust fCloBound and fCupBound appropriately
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 // Assignment operator
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 }

Generated on Tue Jul 5 14:37:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1