-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathHighsInterface.cpp
More file actions
4245 lines (4081 loc) · 178 KB
/
HighsInterface.cpp
File metadata and controls
4245 lines (4081 loc) · 178 KB
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the HiGHS linear optimization suite */
/* */
/* Available as open-source under the MIT License */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file lp_data/HighsInterface.cpp
* @brief
*/
#include <sstream>
#include "Highs.h"
#include "lp_data/HighsLpUtils.h"
#include "lp_data/HighsModelUtils.h"
#include "model/HighsHessianUtils.h"
#include "simplex/HSimplex.h"
#include "util/HighsMatrixUtils.h"
#include "util/HighsSort.h"
void Highs::reportModelStats() const {
const HighsLp& lp = this->model_.lp_;
const HighsHessian& hessian = this->model_.hessian_;
const HighsLogOptions& log_options = this->options_.log_options;
if (!*log_options.output_flag) return;
HighsInt num_integer = 0;
HighsInt num_binary = 0;
HighsInt num_semi_continuous = 0;
HighsInt num_semi_integer = 0;
for (HighsInt iCol = 0; iCol < static_cast<HighsInt>(lp.integrality_.size());
iCol++) {
switch (lp.integrality_[iCol]) {
case HighsVarType::kInteger:
num_integer++;
if (lp.col_lower_[iCol] == 0 && lp.col_upper_[iCol] == 1) num_binary++;
break;
case HighsVarType::kSemiContinuous:
num_semi_continuous++;
break;
case HighsVarType::kSemiInteger:
num_semi_integer++;
break;
default:
break;
}
}
std::string problem_type;
const bool non_continuous =
num_integer + num_semi_continuous + num_semi_integer;
if (hessian.dim_) {
if (non_continuous) {
problem_type = "MIQP";
} else {
problem_type = "QP ";
}
} else {
if (non_continuous) {
problem_type = "MIP ";
} else {
problem_type = "LP ";
}
}
const HighsInt a_num_nz = lp.a_matrix_.numNz();
const HighsInt q_num_nz = hessian.dim_ > 0 ? hessian.numNz() : 0;
if (*log_options.log_dev_level) {
highsLogDev(log_options, HighsLogType::kInfo, "%4s : %s\n",
problem_type.c_str(), lp.model_name_.c_str());
highsLogDev(log_options, HighsLogType::kInfo,
"Rows : %" HIGHSINT_FORMAT "\n", lp.num_row_);
highsLogDev(log_options, HighsLogType::kInfo,
"Cols : %" HIGHSINT_FORMAT "\n", lp.num_col_);
if (q_num_nz) {
highsLogDev(log_options, HighsLogType::kInfo,
"Matrix Nz : %" HIGHSINT_FORMAT "\n", a_num_nz);
highsLogDev(log_options, HighsLogType::kInfo,
"Hessian Nz: %" HIGHSINT_FORMAT "\n", q_num_nz);
} else {
highsLogDev(log_options, HighsLogType::kInfo,
"Nonzeros : %" HIGHSINT_FORMAT "\n", a_num_nz);
}
if (num_integer)
highsLogDev(log_options, HighsLogType::kInfo,
"Integer : %" HIGHSINT_FORMAT " (%" HIGHSINT_FORMAT
" binary)\n",
num_integer, num_binary);
if (num_semi_continuous)
highsLogDev(log_options, HighsLogType::kInfo,
"SemiConts : %" HIGHSINT_FORMAT "\n", num_semi_continuous);
if (num_semi_integer)
highsLogDev(log_options, HighsLogType::kInfo,
"SemiInt : %" HIGHSINT_FORMAT "\n", num_semi_integer);
} else {
std::stringstream stats_line;
stats_line << problem_type;
if (lp.model_name_.length()) stats_line << " " << lp.model_name_;
stats_line << " has " << lp.num_row_ << " rows; " << lp.num_col_ << " cols";
if (q_num_nz) {
stats_line << "; " << a_num_nz << " matrix nonzeros";
stats_line << "; " << q_num_nz << " Hessian nonzeros";
} else {
stats_line << "; " << a_num_nz << " nonzeros";
}
if (num_integer)
stats_line << "; " << num_integer << " integer variables (" << num_binary
<< " binary)";
if (num_semi_continuous)
stats_line << "; " << num_semi_continuous << " semi-continuous variables";
if (num_semi_integer)
stats_line << "; " << num_semi_integer << " semi-integer variables";
highsLogUser(log_options, HighsLogType::kInfo, "%s\n",
stats_line.str().c_str());
}
}
HighsStatus Highs::formStandardFormLp() {
this->clearStandardFormLp();
HighsLp& lp = this->model_.lp_;
HighsSparseMatrix& matrix = lp.a_matrix_;
// Ensure that the incumbent LP and standard form LP matrices are rowwise
matrix.ensureRowwise();
// Original rows are processed before columns, so that any original
// boxed rows can be transformed to pairs of one-sided rows,
// requiring the standard form matrix to be row-wise. The original
// columns are assumed to come before any new columns, so their
// costs (as a minimization) must be defined befor costs of new
// columns.
// Determine the objective scaling, and apply it to any offset
HighsInt sense = HighsInt(lp.sense_);
this->standard_form_offset_ = sense * lp.offset_;
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++)
this->standard_form_cost_.push_back(sense * lp.col_cost_[iCol]);
this->standard_form_matrix_.format_ = MatrixFormat::kRowwise;
this->standard_form_matrix_.num_col_ = lp.num_col_;
// Create a HighsSparseMatrix instance to store rows extracted from
// the original constraint matrix
HighsInt local_row_min_nnz = std::max(lp.num_col_, HighsInt(2));
HighsSparseMatrix local_row;
local_row.ensureRowwise();
local_row.num_row_ = 1;
local_row.num_col_ = lp.num_col_;
local_row.index_.resize(local_row_min_nnz);
local_row.value_.resize(local_row_min_nnz);
local_row.start_.resize(2);
HighsInt& num_nz = local_row.start_[1];
local_row.start_[0] = 0;
HighsInt num_fixed_row = 0;
HighsInt num_boxed_row = 0;
HighsInt num_lower_row = 0;
HighsInt num_upper_row = 0;
HighsInt num_free_row = 0;
HighsInt num_fixed_col = 0;
HighsInt num_boxed_col = 0;
HighsInt num_lower_col = 0;
HighsInt num_upper_col = 0;
HighsInt num_free_col = 0;
std::vector<HighsInt> slack_ix;
for (HighsInt iRow = 0; iRow < lp.num_row_; iRow++) {
double lower = lp.row_lower_[iRow];
double upper = lp.row_upper_[iRow];
if (lower <= -kHighsInf && upper >= kHighsInf) {
assert(0 == 1);
// Free row
num_free_row++;
continue;
}
if (lower == upper) {
// Equality row
num_fixed_row++;
matrix.getRow(iRow, num_nz, local_row.index_.data(),
local_row.value_.data());
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(upper);
continue;
} else if (lower <= -kHighsInf) {
// Upper bounded row, so record the slack
num_upper_row++;
assert(upper < kHighsInf);
HighsInt standard_form_row = this->standard_form_rhs_.size();
slack_ix.push_back(standard_form_row + 1);
matrix.getRow(iRow, num_nz, local_row.index_.data(),
local_row.value_.data());
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(upper);
} else if (upper >= kHighsInf) {
// Lower bounded row, so record the slack
num_lower_row++;
assert(lower > -kHighsInf);
HighsInt standard_form_row = this->standard_form_rhs_.size();
slack_ix.push_back(-(standard_form_row + 1));
matrix.getRow(iRow, num_nz, local_row.index_.data(),
local_row.value_.data());
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(lower);
} else {
// Boxed row, so record the lower slack
assert(lower > -kHighsInf);
assert(upper < kHighsInf);
num_boxed_row++;
HighsInt standard_form_row = this->standard_form_rhs_.size();
slack_ix.push_back(-(standard_form_row + 1));
matrix.getRow(iRow, num_nz, local_row.index_.data(),
local_row.value_.data());
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(lower);
// .. and upper slack, adding a copy of the row
standard_form_row = this->standard_form_rhs_.size();
slack_ix.push_back(standard_form_row + 1);
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(upper);
}
}
// Add rows corresponding to boxed columns
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
double lower = lp.col_lower_[iCol];
double upper = lp.col_upper_[iCol];
if (lower > -kHighsInf && upper < kHighsInf) {
// Boxed column
//
// x will be replaced by x = l + X (below) with X >= 0
//
// Introduce variable s >= 0 so that (with x >= l still)
//
// x = u - s => x + s = u
this->standard_form_cost_.push_back(0);
this->standard_form_matrix_.num_col_++;
local_row.num_col_++;
local_row.index_[0] = iCol;
local_row.index_[1] = this->standard_form_matrix_.num_col_ - 1;
local_row.value_[0] = 1;
local_row.value_[1] = 1;
local_row.start_[1] = 2;
this->standard_form_matrix_.addRows(local_row);
this->standard_form_rhs_.push_back(upper);
}
}
// Finished with both matrices, row-wise, so ensure that the
// incumbent matrix leaves col-wise, and that the standard form
// matrix is col-wise so RHS shifts can be applied and more columns
// can be added
matrix.ensureColwise();
this->standard_form_matrix_.ensureColwise();
// Work through the columns, ensuring that all have non-negativity bounds
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
double cost = sense * lp.col_cost_[iCol];
double lower = lp.col_lower_[iCol];
double upper = lp.col_upper_[iCol];
if (lower > -kHighsInf) {
// Finite lower bound
if (upper < kHighsInf) {
if (lower == upper) {
// Fixed column
num_fixed_col++;
} else {
// Boxed column
num_boxed_col++;
}
} else {
// Lower column
num_lower_col++;
}
if (lower != 0) {
// x >= l, so shift x-l = X >= 0, giving x = X + l
//
// Cost contribution c(X+l) = cX + cl
this->standard_form_offset_ += cost * lower;
// Constraint contribution a(X+l) = aX + al
for (HighsInt iEl = this->standard_form_matrix_.start_[iCol];
iEl < this->standard_form_matrix_.start_[iCol + 1]; iEl++)
this->standard_form_rhs_[this->standard_form_matrix_.index_[iEl]] -=
this->standard_form_matrix_.value_[iEl] * lower;
}
} else if (upper < kHighsInf) {
// Upper column
num_upper_col++;
// Have to operate even if u=0, since cost and column values are negated
//
// x <= u, so shift u-x = X >= 0, giving x = u - X
//
// Cost contribution c(u-X) = cu - cX
this->standard_form_offset_ += cost * upper;
this->standard_form_cost_[iCol] = -cost;
// Constraint contribution a(u-X) = -aX + au
for (HighsInt iEl = this->standard_form_matrix_.start_[iCol];
iEl < this->standard_form_matrix_.start_[iCol + 1]; iEl++) {
this->standard_form_rhs_[this->standard_form_matrix_.index_[iEl]] -=
this->standard_form_matrix_.value_[iEl] * upper;
this->standard_form_matrix_.value_[iEl] =
-this->standard_form_matrix_.value_[iEl];
}
} else {
// Free column
num_free_col++;
// Represent as x = x+ - x-
//
// where original column is now x+ >= 0
//
// and x- >= 0 has negation of its cost and matrix column
this->standard_form_cost_.push_back(-cost);
for (HighsInt iEl = this->standard_form_matrix_.start_[iCol];
iEl < this->standard_form_matrix_.start_[iCol + 1]; iEl++) {
this->standard_form_matrix_.index_.push_back(
this->standard_form_matrix_.index_[iEl]);
this->standard_form_matrix_.value_.push_back(
-this->standard_form_matrix_.value_[iEl]);
}
this->standard_form_matrix_.start_.push_back(
HighsInt(this->standard_form_matrix_.index_.size()));
}
}
// Now add the slack variables
for (HighsInt iRow : slack_ix) {
this->standard_form_cost_.push_back(0);
if (iRow > 0) {
this->standard_form_matrix_.index_.push_back(iRow - 1);
this->standard_form_matrix_.value_.push_back(1);
} else {
this->standard_form_matrix_.index_.push_back(-iRow - 1);
this->standard_form_matrix_.value_.push_back(-1);
}
this->standard_form_matrix_.start_.push_back(
HighsInt(this->standard_form_matrix_.index_.size()));
}
// Now set correct values for the dimensions of this->standard_form_matrix_
this->standard_form_matrix_.num_col_ = int(standard_form_cost_.size());
this->standard_form_matrix_.num_row_ = int(standard_form_rhs_.size());
this->standard_form_valid_ = true;
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Standard form LP obtained for LP with (free / lower / upper / "
"boxed / fixed) variables"
" (%d / %d / %d / %d / %d) and constraints"
" (%d / %d / %d / %d / %d) \n",
int(num_free_col), int(num_lower_col), int(num_upper_col),
int(num_boxed_col), int(num_fixed_col), int(num_free_row),
int(num_lower_row), int(num_upper_row), int(num_boxed_row),
int(num_fixed_row));
return HighsStatus::kOk;
}
HighsStatus Highs::basisForSolution() {
HighsLp& lp = model_.lp_;
assert(!lp.isMip() || options_.solve_relaxation);
assert(solution_.value_valid);
invalidateBasis();
HighsInt num_basic = 0;
HighsBasis basis;
for (HighsInt iCol = 0; iCol < lp.num_col_; iCol++) {
if (std::fabs(lp.col_lower_[iCol] - solution_.col_value[iCol]) <=
options_.primal_feasibility_tolerance) {
basis.col_status.push_back(HighsBasisStatus::kLower);
} else if (std::fabs(lp.col_upper_[iCol] - solution_.col_value[iCol]) <=
options_.primal_feasibility_tolerance) {
basis.col_status.push_back(HighsBasisStatus::kUpper);
} else {
num_basic++;
basis.col_status.push_back(HighsBasisStatus::kBasic);
}
}
const HighsInt num_basic_col = num_basic;
for (HighsInt iRow = 0; iRow < lp.num_row_; iRow++) {
if (std::fabs(lp.row_lower_[iRow] - solution_.row_value[iRow]) <=
options_.primal_feasibility_tolerance) {
basis.row_status.push_back(HighsBasisStatus::kLower);
} else if (std::fabs(lp.row_upper_[iRow] - solution_.row_value[iRow]) <=
options_.primal_feasibility_tolerance) {
basis.row_status.push_back(HighsBasisStatus::kUpper);
} else {
num_basic++;
basis.row_status.push_back(HighsBasisStatus::kBasic);
}
}
const HighsInt num_basic_row = num_basic - num_basic_col;
assert((int)basis.col_status.size() == lp.num_col_);
assert((int)basis.row_status.size() == lp.num_row_);
highsLogDev(options_.log_options, HighsLogType::kInfo,
"LP has %d rows and solution yields %d possible basic variables "
"(%d / %d; %d / %d)\n",
(int)lp.num_row_, (int)num_basic, (int)num_basic_col,
(int)lp.num_col_, (int)num_basic_row, (int)lp.num_row_);
return this->setBasis(basis);
}
HighsStatus Highs::addColsInterface(
HighsInt ext_num_new_col, const double* ext_col_cost,
const double* ext_col_lower, const double* ext_col_upper,
HighsInt ext_num_new_nz, const HighsInt* ext_a_start,
const HighsInt* ext_a_index, const double* ext_a_value) {
HighsStatus return_status = HighsStatus::kOk;
HighsOptions& options = options_;
if (ext_num_new_col < 0) return HighsStatus::kError;
if (ext_num_new_nz < 0) return HighsStatus::kError;
if (ext_num_new_col == 0) return HighsStatus::kOk;
if (ext_num_new_col > 0)
if (isColDataNull(options.log_options, ext_col_cost, ext_col_lower,
ext_col_upper))
return HighsStatus::kError;
if (ext_num_new_nz > 0)
if (isMatrixDataNull(options.log_options, ext_a_start, ext_a_index,
ext_a_value))
return HighsStatus::kError;
HighsLp& lp = model_.lp_;
HighsBasis& basis = basis_;
HighsScale& scale = lp.scale_;
bool& useful_basis = basis.useful;
bool& lp_has_scaling = lp.scale_.has_scaling;
// Check that if nonzeros are to be added then the model has a positive number
// of rows
if (lp.num_row_ <= 0 && ext_num_new_nz > 0) return HighsStatus::kError;
// Record the new number of columns
HighsInt newNumCol = lp.num_col_ + ext_num_new_col;
HighsIndexCollection index_collection;
index_collection.dimension_ = ext_num_new_col;
index_collection.is_interval_ = true;
index_collection.from_ = 0;
index_collection.to_ = ext_num_new_col - 1;
// Take a copy of the cost and bounds that can be normalised
std::vector<double> local_colCost{ext_col_cost,
ext_col_cost + ext_num_new_col};
std::vector<double> local_colLower{ext_col_lower,
ext_col_lower + ext_num_new_col};
std::vector<double> local_colUpper{ext_col_upper,
ext_col_upper + ext_num_new_col};
bool local_has_infinite_cost = false;
return_status = interpretCallStatus(
options_.log_options,
assessCosts(options, lp.num_col_, index_collection, local_colCost,
local_has_infinite_cost, options.infinite_cost),
return_status, "assessCosts");
if (return_status == HighsStatus::kError) return return_status;
// Assess the column bounds
return_status = interpretCallStatus(
options_.log_options,
assessBounds(options, "Col", lp.num_col_, index_collection,
local_colLower, local_colUpper, options.infinite_bound),
return_status, "assessBounds");
if (return_status == HighsStatus::kError) return return_status;
if (lp.user_bound_scale_) {
// Assess and apply any user bound scaling
if (!boundScaleOk(local_colLower, local_colUpper, lp.user_bound_scale_,
options.infinite_bound)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"User bound scaling yields infinite bound\n");
return HighsStatus::kError;
}
double bound_scale_value = std::pow(2, lp.user_bound_scale_);
for (HighsInt iCol = 0; iCol < ext_num_new_col; iCol++) {
local_colLower[iCol] *= bound_scale_value;
local_colUpper[iCol] *= bound_scale_value;
}
}
if (lp.user_cost_scale_) {
// Assess and apply any user cost scaling
if (!costScaleOk(local_colCost, lp.user_cost_scale_,
options.infinite_cost)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"User cost scaling yields infinite cost\n");
return HighsStatus::kError;
}
double cost_scale_value = std::pow(2, lp.user_cost_scale_);
for (HighsInt iCol = 0; iCol < ext_num_new_col; iCol++)
local_colCost[iCol] *= cost_scale_value;
}
// Append the columns to the LP vectors and matrix
appendColsToLpVectors(lp, ext_num_new_col, local_colCost, local_colLower,
local_colUpper);
// Form a column-wise HighsSparseMatrix of the new matrix columns so
// that is easy to handle and, if there are nonzeros, it can be
// normalised
HighsSparseMatrix local_a_matrix;
local_a_matrix.num_col_ = ext_num_new_col;
local_a_matrix.num_row_ = lp.num_row_;
local_a_matrix.format_ = MatrixFormat::kColwise;
if (ext_num_new_nz) {
local_a_matrix.start_ = {ext_a_start, ext_a_start + ext_num_new_col};
local_a_matrix.start_.resize(ext_num_new_col + 1);
local_a_matrix.start_[ext_num_new_col] = ext_num_new_nz;
local_a_matrix.index_ = {ext_a_index, ext_a_index + ext_num_new_nz};
local_a_matrix.value_ = {ext_a_value, ext_a_value + ext_num_new_nz};
// Assess the matrix rows
return_status =
interpretCallStatus(options_.log_options,
local_a_matrix.assess(options.log_options, "LP",
options.small_matrix_value,
options.large_matrix_value),
return_status, "assessMatrix");
if (return_status == HighsStatus::kError) return return_status;
} else {
// No nonzeros so, whether the constraint matrix is column-wise or
// row-wise, adding the empty matrix is trivial. Complete the
// setup of an empty column-wise HighsSparseMatrix of the new
// matrix columns
local_a_matrix.start_.assign(ext_num_new_col + 1, 0);
}
// Append the columns to LP matrix
lp.a_matrix_.addCols(local_a_matrix);
if (lp_has_scaling) {
// Extend the column scaling factors
scale.col.resize(newNumCol);
for (HighsInt iCol = 0; iCol < ext_num_new_col; iCol++)
scale.col[lp.num_col_ + iCol] = 1.0;
scale.num_col = newNumCol;
// Apply the existing row scaling to the new columns
local_a_matrix.applyRowScale(scale);
// Consider applying column scaling to the new columns.
local_a_matrix.considerColScaling(options.allowed_matrix_scale_factor,
&scale.col[lp.num_col_]);
}
// Update the basis corresponding to new nonbasic columns
if (useful_basis) appendNonbasicColsToBasisInterface(ext_num_new_col);
// Possibly add column names
lp.addColNames("", ext_num_new_col);
// Increase the number of columns in the LP
lp.num_col_ += ext_num_new_col;
assert(lpDimensionsOk("addCols", lp, options.log_options));
// Interpret possible introduction of infinite costs
lp.has_infinite_cost_ = lp.has_infinite_cost_ || local_has_infinite_cost;
assert(lp.has_infinite_cost_ == lp.hasInfiniteCost(options.infinite_cost));
// Deduce the consequences of adding new columns
invalidateModelStatusSolutionAndInfo();
// Determine any implications for simplex data
ekk_instance_.addCols(lp, local_a_matrix);
// Extend any Hessian with zeros on the diagonal
if (this->model_.hessian_.dim_)
completeHessian(lp.num_col_, this->model_.hessian_);
return return_status;
}
HighsStatus Highs::addRowsInterface(HighsInt ext_num_new_row,
const double* ext_row_lower,
const double* ext_row_upper,
HighsInt ext_num_new_nz,
const HighsInt* ext_ar_start,
const HighsInt* ext_ar_index,
const double* ext_ar_value) {
// addRows is fundamentally different from addCols, since the new
// matrix data are held row-wise, so we have to insert data into the
// column-wise matrix of the LP.
if (kExtendInvertWhenAddingRows) {
if (ekk_instance_.status_.has_nla)
ekk_instance_.debugNlaCheckInvert("Start of Highs::addRowsInterface",
kHighsDebugLevelExpensive + 1);
}
HighsStatus return_status = HighsStatus::kOk;
HighsOptions& options = options_;
if (ext_num_new_row < 0) return HighsStatus::kError;
if (ext_num_new_nz < 0) return HighsStatus::kError;
if (ext_num_new_row == 0) return HighsStatus::kOk;
if (ext_num_new_row > 0)
if (isRowDataNull(options.log_options, ext_row_lower, ext_row_upper))
return HighsStatus::kError;
if (ext_num_new_nz > 0)
if (isMatrixDataNull(options.log_options, ext_ar_start, ext_ar_index,
ext_ar_value))
return HighsStatus::kError;
HighsLp& lp = model_.lp_;
HighsBasis& basis = basis_;
HighsScale& scale = lp.scale_;
bool& useful_basis = basis.useful;
bool& lp_has_scaling = lp.scale_.has_scaling;
// Check that if nonzeros are to be added then the model has a positive number
// of columns
if (lp.num_col_ <= 0 && ext_num_new_nz > 0) return HighsStatus::kError;
// Record the new number of rows
HighsInt newNumRow = lp.num_row_ + ext_num_new_row;
HighsIndexCollection index_collection;
index_collection.dimension_ = ext_num_new_row;
index_collection.is_interval_ = true;
index_collection.from_ = 0;
index_collection.to_ = ext_num_new_row - 1;
// Take a copy of the bounds that can be normalised
std::vector<double> local_rowLower{ext_row_lower,
ext_row_lower + ext_num_new_row};
std::vector<double> local_rowUpper{ext_row_upper,
ext_row_upper + ext_num_new_row};
return_status = interpretCallStatus(
options_.log_options,
assessBounds(options, "Row", lp.num_row_, index_collection,
local_rowLower, local_rowUpper, options.infinite_bound),
return_status, "assessBounds");
if (return_status == HighsStatus::kError) return return_status;
if (lp.user_bound_scale_) {
// Assess and apply any user bound scaling
if (!boundScaleOk(local_rowLower, local_rowUpper, lp.user_bound_scale_,
options_.infinite_bound)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"User bound scaling yields infinite bound\n");
return HighsStatus::kError;
}
double bound_scale_value = std::pow(2, lp.user_bound_scale_);
for (HighsInt iRow = 0; iRow < ext_num_new_row; iRow++) {
local_rowLower[iRow] *= bound_scale_value;
local_rowUpper[iRow] *= bound_scale_value;
}
}
// Append the rows to the LP vectors
appendRowsToLpVectors(lp, ext_num_new_row, local_rowLower, local_rowUpper);
// Form a row-wise HighsSparseMatrix of the new matrix rows so that
// is easy to handle and, if there are nonzeros, it can be
// normalised
HighsSparseMatrix local_ar_matrix;
local_ar_matrix.num_col_ = lp.num_col_;
local_ar_matrix.num_row_ = ext_num_new_row;
local_ar_matrix.format_ = MatrixFormat::kRowwise;
if (ext_num_new_nz) {
local_ar_matrix.start_ = {ext_ar_start, ext_ar_start + ext_num_new_row};
local_ar_matrix.start_.resize(ext_num_new_row + 1);
local_ar_matrix.start_[ext_num_new_row] = ext_num_new_nz;
local_ar_matrix.index_ = {ext_ar_index, ext_ar_index + ext_num_new_nz};
local_ar_matrix.value_ = {ext_ar_value, ext_ar_value + ext_num_new_nz};
// Assess the matrix columns
return_status =
interpretCallStatus(options_.log_options,
local_ar_matrix.assess(options.log_options, "LP",
options.small_matrix_value,
options.large_matrix_value),
return_status, "assessMatrix");
if (return_status == HighsStatus::kError) return return_status;
} else {
// No nonzeros so, whether the constraint matrix is row-wise or
// column-wise, adding the empty matrix is trivial. Complete the
// setup of an empty row-wise HighsSparseMatrix of the new matrix
// rows
local_ar_matrix.start_.assign(ext_num_new_row + 1, 0);
}
// Append the rows to LP matrix
lp.a_matrix_.addRows(local_ar_matrix);
if (lp_has_scaling) {
// Extend the row scaling factors
scale.row.resize(newNumRow);
for (HighsInt iRow = 0; iRow < ext_num_new_row; iRow++)
scale.row[lp.num_row_ + iRow] = 1.0;
scale.num_row = newNumRow;
// Apply the existing column scaling to the new rows
local_ar_matrix.applyColScale(scale);
// Consider applying row scaling to the new rows.
local_ar_matrix.considerRowScaling(options.allowed_matrix_scale_factor,
&scale.row[lp.num_row_]);
}
// Update the basis corresponding to new basic rows
if (useful_basis) appendBasicRowsToBasisInterface(ext_num_new_row);
// Possibly add row names
lp.addRowNames("", ext_num_new_row);
// Increase the number of rows in the LP
lp.num_row_ += ext_num_new_row;
assert(lpDimensionsOk("addRows", lp, options.log_options));
// Deduce the consequences of adding new rows
invalidateModelStatusSolutionAndInfo();
// Determine any implications for simplex data
ekk_instance_.addRows(lp, local_ar_matrix);
return return_status;
}
static void deleteBasisEntries(std::vector<HighsBasisStatus>& status,
bool& deleted_basic, bool& deleted_nonbasic,
const HighsIndexCollection& index_collection,
const HighsInt entry_dim) {
assert(ok(index_collection));
assert(static_cast<size_t>(entry_dim) == status.size());
HighsInt from_k;
HighsInt to_k;
limits(index_collection, from_k, to_k);
if (from_k > to_k) return;
HighsInt delete_from_entry;
HighsInt delete_to_entry;
HighsInt keep_from_entry;
HighsInt keep_to_entry = -1;
HighsInt current_set_entry = 0;
HighsInt new_num_entry = 0;
deleted_basic = false;
deleted_nonbasic = false;
for (HighsInt k = from_k; k <= to_k; k++) {
updateOutInIndex(index_collection, delete_from_entry, delete_to_entry,
keep_from_entry, keep_to_entry, current_set_entry);
// Account for the initial entries being kept
if (k == from_k) new_num_entry = delete_from_entry;
// Identify whether a basic or a nonbasic entry has been deleted
for (HighsInt entry = delete_from_entry; entry <= delete_to_entry;
entry++) {
if (status[entry] == HighsBasisStatus::kBasic) {
deleted_basic = true;
} else {
deleted_nonbasic = true;
}
}
if (delete_to_entry >= entry_dim - 1) break;
for (HighsInt entry = keep_from_entry; entry <= keep_to_entry; entry++) {
status[new_num_entry] = status[entry];
new_num_entry++;
}
if (keep_to_entry >= entry_dim - 1) break;
}
status.resize(new_num_entry);
}
static void deleteBasisCols(HighsBasis& basis,
const HighsIndexCollection& index_collection,
const HighsInt original_num_col) {
bool deleted_basic;
bool deleted_nonbasic;
deleteBasisEntries(basis.col_status, deleted_basic, deleted_nonbasic,
index_collection, original_num_col);
if (deleted_basic) basis.valid = false;
}
static void deleteBasisRows(HighsBasis& basis,
const HighsIndexCollection& index_collection,
const HighsInt original_num_row) {
bool deleted_basic;
bool deleted_nonbasic;
deleteBasisEntries(basis.row_status, deleted_basic, deleted_nonbasic,
index_collection, original_num_row);
if (deleted_nonbasic) basis.valid = false;
}
void Highs::deleteColsInterface(HighsIndexCollection& index_collection) {
HighsLp& lp = model_.lp_;
HighsBasis& basis = basis_;
lp.ensureColwise();
// Keep a copy of the original number of columns to check whether
// any columns have been removed, and if there is mask to be updated
HighsInt original_num_col = lp.num_col_;
lp.deleteCols(index_collection);
model_.hessian_.deleteCols(index_collection);
// Bail out if no columns were actually deleted
if (lp.num_col_ == original_num_col) return;
assert(lp.num_col_ < original_num_col);
// Nontrivial deletion so reset the model_status and update any
// Highs basis
model_status_ = HighsModelStatus::kNotset;
if (basis_.useful) {
assert(basis_.col_status.size() == static_cast<size_t>(original_num_col));
// Have a full set of column basis status values, so maintain
// them, and only invalidate the basis if a basic column has been
// deleted
deleteBasisCols(basis_, index_collection, original_num_col);
} else {
assert(!basis.valid);
}
if (lp.scale_.has_scaling) {
deleteScale(lp.scale_.col, index_collection);
lp.scale_.col.resize(lp.num_col_);
lp.scale_.num_col = lp.num_col_;
}
// Deduce the consequences of deleting columns
invalidateModelStatusSolutionAndInfo();
// Determine any implications for simplex data
ekk_instance_.deleteCols(index_collection);
if (index_collection.is_mask_) {
// Set the mask values to indicate the new index value of the
// remaining columns
HighsInt new_col = 0;
for (HighsInt col = 0; col < original_num_col; col++) {
if (!index_collection.mask_[col]) {
index_collection.mask_[col] = new_col;
new_col++;
} else {
index_collection.mask_[col] = -1;
}
}
assert(new_col == lp.num_col_);
}
assert(lpDimensionsOk("deleteCols", lp, options_.log_options));
lp.col_hash_.name2index.clear();
}
void Highs::deleteRowsInterface(HighsIndexCollection& index_collection) {
HighsLp& lp = model_.lp_;
HighsBasis& basis = basis_;
lp.ensureColwise();
// Keep a copy of the original number of rows to check whether
// any rows have been removed, and if there is mask to be updated
HighsInt original_num_row = lp.num_row_;
lp.deleteRows(index_collection);
// Bail out if no rows were actually deleted
if (lp.num_row_ == original_num_row) return;
assert(lp.num_row_ < original_num_row);
// Nontrivial deletion so reset the model_status and update any
// Highs basis
model_status_ = HighsModelStatus::kNotset;
if (basis_.useful) {
assert(basis_.row_status.size() == static_cast<size_t>(original_num_row));
// Have a full set of row basis status values, so maintain them,
// and only invalidate the basis if a nonbasic row has been
// deleted
deleteBasisRows(basis_, index_collection, original_num_row);
} else {
assert(!basis.valid);
}
if (lp.scale_.has_scaling) {
deleteScale(lp.scale_.row, index_collection);
lp.scale_.row.resize(lp.num_row_);
lp.scale_.num_row = lp.num_row_;
}
// Deduce the consequences of deleting rows
invalidateModelStatusSolutionAndInfo();
// Determine any implications for simplex data
ekk_instance_.deleteRows(index_collection);
if (index_collection.is_mask_) {
HighsInt new_row = 0;
for (HighsInt row = 0; row < original_num_row; row++) {
if (!index_collection.mask_[row]) {
index_collection.mask_[row] = new_row;
new_row++;
} else {
index_collection.mask_[row] = -1;
}
}
assert(new_row == lp.num_row_);
}
assert(lpDimensionsOk("deleteRows", lp, options_.log_options));
lp.row_hash_.name2index.clear();
}
void Highs::getColsInterface(const HighsIndexCollection& index_collection,
HighsInt& num_col, double* cost, double* lower,
double* upper, HighsInt& num_nz, HighsInt* start,
HighsInt* index, double* value) const {
const HighsLp& lp = model_.lp_;
if (lp.a_matrix_.isColwise()) {
getSubVectors(index_collection, lp.num_col_, lp.col_cost_.data(),
lp.col_lower_.data(), lp.col_upper_.data(), lp.a_matrix_,
num_col, cost, lower, upper, num_nz, start, index, value);
} else {
getSubVectorsTranspose(index_collection, lp.num_col_, lp.col_cost_.data(),
lp.col_lower_.data(), lp.col_upper_.data(),
lp.a_matrix_, num_col, cost, lower, upper, num_nz,
start, index, value);
}
}
void Highs::getRowsInterface(const HighsIndexCollection& index_collection,
HighsInt& num_row, double* lower, double* upper,
HighsInt& num_nz, HighsInt* start, HighsInt* index,
double* value) const {
const HighsLp& lp = model_.lp_;
if (lp.a_matrix_.isColwise()) {
getSubVectorsTranspose(index_collection, lp.num_row_, nullptr,
lp.row_lower_.data(), lp.row_upper_.data(),
lp.a_matrix_, num_row, nullptr, lower, upper, num_nz,
start, index, value);
} else {
getSubVectors(index_collection, lp.num_row_, nullptr, lp.row_lower_.data(),
lp.row_upper_.data(), lp.a_matrix_, num_row, nullptr, lower,
upper, num_nz, start, index, value);
}
}
void Highs::getCoefficientInterface(const HighsInt ext_row,
const HighsInt ext_col,
double& value) const {
const HighsLp& lp = model_.lp_;
assert(0 <= ext_row && ext_row < lp.num_row_);
assert(0 <= ext_col && ext_col < lp.num_col_);
value = 0;
if (lp.a_matrix_.isColwise()) {
for (HighsInt el = lp.a_matrix_.start_[ext_col];
el < lp.a_matrix_.start_[ext_col + 1]; el++) {
if (lp.a_matrix_.index_[el] == ext_row) {
value = lp.a_matrix_.value_[el];
break;
}
}
} else {
for (HighsInt el = lp.a_matrix_.start_[ext_row];
el < lp.a_matrix_.start_[ext_row + 1]; el++) {
if (lp.a_matrix_.index_[el] == ext_col) {
value = lp.a_matrix_.value_[el];
break;
}
}
}
}
HighsStatus Highs::changeIntegralityInterface(
HighsIndexCollection& index_collection, const HighsVarType* integrality) {
HighsInt num_integrality = dataSize(index_collection);
// If a non-positive number of integrality (may) need changing nothing needs
// to be done
if (num_integrality <= 0) return HighsStatus::kOk;
if (highsVarTypeUserDataNotNull(options_.log_options, integrality,
"column integrality"))
return HighsStatus::kError;
// Take a copy of the integrality that can be normalised
std::vector<HighsVarType> local_integrality{integrality,
integrality + num_integrality};
// If changing the integrality for a set of columns, verify that the
// set entries are in ascending order
if (index_collection.is_set_)
assert(increasingSetOk(index_collection.set_, 0,
index_collection.dimension_, true));
changeLpIntegrality(model_.lp_, index_collection, local_integrality);
// Deduce the consequences of new integrality
invalidateModelStatus();
return HighsStatus::kOk;
}
HighsStatus Highs::changeCostsInterface(HighsIndexCollection& index_collection,
const double* cost) {
HighsInt num_cost = dataSize(index_collection);
// If a non-positive number of costs (may) need changing nothing needs to be
// done
if (num_cost <= 0) return HighsStatus::kOk;
if (doubleUserDataNotNull(options_.log_options, cost, "column costs"))
return HighsStatus::kError;
// Take a copy of the cost that can be normalised
std::vector<double> local_colCost{cost, cost + num_cost};
HighsStatus return_status = HighsStatus::kOk;
bool local_has_infinite_cost = false;
return_status = interpretCallStatus(
options_.log_options,
assessCosts(options_, 0, index_collection, local_colCost,
local_has_infinite_cost, options_.infinite_cost),
return_status, "assessCosts");
if (return_status == HighsStatus::kError) return return_status;
HighsLp& lp = model_.lp_;
if (lp.user_cost_scale_) {
// Assess and apply any user cost scaling
if (!costScaleOk(local_colCost, lp.user_cost_scale_,
options_.infinite_cost)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"User cost scaling yields infinite cost\n");
return HighsStatus::kError;
}
double cost_scale_value = std::pow(2, lp.user_cost_scale_);
for (HighsInt iCol = 0; iCol < num_cost; iCol++)
local_colCost[iCol] *= cost_scale_value;
}
changeLpCosts(lp, index_collection, local_colCost, options_.infinite_cost);
// Interpret possible introduction of infinite costs
lp.has_infinite_cost_ = lp.has_infinite_cost_ || local_has_infinite_cost;
assert(lp.has_infinite_cost_ == lp.hasInfiniteCost(options_.infinite_cost));
// Deduce the consequences of new costs
invalidateModelStatusSolutionAndInfo();
// Determine any implications for simplex data
ekk_instance_.updateStatus(LpAction::kNewCosts);
return HighsStatus::kOk;
}
HighsStatus Highs::changeColBoundsInterface(
HighsIndexCollection& index_collection, const double* col_lower,
const double* col_upper) {
HighsInt num_col_bounds = dataSize(index_collection);
// If a non-positive number of costs (may) need changing nothing needs to be
// done
if (num_col_bounds <= 0) return HighsStatus::kOk;
bool null_data = false;
null_data = doubleUserDataNotNull(options_.log_options, col_lower,
"column lower bounds") ||
null_data;
null_data = doubleUserDataNotNull(options_.log_options, col_upper,
"column upper bounds") ||
null_data;
if (null_data) return HighsStatus::kError;
// Take a copy of the cost that can be normalised
std::vector<double> local_colLower{col_lower, col_lower + num_col_bounds};
std::vector<double> local_colUpper{col_upper, col_upper + num_col_bounds};
// If changing the bounds for a set of columns, ensure that the
// set and data are in ascending order
if (index_collection.is_set_)
sortSetData(index_collection.set_num_entries_, index_collection.set_,
col_lower, col_upper, NULL, local_colLower.data(),