21#include "absl/strings/str_format.h"
32int FindSegmentIndex(
const std::vector<PiecewiseSegment>& segments, int64_t x) {
33 if (segments.empty() || segments.front().start_x() > x) {
41 if (position == segments.end()) {
42 return segments.size() - 1;
44 position -= position->start_x() > x ? 1 : 0;
46 return position - segments.begin();
49inline bool IsAtBounds(int64_t
value) {
53inline bool PointInsideRange(int64_t point, int64_t range_start,
55 return range_start <= point && range_end >= point;
60inline bool FormConvexPair(
const PiecewiseSegment& left,
61 const PiecewiseSegment& right) {
62 return right.slope() >= left.slope() && right.start_x() == left.end_x() &&
63 right.start_y() == left.end_y();
66uint64_t UnsignedCapAdd(uint64_t left, uint64_t right) {
70uint64_t UnsignedCapProd(uint64_t left, uint64_t right) {
71 if (right == 0)
return 0;
78 int64_t slope, int64_t other_point_x)
79 : slope_(slope), reference_x_(point_x), reference_y_(point_y) {
80 start_x_ =
std::min(point_x, other_point_x);
81 end_x_ =
std::max(point_x, other_point_x);
83 reference_x_ < 0 ? SafeValuePostReference(0) : SafeValuePreReference(0);
90 const int64_t span_x =
CapSub(x, reference_x_);
93 return SafeValuePostReference(x);
96 return SafeValuePreReference(x);
99 const int64_t span_y =
CapProd(slope_, span_x);
100 if (IsAtBounds(span_y)) {
102 return SafeValuePostReference(x);
104 return SafeValuePreReference(x);
108 const int64_t
value =
CapAdd(reference_y_, span_y);
109 if (IsAtBounds(
value)) {
111 return SafeValuePostReference(x);
113 return SafeValuePreReference(x);
120int64_t PiecewiseSegment::SafeValuePostReference(int64_t x)
const {
122 const uint64_t span_x =
static_cast<uint64_t
>(x) - reference_x_;
129 }
else if (slope_ > 0) {
131 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
132 if (reference_y_ == 0) {
134 }
else if (reference_y_ > 0) {
135 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
137 :
static_cast<int64_t
>(unsigned_sum);
139 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
140 if (span_y >= opp_reference_y) {
141 return span_y - opp_reference_y >
kint64max
143 :
static_cast<int64_t
>(span_y - opp_reference_y);
145 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
147 : -
static_cast<int64_t
>(opp_reference_y - span_y);
152 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
153 if (reference_y_ == 0) {
155 }
else if (reference_y_ < 0) {
156 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
157 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
160 : -
static_cast<int64_t
>(opp_unsigned_sum);
162 if (reference_y_ >= span_y) {
165 :
static_cast<int64_t
>(reference_y_ - span_y);
167 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
169 : -
static_cast<int64_t
>(span_y - reference_y_);
175int64_t PiecewiseSegment::SafeValuePreReference(int64_t x)
const {
177 const uint64_t span_x =
static_cast<uint64_t
>(reference_x_) - x;
181 }
else if (slope_ > 0) {
183 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
184 if (reference_y_ == 0) {
186 }
else if (reference_y_ > 0) {
187 if (reference_y_ >= span_y) {
190 :
static_cast<int64_t
>(reference_y_ - span_y);
192 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
194 : -
static_cast<uint64_t
>(span_y - reference_y_);
197 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
198 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
201 : -
static_cast<uint64_t
>(opp_unsigned_sum);
205 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
206 if (reference_y_ == 0) {
208 }
else if (reference_y_ < 0) {
209 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
210 if (span_y >= opp_reference_y) {
211 return span_y - opp_reference_y >
kint64max
213 :
static_cast<int64_t
>(span_y - opp_reference_y);
215 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
217 : -
static_cast<uint64_t
>(opp_reference_y - span_y);
220 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
222 :
static_cast<int64_t
>(unsigned_sum);
229 return segment1.start_x_ < segment2.start_x_;
242 if (IsAtBounds(
CapAdd(reference_x_, constant))) {
246 start_x_ =
CapAdd(start_x_, constant);
247 end_x_ =
CapAdd(end_x_, constant);
248 reference_x_ =
CapAdd(reference_x_, constant);
252 if (IsAtBounds(
CapAdd(reference_y_, constant))) {
256 reference_y_ =
CapAdd(reference_y_, constant);
260 std::string result = absl::StrFormat(
261 "PiecewiseSegment(<start: (%d, %d), end: (%d, %d), "
262 "reference: (%d, %d), slope = %d>)",
263 start_x_,
Value(start_x_), end_x_,
Value(end_x_), reference_x_,
264 reference_y_, slope_);
270PiecewiseLinearFunction::PiecewiseLinearFunction(
271 std::vector<PiecewiseSegment> segments)
272 : is_modified_(true),
274 is_non_decreasing_(false),
275 is_non_increasing_(false) {
279 for (
int i = 0; i < segments.size() - 1; ++i) {
281 LOG(
FATAL) <<
"Overlapping segments: " << segments[i].DebugString()
282 <<
" & " << segments[i + 1].DebugString();
286 for (
const auto& segment : segments) {
287 InsertSegment(segment);
292 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
293 std::vector<int64_t> slopes, std::vector<int64_t> other_points_x) {
294 CHECK_EQ(points_x.size(), points_y.size());
295 CHECK_EQ(points_x.size(), other_points_x.size());
296 CHECK_EQ(points_x.size(), slopes.size());
299 std::vector<PiecewiseSegment>
segments;
300 for (
int i = 0; i < points_x.size(); ++i) {
309 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
310 std::vector<int64_t> other_points_x) {
311 CHECK_EQ(points_x.size(), points_y.size());
312 CHECK_EQ(points_x.size(), other_points_x.size());
315 std::vector<PiecewiseSegment>
segments;
316 for (
int i = 0; i < points_x.size(); ++i) {
325 int64_t initial_level, std::vector<int64_t> points_x,
326 std::vector<int64_t> slopes) {
327 CHECK_EQ(points_x.size(), slopes.size() - 1);
330 int64_t level = initial_level;
331 std::vector<PiecewiseSegment>
segments;
335 level = segment.
Value(points_x[0]);
336 for (
int i = 1; i < points_x.size(); ++i) {
340 level = segment.
Value(points_x[i]);
349 int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x) {
352 std::vector<PiecewiseSegment>
segments = {
358 int64_t point_x, int64_t point_y, int64_t slope) {
359 std::vector<PiecewiseSegment>
segments = {
365 int64_t point_x, int64_t point_y, int64_t slope) {
366 std::vector<PiecewiseSegment>
segments = {
372 int64_t slope, int64_t
value) {
373 std::vector<PiecewiseSegment>
segments = {
382 int64_t reference, int64_t earliness_slope, int64_t tardiness_slope) {
383 std::vector<PiecewiseSegment>
segments = {
393 int64_t early_slack, int64_t late_slack, int64_t earliness_slope,
394 int64_t tardiness_slope) {
395 std::vector<PiecewiseSegment>
segments = {
406 int index = FindSegmentIndex(segments_, x);
410 if (segments_[
index].end_x() < x) {
423 return is_non_decreasing_;
428 return is_non_increasing_;
437 const int index = FindSegmentIndex(segments_, x);
438 return segments_[
index].Value(x);
442 int64_t range_end)
const {
444 return Value(range_end);
446 return Value(range_start);
448 int start_segment = -1;
449 int end_segment = -1;
450 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
454 CHECK_GE(end_segment, start_segment);
464 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
465 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
466 range_maximum =
std::max(range_maximum, segments_[i].start_y());
468 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
469 range_maximum =
std::max(range_maximum, segments_[i].end_y());
472 return range_maximum;
476 int64_t range_end)
const {
478 return Value(range_start);
480 return Value(range_end);
482 int start_segment = -1;
483 int end_segment = -1;
484 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
488 CHECK_GE(end_segment, start_segment);
498 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
499 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
500 range_minimum =
std::min(range_minimum, segments_[i].start_y());
502 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
503 range_minimum =
std::min(range_minimum, segments_[i].end_y());
506 return range_minimum;
510 return GetMaximum(segments_.front().start_x(), segments_.back().end_x());
514 return GetMinimum(segments_.front().start_x(), segments_.back().end_x());
517std::pair<int64_t, int64_t>
520 int64_t
value)
const {
524std::pair<int64_t, int64_t>
527 int64_t
value)
const {
532std::pair<int64_t, int64_t> ComputeXFromY(int64_t start_x, int64_t start_y,
533 int64_t slope, int64_t y) {
535 const int64_t delta_y =
CapSub(y, start_y);
536 const int64_t delta_x = delta_y / slope;
537 if ((delta_y >= 0 && slope >= 0) || (delta_y <= 0 && slope <= 0)) {
538 const int64_t delta_x_down = delta_x;
539 const int64_t delta_x_up = delta_y % slope == 0 ? delta_x : delta_x + 1;
540 return {delta_x_down + start_x, delta_x_up + start_x};
542 const int64_t delta_x_down = delta_y % slope == 0 ? delta_x : delta_x - 1;
543 const int64_t delta_x_up = -(-delta_y / slope);
544 return {delta_x_down + start_x, delta_x_up + start_x};
548std::pair<int64_t, int64_t> GetRangeInValueRange(int64_t start_x, int64_t end_x,
549 int64_t start_y, int64_t end_y,
553 if ((start_y > value_max && end_y > value_max) ||
554 (start_y < value_min && end_y < value_min)) {
558 if (start_y <= value_max && end_y <= value_max) {
559 x_range_max = {start_x, end_x};
560 }
else if (start_y <= value_max || end_y <= value_max) {
562 ? ComputeXFromY(end_x, end_y, slope, value_max)
563 : ComputeXFromY(start_x, start_y, slope, value_max);
564 if (end_y <= value_max) {
565 x_range_max = {x.second, end_x};
567 x_range_max = {start_x, x.first};
571 if (start_y >= value_min && end_y >= value_min) {
572 x_range_min = {start_x, end_x};
573 }
else if (start_y >= value_min || end_y >= value_min) {
575 ? ComputeXFromY(end_x, end_y, slope, value_min)
576 : ComputeXFromY(start_x, start_y, slope, value_min);
577 if (end_y >= value_min) {
578 x_range_min = {x.second, end_x};
580 x_range_min = {start_x, x.first};
583 if (x_range_min.first > x_range_max.second ||
584 x_range_max.first > x_range_min.second) {
587 return {
std::max(x_range_min.first, x_range_max.first),
588 std::min(x_range_min.second, x_range_max.second)};
592std::pair<int64_t, int64_t>
596 int64_t value_max)
const {
599 int start_segment = -1;
600 int end_segment = -1;
601 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
603 return {reduced_range_start, reduced_range_end};
605 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
606 const auto& segment = segments_[i];
607 const int64_t start_x =
std::max(range_start, segment.start_x());
608 const int64_t end_x =
std::min(range_end, segment.end_x());
609 const int64_t start_y = segment.Value(start_x);
610 const int64_t end_y = segment.Value(end_x);
611 const std::pair<int64_t, int64_t> range = GetRangeInValueRange(
612 start_x, end_x, start_y, end_y, segment.slope(), value_min, value_max);
613 reduced_range_start =
std::min(reduced_range_start, range.first);
614 reduced_range_end =
std::max(reduced_range_end, range.second);
616 return {reduced_range_start, reduced_range_end};
621 for (
int i = 0; i < segments_.size(); ++i) {
622 segments_[i].AddConstantToX(constant);
628 for (
int i = 0; i < segments_.size(); ++i) {
629 segments_[i].AddConstantToY(constant);
634 Operation(other, [](int64_t
a, int64_t
b) {
return CapAdd(
a,
b); });
638 Operation(other, [](int64_t
a, int64_t
b) {
return CapSub(
a,
b); });
641std::vector<PiecewiseLinearFunction*>
648 std::vector<PiecewiseLinearFunction*> convex_functions;
649 std::vector<PiecewiseSegment> convex_segments;
652 if (convex_segments.empty()) {
653 convex_segments.push_back(segment);
658 if (FormConvexPair(last, segment)) {
660 convex_segments.push_back(segment);
663 convex_segments.clear();
664 convex_segments.push_back(segment);
668 if (!convex_segments.empty()) {
669 convex_functions.push_back(
672 return convex_functions;
676 std::string result =
"PiecewiseLinearFunction(";
677 for (
int i = 0; i < segments_.size(); ++i) {
684void PiecewiseLinearFunction::InsertSegment(
const PiecewiseSegment& segment) {
687 if (segments_.empty() || segments_.back().end_x() < segment.
start_x()) {
688 segments_.push_back(segment);
693 if (segments_.back().end_x() == segment.
start_x()) {
694 if (segments_.back().end_y() == segment.
start_y() &&
695 segments_.back().slope() == segment.
slope()) {
696 segments_.back().ExpandEnd(segment.
end_x());
699 segments_.push_back(segment);
703void PiecewiseLinearFunction::Operation(
704 const PiecewiseLinearFunction& other,
705 const std::function<int64_t(int64_t, int64_t)>& operation) {
707 std::vector<PiecewiseSegment> own_segments;
708 const std::vector<PiecewiseSegment>& other_segments = other.segments();
709 own_segments.swap(segments_);
711 std::set<int64_t> start_x_points;
712 for (
int i = 0; i < own_segments.size(); ++i) {
713 start_x_points.insert(own_segments[i].start_x());
715 for (
int i = 0; i < other_segments.size(); ++i) {
716 start_x_points.insert(other_segments[i].start_x());
719 for (int64_t start_x : start_x_points) {
720 const int own_index = FindSegmentIndex(own_segments, start_x);
721 const int other_index = FindSegmentIndex(other_segments, start_x);
722 if (own_index >= 0 && other_index >= 0) {
723 const PiecewiseSegment& own_segment = own_segments[own_index];
724 const PiecewiseSegment& other_segment = other_segments[other_index];
726 const int64_t end_x =
727 std::min(own_segment.end_x(), other_segment.end_x());
728 const int64_t start_y =
729 operation(own_segment.Value(start_x), other_segment.Value(start_x));
730 const int64_t end_y =
731 operation(own_segment.Value(end_x), other_segment.Value(end_x));
732 const int64_t slope =
733 operation(own_segment.slope(), other_segment.slope());
735 int64_t point_x, point_y, other_point_x;
736 if (IsAtBounds(start_y)) {
739 other_point_x = start_x;
743 other_point_x = end_x;
745 InsertSegment(PiecewiseSegment(point_x, point_y, slope, other_point_x));
750bool PiecewiseLinearFunction::FindSegmentIndicesFromRange(
751 int64_t range_start, int64_t range_end,
int* start_segment,
752 int* end_segment)
const {
753 *start_segment = FindSegmentIndex(segments_, range_start);
754 *end_segment = FindSegmentIndex(segments_, range_end);
755 if (*start_segment == *end_segment) {
756 if (*start_segment < 0) {
760 if (segments_[*start_segment].end_x() < range_start) {
768bool PiecewiseLinearFunction::IsConvexInternal()
const {
769 for (
int i = 1; i < segments_.size(); ++i) {
770 if (!FormConvexPair(segments_[i - 1], segments_[i])) {
777bool PiecewiseLinearFunction::IsNonDecreasingInternal()
const {
779 for (
const auto& segment : segments_) {
780 const int64_t start_y = segment.
start_y();
781 const int64_t end_y = segment.
end_y();
782 if (end_y < start_y || start_y <
value)
return false;
788bool PiecewiseLinearFunction::IsNonIncreasingInternal()
const {
790 for (
const auto& segment : segments_) {
791 const int64_t start_y = segment.
start_y();
792 const int64_t end_y = segment.
end_y();
793 if (end_y > start_y || start_y >
value)
return false;
#define DCHECK_LE(val1, val2)
#define DCHECK_NE(val1, val2)
#define CHECK_EQ(val1, val2)
#define CHECK_GE(val1, val2)
#define CHECK_GT(val1, val2)
#define DCHECK_GE(val1, val2)
#define CHECK_LE(val1, val2)
int64_t GetMaximum() const
static PiecewiseLinearFunction * CreateRightRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
void AddConstantToX(int64_t constant)
static PiecewiseLinearFunction * CreateLeftRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
static PiecewiseLinearFunction * CreateStepFunction(std::vector< int64_t > points_x, std::vector< int64_t > points_y, std::vector< int64_t > other_points_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 const int kNotFound
int64_t Value(int64_t x) const
std::vector< PiecewiseLinearFunction * > DecomposeToConvexFunctions() const
std::string DebugString() const
void Add(const PiecewiseLinearFunction &other)
bool IsNonDecreasing() const
static PiecewiseLinearFunction * CreateOneSegmentFunction(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
int64_t GetMinimum() const
void AddConstantToY(int64_t constant)
bool IsNonIncreasing() 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)
std::pair< int64_t, int64_t > GetSmallestRangeLessThanValue(int64_t range_start, int64_t range_end, int64_t value) const
const std::vector< PiecewiseSegment > & segments() const
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)
static PiecewiseLinearFunction * CreateFullDomainFunction(int64_t initial_level, std::vector< int64_t > points_x, std::vector< int64_t > slopes)
static PiecewiseLinearFunction * CreateEarlyTardyFunction(int64_t reference, int64_t earliness_slope, int64_t tardiness_slope)
static PiecewiseLinearFunction * CreateFixedChargeFunction(int64_t slope, int64_t value)
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
std::string DebugString() const
static bool FindComparator(int64_t point, const PiecewiseSegment &segment)
void ExpandEnd(int64_t end_x)
void AddConstantToY(int64_t constant)
static bool SortComparator(const PiecewiseSegment &segment1, const PiecewiseSegment &segment2)
static const uint64_t kuint64max
static const int64_t kint64max
static const int64_t kint64min
Collection of objects used to extend the Constraint Solver library.
int64_t CapAdd(int64_t x, int64_t y)
int64_t CapSub(int64_t x, int64_t y)
int64_t CapProd(int64_t x, int64_t y)