model_exporter.h 3.82 KB
Newer Older
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
// Copyright 2010-2018 Google LLC
// 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.

#ifndef OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_
#define OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_

#include <string>
#include <vector>

#include "absl/strings/str_format.h"
#include "ortools/base/hash.h"
#include "ortools/base/macros.h"
#include "ortools/base/statusor.h"
#include "ortools/linear_solver/linear_solver.pb.h"

namespace operations_research {

/// Export options.
struct MPModelExportOptions {
  MPModelExportOptions() {}

  /// Obfuscates variable and constraint names.
  bool obfuscate = false;
  /// Whether to log invalid variable and constraint names.
  bool log_invalid_names = false;

  /**
   * For .lp files only. Decides whether variables unused in the objective and
   * constraints are shown when exported to a file.
   */
  bool show_unused_variables = false;

  /**
   * For .lp files only. Maximum line length in exported files. The default
   * was chosen so that SCIP can read the files.
   */
  int max_line_length = 10000;
};

/**
 *  Outputs the current model (variables, constraints, objective) as a string
 * encoded in the so-called "CPLEX LP file format" as generated by SCIP.
 * The LP file format is easily readable by a human.
 *
 * Returns false if some error has occurred during execution.
 * The validity of names is automatically checked. If a variable name or a
 * constraint name is invalid or non-existent, a new valid name is
 * automatically generated.
 *
 * If 'obfuscated' is true, the variable and constraint names of proto_
 * are not used.  Variable and constraint names of the form "V12345"
 * and "C12345" are used instead.
 *
 * For more information about the different LP file formats:
 * http://lpsolve.sourceforge.net/5.5/lp-format.htm
 * The following give a reasonable idea of the CPLEX LP file format:
 * http://lpsolve.sourceforge.net/5.5/CPLEX-format.htm
 * http://tinyurl.com/cplex-lp-format
 * http://www.gurobi.com/documentation/5.1/reference-manual/node871
 */
absl::StatusOr<std::string> ExportModelAsLpFormat(
    const MPModelProto& model,
    const MPModelExportOptions& options = MPModelExportOptions());

/**
 * Outputs the current model (variables, constraints, objective) as a string
 * encoded in MPS file format, using the "free" MPS format.
 *
 * Returns false if some error has occurred during execution. Models with
 * maximization objectives trigger an error, because MPS can encode only
 * minimization problems.
 *
 * The validity of names is automatically checked. If a variable name or a
 * constraint name is invalid or non-existent, a new valid name is
 * automatically generated.
 *
 * Name validity and obfuscation works exactly as in ExportModelAsLpFormat().
 *
 * For more information about the MPS format:
 * http://en.wikipedia.org/wiki/MPS_(format)
 * A close-to-original description coming from OSL:
 * http://tinyurl.com/mps-format-by-osl
 * A recent description from CPLEX:
 * http://tinyurl.com/mps-format-by-cplex
 * CPLEX extensions:
 * http://tinyurl.com/mps-extensions-by-cplex
 * Gurobi's description:
 * http://www.gurobi.com/documentation/5.1/reference-manual/node869
 */
absl::StatusOr<std::string> ExportModelAsMpsFormat(
    const MPModelProto& model,
    const MPModelExportOptions& options = MPModelExportOptions());

}  // namespace operations_research

#endif  // OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_