1 // $Id$
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others.  All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #include <cstdio>
7 
8 #ifdef NDEBUG
9 #undef NDEBUG
10 #endif
11 #include <cassert>
12 
13 #include "CoinPragma.hpp"
14 #include "CoinPackedMatrix.hpp"
15 #include "CglProbing.hpp"
16 
17 
18 //--------------------------------------------------------------------------
19 // test EKKsolution methods.
20 void
CglProbingUnitTest(const OsiSolverInterface * baseSiP,const std::string mpsDir)21 CglProbingUnitTest(
22   const OsiSolverInterface * baseSiP,
23   const std::string mpsDir )
24 {
25 # ifdef CGL_DEBUG
26   int i ;	// define just once
27 # endif
28   CoinRelFltEq eq(0.000001);
29 
30   // Test default constructor
31   {
32     CglProbing aGenerator;
33   }
34 
35   // Test copy & assignment
36   {
37     CglProbing rhs;
38     {
39       CglProbing bGenerator;
40       CglProbing cGenerator(bGenerator);
41       rhs=bGenerator;
42     }
43   }
44 
45   {
46     OsiCuts osicuts;
47     CglProbing test1;
48     OsiSolverInterface  * siP = baseSiP->clone();
49     int nColCuts;
50     int nRowCuts;
51 
52     std::string fn = mpsDir+"p0033";
53     siP->readMps(fn.c_str(),"mps");
54     siP->initialSolve();
55     // just unsatisfied variables
56     test1.generateCuts(*siP,osicuts);
57     nColCuts = osicuts.sizeColCuts();
58     nRowCuts = osicuts.sizeRowCuts();
59     std::cout<<"There are "<<nRowCuts<<" probing cuts"<<std::endl;
60     {
61       std::cout<<"there are "<<nColCuts<<" probing column cuts"<<std::endl;
62 
63 #ifdef CGL_DEBUG
64       const double * lo = siP->getColLower();
65       const double * up = siP->getColUpper();
66       for (i=0; i<nColCuts; i++){
67 	OsiColCut ccut;
68 	CoinPackedVector cpv;
69 	ccut = osicuts.colCut(i);
70 	cpv = ccut.lbs();
71 	int n = cpv.getNumElements();
72         int j;
73 	const int * indices = cpv.getIndices();
74 	double* elements = cpv.getElements();
75 	for (j=0;j<n;j++) {
76 	  int icol=indices[j];
77 	  if (elements[j]>lo[icol])
78 	    std::cout<<"Can increase lb on "<<icol<<" from "<<lo[icol]<<
79 	      " to "<<elements[j]<<std::endl;
80 	}
81 	cpv = ccut.ubs();
82 	n = cpv.getNumElements();
83 	indices = cpv.getIndices();
84 	elements = cpv.getElements();
85 
86 	for (j=0;j<n;j++) {
87 	  int icol=indices[j];
88 	  if (elements[j]<up[icol])
89 	    std::cout<<"Can decrease ub on "<<icol<<" from "<<up[icol]<<
90 	      " to "<<elements[j]<<std::endl;
91 	}
92       }
93 
94 #endif
95 
96     }
97 
98 #ifdef CGL_DEBUG
99     for (i=0; i<nRowCuts; i++){
100       OsiRowCut rcut;
101       CoinPackedVector rpv;
102       const double * colsol = siP->getColSolution();
103       rcut = osicuts.rowCut(i);
104       rpv = rcut.row();
105       const int n = rpv.getNumElements();
106       const int * indices = rpv.getIndices();
107       double* elements = rpv.getElements();
108       double sum2=0.0;
109       int k=0;
110       double lb=rcut.lb();
111       double ub=rcut.ub();
112       for (k=0; k<n; k++){
113 	int column=indices[k];
114 	sum2 += colsol[column]*elements[k];
115       }
116       if (sum2 >ub + 1.0e-7 ||sum2 < lb - 1.0e-7) {
117 	std::cout<<"Cut "<<i<<" lb "<<lb<<" solution "<<sum2<<" ub "<<ub<<std::endl;
118 	for (k=0; k<n; k++){
119 	  int column=indices[k];
120 	  std::cout<<"(col="<<column<<",el="<<elements[k]<<",sol="<<
121 	    colsol[column]<<") ";
122 	}
123 	std::cout <<std::endl;
124       }
125     }
126 #endif
127 
128     if (nRowCuts==1) {
129       CoinPackedVector check;
130       int index[] = {6,32};
131       double el[] = {1,1};
132       check.setVector(2,index,el);
133       // sort Elements in increasing order
134       CoinPackedVector rpv=osicuts.rowCut(0).row();
135       assert (rpv.getNumElements()==2);
136       rpv.sortIncrIndex();
137       assert (check==rpv);
138       assert (osicuts.rowCut(0).lb()==1.0);
139     }
140     // now all variables
141     osicuts=OsiCuts();
142     test1.setMode(2);
143     test1.setRowCuts(3);
144     test1.generateCuts(*siP,osicuts);
145     nColCuts = osicuts.sizeColCuts();
146     nRowCuts = osicuts.sizeRowCuts();
147     std::cout<<"There are "<<nRowCuts<<" probing cuts"<<std::endl;
148     {
149       std::cout<<"there are "<<nColCuts<<" probing column cuts"<<std::endl;
150 
151 #ifdef CGL_DEBUG
152       const double * lo = siP->getColLower();
153       const double * up = siP->getColUpper();
154       for (i=0; i<nColCuts; i++){
155 	OsiColCut ccut;
156 	CoinPackedVector cpv;
157 	ccut = osicuts.colCut(i);
158 	cpv = ccut.lbs();
159 	int n = cpv.getNumElements();
160         int j;
161 	const int * indices = cpv.getIndices();
162 	double* elements = cpv.getElements();
163 	for (j=0;j<n;j++) {
164 	  int icol=indices[j];
165 	  if (elements[j]>lo[icol])
166 	    std::cout<<"Can increase lb on "<<icol<<" from "<<lo[icol]<<
167 	      " to "<<elements[j]<<std::endl;
168 	}
169 	cpv = ccut.ubs();
170 	n = cpv.getNumElements();
171 	indices = cpv.getIndices();
172 	elements = cpv.getElements();
173 	for (j=0;j<n;j++) {
174 	  int icol=indices[j];
175 	  if (elements[j]<up[icol])
176 	    std::cout<<"Can decrease ub on "<<icol<<" from "<<up[icol]<<
177 	      " to "<<elements[j]<<std::endl;
178 	}
179       }
180 #endif
181 
182     }
183 
184 #ifdef CGL_DEBUG
185     for (i=0; i<nRowCuts; i++){
186       OsiRowCut rcut;
187       CoinPackedVector rpv;
188       const double * colsol = siP->getColSolution();
189       rcut = osicuts.rowCut(i);
190       rpv = rcut.row();
191       const int n = rpv.getNumElements();
192       const int * indices = rpv.getIndices();
193       double* elements = rpv.getElements();
194       double sum2=0.0;
195       int k=0;
196       double lb=rcut.lb();
197       double ub=rcut.ub();
198       for (k=0; k<n; k++){
199 	int column=indices[k];
200 	sum2 += colsol[column]*elements[k];
201       }
202       if (sum2 >ub + 1.0e-7 ||sum2 < lb - 1.0e-7) {
203 	std::cout<<"Cut "<<i<<" lb "<<lb<<" solution "<<sum2<<" ub "<<ub<<std::endl;
204 	for (k=0; k<n; k++){
205 	  int column=indices[k];
206 	  std::cout<<"(col="<<column<<",el="<<elements[k]<<",sol="<<
207 	    colsol[column]<<") ";
208 	}
209 	std::cout <<std::endl;
210       }
211     }
212 #endif
213 
214     assert (osicuts.sizeRowCuts()>=4);
215     delete siP;
216   }
217 
218 }
219 
220