Skip to content

Commit 9f4bf2a

Browse files
committed
[CP-SAT] remove dead code
1 parent 8b4a24a commit 9f4bf2a

File tree

3 files changed

+15
-58
lines changed

3 files changed

+15
-58
lines changed

ortools/sat/cumulative_energy_test.cc

+11-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,8 @@ std::string InstanceDebugString(const EnergyInstance& instance) {
8282
bool SolveUsingConstraint(const EnergyInstance& instance) {
8383
Model model;
8484
std::vector<IntervalVariable> intervals;
85-
std::vector<LinearExpression> energies;
85+
std::vector<std::vector<LiteralValueValue>> decomposed_energies;
8686
for (const auto& task : instance.tasks) {
87-
LinearExpression energy;
88-
energy.vars.push_back(
89-
model.Add(NewIntegerVariable(task.energy_min, task.energy_max)));
90-
energy.coeffs.push_back(IntegerValue(1));
91-
energies.push_back(energy);
9287
if (task.is_optional) {
9388
const Literal is_present = Literal(model.Add(NewBooleanVariable()), true);
9489
const IntegerVariable start =
@@ -103,6 +98,15 @@ bool SolveUsingConstraint(const EnergyInstance& instance) {
10398
intervals.push_back(model.Add(NewIntervalWithVariableSize(
10499
task.start_min, task.end_max, task.duration_min, task.duration_max)));
105100
}
101+
std::vector<Literal> energy_literals;
102+
std::vector<LiteralValueValue> energy_literals_values_values;
103+
for (int e = task.energy_min; e <= task.energy_max; ++e) {
104+
const Literal lit = Literal(model.Add(NewBooleanVariable()), true);
105+
energy_literals.push_back(lit);
106+
energy_literals_values_values.push_back({lit, e, 1});
107+
}
108+
model.Add(ExactlyOneConstraint(energy_literals));
109+
decomposed_energies.push_back(energy_literals_values_values);
106110
}
107111

108112
const AffineExpression capacity(
@@ -112,7 +116,7 @@ bool SolveUsingConstraint(const EnergyInstance& instance) {
112116
SchedulingConstraintHelper* helper = repo->GetOrCreateHelper(intervals);
113117
SchedulingDemandHelper* demands_helper =
114118
new SchedulingDemandHelper({}, helper, &model);
115-
demands_helper->OverrideLinearizedEnergies(energies);
119+
demands_helper->OverrideDecomposedEnergies(decomposed_energies);
116120
model.TakeOwnership(demands_helper);
117121

118122
AddCumulativeOverloadChecker(capacity, helper, demands_helper, &model);

ortools/sat/scheduling_helpers.cc

+4-44
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,6 @@ SchedulingDemandHelper::SchedulingDemandHelper(
720720
demands_(demands.begin(), demands.end()),
721721
helper_(helper) {
722722
const int num_tasks = helper->NumTasks();
723-
linearized_energies_.resize(num_tasks);
724723
decomposed_energies_.resize(num_tasks);
725724
cached_energies_min_.resize(num_tasks, kMinIntegerValue);
726725
cached_energies_max_.resize(num_tasks, kMaxIntegerValue);
@@ -747,11 +746,6 @@ IntegerValue SchedulingDemandHelper::SimpleEnergyMin(int t) const {
747746
return CapProdI(DemandMin(t), helper_->SizeMin(t));
748747
}
749748

750-
IntegerValue SchedulingDemandHelper::LinearEnergyMin(int t) const {
751-
if (!linearized_energies_[t].has_value()) return kMinIntegerValue;
752-
return linearized_energies_[t]->Min(*integer_trail_);
753-
}
754-
755749
IntegerValue SchedulingDemandHelper::DecomposedEnergyMin(int t) const {
756750
if (decomposed_energies_[t].empty()) return kMinIntegerValue;
757751
IntegerValue result = kMaxIntegerValue;
@@ -771,11 +765,6 @@ IntegerValue SchedulingDemandHelper::SimpleEnergyMax(int t) const {
771765
return CapProdI(DemandMax(t), helper_->SizeMax(t));
772766
}
773767

774-
IntegerValue SchedulingDemandHelper::LinearEnergyMax(int t) const {
775-
if (!linearized_energies_[t].has_value()) return kMaxIntegerValue;
776-
return linearized_energies_[t]->Max(*integer_trail_);
777-
}
778-
779768
IntegerValue SchedulingDemandHelper::DecomposedEnergyMax(int t) const {
780769
if (decomposed_energies_[t].empty()) return kMaxIntegerValue;
781770
IntegerValue result = kMinIntegerValue;
@@ -806,14 +795,14 @@ bool SchedulingDemandHelper::CacheAllEnergyValues() {
806795
decomposed_energies_[t].resize(new_size);
807796
}
808797

809-
cached_energies_min_[t] = std::max(
810-
{SimpleEnergyMin(t), LinearEnergyMin(t), DecomposedEnergyMin(t)});
798+
cached_energies_min_[t] =
799+
std::max(SimpleEnergyMin(t), DecomposedEnergyMin(t));
811800
if (cached_energies_min_[t] <= kMinIntegerValue) return false;
812801
energy_is_quadratic_[t] =
813802
decomposed_energies_[t].empty() && !demands_.empty() &&
814803
!integer_trail_->IsFixed(demands_[t]) && !helper_->SizeIsFixed(t);
815-
cached_energies_max_[t] = std::min(
816-
{SimpleEnergyMax(t), LinearEnergyMax(t), DecomposedEnergyMax(t)});
804+
cached_energies_max_[t] =
805+
std::min(SimpleEnergyMax(t), DecomposedEnergyMax(t));
817806
if (cached_energies_max_[t] >= kMaxIntegerValue) return false;
818807
}
819808

@@ -849,14 +838,6 @@ bool SchedulingDemandHelper::DecreaseEnergyMax(int t, IntegerValue value) {
849838
if (!helper_->PushLiteral(lit.Negated())) return false;
850839
}
851840
}
852-
} else if (linearized_energies_[t].has_value() &&
853-
linearized_energies_[t]->vars.size() == 1) {
854-
const LinearExpression& e = linearized_energies_[t].value();
855-
const AffineExpression affine_energy(e.vars[0], e.coeffs[0], e.offset);
856-
const IntegerLiteral deduction = affine_energy.LowerOrEqual(value);
857-
if (!helper_->PushIntegerLiteralIfTaskPresent(t, deduction)) {
858-
return false;
859-
}
860841
} else {
861842
// TODO(user): Propagate if possible.
862843
VLOG(3) << "Cumulative energy missed propagation";
@@ -900,12 +881,6 @@ void SchedulingDemandHelper::AddEnergyMinReason(int t) {
900881
} else if (SimpleEnergyMin(t) >= value) {
901882
AddDemandMinReason(t);
902883
helper_->AddSizeMinReason(t);
903-
} else {
904-
DCHECK_GE(LinearEnergyMin(t), value);
905-
for (const IntegerVariable var : linearized_energies_[t]->vars) {
906-
helper_->MutableIntegerReason()->push_back(
907-
integer_trail_->LowerBoundAsLiteral(var));
908-
}
909884
}
910885
}
911886

@@ -927,21 +902,6 @@ bool SchedulingDemandHelper::AddLinearizedDemand(
927902
return true;
928903
}
929904

930-
void SchedulingDemandHelper::OverrideLinearizedEnergies(
931-
absl::Span<const LinearExpression> energies) {
932-
const int num_tasks = energies.size();
933-
DCHECK_EQ(num_tasks, helper_->NumTasks());
934-
linearized_energies_.resize(num_tasks);
935-
for (int t = 0; t < num_tasks; ++t) {
936-
linearized_energies_[t] = energies[t];
937-
if (DEBUG_MODE) {
938-
for (const IntegerValue coeff : linearized_energies_[t]->coeffs) {
939-
DCHECK_GE(coeff, 0);
940-
}
941-
}
942-
}
943-
}
944-
945905
std::vector<LiteralValueValue> SchedulingDemandHelper::FilteredDecomposedEnergy(
946906
int index) {
947907
if (decomposed_energies_[index].empty()) return {};

ortools/sat/scheduling_helpers.h

-7
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ class SchedulingDemandHelper {
549549
}
550550

551551
// Visible for testing.
552-
void OverrideLinearizedEnergies(absl::Span<const LinearExpression> energies);
553552
void OverrideDecomposedEnergies(
554553
const std::vector<std::vector<LiteralValueValue>>& energies);
555554
// Returns the decomposed energy terms compatible with the current literal
@@ -565,9 +564,7 @@ class SchedulingDemandHelper {
565564

566565
private:
567566
IntegerValue SimpleEnergyMin(int t) const;
568-
IntegerValue LinearEnergyMin(int t) const;
569567
IntegerValue SimpleEnergyMax(int t) const;
570-
IntegerValue LinearEnergyMax(int t) const;
571568
IntegerValue DecomposedEnergyMin(int t) const;
572569
IntegerValue DecomposedEnergyMax(int t) const;
573570

@@ -586,10 +583,6 @@ class SchedulingDemandHelper {
586583
// A representation of the energies as a set of alternative.
587584
// If subvector is empty, we don't have this representation.
588585
std::vector<std::vector<LiteralValueValue>> decomposed_energies_;
589-
590-
// A representation of the energies as a set of linear expression.
591-
// If the optional is not set, we don't have this representation.
592-
std::vector<std::optional<LinearExpression>> linearized_energies_;
593586
};
594587

595588
// =============================================================================

0 commit comments

Comments
 (0)