File: TestOptions.cpp

package info (click to toggle)
scipy 1.16.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 236,092 kB
  • sloc: cpp: 503,720; python: 345,302; ansic: 195,677; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 857; makefile: 792; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (514 lines) | stat: -rw-r--r-- 21,587 bytes parent folder | download | duplicates (3)
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
#include <cstdio>

#include "HCheckConfig.h"
#include "Highs.h"
#include "catch.hpp"
#include "io/HMPSIO.h"
#include "io/LoadOptions.h"

const bool dev_run = false;

TEST_CASE("external-options", "[highs_options]") {
  Highs highs;
  highs.setOptionValue("output_flag", dev_run);
  HighsInt num_options = highs.getNumOptions();
  if (dev_run) printf("Number of options is %d\n", int(num_options));
  std::string option;
  HighsOptionType type;
  bool current_bool_value, default_bool_value;
  HighsInt min_int_value, max_int_value, current_int_value, default_int_value;
  double min_double_value, max_double_value, current_double_value,
      default_double_value;
  std::string current_string_value, default_string_value;
  for (HighsInt index = 0; index < num_options; index++) {
    REQUIRE(highs.getOptionName(index, &option) == HighsStatus::kOk);
    REQUIRE(highs.getOptionType(option, &type) == HighsStatus::kOk);
    if (dev_run)
      printf("Option %2d is \"%s\" of type %d", int(index), option.c_str(),
             int(type));
    if (type == HighsOptionType::kBool) {
      REQUIRE(highs.getBoolOptionValues(option, &current_bool_value,
                                        &default_bool_value) ==
              HighsStatus::kOk);
      if (dev_run)
        printf(": current = %d; default = %d\n", current_bool_value,
               default_bool_value);
    } else if (type == HighsOptionType::kInt) {
      REQUIRE(highs.getIntOptionValues(option, &current_int_value,
                                       &min_int_value, &max_int_value,
                                       &default_int_value) == HighsStatus::kOk);
      if (dev_run)
        printf(": current = %d; min = %d; max = %d; default = %d\n",
               int(current_int_value), int(min_int_value), int(max_int_value),
               int(default_int_value));
    } else if (type == HighsOptionType::kDouble) {
      REQUIRE(highs.getDoubleOptionValues(option, &current_double_value,
                                          &min_double_value, &max_double_value,
                                          &default_double_value) ==
              HighsStatus::kOk);
      if (dev_run)
        printf(": current = %g; min = %g; max = %g; default = %g\n",
               current_double_value, min_double_value, max_double_value,
               default_double_value);
      REQUIRE(highs.getDoubleOptionValues(option, &current_double_value) ==
              HighsStatus::kOk);
      if (dev_run)
        printf("          is \"%s\" of type %d: current = %g\n", option.c_str(),
               int(type), current_double_value);
    } else {
      REQUIRE(highs.getStringOptionValues(option, &current_string_value,
                                          &default_string_value) ==
              HighsStatus::kOk);
      if (dev_run)
        printf(": current = \"%s\"; default = \"%s\"\n",
               current_string_value.c_str(), default_string_value.c_str());
    }
  }
  HighsInt num_string_option = 0;
  if (dev_run) printf("\nString options are:\n");
  for (HighsInt index = 0; index < num_options; index++) {
    highs.getOptionName(index, &option);
    highs.getOptionType(option, &type);
    highs.getStringOptionValues(option, &current_string_value);
    if (type != HighsOptionType::kString) continue;
    num_string_option++;
    if (dev_run)
      printf("%2d: %-24s \"%s\"\n", int(num_string_option), option.c_str(),
             current_string_value.c_str());
  }
}

TEST_CASE("internal-options", "[highs_options]") {
  HighsOptions options;
  HighsLogOptions report_log_options = options.log_options;
  if (!dev_run) options.output_flag = false;
  OptionStatus return_status =
      checkOptions(report_log_options, options.records);
  REQUIRE(return_status == OptionStatus::kOk);

  std::string filename = std::string(HIGHS_DIR) + "/check/sample_options_file";

  bool success = loadOptionsFromFile(report_log_options, options, filename) ==
                 HighsLoadOptionsStatus::kOk;
  REQUIRE(success == true);
  REQUIRE(options.presolve == kHighsOnString);
  REQUIRE(options.small_matrix_value == 0.001);
  REQUIRE(options.mps_parser_type_free);

  if (dev_run) reportOptions(stdout, options.records, true);

  return_status = checkOptions(report_log_options, options.records);
  REQUIRE(return_status == OptionStatus::kOk);

  // Check setting boolean options
  std::string setting_string = "fixed";
  return_status =
      setLocalOptionValue(report_log_options, "mps_parser_type_free",
                          options.log_options, options.records, setting_string);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(options.log_options, "mps_parser_type_free",
                          options.log_options, options.records, "fixed");
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(options.log_options, "mps_parser_type_free",
                          options.log_options, options.records, "False");
  REQUIRE(return_status == OptionStatus::kOk);

  return_status =
      setLocalOptionValue(options.log_options, "mps_parser_type_free",
                          options.log_options, options.records, "F");
  REQUIRE(return_status == OptionStatus::kOk);

  bool mps_parser_type_free = false;
  return_status =
      setLocalOptionValue(report_log_options, "mps_parser_type_free",
                          options.records, mps_parser_type_free);
  REQUIRE(return_status == OptionStatus::kOk);

  return_status = setLocalOptionValue(report_log_options, "mps_parser_type",
                                      options.records, true);
  REQUIRE(return_status == OptionStatus::kUnknownOption);

  // Check setting HighsInt options

  return_status = setLocalOptionValue(
      options.log_options, "allowed_matrix_scale_factor", options.records, -1);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(options.log_options, "allowed_matrix_scale_factor",
                          options.records, kMaxAllowedMatrixPow2Scale + 5);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  std::string allowed_matrix_scale_factor_string = "1e-7";
  return_status = setLocalOptionValue(
      report_log_options, "allowed_matrix_scale_factor", options.log_options,
      options.records, allowed_matrix_scale_factor_string);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(report_log_options, "allowed_matrix_scale_factor",
                          options.log_options, options.records, "3.14159");

  REQUIRE(return_status == OptionStatus::kIllegalValue);

  if (dev_run) {
    printf("\nAfter setting allowed_matrix_scale_factor to 1\n");
    reportOptions(stdout, options.records);
  }

  double allowed_matrix_scale_factor_double = 1e-7;
  return_status =
      setLocalOptionValue(report_log_options, "allowed_matrix_scale_factor",
                          options.records, allowed_matrix_scale_factor_double);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  HighsInt allowed_matrix_scale_factor = 12;
  return_status =
      setLocalOptionValue(report_log_options, "allowed_matrix_scale_factor",
                          options.records, allowed_matrix_scale_factor);
  REQUIRE(return_status == OptionStatus::kOk);

  if (dev_run) {
    printf("\nAfter testing HighsInt options\n");
    reportOptions(stdout, options.records);
  }

  // Check setting double options

  return_status = setLocalOptionValue(report_log_options, "large_matrix_value",
                                      options.records, -1);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(report_log_options, "large_matrix_value",
                          options.log_options, options.records, "1");
  REQUIRE(return_status == OptionStatus::kOk);

  return_status = setLocalOptionValue(report_log_options, "small_matrix_value",
                                      options.records, -1);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(report_log_options, "small_matrix_value",
                          options.log_options, options.records, "1e-6");
  REQUIRE(return_status == OptionStatus::kOk);

  double small_matrix_value = 1e-7;
  return_status = setLocalOptionValue(report_log_options, "small_matrix_value",
                                      options.records, small_matrix_value);
  REQUIRE(return_status == OptionStatus::kOk);

  // Check setting string options

  return_status =
      setLocalOptionValue(report_log_options, kPresolveString,
                          options.log_options, options.records, "ml.mps");
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  std::string model_file = "ml.mps";
  return_status =
      setLocalOptionValue(report_log_options, kPresolveString,
                          options.log_options, options.records, model_file);
  REQUIRE(return_status == OptionStatus::kIllegalValue);

  return_status =
      setLocalOptionValue(report_log_options, kPresolveString,
                          options.log_options, options.records, "off");
  REQUIRE(return_status == OptionStatus::kOk);

  std::string presolve = "choose";
  return_status =
      setLocalOptionValue(report_log_options, kPresolveString,
                          options.log_options, options.records, presolve);
  REQUIRE(return_status == OptionStatus::kOk);

  return_status =
      setLocalOptionValue(report_log_options, kModelFileString,
                          options.log_options, options.records, model_file);
  REQUIRE(return_status == OptionStatus::kUnknownOption);

  if (dev_run) reportOptions(stdout, options.records);

  bool get_mps_parser_type_free;
  return_status =
      getLocalOptionValues(report_log_options, "mps_parser_type_free",
                           options.records, &get_mps_parser_type_free);
  REQUIRE(return_status == OptionStatus::kOk);
  REQUIRE(get_mps_parser_type_free == false);

  HighsInt get_allowed_matrix_scale_factor;
  return_status =
      getLocalOptionValues(report_log_options, "allowed_matrix_scale_factor",
                           options.records, &get_allowed_matrix_scale_factor);
  REQUIRE(return_status == OptionStatus::kOk);
  REQUIRE(get_allowed_matrix_scale_factor == allowed_matrix_scale_factor);

  double get_small_matrix_value;
  return_status =
      getLocalOptionValues(report_log_options, "small_matrix_value",
                           options.records, &get_small_matrix_value);
  REQUIRE(return_status == OptionStatus::kOk);
  REQUIRE(get_small_matrix_value == small_matrix_value);

  return_status = checkOptions(report_log_options, options.records);
  REQUIRE(return_status == OptionStatus::kOk);
  std::remove(model_file.c_str());
}

TEST_CASE("highs-options", "[highs_options]") {
  Highs highs;
  if (!dev_run) highs.setOptionValue("output_flag", false);
  HighsStatus return_status = highs.writeOptions("Highs.set");
  REQUIRE(return_status == HighsStatus::kOk);

  // Check mixed-string value
  //
  // For bool, values from a precise set can be expected
  return_status = highs.setOptionValue("mps_parser_type_free", "10true");
  REQUIRE(return_status == HighsStatus::kError);

  // For int, the number of digits scanned in the (trimmed) string to
  // get an integer must match the number of characters in the
  // (trimmed) string
  const HighsInt set_int_option_value = 10;
  HighsInt get_int_option_value;
  return_status = highs.setOptionValue("simplex_iteration_limit", " 10");
  REQUIRE(return_status == HighsStatus::kOk);
  highs.getOptionValue("simplex_iteration_limit", get_int_option_value);
  REQUIRE(get_int_option_value == set_int_option_value);

  return_status = highs.setOptionValue("simplex_iteration_limit", "10 ");
  REQUIRE(return_status == HighsStatus::kOk);
  highs.getOptionValue("simplex_iteration_limit", get_int_option_value);
  REQUIRE(get_int_option_value == set_int_option_value);

  return_status = highs.setOptionValue("simplex_iteration_limit", "1 0");
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("simplex_iteration_limit", "10.");
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("simplex_iteration_limit", "10hi");
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("simplex_iteration_limit", "10.2hi");
  REQUIRE(return_status == HighsStatus::kError);

  // For double, make sure that the string contains a numerical
  // character, otherwise anything goes!
  return_status = highs.setOptionValue("objective_bound", "1E2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1.1E2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1E+2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1.1E+2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1E-2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1.1E-2");
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue("objective_bound", "-1E2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "-1.1E2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "-1E+2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "-1.1E+2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "-1E-2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "-1.1E-2");
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue("objective_bound", "e12");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1e2");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "12e");
  REQUIRE(return_status == HighsStatus::kOk);
  return_status = highs.setOptionValue("objective_bound", "1.1e2");
  REQUIRE(return_status == HighsStatus::kOk);
  // Illegal
  return_status = highs.setOptionValue("objective_bound", "10hi");
  REQUIRE(return_status == HighsStatus::kError);
  return_status = highs.setOptionValue("objective_bound", "1d2");
  REQUIRE(return_status == HighsStatus::kError);
  return_status = highs.setOptionValue("objective_bound", "1.1d2");
  REQUIRE(return_status == HighsStatus::kError);
  return_status = highs.setOptionValue("objective_bound", "1D2");
  REQUIRE(return_status == HighsStatus::kError);
  return_status = highs.setOptionValue("objective_bound", "1.1D2");
  REQUIRE(return_status == HighsStatus::kError);

  // And the original motivation in #1250!
  return_status = highs.setOptionValue("time_limit", "hi");
  REQUIRE(return_status == HighsStatus::kError);

  // Check setting boolean options
  std::string setting_string = "fixed";
  return_status = highs.setOptionValue("mps_parser_type_free", setting_string);
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("mps_parser_type_free", "fixed");
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("mps_parser_type_free", "False");
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue("mps_parser_type_free", "F");
  REQUIRE(return_status == HighsStatus::kOk);

  bool mps_parser_type_free = false;
  return_status =
      highs.setOptionValue("mps_parser_type_free", mps_parser_type_free);
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue("mps_parser_type", true);
  REQUIRE(return_status == HighsStatus::kError);

  // Check setting HighsInt options

  return_status = highs.setOptionValue("allowed_matrix_scale_factor", -1);
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("allowed_matrix_scale_factor",
                                       kMaxAllowedMatrixPow2Scale + 5);
  REQUIRE(return_status == HighsStatus::kError);

  std::string allowed_matrix_scale_factor_string = "1e-7";
  return_status = highs.setOptionValue("allowed_matrix_scale_factor",
                                       allowed_matrix_scale_factor_string);
  REQUIRE(return_status == HighsStatus::kError);

  return_status =
      highs.setOptionValue("allowed_matrix_scale_factor", "3.14159");
  REQUIRE(return_status == HighsStatus::kError);

  if (dev_run) printf("\nAfter setting allowed_matrix_scale_factor to 1\n");
  return_status = highs.writeOptions("Highs.set");
  REQUIRE(return_status == HighsStatus::kOk);

  double allowed_matrix_scale_factor_double = 1e-7;
  return_status = highs.setOptionValue("allowed_matrix_scale_factor",
                                       allowed_matrix_scale_factor_double);
  REQUIRE(return_status == HighsStatus::kError);

  HighsInt allowed_matrix_scale_factor = 12;
  return_status = highs.setOptionValue("allowed_matrix_scale_factor",
                                       allowed_matrix_scale_factor);
  REQUIRE(return_status == HighsStatus::kOk);

  if (dev_run) printf("\nAfter testing HighsInt options\n");
  return_status = highs.writeOptions("Highs.set");
  REQUIRE(return_status == HighsStatus::kOk);

  // Check setting double options

  return_status = highs.setOptionValue("large_matrix_value", -1);
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("large_matrix_value", "1");
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue("small_matrix_value", -1);
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue("small_matrix_value", "1e-6");
  REQUIRE(return_status == HighsStatus::kOk);

  double small_matrix_value = 1e-7;
  return_status =
      highs.setOptionValue("small_matrix_value", small_matrix_value);
  REQUIRE(return_status == HighsStatus::kOk);

  // Check setting string options

  return_status = highs.setOptionValue(kPresolveString, "ml.mps");
  REQUIRE(return_status == HighsStatus::kError);

  std::string model_file = "ml.mps";
  return_status = highs.setOptionValue(kPresolveString, model_file);
  REQUIRE(return_status == HighsStatus::kError);

  return_status = highs.setOptionValue(kPresolveString, "off");
  REQUIRE(return_status == HighsStatus::kOk);

  std::string presolve = "choose";
  return_status = highs.setOptionValue(kPresolveString, presolve);
  REQUIRE(return_status == HighsStatus::kOk);

  return_status = highs.setOptionValue(kModelFileString, model_file);
  REQUIRE(return_status == HighsStatus::kError);

  std::string options_file = "Highs.set";
  return_status = highs.writeOptions(options_file);
  REQUIRE(return_status == HighsStatus::kOk);

  HighsOptionType highs_option_type;

  bool get_mps_parser_type_free;
  return_status =
      highs.getOptionValue("mps_parser_type_free", get_mps_parser_type_free);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(get_mps_parser_type_free == false);

  return_status =
      highs.getOptionType("mps_parser_type_free", highs_option_type);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(highs_option_type == HighsOptionType::kBool);

  HighsInt get_allowed_matrix_scale_factor;
  return_status = highs.getOptionValue("allowed_matrix_scale_factor",
                                       get_allowed_matrix_scale_factor);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(get_allowed_matrix_scale_factor == allowed_matrix_scale_factor);

  return_status =
      highs.getOptionType("allowed_matrix_scale_factor", highs_option_type);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(highs_option_type == HighsOptionType::kInt);

  double get_small_matrix_value;
  return_status =
      highs.getOptionValue("small_matrix_value", get_small_matrix_value);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(get_small_matrix_value == small_matrix_value);

  return_status = highs.getOptionType("small_matrix_value", highs_option_type);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(highs_option_type == HighsOptionType::kDouble);

  return_status = highs.getOptionType("log_file", highs_option_type);
  REQUIRE(return_status == HighsStatus::kOk);
  REQUIRE(highs_option_type == HighsOptionType::kString);

  HighsOptions options = highs.getOptions();
  REQUIRE(options.small_matrix_value == small_matrix_value);
  REQUIRE(options.allowed_matrix_scale_factor == allowed_matrix_scale_factor);
  REQUIRE(options.mps_parser_type_free == mps_parser_type_free);
  std::remove(options_file.c_str());

  return_status = highs.setOptionValue("time_limit", 1);
  REQUIRE(return_status == HighsStatus::kOk);
}

TEST_CASE("inf-value-options", "[highs_options]") {
  Highs highs;
  highs.setOptionValue("output_flag", dev_run);
  std::string options_file =
      std::string(HIGHS_DIR) + "/check/instances/WithInf.set";
  REQUIRE(highs.readOptions(options_file) == HighsStatus::kOk);
  double value;
  highs.getOptionValue("time_limit", value);
  REQUIRE(value == kHighsInf);
  highs.getOptionValue("objective_bound", value);
  REQUIRE(value == -kHighsInf);
  highs.getOptionValue("objective_target", value);
  REQUIRE(value == kHighsInf);
}