21#include "absl/container/btree_set.h"
22#include "absl/strings/str_format.h"
33int FindSegmentIndex(
const std::vector<PiecewiseSegment>& segments, int64_t x) {
34 if (segments.empty() || segments.front().start_x() > x) {
42 if (position == segments.end()) {
43 return segments.size() - 1;
45 position -= position->start_x() > x ? 1 : 0;
47 return position - segments.begin();
50inline bool IsAtBounds(int64_t
value) {
54inline bool PointInsideRange(int64_t point, int64_t range_start,
56 return range_start <= point && range_end >= point;
61inline bool FormConvexPair(
const PiecewiseSegment& left,
62 const PiecewiseSegment& right) {
63 return right.slope() >= left.slope() && right.start_x() == left.end_x() &&
64 right.start_y() == left.end_y();
67uint64_t UnsignedCapAdd(uint64_t left, uint64_t right) {
71uint64_t UnsignedCapProd(uint64_t left, uint64_t right) {
72 if (right == 0)
return 0;
79 int64_t slope, int64_t other_point_x)
80 : slope_(slope), reference_x_(point_x), reference_y_(point_y) {
81 start_x_ =
std::min(point_x, other_point_x);
82 end_x_ =
std::max(point_x, other_point_x);
84 reference_x_ < 0 ? SafeValuePostReference(0) : SafeValuePreReference(0);
91 const int64_t span_x =
CapSub(x, reference_x_);
94 return SafeValuePostReference(x);
97 return SafeValuePreReference(x);
100 const int64_t span_y =
CapProd(slope_, span_x);
101 if (IsAtBounds(span_y)) {
103 return SafeValuePostReference(x);
105 return SafeValuePreReference(x);
109 const int64_t
value =
CapAdd(reference_y_, span_y);
110 if (IsAtBounds(
value)) {
112 return SafeValuePostReference(x);
114 return SafeValuePreReference(x);
121int64_t PiecewiseSegment::SafeValuePostReference(int64_t x)
const {
123 const uint64_t span_x =
static_cast<uint64_t
>(x) - reference_x_;
130 }
else if (slope_ > 0) {
132 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
133 if (reference_y_ == 0) {
135 }
else if (reference_y_ > 0) {
136 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
138 :
static_cast<int64_t
>(unsigned_sum);
140 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
141 if (span_y >= opp_reference_y) {
142 return span_y - opp_reference_y >
kint64max
144 :
static_cast<int64_t
>(span_y - opp_reference_y);
146 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
148 : -
static_cast<int64_t
>(opp_reference_y - span_y);
153 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
154 if (reference_y_ == 0) {
156 }
else if (reference_y_ < 0) {
157 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
158 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
161 : -
static_cast<int64_t
>(opp_unsigned_sum);
163 if (reference_y_ >= span_y) {
166 :
static_cast<int64_t
>(reference_y_ - span_y);
168 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
170 : -
static_cast<int64_t
>(span_y - reference_y_);
176int64_t PiecewiseSegment::SafeValuePreReference(int64_t x)
const {
178 const uint64_t span_x =
static_cast<uint64_t
>(reference_x_) - x;
182 }
else if (slope_ > 0) {
184 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
185 if (reference_y_ == 0) {
187 }
else if (reference_y_ > 0) {
188 if (reference_y_ >= span_y) {
191 :
static_cast<int64_t
>(reference_y_ - span_y);
193 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
195 : -
static_cast<uint64_t
>(span_y - reference_y_);
198 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
199 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
202 : -
static_cast<uint64_t
>(opp_unsigned_sum);
206 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
207 if (reference_y_ == 0) {
209 }
else if (reference_y_ < 0) {
210 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
211 if (span_y >= opp_reference_y) {
212 return span_y - opp_reference_y >
kint64max
214 :
static_cast<int64_t
>(span_y - opp_reference_y);
216 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
218 : -
static_cast<uint64_t
>(opp_reference_y - span_y);
221 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
223 :
static_cast<int64_t
>(unsigned_sum);
230 return segment1.start_x_ < segment2.start_x_;
261 std::string result = absl::StrFormat(
262 "PiecewiseSegment(<start: (%d, %d), end: (%d, %d), "
263 "reference: (%d, %d), slope = %d>)",
264 start_x_,
Value(start_x_), end_x_,
Value(end_x_), reference_x_,
265 reference_y_, slope_);
271PiecewiseLinearFunction::PiecewiseLinearFunction(
272 std::vector<PiecewiseSegment> segments)
273 : is_modified_(true),
275 is_non_decreasing_(false),
276 is_non_increasing_(false) {
280 for (
int i = 0; i < segments.size() - 1; ++i) {
282 LOG(
FATAL) <<
"Overlapping segments: " << segments[i].DebugString()
283 <<
" & " << segments[i + 1].DebugString();
287 for (
const auto& segment : segments) {
288 InsertSegment(segment);
293 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
294 std::vector<int64_t> slopes, std::vector<int64_t> other_points_x) {
295 CHECK_EQ(points_x.size(), points_y.size());
296 CHECK_EQ(points_x.size(), other_points_x.size());
297 CHECK_EQ(points_x.size(), slopes.size());
300 std::vector<PiecewiseSegment>
segments;
301 for (
int i = 0; i < points_x.size(); ++i) {
310 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
311 std::vector<int64_t> other_points_x) {
312 CHECK_EQ(points_x.size(), points_y.size());
313 CHECK_EQ(points_x.size(), other_points_x.size());
316 std::vector<PiecewiseSegment>
segments;
317 for (
int i = 0; i < points_x.size(); ++i) {
326 int64_t initial_level, std::vector<int64_t> points_x,
327 std::vector<int64_t> slopes) {
328 CHECK_EQ(points_x.size(), slopes.size() - 1);
331 int64_t level = initial_level;
332 std::vector<PiecewiseSegment>
segments;
336 level = segment.
Value(points_x[0]);
337 for (
int i = 1; i < points_x.size(); ++i) {
341 level = segment.
Value(points_x[i]);
350 int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x) {
353 std::vector<PiecewiseSegment>
segments = {
359 int64_t point_x, int64_t point_y, int64_t slope) {
360 std::vector<PiecewiseSegment>
segments = {
366 int64_t point_x, int64_t point_y, int64_t slope) {
367 std::vector<PiecewiseSegment>
segments = {
373 int64_t slope, int64_t
value) {
374 std::vector<PiecewiseSegment>
segments = {
383 int64_t reference, int64_t earliness_slope, int64_t tardiness_slope) {
384 std::vector<PiecewiseSegment>
segments = {
394 int64_t early_slack, int64_t late_slack, int64_t earliness_slope,
395 int64_t tardiness_slope) {
396 std::vector<PiecewiseSegment>
segments = {
407 int index = FindSegmentIndex(segments_, x);
411 if (segments_[
index].end_x() < x) {
424 return is_non_decreasing_;
429 return is_non_increasing_;
438 const int index = FindSegmentIndex(segments_, x);
439 return segments_[
index].Value(x);
443 int64_t range_end)
const {
445 return Value(range_end);
447 return Value(range_start);
449 int start_segment = -1;
450 int end_segment = -1;
451 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
455 CHECK_GE(end_segment, start_segment);
465 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
466 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
467 range_maximum =
std::max(range_maximum, segments_[i].start_y());
469 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
470 range_maximum =
std::max(range_maximum, segments_[i].end_y());
473 return range_maximum;
477 int64_t range_end)
const {
479 return Value(range_start);
481 return Value(range_end);
483 int start_segment = -1;
484 int end_segment = -1;
485 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
489 CHECK_GE(end_segment, start_segment);
499 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
500 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
501 range_minimum =
std::min(range_minimum, segments_[i].start_y());
503 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
504 range_minimum =
std::min(range_minimum, segments_[i].end_y());
507 return range_minimum;
511 return GetMaximum(segments_.front().start_x(), segments_.back().end_x());
515 return GetMinimum(segments_.front().start_x(), segments_.back().end_x());
518std::pair<int64_t, int64_t>
521 int64_t
value)
const {
525std::pair<int64_t, int64_t>
528 int64_t
value)
const {
533std::pair<int64_t, int64_t> ComputeXFromY(int64_t start_x, int64_t start_y,
534 int64_t slope, int64_t y) {
536 const int64_t delta_y =
CapSub(y, start_y);
537 const int64_t delta_x = delta_y / slope;
538 if ((delta_y >= 0 && slope >= 0) || (delta_y <= 0 && slope <= 0)) {
539 const int64_t delta_x_down = delta_x;
540 const int64_t delta_x_up = delta_y % slope == 0 ? delta_x : delta_x + 1;
541 return {delta_x_down + start_x, delta_x_up + start_x};
543 const int64_t delta_x_down = delta_y % slope == 0 ? delta_x : delta_x - 1;
544 const int64_t delta_x_up = -(-delta_y / slope);
545 return {delta_x_down + start_x, delta_x_up + start_x};
549std::pair<int64_t, int64_t> GetRangeInValueRange(int64_t start_x, int64_t end_x,
550 int64_t start_y, int64_t end_y,
554 if ((start_y > value_max && end_y > value_max) ||
555 (start_y < value_min && end_y < value_min)) {
559 if (start_y <= value_max && end_y <= value_max) {
560 x_range_max = {start_x, end_x};
561 }
else if (start_y <= value_max || end_y <= value_max) {
563 ? ComputeXFromY(end_x, end_y, slope, value_max)
564 : ComputeXFromY(start_x, start_y, slope, value_max);
565 if (end_y <= value_max) {
566 x_range_max = {x.second, end_x};
568 x_range_max = {start_x, x.first};
572 if (start_y >= value_min && end_y >= value_min) {
573 x_range_min = {start_x, end_x};
574 }
else if (start_y >= value_min || end_y >= value_min) {
576 ? ComputeXFromY(end_x, end_y, slope, value_min)
577 : ComputeXFromY(start_x, start_y, slope, value_min);
578 if (end_y >= value_min) {
579 x_range_min = {x.second, end_x};
581 x_range_min = {start_x, x.first};
584 if (x_range_min.first > x_range_max.second ||
585 x_range_max.first > x_range_min.second) {
588 return {
std::max(x_range_min.first, x_range_max.first),
589 std::min(x_range_min.second, x_range_max.second)};
593std::pair<int64_t, int64_t>
597 int64_t value_max)
const {
600 int start_segment = -1;
601 int end_segment = -1;
602 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
604 return {reduced_range_start, reduced_range_end};
606 for (
int i =
std::max(0, start_segment); i <= end_segment; ++i) {
607 const auto& segment = segments_[i];
608 const int64_t start_x =
std::max(range_start, segment.start_x());
609 const int64_t end_x =
std::min(range_end, segment.end_x());
610 const int64_t start_y = segment.Value(start_x);
611 const int64_t end_y = segment.Value(end_x);
612 const std::pair<int64_t, int64_t> range = GetRangeInValueRange(
613 start_x, end_x, start_y, end_y, segment.slope(), value_min, value_max);
614 reduced_range_start =
std::min(reduced_range_start, range.first);
615 reduced_range_end =
std::max(reduced_range_end, range.second);
617 return {reduced_range_start, reduced_range_end};
622 for (
int i = 0; i < segments_.size(); ++i) {
623 segments_[i].AddConstantToX(
constant);
629 for (
int i = 0; i < segments_.size(); ++i) {
630 segments_[i].AddConstantToY(
constant);
635 Operation(other, [](int64_t
a, int64_t
b) {
return CapAdd(
a,
b); });
639 Operation(other, [](int64_t
a, int64_t
b) {
return CapSub(
a,
b); });
642std::vector<PiecewiseLinearFunction*>
649 std::vector<PiecewiseLinearFunction*> convex_functions;
650 std::vector<PiecewiseSegment> convex_segments;
653 if (convex_segments.empty()) {
654 convex_segments.push_back(segment);
659 if (FormConvexPair(last, segment)) {
661 convex_segments.push_back(segment);
664 convex_segments.clear();
665 convex_segments.push_back(segment);
669 if (!convex_segments.empty()) {
670 convex_functions.push_back(
673 return convex_functions;
677 std::string result =
"PiecewiseLinearFunction(";
678 for (
int i = 0; i < segments_.size(); ++i) {
685void PiecewiseLinearFunction::InsertSegment(
const PiecewiseSegment& segment) {
688 if (segments_.empty() || segments_.back().end_x() < segment.
start_x()) {
689 segments_.push_back(segment);
694 if (segments_.back().end_x() == segment.
start_x()) {
695 if (segments_.back().end_y() == segment.
start_y() &&
696 segments_.back().slope() == segment.
slope()) {
697 segments_.back().ExpandEnd(segment.
end_x());
700 segments_.push_back(segment);
704void PiecewiseLinearFunction::Operation(
705 const PiecewiseLinearFunction& other,
706 const std::function<int64_t(int64_t, int64_t)>& operation) {
708 std::vector<PiecewiseSegment> own_segments;
709 const std::vector<PiecewiseSegment>& other_segments = other.segments();
710 own_segments.swap(segments_);
712 absl::btree_set<int64_t> start_x_points;
713 for (
int i = 0; i < own_segments.size(); ++i) {
714 start_x_points.insert(own_segments[i].start_x());
716 for (
int i = 0; i < other_segments.size(); ++i) {
717 start_x_points.insert(other_segments[i].start_x());
720 for (int64_t start_x : start_x_points) {
721 const int own_index = FindSegmentIndex(own_segments, start_x);
722 const int other_index = FindSegmentIndex(other_segments, start_x);
723 if (own_index >= 0 && other_index >= 0) {
724 const PiecewiseSegment& own_segment = own_segments[own_index];
725 const PiecewiseSegment& other_segment = other_segments[other_index];
727 const int64_t end_x =
728 std::min(own_segment.end_x(), other_segment.end_x());
729 const int64_t start_y =
730 operation(own_segment.Value(start_x), other_segment.Value(start_x));
731 const int64_t end_y =
732 operation(own_segment.Value(end_x), other_segment.Value(end_x));
733 const int64_t slope =
734 operation(own_segment.slope(), other_segment.slope());
736 int64_t point_x, point_y, other_point_x;
737 if (IsAtBounds(start_y)) {
740 other_point_x = start_x;
744 other_point_x = end_x;
746 InsertSegment(PiecewiseSegment(point_x, point_y, slope, other_point_x));
751bool PiecewiseLinearFunction::FindSegmentIndicesFromRange(
752 int64_t range_start, int64_t range_end,
int* start_segment,
753 int* end_segment)
const {
754 *start_segment = FindSegmentIndex(segments_, range_start);
755 *end_segment = FindSegmentIndex(segments_, range_end);
756 if (*start_segment == *end_segment) {
757 if (*start_segment < 0) {
761 if (segments_[*start_segment].end_x() < range_start) {
769bool PiecewiseLinearFunction::IsConvexInternal()
const {
770 for (
int i = 1; i < segments_.size(); ++i) {
771 if (!FormConvexPair(segments_[i - 1], segments_[i])) {
778bool PiecewiseLinearFunction::IsNonDecreasingInternal()
const {
780 for (
const auto& segment : segments_) {
781 const int64_t start_y = segment.
start_y();
782 const int64_t end_y = segment.
end_y();
783 if (end_y < start_y || start_y <
value)
return false;
789bool PiecewiseLinearFunction::IsNonIncreasingInternal()
const {
791 for (
const auto& segment : segments_) {
792 const int64_t start_y = segment.
start_y();
793 const int64_t end_y = segment.
end_y();
794 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)