Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

ClpLinearObjective.cpp

00001 // Copyright (C) 2003, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #include "CoinPragma.hpp"
00005 #include "ClpModel.hpp"
00006 #include "ClpLinearObjective.hpp"
00007 
00008 //#############################################################################
00009 // Constructors / Destructor / Assignment
00010 //#############################################################################
00011 
00012 //-------------------------------------------------------------------
00013 // Default Constructor 
00014 //-------------------------------------------------------------------
00015 ClpLinearObjective::ClpLinearObjective () 
00016 : ClpObjective()
00017 {
00018   type_=1;
00019   objective_=NULL;
00020   numberColumns_=0;
00021 }
00022 
00023 //-------------------------------------------------------------------
00024 // Useful Constructor 
00025 //-------------------------------------------------------------------
00026 ClpLinearObjective::ClpLinearObjective (const double * objective , 
00027                                         int numberColumns) 
00028   : ClpObjective()
00029 {
00030   type_=1;
00031   numberColumns_=numberColumns;
00032   if (objective) {
00033     objective_ = new double [numberColumns_];
00034     memcpy(objective_,objective,numberColumns_*sizeof(double));
00035   } else {
00036     objective_ = new double [numberColumns_];
00037     memset(objective_,0,numberColumns_*sizeof(double));
00038   }
00039 }
00040 
00041 //-------------------------------------------------------------------
00042 // Copy constructor 
00043 //-------------------------------------------------------------------
00044 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective & rhs) 
00045 : ClpObjective(rhs)
00046 {  
00047   numberColumns_=rhs.numberColumns_;
00048   if (rhs.objective_) {
00049     objective_ = new double [numberColumns_];
00050     memcpy(objective_,rhs.objective_,numberColumns_*sizeof(double));
00051   } else {
00052     objective_=NULL;
00053   }
00054 }
00055 /* Subset constructor.  Duplicates are allowed
00056    and order is as given.
00057 */
00058 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective &rhs,
00059                                         int numberColumns, 
00060                                         const int * whichColumn) 
00061 : ClpObjective(rhs)
00062 {
00063   objective_=NULL;
00064   numberColumns_=0;
00065   if (numberColumns>0) {
00066     // check valid lists
00067     int numberBad=0;
00068     int i;
00069     for (i=0;i<numberColumns;i++)
00070       if (whichColumn[i]<0||whichColumn[i]>=rhs.numberColumns_)
00071         numberBad++;
00072     if (numberBad)
00073       throw CoinError("bad column list", "subset constructor", 
00074                       "ClpLinearObjective");
00075     numberColumns_ = numberColumns;
00076     objective_ = new double[numberColumns_];
00077     for (i=0;i<numberColumns_;i++) 
00078       objective_[i]=rhs.objective_[whichColumn[i]];
00079   }
00080 }
00081   
00082 
00083 //-------------------------------------------------------------------
00084 // Destructor 
00085 //-------------------------------------------------------------------
00086 ClpLinearObjective::~ClpLinearObjective ()
00087 {
00088   delete [] objective_;
00089 }
00090 
00091 //----------------------------------------------------------------
00092 // Assignment operator 
00093 //-------------------------------------------------------------------
00094 ClpLinearObjective &
00095 ClpLinearObjective::operator=(const ClpLinearObjective& rhs)
00096 {
00097   if (this != &rhs) {
00098     ClpObjective::operator=(rhs);
00099     numberColumns_=rhs.numberColumns_;
00100     if (rhs.objective_) {
00101       objective_ = new double [numberColumns_];
00102       memcpy(objective_,rhs.objective_,numberColumns_*sizeof(double));
00103     } else {
00104       objective_=NULL;
00105     }
00106   }
00107   return *this;
00108 }
00109 
00110 // Returns gradient
00111 double *  
00112 ClpLinearObjective::gradient(const double * solution, double & offset,bool refresh)
00113 {
00114   offset=0.0;
00115   return objective_;
00116 }
00117   
00118 //-------------------------------------------------------------------
00119 // Clone
00120 //-------------------------------------------------------------------
00121 ClpObjective * ClpLinearObjective::clone() const
00122 {
00123   return new ClpLinearObjective(*this);
00124 }
00125 /* Subset clone.  Duplicates are allowed
00126    and order is as given.
00127 */
00128 ClpObjective * 
00129 ClpLinearObjective::subsetClone (int numberColumns, 
00130                            const int * whichColumns) const
00131 {
00132   return new ClpLinearObjective(*this, numberColumns, whichColumns);
00133 }
00134 // Resize objective
00135 void 
00136 ClpLinearObjective::resize(int newNumberColumns)
00137 {
00138   if (numberColumns_!=newNumberColumns) {
00139     int i;
00140     double * newArray = new double[newNumberColumns];
00141     if (objective_)
00142       memcpy(newArray,objective_,
00143              min(newNumberColumns,numberColumns_)*sizeof(double));
00144     delete [] objective_;
00145     objective_ = newArray;
00146     for (i=numberColumns_;i<newNumberColumns;i++) 
00147       objective_[i]=0.0;
00148     numberColumns_ = newNumberColumns;
00149   } 
00150   
00151 }
00152 // Delete columns in  objective
00153 void 
00154 ClpLinearObjective::deleteSome(int numberToDelete, const int * which) 
00155 {
00156   if (objective_) {
00157     int i ;
00158     char * deleted = new char[numberColumns_];
00159     int numberDeleted=0;
00160     memset(deleted,0,numberColumns_*sizeof(char));
00161     for (i=0;i<numberToDelete;i++) {
00162       int j = which[i];
00163       if (j>=0&&j<numberColumns_&&!deleted[j]) {
00164         numberDeleted++;
00165         deleted[j]=1;
00166       }
00167     }
00168     int newNumberColumns = numberColumns_-numberDeleted;
00169     double * newArray = new double[newNumberColumns];
00170     int put=0;
00171     for (i=0;i<numberColumns_;i++) {
00172       if (!deleted[i]) {
00173         newArray[put++]=objective_[i];
00174       }
00175     }
00176     delete [] objective_;
00177     objective_ = newArray;
00178     delete [] deleted;
00179     numberColumns_ = newNumberColumns;
00180   }
00181 }
00182 

Generated on Wed Dec 3 14:37:26 2003 for CLP by doxygen 1.3.5