Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_model.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
15#define OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
16
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "absl/container/flat_hash_map.h"
24
26
27DEFINE_STRONG_INT_TYPE(VariableIndex, int64_t);
28DEFINE_STRONG_INT_TYPE(ConstraintIndex, int64_t);
29using Term = std::pair<double, VariableIndex>;
31
32std::ostream& operator<<(std::ostream& ostr, Relation relation);
33
34struct Constraint {
35 std::vector<Term> terms;
37 double rhs = 0.0;
38 std::string name;
39};
40
41// Note: this prints an exact representation of the data in Constraint, not
42// the string form of the constraint in LP format.
43std::ostream& operator<<(std::ostream& ostr, const Constraint& constraint);
44
45// The contents of an optimization model in LP file format.
46//
47// You can convert this to a string in the LP file format using <<, and read
48// from a string in the LP file format using ParseLp from parse_lp.h.
49class LpModel {
50 public:
51 LpModel() = default;
52
53 // Adds a new variable to the model and returns it. Errors if name:
54 // * is empty
55 // * is the same as any existing variable name
56 // * has invalid characters for the LP file format
57 //
58 // Variable names are case sensitive.
59 absl::StatusOr<VariableIndex> AddVariable(absl::string_view name);
60
61 // Adds a new constraint to the model and returns its index.
62 //
63 // Errors if:
64 // * a variable id from constraint.bounds is out of bounds
65 // * constraint.relation is an invalid enum
66 // * a coefficient on constraint.terms is Inf or NaN
67 // * the name has invalid characters
68 // * there are no terms in the constraint
69 //
70 // Constraint names can be repeated but this is not recommended.
71 absl::StatusOr<ConstraintIndex> AddConstraint(Constraint constraint);
72
73 const absl::flat_hash_map<std::string, VariableIndex>& variable_names()
74 const {
75 return variable_names_;
76 }
78 const {
79 return variables_;
80 }
82 const {
83 return constraints_;
84 }
85
86 private:
87 absl::flat_hash_map<std::string, VariableIndex> variable_names_;
90};
91
92// Prints the model in LP format to ostr.
93std::ostream& operator<<(std::ostream& ostr, const LpModel& model);
94
95} // namespace operations_research::lp_format
96
97#endif // OR_TOOLS_MATH_OPT_IO_LP_LP_MODEL_H_
absl::StatusOr< ConstraintIndex > AddConstraint(Constraint constraint)
Definition lp_model.cc:77
const util_intops::StrongVector< ConstraintIndex, Constraint > & constraints() const
Definition lp_model.h:81
const util_intops::StrongVector< VariableIndex, std::string > & variables() const
Definition lp_model.h:77
absl::StatusOr< VariableIndex > AddVariable(absl::string_view name)
Definition lp_model.cc:106
const absl::flat_hash_map< std::string, VariableIndex > & variable_names() const
Definition lp_model.h:73
const std::string name
A name for logging purposes.
GRBmodel * model
std::pair< double, VariableIndex > Term
Definition lp_model.h:29
std::ostream & operator<<(std::ostream &ostr, const Relation relation)
Definition lp_model.cc:48
#define DEFINE_STRONG_INT_TYPE(int_type_name, value_type)
Definition strong_int.h:168