Google OR-Tools: ortools/linear_solver/linear_expr.cc Source File

1

2

3

4

5

6

7

8

9

10

11

12

13

15

16#include <algorithm>

17#include <cstdlib>

18#include <limits>

19#include <ostream>

20#include <string>

21#include <vector>

22

23#include "absl/strings/str_join.h"

24#include "absl/strings/string_view.h"

27

29

31

33

35 terms_[var] = 1.0;

36}

37

39 for (const auto& kv : rhs.terms_) {

40 terms_[kv.first] += kv.second;

41 }

42 offset_ += rhs.offset_;

43 return *this;

44}

45

47 for (const auto& kv : rhs.terms_) {

48 terms_[kv.first] -= kv.second;

49 }

50 offset_ -= rhs.offset_;

51 return *this;

52}

53

55 if (rhs == 0) {

56 terms_.clear();

57 offset_ = 0;

58 } else if (rhs != 1) {

59 for (auto& kv : terms_) {

60 kv.second *= rhs;

61 }

62 offset_ *= rhs;

63 }

64 return *this;

65}

66

68 DCHECK_NE(rhs, 0);

69 return (*this) *= 1 / rhs;

70}

71

73

74

76 var *= -1;

77 var += 1;

78 return var;

79}

80

83 for (const auto& pair : terms_) {

84 solution += pair.first->solution_value() * pair.second;

85 }

87}

88

89namespace {

90

91void AppendTerm(const double coef, absl::string_view var_name,

92 const bool is_first, std::string* s) {

93 if (is_first) {

94 if (coef == 1.0) {

95 absl::StrAppend(s, var_name);

96 } else if (coef == -1.0) {

97 absl::StrAppend(s, "-", var_name);

98 } else {

99 absl::StrAppend(s, coef, "*", var_name);

100 }

101 } else {

102 const std::string op = coef < 0 ? "-" : "+";

103 const double abs_coef = std::abs(coef);

104 if (abs_coef == 1.0) {

105 absl::StrAppend(s, " ", op, " ", var_name);

106 } else {

107 absl::StrAppend(s, " ", op, " ", abs_coef, "*", var_name);

108 }

109 }

110}

111

112void AppendOffset(const double offset, const bool is_first, std::string* s) {

113 if (is_first) {

114 absl::StrAppend(s, offset);

115 } else {

116 if (offset != 0.0) {

117 const std::string op = offset < 0 ? "-" : "+";

118 absl::StrAppend(s, " ", op, " ", std::abs(offset));

119 }

120 }

121}

122

123}

124

126 std::vector<const MPVariable*> vars_in_order;

127 for (const auto& var_val_pair : terms_) {

128 vars_in_order.push_back(var_val_pair.first);

129 }

130 std::sort(vars_in_order.begin(), vars_in_order.end(),

132 return v->index() < u->index();

133 });

134 std::string result;

135 bool is_first = true;

136 for (const MPVariable* var : vars_in_order) {

137

138 DCHECK(!var->name().empty());

139 AppendTerm(terms_.at(var), var->name(), is_first, &result);

140 is_first = false;

141 }

142 AppendOffset(offset_, is_first, &result);

143

144 return result;

145}

146

148 stream << linear_expr.ToString();

149 return stream;

150}

151

153 lhs += rhs;

154 return lhs;

155}

157 lhs -= rhs;

158 return lhs;

159}

161 lhs *= rhs;

162 return lhs;

163}

165 lhs /= rhs;

166 return lhs;

167}

169 rhs *= lhs;

170 return rhs;

171}

172

178 lower_bound_ -= linear_expr_.offset();

179 upper_bound_ -= linear_expr_.offset();

180 linear_expr_ -= linear_expr_.offset();

181}

182

184 return LinearRange(-std::numeric_limits<double>::infinity(), lhs - rhs, 0);

185}

190 return LinearRange(0, lhs - rhs, std::numeric_limits<double>::infinity());

191}

192

193}

double SolutionValue() const

Definition linear_expr.cc:81

LinearExpr()

Definition linear_expr.cc:32

LinearExpr operator-() const

Definition linear_expr.cc:72

LinearExpr & operator/=(double rhs)

Definition linear_expr.cc:67

std::string ToString() const

Definition linear_expr.cc:125

LinearExpr & operator+=(const LinearExpr &rhs)

Definition linear_expr.cc:38

static LinearExpr NotVar(LinearExpr var)

Definition linear_expr.cc:75

LinearExpr & operator-=(const LinearExpr &rhs)

Definition linear_expr.cc:46

LinearExpr & operator*=(double rhs)

Definition linear_expr.cc:54

double upper_bound() const

const LinearExpr & linear_expr() const

double lower_bound() const

The class for variables of a Mathematical Programming (MP) model.

LinearRange operator==(const LinearExpr &lhs, const LinearExpr &rhs)

Definition linear_expr.cc:186

LinearExpr operator*(LinearExpr lhs, double rhs)

Definition linear_expr.cc:160

Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid

LinearExpr operator-(LinearExpr lhs, const LinearExpr &rhs)

Definition linear_expr.cc:156

LinearRange operator<=(const LinearExpr &lhs, const LinearExpr &rhs)

Definition linear_expr.cc:183

std::ostream & operator<<(std::ostream &out, const Assignment &assignment)

LinearExpr operator+(LinearExpr lhs, const LinearExpr &rhs)

Definition linear_expr.cc:152

LinearExpr operator/(LinearExpr lhs, double rhs)

Definition linear_expr.cc:164

LinearRange operator>=(const LinearExpr &lhs, const LinearExpr &rhs)

Definition linear_expr.cc:189