source: trunk/Cbc/src/CbcFixVariable.cpp @ 1433

Last change on this file since 1433 was 1432, checked in by bjarni, 10 years ago

Added extra return at end of each source file where needed, to remove possible linefeed conflicts (NightlyBuild? errors)

File size: 5.7 KB
Line 
1// Edwin 11/10/2009-- carved out of CbcBranchActual
2#if defined(_MSC_VER)
3// Turn off compiler warning about long names
4#  pragma warning(disable:4786)
5#endif
6#include <cassert>
7#include <cstdlib>
8#include <cmath>
9#include <cfloat>
10//#define CBC_DEBUG
11
12#include "CoinTypes.hpp"
13#include "OsiSolverInterface.hpp"
14#include "OsiSolverBranch.hpp"
15#include "CbcModel.hpp"
16#include "CbcMessage.hpp"
17#include "CbcFixVariable.hpp"
18#include "CbcBranchActual.hpp"
19#include "CoinSort.hpp"
20#include "CoinError.hpp"
21
22
23//##############################################################################
24
25// Default Constructor
26CbcFixVariable::CbcFixVariable ()
27        : CbcConsequence(),
28        numberStates_(0),
29        states_(NULL),
30        startLower_(NULL),
31        startUpper_(NULL),
32        newBound_(NULL),
33        variable_(NULL)
34{
35}
36
37// One useful Constructor
38CbcFixVariable::CbcFixVariable (int numberStates, const int * states, const int * numberNewLower,
39                                const int ** newLowerValue,
40                                const int ** lowerColumn,
41                                const int * numberNewUpper, const int ** newUpperValue,
42                                const int ** upperColumn)
43        : CbcConsequence(),
44        states_(NULL),
45        startLower_(NULL),
46        startUpper_(NULL),
47        newBound_(NULL),
48        variable_(NULL)
49{
50    // How much space
51    numberStates_ = numberStates;
52    if (numberStates_) {
53        states_ = new int[numberStates_];
54        memcpy(states_, states, numberStates_*sizeof(int));
55        int i;
56        int n = 0;
57        startLower_ = new int[numberStates_+1];
58        startUpper_ = new int[numberStates_+1];
59        startLower_[0] = 0;
60        //count
61        for (i = 0; i < numberStates_; i++) {
62            n += numberNewLower[i];
63            startUpper_[i] = n;
64            n += numberNewUpper[i];
65            startLower_[i+1] = n;
66        }
67        newBound_ = new double [n];
68        variable_ = new int [n];
69        n = 0;
70        for (i = 0; i < numberStates_; i++) {
71            int j;
72            int k;
73            const int * bound;
74            const int * variable;
75            k = numberNewLower[i];
76            bound = newLowerValue[i];
77            variable = lowerColumn[i];
78            for (j = 0; j < k; j++) {
79                newBound_[n] = bound[j];
80                variable_[n++] = variable[j];
81            }
82            k = numberNewUpper[i];
83            bound = newUpperValue[i];
84            variable = upperColumn[i];
85            for (j = 0; j < k; j++) {
86                newBound_[n] = bound[j];
87                variable_[n++] = variable[j];
88            }
89        }
90    }
91}
92
93// Copy constructor
94CbcFixVariable::CbcFixVariable ( const CbcFixVariable & rhs)
95        : CbcConsequence(rhs)
96{
97    numberStates_ = rhs.numberStates_;
98    states_ = NULL;
99    startLower_ = NULL;
100    startUpper_ = NULL;
101    newBound_ = NULL;
102    variable_ = NULL;
103    if (numberStates_) {
104        states_ = CoinCopyOfArray(rhs.states_, numberStates_);
105        startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1);
106        startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1);
107        int n = startLower_[numberStates_];
108        newBound_ = CoinCopyOfArray(rhs.newBound_, n);
109        variable_ = CoinCopyOfArray(rhs.variable_, n);
110    }
111}
112
113// Clone
114CbcConsequence *
115CbcFixVariable::clone() const
116{
117    return new CbcFixVariable(*this);
118}
119
120// Assignment operator
121CbcFixVariable &
122CbcFixVariable::operator=( const CbcFixVariable & rhs)
123{
124    if (this != &rhs) {
125        CbcConsequence::operator=(rhs);
126        delete [] states_;
127        delete [] startLower_;
128        delete [] startUpper_;
129        delete [] newBound_;
130        delete [] variable_;
131        states_ = NULL;
132        startLower_ = NULL;
133        startUpper_ = NULL;
134        newBound_ = NULL;
135        variable_ = NULL;
136        numberStates_ = rhs.numberStates_;
137        if (numberStates_) {
138            states_ = CoinCopyOfArray(rhs.states_, numberStates_);
139            startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1);
140            startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1);
141            int n = startLower_[numberStates_];
142            newBound_ = CoinCopyOfArray(rhs.newBound_, n);
143            variable_ = CoinCopyOfArray(rhs.variable_, n);
144        }
145    }
146    return *this;
147}
148
149// Destructor
150CbcFixVariable::~CbcFixVariable ()
151{
152    delete [] states_;
153    delete [] startLower_;
154    delete [] startUpper_;
155    delete [] newBound_;
156    delete [] variable_;
157}
158// Set up a startLower for a single member
159void
160CbcFixVariable::applyToSolver(OsiSolverInterface * solver, int state) const
161{
162    assert (state == -9999 || state == 9999);
163    // Find state
164    int find;
165    for (find = 0; find < numberStates_; find++)
166        if (states_[find] == state)
167            break;
168    if (find == numberStates_)
169        return;
170    int i;
171    // Set new lower bounds
172    for (i = startLower_[find]; i < startUpper_[find]; i++) {
173        int iColumn = variable_[i];
174        double value = newBound_[i];
175        double oldValue = solver->getColLower()[iColumn];
176        //printf("for %d old lower bound %g, new %g",iColumn,oldValue,value);
177        solver->setColLower(iColumn, CoinMax(value, oldValue));
178        //printf(" => %g\n",solver->getColLower()[iColumn]);
179    }
180    // Set new upper bounds
181    for (i = startUpper_[find]; i < startLower_[find+1]; i++) {
182        int iColumn = variable_[i];
183        double value = newBound_[i];
184        double oldValue = solver->getColUpper()[iColumn];
185        //printf("for %d old upper bound %g, new %g",iColumn,oldValue,value);
186        solver->setColUpper(iColumn, CoinMin(value, oldValue));
187        //printf(" => %g\n",solver->getColUpper()[iColumn]);
188    }
189}
190
Note: See TracBrowser for help on using the repository browser.