source: trunk/ClpLinearObjective.cpp @ 284

Last change on this file since 284 was 225, checked in by forrest, 16 years ago

This should break everything

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1// Copyright (C) 2003, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "CoinPragma.hpp"
5#include "ClpModel.hpp"
6#include "ClpLinearObjective.hpp"
7
8//#############################################################################
9// Constructors / Destructor / Assignment
10//#############################################################################
11
12//-------------------------------------------------------------------
13// Default Constructor
14//-------------------------------------------------------------------
15ClpLinearObjective::ClpLinearObjective () 
16: ClpObjective()
17{
18  type_=1;
19  objective_=NULL;
20  numberColumns_=0;
21}
22
23//-------------------------------------------------------------------
24// Useful Constructor
25//-------------------------------------------------------------------
26ClpLinearObjective::ClpLinearObjective (const double * objective , 
27                                        int numberColumns) 
28  : ClpObjective()
29{
30  type_=1;
31  numberColumns_=numberColumns;
32  if (objective) {
33    objective_ = new double [numberColumns_];
34    memcpy(objective_,objective,numberColumns_*sizeof(double));
35  } else {
36    objective_ = new double [numberColumns_];
37    memset(objective_,0,numberColumns_*sizeof(double));
38  }
39}
40
41//-------------------------------------------------------------------
42// Copy constructor
43//-------------------------------------------------------------------
44ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective & rhs) 
45: ClpObjective(rhs)
46{ 
47  numberColumns_=rhs.numberColumns_;
48  if (rhs.objective_) {
49    objective_ = new double [numberColumns_];
50    memcpy(objective_,rhs.objective_,numberColumns_*sizeof(double));
51  } else {
52    objective_=NULL;
53  }
54}
55/* Subset constructor.  Duplicates are allowed
56   and order is as given.
57*/
58ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective &rhs,
59                                        int numberColumns, 
60                                        const int * whichColumn) 
61: ClpObjective(rhs)
62{
63  objective_=NULL;
64  numberColumns_=0;
65  if (numberColumns>0) {
66    // check valid lists
67    int numberBad=0;
68    int i;
69    for (i=0;i<numberColumns;i++)
70      if (whichColumn[i]<0||whichColumn[i]>=rhs.numberColumns_)
71        numberBad++;
72    if (numberBad)
73      throw CoinError("bad column list", "subset constructor", 
74                      "ClpLinearObjective");
75    numberColumns_ = numberColumns;
76    objective_ = new double[numberColumns_];
77    for (i=0;i<numberColumns_;i++) 
78      objective_[i]=rhs.objective_[whichColumn[i]];
79  }
80}
81 
82
83//-------------------------------------------------------------------
84// Destructor
85//-------------------------------------------------------------------
86ClpLinearObjective::~ClpLinearObjective ()
87{
88  delete [] objective_;
89}
90
91//----------------------------------------------------------------
92// Assignment operator
93//-------------------------------------------------------------------
94ClpLinearObjective &
95ClpLinearObjective::operator=(const ClpLinearObjective& rhs)
96{
97  if (this != &rhs) {
98    ClpObjective::operator=(rhs);
99    numberColumns_=rhs.numberColumns_;
100    if (rhs.objective_) {
101      objective_ = new double [numberColumns_];
102      memcpy(objective_,rhs.objective_,numberColumns_*sizeof(double));
103    } else {
104      objective_=NULL;
105    }
106  }
107  return *this;
108}
109
110// Returns gradient
111double * 
112ClpLinearObjective::gradient(const double * solution, double & offset,bool refresh)
113{
114  offset=0.0;
115  return objective_;
116}
117 
118//-------------------------------------------------------------------
119// Clone
120//-------------------------------------------------------------------
121ClpObjective * ClpLinearObjective::clone() const
122{
123  return new ClpLinearObjective(*this);
124}
125/* Subset clone.  Duplicates are allowed
126   and order is as given.
127*/
128ClpObjective * 
129ClpLinearObjective::subsetClone (int numberColumns, 
130                           const int * whichColumns) const
131{
132  return new ClpLinearObjective(*this, numberColumns, whichColumns);
133}
134// Resize objective
135void 
136ClpLinearObjective::resize(int newNumberColumns)
137{
138  if (numberColumns_!=newNumberColumns) {
139    int i;
140    double * newArray = new double[newNumberColumns];
141    if (objective_)
142      memcpy(newArray,objective_,
143             min(newNumberColumns,numberColumns_)*sizeof(double));
144    delete [] objective_;
145    objective_ = newArray;
146    for (i=numberColumns_;i<newNumberColumns;i++) 
147      objective_[i]=0.0;
148    numberColumns_ = newNumberColumns;
149  } 
150 
151}
152// Delete columns in  objective
153void 
154ClpLinearObjective::deleteSome(int numberToDelete, const int * which) 
155{
156  if (objective_) {
157    int i ;
158    char * deleted = new char[numberColumns_];
159    int numberDeleted=0;
160    memset(deleted,0,numberColumns_*sizeof(char));
161    for (i=0;i<numberToDelete;i++) {
162      int j = which[i];
163      if (j>=0&&j<numberColumns_&&!deleted[j]) {
164        numberDeleted++;
165        deleted[j]=1;
166      }
167    }
168    int newNumberColumns = numberColumns_-numberDeleted;
169    double * newArray = new double[newNumberColumns];
170    int put=0;
171    for (i=0;i<numberColumns_;i++) {
172      if (!deleted[i]) {
173        newArray[put++]=objective_[i];
174      }
175    }
176    delete [] objective_;
177    objective_ = newArray;
178    delete [] deleted;
179    numberColumns_ = newNumberColumns;
180  }
181}
182
Note: See TracBrowser for help on using the repository browser.