-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathHighs.cpp
More file actions
4903 lines (4599 loc) · 203 KB
/
Highs.cpp
File metadata and controls
4903 lines (4599 loc) · 203 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/Highs.cpp
* @brief
*/
#include "Highs.h"
#include <algorithm>
#include <cassert>
#include <csignal>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include "io/Filereader.h"
#include "io/LoadOptions.h"
#include "lp_data/HighsCallbackStruct.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/a_quass.hpp"
#include "qpsolver/runtime.hpp"
#include "simplex/HSimplex.h"
#include "simplex/HSimplexDebug.h"
#include "util/HighsMatrixPic.h"
#include "util/HighsSort.h"
#define STRINGFY(s) STRINGFY0(s)
#define STRINGFY0(s) #s
const char* highsVersion() {
return STRINGFY(HIGHS_VERSION_MAJOR) "." STRINGFY(
HIGHS_VERSION_MINOR) "." STRINGFY(HIGHS_VERSION_PATCH);
}
HighsInt highsVersionMajor() { return HIGHS_VERSION_MAJOR; }
HighsInt highsVersionMinor() { return HIGHS_VERSION_MINOR; }
HighsInt highsVersionPatch() { return HIGHS_VERSION_PATCH; }
const char* highsGithash() { return HIGHS_GITHASH; }
const char* highsCompilationDate() { return "deprecated"; }
Highs::Highs() : callback_(this) {}
HighsStatus Highs::clear() {
resetOptions();
return clearModel();
}
HighsStatus Highs::clearModel() {
model_.clear();
multi_linear_objective_.clear();
return clearSolver();
}
HighsStatus Highs::clearSolver() {
HighsStatus return_status = HighsStatus::kOk;
clearDerivedModelProperties();
invalidateSolverData();
return returnFromHighs(return_status);
}
HighsStatus Highs::clearSolverDualData() {
HighsStatus return_status = HighsStatus::kOk;
clearDerivedModelProperties();
invalidateSolverDualData();
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 optionChangeAction();
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 optionChangeAction();
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 optionChangeAction();
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 optionChangeAction();
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 optionChangeAction();
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;
switch (loadOptionsFromFile(report_log_options, options_, filename)) {
case HighsLoadOptionsStatus::kError:
case HighsLoadOptionsStatus::kEmpty:
return HighsStatus::kError;
default:
break;
}
return optionChangeAction();
}
HighsStatus Highs::passOptions(const HighsOptions& options) {
if (passLocalOptions(options_.log_options, options, options_) ==
OptionStatus::kOk)
return optionChangeAction();
return HighsStatus::kError;
}
HighsStatus Highs::resetOptions() {
resetLocalOptions(options_.records);
return optionChangeAction();
}
HighsStatus Highs::writeOptions(const std::string& filename,
const bool report_only_deviations) {
this->logHeader();
HighsStatus return_status = HighsStatus::kOk;
FILE* file;
HighsFileType file_type;
return_status = interpretCallStatus(
options_.log_options,
openWriteFile(filename, "writeOptions", file, file_type), return_status,
"openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
if (filename == "") file_type = HighsFileType::kMinimal;
// Report to user that options are being written to a file
if (filename != "")
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Writing the option values to %s\n", filename.c_str());
return_status = interpretCallStatus(
options_.log_options,
writeOptionsToFile(file, options_.log_options, options_.records,
report_only_deviations, file_type),
return_status, "writeOptionsToFile");
if (file != stdout) fclose(file);
return return_status;
}
// HighsStatus Highs::getOptionType(const char* option, HighsOptionType* type)
// const { return getOptionType(option, type);}
HighsStatus Highs::getOptionName(const HighsInt index,
std::string* name) const {
if (index < 0 || index >= HighsInt(this->options_.records.size()))
return HighsStatus::kError;
*name = this->options_.records[index]->name;
return HighsStatus::kOk;
}
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::getBoolOptionValues(const std::string& option,
bool* current_value,
bool* default_value) const {
if (getLocalOptionValues(options_.log_options, option, options_.records,
current_value, default_value) != OptionStatus::kOk)
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::getIntOptionValues(const std::string& option,
HighsInt* current_value,
HighsInt* min_value, HighsInt* max_value,
HighsInt* default_value) const {
if (getLocalOptionValues(options_.log_options, option, options_.records,
current_value, min_value, max_value,
default_value) != OptionStatus::kOk)
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::getDoubleOptionValues(const std::string& option,
double* current_value,
double* min_value, double* max_value,
double* default_value) const {
if (getLocalOptionValues(options_.log_options, option, options_.records,
current_value, min_value, max_value,
default_value) != OptionStatus::kOk)
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::getStringOptionValues(const std::string& option,
std::string* current_value,
std::string* default_value) const {
if (getLocalOptionValues(options_.log_options, option, options_.records,
current_value, default_value) != OptionStatus::kOk)
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::getInfoValue(const std::string& info,
HighsInt& value) const {
InfoStatus status = getLocalInfoValue(options_.log_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_.log_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::getInfoType(const std::string& info,
HighsInfoType& type) const {
if (getLocalInfoType(options_.log_options, info, info_.records, type) ==
InfoStatus::kOk)
return HighsStatus::kOk;
return HighsStatus::kError;
}
HighsStatus Highs::getInfoValue(const std::string& info, double& value) const {
InfoStatus status = getLocalInfoValue(options_.log_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;
HighsFileType file_type;
return_status =
interpretCallStatus(options_.log_options,
openWriteFile(filename, "writeInfo", file, file_type),
return_status, "openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
// Report to user that options are being written to a file
if (filename != "")
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Writing the info values to %s\n", filename.c_str());
return_status = interpretCallStatus(
options_.log_options,
writeInfoToFile(file, info_.valid, info_.records, file_type),
return_status, "writeInfoToFile");
if (file != stdout) fclose(file);
return return_status;
}
/**
* @brief Get the size of HighsInt
*/
// HighsInt getSizeofHighsInt() {
// Methods below change the incumbent model or solver information
// 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
// (and readModel) eventually call it
this->logHeader();
// Possibly analyse the LP data
if (kHighsAnalysisLevelModelData & options_.highs_analysis_level)
analyseLp(options_.log_options, model.lp_);
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_);
lp.origin_name_ = "Original";
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
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Model has either no columns or no rows, so ignoring user "
"constraint matrix data and initialising empty matrix\n");
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;
// 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;
// Now legality of matrix is established, ensure that it is
// column-wise
lp.ensureColwise();
// 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();
}
}
// Ensure that any non-zero Hessian of dimension less than the
// number of columns in the model is completed
if (hessian.dim_) completeHessian(this->model_.lp_.num_col_, hessian);
// if (model_.lp_.num_row_>0 && model_.lp_.num_col_>0)
// writeLpMatrixPicToFile(options_, "LpMatrix", model_.lp_);
// 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) ||
integrality_status == HighsInt(HighsVarType::kImplicitInteger);
if (!legal_integrality_status) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has illegal integer value of %d (type %s) for "
"integrality[%d]\n",
int(integrality_status),
highsVarTypeToString(integrality_status).c_str(), 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();
}
}
// Ensure that any non-zero Hessian of dimension less than the
// number of columns in the model is completed
if (hessian.dim_) completeHessian(this->model_.lp_.num_col_, hessian);
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::passLinearObjectives(
const HighsInt num_linear_objective,
const HighsLinearObjective* linear_objective) {
if (num_linear_objective < 0) return HighsStatus::kOk;
this->multi_linear_objective_.clear();
for (HighsInt iObj = 0; iObj < num_linear_objective; iObj++)
if (this->addLinearObjective(linear_objective[iObj], iObj) !=
HighsStatus::kOk)
return HighsStatus::kError;
return HighsStatus::kOk;
}
HighsStatus Highs::addLinearObjective(
const HighsLinearObjective& linear_objective, const HighsInt iObj) {
if (model_.isQp()) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot define additional linear objective for QP\n");
return HighsStatus::kError;
}
if (!this->validLinearObjective(linear_objective, iObj))
return HighsStatus::kError;
this->multi_linear_objective_.push_back(linear_objective);
return HighsStatus::kOk;
}
HighsStatus Highs::clearLinearObjectives() {
this->multi_linear_objective_.clear();
return HighsStatus::kOk;
}
HighsStatus Highs::passColName(const HighsInt col, const std::string& name) {
const HighsInt num_col = this->model_.lp_.num_col_;
if (col < 0 || col >= num_col) {
highsLogUser(
options_.log_options, HighsLogType::kError,
"Index %d for column name %s is outside the range [0, num_col = %d)\n",
int(col), name.c_str(), int(num_col));
return HighsStatus::kError;
}
if (int(name.length()) <= 0) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot define empty column names\n");
return HighsStatus::kError;
}
this->model_.lp_.col_names_.resize(num_col);
this->model_.lp_.col_hash_.update(col, this->model_.lp_.col_names_[col],
name);
this->model_.lp_.col_names_[col] = name;
return HighsStatus::kOk;
}
HighsStatus Highs::passRowName(const HighsInt row, const std::string& name) {
const HighsInt num_row = this->model_.lp_.num_row_;
if (row < 0 || row >= num_row) {
highsLogUser(
options_.log_options, HighsLogType::kError,
"Index %d for row name %s is outside the range [0, num_row = %d)\n",
int(row), name.c_str(), int(num_row));
return HighsStatus::kError;
}
if (int(name.length()) <= 0) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Cannot define empty column names\n");
return HighsStatus::kError;
}
this->model_.lp_.row_names_.resize(num_row);
this->model_.lp_.row_hash_.update(row, this->model_.lp_.row_names_[row],
name);
this->model_.lp_.row_names_[row] = name;
return HighsStatus::kOk;
}
HighsStatus Highs::passModelName(const std::string& name) {
this->model_.lp_.model_name_ = name;
return HighsStatus::kOk;
}
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);
const HighsStatus call_status = call_code == FilereaderRetcode::kWarning
? HighsStatus::kWarning
: HighsStatus::kError;
return_status = interpretCallStatus(options_.log_options, call_status,
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, model_.lp_, 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;
basis_.useful = true;
// Follow implications of a new HiGHS basis
newHighsBasis();
// Can't use returnFromHighs since...
return HighsStatus::kOk;
}
HighsStatus Highs::writeModel(const std::string& filename) {
return writeLocalModel(model_, filename);
}
HighsStatus Highs::writePresolvedModel(const std::string& filename) {
return writeLocalModel(presolved_model_, filename);
}
HighsStatus Highs::writeIisModel(const std::string& filename) {
HighsStatus return_status = this->getIisInterface();
if (return_status == HighsStatus::kError) return return_status;
return writeLocalModel(iis_.model_, filename);
}
HighsStatus Highs::writeLocalModel(HighsModel& model,
const std::string& filename) {
HighsStatus return_status = HighsStatus::kOk;
HighsStatus call_status;
HighsLp& lp = model.lp_;
// Dimensions in a_matrix_ may not be set, so take them from lp
lp.setMatrixDimensions();
// Replace any blank names and return error if there are duplicates
// or names with spaces
call_status = normaliseNames(this->options_.log_options, lp);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "normaliseNames");
if (return_status == HighsStatus::kError) return return_status;
// Ensure that the LP is column-wise
lp.ensureColwise();
// Ensure that the dimensions are OK
if (!lpDimensionsOk("writeLocalModel", lp, options_.log_options))
return HighsStatus::kError;
if (model.hessian_.dim_ > 0) {
call_status = assessHessianDimensions(options_, model.hessian_);
if (call_status == HighsStatus::kError) return call_status;
}
// Check that the matrix starts are OK
call_status = lp.a_matrix_.assessStart(options_.log_options);
if (call_status == HighsStatus::kError) return call_status;
// Check that the matrix indices are within bounds
call_status = lp.a_matrix_.assessIndexBounds(options_.log_options);
if (call_status == HighsStatus::kError) return call_status;
// Check for repeated column or row names that would corrupt the file
if (model.lp_.col_hash_.hasDuplicate(model.lp_.col_names_)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has repeated column names\n");
return returnFromHighs(HighsStatus::kError);
}
if (model.lp_.row_hash_.hasDuplicate(model.lp_.row_names_)) {
highsLogUser(options_.log_options, HighsLogType::kError,
"Model has repeated row names\n");
return returnFromHighs(HighsStatus::kError);
}
if (filename == "") {
// Empty file name: report model on logging stream
reportModel(model);
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;
}
// Report to user that model is being written
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Writing the model to %s\n", filename.c_str());
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;
HighsFileType file_type;
call_status = openWriteFile(filename, "writeBasis", file, file_type);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "openWriteFile");
if (return_status == HighsStatus::kError) return return_status;
// Replace any blank names and return error if there are duplicates
// or names with spaces
call_status = normaliseNames(this->options_.log_options, this->model_.lp_);
return_status = interpretCallStatus(options_.log_options, call_status,
return_status, "normaliseNames");
if (return_status == HighsStatus::kError) return return_status;
// Report to user that basis is being written
if (filename != "")
highsLogUser(options_.log_options, HighsLogType::kInfo,
"Writing the basis to %s\n", filename.c_str());
writeBasisFile(file, options_, model_.lp_, basis_);
if (file != stdout) fclose(file);
return return_status;
}
HighsStatus Highs::presolve() {
const HighsLogOptions& log_options = options_.log_options;
if (model_.needsMods(options_.infinite_cost)) {
highsLogUser(log_options, HighsLogType::kError,
"Model contains infinite costs or semi-variables, so cannot "
"be presolved independently\n");
return HighsStatus::kError;
}
HighsStatus return_status = HighsStatus::kOk;
this->reportModelStats();
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(
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;
}
// If problem is a MIP and solve_relaxation is true, it's natural
// to force LP presolve. It means that someone wanting the
// presolved relaxation of a MIP just has to set solve_relaxation
// to true, rather than clearing the integrality of the model (cf
// #2234)
const bool force_lp_presolve = options_.solve_relaxation;
model_presolve_status_ = runPresolve(force_lp_presolve, force_presolve);
}
bool using_reduced_lp = false;
reportPresolveReductions(log_options, model_presolve_status_, model_.lp_,
presolve_.getReducedProblem());
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 ||
model_presolve_status_ ==
HighsPresolveStatus::kReducedToEmpty) {
// 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::kOutOfMemory
assert(model_presolve_status_ == HighsPresolveStatus::kOutOfMemory);
highsLogUser(log_options, HighsLogType::kError,
"Presolve fails due to memory allocation error\n");
setHighsModelStatusAndClearSolutionAndBasis(
HighsModelStatus::kPresolveError);
return_status = HighsStatus::kError;
}
}
if (using_reduced_lp) {
presolved_model_.lp_ = presolve_.getReducedProblem();
presolved_model_.lp_.setMatrixDimensions();
}
highsLogUser(log_options, HighsLogType::kInfo, "Presolve status: %s\n",
presolveStatusToString(model_presolve_status_).c_str());
return returnFromHighs(return_status);
}
HighsStatus Highs::run() {
// Level 0 of Highs::run()
//
// Action the file operations associated with running HiGHS, and
// call Highs::runFromUserScaling()
HighsStatus status = HighsStatus::kOk;
if (this->options_.read_solution_file != "")
status = this->readSolution(this->options_.read_solution_file);
if (this->options_.read_basis_file != "")
status = this->readBasis(this->options_.read_basis_file);
if (this->options_.write_model_file != "")
status = this->writeModel(this->options_.write_model_file);
if (status != HighsStatus::kOk) return status;
this->reportModelStats();
// Apply any user objective and bound scaling
HighsUserScaleData user_scale_data;
if (this->userScale(user_scale_data) == HighsStatus::kError)
return HighsStatus::kError;
// Determine coefficient ranges and possibly warn the user about
// excessive values, obtaining suggested values for user_objective_scale
// and user_bound_scale
assessExcessiveObjectiveBoundScaling(this->options_.log_options, this->model_,
user_scale_data);
// Optimize the model in the Highs instance
status = optimizeHighs();
if (status == HighsStatus::kError) return status;
// Undo any user objective and bound scaling
if (this->userUnscale(user_scale_data) == HighsStatus::kError)
return HighsStatus::kError;
if (this->options_.write_iis_model_file != "")
status = this->writeIisModel(this->options_.write_iis_model_file);
if (this->options_.solution_file != "")
status = this->writeSolution(this->options_.solution_file,
this->options_.write_solution_style);
if (this->options_.write_basis_file != "")
status = this->writeBasis(this->options_.write_basis_file);
return status;
}
HighsStatus Highs::optimizeHighs() {
// Level 1 of Highs::run()
//
// Move the "mods" to here
return this->multi_linear_objective_.size() ? this->multiobjectiveSolve()
: this->optimizeModel();
}
HighsStatus Highs::optimizeLp() {
// Solve what's in the HighsLp instance Highs::model_.lp_
assert(!model_.isQp());
assert(!model_.lp_.hasSemiVariables());
assert(!this->multi_linear_objective_.size());
return optimizeModel();
}
HighsStatus Highs::optimizeModel() {
// Level 2a of Highs::run()
//
if (!options_.use_warm_start) this->clearSolver();
this->sub_solver_call_time_.initialise();
HighsStatus status = this->calledOptimizeModel();
if (this->options_.log_dev_level > 0) this->reportSubSolverCallTime();
return status;