Google OR-Tools: ortools/util/piecewise_linear_function.h Source File
20#ifndef ORTOOLS_UTIL_PIECEWISE_LINEAR_FUNCTION_H_
21#define ORTOOLS_UTIL_PIECEWISE_LINEAR_FUNCTION_H_
30#include "absl/algorithm/container.h"
31#include "absl/container/inlined_vector.h"
33#include "absl/strings/string_view.h"
45 int64_t Value(int64_t x) const;
47 int64_t start_x() const { return start_x_; }
49 int64_t end_x() const { return end_x_; }
53 int64_t end_y() const { return Value(end_x_); }
55 int64_t slope() const { return slope_; }
80 int64_t SafeValuePostReference(int64_t x) const;
102class PiecewiseLinearFunction {
122 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
123 std::vector<int64_t> slopes, std::vector<int64_t> other_points_x);
129 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
130 std::vector<int64_t> other_points_x);
138 int64_t initial_level, std::vector<int64_t> points_x,
139 std::vector<int64_t> slopes);
143 int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x);
170 int64_t reference, int64_t earliness_slope, int64_t tardiness_slope);
179 int64_t early_slack, int64_t late_slack, int64_t earliness_slope,
183 bool InDomain(int64_t x) const;
192 int64_t Value(int64_t x) const;
200 int64_t GetMaximum(int64_t range_start, int64_t range_end) const;
204 int64_t GetMinimum(int64_t range_start, int64_t range_end) const;
208 int64_t range_start, int64_t range_end, int64_t value) const;
212 int64_t range_start, int64_t range_end, int64_t value) const;
216 int64_t range_start, int64_t range_end, int64_t value_min,
230 void Add(const PiecewiseLinearFunction& other);
234 void Subtract(const PiecewiseLinearFunction& other);
239 const std::vector<PiecewiseSegment>& segments() const { return segments_; }
252 const std::function<int64_t(int64_t, int64_t)>& operation);
255 bool FindSegmentIndicesFromRange(int64_t range_start, int64_t range_end,
256 int* start_segment, int* end_segment) const;
259 is_convex_ = IsConvexInternal();
260 is_non_decreasing_ = IsNonDecreasingInternal();
261 is_non_increasing_ = IsNonIncreasingInternal();
265 bool IsConvexInternal() const;
266 bool IsNonDecreasingInternal() const;
267 bool IsNonIncreasingInternal() const;
293 absl::InlinedVector<int64_t, 8> y_anchors);
306 std::string DebugString(absl::string_view line_prefix = {}) const;
329 int GetSegmentIndex(int64_t x) const {
330 if (x_anchors_.empty() || x < x_anchors_[0] || x > x_anchors_.back()) {
333 if (x == x_anchors_.back()) return x_anchors_.size() - 2;
336 const auto upper_segment = absl::c_upper_bound(x_anchors_, x);
338 std::distance(x_anchors_.begin(), upper_segment) - 1;
339 DCHECK_GE(segment_index, 0);
340 DCHECK_LE(segment_index, x_anchors_.size() - 2);
346 int64_t GetValueOnSegment(int64_t x, int segment_index) const;
349 absl::InlinedVector<int64_t, 8> x_anchors_;
int64_t ComputeConvexValue(int64_t x) const
FloatSlopePiecewiseLinearFunction(FloatSlopePiecewiseLinearFunction &&other) noexcept
Definition piecewise_linear_function.h:294
FloatSlopePiecewiseLinearFunction()=default
int64_t ComputeInBoundsValue(int64_t x) const
std::string DebugString(absl::string_view line_prefix={}) const
const absl::InlinedVector< int64_t, 8 > & x_anchors() const
Definition piecewise_linear_function.h:308
const absl::InlinedVector< int64_t, 8 > & y_anchors() const
Definition piecewise_linear_function.h:311
static const int kNoValue
Definition piecewise_linear_function.h:289
FloatSlopePiecewiseLinearFunction & operator=(FloatSlopePiecewiseLinearFunction &&other) noexcept
Definition piecewise_linear_function.h:299
int64_t GetMaximum() const
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
std::pair< int64_t, int64_t > GetSmallestRangeLessThanValue(int64_t range_start, int64_t range_end, int64_t value) const
static const int kNotFound
Definition piecewise_linear_function.h:104
static PiecewiseLinearFunction * CreateFixedChargeFunction(int64_t slope, int64_t value)
std::string DebugString() const
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
void AddConstantToY(int64_t constant)
const std::vector< PiecewiseSegment > & segments() const
Definition piecewise_linear_function.h:239
static PiecewiseLinearFunction * CreateOneSegmentFunction(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
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
static PiecewiseLinearFunction * CreateEarlyTardyFunction(int64_t reference, int64_t earliness_slope, int64_t tardiness_slope)
void AddConstantToX(int64_t constant)
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
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
void Subtract(const PiecewiseLinearFunction &other)
int64_t end_x() const
Definition piecewise_linear_function.h:49
int64_t start_y() const
Definition piecewise_linear_function.h:51
static bool FindComparator(int64_t point, const PiecewiseSegment &segment)
static bool SortComparator(const PiecewiseSegment &segment1, const PiecewiseSegment &segment2)
std::string DebugString() const
void ExpandEnd(int64_t end_x)
void AddConstantToY(int64_t constant)
int64_t intersection_y() const
Definition piecewise_linear_function.h:57
int64_t slope() const
Definition piecewise_linear_function.h:55
int64_t start_x() const
Definition piecewise_linear_function.h:47
void AddConstantToX(int64_t constant)
PiecewiseSegment(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
int64_t Value(int64_t x) const
int64_t end_y() const
Definition piecewise_linear_function.h:53