Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ class optimization_problem_t {
*/
void set_row_names(const std::vector<std::string>& row_names);

/**
* @brief Write the problem to an MPS formatted file
*
* @param[in] mps_file_path Path to the MPS file to write
*/
void write_to_mps(const std::string& mps_file_path);

i_t get_n_variables() const;
i_t get_n_constraints() const;
i_t get_nnz() const;
Expand Down
1 change: 1 addition & 0 deletions cpp/include/cuopt/linear_programming/solve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cuopt/linear_programming/solver_settings.hpp>
#include <cuopt/linear_programming/utilities/internals.hpp>
#include <mps_parser/mps_data_model.hpp>
#include <string>
#include <vector>

namespace cuopt::linear_programming {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include <cuopt/linear_programming/pdlp/solver_solution.hpp>
#include <cuopt/linear_programming/solver_settings.hpp>
#include <cuopt/linear_programming/utilities/internals.hpp>
#include <memory>
#include <mps_parser/data_model_view.hpp>
#include <raft/core/handle.hpp>

#include <memory>
#include <string>
#include <utility>
#include <vector>

Expand Down
2 changes: 2 additions & 0 deletions cpp/libmps_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ add_library(mps_parser SHARED
src/data_model_view.cpp
src/mps_data_model.cpp
src/mps_parser.cpp
src/mps_writer.cpp
src/parser.cpp
src/writer.cpp
src/utilities/cython_mps_parser.cpp
)

Expand Down
29 changes: 29 additions & 0 deletions cpp/libmps_parser/include/mps_parser/data_model_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ class data_model_view_t {
* @param[in] problem_name Problem name value.
*/
void set_problem_name(const std::string& problem_name);
/**
* @brief Set the variables names.
* @note Setting before calling the solver is optional.
*
* @param[in] variable_names Variable names values.
*/
void set_variable_names(const std::vector<std::string>& variables_names);
/**
* @brief Set the row names.
* @note Setting before calling the solver is optional.
*
* @param[in] row_names Row names value.
*/
void set_row_names(const std::vector<std::string>& row_names);
/**
* @brief Set the constraints lower bounds.
* @note Setting before calling the solver is optional if you set the row type, else it's
Expand Down Expand Up @@ -350,6 +364,19 @@ class data_model_view_t {
*/
span<f_t const> get_initial_dual_solution() const noexcept;

/**
* @brief Get the variable names
*
* @return span<std::string const>
*/
const std::vector<std::string>& get_variable_names() const noexcept;
/**
* @brief Get the row names
*
* @return span<std::string const>
*/
const std::vector<std::string>& get_row_names() const noexcept;

/**
* @brief Get the problem name
*
Expand Down Expand Up @@ -404,6 +431,8 @@ class data_model_view_t {
span<char const> row_types_;
std::string objective_name_;
std::string problem_name_;
std::vector<std::string> variable_names_;
std::vector<std::string> row_names_;
span<f_t const> constraint_lower_bounds_;
span<f_t const> constraint_upper_bounds_;

Expand Down
59 changes: 59 additions & 0 deletions cpp/libmps_parser/include/mps_parser/mps_writer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights
* reserved. SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <mps_parser/data_model_view.hpp>

#include <stdarg.h>
#include <limits>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace cuopt::mps_parser {

/**
* @brief Main writer class for MPS files
*
* @tparam f_t data type of the weights and variables
* @tparam i_t data type of the indices
*/
template <typename i_t, typename f_t>
class mps_writer_t {
public:
/**
* @brief Ctor. Takes a data model view as input and writes it out as a MPS formatted file
*
* @param[in] problem Data model view to write
* @param[in] file Path to the MPS file to write
*/
mps_writer_t(const data_model_view_t<i_t, f_t>& problem);

/**
* @brief Writes the problem to an MPS formatted file
*
* @param[in] mps_file_path Path to the MPS file to write
*/
void write(const std::string& mps_file_path);

private:
const data_model_view_t<i_t, f_t>& problem_;
}; // class mps_writer_t

} // namespace cuopt::mps_parser
38 changes: 38 additions & 0 deletions cpp/libmps_parser/include/mps_parser/writer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights
* reserved. SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <mps_parser/data_model_view.hpp>

// TODO: we might want to eventually rename libmps_parser to libmps_io
// (or libcuopt_io if we want to support other hypothetical formats)
namespace cuopt::mps_parser {

/**
* @brief Writes the problem to an MPS formatted file
*
* Read this link http://lpsolve.sourceforge.net/5.5/mps-format.htm for more
* details on both free and fixed MPS format.
*
* @param[in] problem The problem data model view to write
* @param[in] mps_file_path Path to the MPS file to write
*/
template <typename i_t, typename f_t>
void write_mps(const data_model_view_t<i_t, f_t>& problem, const std::string& mps_file_path);

} // namespace cuopt::mps_parser
25 changes: 25 additions & 0 deletions cpp/libmps_parser/src/data_model_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ void data_model_view_t<i_t, f_t>::set_problem_name(const std::string& problem_na
problem_name_ = problem_name;
}

template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_variable_names(
const std::vector<std::string>& variables_names)
{
variable_names_ = variables_names;
}

template <typename i_t, typename f_t>
void data_model_view_t<i_t, f_t>::set_row_names(const std::vector<std::string>& row_names)
{
row_names_ = row_names;
}

template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_constraint_matrix_values() const noexcept
{
Expand Down Expand Up @@ -306,6 +319,18 @@ bool data_model_view_t<i_t, f_t>::get_sense() const noexcept
return maximize_;
}

template <typename i_t, typename f_t>
const std::vector<std::string>& data_model_view_t<i_t, f_t>::get_variable_names() const noexcept
{
return variable_names_;
}

template <typename i_t, typename f_t>
const std::vector<std::string>& data_model_view_t<i_t, f_t>::get_row_names() const noexcept
{
return row_names_;
}

// QPS-specific getter implementations
template <typename i_t, typename f_t>
span<const f_t> data_model_view_t<i_t, f_t>::get_quadratic_objective_values() const noexcept
Expand Down
Loading