Skip to content

Commit 0a36960

Browse files
Initialize the objective sense of mps_data_model_t to minimize
mps_data_model_t's default constructor left maximize_ uninitialized; it is the only data member without a default member initializer. A model built through the public C++ API without an explicit set_maximize() call could therefore be solved or written to MPS as a maximization problem, depending on whatever bytes happened to be in memory. Default the sense to minimize, as data_model_view_t already does, and add a test that default-constructs the model over non-zero storage. Signed-off-by: Nicolò Angileri <[email protected]>
1 parent 02bfe53 commit 0a36960

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cpp/include/cuopt/mathematical_optimization/io/mps_data_model.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class mps_data_model_t {
333333
bool has_quadratic_constraints() const noexcept;
334334

335335
/** whether to maximize or minimize the objective function */
336-
bool maximize_;
336+
bool maximize_{false};
337337
/**
338338
* the constraint matrix itself in the CSR format
339339
* @{

‎cpp/tests/linear_programming/parser_test.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
#include <cstdint>
2222
#include <cstdio>
2323
#include <cstdlib>
24+
#include <cstring>
2425
#include <filesystem>
2526
#include <fstream>
2627
#include <limits>
28+
#include <new>
2729
#include <sstream>
2830
#include <stdexcept>
2931
#include <string>
@@ -1244,6 +1246,18 @@ void compare_data_models(const mps_data_model_t<i_t, f_t>& original,
12441246
}
12451247
}
12461248

1249+
TEST(mps_data_model, default_sense_is_minimize)
1250+
{
1251+
// Construct the model in storage filled with non-zero bytes, so that a sense flag left
1252+
// uninitialized by the default constructor would read as maximize.
1253+
using model_t = mps_data_model_t<int, double>;
1254+
alignas(model_t) unsigned char storage[sizeof(model_t)];
1255+
std::memset(storage, 1, sizeof(storage));
1256+
auto* model = new (storage) model_t;
1257+
EXPECT_FALSE(model->get_sense());
1258+
model->~model_t();
1259+
}
1260+
12471261
TEST(mps_roundtrip, empty_constraint_matrix)
12481262
{
12491263
mps_data_model_t<int, double> model;
@@ -1270,6 +1284,7 @@ TEST(mps_roundtrip, empty_constraint_matrix)
12701284
writer.write(temp_file.string());
12711285

12721286
auto reloaded = read_mps<int, double>(temp_file.string(), false);
1287+
EXPECT_FALSE(reloaded.get_sense());
12731288
EXPECT_THAT(reloaded.get_constraint_matrix_offsets(), ElementsAre(0, 0));
12741289
EXPECT_TRUE(reloaded.get_constraint_matrix_values().empty());
12751290
EXPECT_THAT(reloaded.get_constraint_bounds(), ElementsAre(1.0));

0 commit comments

Comments
 (0)