source: branches/devel/Cbc/examples/CoinWarmStartBasisDynamic.cpp @ 529

Last change on this file since 529 was 403, checked in by forrest, 14 years ago

adding some examples

File size: 8.5 KB
Line 
1// Copyright (C) 2006, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <cassert>
9
10#include "CoinWarmStartBasisDynamic.hpp"
11#include <cmath>
12#include <iostream>
13
14CoinWarmStartBasisDynamic::CoinWarmStartBasisDynamic()
15  : CoinWarmStartBasis(),
16    numberCommonVariables_(0),
17    numberCommonRows_(0),
18    numberDynamicVariables_(0),
19    dynamicVariables_(NULL)
20{
21}
22CoinWarmStartBasisDynamic::CoinWarmStartBasisDynamic(int ns, int na, 
23                                                     const char* sStat, const char* aStat,
24                                                     int numberCommon, int numberDynamicVariables,
25                                                     const int * dynamicVariables) 
26  : CoinWarmStartBasis(ns, na, sStat, aStat),
27    numberCommonVariables_(numberCommon),
28    numberCommonRows_(0),
29    numberDynamicVariables_(numberDynamicVariables)
30{
31  dynamicVariables_ = CoinCopyOfArray(dynamicVariables,numberDynamicVariables);
32}
33
34CoinWarmStartBasisDynamic::CoinWarmStartBasisDynamic(const CoinWarmStartBasisDynamic& rhs) 
35  : CoinWarmStartBasis(rhs),
36    numberCommonVariables_(rhs.numberCommonVariables_),
37    numberCommonRows_(rhs.numberCommonRows_),
38    numberDynamicVariables_(rhs.numberDynamicVariables_)
39{
40  dynamicVariables_ = CoinCopyOfArray(rhs.dynamicVariables_,numberDynamicVariables_);
41}
42
43CoinWarmStartBasisDynamic& 
44CoinWarmStartBasisDynamic::operator=(const CoinWarmStartBasisDynamic& rhs)
45{
46  if (this != &rhs) {
47    CoinWarmStartBasis::operator=(rhs);
48    delete [] dynamicVariables_;
49    numberCommonVariables_ = rhs.numberCommonVariables_;
50    numberCommonRows_ = rhs.numberCommonRows_;
51    numberDynamicVariables_ = rhs.numberDynamicVariables_;
52    dynamicVariables_ = CoinCopyOfArray(rhs.dynamicVariables_,numberDynamicVariables_);
53  }
54  return *this;
55}
56CoinWarmStartBasisDynamic::~CoinWarmStartBasisDynamic()
57{
58  delete[] dynamicVariables_;
59}
60/*  Save list of dynamic variables */
61
62void 
63CoinWarmStartBasisDynamic::setDynamicVariables(int numberDynamicVariables, const int * dynamicVariables)
64{
65  delete[] dynamicVariables_;
66  dynamicVariables_ = CoinCopyOfArray(dynamicVariables,numberDynamicVariables);
67  numberDynamicVariables_=numberDynamicVariables;
68}
69void 
70CoinWarmStartBasisDynamic::assignBasisStatus(int ns, int na, char*& sStat, 
71                                             char*& aStat,
72                                             int numberCommon, int numberDynamicVariables,
73                                             int *& dynamicVariables) 
74{
75  CoinWarmStartBasis::assignBasisStatus(ns, na, sStat, aStat);
76  delete[] dynamicVariables_;
77  dynamicVariables_ = dynamicVariables;
78  dynamicVariables=NULL;
79  numberCommonVariables_=numberCommon;
80  numberDynamicVariables_=numberDynamicVariables;
81  assert (numberCommonVariables_+numberDynamicVariables_<=ns);
82  numberCommonRows_=0;
83}
84// Deletes columns
85void 
86CoinWarmStartBasisDynamic::deleteColumns(int number, const int * which)
87{
88  int i ;
89  int numStructural = getNumStructural();
90  char * deleted = new char[numStructural];
91  int numberDeleted=0;
92  memset(deleted,0,numStructural*sizeof(char));
93  for (i=0;i<number;i++) {
94    int j = which[i];
95    if (j>=0&&j<numStructural&&!deleted[j]) {
96      numberDeleted++;
97      deleted[j]=1;
98    }
99  }
100  int put=0;
101  for (i=0;i<numberDynamicVariables_;i++) {
102    if (!deleted[i+numberCommonVariables_]) 
103      dynamicVariables_[put++]=dynamicVariables_[i];
104  }
105  delete [] deleted;
106  CoinWarmStartBasis::deleteColumns(number, which);
107}
108// Prints in readable format (for debug)
109void 
110CoinWarmStartBasisDynamic::print() const
111{
112  CoinWarmStartBasis::print();
113  std::cout<<"There are "<<numberCommonVariables_<<" permanent and "
114           <<numberDynamicVariables_<<" dynamic variables"<<std::endl;
115  int i;
116  bool first=true;
117  for (i=0;i<numberDynamicVariables_;i++) {
118    if ((i%5)==0) {
119      if (!first) 
120        std::cout<<std::endl;
121      else
122        first=false;
123      std::cout<<i;
124    }
125    std::cout<<" "<<dynamicVariables_[i];
126  }
127  std::cout<<std::endl;
128}
129
130CoinWarmStartBasisDiffDynamic &
131CoinWarmStartBasisDiffDynamic::operator= (const CoinWarmStartBasisDiffDynamic &rhs) 
132{
133  if (this != &rhs) {
134    CoinWarmStartBasisDiff::operator=(rhs);
135    delete [] dynamic_;
136    numberDynamic_ = rhs.numberDynamic_;
137    dynamic_ = CoinCopyOfArray(rhs.dynamic_,numberDynamic_);
138  }
139  return *this;
140}
141CoinWarmStartBasisDiffDynamic::CoinWarmStartBasisDiffDynamic (const CoinWarmStartBasisDiffDynamic &rhs) 
142  : CoinWarmStartBasisDiff(rhs)
143{
144  numberDynamic_ = rhs.numberDynamic_;
145  dynamic_ = CoinCopyOfArray(rhs.dynamic_,numberDynamic_);
146}
147CoinWarmStartBasisDiffDynamic::CoinWarmStartBasisDiffDynamic (int sze, const unsigned int *const diffNdxs,
148                                                              const unsigned int *const diffVals,
149                                                              int numberDynamic, const int * dynamic) 
150  : CoinWarmStartBasisDiff(sze, diffNdxs, diffVals)
151{
152  numberDynamic_ = numberDynamic;
153  dynamic_ = CoinCopyOfArray(dynamic,numberDynamic_);
154}
155
156/*
157  Generate a diff that'll convert oldCWS into the basis pointed to by this.
158
159  This routine is a bit of a hack, for efficiency's sake. Rather than work
160  with individual status vector entries, we're going to treat the vectors as
161  int's --- in effect, we create one diff entry for each block of 16 status
162  entries. Diffs for logicals are tagged with 0x80000000.
163*/
164
165CoinWarmStartDiff*
166CoinWarmStartBasisDynamic::generateDiff (const CoinWarmStart *const oldCWS) const
167{ 
168/*
169  Make sure the parameter is CoinWarmStartBasis or derived class.
170*/
171  const CoinWarmStartBasis *oldBasis =
172      dynamic_cast<const CoinWarmStartBasis *>(oldCWS) ;
173  if (!oldBasis)
174  { throw CoinError("Old basis not derived from CoinWarmStartBasis.",
175                    "generateDiff","CoinWarmStartBasis") ; }
176  const CoinWarmStartBasis *newBasis = this ;
177/*
178  Make sure newBasis is equal or bigger than oldBasis. Calculate the worst case
179  number of diffs and allocate vectors to hold them.
180*/
181  const int oldArtifCnt = oldBasis->getNumArtificial() ;
182  const int oldStructCnt = oldBasis->getNumStructural() ;
183  const int newArtifCnt = newBasis->getNumArtificial() ;
184  const int newStructCnt = newBasis->getNumStructural() ;
185
186  assert(newArtifCnt >= oldArtifCnt) ;
187  assert(newStructCnt >= oldStructCnt) ;
188
189  int sizeOldArtif = (oldArtifCnt+15)>>4 ;
190  int sizeNewArtif = (newArtifCnt+15)>>4 ;
191  int sizeOldStruct = (oldStructCnt+15)>>4 ;
192  int sizeNewStruct = (newStructCnt+15)>>4 ;
193  int maxBasisLength = sizeNewArtif+sizeNewStruct ;
194
195  unsigned int *diffNdx = new unsigned int [maxBasisLength]; 
196  unsigned int *diffVal = new unsigned int [maxBasisLength]; 
197/*
198  Ok, setup's over. Now scan the logicals (aka artificials, standing in for
199  constraints). For the portion of the status arrays which overlap, create
200  diffs. Then add any additional status from newBasis.
201
202  I removed the following bit of code & comment:
203
204    if (sizeNew == sizeOld) sizeOld--; // make sure all taken
205
206  I assume this is meant to trap cases where oldBasis does not occupy all of
207  the final int, but I can't see where it's necessary.
208*/
209  const unsigned int *oldStatus =
210      reinterpret_cast<const unsigned int *>(oldBasis->getArtificialStatus()) ;
211  const unsigned int *newStatus = 
212      reinterpret_cast<const unsigned int *>(newBasis->getArtificialStatus()) ;
213  int numberChanged = 0 ;
214  int i ;
215  for (i = 0 ; i < sizeOldArtif ; i++)
216  { if (oldStatus[i] != newStatus[i])
217    { diffNdx[numberChanged] = i|0x80000000 ;
218      diffVal[numberChanged++] = newStatus[i] ; } }
219  for ( ; i < sizeNewArtif ; i++)
220  { diffNdx[numberChanged] = i|0x80000000 ;
221    diffVal[numberChanged++] = newStatus[i] ; }
222/*
223  Repeat for structural variables.
224*/
225  oldStatus =
226      reinterpret_cast<const unsigned int *>(oldBasis->getStructuralStatus()) ;
227  newStatus =
228      reinterpret_cast<const unsigned int *>(newBasis->getStructuralStatus()) ;
229  for (i = 0 ; i < sizeOldStruct ; i++)
230  { if (oldStatus[i] != newStatus[i])
231    { diffNdx[numberChanged] = i ;
232      diffVal[numberChanged++] = newStatus[i] ; } }
233  for ( ; i < sizeNewStruct ; i++)
234  { diffNdx[numberChanged] = i ;
235    diffVal[numberChanged++] = newStatus[i] ; }
236/*
237  Create the object of our desire.
238*/
239  CoinWarmStartBasisDiffDynamic *diff =
240    new CoinWarmStartBasisDiffDynamic(numberChanged,diffNdx,diffVal,numberDynamicVariables_,
241                                      dynamicVariables_) ;
242/*
243  Clean up and return.
244*/
245  delete[] diffNdx ;
246  delete[] diffVal ;
247
248  return (dynamic_cast<CoinWarmStartDiff *>(diff)) ; }
249
250
Note: See TracBrowser for help on using the repository browser.