Debian Codegraph @

Osi/src/OsiCommonTest/OsiCutsTest.cpp

raw symbols SCIP
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // This code is licensed under the terms of the Eclipse Public License (EPL).
4
5 #include "CoinPragma.hpp"
6
7 #include "OsiUnitTests.hpp"
8
9 #include "OsiCuts.hpp"
10
11 //--------------------------------------------------------------------------
12 void OsiCutsUnitTest()
13 {
14 CoinRelFltEq eq;
15 // Test default constructor
16 {
17 OsiCuts r;
18 OSIUNITTEST_ASSERT_ERROR(r.colCutPtrs_.empty(), {}, "osicuts", "default constructor");
19 OSIUNITTEST_ASSERT_ERROR(r.rowCutPtrs_.empty(), {}, "osicuts", "default constructor");
20 OSIUNITTEST_ASSERT_ERROR(r.sizeColCuts() == 0, {}, "osicuts", "default constructor");
21 OSIUNITTEST_ASSERT_ERROR(r.sizeRowCuts() == 0, {}, "osicuts", "default constructor");
22 OSIUNITTEST_ASSERT_ERROR(r.sizeCuts() == 0, {}, "osicuts", "default constructor");
23 OSIUNITTEST_ASSERT_ERROR(r.mostEffectiveCutPtr() == NULL, {}, "osicuts", "default constructor");
24 }
25
26 // Create some cuts
27 OsiRowCut rcv[5];
28 OsiColCut ccv[5];
29 OsiCuts cuts;
30 int i;
31 for (i = 0; i < 5; i++) {
32 rcv[i].setEffectiveness(100. + i);
33 ccv[i].setEffectiveness(200. + i);
34 cuts.insert(rcv[i]);
35 cuts.insert(ccv[i]);
36 }
37
38 OsiCuts rhs;
39 {
40 OsiCuts cs;
41
42 // test inserting & accessing cut
43 for (i = 0; i < 5; i++) {
44 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i, {}, "osicuts", "inserting and accessing cuts");
45 OSIUNITTEST_ASSERT_ERROR(cs.sizeCuts() == 2 * i, {}, "osicuts", "inserting and accessing cuts");
46 cs.insert(rcv[i]);
47 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i + 1, {}, "osicuts", "inserting and accessing cuts");
48 OSIUNITTEST_ASSERT_ERROR(cs.sizeCuts() == 2 * i + 1, {}, "osicuts", "inserting and accessing cuts");
49 OSIUNITTEST_ASSERT_ERROR(cs.rowCut(i) == rcv[i], {}, "osicuts", "inserting and accessing cuts");
50 #if 0
51 const OsiCut * cp = cs.cutPtr(2*i);
52 OSIUNITTEST_ASSERT_ERROR(cs.rowCut(i).effectiveness() == cp->effectiveness(), {}, "osicuts", "inserting and accessing cuts");
53 const OsiRowCut * rcP = dynamic_cast<const OsiRowCut*>( cp );
54 OSIUNITTEST_ASSERT_ERROR(rcP != NULL, {}, "osicuts", "inserting and accessing cuts");
55 OSIUNITTEST_ASSERT_ERROR(*rcP == rcv[i], {}, "osicuts", "inserting and accessing cuts");
56 OSIUNITTEST_ASSERT_ERROR(dynamic_cast<const OsiColCut*>(cs.cutPtr(2*i)) == NULL, {}, "osicuts", "inserting and accessing cuts");
57 #endif
58
59 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == i, {}, "osicuts", "inserting and accessing cuts");
60 OSIUNITTEST_ASSERT_ERROR(cs.sizeCuts() == 2 * i + 1, {}, "osicuts", "inserting and accessing cuts");
61 cs.insert(ccv[i]);
62 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == i + 1, {}, "osicuts", "inserting and accessing cuts");
63 OSIUNITTEST_ASSERT_ERROR(cs.sizeCuts() == 2 * i + 2, {}, "osicuts", "inserting and accessing cuts");
64 OSIUNITTEST_ASSERT_ERROR(cs.colCut(i) == ccv[i], {}, "osicuts", "inserting and accessing cuts");
65 #if 0
66 OSIUNITTEST_ASSERT_ERROR(dynamic_cast<const OsiRowCut*>(cs.cutPtr(2*i+1)) == NULL, {}, "osicuts", "inserting and accessing cuts");
67 ccP = dynamic_cast<const OsiColCut*>( cs.cutPtr(2*i+1) );
68 OSIUNITTEST_ASSERT_ERROR(ccP != NULL, {}, "osicuts", "inserting and accessing cuts");
69 OSIUNITTEST_ASSERT_ERROR(*ccP == ccv[i], {}, "osicuts", "inserting and accessing cuts");
70 #endif
71 OSIUNITTEST_ASSERT_ERROR(eq(cs.mostEffectiveCutPtr()->effectiveness(), 200.0 + i), {}, "osicuts", "inserting and accessing cuts");
72 }
73
74 // Test inserting collection of OsiCuts
75 {
76 OsiCuts cs;
77 cs.insert(cuts);
78 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == 5, {}, "osicuts", "inserting collection of cuts");
79 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == 5, {}, "osicuts", "inserting collection of cuts");
80 OSIUNITTEST_ASSERT_ERROR(cs.sizeCuts() == 10, {}, "osicuts", "inserting collection of cuts");
81 }
82 /*
83 Test handling of cut pointers. Create a vector of cut pointers, then add
84 them to the collection. Dump the row cuts, then add again. The cut
85 objects should be deleted when the OsiCuts object is deleted at the end of
86 the block. This test should be monitored with some flavour of runtime
87 access checking to be sure that cuts are not freed twice or not freed at
88 all.
89 */
90 {
91 OsiCuts cs;
92 OsiRowCut *rcPVec[5];
93 OsiColCut *ccPVec[5];
94 for (i = 0; i < 5; i++) {
95 rcPVec[i] = rcv[i].clone();
96 ccPVec[i] = ccv[i].clone();
97 }
98 // test inserting cut ptr & accessing cut
99 for (i = 0; i < 5; i++) {
100 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i, {}, "osicuts", "handling of cut pointers");
101 OsiRowCut *rcP = rcPVec[i];
102 OSIUNITTEST_ASSERT_ERROR(rcP != NULL, {}, "osicuts", "handling of cut pointers");
103 cs.insert(rcP);
104 OSIUNITTEST_ASSERT_ERROR(rcP == NULL, {}, "osicuts", "handling of cut pointers");
105 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i + 1, {}, "osicuts", "handling of cut pointers");
106 OSIUNITTEST_ASSERT_ERROR(cs.rowCut(i) == rcv[i], {}, "osicuts", "handling of cut pointers");
107
108 OsiColCut *ccP = ccPVec[i];
109 OSIUNITTEST_ASSERT_ERROR(ccP != NULL, {}, "osicuts", "handling of cut pointers");
110 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == i, {}, "osicuts", "handling of cut pointers");
111 cs.insert(ccP);
112 OSIUNITTEST_ASSERT_ERROR(ccP == NULL, {}, "osicuts", "handling of cut pointers");
113 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == i + 1, {}, "osicuts", "handling of cut pointers");
114 OSIUNITTEST_ASSERT_ERROR(cs.colCut(i) == ccv[i], {}, "osicuts", "handling of cut pointers");
115
116 OSIUNITTEST_ASSERT_ERROR(eq(cs.mostEffectiveCutPtr()->effectiveness(), 200.0 + i), {}, "osicuts", "handling of cut pointers");
117 }
118 cs.dumpCuts(); // row cuts only
119 for (i = 0; i < 5; i++) {
120 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i, {}, "osicuts", "handling of cut pointers");
121 OsiRowCut *rcP = rcPVec[i];
122 OSIUNITTEST_ASSERT_ERROR(rcP != NULL, {}, "osicuts", "handling of cut pointers");
123 cs.insert(rcP);
124 OSIUNITTEST_ASSERT_ERROR(rcP == NULL, {}, "osicuts", "handling of cut pointers");
125 OSIUNITTEST_ASSERT_ERROR(cs.sizeRowCuts() == i + 1, {}, "osicuts", "handling of cut pointers");
126 OSIUNITTEST_ASSERT_ERROR(cs.rowCut(i) == rcv[i], {}, "osicuts", "handling of cut pointers");
127
128 OSIUNITTEST_ASSERT_ERROR(cs.sizeColCuts() == 5, {}, "osicuts", "handling of cut pointers");
129 }
130 }
131
132 // Test copy constructor
133 OsiCuts csC(cs);
134 OSIUNITTEST_ASSERT_ERROR(csC.sizeRowCuts() == 5, {}, "osicuts", "copy constructor");
135 OSIUNITTEST_ASSERT_ERROR(csC.sizeColCuts() == 5, {}, "osicuts", "copy constructor");
136 bool copy_ok = true;
137 for (i = 0; i < 5; i++) {
138 copy_ok &= csC.rowCut(i) == rcv[i];
139 copy_ok &= csC.colCut(i) == ccv[i];
140 copy_ok &= csC.rowCut(i) == cs.rowCut(i);
141 copy_ok &= csC.colCut(i) == cs.colCut(i);
142 ;
143 }
144 OSIUNITTEST_ASSERT_ERROR(copy_ok, {}, "osicuts", "copy constructor");
145 OSIUNITTEST_ASSERT_ERROR(eq(csC.mostEffectiveCutPtr()->effectiveness(), 204.0), {}, "osicuts", "copy constructor");
146
147 rhs = cs;
148 }
149
150 // Test results of assignment operation
151 bool ok = true;
152 for (i = 0; i < 5; i++) {
153 ok &= rhs.rowCut(i) == rcv[i];
154 ok &= rhs.colCut(i) == ccv[i];
155 }
156 OSIUNITTEST_ASSERT_ERROR(ok, {}, "osicuts", "assignment operator");
157 OSIUNITTEST_ASSERT_ERROR(eq(rhs.mostEffectiveCutPtr()->effectiveness(), 204.0), {}, "osicuts", "assignment operator");
158
159 // Test removing cuts
160 {
161 OsiCuts t(rhs);
162 OSIUNITTEST_ASSERT_ERROR(t.sizeRowCuts() == 5, {}, "osicuts", "removing cuts");
163 OSIUNITTEST_ASSERT_ERROR(t.sizeColCuts() == 5, {}, "osicuts", "removing cuts");
164 OSIUNITTEST_ASSERT_ERROR(eq(rhs.mostEffectiveCutPtr()->effectiveness(), 204.0), {}, "osicuts", "removing cuts");
165 OSIUNITTEST_ASSERT_ERROR(eq(t.mostEffectiveCutPtr()->effectiveness(), 204.0), {}, "osicuts", "removing cuts");
166
167 t.eraseRowCut(3);
168 OSIUNITTEST_ASSERT_ERROR(t.sizeRowCuts() == 4, {}, "osicuts", "removing cuts");
169 OSIUNITTEST_ASSERT_ERROR(t.sizeColCuts() == 5, {}, "osicuts", "removing cuts");
170 bool ok = true;
171 for (i = 0; i < 5; i++)
172 ok &= t.colCut(i) == ccv[i];
173 OSIUNITTEST_ASSERT_ERROR(ok, {}, "osicuts", "removing cuts");
174 OSIUNITTEST_ASSERT_ERROR(t.rowCut(0) == rcv[0], {}, "osicuts", "removing cuts");
175 OSIUNITTEST_ASSERT_ERROR(t.rowCut(1) == rcv[1], {}, "osicuts", "removing cuts");
176 OSIUNITTEST_ASSERT_ERROR(t.rowCut(2) == rcv[2], {}, "osicuts", "removing cuts");
177 OSIUNITTEST_ASSERT_ERROR(t.rowCut(3) == rcv[4], {}, "osicuts", "removing cuts");
178 OSIUNITTEST_ASSERT_ERROR(eq(t.mostEffectiveCutPtr()->effectiveness(), 204.0), {}, "osicuts", "removing cuts");
179
180 t.eraseColCut(4);
181 OSIUNITTEST_ASSERT_ERROR(t.sizeRowCuts() == 4, {}, "osicuts", "removing cuts");
182 OSIUNITTEST_ASSERT_ERROR(t.sizeColCuts() == 4, {}, "osicuts", "removing cuts");
183 ok = true;
184 for (i = 0; i < 4; i++)
185 ok &= t.colCut(i) == ccv[i];
186 OSIUNITTEST_ASSERT_ERROR(ok, {}, "osicuts", "removing cuts");
187 OSIUNITTEST_ASSERT_ERROR(t.rowCut(0) == rcv[0], {}, "osicuts", "removing cuts");
188 OSIUNITTEST_ASSERT_ERROR(t.rowCut(1) == rcv[1], {}, "osicuts", "removing cuts");
189 OSIUNITTEST_ASSERT_ERROR(t.rowCut(2) == rcv[2], {}, "osicuts", "removing cuts");
190 OSIUNITTEST_ASSERT_ERROR(t.rowCut(3) == rcv[4], {}, "osicuts", "removing cuts");
191 OSIUNITTEST_ASSERT_ERROR(eq(t.mostEffectiveCutPtr()->effectiveness(), 203.0), {}, "osicuts", "removing cuts");
192 }
193
194 // sorting cuts
195 {
196 OsiCuts t(rhs);
197 OSIUNITTEST_ASSERT_ERROR(t.sizeRowCuts() == 5, {}, "osicuts", "sorting cuts");
198 OSIUNITTEST_ASSERT_ERROR(t.sizeColCuts() == 5, {}, "osicuts", "sorting cuts");
199 t.rowCut(0).setEffectiveness(9.);
200 t.rowCut(1).setEffectiveness(1.);
201 t.rowCut(2).setEffectiveness(7.);
202 t.rowCut(3).setEffectiveness(3.);
203 t.rowCut(4).setEffectiveness(5.);
204 t.colCut(0).setEffectiveness(2.);
205 t.colCut(1).setEffectiveness(8.);
206 t.colCut(2).setEffectiveness(4.);
207 t.colCut(3).setEffectiveness(6.);
208 t.colCut(4).setEffectiveness(.5);
209 double totEff = 1. + 2. + 3. + 4. + 5. + 6. + 7. + 8. + 9. + 0.5;
210
211 {
212 // Test iterator over all cuts
213 double sumEff = 0.;
214 for (OsiCuts::iterator it = t.begin(); it != t.end(); ++it) {
215 double eff = (*it)->effectiveness();
216 sumEff += eff;
217 }
218 OSIUNITTEST_ASSERT_ERROR(sumEff == totEff, {}, "osicuts", "sorting cuts");
219 }
220
221 t.sort();
222 bool colcutsort_ok = true;
223 for (i = 1; i < 5; i++)
224 colcutsort_ok &= t.colCut(i - 1) > t.colCut(i);
225 OSIUNITTEST_ASSERT_ERROR(colcutsort_ok, {}, "osicuts", "sorting cuts");
226 bool rowcutsort_ok = true;
227 for (i = 1; i < 5; i++)
228 rowcutsort_ok &= t.rowCut(i - 1) > t.rowCut(i);
229 OSIUNITTEST_ASSERT_ERROR(rowcutsort_ok, {}, "osicuts", "sorting cuts");
230
231 {
232 // Test iterator over all cuts
233 double sumEff = 0.;
234 for (OsiCuts::iterator it = t.begin(); it != t.end(); ++it) {
235 sumEff += (*it)->effectiveness();
236 }
237 OSIUNITTEST_ASSERT_ERROR(sumEff == totEff, {}, "osicuts", "sorting cuts");
238 }
239
240 {
241 OsiCuts::iterator it = t.begin();
242 OsiCut *cm1 = *it;
243 ++it;
244 bool sort_ok = true;
245 for (; it != t.end(); it++) {
246 OsiCut *c = *it;
247 sort_ok &= (*cm1) > (*c);
248 cm1 = c;
249 }
250 OSIUNITTEST_ASSERT_ERROR(sort_ok, {}, "osicuts", "sorting cuts");
251 }
252 }
253 }
254
255 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
256 */