-
Notifications
You must be signed in to change notification settings - Fork 166
/
Highs.cpp
3330 lines (3127 loc) · 135 KB
/
Highs.cpp
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 */
/* */
/* Written and engineered 2008-2022 at the University of Edinburgh */
/* */
/* Available as open-source under the MIT License */
/* */
/* Authors: Julian Hall, Ivet Galabova, Leona Gottwald and Michael */
/* Feldmeier */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file lp_data/Highs.cpp
* @brief
*/
#include "Highs.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <sstream>
#include "io/Filereader.h"
#include "io/LoadOptions.h"
#include "lp_data/HighsInfoDebug.h"
#include "lp_data/HighsLpSolverObject.h"
#include "lp_data/HighsSolve.h"
#include "mip/HighsMipSolver.h"
#include "model/HighsHessianUtils.h"
#include "parallel/HighsParallel.h"
#include "presolve/ICrashX.h"
#include "qpsolver/quass.hpp"
#include "simplex/HSimplex.h"
#include "simplex/HSimplexDebug.h"
#include "util/HighsMatrixPic.h"
#include "util/HighsSort.h"
Highs::Highs() {}
HighsStatus Highs::clear() {
resetOptions();
return clearModel();
}
HighsStatus Highs::clearModel() {
model_.clear();
return clearSolver();
}
HighsStatus Highs::clearSolver() {
HighsStatus return_status = HighsStatus::kOk;
clearPresolve();
invalidateUserSolverData();
return returnFromHighs(return_status);
}
HighsStatus Highs::setOptionValue(const std::string& option, const bool value) {
if (setLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::setOptionValue(const std::string& option,
const HighsInt value) {
if (setLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::setOptionValue(const std::string& option,
const double value) {
if (setLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::setOptionValue(const std::string& option,
const std::string value) {
HighsLogOptions report_log_options = options_.log_options;
if (setLocalOptionValue(report_log_options, option, options_.log_options,
options_.records, value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::setOptionValue(const std::string& option,
const char* value) {
HighsLogOptions report_log_options = options_.log_options;
if (setLocalOptionValue(report_log_options, option, options_.log_options,
options_.records, value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::readOptions(const std::string filename) {
if (filename.size() <= 0) {
highsLogUser(options_.log_options, HighsLogType::kWarning,
"Empty file name so not reading options\n");
return HighsStatus::kWarning;
}
HighsLogOptions report_log_options = options_.log_options;
if (!loadOptionsFromFile(report_log_options, options_, filename))
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::passOptions(const HighsOptions& options) {
if (passLocalOptions(options_.log_options, options, options_) ==
OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getOptionValue(const std::string& option,
bool& value) const {
if (getLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getOptionValue(const std::string& option,
HighsInt& value) const {
if (getLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getOptionValue(const std::string& option,
double& value) const {
if (getLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getOptionValue(const std::string& option,
std::string& value) const {
if (getLocalOptionValue(options_.log_options, option, options_.records,
value) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getOptionType(const std::string& option,
HighsOptionType& type) const {
if (getLocalOptionType(options_.log_options, option, options_.records,
type) == OptionStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::resetOptions() {
resetLocalOptions(options_.records);
return HighsStatus::kOk;
}
HighsStatus Highs::writeOptions(const std::string filename,
const bool report_only_deviations) const {
HighsStatus return_status = HighsStatus::kOk;
FILE* file;
bool html;
return_status = interpretCallStatus(
options_.log_options, openWriteFile(filename, "writeOptions", file, html),
return_status, "openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
return_status = interpretCallStatus(
options_.log_options,
writeOptionsToFile(file, options_.records, report_only_deviations, html),
return_status, "writeOptionsToFile");
if (file != stdout) fclose(file);
return return_status;
}
HighsStatus Highs::getInfoValue(const std::string& info,
HighsInt& value) const {
InfoStatus status =
getLocalInfoValue(options_, info, info_.valid, info_.records, value);
if (status == InfoStatus::kOk) {
return HighsStatus::kOk;
} else if (status == InfoStatus::kUnavailable) {
return HighsStatus::kWarning;
} else {
return HighsStatus::kError;
}
}
#ifndef HIGHSINT64
HighsStatus Highs::getInfoValue(const std::string& info, int64_t& value) const {
InfoStatus status =
getLocalInfoValue(options_, info, info_.valid, info_.records, value);
if (status == InfoStatus::kOk) {
return HighsStatus::kOk;
} else if (status == InfoStatus::kUnavailable) {
return HighsStatus::kWarning;
} else {
return HighsStatus::kError;
}
}
#endif
HighsStatus Highs::getInfoValue(const std::string& info, double& value) const {
InfoStatus status =
getLocalInfoValue(options_, info, info_.valid, info_.records, value);
if (status == InfoStatus::kOk) {
return HighsStatus::kOk;
} else if (status == InfoStatus::kUnavailable) {
return HighsStatus::kWarning;
} else {
return HighsStatus::kError;
}
}
HighsStatus Highs::writeInfo(const std::string filename) const {
HighsStatus return_status = HighsStatus::kOk;
FILE* file;
bool html;
return_status = interpretCallStatus(
options_.log_options, openWriteFile(filename, "writeInfo", file, html),
return_status, "openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
return_status = interpretCallStatus(
options_.log_options,
writeInfoToFile(file, info_.valid, info_.records, html), return_status,
"writeInfoToFile");
if (file != stdout) fclose(file);
return return_status;
}
// Methods below change the incumbent model or solver infomation
// associated with it. Hence returnFromHighs is called at the end of
// each
HighsStatus Highs::passModel(HighsModel model) {
// This is the "master" Highs::passModel, in that all the others
// eventually call it
this->logHeader();
HighsStatus return_status = HighsStatus::kOk;
// Clear the incumbent model and any associated data
clearModel();
HighsLp& lp = model_.lp_;
HighsHessian& hessian = model_.hessian_;
// Move the model's LP and Hessian to the internal LP and Hessian
lp = std::move(model.lp_);
hessian = std::move(model.hessian_);
assert(lp.a_matrix_.formatOk());
if (lp.num_col_ == 0 || lp.num_row_ == 0) {
// Model constraint matrix has either no columns or no
// rows. Clearly the matrix is empty, so may have no orientation
// or starts assigned. HiGHS assumes that such a model will have
// null starts, so make it column-wise
lp.a_matrix_.format_ = MatrixFormat::kColwise;
lp.a_matrix_.start_.assign(lp.num_col_ + 1, 0);
lp.a_matrix_.index_.clear();
lp.a_matrix_.value_.clear();
} else {
// Matrix has rows and columns, so a_matrix format must be valid
if (!lp.a_matrix_.formatOk()) return HighsStatus::kError;
}
// Dimensions in a_matrix_ may not be set, so take them from lp.
lp.setMatrixDimensions();
// Residual scale factors may be present. ToDo Allow user-defined
// scale factors to be passed
assert(!lp.is_scaled_);
assert(!lp.is_moved_);
lp.resetScale();
// Check that the LP array dimensions are valid
if (!lpDimensionsOk("passModel", lp, options_.log_options))
return HighsStatus::kError;
// Check that the Hessian format is valid
if (!hessian.formatOk()) return HighsStatus::kError;
// Ensure that the LP is column-wise
lp.ensureColwise();
// Check validity of the LP, normalising its values
return_status = interpretCallStatus(
options_.log_options, assessLp(lp, options_), return_status, "assessLp");
if (return_status == HighsStatus::kError) return return_status;
// Check validity of any Hessian, normalising its entries
return_status = interpretCallStatus(options_.log_options,
assessHessian(hessian, options_),
return_status, "assessHessian");
if (return_status == HighsStatus::kError) return return_status;
if (hessian.dim_) {
// Clear any zero Hessian
if (hessian.numNz() == 0) {
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Hessian has dimension %" HIGHSINT_FORMAT
" but no nonzeros, so is ignored\n",
hessian.dim_);
hessian.clear();
}
}
// Clear solver status, solution, basis and info associated with any
// previous model; clear any HiGHS model object; create a HiGHS
// model object for this LP
return_status = interpretCallStatus(options_.log_options, clearSolver(),
return_status, "clearSolver");
return returnFromHighs(return_status);
}
HighsStatus Highs::passModel(HighsLp lp) {
HighsModel model;
model.lp_ = std::move(lp);
return passModel(std::move(model));
}
HighsStatus Highs::passModel(
const HighsInt num_col, const HighsInt num_row, const HighsInt a_num_nz,
const HighsInt q_num_nz, const HighsInt a_format, const HighsInt q_format,
const HighsInt sense, const double offset, const double* costs,
const double* col_lower, const double* col_upper, const double* row_lower,
const double* row_upper, const HighsInt* a_start, const HighsInt* a_index,
const double* a_value, const HighsInt* q_start, const HighsInt* q_index,
const double* q_value, const HighsInt* integrality) {
this->logHeader();
HighsModel model;
HighsLp& lp = model.lp_;
// Check that the formats of the constraint matrix and Hessian are valid
if (!aFormatOk(a_num_nz, a_format)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has illegal constraint matrix format\n");
return HighsStatus::kError;
}
if (!qFormatOk(q_num_nz, q_format)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has illegal Hessian matrix format\n");
return HighsStatus::kError;
}
const bool a_rowwise =
a_num_nz > 0 ? a_format == (HighsInt)MatrixFormat::kRowwise : false;
// if (num_nz) a_rowwise = a_format == (HighsInt)MatrixFormat::kRowwise;
lp.num_col_ = num_col;
lp.num_row_ = num_row;
if (num_col > 0) {
assert(costs != NULL);
assert(col_lower != NULL);
assert(col_upper != NULL);
lp.col_cost_.assign(costs, costs + num_col);
lp.col_lower_.assign(col_lower, col_lower + num_col);
lp.col_upper_.assign(col_upper, col_upper + num_col);
}
if (num_row > 0) {
assert(row_lower != NULL);
assert(row_upper != NULL);
lp.row_lower_.assign(row_lower, row_lower + num_row);
lp.row_upper_.assign(row_upper, row_upper + num_row);
}
if (a_num_nz > 0) {
assert(num_col > 0);
assert(num_row > 0);
assert(a_start != NULL);
assert(a_index != NULL);
assert(a_value != NULL);
if (a_rowwise) {
lp.a_matrix_.start_.assign(a_start, a_start + num_row);
} else {
lp.a_matrix_.start_.assign(a_start, a_start + num_col);
}
lp.a_matrix_.index_.assign(a_index, a_index + a_num_nz);
lp.a_matrix_.value_.assign(a_value, a_value + a_num_nz);
}
if (a_rowwise) {
lp.a_matrix_.start_.resize(num_row + 1);
lp.a_matrix_.start_[num_row] = a_num_nz;
lp.a_matrix_.format_ = MatrixFormat::kRowwise;
} else {
lp.a_matrix_.start_.resize(num_col + 1);
lp.a_matrix_.start_[num_col] = a_num_nz;
lp.a_matrix_.format_ = MatrixFormat::kColwise;
}
if (sense == (HighsInt)ObjSense::kMaximize) {
lp.sense_ = ObjSense::kMaximize;
} else {
lp.sense_ = ObjSense::kMinimize;
}
lp.offset_ = offset;
if (num_col > 0 && integrality != NULL) {
lp.integrality_.resize(num_col);
for (HighsInt iCol = 0; iCol < num_col; iCol++) {
HighsInt integrality_status = integrality[iCol];
const bool legal_integrality_status =
integrality_status == (HighsInt)HighsVarType::kContinuous ||
integrality_status == (HighsInt)HighsVarType::kInteger ||
integrality_status == (HighsInt)HighsVarType::kSemiContinuous ||
integrality_status == (HighsInt)HighsVarType::kSemiInteger;
if (!legal_integrality_status) {
highsLogDev(
options_.log_options, HighsLogType::kError,
"Model has illegal integer value of %d for integrality[%d]\n",
(int)integrality_status, iCol);
return HighsStatus::kError;
}
lp.integrality_[iCol] = (HighsVarType)integrality_status;
}
}
if (q_num_nz > 0) {
assert(num_col > 0);
assert(q_start != NULL);
assert(q_index != NULL);
assert(q_value != NULL);
HighsHessian& hessian = model.hessian_;
hessian.dim_ = num_col;
hessian.format_ = HessianFormat::kTriangular;
hessian.start_.assign(q_start, q_start + num_col);
hessian.start_.resize(num_col + 1);
hessian.start_[num_col] = q_num_nz;
hessian.index_.assign(q_index, q_index + q_num_nz);
hessian.value_.assign(q_value, q_value + q_num_nz);
}
return passModel(std::move(model));
}
HighsStatus Highs::passModel(const HighsInt num_col, const HighsInt num_row,
const HighsInt num_nz, const HighsInt a_format,
const HighsInt sense, const double offset,
const double* costs, const double* col_lower,
const double* col_upper, const double* row_lower,
const double* row_upper, const HighsInt* a_start,
const HighsInt* a_index, const double* a_value,
const HighsInt* integrality) {
return passModel(num_col, num_row, num_nz, 0, a_format, 0, sense, offset,
costs, col_lower, col_upper, row_lower, row_upper, a_start,
a_index, a_value, NULL, NULL, NULL, integrality);
}
HighsStatus Highs::passHessian(HighsHessian hessian_) {
this->logHeader();
HighsStatus return_status = HighsStatus::kOk;
HighsHessian& hessian = model_.hessian_;
hessian = std::move(hessian_);
// Check validity of any Hessian, normalising its entries
return_status = interpretCallStatus(options_.log_options,
assessHessian(hessian, options_),
return_status, "assessHessian");
if (return_status == HighsStatus::kError) return return_status;
if (hessian.dim_) {
// Clear any zero Hessian
if (hessian.numNz() == 0) {
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Hessian has dimension %" HIGHSINT_FORMAT
" but no nonzeros, so is ignored\n",
hessian.dim_);
hessian.clear();
}
}
return_status = interpretCallStatus(options_.log_options, clearSolver(),
return_status, "clearSolver");
return returnFromHighs(return_status);
}
HighsStatus Highs::passHessian(const HighsInt dim, const HighsInt num_nz,
const HighsInt format, const HighsInt* start,
const HighsInt* index, const double* value) {
this->logHeader();
HighsHessian hessian;
if (!qFormatOk(num_nz, format)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has illegal Hessian matrix format\n");
return HighsStatus::kError;
}
HighsInt num_col = model_.lp_.num_col_;
if (dim != num_col) return HighsStatus::kError;
hessian.dim_ = num_col;
hessian.format_ = HessianFormat::kTriangular;
if (dim > 0) {
assert(start != NULL);
hessian.start_.assign(start, start + num_col);
hessian.start_.resize(num_col + 1);
hessian.start_[num_col] = num_nz;
}
if (num_nz > 0) {
assert(index != NULL);
assert(value != NULL);
hessian.index_.assign(index, index + num_nz);
hessian.value_.assign(value, value + num_nz);
}
return passHessian(hessian);
}
HighsStatus Highs::readModel(const std::string filename) {
this->logHeader();
HighsStatus return_status = HighsStatus::kOk;
Filereader* reader =
Filereader::getFilereader(options_.log_options, filename);
if (reader == NULL) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model file %s not supported\n", filename.c_str());
return HighsStatus::kError;
}
HighsModel model;
FilereaderRetcode call_code =
reader->readModelFromFile(options_, filename, model);
delete reader;
if (call_code != FilereaderRetcode::kOk) {
interpretFilereaderRetcode(options_.log_options, filename.c_str(),
call_code);
return_status =
interpretCallStatus(options_.log_options, HighsStatus::kError,
return_status, "readModelFromFile");
if (return_status == HighsStatus::kError) return return_status;
}
model.lp_.model_name_ = extractModelName(filename);
const bool remove_rows_of_count_1 = false;
if (remove_rows_of_count_1) {
// .lp files from PWSC (notably st-test23.lp) have bounds for
// semi-continuous variables in the constraints section. By default,
// these are interpreted as constraints, so the semi-continuous
// variables are not set up correctly. Fix is to remove all rows of
// count 1, interpreting their bounds as bounds on the corresponding
// variable.
removeRowsOfCountOne(options_.log_options, model.lp_);
}
return_status =
interpretCallStatus(options_.log_options, passModel(std::move(model)),
return_status, "passModel");
return returnFromHighs(return_status);
}
HighsStatus Highs::readBasis(const std::string filename) {
this->logHeader();
HighsStatus return_status = HighsStatus::kOk;
// Try to read basis file into read_basis
HighsBasis read_basis = basis_;
return_status = interpretCallStatus(
options_.log_options,
readBasisFile(options_.log_options, read_basis, filename), return_status,
"readBasis");
if (return_status != HighsStatus::kOk) return return_status;
// Basis read OK: check whether it's consistent with the LP
if (!isBasisConsistent(model_.lp_, read_basis)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"readBasis: invalid basis\n");
return HighsStatus::kError;
}
// Update the HiGHS basis and invalidate any simplex basis for the model
basis_ = read_basis;
basis_.valid = true;
// Follow implications of a new HiGHS basis
newHighsBasis();
// Can't use returnFromHighs since...
return HighsStatus::kOk;
}
HighsStatus Highs::writeModel(const std::string filename) {
HighsStatus return_status = HighsStatus::kOk;
// Ensure that the LP is column-wise
model_.lp_.ensureColwise();
if (filename == "") {
// Empty file name: report model on logging stream
reportModel();
return_status = HighsStatus::kOk;
} else {
Filereader* writer =
Filereader::getFilereader(options_.log_options, filename);
if (writer == NULL) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model file %s not supported\n", filename.c_str());
return HighsStatus::kError;
}
return_status = interpretCallStatus(
options_.log_options,
writer->writeModelToFile(options_, filename, model_), return_status,
"writeModelToFile");
delete writer;
}
return returnFromHighs(return_status);
}
HighsStatus Highs::writeBasis(const std::string filename) {
HighsStatus return_status = HighsStatus::kOk;
HighsStatus call_status;
FILE* file;
bool html;
call_status = openWriteFile(filename, "writebasis", file, html);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
writeBasisFile(file, basis_);
if (file != stdout) fclose(file);
return returnFromHighs(return_status);
}
HighsStatus Highs::presolve() {
HighsStatus return_status = HighsStatus::kOk;
clearPresolve();
if (model_.isEmpty()) {
model_presolve_status_ = HighsPresolveStatus::kNotReduced;
} else {
const bool force_presolve = true;
// make sure global scheduler is initialized before calling presolve, since
// MIP presolve may use parallelism
highs::parallel::initialize_scheduler(options_.threads);
max_threads = highs::parallel::num_threads();
if (options_.threads != 0 && max_threads != options_.threads) {
highsLogUser(
options_.log_options, HighsLogType::kError,
"Option 'threads' is set to %d but global scheduler has already been "
"initialized to use %d threads. The previous scheduler instance can "
"be destroyed by calling Highs::resetGlobalScheduler().\n",
(int)options_.threads, max_threads);
return HighsStatus::kError;
}
model_presolve_status_ = runPresolve(force_presolve);
}
bool using_reduced_lp = false;
switch (model_presolve_status_) {
case HighsPresolveStatus::kNotPresolved: {
// Shouldn't happen
assert(model_presolve_status_ != HighsPresolveStatus::kNotPresolved);
return_status = HighsStatus::kError;
break;
}
case HighsPresolveStatus::kNotReduced:
case HighsPresolveStatus::kInfeasible:
case HighsPresolveStatus::kReduced:
case HighsPresolveStatus::kReducedToEmpty:
case HighsPresolveStatus::kUnboundedOrInfeasible: {
// All OK
if (model_presolve_status_ == HighsPresolveStatus::kInfeasible) {
// Infeasible model, so indicate that the incumbent model is
// known as such
setHighsModelStatusAndClearSolutionAndBasis(
HighsModelStatus::kInfeasible);
} else if (model_presolve_status_ == HighsPresolveStatus::kNotReduced) {
// No reduction, so fill Highs presolved model with the
// incumbent model
presolved_model_ = model_;
} else if (model_presolve_status_ == HighsPresolveStatus::kReduced) {
// Nontrivial reduction, so fill Highs presolved model with the
// presolved model
using_reduced_lp = true;
}
return_status = HighsStatus::kOk;
break;
}
case HighsPresolveStatus::kTimeout: {
// Timeout, so assume that it's OK to fill the Highs presolved model with
// the presolved model, but return warning.
using_reduced_lp = true;
return_status = HighsStatus::kWarning;
break;
}
default: {
// case HighsPresolveStatus::kError
setHighsModelStatusAndClearSolutionAndBasis(
HighsModelStatus::kPresolveError);
return_status = HighsStatus::kError;
}
}
if (using_reduced_lp) {
presolved_model_.lp_ = presolve_.getReducedProblem();
presolved_model_.lp_.setMatrixDimensions();
}
highsLogUser(
options_.log_options, HighsLogType::kInfo, "Presolve status: %s\n",
presolve_.presolveStatusToString(model_presolve_status_).c_str());
return returnFromHighs(return_status);
}
// Checks the options calls presolve and postsolve if needed. Solvers are called
// with callSolveLp(..)
HighsStatus Highs::run() {
HighsInt min_highs_debug_level = kHighsDebugLevelMin;
// kHighsDebugLevelCostly;
// kHighsDebugLevelMax;
//
// if (model_.lp_.num_row_>0 && model_.lp_.num_col_>0)
// writeLpMatrixPicToFile(options_, "LpMatrix", model_.lp_);
if (options_.highs_debug_level < min_highs_debug_level)
options_.highs_debug_level = min_highs_debug_level;
const bool possibly_use_log_dev_level_2 = false;
const HighsInt log_dev_level = options_.log_dev_level;
const bool output_flag = options_.output_flag;
HighsInt use_log_dev_level = log_dev_level;
bool use_output_flag = output_flag;
const HighsInt check_debug_run_call_num = -103757;
const HighsInt check_num_col = -317;
const HighsInt check_num_row = -714;
if (possibly_use_log_dev_level_2) {
if (this->debug_run_call_num_ == check_debug_run_call_num &&
model_.lp_.num_col_ == check_num_col &&
model_.lp_.num_row_ == check_num_row) {
std::string message =
"Entering Highs::run(): run/col/row matching check ";
highsLogDev(options_.log_options, HighsLogType::kInfo,
"%s: run %d: LP(%6d, %6d)\n", message.c_str(),
(int)this->debug_run_call_num_, (int)model_.lp_.num_col_,
(int)model_.lp_.num_row_);
// highsPause(true, message);
use_log_dev_level = 2;
use_output_flag = true;
}
}
if (ekk_instance_.status_.has_nla)
assert(ekk_instance_.lpFactorRowCompatible(model_.lp_.num_row_));
highs::parallel::initialize_scheduler(options_.threads);
max_threads = highs::parallel::num_threads();
if (options_.threads != 0 && max_threads != options_.threads) {
highsLogUser(
options_.log_options, HighsLogType::kError,
"Option 'threads' is set to %d but global scheduler has already been "
"initialized to use %d threads. The previous scheduler instance can "
"be destroyed by calling Highs::resetGlobalScheduler().\n",
(int)options_.threads, max_threads);
return HighsStatus::kError;
}
assert(max_threads > 0);
if (max_threads <= 0)
highsLogDev(options_.log_options, HighsLogType::kWarning,
"WARNING: max_threads() returns %" HIGHSINT_FORMAT "\n",
max_threads);
highsLogDev(options_.log_options, HighsLogType::kDetailed,
"Running with %" HIGHSINT_FORMAT " thread(s)\n", max_threads);
assert(called_return_from_run);
if (!called_return_from_run) {
highsLogDev(options_.log_options, HighsLogType::kError,
"Highs::run() called with called_return_from_run false\n");
return HighsStatus::kError;
}
// Ensure that all vectors in the model have exactly the right size
exactResizeModel();
// Set this so that calls to returnFromRun() can be checked
called_return_from_run = false;
// From here all return statements execute returnFromRun()
HighsStatus return_status = HighsStatus::kOk;
HighsStatus call_status;
// Initialise the HiGHS model status
model_status_ = HighsModelStatus::kNotset;
// Clear the run info
invalidateInfo();
// Zero the iteration counts
zeroIterationCounts();
// Start the HiGHS run clock
timer_.startRunHighsClock();
// Return immediately if the model has no columns
if (!model_.lp_.num_col_) {
setHighsModelStatusAndClearSolutionAndBasis(HighsModelStatus::kModelEmpty);
return returnFromRun(HighsStatus::kOk);
}
// Return immediately if the model is infeasible due to inconsistent bounds
if (isBoundInfeasible(options_.log_options, model_.lp_)) {
setHighsModelStatusAndClearSolutionAndBasis(HighsModelStatus::kInfeasible);
return returnFromRun(return_status);
}
// Ensure that the LP (and any simplex LP) has the matrix column-wise
model_.lp_.ensureColwise();
// Ensure that the matrix has no large values
if (model_.lp_.a_matrix_.hasLargeValue(options_.large_matrix_value)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot solve a model with a |value| exceeding %g in "
"constraint matrix\n",
options_.large_matrix_value);
return returnFromRun(HighsStatus::kError);
}
if (options_.highs_debug_level > min_highs_debug_level) {
// Shouldn't have to check validity of the LP since this is done when it is
// loaded or modified
call_status = assessLp(model_.lp_, options_);
// If any errors have been found or normalisation carried out,
// call_status will be kError or kWarning, so only valid return is OK.
assert(call_status == HighsStatus::kOk);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "assessLp");
if (return_status == HighsStatus::kError)
return returnFromRun(return_status);
// Shouldn't have to check that the options settings are legal,
// since they are checked when modified
if (checkOptions(options_.log_options, options_.records) !=
OptionStatus::kOk) {
return_status = HighsStatus::kError;
return returnFromRun(return_status);
}
}
if (model_.lp_.model_name_.compare(""))
highsLogDev(options_.log_options, HighsLogType::kVerbose,
"Solving model: %s\n", model_.lp_.model_name_.c_str());
// Check validity of any integrality, keeping a record of any upper
// bound modifications for semi-variables
call_status = assessIntegrality(model_.lp_, options_);
if (call_status == HighsStatus::kError) {
setHighsModelStatusAndClearSolutionAndBasis(HighsModelStatus::kSolveError);
return returnFromRun(HighsStatus::kError);
}
if (!options_.solver.compare(kHighsChooseString) && model_.isQp()) {
// Choosing method according to model class, and model is a QP
//
// Ensure that it's not MIQP!
if (model_.isMip()) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot solve MIQP problems with HiGHS\n");
return returnFromRun(HighsStatus::kError);
}
// Ensure that its diagonal entries are OK in the context of the
// objective sense. It's OK to be semi-definite
if (!okHessianDiagonal(options_, model_.hessian_, model_.lp_.sense_)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot solve non-convex QP problems with HiGHS\n");
return returnFromRun(HighsStatus::kError);
}
call_status = callSolveQp();
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "callSolveQp");
return returnFromRun(return_status);
}
if (!options_.solver.compare(kHighsChooseString) && model_.isMip()) {
// Choosing method according to model class, and model is a MIP
call_status = callSolveMip();
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "callSolveMip");
return returnFromRun(return_status);
}
// Solve the model as an LP
HighsLp& incumbent_lp = model_.lp_;
HighsLogOptions& log_options = options_.log_options;
bool no_incumbent_lp_solution_or_basis = false;
//
// Record the initial time and set the component times and postsolve
// iteration count to -1 to identify whether they are not required
double initial_time = timer_.readRunHighsClock();
double this_presolve_time = -1;
double this_solve_presolved_lp_time = -1;
double this_postsolve_time = -1;
double this_solve_original_lp_time = -1;
HighsInt postsolve_iteration_count = -1;
const bool ipx_no_crossover =
options_.solver == kIpmString && !options_.run_crossover;
if (options_.icrash) {
ICrashStrategy strategy = ICrashStrategy::kICA;
bool strategy_ok = parseICrashStrategy(options_.icrash_strategy, strategy);
if (!strategy_ok) {
// std::cout << "ICrash error: unknown strategy." << std::endl;
highsLogUser(options_.log_options, HighsLogType::kError,
"ICrash error: unknown strategy.\n");
return HighsStatus::kError;
}
ICrashOptions icrash_options{
options_.icrash_dualize, strategy,
options_.icrash_starting_weight, options_.icrash_iterations,
options_.icrash_approx_iter, options_.icrash_exact,
options_.icrash_breakpoints, options_.log_options};
HighsStatus icrash_status =
callICrash(model_.lp_, icrash_options, icrash_info_);
if (icrash_status != HighsStatus::kOk) return returnFromRun(icrash_status);
// for now set the solution_.col_value
solution_.col_value = icrash_info_.x_values;
// Better not to use Highs::crossover
const bool use_highs_crossover = false;
if (use_highs_crossover) {
crossover(solution_);
// loops:
called_return_from_run = true;
options_.icrash = false; // to avoid loop
} else {
HighsStatus crossover_status = callCrossover(
options_, model_.lp_, basis_, solution_, model_status_, info_);
// callCrossover can return HighsStatus::kWarning due to
// imprecise dual values. Ignore this since primal simplex will
// be called to clean up duals
highsLogUser(log_options, HighsLogType::kInfo,
"Crossover following iCrash has return status of %s, and "
"problem status is %s\n",
highsStatusToString(crossover_status).c_str(),
modelStatusToString(model_status_).c_str());
if (crossover_status == HighsStatus::kError)
return returnFromRun(crossover_status);
assert(options_.simplex_strategy == kSimplexStrategyPrimal);
}
// timer_.stopRunHighsClock();
// run();
// todo: add "dual" values
// return HighsStatus::kOk;
}
if (!basis_.valid && solution_.value_valid) {
// There is no valid basis, but there is a valid solution, so use
// it to construct a basis
return_status =
interpretCallStatus(options_.log_options, basisForSolution(),
return_status, "basisForSolution");
if (return_status == HighsStatus::kError)
return returnFromRun(return_status);
assert(basis_.valid);
}
if (basis_.valid || options_.presolve == kHighsOffString) {
// There is a valid basis for the problem or presolve is off
ekk_instance_.lp_name_ = "LP without presolve or with basis";
// If there is a valid HiGHS basis, refine any status values that
// are simply HighsBasisStatus::kNonbasic
if (basis_.valid) refineBasis(incumbent_lp, solution_, basis_);
this_solve_original_lp_time = -timer_.read(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = use_log_dev_level;
options_.output_flag = use_output_flag;
}
timer_.start(timer_.solve_clock);
call_status =
callSolveLp(incumbent_lp, "Solving LP without presolve or with basis");
timer_.stop(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = log_dev_level;
options_.output_flag = output_flag;
}
this_solve_original_lp_time += timer_.read(timer_.solve_clock);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "callSolveLp");
if (return_status == HighsStatus::kError)
return returnFromRun(return_status);
} else {
// No HiGHS basis so consider presolve
//
// If using IPX to solve the reduced LP, but not crossover, set
// lp_presolve_requires_basis_postsolve so that presolve can use
// rules for which postsolve does not generate a basis.
const bool lp_presolve_requires_basis_postsolve =
options_.lp_presolve_requires_basis_postsolve;
if (ipx_no_crossover) options_.lp_presolve_requires_basis_postsolve = false;
// Possibly presolve - according to option_.presolve
const double from_presolve_time = timer_.read(timer_.presolve_clock);
this_presolve_time = -from_presolve_time;
timer_.start(timer_.presolve_clock);
model_presolve_status_ = runPresolve();
timer_.stop(timer_.presolve_clock);
const double to_presolve_time = timer_.read(timer_.presolve_clock);
this_presolve_time += to_presolve_time;
presolve_.info_.presolve_time = this_presolve_time;
// Recover any modified options
options_.lp_presolve_requires_basis_postsolve =
lp_presolve_requires_basis_postsolve;
// Set an illegal local pivot threshold value that's updated after
// solving the presolved LP - if simplex is used
double factor_pivot_threshold = -1;
// Run solver.
bool have_optimal_solution = false;
// ToDo Put solution of presolved problem in a separate method
switch (model_presolve_status_) {
case HighsPresolveStatus::kNotPresolved: {
ekk_instance_.lp_name_ = "Original LP";
this_solve_original_lp_time = -timer_.read(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = use_log_dev_level;
options_.output_flag = use_output_flag;
}
timer_.start(timer_.solve_clock);
call_status =
callSolveLp(incumbent_lp, "Not presolved: solving the LP");
timer_.stop(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = log_dev_level;
options_.output_flag = output_flag;
}
this_solve_original_lp_time += timer_.read(timer_.solve_clock);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "callSolveLp");
if (return_status == HighsStatus::kError)
return returnFromRun(return_status);
break;
}
case HighsPresolveStatus::kNotReduced: {
ekk_instance_.lp_name_ = "Unreduced LP";
// Log the presolve reductions
reportPresolveReductions(log_options, incumbent_lp, false);
this_solve_original_lp_time = -timer_.read(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = use_log_dev_level;
options_.output_flag = use_output_flag;
}
timer_.start(timer_.solve_clock);
call_status = callSolveLp(
incumbent_lp, "Problem not reduced by presolve: solving the LP");
timer_.stop(timer_.solve_clock);
if (possibly_use_log_dev_level_2) {
options_.log_dev_level = log_dev_level;