Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
piecewise_linear_function.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// This file implements piecewise linear functions over int64_t. It is built
15// by inserting segments.
16//
17// This class maintains a minimal internal representation and checks for
18// overflow.
19
20#ifndef OR_TOOLS_UTIL_PIECEWISE_LINEAR_FUNCTION_H_
21#define OR_TOOLS_UTIL_PIECEWISE_LINEAR_FUNCTION_H_
22
23#include <cstdint>
24#include <functional>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace operations_research {
30// This structure stores one straight line. It contains the start point, the
31// end point and the slope.
32// It is defined for x values between start_x and end_x.
34 public:
35 PiecewiseSegment(int64_t point_x, int64_t point_y, int64_t slope,
36 int64_t other_point_x);
37
38 // Returns the value of the segment at point x.
39 int64_t Value(int64_t x) const;
40 // Returns the start of the segment's domain.
41 int64_t start_x() const { return start_x_; }
42 // Returns the end of the segment's domain.
43 int64_t end_x() const { return end_x_; }
44 // Returns the value at the start of the segment's domain.
45 int64_t start_y() const { return Value(start_x_); }
46 // Returns the value at the end of the segment's domain.
47 int64_t end_y() const { return Value(end_x_); }
48 // Returns the segment's slope.
49 int64_t slope() const { return slope_; }
50 // Returns the intersection of the segment's extension with the y axis.
51 int64_t intersection_y() const { return intersection_y_; }
52
53 // Comparison method useful for sorting a sequence of segments.
54 static bool SortComparator(const PiecewiseSegment& segment1,
55 const PiecewiseSegment& segment2);
56 // Comparison method useful for finding in which segment a point belongs.
57 static bool FindComparator(int64_t point, const PiecewiseSegment& segment);
58
59 // Expands segment to the specified endpoint, if it is further
60 // than the current endpoint. The reference point of the segment
61 // doesn't change for overflow reasons.
62 void ExpandEnd(int64_t end_x);
63 // Adds 'constant' to the 'x' the segments.
64 void AddConstantToX(int64_t constant);
65 // Adds 'constant' to the 'y' the segments.
66 void AddConstantToY(int64_t constant);
67
68 std::string DebugString() const;
69
70 private:
71 // Computes the value of the segment at point x, taking care of possible
72 // overflows when the value x follow the x coordinate of the segment's
73 // reference point.
74 int64_t SafeValuePostReference(int64_t x) const;
75 // Computes the value of the segment at point x, taking care of possible
76 // overflows when the value x follow the x coordinate of the segment's
77 // reference point.
78 int64_t SafeValuePreReference(int64_t x) const;
79
80 // The x coordinate of the segment's left endpoint.
81 int64_t start_x_;
82 // The x coordinate of the segment's right endpoint.
83 int64_t end_x_;
84 // The segment's slope.
85 int64_t slope_;
86 // The x coordinate of the segment's finite reference point.
87 int64_t reference_x_;
88 // The y coordinate of the segment's finite reference point.
89 int64_t reference_y_;
90 // The intersection of the segment's extension with the y axis.
91 int64_t intersection_y_;
92};
93
94// In mathematics, a piecewise linear function is a function composed
95// of straight-line, non overlapping sections.
97 public:
98 static const int kNotFound;
99
100 // This API provides a factory for creating different families of Piecewise
101 // Linear Functions based on specific properties of each family. The
102 // PiecewiseLinearFunction is composed by a set of PiecwiseSegments and upon
103 // creation is not modifiable but with the provided function operations.
104 // The object returned by any of these builders in the factory is owned by
105 // the client code.
106
107 // Builds the most generic form of multiple-segment piecewise linear function
108 // supporting domain holes. For a fixed index i the elements in points_x[i]
109 // points_y[i], slopes[i], other_points_x[i] represent a segment.
110 // The point (points_x[i], points_y[i]) represents one of the endpoints of
111 // the segment and the other_points_x[i] represents the x coordinate of the
112 // other endpoint which may precede, follow or coincide with points_x[i].
113 // The segments represented by these vectors should not be overlapping.
114 // Common endpoints are allowed.
116 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
117 std::vector<int64_t> slopes, std::vector<int64_t> other_points_x);
118
119 // Builds a multiple-segment step function with continuous or non continuous
120 // domain. The arguments have the same semantics with the generic builder of
121 // the piecewise linear function. In the step function all the slopes are 0.
123 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
124 std::vector<int64_t> other_points_x);
125
126 // Builds a multiple-segment piecewise linear function with domain from
127 // from kint64min to kint64max with n points and n+1 slopes. Each slope
128 // stops at the point with the corresponding index apart from the last one
129 // which stops at kint64max. The first slope stops at the first point at
130 // the level specified.
132 int64_t initial_level, std::vector<int64_t> points_x,
133 std::vector<int64_t> slopes);
134
135 // Builds a function consisting of one segment.
137 int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x);
138
139 // Builds a function consisting of one ray starting at the specified
140 // x and y coordinates with the specified slope.
141 static PiecewiseLinearFunction* CreateRightRayFunction(int64_t point_x,
142 int64_t point_y,
143 int64_t slope);
144
145 // Builds a function consisting of one ray starting at the specified
146 // x and y coordinates with the specified slope.
147 static PiecewiseLinearFunction* CreateLeftRayFunction(int64_t point_x,
148 int64_t point_y,
149 int64_t slope);
150
151 // Builds a two-segment fixed charge piecewise linear cost function. For
152 // values less than zero, the cost is zero. For values greater than zero,
153 // cost follows the line specified by the slope and the value given as
154 // arguments. The slope and value are positive.
156 int64_t value);
157
158 // Builds an earliness-tardiness two-segment piecewise linear cost function.
159 // The reference specifies the point where the cost is zero. Before the
160 // reference, the cost increases with the earliness slope and after the
161 // reference, it increases with the tardiness slope. The absolute values of
162 // the slopes are given.
164 int64_t reference, int64_t earliness_slope, int64_t tardiness_slope);
165
166 // Builds an earliness-tardiness three-segment piecewise linear cost function
167 // with a slack period around the due date. The early slack is the point
168 // before which the cost increases with the ealiness slope specified. The
169 // late slack is the point after which the cost increases with the late slope
170 // specified. Between the early and the late slack point, the cost is zero.
171 // The absolute values of the slopes are given.
173 int64_t early_slack, int64_t late_slack, int64_t earliness_slope,
174 int64_t tardiness_slope);
175
176 // Returns if x is in the domain of the function.
177 bool InDomain(int64_t x) const;
178 // Determines whether the piecewise linear function is convex or non-convex
179 // and returns true when the function is convex.
180 bool IsConvex() const;
181 // Returns true if the piecewise linear function is non-decreasing.
182 bool IsNonDecreasing() const;
183 // Returns true if the piecewise linear function is non-increasing.
184 bool IsNonIncreasing() const;
185 // Returns the value of the piecewise linear function for x.
186 int64_t Value(int64_t x) const;
187 // Returns the maximum value of all the segments in the function.
188 int64_t GetMaximum() const;
189 // Returns the minimum value of all the segments in the function.
190 int64_t GetMinimum() const;
191 // Returns the maximum endpoint value of the segments in the specified
192 // range. If the range is disjoint from the segments in the function, it
193 // returns kint64max.
194 int64_t GetMaximum(int64_t range_start, int64_t range_end) const;
195 // Returns the minimum endpoint value of the segments in the specified
196 // range. If the range is disjoint from the segments in the function, it
197 // returns kint64max.
198 int64_t GetMinimum(int64_t range_start, int64_t range_end) const;
199 // Returns the smallest range within a given range containing all values
200 // greater than a given value.
201 std::pair<int64_t, int64_t> GetSmallestRangeGreaterThanValue(
202 int64_t range_start, int64_t range_end, int64_t value) const;
203 // Returns the smallest range within a given range containing all values
204 // less than a given value.
205 std::pair<int64_t, int64_t> GetSmallestRangeLessThanValue(
206 int64_t range_start, int64_t range_end, int64_t value) const;
207 // Returns the smallest range within a given range containing all values
208 // greater than value_min and less than value_max.
209 std::pair<int64_t, int64_t> GetSmallestRangeInValueRange(
210 int64_t range_start, int64_t range_end, int64_t value_min,
211 int64_t value_max) const;
212
213 // Adds 'constant' to the 'x' of all segments. If the argument is positive,
214 // the translation is to the right and when it's negative, to the left. The
215 // overflows and the underflows are sticky.
216 void AddConstantToX(int64_t constant);
217 // Adds 'constant' to the 'y' of all segments. If the argument is positive,
218 // the translation is up and when it's negative, down. The overflows and the
219 // underflows are sticky.
220 void AddConstantToY(int64_t constant);
221 // Adds the function to the existing one. The domain of the resulting
222 // function is the intersection of the two domains. The overflows and
223 // the underflows are sticky.
224 void Add(const PiecewiseLinearFunction& other);
225 // Subtracts the function to the existing one. The domain of the
226 // resulting function is the intersection of the two domains. The
227 // overflows and the underflows are sticky.
228 void Subtract(const PiecewiseLinearFunction& other);
229 // Decomposes the piecewise linear function in a set of convex piecewise
230 // linear functions. The objects in the vector are owned by the client code.
231 std::vector<PiecewiseLinearFunction*> DecomposeToConvexFunctions() const;
232
233 const std::vector<PiecewiseSegment>& segments() const { return segments_; }
234
235 std::string DebugString() const;
236
237 private:
238 // Takes the sequence of segments, sorts them on increasing start and inserts
239 // them in the piecewise linear function.
240 explicit PiecewiseLinearFunction(std::vector<PiecewiseSegment> segments);
241 // Inserts a segment in the function.
242 void InsertSegment(const PiecewiseSegment& segment);
243 // Operation between two functions. In any operation between two functions the
244 // final domain is the intersection between the two domains.
245 void Operation(const PiecewiseLinearFunction& other,
246 const std::function<int64_t(int64_t, int64_t)>& operation);
247 // Finds start and end segment indices from a range; returns false if the
248 // range is outside the domain of the function.
249 bool FindSegmentIndicesFromRange(int64_t range_start, int64_t range_end,
250 int* start_segment, int* end_segment) const;
251 void UpdateStatus() {
252 if (is_modified_) {
253 is_convex_ = IsConvexInternal();
254 is_non_decreasing_ = IsNonDecreasingInternal();
255 is_non_increasing_ = IsNonIncreasingInternal();
256 is_modified_ = false;
257 }
258 }
259 bool IsConvexInternal() const;
260 bool IsNonDecreasingInternal() const;
261 bool IsNonIncreasingInternal() const;
262
263 // The vector of segments in the function, sorted in ascending order of start
264 // points.
265 std::vector<PiecewiseSegment> segments_;
266 bool is_modified_;
267 bool is_convex_;
268 bool is_non_decreasing_;
269 bool is_non_increasing_;
270};
271} // namespace operations_research
272#endif // OR_TOOLS_UTIL_PIECEWISE_LINEAR_FUNCTION_H_
int64_t GetMaximum() const
Returns the maximum value of all the segments in the function.
std::vector< PiecewiseLinearFunction * > DecomposeToConvexFunctions() const
static PiecewiseLinearFunction * CreateFullDomainFunction(int64_t initial_level, std::vector< int64_t > points_x, std::vector< int64_t > slopes)
static PiecewiseLinearFunction * CreateRightRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
bool IsNonIncreasing() const
Returns true if the piecewise linear function is non-increasing.
std::pair< int64_t, int64_t > GetSmallestRangeLessThanValue(int64_t range_start, int64_t range_end, int64_t value) const
static PiecewiseLinearFunction * CreateFixedChargeFunction(int64_t slope, int64_t value)
void Add(const PiecewiseLinearFunction &other)
static PiecewiseLinearFunction * CreateStepFunction(std::vector< int64_t > points_x, std::vector< int64_t > points_y, std::vector< int64_t > other_points_x)
int64_t GetMinimum() const
Returns the minimum value of all the segments in the function.
const std::vector< PiecewiseSegment > & segments() const
static PiecewiseLinearFunction * CreateOneSegmentFunction(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
Builds a function consisting of one segment.
std::pair< int64_t, int64_t > GetSmallestRangeInValueRange(int64_t range_start, int64_t range_end, int64_t value_min, int64_t value_max) const
static PiecewiseLinearFunction * CreateLeftRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
bool IsNonDecreasing() const
Returns true if the piecewise linear function is non-decreasing.
static PiecewiseLinearFunction * CreateEarlyTardyFunction(int64_t reference, int64_t earliness_slope, int64_t tardiness_slope)
static PiecewiseLinearFunction * CreatePiecewiseLinearFunction(std::vector< int64_t > points_x, std::vector< int64_t > points_y, std::vector< int64_t > slopes, std::vector< int64_t > other_points_x)
static PiecewiseLinearFunction * CreateEarlyTardyFunctionWithSlack(int64_t early_slack, int64_t late_slack, int64_t earliness_slope, int64_t tardiness_slope)
int64_t Value(int64_t x) const
Returns the value of the piecewise linear function for x.
std::pair< int64_t, int64_t > GetSmallestRangeGreaterThanValue(int64_t range_start, int64_t range_end, int64_t value) const
bool InDomain(int64_t x) const
Returns if x is in the domain of the function.
void Subtract(const PiecewiseLinearFunction &other)
int64_t end_x() const
Returns the end of the segment's domain.
int64_t start_y() const
Returns the value at the start of the segment's domain.
static bool FindComparator(int64_t point, const PiecewiseSegment &segment)
Comparison method useful for finding in which segment a point belongs.
static bool SortComparator(const PiecewiseSegment &segment1, const PiecewiseSegment &segment2)
Comparison method useful for sorting a sequence of segments.
void AddConstantToY(int64_t constant)
Adds 'constant' to the 'y' the segments.
int64_t intersection_y() const
Returns the intersection of the segment's extension with the y axis.
int64_t slope() const
Returns the segment's slope.
int64_t start_x() const
Returns the start of the segment's domain.
void AddConstantToX(int64_t constant)
Adds 'constant' to the 'x' the segments.
PiecewiseSegment(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
int64_t Value(int64_t x) const
Returns the value of the segment at point x.
int64_t end_y() const
Returns the value at the end of the segment's domain.
int64_t value
In SWIG mode, we don't want anything besides these top-level includes.
const Variable x
Definition qp_tests.cc:127