[CP-SAT] small fixes, reindent

This commit is contained in:
Laurent Perron
2025-04-30 13:54:23 +02:00
parent 6549049cf6
commit 1de64adc76
3 changed files with 43 additions and 37 deletions

View File

@@ -2335,6 +2335,19 @@ void RegisterSearchStatisticCallback(Model* global_model) {
response->set_num_lp_iterations(num_lp_iters);
});
}
void MergeParamsWithFlagsAndDefaults(SatParameters* params) {
#if !defined(__PORTABLE_PLATFORM__)
// Override parameters?
if (!absl::GetFlag(FLAGS_cp_model_params).empty()) {
SatParameters flag_params;
CHECK(google::protobuf::TextFormat::ParseFromString(
absl::GetFlag(FLAGS_cp_model_params), &flag_params));
params->MergeFrom(flag_params);
}
#endif // __PORTABLE_PLATFORM__
}
} // namespace
CpSolverResponse SolveCpModel(const CpModelProto& model_proto, Model* model) {
@@ -2355,20 +2368,12 @@ CpSolverResponse SolveCpModel(const CpModelProto& model_proto, Model* model) {
DumpModelProto(model_proto, model_proto.name());
}
}
// Override parameters?
if (!absl::GetFlag(FLAGS_cp_model_params).empty()) {
SatParameters params = *model->GetOrCreate<SatParameters>();
SatParameters flag_params;
CHECK(google::protobuf::TextFormat::ParseFromString(
absl::GetFlag(FLAGS_cp_model_params), &flag_params));
params.MergeFrom(flag_params);
*(model->GetOrCreate<SatParameters>()) = params;
}
#endif // __PORTABLE_PLATFORM__
// Enable the logging component.
MergeParamsWithFlagsAndDefaults(model->GetOrCreate<SatParameters>());
const SatParameters& params = *model->GetOrCreate<SatParameters>();
// Enable the logging component.
SolverLogger* logger = model->GetOrCreate<SolverLogger>();
logger->EnableLogging(params.log_search_progress());
logger->SetLogToStdOut(params.log_to_stdout());

View File

@@ -134,33 +134,33 @@ public sealed class CpSolver : IDisposable
{
switch (expr)
{
case LinearExprBuilder a:
constant += coefficient * a.Offset;
if (coefficient == 1)
case LinearExprBuilder a:
constant += coefficient * a.Offset;
if (coefficient == 1)
{
foreach (var sub in a.Terms)
{
foreach (var sub in a.Terms)
{
_terms.Enqueue(sub);
}
_terms.Enqueue(sub);
}
else
}
else
{
foreach (var sub in a.Terms)
{
foreach (var sub in a.Terms)
{
_terms.Enqueue(new Term(sub.expr, sub.coefficient * coefficient));
}
_terms.Enqueue(new Term(sub.expr, sub.coefficient * coefficient));
}
}
break;
case IntVar intVar:
var index = intVar.GetIndex();
var value = index >= 0 ? Response!.Solution[index] : -Response!.Solution[-index - 1];
constant += coefficient * value;
break;
case NotBoolVar:
throw new ArgumentException("Cannot evaluate a literal in an integer expression.");
default:
throw new ArgumentException("Cannot evaluate '" + expr + "' in an integer expression");
break;
case IntVar intVar:
var index = intVar.GetIndex();
var value = index >= 0 ? Response!.Solution[index] : -Response!.Solution[-index - 1];
constant += coefficient * value;
break;
case NotBoolVar:
throw new ArgumentException("Cannot evaluate a literal in an integer expression.");
default:
throw new ArgumentException("Cannot evaluate '" + expr + "' in an integer expression");
}
if (!_terms.TryDequeue(out var term))
@@ -170,8 +170,7 @@ public sealed class CpSolver : IDisposable
expr = term.expr;
coefficient = term.coefficient;
}
while (true);
} while (true);
return constant;
}
@@ -250,7 +249,6 @@ class BestBoundCallbackDelegate : BestBoundCallback
public BestBoundCallbackDelegate(DoubleToVoidDelegate del) => _delegate = del;
public override void NewBestBound(double bound) => _delegate(bound);
}
} // namespace Google.OrTools.Sat

View File

@@ -794,7 +794,10 @@ bool PresolveContext::HasUnusedAffineVariable() const {
// TODO(user): Also test var_to_constraints_ !!
bool PresolveContext::ConstraintVariableUsageIsConsistent() {
if (is_unsat_) return true; // We do not care in this case.
// We do not care in these cases.
if (is_unsat_) return true;
if (time_limit_->LimitReached()) return true;
if (var_to_constraints_.size() != working_model->variables_size()) {
LOG(INFO) << "Wrong var_to_constraints_ size!";
return false;