1 #ifndef LPINTERFACE_DATA_OBJECTS_H
2 #define LPINTERFACE_DATA_OBJECTS_H
29 static_assert(std::is_arithmetic<T>::value,
30 "MatrixEntry<T> requires T to be arithmetic");
33 using iterator =
typename std::vector<T>::iterator;
34 using const_iterator =
typename std::vector<T>::const_iterator;
38 using SizeType = std::size_t;
40 using iterator_category = std::forward_iterator_tag;
42 using difference_type = int;
43 using pointer = value_type*;
44 using reference = value_type&;
53 : values_(size), nonzero_indices_(size) {}
56 : values_(values), nonzero_indices_(indices) {
57 if (values_.size() != nonzero_indices_.size()) {
73 std::find(nonzero_indices_.begin(), nonzero_indices_.end(), index);
74 if (index_in_data == nonzero_indices_.end()) {
78 return values_[
static_cast<SizeType
>(index_in_data -
79 nonzero_indices_.begin())];
82 static_cast<SizeType>(index_in_data - nonzero_indices_.begin()));
89 return std::min_element(values_.begin(), values_.end());
94 return std::max_element(values_.begin(), values_.end());
99 return values_.size();
103 const std::vector<T>&
values()
const {
return values_; }
106 std::vector<T>&
values() {
return values_; }
116 iterator
begin() {
return values_.begin(); }
119 const_iterator
begin()
const {
return values_.begin(); }
122 iterator
end() {
return values_.end(); }
125 const_iterator
end()
const {
return values_.end(); }
128 std::vector<T> values_;
129 std::vector<Index> nonzero_indices_;
131 void check_entry_valid() {
134 std::set<Index> index_set(nonzero_indices_.begin(), nonzero_indices_.end());
135 if (nonzero_indices_.size() != index_set.size()) {
145 bool operator==(
const MatrixEntry<T>& left,
const MatrixEntry<T>& right) {
146 return std::is_permutation(left.nonzero_indices().begin(),
147 left.nonzero_indices().end(),
148 right.nonzero_indices().begin()) &&
149 std::is_permutation(left.values().begin(), left.values().end(),
150 right.values().begin());
153 template <
typename T>
156 using Index =
typename MatrixEntry<T>::Index;
157 using SizeType =
typename MatrixEntry<T>::SizeType;
162 Column(
const std::vector<T>& values,
const std::vector<Index>& indices)
167 template <
typename T>
170 using Index =
typename MatrixEntry<T>::Index;
171 using SizeType =
typename MatrixEntry<T>::SizeType;
176 Row(
const std::vector<T>& values,
const std::vector<Index>& indices)
191 template <
typename T>
193 static_assert(std::is_arithmetic<T>::value,
194 "T must be arithmetic in order to be ordered");
210 left.row == right.row;
222 template <
typename T>
265 Variable(
double lb,
double ub) : lower_bound_(lb), upper_bound_(ub)
273 double upper()
const {
return upper_bound_; }
275 double lower()
const {
return lower_bound_; }
278 double lower_bound_ = 0.0;
282 inline bool operator==(
const Variable& left,
const Variable& right) {
283 return left.lower() == right.lower() && left.upper() == right.upper();
286 inline std::ostream& operator<<(std::ostream& os,
const Variable& var) {
287 os <<
"Variable { lower = " << var.lower() <<
", upper = " << var.upper()
297 template <
typename T>
308 template <
typename T>
309 inline std::ostream& operator<<(std::ostream& os,
311 os << constraint.
lower_bound <<
" <= " << constraint.row
316 template <
typename T>
317 inline std::ostream& operator<<(std::ostream& os, const Objective<T>& obj) {
318 if (obj.values.size() == 0) {
319 os <<
"Objective {}";
323 std::size_t n = obj.values.size();
324 for (std::size_t i = 0; i < n; i++) {
325 os << obj.values[i] <<
", ";
331 template <
typename T>
332 inline std::ostream& operator<<(std::ostream& os, const MatrixEntry<T>& row) {
333 if (row.num_nonzero() == 0) {
334 os <<
"Entry {[] []}";
338 for (
const auto& val : row.values()) {
342 for (
const auto& val : row.nonzero_indices()) {
352 #endif // LPINTERFACE_DATA_OBJECTS_H
const std::vector< T > & values() const
Get a const reference to the underlying value array.
Definition: data_objects.hpp:103
Struct to represent right-hand side of LP constraints. In linear programming, we have constraints of ...
Definition: data_objects.hpp:192
Attempt to construct a variable with invalid bounds.
Definition: errors.hpp:70
Definition: data_objects.hpp:168
const std::vector< Index > & nonzero_indices() const
Get a const reference to the underlying nonzero indices.
Definition: data_objects.hpp:109
std::vector< T > & values()
Get a reference to the underlying value array.
Definition: data_objects.hpp:106
iterator begin()
Definition: data_objects.hpp:116
constexpr double LPINT_INFINITY
Constant representing floating point infinity.
Definition: common.hpp:10
const_iterator end() const
Obtain a const iterator to the end of the underlying value array.
Definition: data_objects.hpp:125
std::vector< Index > & nonzero_indices()
Get a reference to the underlying nonzero indices.
Definition: data_objects.hpp:112
double upper() const
Retrieve the upper bound of this variable.
Definition: data_objects.hpp:273
Definition: data_objects.hpp:154
Attempt to add a matrix entry containing duplicate indices.
Definition: errors.hpp:54
Definition: errors.hpp:122
Variable()=default
Construct a non-negative variable.
const_iterator begin() const
Obtain a const iterator to the begin of the underlying value array.
Definition: data_objects.hpp:119
T objective_value
Value of the objective .
Definition: data_objects.hpp:304
std::vector< T >::size_type num_nonzero() const
Return the number of nonzero entries in the matrix entry.
Definition: data_objects.hpp:98
T operator[](const SizeType index) const
Indexing operator; can be used identically to a dense vector. Will perform bounds checking ifndef NDE...
Definition: data_objects.hpp:71
Matrix entry type, for use in sparse matrix. The matrix entry base class is specialized as Row and Co...
Definition: data_objects.hpp:28
T upper_bound() const
Returns the largest entry of this matrix element.
Definition: data_objects.hpp:93
double lower() const
Retrieve the lower bound of this variable.
Definition: data_objects.hpp:275
std::vector< T > dual
Values in the dual solution vector.
Definition: data_objects.hpp:302
Variable(double lb, double ub)
Construct a new Variable object.
Definition: data_objects.hpp:265
T lower_bound
Lower bound of constraint equation.
Definition: data_objects.hpp:201
Class representing a variable in the LP.
Definition: data_objects.hpp:253
Struct representing the objective vector. A linear program has the canonical form This structure rep...
Definition: data_objects.hpp:223
iterator end()
Obtain an iterator to the end of the underlying value array.
Definition: data_objects.hpp:122
T upper_bound
Upper bound of constraint equation.
Definition: data_objects.hpp:203
std::vector< T > values
Values of elements in the objective vector.
Definition: data_objects.hpp:231
T lower_bound() const
Returns the smallest entry of this matrix element.
Definition: data_objects.hpp:88
Struct representing the solution of a linear program.
Definition: data_objects.hpp:298
std::vector< T > primal
Values in the primal solution vector.
Definition: data_objects.hpp:300