1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the class library */
/* SoPlex --- the Sequential object-oriented simPlex. */
/* */
/* Copyright 1996-2022 Zuse Institute Berlin */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* You should have received a copy of the Apache-2.0 license */
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file lprowsetbase.h
* @brief Set of LP columns.
*/
#ifndef _LPROWSETBASE_H_
#define _LPROWSETBASE_H_
#include <assert.h>
#include "soplex/spxdefines.h"
#include "soplex/basevectors.h"
#include "soplex/datakey.h"
#include "soplex/lprowbase.h"
namespace soplex
{
/**@brief Set of LP rows.
* @ingroup Algebra
*
* Class LPRowSetBase implements a set of \ref LPRowBase "LPRowBase%s". Unless for memory limitations, any number of
* LPRowBase%s may be #add%ed to an LPRowSetBase. Single or multiple LPRowBase%s may be added to an LPRowSetBase, where
* each method add() comes with two different signatures. One with and one without a parameter, used for returning the
* Keys assigned to the new LPRowBase%s by the set. See DataKey for a more detailed description of the concept of
* keys. For the concept of renumbering LPRowBase%s within an LPRowSetBase after removal of some LPRows see DataSet.
*
* @see DataSet, DataKey
*/
template < class R >
class LPRowSetBase : protected SVSetBase<R>
{
template < class S > friend class LPRowSetBase;
private:
// ------------------------------------------------------------------------------------------------------------------
/**@name Data */
///@{
VectorBase<R> left; ///< vector of left hand sides (lower bounds) of LPRowBase%s.
VectorBase<R> right; ///< vector of right hand sides (upper bounds) of LPRowBase%s.
VectorBase<R> object; ///< vector of objective coefficients.
///@}
protected:
DataArray < int > scaleExp; ///< row scaling factors (stored as bitshift)
// ------------------------------------------------------------------------------------------------------------------
/**@name Helpers */
///@{
/// Returns the complete SVSet.
const SVSetBase<R>* rowSet() const
{
return this;
}
///@}
public:
// ------------------------------------------------------------------------------------------------------------------
/**@name Access / modification */
///@{
/// Returns the number of LPRowBase%s in LPRowSetBase.
int num() const
{
return SVSetBase<R>::num();
}
/// Returns the maximum number of LPRowBase%s that fit.
int max() const
{
return SVSetBase<R>::max();
}
/// Returns the vector of lhs values.
const VectorBase<R>& lhs() const
{
return left;
}
/// Returns the vector of lhs values.
VectorBase<R>& lhs_w()
{
return left;
}
/// Returns the lhs of the \p i 'th LPRowBase.
const R& lhs(int i) const
{
return left[i];
}
/// Returns the lhs of the \p i 'th LPRowBase.
R& lhs_w(int i)
{
return left[i];
}
/// Returns the lhs of the LPRowBase with DataKey \p k in LPRowSetBase.
const R& lhs(const DataKey& k) const
{
return left[number(k)];
}
/// Returns the lhs of the LPRowBase with DataKey \p k in LPRowSetBase.
R& lhs_w(const DataKey& k)
{
return left[number(k)];
}
/// Returns the vector of rhs values.
const VectorBase<R>& rhs() const
{
return right;
}
/// Returns the vector of rhs values (writeable).
VectorBase<R>& rhs_w()
{
return right;
}
/// Returns the rhs of the \p i 'th LPRowBase.
const R& rhs(int i) const
{
return right[i];
}
/// Returns the rhs of the \p i 'th LPRowBase (writeable).
R& rhs_w(int i)
{
return right[i];
}
/// Returns the rhs of the LPRowBase with DataKey \p k in LPRowSetBase.
const R& rhs(const DataKey& k) const
{
return right[number(k)];
}
/// Returns the rhs of the LPRowBase with DataKey \p k in LPRowSetBase (writeable).
R& rhs_w(const DataKey& k)
{
return right[number(k)];
}
/// Returns the vector of objective coefficients.
const VectorBase<R>& obj() const
{
return object;
}
/// Returns the vector of objective coefficients (writeable).
VectorBase<R>& obj_w()
{
return object;
}
/// Returns the objective coefficient of the \p i 'th LPRowBase.
const R& obj(int i) const
{
return object[i];
}
/// Returns the objective coefficient of the \p i 'th LPRowBase (writeable).
R& obj_w(int i)
{
return object[i];
}
/// Returns the objective coefficient of the LPRowBase with DataKey \p k in LPRowSetBase.
const R& obj(const DataKey& k) const
{
return object[number(k)];
}
/// Returns the objective coefficient of the LPRowBase with DataKey \p k in LPRowSetBase (writeable).
R& obj_w(const DataKey& k)
{
return object[number(k)];
}
/// Returns a writable rowVector of the \p i 'th LPRowBase.
SVectorBase<R>& rowVector_w(int i)
{
return SVSetBase<R>::operator[](i);
}
/// Returns the rowVector of the \p i 'th LPRowBase.
const SVectorBase<R>& rowVector(int i) const
{
return SVSetBase<R>::operator[](i);
}
/// Returns a writable rowVector of the LPRowBase with DataKey \p k.
SVectorBase<R>& rowVector_w(const DataKey& k)
{
return SVSetBase<R>::operator[](k);
}
/// Returns the rowVector of the LPRowBase with DataKey \p k.
const SVectorBase<R>& rowVector(const DataKey& k) const
{
return SVSetBase<R>::operator[](k);
}
/// Returns the inequalitiy type of the \p i 'th LPRowBase.
typename LPRowBase<R>::Type type(int i) const
{
if(rhs(i) >= R(infinity))
return LPRowBase<R>::GREATER_EQUAL;
if(lhs(i) <= R(-infinity))
return LPRowBase<R>::LESS_EQUAL;
if(lhs(i) == rhs(i))
return LPRowBase<R>::EQUAL;
return LPRowBase<R>::RANGE;
}
/// Returns the inequality type of the LPRowBase with DataKey \p k.
typename LPRowBase<R>::Type type(const DataKey& k) const
{
return type(number(k));
}
/// Changes the inequality type of row \p i to \p type.
void setType(int i, typename LPRowBase<R>::Type t)
{
switch(t)
{
case LPRowBase<R>::LESS_EQUAL:
lhs_w(i) = R(-infinity);
break;
case LPRowBase<R>::EQUAL:
if(lhs_w(i) > R(-infinity))
rhs_w(i) = lhs(i);
else
lhs_w(i) = rhs(i);
break;
case LPRowBase<R>::GREATER_EQUAL:
rhs_w(i) = R(infinity);
break;
case LPRowBase<R>::RANGE:
MSG_ERROR(std::cerr << "EROWST01 RANGE not supported in LPRowSet::setType()" << std::endl);
throw SPxInternalCodeException("XROWST01 This should never happen.");
default:
throw SPxInternalCodeException("XROWST02 This should never happen.");
}
}
/// Returns the value of the \p i'th LPRowBase.
const R& value(int i) const
{
if(rhs(i) < R(infinity))
return rhs(i);
else
{
assert(lhs(i) > R(-infinity));
return lhs(i);
}
}
/// Returns the value of the LPRowBase with DataKey \p k.
/** The \em value of a row depends on its type: if the inequality is of type "greater or equal", the value is the lhs
* of the row. Otherwise, the value is the rhs.
*/
const R& value(const DataKey& k) const
{
return value(number(k));
}
/// Returns the DataKey of the \p i 'th LPRowBase in LPRowSetBase.
DataKey key(int i) const
{
return SVSetBase<R>::key(i);
}
/// Returns the number of the LPRowBase with DataKey \p k in LPRowSetBase.
int number(const DataKey& k) const
{
return SVSetBase<R>::number(k);
}
/// does DataKey \p k belong to LPRowSetBase ?
bool has(const DataKey& k) const
{
return SVSetBase<R>::has(k);
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Extension
*
* Extension methods come with two signatures, one of them providing a parameter to return the assigned
* DataKey(s). See DataSet for a more detailed description. All extension methods will automatically rearrange or
* allocate more memory if required.
*/
///@{
///
void add(const LPRowBase<R>& row)
{
DataKey k;
add(k, row);
}
/// Adds \p row to LPRowSetBase.
void add(DataKey& pkey, const LPRowBase<R>& prow)
{
add(pkey, prow.lhs(), prow.rowVector(), prow.rhs(), prow.obj());
}
/// Adds LPRowBase consisting of left hand side \p lhs, row vector \p rowVector, and right hand side \p rhs to LPRowSetBase.
void add(const R& plhs, const SVectorBase<R>& prowVector, const R& prhs, const R& pobj = 0,
const int& pscaleExp = 0)
{
DataKey k;
add(k, plhs, prowVector, prhs, pobj, pscaleExp);
}
/// Adds LPRowBase consisting of left hand side \p lhs, row vector \p rowVector, and right hand side \p rhs to LPRowSetBase.
template < class S >
void add(const S* lhsValue, const S* rowValues, const int* rowIndices, int rowSize,
const S* rhsValue, const S* objValue = 0)
{
assert(lhsValue != 0);
assert(rowSize <= 0 || rowValues != 0);
assert(rowSize <= 0 || rowIndices != 0);
assert(rhsValue != 0);
DataKey k;
add(k, lhsValue, rowValues, rowIndices, rowSize, rhsValue, objValue);
}
/// Adds LPRowBase consisting of left hand side \p lhs, row vector \p rowVector, and right hand side \p rhs to
/// LPRowSetBase, with DataKey \p key.
template < class S >
void add(DataKey& newkey, const S* lhsValue, const S* rowValues, const int* rowIndices, int rowSize,
const S* rhsValue, const S* objValue = 0)
{
assert(lhsValue != 0);
assert(rowSize <= 0 || rowValues != 0);
assert(rowSize <= 0 || rowIndices != 0);
assert(rhsValue != 0);
SVSetBase<R>::add(newkey, rowValues, rowIndices, rowSize);
if(num() > left.dim())
{
left.reDim(num());
right.reDim(num());
object.reDim(num());
}
left[num() - 1] = *lhsValue;
right[num() - 1] = *rhsValue;
if(objValue != 0)
object[num() - 1] = *objValue;
else
object[num() - 1] = 0;
}
/// Adds LPRowBase consisting of left hand side \p lhs, row vector \p rowVector, and right hand side \p rhs to
/// LPRowSetBase, with DataKey \p key.
void add(DataKey& newkey, const R& newlhs, const SVectorBase<R>& newrowVector, const R& newrhs,
const R& newobj = 0, const int& newscaleExp = 0)
{
SVSetBase<R>::add(newkey, newrowVector);
if(num() > left.dim())
{
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.reSize(num());
}
left[num() - 1] = newlhs;
right[num() - 1] = newrhs;
object[num() - 1] = newobj;
scaleExp[num() - 1] = newscaleExp;
}
///
void add(const LPRowSetBase<R>& newset)
{
int i = num();
SVSetBase<R>::add(newset);
if(num() > left.dim())
{
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.reSize(num());
}
for(int j = 0; i < num(); ++i, ++j)
{
left[i] = newset.lhs(j);
right[i] = newset.rhs(j);
object[i] = newset.obj(j);
scaleExp[i] = newset.scaleExp[j];
}
}
/// Adds all LPRowBase%s of \p set to LPRowSetBase.
void add(DataKey keys[], const LPRowSetBase<R>& set)
{
int i = num();
add(set);
for(int j = 0; i < num(); ++i, ++j)
keys[j] = key(i);
}
/// Extends row \p n to fit \p newmax nonzeros.
void xtend(int n, int newmax)
{
SVSetBase<R>::xtend(rowVector_w(n), newmax);
}
/// Extends row with DataKey \p key to fit \p newmax nonzeros.
void xtend(const DataKey& pkey, int pnewmax)
{
SVSetBase<R>::xtend(rowVector_w(pkey), pnewmax);
}
/// Adds \p n nonzero (\p idx, \p val)-pairs to rowVector with DataKey \p k.
void add2(const DataKey& k, int n, const int idx[], const R val[])
{
SVSetBase<R>::add2(rowVector_w(k), n, idx, val);
}
/// Adds \p n nonzero (\p idx, \p val)-pairs to \p i 'th rowVector.
void add2(int i, int n, const int idx[], const R val[])
{
SVSetBase<R>::add2(rowVector_w(i), n, idx, val);
}
/// Adds \p n nonzero (\p idx, \p val)-pairs to \p i 'th rowVector.
template < class S >
void add2(int i, int n, const int idx[], const S val[])
{
SVSetBase<R>::add2(rowVector_w(i), n, idx, val);
}
/// Creates new LPRowBase with specified parameters and returns a reference to its row vector.
SVectorBase<R>& create(int pnonzeros = 0, const R& plhs = 0, const R& prhs = 1, const R& pobj = 0,
const int& pscaleExp = 0)
{
DataKey k;
return create(k, pnonzeros, plhs, prhs, pobj, pscaleExp);
}
/// Creates new LPRowBase with specified parameters and returns a reference to its row vector.
SVectorBase<R>& create(DataKey& newkey, int nonzeros = 0, const R& newlhs = 0, const R& newrhs = 1,
const R& newobj = 0, const int& newscaleExp = 0)
{
if(num() + 1 > left.dim())
{
left.reDim(num() + 1);
right.reDim(num() + 1);
object.reDim(num() + 1);
scaleExp.reSize(num() + 1);
}
left[num()] = newlhs;
right[num()] = newrhs;
object[num()] = newobj;
scaleExp[num()] = newscaleExp;
return *SVSetBase<R>::create(newkey, nonzeros);
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Shrinking
*
* See DataSet for a description of the renumbering of the remaining LPRowBase%s in a LPRowSetBase after the call of
* a removal method.
*/
///@{
/// Removes \p i 'th LPRowBase.
void remove(int i)
{
SVSetBase<R>::remove(i);
left[i] = left[num()];
right[i] = right[num()];
object[i] = object[num()];
scaleExp[i] = scaleExp[num()];
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.reSize(num());
}
/// Removes LPRowBase with DataKey \p k.
void remove(const DataKey& k)
{
remove(number(k));
}
/// Removes multiple LPRowBase%s.
void remove(int perm[])
{
int j = num();
SVSetBase<R>::remove(perm);
for(int i = 0; i < j; ++i)
{
if(perm[i] >= 0 && perm[i] != i)
{
left[perm[i]] = left[i];
right[perm[i]] = right[i];
object[perm[i]] = object[i];
scaleExp[perm[i]] = scaleExp[i];
}
}
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.reSize(num());
}
/// Removes \p n LPRowBase%s with row numbers given by \p nums.
void remove(const int nums[], int n)
{
DataArray<int> perm(num());
remove(nums, n, perm.get_ptr());
}
/// Removes \p n LPRowBase%s with row numbers given by \p nums,
/// Stores permutation of row indices in \p perm.
void remove(const int nums[], int n, int* perm)
{
SVSetBase<R>::remove(nums, n, perm);
int j = num();
for(int i = 0; i < j; ++i)
{
if(perm[i] >= 0 && perm[i] != i)
{
left[perm[i]] = left[i];
right[perm[i]] = right[i];
object[perm[i]] = object[i];
scaleExp[perm[i]] = scaleExp[i];
}
}
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.reSize(num());
}
/// Removes all LPRowBase%s.
void clear()
{
SVSetBase<R>::clear();
left.reDim(num());
right.reDim(num());
object.reDim(num());
scaleExp.clear();
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Memory Management
*
* For a description of the memory management methods, see the documentation of SVSet, which has been used for
* implementating LPRowSetBase.
*/
///@{
/// Reallocates memory to be able to store \p newmax LPRowBase%s.
void reMax(int newmax = 0)
{
SVSetBase<R>::reMax(newmax);
left.reSize(max());
right.reSize(max());
object.reSize(max());
scaleExp.reSize(max());
}
/// Returns number of used nonzero entries.
int memSize() const
{
return SVSetBase<R>::memSize();
}
/// Returns length of nonzero memory.
int memMax() const
{
return SVSetBase<R>::memMax();
}
/// Reallocates memory to be able to store \p newmax nonzeros.
void memRemax(int newmax)
{
SVSetBase<R>::memRemax(newmax);
}
/// Garbage collection in nonzero memory.
void memPack()
{
SVSetBase<R>::memPack();
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Consistency check */
/// Checks consistency.
bool isConsistent() const
{
#ifdef ENABLE_CONSISTENCY_CHECKS
const int ldim = left.dim();
if(ldim != right.dim())
return MSGinconsistent("LPRowSetBase");
if(ldim != object.dim())
return MSGinconsistent("LPRowSetBase");
if(ldim != num())
return MSGinconsistent("LPRowSetBase");
return SVSetBase<R>::isConsistent();
#else
return true;
#endif
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Construction / Destruction */
///@{
/// Default constructor.
/** The user can specify the initial maximum number of rows \p max and the initial maximum number of nonzero entries
* \p memmax. If these parameters are omitted, a default size is used. However, one can add an arbitrary number of
* rows to the LPRowSetBase, which may result in automated memory realllocation.
*/
explicit
LPRowSetBase<R>(int pmax = -1, int pmemmax = -1)
: SVSetBase<R>(pmax, pmemmax), left(0), right(0), object(0), scaleExp(0)
{
assert(isConsistent());
}
/// Assignment operator.
LPRowSetBase<R>& operator=(const LPRowSetBase<R>& rs)
{
if(this != &rs)
{
SVSetBase<R>::operator=(rs);
left = rs.left;
right = rs.right;
object = rs.object;
scaleExp = rs.scaleExp;
assert(isConsistent());
}
return *this;
}
/// Assignment operator.
template < class S >
LPRowSetBase<R>& operator=(const LPRowSetBase<S>& rs)
{
if(this != (const LPRowSetBase<R>*)(&rs))
{
SVSetBase<R>::operator=(rs);
left = rs.left;
right = rs.right;
object = rs.object;
scaleExp = rs.scaleExp;
assert(isConsistent());
}
return *this;
}
/// Copy constructor.
LPRowSetBase<R>(const LPRowSetBase<R>& rs)
: SVSetBase<R>(rs)
, left(rs.left)
, right(rs.right)
, object(rs.object)
, scaleExp(rs.scaleExp)
{
assert(isConsistent());
}
/// Copy constructor.
template < class S >
LPRowSetBase<R>(const LPRowSetBase<S>& rs)
: SVSetBase<R>(rs)
, left(rs.left)
, right(rs.right)
, object(rs.object)
, scaleExp(rs.scaleExp)
{
assert(isConsistent());
}
/// Destructor.
virtual ~LPRowSetBase<R>()
{}
///@}
};
} // namespace soplex
#endif // _LPROWSETBASE_H_