2012-03-28 18:50:03 +00:00
|
|
|
// Copyright 2010-2012 Google
|
2010-09-15 12:42:33 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// This file implements the core objects of the constraint solver:
|
|
|
|
|
// Solver, Search, Queue, ... along with the main resolution loop.
|
|
|
|
|
|
|
|
|
|
#include "constraint_solver/constraint_solver.h"
|
|
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
2011-09-21 15:16:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <iosfwd>
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
#include "base/callback.h"
|
|
|
|
|
#include "base/commandlineflags.h"
|
|
|
|
|
#include "base/integral_types.h"
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
#include "base/macros.h"
|
|
|
|
|
#include "base/scoped_ptr.h"
|
|
|
|
|
#include "base/stringprintf.h"
|
2011-09-05 13:45:29 +00:00
|
|
|
#include "base/file.h"
|
|
|
|
|
#include "base/recordio.h"
|
2010-09-15 12:42:33 +00:00
|
|
|
#include "base/stringpiece.h"
|
2011-09-21 15:16:48 +00:00
|
|
|
#include "zlib.h"
|
2011-06-28 09:27:03 +00:00
|
|
|
#include "base/concise_iterator.h"
|
2010-09-15 12:42:33 +00:00
|
|
|
#include "base/map-util.h"
|
2011-12-15 11:50:32 +00:00
|
|
|
#include "base/stl_util.h"
|
2010-09-15 12:42:33 +00:00
|
|
|
#include "constraint_solver/constraint_solveri.h"
|
2011-09-05 13:45:29 +00:00
|
|
|
#include "constraint_solver/model.pb.h"
|
2011-07-11 20:13:14 +00:00
|
|
|
#include "util/const_int_array.h"
|
2012-03-15 16:22:13 +00:00
|
|
|
#include "util/tuple_set.h"
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
DEFINE_bool(cp_trace_propagation,
|
|
|
|
|
false,
|
2011-11-16 17:32:24 +00:00
|
|
|
"Trace propagation events (constraint and demon executions,"
|
|
|
|
|
" variable modifications).");
|
2012-01-17 15:06:01 +00:00
|
|
|
DEFINE_bool(cp_trace_search, false, "Trace search events");
|
2010-09-15 12:42:33 +00:00
|
|
|
DEFINE_bool(cp_show_constraints, false,
|
2011-07-11 20:13:14 +00:00
|
|
|
"show all constraints added to the solver.");
|
2011-07-13 19:51:33 +00:00
|
|
|
DEFINE_bool(cp_print_model, false,
|
2011-07-11 20:13:14 +00:00
|
|
|
"use PrintModelVisitor on model before solving.");
|
2011-07-13 19:51:33 +00:00
|
|
|
DEFINE_bool(cp_model_stats, false,
|
|
|
|
|
"use StatisticsModelVisitor on model before solving.");
|
2011-09-05 13:45:29 +00:00
|
|
|
DEFINE_string(cp_export_file, "", "Export model to file using CPModelProto.");
|
2011-09-19 13:00:59 +00:00
|
|
|
DEFINE_bool(cp_no_solve, false, "Force failure at the beginning of a search.");
|
|
|
|
|
DEFINE_string(cp_profile_file, "", "Export profiling overview to file.");
|
2011-11-09 10:59:31 +00:00
|
|
|
DEFINE_bool(cp_verbose_fail, false, "Verbose output when failing.");
|
2011-11-19 01:59:37 +00:00
|
|
|
DEFINE_bool(cp_name_variables, false, "Force all variables to have names.");
|
2012-07-04 13:29:41 +00:00
|
|
|
DEFINE_bool(cp_name_cast_variables, false,
|
|
|
|
|
"Name variables casted from expressions");
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-11-03 10:27:53 +00:00
|
|
|
void ConstraintSolverFailsHere() {
|
2010-09-15 12:42:33 +00:00
|
|
|
VLOG(3) << "Fail";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) // WINDOWS
|
|
|
|
|
#pragma warning(disable : 4351 4355)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
|
2011-09-13 08:18:38 +00:00
|
|
|
// ----- SolverParameters -----
|
|
|
|
|
|
|
|
|
|
SolverParameters::SolverParameters()
|
|
|
|
|
: compress_trail(kDefaultTrailCompression),
|
|
|
|
|
trail_block_size(kDefaultTrailBlockSize),
|
|
|
|
|
array_split_size(kDefaultArraySplitSize),
|
|
|
|
|
store_names(kDefaultNameStoring),
|
2011-11-09 10:59:31 +00:00
|
|
|
profile_level(kDefaultProfileLevel),
|
2011-11-19 01:59:37 +00:00
|
|
|
trace_level(kDefaultTraceLevel),
|
|
|
|
|
name_all_variables(kDefaultNameAllVariables) {}
|
2011-09-13 08:18:38 +00:00
|
|
|
|
2011-09-13 09:24:49 +00:00
|
|
|
// ----- Forward Declarations and Profiling Support -----
|
2011-11-19 01:59:37 +00:00
|
|
|
extern DemonProfiler* BuildDemonProfiler(Solver* const solver);
|
|
|
|
|
extern void DeleteDemonProfiler(DemonProfiler* const monitor);
|
|
|
|
|
extern void InstallDemonProfiler(DemonProfiler* const monitor);
|
2011-11-16 17:32:24 +00:00
|
|
|
|
2011-11-09 10:59:31 +00:00
|
|
|
|
|
|
|
|
// TODO(user): remove this complex logic.
|
|
|
|
|
// We need the double test because parameters are set too late when using
|
|
|
|
|
// python in the open source. This is the cheapest work-around.
|
2011-11-10 09:36:43 +00:00
|
|
|
bool Solver::InstrumentsDemons() const {
|
2011-11-19 01:59:37 +00:00
|
|
|
return IsProfilingEnabled() || InstrumentsVariables();
|
2011-11-14 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::IsProfilingEnabled() const {
|
2011-09-13 09:24:49 +00:00
|
|
|
return parameters_.profile_level != SolverParameters::NO_PROFILING ||
|
|
|
|
|
!FLAGS_cp_profile_file.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-10 09:36:43 +00:00
|
|
|
bool Solver::InstrumentsVariables() const {
|
2011-11-09 10:59:31 +00:00
|
|
|
return parameters_.trace_level != SolverParameters::NO_TRACE ||
|
2011-11-14 20:55:35 +00:00
|
|
|
FLAGS_cp_trace_propagation;
|
2011-11-09 10:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
bool Solver::NameAllVariables() const {
|
|
|
|
|
return parameters_.name_all_variables || FLAGS_cp_name_variables;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// ------------------ Demon class ----------------
|
|
|
|
|
|
|
|
|
|
Solver::DemonPriority Demon::priority() const {
|
|
|
|
|
return Solver::NORMAL_PRIORITY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Demon::DebugString() const {
|
|
|
|
|
return "Demon";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Demon::inhibit(Solver* const s) {
|
|
|
|
|
if (stamp_ < kuint64max) {
|
|
|
|
|
s->SaveAndSetValue(&stamp_, kuint64max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Demon::desinhibit(Solver* const s) {
|
|
|
|
|
if (stamp_ == kuint64max) {
|
|
|
|
|
s->SaveAndSetValue(&stamp_, s->stamp() - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------ Action class ------------------
|
|
|
|
|
|
|
|
|
|
string Action::DebugString() const {
|
|
|
|
|
return "Action";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------ Queue class ------------------
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
namespace {
|
2012-09-07 13:45:02 +00:00
|
|
|
class FifoPriorityQueue {
|
2010-09-15 12:42:33 +00:00
|
|
|
public:
|
|
|
|
|
struct Cell {
|
2010-12-06 16:29:32 +00:00
|
|
|
explicit Cell(Demon* const d) : demon(d), next(NULL) {}
|
|
|
|
|
Demon* demon;
|
|
|
|
|
Cell* next;
|
2010-09-15 12:42:33 +00:00
|
|
|
};
|
|
|
|
|
|
2010-12-06 16:29:32 +00:00
|
|
|
FifoPriorityQueue() : first_(NULL), last_(NULL), free_cells_(NULL) {}
|
|
|
|
|
|
2012-09-07 13:45:02 +00:00
|
|
|
~FifoPriorityQueue() {
|
2010-12-06 16:29:32 +00:00
|
|
|
while (first_ != NULL) {
|
|
|
|
|
Cell* const tmp = first_;
|
|
|
|
|
first_ = tmp->next;
|
|
|
|
|
delete tmp;
|
|
|
|
|
}
|
|
|
|
|
while (free_cells_ != NULL) {
|
|
|
|
|
Cell* const tmp = free_cells_;
|
|
|
|
|
free_cells_ = tmp->next;
|
|
|
|
|
delete tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 13:45:02 +00:00
|
|
|
Demon* Next() {
|
2010-12-06 16:29:32 +00:00
|
|
|
if (first_ != NULL) {
|
|
|
|
|
DCHECK(last_ != NULL);
|
|
|
|
|
Cell* const tmp_cell = first_;
|
|
|
|
|
Demon* const demon = tmp_cell->demon;
|
|
|
|
|
first_ = tmp_cell->next;
|
|
|
|
|
if (first_ == NULL) {
|
|
|
|
|
last_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
tmp_cell->next = free_cells_;
|
|
|
|
|
free_cells_ = tmp_cell;
|
|
|
|
|
return demon;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 13:45:02 +00:00
|
|
|
void Enqueue(Demon* const d) {
|
2010-12-06 16:29:32 +00:00
|
|
|
Cell* cell = free_cells_;
|
|
|
|
|
if (cell != NULL) {
|
|
|
|
|
cell->demon = d;
|
|
|
|
|
free_cells_ = cell->next;
|
|
|
|
|
cell->next = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
cell = new Cell(d);
|
|
|
|
|
}
|
|
|
|
|
if (last_ != NULL) {
|
|
|
|
|
last_->next = cell;
|
|
|
|
|
last_ = cell;
|
|
|
|
|
} else {
|
|
|
|
|
first_ = cell;
|
|
|
|
|
last_ = cell;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 13:45:02 +00:00
|
|
|
void AfterFailure() {
|
2010-12-06 16:29:32 +00:00
|
|
|
if (first_ != NULL) {
|
|
|
|
|
last_->next = free_cells_;
|
|
|
|
|
free_cells_ = first_;
|
|
|
|
|
first_ = NULL;
|
|
|
|
|
last_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Cell* first_;
|
|
|
|
|
Cell* last_;
|
|
|
|
|
Cell* free_cells_;
|
|
|
|
|
};
|
2011-08-13 01:52:01 +00:00
|
|
|
} // namespace
|
2010-12-06 16:29:32 +00:00
|
|
|
|
|
|
|
|
class Queue {
|
|
|
|
|
public:
|
2010-09-15 12:42:33 +00:00
|
|
|
explicit Queue(Solver* const s)
|
|
|
|
|
: solver_(s),
|
|
|
|
|
stamp_(1),
|
2010-10-26 08:49:34 +00:00
|
|
|
freeze_level_(0),
|
2010-09-15 12:42:33 +00:00
|
|
|
in_process_(false),
|
2010-11-15 15:08:17 +00:00
|
|
|
clear_action_(NULL),
|
2011-11-16 17:32:24 +00:00
|
|
|
in_add_(false),
|
|
|
|
|
instruments_demons_(s->InstrumentsDemons()) {
|
2010-09-15 12:42:33 +00:00
|
|
|
for (int i = 0; i < Solver::kNumPriorities; ++i) {
|
2010-12-06 16:29:32 +00:00
|
|
|
containers_[i] = new FifoPriorityQueue();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Queue() {
|
|
|
|
|
for (int i = 0; i < Solver::kNumPriorities; ++i) {
|
2010-12-06 16:29:32 +00:00
|
|
|
delete containers_[i];
|
|
|
|
|
containers_[i] = NULL;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Freeze() {
|
2010-10-26 08:49:34 +00:00
|
|
|
freeze_level_++;
|
2010-09-15 12:42:33 +00:00
|
|
|
stamp_++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Unfreeze() {
|
2010-10-26 08:49:34 +00:00
|
|
|
freeze_level_--;
|
|
|
|
|
ProcessIfUnfrozen();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-07 12:14:10 +00:00
|
|
|
void ProcessOneDemon(Demon* const demon) {
|
|
|
|
|
demon->set_stamp(stamp_ - 1);
|
|
|
|
|
const int prio = demon->priority();
|
|
|
|
|
DCHECK_EQ(prio, demon->priority());
|
|
|
|
|
if (instruments_demons_) {
|
|
|
|
|
solver_->GetPropagationMonitor()->BeginDemonRun(demon);
|
|
|
|
|
}
|
|
|
|
|
solver_->demon_runs_[prio]++;
|
|
|
|
|
if (solver_->demon_runs_[prio] % 10000 == 0) {
|
|
|
|
|
solver_->TopPeriodicCheck();
|
|
|
|
|
}
|
|
|
|
|
demon->Run(solver_);
|
|
|
|
|
if (instruments_demons_) {
|
|
|
|
|
solver_->GetPropagationMonitor()->EndDemonRun(demon);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
void ProcessNormalDemons() {
|
2012-09-07 12:14:10 +00:00
|
|
|
Demon* demon = NULL;
|
2012-09-07 12:28:40 +00:00
|
|
|
while ((demon = containers_[Solver::NORMAL_PRIORITY]->Next()) !=
|
2012-09-07 12:14:10 +00:00
|
|
|
NULL) {
|
|
|
|
|
ProcessOneDemon(demon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
void Process() {
|
|
|
|
|
if (!in_process_) {
|
|
|
|
|
in_process_ = true;
|
2012-09-07 12:28:40 +00:00
|
|
|
Demon* d = NULL;
|
|
|
|
|
while ((d = containers_[Solver::NORMAL_PRIORITY]->Next()) != NULL ||
|
|
|
|
|
(d = containers_[Solver::VAR_PRIORITY]->Next()) != NULL ||
|
|
|
|
|
(d = containers_[Solver::DELAYED_PRIORITY]->Next()) != NULL) {
|
|
|
|
|
ProcessOneDemon(d);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
in_process_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 16:29:32 +00:00
|
|
|
void Enqueue(Demon* const demon) {
|
|
|
|
|
if (demon->stamp() < stamp_) {
|
|
|
|
|
demon->set_stamp(stamp_);
|
|
|
|
|
containers_[demon->priority()]->Enqueue(demon);
|
2010-10-26 08:49:34 +00:00
|
|
|
ProcessIfUnfrozen();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 16:29:32 +00:00
|
|
|
void AfterFailure() {
|
2010-09-15 12:42:33 +00:00
|
|
|
for (int i = 0; i < Solver::kNumPriorities; ++i) {
|
2010-12-06 16:29:32 +00:00
|
|
|
containers_[i]->AfterFailure();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
2010-12-06 16:29:32 +00:00
|
|
|
if (clear_action_ != NULL) {
|
2010-09-15 12:42:33 +00:00
|
|
|
clear_action_->Run(solver_);
|
2010-12-06 16:29:32 +00:00
|
|
|
clear_action_ = NULL;
|
|
|
|
|
}
|
2010-10-26 08:49:34 +00:00
|
|
|
freeze_level_ = 0;
|
2010-09-15 12:42:33 +00:00
|
|
|
in_process_ = false;
|
2010-11-15 15:08:17 +00:00
|
|
|
in_add_ = false;
|
|
|
|
|
to_add_.clear();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void increase_stamp() {
|
|
|
|
|
stamp_++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64 stamp() const {
|
|
|
|
|
return stamp_;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 16:29:32 +00:00
|
|
|
void set_action_on_fail(Action* const a) {
|
2010-09-15 12:42:33 +00:00
|
|
|
clear_action_ = a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear_action_on_fail() {
|
|
|
|
|
clear_action_ = NULL;
|
|
|
|
|
}
|
2010-11-15 15:08:17 +00:00
|
|
|
|
|
|
|
|
void AddConstraint(Constraint* const c) {
|
|
|
|
|
to_add_.push_back(c);
|
|
|
|
|
ProcessConstraints();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProcessConstraints() {
|
|
|
|
|
if (!in_add_) {
|
|
|
|
|
in_add_ = true;
|
|
|
|
|
// We cannot store to_add_.size() as constraints can add other
|
|
|
|
|
// constraints.
|
|
|
|
|
for (int counter = 0; counter < to_add_.size(); ++counter) {
|
|
|
|
|
Constraint* const constraint = to_add_[counter];
|
2011-01-06 17:40:30 +00:00
|
|
|
// TODO(user): Add profiling to initial propagation
|
2010-11-15 15:08:17 +00:00
|
|
|
constraint->PostAndPropagate();
|
|
|
|
|
}
|
|
|
|
|
in_add_ = false;
|
|
|
|
|
to_add_.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-28 09:27:03 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
private:
|
2010-10-26 08:49:34 +00:00
|
|
|
void ProcessIfUnfrozen() {
|
|
|
|
|
if (freeze_level_ == 0) {
|
|
|
|
|
Process();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
Solver* const solver_;
|
2012-09-07 13:45:02 +00:00
|
|
|
FifoPriorityQueue* containers_[Solver::kNumPriorities];
|
2010-09-15 12:42:33 +00:00
|
|
|
uint64 stamp_;
|
2010-10-26 08:49:34 +00:00
|
|
|
// The number of nested freeze levels. The queue is frozen if and only if
|
|
|
|
|
// freeze_level_ > 0.
|
|
|
|
|
uint32 freeze_level_;
|
2010-09-15 12:42:33 +00:00
|
|
|
bool in_process_;
|
|
|
|
|
Action* clear_action_;
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<Constraint*> to_add_;
|
2010-11-15 15:08:17 +00:00
|
|
|
bool in_add_;
|
2011-11-16 17:32:24 +00:00
|
|
|
const bool instruments_demons_;
|
2010-09-15 12:42:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ------------------ StateMarker / StateInfo struct -----------
|
|
|
|
|
|
|
|
|
|
struct StateInfo { // This is an internal structure to store
|
|
|
|
|
// additional information on the choice point.
|
|
|
|
|
public:
|
|
|
|
|
StateInfo() : ptr_info(NULL), int_info(0), depth(0), left_depth(0) {}
|
|
|
|
|
StateInfo(void* pinfo, int iinfo)
|
|
|
|
|
: ptr_info(pinfo), int_info(iinfo), depth(0), left_depth(0) {}
|
|
|
|
|
StateInfo(void* pinfo, int iinfo, int d, int ld)
|
|
|
|
|
: ptr_info(pinfo), int_info(iinfo), depth(d), left_depth(ld) {}
|
|
|
|
|
void* ptr_info;
|
|
|
|
|
int int_info;
|
|
|
|
|
int depth;
|
|
|
|
|
int left_depth;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct StateMarker {
|
|
|
|
|
public:
|
2011-11-07 15:31:18 +00:00
|
|
|
StateMarker(Solver::MarkerType t, const StateInfo& info);
|
2010-09-15 12:42:33 +00:00
|
|
|
friend class Solver;
|
|
|
|
|
friend struct Trail;
|
|
|
|
|
private:
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType type_;
|
2010-09-15 12:42:33 +00:00
|
|
|
int rev_int_index_;
|
|
|
|
|
int rev_int64_index_;
|
|
|
|
|
int rev_uint64_index_;
|
|
|
|
|
int rev_ptr_index_;
|
|
|
|
|
int rev_boolvar_list_index_;
|
|
|
|
|
int rev_bools_index_;
|
|
|
|
|
int rev_int_memory_index_;
|
|
|
|
|
int rev_int64_memory_index_;
|
|
|
|
|
int rev_object_memory_index_;
|
|
|
|
|
int rev_object_array_memory_index_;
|
|
|
|
|
int rev_memory_index_;
|
|
|
|
|
int rev_memory_array_index_;
|
|
|
|
|
StateInfo info_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
StateMarker::StateMarker(Solver::MarkerType t, const StateInfo& info)
|
2010-09-15 12:42:33 +00:00
|
|
|
: type_(t),
|
|
|
|
|
rev_int_index_(0),
|
|
|
|
|
rev_int64_index_(0),
|
|
|
|
|
rev_uint64_index_(0),
|
|
|
|
|
rev_ptr_index_(0),
|
|
|
|
|
rev_boolvar_list_index_(0),
|
|
|
|
|
rev_bools_index_(0),
|
|
|
|
|
rev_int_memory_index_(0),
|
|
|
|
|
rev_int64_memory_index_(0),
|
|
|
|
|
rev_object_memory_index_(0),
|
|
|
|
|
rev_object_array_memory_index_(0),
|
|
|
|
|
info_(info) {}
|
|
|
|
|
|
|
|
|
|
// ---------- Trail and Reversibility ----------
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
namespace {
|
2010-09-15 12:42:33 +00:00
|
|
|
// ----- addrval struct -----
|
|
|
|
|
|
|
|
|
|
// This template class is used internally to implement reversibility.
|
|
|
|
|
// It stores an address and the value that was at the address.
|
|
|
|
|
template <class T> struct addrval {
|
|
|
|
|
public:
|
|
|
|
|
addrval() : address_(NULL) {}
|
|
|
|
|
explicit addrval(T* adr) : address_(adr), old_value_(*adr) {}
|
|
|
|
|
void restore() const { (*address_) = old_value_; }
|
|
|
|
|
private:
|
|
|
|
|
T* address_;
|
|
|
|
|
T old_value_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----- Compressed trail -----
|
|
|
|
|
|
|
|
|
|
// ---------- Trail Packer ---------
|
|
|
|
|
// Abstract class to pack trail blocks.
|
|
|
|
|
|
|
|
|
|
template <class T> class TrailPacker {
|
|
|
|
|
public:
|
|
|
|
|
explicit TrailPacker(int block_size) : block_size_(block_size) {}
|
|
|
|
|
virtual ~TrailPacker() {}
|
|
|
|
|
int input_size() const { return block_size_ * sizeof(addrval<T>); }
|
|
|
|
|
virtual void Pack(const addrval<T>* block, string* packed_block) = 0;
|
|
|
|
|
virtual void Unpack(const string& packed_block, addrval<T>* block) = 0;
|
|
|
|
|
private:
|
|
|
|
|
const int block_size_;
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TrailPacker);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> class NoCompressionTrailPacker : public TrailPacker<T> {
|
|
|
|
|
public:
|
|
|
|
|
explicit NoCompressionTrailPacker(int block_size)
|
|
|
|
|
: TrailPacker<T>(block_size) {}
|
|
|
|
|
virtual ~NoCompressionTrailPacker() {}
|
|
|
|
|
virtual void Pack(const addrval<T>* block, string* packed_block) {
|
|
|
|
|
DCHECK(block != NULL);
|
|
|
|
|
DCHECK(packed_block != NULL);
|
|
|
|
|
StringPiece block_str;
|
|
|
|
|
block_str.set(block, this->input_size());
|
|
|
|
|
block_str.CopyToString(packed_block);
|
|
|
|
|
}
|
|
|
|
|
virtual void Unpack(const string& packed_block, addrval<T>* block) {
|
|
|
|
|
DCHECK(block != NULL);
|
|
|
|
|
memcpy(block, packed_block.c_str(), packed_block.size());
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NoCompressionTrailPacker<T>);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T> class ZlibTrailPacker : public TrailPacker<T> {
|
|
|
|
|
public:
|
|
|
|
|
explicit ZlibTrailPacker(int block_size)
|
|
|
|
|
: TrailPacker<T>(block_size),
|
|
|
|
|
tmp_size_(compressBound(this->input_size())),
|
|
|
|
|
tmp_block_(new char[tmp_size_]) {}
|
|
|
|
|
|
|
|
|
|
virtual ~ZlibTrailPacker() {}
|
|
|
|
|
|
|
|
|
|
virtual void Pack(const addrval<T>* block, string* packed_block) {
|
|
|
|
|
DCHECK(block != NULL);
|
|
|
|
|
DCHECK(packed_block != NULL);
|
|
|
|
|
uLongf size = tmp_size_;
|
|
|
|
|
const int result = compress(reinterpret_cast<Bytef*>(tmp_block_.get()),
|
|
|
|
|
&size,
|
|
|
|
|
reinterpret_cast<const Bytef*>(block),
|
|
|
|
|
this->input_size());
|
|
|
|
|
CHECK_EQ(Z_OK, result);
|
|
|
|
|
StringPiece block_str;
|
|
|
|
|
block_str.set(tmp_block_.get(), size);
|
|
|
|
|
block_str.CopyToString(packed_block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Unpack(const string& packed_block, addrval<T>* block) {
|
|
|
|
|
DCHECK(block != NULL);
|
|
|
|
|
uLongf size = this->input_size();
|
|
|
|
|
const int result =
|
|
|
|
|
uncompress(reinterpret_cast<Bytef*>(block),
|
|
|
|
|
&size,
|
|
|
|
|
reinterpret_cast<const Bytef*>(packed_block.c_str()),
|
|
|
|
|
packed_block.size());
|
|
|
|
|
CHECK_EQ(Z_OK, result);
|
|
|
|
|
}
|
2011-06-28 09:27:03 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
private:
|
|
|
|
|
const uint64 tmp_size_;
|
|
|
|
|
scoped_array<char> tmp_block_;
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ZlibTrailPacker<T>);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> class CompressedTrail {
|
|
|
|
|
public:
|
2010-12-06 10:59:35 +00:00
|
|
|
CompressedTrail(int block_size,
|
|
|
|
|
SolverParameters::TrailCompression compression_level)
|
2010-09-15 12:42:33 +00:00
|
|
|
: block_size_(block_size),
|
|
|
|
|
blocks_(NULL),
|
|
|
|
|
free_blocks_(NULL),
|
|
|
|
|
data_(new addrval<T>[block_size]),
|
|
|
|
|
buffer_(new addrval<T>[block_size]),
|
|
|
|
|
buffer_used_(false),
|
|
|
|
|
current_(0),
|
|
|
|
|
size_(0) {
|
|
|
|
|
switch (compression_level) {
|
2010-12-06 10:59:35 +00:00
|
|
|
case SolverParameters::NO_COMPRESSION: {
|
2010-09-15 12:42:33 +00:00
|
|
|
packer_.reset(new NoCompressionTrailPacker<T>(block_size));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-12-06 10:59:35 +00:00
|
|
|
case SolverParameters::COMPRESS_WITH_ZLIB: {
|
|
|
|
|
packer_.reset(new ZlibTrailPacker<T>(block_size));
|
2010-09-15 12:42:33 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We zero all memory used by addrval arrays.
|
|
|
|
|
// Because of padding, all bytes may not be initialized, while compression
|
|
|
|
|
// will read them all, even if the uninitialized bytes are never used.
|
|
|
|
|
// This makes valgrind happy.
|
|
|
|
|
|
|
|
|
|
memset(data_.get(), 0, sizeof(*data_.get()) * block_size);
|
|
|
|
|
memset(buffer_.get(), 0, sizeof(*buffer_.get()) * block_size);
|
|
|
|
|
}
|
|
|
|
|
~CompressedTrail() {
|
|
|
|
|
FreeBlocks(blocks_);
|
|
|
|
|
FreeBlocks(free_blocks_);
|
|
|
|
|
}
|
|
|
|
|
const addrval<T>& Back() const {
|
|
|
|
|
// Back of empty trail.
|
|
|
|
|
DCHECK_GT(current_, 0);
|
|
|
|
|
return data_[current_ - 1];
|
|
|
|
|
}
|
|
|
|
|
void PopBack() {
|
|
|
|
|
if (size_ > 0) {
|
|
|
|
|
--current_;
|
|
|
|
|
if (current_ <= 0) {
|
|
|
|
|
if (buffer_used_) {
|
|
|
|
|
data_.swap(buffer_);
|
|
|
|
|
current_ = block_size_;
|
|
|
|
|
buffer_used_ = false;
|
|
|
|
|
} else if (blocks_ != NULL) {
|
|
|
|
|
packer_->Unpack(blocks_->compressed, data_.get());
|
|
|
|
|
FreeTopBlock();
|
|
|
|
|
current_ = block_size_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
--size_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void PushBack(const addrval<T>& addr_val) {
|
|
|
|
|
if (current_ >= block_size_) {
|
|
|
|
|
if (buffer_used_) { // Buffer is used.
|
|
|
|
|
NewTopBlock();
|
|
|
|
|
packer_->Pack(buffer_.get(), &blocks_->compressed);
|
|
|
|
|
// O(1) operation.
|
|
|
|
|
data_.swap(buffer_);
|
|
|
|
|
} else {
|
|
|
|
|
data_.swap(buffer_);
|
|
|
|
|
buffer_used_ = true;
|
|
|
|
|
}
|
|
|
|
|
current_ = 0;
|
|
|
|
|
}
|
|
|
|
|
data_[current_] = addr_val;
|
|
|
|
|
++current_;
|
|
|
|
|
++size_;
|
|
|
|
|
}
|
|
|
|
|
int size() const { return size_; }
|
2011-06-28 09:27:03 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
private:
|
|
|
|
|
struct Block {
|
|
|
|
|
string compressed;
|
|
|
|
|
Block* next;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void FreeTopBlock() {
|
|
|
|
|
Block* block = blocks_;
|
|
|
|
|
blocks_ = block->next;
|
|
|
|
|
block->compressed.clear();
|
|
|
|
|
block->next = free_blocks_;
|
|
|
|
|
free_blocks_ = block;
|
|
|
|
|
}
|
|
|
|
|
void NewTopBlock() {
|
|
|
|
|
Block* block = NULL;
|
|
|
|
|
if (free_blocks_ != NULL) {
|
|
|
|
|
block = free_blocks_;
|
|
|
|
|
free_blocks_ = block->next;
|
|
|
|
|
} else {
|
|
|
|
|
block = new Block;
|
|
|
|
|
}
|
|
|
|
|
block->next = blocks_;
|
|
|
|
|
blocks_ = block;
|
|
|
|
|
}
|
|
|
|
|
void FreeBlocks(Block* blocks) {
|
|
|
|
|
while (NULL != blocks) {
|
|
|
|
|
Block* next = blocks->next;
|
|
|
|
|
delete blocks;
|
|
|
|
|
blocks = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scoped_ptr<TrailPacker<T> > packer_;
|
|
|
|
|
const int block_size_;
|
|
|
|
|
Block* blocks_;
|
|
|
|
|
Block* free_blocks_;
|
|
|
|
|
scoped_array<addrval<T> > data_;
|
|
|
|
|
scoped_array<addrval<T> > buffer_;
|
|
|
|
|
bool buffer_used_;
|
|
|
|
|
int current_;
|
|
|
|
|
int size_;
|
|
|
|
|
};
|
2011-08-11 07:26:19 +00:00
|
|
|
} // namespace
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
// ----- Trail -----
|
|
|
|
|
|
|
|
|
|
// Object are explicitely copied using the copy ctor instead of
|
|
|
|
|
// passing and storing a pointer. As objects are small, copying is
|
|
|
|
|
// much faster than allocating (around 35% on a complete solve).
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
extern void RestoreBoolValue(IntVar* const var);
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
struct Trail {
|
|
|
|
|
CompressedTrail<int> rev_ints_;
|
|
|
|
|
CompressedTrail<int64> rev_int64s_;
|
|
|
|
|
CompressedTrail<uint64> rev_uint64s_;
|
|
|
|
|
CompressedTrail<void*> rev_ptrs_;
|
2011-08-11 05:15:18 +00:00
|
|
|
std::vector<IntVar*> rev_boolvar_list_;
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<bool*> rev_bools_;
|
|
|
|
|
std::vector<bool> rev_bool_value_;
|
|
|
|
|
std::vector<int*> rev_int_memory_;
|
|
|
|
|
std::vector<int64*> rev_int64_memory_;
|
|
|
|
|
std::vector<BaseObject*> rev_object_memory_;
|
|
|
|
|
std::vector<BaseObject**> rev_object_array_memory_;
|
|
|
|
|
std::vector<void*> rev_memory_;
|
|
|
|
|
std::vector<void**> rev_memory_array_;
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2010-12-06 10:59:35 +00:00
|
|
|
Trail(int block_size, SolverParameters::TrailCompression compression_level)
|
|
|
|
|
: rev_ints_(block_size, compression_level),
|
|
|
|
|
rev_int64s_(block_size, compression_level),
|
|
|
|
|
rev_uint64s_(block_size, compression_level),
|
|
|
|
|
rev_ptrs_(block_size, compression_level) {}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
void BacktrackTo(StateMarker* m) {
|
|
|
|
|
int target = m->rev_int_index_;
|
|
|
|
|
for (int curr = rev_ints_.size(); curr > target; --curr) {
|
|
|
|
|
const addrval<int>& cell = rev_ints_.Back();
|
|
|
|
|
cell.restore();
|
|
|
|
|
rev_ints_.PopBack();
|
|
|
|
|
}
|
|
|
|
|
DCHECK_EQ(rev_ints_.size(), target);
|
|
|
|
|
// Incorrect trail size after backtrack.
|
|
|
|
|
target = m->rev_int64_index_;
|
|
|
|
|
for (int curr = rev_int64s_.size(); curr > target; --curr) {
|
|
|
|
|
const addrval<int64>& cell = rev_int64s_.Back();
|
|
|
|
|
cell.restore();
|
|
|
|
|
rev_int64s_.PopBack();
|
|
|
|
|
}
|
|
|
|
|
DCHECK_EQ(rev_int64s_.size(), target);
|
|
|
|
|
// Incorrect trail size after backtrack.
|
|
|
|
|
target = m->rev_uint64_index_;
|
|
|
|
|
for (int curr = rev_uint64s_.size(); curr > target; --curr) {
|
|
|
|
|
const addrval<uint64>& cell = rev_uint64s_.Back();
|
|
|
|
|
cell.restore();
|
|
|
|
|
rev_uint64s_.PopBack();
|
|
|
|
|
}
|
|
|
|
|
DCHECK_EQ(rev_uint64s_.size(), target);
|
|
|
|
|
// Incorrect trail size after backtrack.
|
|
|
|
|
target = m->rev_ptr_index_;
|
|
|
|
|
for (int curr = rev_ptrs_.size(); curr > target; --curr) {
|
|
|
|
|
const addrval<void*>& cell = rev_ptrs_.Back();
|
|
|
|
|
cell.restore();
|
|
|
|
|
rev_ptrs_.PopBack();
|
|
|
|
|
}
|
|
|
|
|
DCHECK_EQ(rev_ptrs_.size(), target);
|
|
|
|
|
// Incorrect trail size after backtrack.
|
|
|
|
|
target = m->rev_boolvar_list_index_;
|
|
|
|
|
for (int curr = rev_boolvar_list_.size() - 1; curr >= target; --curr) {
|
2011-08-11 05:15:18 +00:00
|
|
|
IntVar* const var = rev_boolvar_list_[curr];
|
2010-09-15 12:42:33 +00:00
|
|
|
RestoreBoolValue(var);
|
|
|
|
|
}
|
|
|
|
|
rev_boolvar_list_.resize(target);
|
|
|
|
|
|
|
|
|
|
DCHECK_EQ(rev_bools_.size(), rev_bool_value_.size());
|
|
|
|
|
target = m->rev_bools_index_;
|
|
|
|
|
for (int curr = rev_bools_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
*(rev_bools_[curr]) = rev_bool_value_[curr];
|
|
|
|
|
}
|
|
|
|
|
rev_bools_.resize(target);
|
|
|
|
|
rev_bool_value_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_int_memory_index_;
|
|
|
|
|
for (int curr = rev_int_memory_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
delete[] rev_int_memory_[curr];
|
|
|
|
|
}
|
|
|
|
|
rev_int_memory_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_int64_memory_index_;
|
|
|
|
|
for (int curr = rev_int64_memory_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
delete[] rev_int64_memory_[curr];
|
|
|
|
|
}
|
|
|
|
|
rev_int64_memory_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_object_memory_index_;
|
|
|
|
|
for (int curr = rev_object_memory_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
delete rev_object_memory_[curr];
|
|
|
|
|
}
|
|
|
|
|
rev_object_memory_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_object_array_memory_index_;
|
|
|
|
|
for (int curr = rev_object_array_memory_.size() - 1;
|
|
|
|
|
curr >= target; --curr) {
|
|
|
|
|
delete[] rev_object_array_memory_[curr];
|
|
|
|
|
}
|
|
|
|
|
rev_object_array_memory_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_memory_index_;
|
|
|
|
|
for (int curr = rev_memory_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
delete reinterpret_cast<char*>(rev_memory_[curr]);
|
|
|
|
|
// The previous cast is necessary to deallocate generic memory
|
|
|
|
|
// described by a void* when passed to the RevAlloc procedure
|
|
|
|
|
// We cannot do a delete[] there
|
|
|
|
|
// This is useful for cells of RevFIFO and should not be used outside
|
|
|
|
|
// of the product
|
|
|
|
|
}
|
|
|
|
|
rev_memory_.resize(target);
|
|
|
|
|
|
|
|
|
|
target = m->rev_memory_array_index_;
|
|
|
|
|
for (int curr = rev_memory_array_.size() - 1; curr >= target; --curr) {
|
|
|
|
|
delete [] rev_memory_array_[curr];
|
|
|
|
|
// delete [] version of the previous unsafe case.
|
|
|
|
|
}
|
|
|
|
|
rev_memory_array_.resize(target);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void Solver::InternalSaveValue(int* valptr) {
|
|
|
|
|
trail_->rev_ints_.PushBack(addrval<int>(valptr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::InternalSaveValue(int64* valptr) {
|
|
|
|
|
trail_->rev_int64s_.PushBack(addrval<int64>(valptr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::InternalSaveValue(uint64* valptr) {
|
|
|
|
|
trail_->rev_uint64s_.PushBack(addrval<uint64>(valptr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::InternalSaveValue(void** valptr) {
|
|
|
|
|
trail_->rev_ptrs_.PushBack(addrval<void*>(valptr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(user) : this code is unsafe if you save the same alternating
|
|
|
|
|
// bool multiple times.
|
|
|
|
|
// The correct code should use a bitset and a single list.
|
|
|
|
|
void Solver::InternalSaveValue(bool* valptr) {
|
|
|
|
|
trail_->rev_bools_.push_back(valptr);
|
|
|
|
|
trail_->rev_bool_value_.push_back(*valptr);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
BaseObject* Solver::SafeRevAlloc(BaseObject* ptr) {
|
2010-09-15 12:42:33 +00:00
|
|
|
check_alloc_state();
|
2011-11-14 20:55:35 +00:00
|
|
|
trail_->rev_object_memory_.push_back(ptr);
|
2010-09-15 12:42:33 +00:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
int* Solver::SafeRevAllocArray(int* ptr) {
|
2010-09-15 12:42:33 +00:00
|
|
|
check_alloc_state();
|
2011-11-14 20:55:35 +00:00
|
|
|
trail_->rev_int_memory_.push_back(ptr);
|
2010-09-15 12:42:33 +00:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
int64* Solver::SafeRevAllocArray(int64* ptr) {
|
2010-09-15 12:42:33 +00:00
|
|
|
check_alloc_state();
|
2011-11-14 20:55:35 +00:00
|
|
|
trail_->rev_int64_memory_.push_back(ptr);
|
2010-09-15 12:42:33 +00:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
uint64* Solver::SafeRevAllocArray(uint64* ptr) {
|
2010-09-15 12:42:33 +00:00
|
|
|
check_alloc_state();
|
2011-11-14 20:55:35 +00:00
|
|
|
trail_->rev_int64_memory_.push_back(reinterpret_cast<int64*>(ptr));
|
2010-09-15 12:42:33 +00:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
BaseObject** Solver::SafeRevAllocArray(BaseObject** ptr) {
|
2010-09-15 12:42:33 +00:00
|
|
|
check_alloc_state();
|
|
|
|
|
trail_->rev_object_array_memory_.push_back(ptr);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
IntVar** Solver::SafeRevAllocArray(IntVar** ptr) {
|
|
|
|
|
BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
|
2010-09-15 12:42:33 +00:00
|
|
|
return reinterpret_cast<IntVar**>(in);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
IntExpr** Solver::SafeRevAllocArray(IntExpr** ptr) {
|
|
|
|
|
BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
|
2010-09-15 12:42:33 +00:00
|
|
|
return reinterpret_cast<IntExpr**>(in);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 20:55:35 +00:00
|
|
|
Constraint** Solver::SafeRevAllocArray(Constraint** ptr) {
|
|
|
|
|
BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
|
2010-09-15 12:42:33 +00:00
|
|
|
return reinterpret_cast<Constraint**>(in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* Solver::UnsafeRevAllocAux(void* ptr) {
|
|
|
|
|
check_alloc_state();
|
|
|
|
|
trail_->rev_memory_.push_back(ptr);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void** Solver::UnsafeRevAllocArrayAux(void** ptr) {
|
|
|
|
|
check_alloc_state();
|
|
|
|
|
trail_->rev_memory_array_.push_back(ptr);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void InternalSaveBooleanVarValue(Solver* const solver, IntVar* const var) {
|
|
|
|
|
solver->trail_->rev_boolvar_list_.push_back(var);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------ Search class -----------------
|
|
|
|
|
|
|
|
|
|
class Search {
|
|
|
|
|
public:
|
|
|
|
|
explicit Search(Solver* const s)
|
|
|
|
|
: solver_(s), marker_stack_(), fail_buffer_(), solution_counter_(0),
|
|
|
|
|
decision_builder_(NULL), created_by_solve_(false),
|
|
|
|
|
selector_(NULL), search_depth_(0), left_search_depth_(0),
|
|
|
|
|
should_restart_(false), should_finish_(false),
|
2012-05-17 15:43:34 +00:00
|
|
|
sentinel_pushed_(0), jmpbuf_filled_(false),
|
2012-06-13 16:14:46 +00:00
|
|
|
backtrack_at_the_end_of_the_search_(true) {}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-12-15 11:50:32 +00:00
|
|
|
// Constructor for a dummy search. The only difference between a dummy search
|
|
|
|
|
// and a regular one is that the search depth and left search depth is
|
|
|
|
|
// initialized to -1 instead of zero.
|
|
|
|
|
Search(Solver* const s, int /* dummy_argument */)
|
|
|
|
|
: solver_(s), marker_stack_(), fail_buffer_(), solution_counter_(0),
|
|
|
|
|
decision_builder_(NULL), created_by_solve_(false),
|
|
|
|
|
selector_(NULL), search_depth_(-1), left_search_depth_(-1),
|
|
|
|
|
should_restart_(false), should_finish_(false),
|
2012-05-17 15:43:34 +00:00
|
|
|
sentinel_pushed_(0), jmpbuf_filled_(false),
|
2012-06-13 16:14:46 +00:00
|
|
|
backtrack_at_the_end_of_the_search_(true) {}
|
2011-12-15 11:50:32 +00:00
|
|
|
|
|
|
|
|
~Search() {
|
|
|
|
|
STLDeleteElements(&marker_stack_);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
void EnterSearch();
|
|
|
|
|
void RestartSearch();
|
|
|
|
|
void ExitSearch();
|
|
|
|
|
void BeginNextDecision(DecisionBuilder* const b);
|
|
|
|
|
void EndNextDecision(DecisionBuilder* const b, Decision* const d);
|
|
|
|
|
void ApplyDecision(Decision* const d);
|
2011-03-31 12:28:12 +00:00
|
|
|
void AfterDecision(Decision* const d, bool apply);
|
2010-09-15 12:42:33 +00:00
|
|
|
void RefuteDecision(Decision* const d);
|
|
|
|
|
void BeginFail();
|
|
|
|
|
void EndFail();
|
|
|
|
|
void BeginInitialPropagation();
|
|
|
|
|
void EndInitialPropagation();
|
2010-10-06 16:04:31 +00:00
|
|
|
bool AtSolution();
|
|
|
|
|
bool AcceptSolution();
|
2010-09-15 12:42:33 +00:00
|
|
|
void NoMoreSolutions();
|
|
|
|
|
bool LocalOptimum();
|
|
|
|
|
bool AcceptDelta(Assignment* delta, Assignment* deltadelta);
|
|
|
|
|
void AcceptNeighbor();
|
|
|
|
|
void PeriodicCheck();
|
2012-02-09 19:42:58 +00:00
|
|
|
int ProgressPercent();
|
2011-08-11 05:15:18 +00:00
|
|
|
void Accept(ModelVisitor* const visitor) const;
|
2010-09-15 12:42:33 +00:00
|
|
|
void push_monitor(SearchMonitor* const m);
|
|
|
|
|
void Clear();
|
|
|
|
|
void IncrementSolutionCounter() { ++solution_counter_; }
|
|
|
|
|
int64 solution_counter() const { return solution_counter_; }
|
|
|
|
|
void set_decision_builder(DecisionBuilder* const db) {
|
|
|
|
|
decision_builder_ = db;
|
|
|
|
|
}
|
|
|
|
|
DecisionBuilder* decision_builder() const { return decision_builder_; }
|
|
|
|
|
void set_created_by_solve(bool c) { created_by_solve_ = c; }
|
|
|
|
|
bool created_by_solve() const { return created_by_solve_; }
|
|
|
|
|
Solver::DecisionModification ModifyDecision();
|
|
|
|
|
void SetBranchSelector(
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const s);
|
|
|
|
|
void LeftMove() {
|
|
|
|
|
search_depth_++;
|
|
|
|
|
left_search_depth_++;
|
|
|
|
|
}
|
|
|
|
|
void RightMove() {
|
|
|
|
|
search_depth_++;
|
|
|
|
|
}
|
2012-06-13 16:14:46 +00:00
|
|
|
bool backtrack_at_the_end_of_the_search() const {
|
|
|
|
|
return backtrack_at_the_end_of_the_search_;
|
|
|
|
|
}
|
|
|
|
|
void set_backtrack_at_the_end_of_the_search(bool restore) {
|
|
|
|
|
backtrack_at_the_end_of_the_search_ = restore;
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
int search_depth() const { return search_depth_; }
|
|
|
|
|
void set_search_depth(int d) { search_depth_ = d; }
|
|
|
|
|
int left_search_depth() const { return left_search_depth_; }
|
|
|
|
|
void set_search_left_depth(int d) { left_search_depth_ = d; }
|
|
|
|
|
void set_should_restart(bool s) { should_restart_ = s; }
|
|
|
|
|
bool should_restart() const { return should_restart_; }
|
|
|
|
|
void set_should_finish(bool s) { should_finish_ = s; }
|
|
|
|
|
bool should_finish() const { return should_finish_; }
|
|
|
|
|
void CheckFail() {
|
|
|
|
|
if (should_finish_ || should_restart_) {
|
|
|
|
|
solver_->Fail();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
friend class Solver;
|
|
|
|
|
private:
|
|
|
|
|
// Jumps back to the previous choice point, Checks if it was correctly set.
|
2010-09-16 09:15:57 +00:00
|
|
|
void JumpBack();
|
2010-09-15 12:42:33 +00:00
|
|
|
void ClearBuffer() {
|
2010-09-16 14:16:51 +00:00
|
|
|
CHECK(jmpbuf_filled_) << "Internal error in backtracking";
|
2010-09-15 12:42:33 +00:00
|
|
|
jmpbuf_filled_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Solver* const solver_;
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<StateMarker*> marker_stack_;
|
|
|
|
|
std::vector<SearchMonitor*> monitors_;
|
2010-09-15 12:42:33 +00:00
|
|
|
jmp_buf fail_buffer_;
|
|
|
|
|
int64 solution_counter_;
|
|
|
|
|
DecisionBuilder* decision_builder_;
|
|
|
|
|
bool created_by_solve_;
|
|
|
|
|
scoped_ptr<ResultCallback1<Solver::DecisionModification, Solver*> > selector_;
|
|
|
|
|
int search_depth_;
|
|
|
|
|
int left_search_depth_;
|
|
|
|
|
bool should_restart_;
|
|
|
|
|
bool should_finish_;
|
|
|
|
|
int sentinel_pushed_;
|
|
|
|
|
bool jmpbuf_filled_;
|
2012-06-13 16:14:46 +00:00
|
|
|
bool backtrack_at_the_end_of_the_search_;
|
2010-09-15 12:42:33 +00:00
|
|
|
};
|
|
|
|
|
|
2010-09-16 09:15:57 +00:00
|
|
|
// Backtrack is implemented using 3 primitives:
|
|
|
|
|
// CP_TRY to start searching
|
2010-09-16 14:16:51 +00:00
|
|
|
// CP_DO_FAIL to signal a failure. The program will continue on the CP_ON_FAIL
|
2010-09-16 09:15:57 +00:00
|
|
|
// primitive.
|
2010-09-16 14:16:51 +00:00
|
|
|
// CP_FAST_BACKTRACK protects an implementation of backtrack using
|
|
|
|
|
// setjmp/longjmp. The clean portable way is to use exceptions,
|
2010-09-16 09:15:57 +00:00
|
|
|
// unfortunately, it can be much slower. Thus we use ideas from
|
2010-09-16 14:16:51 +00:00
|
|
|
// Prolog, CP/CLP implementations, continuations in C and implement failing
|
|
|
|
|
// and backtracking using setjmp/longjmp.
|
|
|
|
|
#define CP_FAST_BACKTRACK
|
|
|
|
|
#if defined(CP_FAST_BACKTRACK)
|
|
|
|
|
// We cannot use a method/function for this as we would lose the
|
2010-09-16 09:15:57 +00:00
|
|
|
// context in the setjmp implementation.
|
|
|
|
|
#define CP_TRY(search) \
|
2010-09-15 12:42:33 +00:00
|
|
|
CHECK(!search->jmpbuf_filled_) << "Fail() called outside search"; \
|
|
|
|
|
search->jmpbuf_filled_ = true; \
|
|
|
|
|
if (setjmp(search->fail_buffer_) == 0)
|
2010-09-16 14:16:51 +00:00
|
|
|
#define CP_ON_FAIL else
|
|
|
|
|
#define CP_DO_FAIL(search) longjmp(search->fail_buffer_, 1)
|
|
|
|
|
#else // CP_FAST_BACKTRACK
|
2010-09-16 09:15:57 +00:00
|
|
|
class FailException {};
|
|
|
|
|
#define CP_TRY(search) \
|
|
|
|
|
CHECK(!search->jmpbuf_filled_) << "Fail() called outside search"; \
|
|
|
|
|
search->jmpbuf_filled_ = true; \
|
|
|
|
|
try
|
2010-09-16 14:16:51 +00:00
|
|
|
#define CP_ON_FAIL catch(FailException&)
|
|
|
|
|
#define CP_DO_FAIL(search) throw FailException()
|
|
|
|
|
#endif // CP_FAST_BACKTRACK
|
2010-09-16 09:15:57 +00:00
|
|
|
|
|
|
|
|
void Search::JumpBack() {
|
2012-01-04 22:35:44 +00:00
|
|
|
if (jmpbuf_filled_) {
|
|
|
|
|
jmpbuf_filled_ = false;
|
|
|
|
|
CP_DO_FAIL(this);
|
|
|
|
|
} else {
|
|
|
|
|
string explanation = "Failure outside of search";
|
|
|
|
|
solver_->AddConstraint(solver_->MakeFalseConstraint(explanation));
|
|
|
|
|
}
|
2010-09-16 09:15:57 +00:00
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-08-13 01:52:01 +00:00
|
|
|
Search* Solver::ActiveSearch() const {
|
2011-12-15 11:50:32 +00:00
|
|
|
return searches_.back();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
namespace {
|
2010-09-15 12:42:33 +00:00
|
|
|
class UndoBranchSelector : public Action {
|
|
|
|
|
public:
|
|
|
|
|
explicit UndoBranchSelector(int depth) : depth_(depth) {}
|
|
|
|
|
virtual ~UndoBranchSelector() {}
|
|
|
|
|
virtual void Run(Solver* const s) {
|
2011-08-11 07:26:19 +00:00
|
|
|
if (s->SolveDepth() == depth_) {
|
2011-08-13 01:52:01 +00:00
|
|
|
s->ActiveSearch()->SetBranchSelector(NULL);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual string DebugString() const {
|
|
|
|
|
return StringPrintf("UndoBranchSelector(%i)", depth_);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
const int depth_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ApplyBranchSelector : public DecisionBuilder {
|
|
|
|
|
public:
|
|
|
|
|
explicit ApplyBranchSelector(
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const bs)
|
|
|
|
|
: selector_(bs) {}
|
|
|
|
|
virtual ~ApplyBranchSelector() {}
|
|
|
|
|
|
|
|
|
|
virtual Decision* Next(Solver* const s) {
|
|
|
|
|
s->SetBranchSelector(selector_);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual string DebugString() const {
|
|
|
|
|
return "Apply(BranchSelector)";
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const selector_;
|
|
|
|
|
};
|
2011-08-11 07:26:19 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2011-08-13 01:52:01 +00:00
|
|
|
void Search::SetBranchSelector(
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
|
|
|
|
|
CHECK(bs == selector_ || selector_ == NULL || bs == NULL);
|
|
|
|
|
if (selector_ != bs) {
|
|
|
|
|
selector_.reset(bs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
void Solver::SetBranchSelector(
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
|
|
|
|
|
bs->CheckIsRepeatable();
|
2011-12-15 11:50:32 +00:00
|
|
|
// We cannot use the trail as the search can be nested and thus
|
|
|
|
|
// deleted upon backtrack. Thus we guard the undo action by a
|
|
|
|
|
// check on the number of nesting of solve().
|
|
|
|
|
AddBacktrackAction(RevAlloc(new UndoBranchSelector(SolveDepth())),
|
|
|
|
|
false);
|
|
|
|
|
searches_.back()->SetBranchSelector(bs);
|
2011-08-11 07:26:19 +00:00
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
DecisionBuilder* Solver::MakeApplyBranchSelector(
|
|
|
|
|
ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
|
|
|
|
|
return RevAlloc(new ApplyBranchSelector(bs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Solver::SolveDepth() const {
|
2012-02-14 14:16:12 +00:00
|
|
|
return state_ == OUTSIDE_SEARCH ? 0 : searches_.size() - 1;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Solver::SearchDepth() const {
|
2011-12-15 11:50:32 +00:00
|
|
|
return searches_.back()->search_depth();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Solver::SearchLeftDepth() const {
|
2011-12-15 11:50:32 +00:00
|
|
|
return searches_.back()->left_search_depth();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Solver::DecisionModification Search::ModifyDecision() {
|
|
|
|
|
if (selector_ != NULL) {
|
|
|
|
|
return selector_->Run(solver_);
|
|
|
|
|
}
|
|
|
|
|
return Solver::NO_CHANGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::push_monitor(SearchMonitor* const m) {
|
|
|
|
|
if (m) {
|
|
|
|
|
monitors_.push_back(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::Clear() {
|
|
|
|
|
monitors_.clear();
|
|
|
|
|
search_depth_ = 0;
|
|
|
|
|
left_search_depth_ = 0;
|
2011-08-13 01:52:01 +00:00
|
|
|
selector_.reset(NULL);
|
2012-06-13 16:14:46 +00:00
|
|
|
backtrack_at_the_end_of_the_search_ = true;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::EnterSearch() {
|
2011-04-11 15:00:18 +00:00
|
|
|
// The solution counter is reset when entering search and not when
|
|
|
|
|
// leaving search. This enables the information to persist outside of
|
|
|
|
|
// top-level search.
|
2011-03-03 21:38:41 +00:00
|
|
|
solution_counter_ = 0;
|
|
|
|
|
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->EnterSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::ExitSearch() {
|
2012-06-13 16:14:46 +00:00
|
|
|
// Backtrack to the correct state.
|
|
|
|
|
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->ExitSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::RestartSearch() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->RestartSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::BeginNextDecision(DecisionBuilder* const db) {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->BeginNextDecision(db);
|
|
|
|
|
}
|
|
|
|
|
CheckFail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::EndNextDecision(DecisionBuilder* const db, Decision* const d) {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->EndNextDecision(db, d);
|
|
|
|
|
}
|
|
|
|
|
CheckFail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::ApplyDecision(Decision* const d) {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->ApplyDecision(d);
|
|
|
|
|
}
|
|
|
|
|
CheckFail();
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-31 12:28:12 +00:00
|
|
|
void Search::AfterDecision(Decision* const d, bool apply) {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2011-03-31 12:28:12 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->AfterDecision(d, apply);
|
|
|
|
|
}
|
|
|
|
|
CheckFail();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
void Search::RefuteDecision(Decision* const d) {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->RefuteDecision(d);
|
|
|
|
|
}
|
|
|
|
|
CheckFail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::BeginFail() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->BeginFail();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::EndFail() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->EndFail();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::BeginInitialPropagation() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->BeginInitialPropagation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::EndInitialPropagation() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->EndInitialPropagation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 16:04:31 +00:00
|
|
|
bool Search::AcceptSolution() {
|
|
|
|
|
bool valid = true;
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
2010-10-06 16:04:31 +00:00
|
|
|
if (!(*it)->AcceptSolution()) {
|
2011-11-07 15:31:18 +00:00
|
|
|
// Even though we know the return value, we cannot return yet: this would
|
|
|
|
|
// break the contract we have with solution monitors. They all deserve
|
|
|
|
|
// a chance to look at the solution.
|
2010-10-06 16:04:31 +00:00
|
|
|
valid = false;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-10-06 16:04:31 +00:00
|
|
|
return valid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Search::AtSolution() {
|
|
|
|
|
bool should_continue = false;
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-10-06 16:04:31 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if ((*it)->AtSolution()) {
|
2011-11-07 15:31:18 +00:00
|
|
|
// Even though we know the return value, we cannot return yet: this would
|
|
|
|
|
// break the contract we have with solution monitors. They all deserve
|
|
|
|
|
// a chance to look at the solution.
|
2010-10-06 16:04:31 +00:00
|
|
|
should_continue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return should_continue;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::NoMoreSolutions() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->NoMoreSolutions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Search::LocalOptimum() {
|
|
|
|
|
bool res = false;
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
if ((*it)->LocalOptimum()) {
|
|
|
|
|
res = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Search::AcceptDelta(Assignment* delta, Assignment* deltadelta) {
|
|
|
|
|
bool accept = true;
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
2010-10-06 16:04:31 +00:00
|
|
|
if (!(*it)->AcceptDelta(delta, deltadelta)) {
|
|
|
|
|
accept = false;
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
return accept;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::AcceptNeighbor() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->AcceptNeighbor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Search::PeriodicCheck() {
|
2011-05-17 20:38:55 +00:00
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
2010-09-15 12:42:33 +00:00
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
(*it)->PeriodicCheck();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-09 19:42:58 +00:00
|
|
|
int Search::ProgressPercent() {
|
|
|
|
|
int progress = SearchMonitor::kNoProgress;
|
|
|
|
|
for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
|
|
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
progress = std::max(progress, (*it)->ProgressPercent());
|
|
|
|
|
}
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void Search::Accept(ModelVisitor* const visitor) const {
|
|
|
|
|
for (std::vector<SearchMonitor*>::const_iterator it = monitors_.begin();
|
|
|
|
|
it != monitors_.end();
|
|
|
|
|
++it) {
|
|
|
|
|
DCHECK((*it) != NULL);
|
|
|
|
|
(*it)->Accept(visitor);
|
|
|
|
|
}
|
|
|
|
|
if (decision_builder_ != NULL) {
|
|
|
|
|
decision_builder_->Accept(visitor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 14:16:12 +00:00
|
|
|
bool LocalOptimumReached(Search* const search) {
|
|
|
|
|
return search->LocalOptimum();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AcceptDelta(Search* const search,
|
|
|
|
|
Assignment* delta,
|
|
|
|
|
Assignment* deltadelta) {
|
|
|
|
|
return search->AcceptDelta(delta, deltadelta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AcceptNeighbor(Search* const search) {
|
|
|
|
|
search->AcceptNeighbor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
namespace {
|
2012-02-14 14:16:12 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// ---------- Fail Decision ----------
|
|
|
|
|
|
|
|
|
|
class FailDecision : public Decision {
|
|
|
|
|
public:
|
|
|
|
|
virtual void Apply(Solver* const s) {
|
|
|
|
|
s->Fail();
|
|
|
|
|
}
|
|
|
|
|
virtual void Refute(Solver* const s) {
|
|
|
|
|
s->Fail();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Balancing decision
|
|
|
|
|
|
|
|
|
|
class BalancingDecision : public Decision {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~BalancingDecision() {}
|
|
|
|
|
virtual void Apply(Solver* const s) {}
|
|
|
|
|
virtual void Refute(Solver* const s) {}
|
|
|
|
|
};
|
2011-08-13 01:52:01 +00:00
|
|
|
} // namespace
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
Decision* Solver::MakeFailDecision() {
|
|
|
|
|
return fail_decision_.get();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
// ------------------ Solver class -----------------
|
|
|
|
|
|
|
|
|
|
// These magic numbers are there to make sure we pop the correct
|
|
|
|
|
// sentinels throughout the search.
|
|
|
|
|
namespace {
|
|
|
|
|
enum SentinelMarker {
|
|
|
|
|
INITIAL_SEARCH_SENTINEL = 10000000,
|
|
|
|
|
ROOT_NODE_SENTINEL = 20000000,
|
|
|
|
|
SOLVER_CTOR_SENTINEL = 40000000
|
|
|
|
|
};
|
2011-08-11 05:15:18 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
extern Action* NewDomainIntVarCleaner();
|
2011-11-16 17:32:24 +00:00
|
|
|
extern PropagationMonitor* BuildTrace(Solver* const s);
|
|
|
|
|
extern ModelCache* BuildModelCache(Solver* const solver);
|
|
|
|
|
extern DependencyGraph* BuildDependencyGraph(Solver* const solver);
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-09-13 09:24:49 +00:00
|
|
|
string Solver::model_name() const { return name_; }
|
|
|
|
|
|
2010-12-06 10:59:35 +00:00
|
|
|
Solver::Solver(const string& name, const SolverParameters& parameters)
|
|
|
|
|
: name_(name),
|
|
|
|
|
parameters_(parameters),
|
|
|
|
|
queue_(new Queue(this)),
|
|
|
|
|
trail_(new Trail(parameters.trail_block_size, parameters.compress_trail)),
|
|
|
|
|
state_(OUTSIDE_SEARCH),
|
|
|
|
|
branches_(0),
|
|
|
|
|
fails_(0),
|
|
|
|
|
decisions_(0),
|
|
|
|
|
neighbors_(0),
|
|
|
|
|
filtered_neighbors_(0),
|
|
|
|
|
accepted_neighbors_(0),
|
2011-08-11 05:15:18 +00:00
|
|
|
variable_cleaner_(NewDomainIntVarCleaner()),
|
2010-12-06 10:59:35 +00:00
|
|
|
timer_(new ClockTimer),
|
2011-12-15 11:50:32 +00:00
|
|
|
searches_(1, new Search(this, 0)),
|
2010-12-06 10:59:35 +00:00
|
|
|
random_(ACMRandom::DeterministicSeed()),
|
|
|
|
|
fail_hooks_(NULL),
|
|
|
|
|
fail_stamp_(GG_ULONGLONG(1)),
|
|
|
|
|
balancing_decision_(new BalancingDecision),
|
|
|
|
|
fail_intercept_(NULL),
|
2011-11-21 02:52:58 +00:00
|
|
|
demon_profiler_(BuildDemonProfiler(this)),
|
2010-12-06 10:59:35 +00:00
|
|
|
true_constraint_(NULL),
|
|
|
|
|
false_constraint_(NULL),
|
|
|
|
|
fail_decision_(new FailDecision()),
|
2011-07-15 18:51:39 +00:00
|
|
|
constraint_index_(0),
|
2011-09-08 00:36:56 +00:00
|
|
|
additional_constraint_index_(0),
|
2011-11-03 10:27:53 +00:00
|
|
|
model_cache_(NULL),
|
2011-11-10 09:36:43 +00:00
|
|
|
dependency_graph_(NULL),
|
2011-11-16 17:32:24 +00:00
|
|
|
propagation_monitor_(BuildTrace(this)),
|
2011-11-19 01:59:37 +00:00
|
|
|
print_trace_(NULL),
|
|
|
|
|
anonymous_variable_index_(0) {
|
2010-12-06 10:59:35 +00:00
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
Solver::Solver(const string& name)
|
|
|
|
|
: name_(name),
|
2010-12-06 10:59:35 +00:00
|
|
|
parameters_(),
|
2010-09-15 12:42:33 +00:00
|
|
|
queue_(new Queue(this)),
|
2010-12-06 10:59:35 +00:00
|
|
|
trail_(new Trail(parameters_.trail_block_size,
|
|
|
|
|
parameters_.compress_trail)),
|
2010-09-15 12:42:33 +00:00
|
|
|
state_(OUTSIDE_SEARCH),
|
|
|
|
|
branches_(0),
|
|
|
|
|
fails_(0),
|
|
|
|
|
decisions_(0),
|
|
|
|
|
neighbors_(0),
|
|
|
|
|
filtered_neighbors_(0),
|
|
|
|
|
accepted_neighbors_(0),
|
2011-08-11 05:15:18 +00:00
|
|
|
variable_cleaner_(NewDomainIntVarCleaner()),
|
2010-09-15 12:42:33 +00:00
|
|
|
timer_(new ClockTimer),
|
2011-12-15 11:50:32 +00:00
|
|
|
searches_(1, new Search(this, 0)),
|
2010-09-15 12:42:33 +00:00
|
|
|
random_(ACMRandom::DeterministicSeed()),
|
|
|
|
|
fail_hooks_(NULL),
|
|
|
|
|
fail_stamp_(GG_ULONGLONG(1)),
|
|
|
|
|
balancing_decision_(new BalancingDecision),
|
2010-10-08 23:55:59 +00:00
|
|
|
fail_intercept_(NULL),
|
2011-11-21 02:52:58 +00:00
|
|
|
demon_profiler_(BuildDemonProfiler(this)),
|
2010-09-15 12:42:33 +00:00
|
|
|
true_constraint_(NULL),
|
|
|
|
|
false_constraint_(NULL),
|
|
|
|
|
fail_decision_(new FailDecision()),
|
2011-07-15 18:51:39 +00:00
|
|
|
constraint_index_(0),
|
2011-09-08 00:36:56 +00:00
|
|
|
additional_constraint_index_(0),
|
2011-11-03 10:27:53 +00:00
|
|
|
model_cache_(NULL),
|
2011-11-10 09:36:43 +00:00
|
|
|
dependency_graph_(NULL),
|
2011-11-16 17:32:24 +00:00
|
|
|
propagation_monitor_(BuildTrace(this)),
|
2011-11-19 01:59:37 +00:00
|
|
|
print_trace_(NULL),
|
|
|
|
|
anonymous_variable_index_(0) {
|
2010-12-06 10:59:35 +00:00
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::Init() {
|
2010-09-15 12:42:33 +00:00
|
|
|
for (int i = 0; i < kNumPriorities; ++i) {
|
|
|
|
|
demon_runs_[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
searches_.push_back(new Search(this));
|
|
|
|
|
PushSentinel(SOLVER_CTOR_SENTINEL);
|
|
|
|
|
InitCachedIntConstants(); // to be called after the SENTINEL is set.
|
|
|
|
|
InitCachedConstraint(); // Cache the true constraint.
|
2011-09-05 13:45:29 +00:00
|
|
|
InitBuilders();
|
2010-10-10 00:23:55 +00:00
|
|
|
timer_->Restart();
|
2011-09-08 00:36:56 +00:00
|
|
|
model_cache_.reset(BuildModelCache(this));
|
2011-11-03 10:27:53 +00:00
|
|
|
dependency_graph_.reset(BuildDependencyGraph(this));
|
2011-11-21 02:52:58 +00:00
|
|
|
AddPropagationMonitor(reinterpret_cast<PropagationMonitor*>(demon_profiler_));
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Solver::~Solver() {
|
|
|
|
|
// solver destructor called with searches open.
|
2012-02-14 14:16:12 +00:00
|
|
|
CHECK_EQ(2, searches_.size());
|
2010-09-15 12:42:33 +00:00
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
|
|
|
|
|
StateInfo info;
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType finalType = PopState(&info);
|
2010-09-15 12:42:33 +00:00
|
|
|
// Not popping a SENTINEL in Solver destructor.
|
|
|
|
|
DCHECK_EQ(finalType, SENTINEL);
|
|
|
|
|
// Not popping initial SENTINEL in Solver destructor.
|
|
|
|
|
DCHECK_EQ(info.int_info, SOLVER_CTOR_SENTINEL);
|
2012-02-14 14:16:12 +00:00
|
|
|
STLDeleteElements(&searches_);
|
2011-11-21 02:52:58 +00:00
|
|
|
DeleteDemonProfiler(demon_profiler_);
|
2011-09-05 13:45:29 +00:00
|
|
|
DeleteBuilders();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-06 16:29:32 +00:00
|
|
|
const SolverParameters::TrailCompression
|
|
|
|
|
SolverParameters::kDefaultTrailCompression = SolverParameters::NO_COMPRESSION;
|
2010-12-06 10:59:35 +00:00
|
|
|
const int SolverParameters::kDefaultTrailBlockSize = 8000;
|
|
|
|
|
const int SolverParameters::kDefaultArraySplitSize = 16;
|
|
|
|
|
const bool SolverParameters::kDefaultNameStoring = true;
|
2011-01-06 17:40:30 +00:00
|
|
|
const SolverParameters::ProfileLevel SolverParameters::kDefaultProfileLevel =
|
|
|
|
|
SolverParameters::NO_PROFILING;
|
2011-11-09 10:59:31 +00:00
|
|
|
const SolverParameters::TraceLevel SolverParameters::kDefaultTraceLevel =
|
|
|
|
|
SolverParameters::NO_TRACE;
|
2011-11-19 01:59:37 +00:00
|
|
|
const bool SolverParameters::kDefaultNameAllVariables = false;
|
2010-12-06 10:59:35 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
string Solver::DebugString() const {
|
|
|
|
|
string out = "Solver(name = \"" + name_ + "\", state = ";
|
|
|
|
|
switch (state_) {
|
|
|
|
|
case OUTSIDE_SEARCH:
|
|
|
|
|
out += "OUTSIDE_SEARCH";
|
|
|
|
|
break;
|
2011-07-15 18:51:39 +00:00
|
|
|
case IN_ROOT_NODE:
|
|
|
|
|
out += "IN_ROOT_NODE";
|
|
|
|
|
break;
|
2010-09-15 12:42:33 +00:00
|
|
|
case IN_SEARCH:
|
|
|
|
|
out += "IN_SEARCH";
|
|
|
|
|
break;
|
2010-10-06 16:04:31 +00:00
|
|
|
case AT_SOLUTION:
|
|
|
|
|
out += "AT_SOLUTION";
|
2010-09-15 12:42:33 +00:00
|
|
|
break;
|
2010-10-06 16:04:31 +00:00
|
|
|
case NO_MORE_SOLUTIONS:
|
|
|
|
|
out += "NO_MORE_SOLUTIONS";
|
2010-09-15 12:42:33 +00:00
|
|
|
break;
|
|
|
|
|
case PROBLEM_INFEASIBLE:
|
|
|
|
|
out += "PROBLEM_INFEASIBLE";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
StringAppendF(&out, ", branches = %" GG_LL_FORMAT
|
|
|
|
|
"d, fails = %" GG_LL_FORMAT
|
|
|
|
|
"d, decisions = %" GG_LL_FORMAT
|
|
|
|
|
"d, delayed demon runs = %" GG_LL_FORMAT
|
|
|
|
|
"d, var demon runs = %" GG_LL_FORMAT
|
|
|
|
|
"d, normal demon runs = %" GG_LL_FORMAT
|
|
|
|
|
"d, Run time = %" GG_LL_FORMAT "d ms)",
|
|
|
|
|
branches_, fails_, decisions_, demon_runs_[DELAYED_PRIORITY],
|
|
|
|
|
demon_runs_[VAR_PRIORITY], demon_runs_[NORMAL_PRIORITY],
|
|
|
|
|
wall_time());
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64 Solver::MemoryUsage() {
|
2010-10-11 17:39:39 +00:00
|
|
|
return GetProcessMemoryUsage();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-11 17:39:39 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
int64 Solver::wall_time() const {
|
|
|
|
|
return timer_->GetInMs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64 Solver::solutions() const {
|
2011-12-15 11:50:32 +00:00
|
|
|
return TopLevelSearch()->solution_counter();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::TopPeriodicCheck() {
|
2011-12-15 11:50:32 +00:00
|
|
|
TopLevelSearch()->PeriodicCheck();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-09 19:42:58 +00:00
|
|
|
int Solver::TopProgressPercent() {
|
|
|
|
|
return TopLevelSearch()->ProgressPercent();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
void Solver::PushState() {
|
|
|
|
|
StateInfo info;
|
|
|
|
|
PushState(SIMPLE_MARKER, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::PopState() {
|
|
|
|
|
StateInfo info;
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType t = PopState(&info);
|
2010-09-15 12:42:33 +00:00
|
|
|
CHECK_EQ(SIMPLE_MARKER, t);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
void Solver::PushState(Solver::MarkerType t, const StateInfo& info) {
|
2010-09-15 12:42:33 +00:00
|
|
|
StateMarker* m = new StateMarker(t, info);
|
|
|
|
|
if (t != REVERSIBLE_ACTION || info.int_info == 0) {
|
|
|
|
|
m->rev_int_index_ = trail_->rev_ints_.size();
|
|
|
|
|
m->rev_int64_index_ = trail_->rev_int64s_.size();
|
|
|
|
|
m->rev_uint64_index_ = trail_->rev_uint64s_.size();
|
|
|
|
|
m->rev_ptr_index_ = trail_->rev_ptrs_.size();
|
|
|
|
|
m->rev_boolvar_list_index_ = trail_->rev_boolvar_list_.size();
|
|
|
|
|
m->rev_bools_index_ = trail_->rev_bools_.size();
|
|
|
|
|
m->rev_int_memory_index_ = trail_->rev_int_memory_.size();
|
|
|
|
|
m->rev_int64_memory_index_ = trail_->rev_int64_memory_.size();
|
|
|
|
|
m->rev_object_memory_index_ = trail_->rev_object_memory_.size();
|
|
|
|
|
m->rev_object_array_memory_index_ = trail_->rev_object_array_memory_.size();
|
|
|
|
|
m->rev_memory_index_ = trail_->rev_memory_.size();
|
|
|
|
|
m->rev_memory_array_index_ = trail_->rev_memory_array_.size();
|
|
|
|
|
}
|
|
|
|
|
searches_.back()->marker_stack_.push_back(m);
|
|
|
|
|
queue_->increase_stamp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::AddBacktrackAction(Action* a, bool fast) {
|
|
|
|
|
StateInfo info(a, static_cast<int>(fast));
|
|
|
|
|
PushState(REVERSIBLE_ACTION, info);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType Solver::PopState(StateInfo* info) {
|
2010-09-15 12:42:33 +00:00
|
|
|
CHECK(!searches_.back()->marker_stack_.empty())
|
|
|
|
|
<< "PopState() on an empty stack";
|
|
|
|
|
CHECK(info != NULL);
|
|
|
|
|
StateMarker* m = searches_.back()->marker_stack_.back();
|
|
|
|
|
if (m->type_ != REVERSIBLE_ACTION || m->info_.int_info == 0) {
|
|
|
|
|
trail_->BacktrackTo(m);
|
|
|
|
|
}
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType t = m->type_;
|
2010-09-15 12:42:33 +00:00
|
|
|
(*info) = m->info_;
|
|
|
|
|
searches_.back()->marker_stack_.pop_back();
|
|
|
|
|
delete m;
|
|
|
|
|
queue_->increase_stamp();
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::check_alloc_state() {
|
|
|
|
|
switch (state_) {
|
|
|
|
|
case OUTSIDE_SEARCH:
|
2011-07-15 18:51:39 +00:00
|
|
|
case IN_ROOT_NODE:
|
2010-09-15 12:42:33 +00:00
|
|
|
case IN_SEARCH:
|
2010-10-06 16:04:31 +00:00
|
|
|
case NO_MORE_SOLUTIONS:
|
2010-09-15 12:42:33 +00:00
|
|
|
case PROBLEM_INFEASIBLE:
|
|
|
|
|
break;
|
2010-10-06 16:04:31 +00:00
|
|
|
case AT_SOLUTION:
|
2010-09-15 12:42:33 +00:00
|
|
|
LOG(FATAL) << "allocating at a leaf node";
|
2011-07-15 18:51:39 +00:00
|
|
|
default:
|
|
|
|
|
LOG(FATAL) << "This switch was supposed to be exhaustive, but it is not!";
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::AddFailHook(Action* a) {
|
|
|
|
|
if (fail_hooks_ == NULL) {
|
|
|
|
|
SaveValue(reinterpret_cast<void**>(&fail_hooks_));
|
|
|
|
|
fail_hooks_ = UnsafeRevAlloc(new SimpleRevFIFO<Action*>);
|
|
|
|
|
}
|
|
|
|
|
fail_hooks_->Push(this, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::CallFailHooks() {
|
|
|
|
|
if (fail_hooks_ != NULL) {
|
|
|
|
|
for (SimpleRevFIFO<Action*>::Iterator it(fail_hooks_); it.ok(); ++it) {
|
|
|
|
|
(*it)->Run(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::FreezeQueue() {
|
|
|
|
|
queue_->Freeze();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::UnfreezeQueue() {
|
|
|
|
|
queue_->Unfreeze();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::Enqueue(Demon* d) {
|
|
|
|
|
queue_->Enqueue(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::ProcessDemonsOnQueue() {
|
2011-11-16 17:32:24 +00:00
|
|
|
queue_->ProcessNormalDemons();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64 Solver::stamp() const {
|
|
|
|
|
return queue_->stamp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64 Solver::fail_stamp() const {
|
|
|
|
|
return fail_stamp_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::set_queue_action_on_fail(Action* a) {
|
|
|
|
|
queue_->set_action_on_fail(a);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void SetQueueCleanerOnFail(Solver* const solver, IntVar* const var) {
|
|
|
|
|
solver->set_queue_cleaner_on_fail(var);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::clear_queue_action_on_fail() {
|
|
|
|
|
queue_->clear_action_on_fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::AddConstraint(Constraint* const c) {
|
2012-07-05 14:22:07 +00:00
|
|
|
if (c == true_constraint_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
if (state_ == IN_SEARCH) {
|
2010-11-15 15:08:17 +00:00
|
|
|
queue_->AddConstraint(c);
|
2011-07-15 18:51:39 +00:00
|
|
|
} else if (state_ == IN_ROOT_NODE) {
|
|
|
|
|
DCHECK_GE(constraint_index_, 0);
|
|
|
|
|
DCHECK_LE(constraint_index_, constraints_list_.size());
|
|
|
|
|
const int constraint_parent =
|
|
|
|
|
constraint_index_ == constraints_list_.size() ?
|
|
|
|
|
additional_constraints_parent_list_[additional_constraint_index_] :
|
|
|
|
|
constraint_index_;
|
|
|
|
|
additional_constraints_list_.push_back(c);
|
|
|
|
|
additional_constraints_parent_list_.push_back(constraint_parent);
|
2010-09-15 12:42:33 +00:00
|
|
|
} else {
|
|
|
|
|
if (FLAGS_cp_show_constraints) {
|
|
|
|
|
LOG(INFO) << c->DebugString();
|
|
|
|
|
}
|
|
|
|
|
constraints_list_.push_back(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
void Solver::AddCastConstraint(CastConstraint* const constraint,
|
|
|
|
|
IntVar* const target_var,
|
|
|
|
|
IntExpr* const expr) {
|
|
|
|
|
if (constraint != NULL) {
|
|
|
|
|
if (state_ != IN_SEARCH) {
|
|
|
|
|
cast_constraints_.insert(constraint);
|
|
|
|
|
cast_information_[target_var] =
|
|
|
|
|
Solver::IntegerCastInfo(target_var, expr, constraint);
|
|
|
|
|
}
|
|
|
|
|
AddConstraint(constraint);
|
2011-08-11 05:15:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
void Solver::Accept(ModelVisitor* const visitor) const {
|
2011-08-11 07:26:19 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2012-08-27 14:10:15 +00:00
|
|
|
Accept(visitor, monitors, NULL);
|
2011-08-11 07:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::Accept(ModelVisitor* const visitor,
|
|
|
|
|
const std::vector<SearchMonitor*>& monitors) const {
|
2012-08-27 14:10:15 +00:00
|
|
|
Accept(visitor, monitors, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::Accept(ModelVisitor* const visitor,
|
|
|
|
|
const std::vector<SearchMonitor*>& monitors,
|
|
|
|
|
DecisionBuilder* const db) const {
|
2011-07-11 20:13:14 +00:00
|
|
|
visitor->BeginVisitModel(name_);
|
|
|
|
|
for (int index = 0; index < constraints_list_.size(); ++index) {
|
|
|
|
|
Constraint* const constraint = constraints_list_[index];
|
|
|
|
|
constraint->Accept(visitor);
|
|
|
|
|
}
|
2011-08-11 05:15:18 +00:00
|
|
|
if (state_ == IN_ROOT_NODE) {
|
2011-12-15 11:50:32 +00:00
|
|
|
TopLevelSearch()->Accept(visitor);
|
2011-08-11 07:26:19 +00:00
|
|
|
} else {
|
|
|
|
|
for (int i = 0; i < monitors.size(); ++i) {
|
|
|
|
|
monitors[i]->Accept(visitor);
|
|
|
|
|
}
|
2011-08-11 05:15:18 +00:00
|
|
|
}
|
2012-08-27 14:10:15 +00:00
|
|
|
if (db != NULL) {
|
|
|
|
|
db->Accept(visitor);
|
|
|
|
|
}
|
2011-07-11 20:13:14 +00:00
|
|
|
visitor->EndVisitModel(name_);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
void Solver::ProcessConstraints() {
|
2011-07-15 18:51:39 +00:00
|
|
|
// Both constraints_list_ and additional_constraints_list_ are used in
|
|
|
|
|
// a FIFO way.
|
2011-07-13 19:51:33 +00:00
|
|
|
if (FLAGS_cp_print_model) {
|
2011-07-11 20:13:14 +00:00
|
|
|
ModelVisitor* const visitor = MakePrintModelVisitor();
|
|
|
|
|
Accept(visitor);
|
|
|
|
|
}
|
2011-07-13 19:51:33 +00:00
|
|
|
if (FLAGS_cp_model_stats) {
|
|
|
|
|
ModelVisitor* const visitor = MakeStatisticsModelVisitor();
|
|
|
|
|
Accept(visitor);
|
|
|
|
|
}
|
2011-09-05 13:45:29 +00:00
|
|
|
if (!FLAGS_cp_export_file.empty()) {
|
|
|
|
|
File::Init();
|
2012-01-03 22:47:57 +00:00
|
|
|
File* file = File::Open(FLAGS_cp_export_file, "wb");
|
2011-09-05 13:45:29 +00:00
|
|
|
if (file == NULL) {
|
|
|
|
|
LOG(WARNING) << "Cannot open " << FLAGS_cp_export_file;
|
|
|
|
|
} else {
|
|
|
|
|
CPModelProto export_proto;
|
|
|
|
|
ExportModel(&export_proto);
|
|
|
|
|
VLOG(1) << export_proto.DebugString();
|
|
|
|
|
RecordWriter writer(file);
|
|
|
|
|
writer.WriteProtocolMessage(export_proto);
|
|
|
|
|
writer.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FLAGS_cp_no_solve) {
|
|
|
|
|
LOG(INFO) << "Forcing early failure";
|
|
|
|
|
Fail();
|
|
|
|
|
}
|
2011-07-11 20:13:14 +00:00
|
|
|
|
2011-07-15 18:51:39 +00:00
|
|
|
// Clear state before processing constraints.
|
|
|
|
|
const int constraints_size = constraints_list_.size();
|
|
|
|
|
additional_constraints_list_.clear();
|
|
|
|
|
additional_constraints_parent_list_.clear();
|
|
|
|
|
|
|
|
|
|
for (constraint_index_ = 0;
|
|
|
|
|
constraint_index_ < constraints_size;
|
|
|
|
|
++constraint_index_) {
|
|
|
|
|
Constraint* const constraint = constraints_list_[constraint_index_];
|
2011-11-14 20:55:35 +00:00
|
|
|
propagation_monitor_->BeginConstraintInitialPropagation(constraint);
|
2011-07-15 18:51:39 +00:00
|
|
|
constraint->PostAndPropagate();
|
2011-11-14 20:55:35 +00:00
|
|
|
propagation_monitor_->EndConstraintInitialPropagation(constraint);
|
2011-07-15 18:51:39 +00:00
|
|
|
}
|
|
|
|
|
CHECK_EQ(constraints_list_.size(), constraints_size);
|
|
|
|
|
|
|
|
|
|
// Process nested constraints added during the previous step.
|
|
|
|
|
for (int additional_constraint_index_ = 0;
|
|
|
|
|
additional_constraint_index_ < additional_constraints_list_.size();
|
|
|
|
|
++additional_constraint_index_) {
|
2011-09-17 13:34:27 +00:00
|
|
|
Constraint* const nested =
|
2011-07-15 18:51:39 +00:00
|
|
|
additional_constraints_list_[additional_constraint_index_];
|
2011-09-14 08:15:08 +00:00
|
|
|
const int parent_index =
|
|
|
|
|
additional_constraints_parent_list_[additional_constraint_index_];
|
2011-09-19 13:00:59 +00:00
|
|
|
const Constraint* const parent = constraints_list_[parent_index];
|
2011-11-14 20:55:35 +00:00
|
|
|
propagation_monitor_->BeginNestedConstraintInitialPropagation(parent,
|
|
|
|
|
nested);
|
2011-09-17 13:34:27 +00:00
|
|
|
nested->PostAndPropagate();
|
2011-11-14 20:55:35 +00:00
|
|
|
propagation_monitor_->EndNestedConstraintInitialPropagation(parent, nested);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::CurrentlyInSolve() const {
|
2011-12-15 11:50:32 +00:00
|
|
|
DCHECK_GT(SolveDepth(), 0);
|
2010-09-15 12:42:33 +00:00
|
|
|
DCHECK(searches_.back() != NULL);
|
|
|
|
|
return searches_.back()->created_by_solve();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db, SearchMonitor* const m1) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
2012-05-30 09:18:17 +00:00
|
|
|
return Solve(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db) {
|
2012-05-30 09:18:17 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
|
|
|
|
return Solve(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
2012-05-30 09:18:17 +00:00
|
|
|
return Solve(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2,
|
|
|
|
|
SearchMonitor* const m3) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
|
|
|
|
monitors.push_back(m3);
|
2012-05-30 09:18:17 +00:00
|
|
|
return Solve(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2,
|
|
|
|
|
SearchMonitor* const m3,
|
|
|
|
|
SearchMonitor* const m4) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
|
|
|
|
monitors.push_back(m3);
|
|
|
|
|
monitors.push_back(m4);
|
2012-05-30 09:18:17 +00:00
|
|
|
return Solve(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::Solve(DecisionBuilder* const db,
|
2012-05-30 09:18:17 +00:00
|
|
|
const std::vector<SearchMonitor*>& monitors) {
|
|
|
|
|
NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
searches_.back()->set_created_by_solve(true); // Overwrites default.
|
2010-10-06 16:04:31 +00:00
|
|
|
NextSolution();
|
2011-03-03 21:38:41 +00:00
|
|
|
const bool solution_found = searches_.back()->solution_counter() > 0;
|
2010-09-15 12:42:33 +00:00
|
|
|
EndSearch();
|
2011-03-03 21:38:41 +00:00
|
|
|
return solution_found;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db, SearchMonitor* const m1) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
2012-05-30 09:18:17 +00:00
|
|
|
return NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db) {
|
2012-05-30 09:18:17 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
|
|
|
|
return NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
2012-05-30 09:18:17 +00:00
|
|
|
return NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2,
|
|
|
|
|
SearchMonitor* const m3) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
|
|
|
|
monitors.push_back(m3);
|
2012-05-30 09:18:17 +00:00
|
|
|
return NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2,
|
|
|
|
|
SearchMonitor* const m3,
|
|
|
|
|
SearchMonitor* const m4) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
|
|
|
|
monitors.push_back(m3);
|
|
|
|
|
monitors.push_back(m4);
|
2012-05-30 09:18:17 +00:00
|
|
|
return NewSearch(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
extern PropagationMonitor* BuildPrintTrace(Solver* const s);
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// Opens a new top level search.
|
|
|
|
|
void Solver::NewSearch(DecisionBuilder* const db,
|
2012-05-30 09:18:17 +00:00
|
|
|
const std::vector<SearchMonitor*>& monitors) {
|
2010-09-15 12:42:33 +00:00
|
|
|
// TODO(user) : reset statistics
|
|
|
|
|
|
2012-05-30 09:18:17 +00:00
|
|
|
const int size = monitors.size();
|
|
|
|
|
|
2010-10-11 17:39:39 +00:00
|
|
|
CHECK_NOTNULL(db);
|
2012-05-17 15:43:34 +00:00
|
|
|
const bool nested = state_ == IN_SEARCH;
|
2010-10-11 17:39:39 +00:00
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
if (state_ == IN_ROOT_NODE) {
|
|
|
|
|
LOG(FATAL) << "Cannot start new searches here.";
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
2012-05-17 15:43:34 +00:00
|
|
|
|
|
|
|
|
Search* const search = nested ? new Search(this) : searches_.back();
|
2010-09-15 12:42:33 +00:00
|
|
|
search->set_created_by_solve(false); // default behavior.
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
// ----- jumps to correct state -----
|
|
|
|
|
|
|
|
|
|
if (nested) {
|
2012-06-13 16:14:46 +00:00
|
|
|
// Nested searches are created on demand, and deleted afterwards.
|
2012-05-17 15:43:34 +00:00
|
|
|
DCHECK_GE(searches_.size(), 2);
|
|
|
|
|
searches_.push_back(search);
|
|
|
|
|
} else {
|
2012-06-13 16:14:46 +00:00
|
|
|
// Top level search is persistent.
|
|
|
|
|
// TODO(user): delete top level search after EndSearch().
|
2012-05-17 15:43:34 +00:00
|
|
|
DCHECK_EQ(2, searches_.size());
|
2012-06-13 16:14:46 +00:00
|
|
|
// TODO(user): Check if these two lines are still necessary.
|
2012-05-17 15:43:34 +00:00
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
state_ = OUTSIDE_SEARCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----- manages all monitors -----
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
// Always install the main propagation monitor.
|
|
|
|
|
propagation_monitor_->Install();
|
2011-11-21 02:52:58 +00:00
|
|
|
if (demon_profiler_ != NULL) {
|
|
|
|
|
InstallDemonProfiler(demon_profiler_);
|
2011-11-16 17:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// Push monitors and enter search.
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2011-11-16 17:32:24 +00:00
|
|
|
if (monitors[i] != NULL) {
|
|
|
|
|
monitors[i]->Install();
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
2011-06-28 09:27:03 +00:00
|
|
|
std::vector<SearchMonitor*> extras;
|
2011-06-29 09:46:25 +00:00
|
|
|
db->AppendMonitors(this, &extras);
|
2011-06-28 09:27:03 +00:00
|
|
|
for (ConstIter<std::vector<SearchMonitor*> > it(extras); !it.at_end(); ++it) {
|
2011-11-16 17:32:24 +00:00
|
|
|
SearchMonitor* const monitor = *it;
|
|
|
|
|
if (monitor != NULL) {
|
|
|
|
|
monitor->Install();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Install the print trace if needed.
|
|
|
|
|
// The print_trace needs to be last to detect propagation from the objective.
|
2012-06-13 16:14:46 +00:00
|
|
|
if (nested) {
|
|
|
|
|
if (print_trace_ != NULL) { // Was installed at the top level?
|
|
|
|
|
print_trace_->Install(); // Propagates to nested search.
|
2012-05-17 15:43:34 +00:00
|
|
|
}
|
2012-06-13 16:14:46 +00:00
|
|
|
} else { // Top level search
|
|
|
|
|
print_trace_ = NULL; // Clears it first.
|
|
|
|
|
if (FLAGS_cp_trace_propagation) {
|
|
|
|
|
print_trace_ = BuildPrintTrace(this);
|
|
|
|
|
print_trace_->Install();
|
|
|
|
|
} else if (FLAGS_cp_trace_search) {
|
|
|
|
|
// This is useful to trace the exact behavior of the search.
|
|
|
|
|
// The '######## ' prefix is the same as the progagation trace.
|
|
|
|
|
// Search trace is subsumed by propagation trace, thus only one
|
|
|
|
|
// is necessary.
|
2012-01-17 15:06:01 +00:00
|
|
|
SearchMonitor* const trace = MakeSearchTrace("######## ");
|
|
|
|
|
trace->Install();
|
|
|
|
|
}
|
2011-06-27 13:45:01 +00:00
|
|
|
}
|
2011-11-16 17:32:24 +00:00
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
// ----- enters search -----
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
search->EnterSearch();
|
|
|
|
|
|
|
|
|
|
// Push sentinel and set decision builder.
|
|
|
|
|
PushSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
search->set_decision_builder(db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Backtrack to the last open right branch in the search tree.
|
|
|
|
|
// It returns true in case the search tree has been completely explored.
|
2011-11-19 01:59:37 +00:00
|
|
|
bool Solver::BacktrackOneLevel(Decision** const fail_decision) {
|
2010-09-15 12:42:33 +00:00
|
|
|
bool no_more_solutions = false;
|
|
|
|
|
bool end_loop = false;
|
|
|
|
|
while (!end_loop) {
|
|
|
|
|
StateInfo info;
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType t = PopState(&info);
|
2010-09-15 12:42:33 +00:00
|
|
|
switch (t) {
|
|
|
|
|
case SENTINEL:
|
|
|
|
|
CHECK_EQ(info.ptr_info, this) << "Wrong sentinel found";
|
2011-12-15 11:50:32 +00:00
|
|
|
CHECK((info.int_info == ROOT_NODE_SENTINEL && SolveDepth() == 1) ||
|
2010-09-15 12:42:33 +00:00
|
|
|
(info.int_info == INITIAL_SEARCH_SENTINEL &&
|
2011-12-15 11:50:32 +00:00
|
|
|
SolveDepth() > 1));
|
2010-09-15 12:42:33 +00:00
|
|
|
searches_.back()->sentinel_pushed_--;
|
|
|
|
|
no_more_solutions = true;
|
|
|
|
|
end_loop = true;
|
|
|
|
|
break;
|
|
|
|
|
case SIMPLE_MARKER:
|
|
|
|
|
LOG(ERROR)
|
|
|
|
|
<< "Simple markers should not be encountered during search";
|
|
|
|
|
break;
|
|
|
|
|
case CHOICE_POINT:
|
|
|
|
|
if (info.int_info == 0) { // was left branch
|
|
|
|
|
(*fail_decision) = reinterpret_cast<Decision*>(info.ptr_info);
|
|
|
|
|
end_loop = true;
|
|
|
|
|
searches_.back()->set_search_depth(info.depth);
|
|
|
|
|
searches_.back()->set_search_left_depth(info.left_depth);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case REVERSIBLE_ACTION: {
|
|
|
|
|
Action* d = reinterpret_cast<Action*>(info.ptr_info);
|
|
|
|
|
d->Run(this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Search* const search = searches_.back();
|
|
|
|
|
search->EndFail();
|
|
|
|
|
CallFailHooks();
|
|
|
|
|
fail_stamp_++;
|
|
|
|
|
if (no_more_solutions) {
|
|
|
|
|
search->NoMoreSolutions();
|
|
|
|
|
}
|
|
|
|
|
return no_more_solutions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::PushSentinel(int magic_code) {
|
|
|
|
|
StateInfo info(this, magic_code);
|
|
|
|
|
PushState(SENTINEL, info);
|
|
|
|
|
// We do not count the sentinel pushed in the ctor.
|
|
|
|
|
if (magic_code != SOLVER_CTOR_SENTINEL) {
|
|
|
|
|
searches_.back()->sentinel_pushed_++;
|
|
|
|
|
}
|
|
|
|
|
const int pushed = searches_.back()->sentinel_pushed_;
|
|
|
|
|
DCHECK((magic_code == SOLVER_CTOR_SENTINEL) ||
|
|
|
|
|
(magic_code == INITIAL_SEARCH_SENTINEL && pushed == 1) ||
|
|
|
|
|
(magic_code == ROOT_NODE_SENTINEL && pushed == 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::RestartSearch() {
|
|
|
|
|
Search* const search = searches_.back();
|
|
|
|
|
CHECK_NE(0, search->sentinel_pushed_);
|
2011-12-15 11:50:32 +00:00
|
|
|
if (SolveDepth() == 1) { // top level.
|
2010-09-15 12:42:33 +00:00
|
|
|
if (search->sentinel_pushed_ > 1) {
|
|
|
|
|
BacktrackToSentinel(ROOT_NODE_SENTINEL);
|
|
|
|
|
}
|
|
|
|
|
CHECK_EQ(1, search->sentinel_pushed_);
|
|
|
|
|
PushSentinel(ROOT_NODE_SENTINEL);
|
|
|
|
|
state_ = IN_SEARCH;
|
|
|
|
|
} else {
|
|
|
|
|
CHECK_EQ(IN_SEARCH, state_);
|
|
|
|
|
if (search->sentinel_pushed_ > 0) {
|
|
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
}
|
|
|
|
|
CHECK_EQ(0, search->sentinel_pushed_);
|
|
|
|
|
PushSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
}
|
2011-01-06 17:40:30 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
search->RestartSearch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Backtrack to the initial search sentinel.
|
|
|
|
|
// Does not change the state, this should be done by the caller.
|
|
|
|
|
void Solver::BacktrackToSentinel(int magic_code) {
|
|
|
|
|
Search* const search = searches_.back();
|
|
|
|
|
bool end_loop = search->sentinel_pushed_ == 0;
|
|
|
|
|
while (!end_loop) {
|
|
|
|
|
StateInfo info;
|
2011-11-07 15:31:18 +00:00
|
|
|
Solver::MarkerType t = PopState(&info);
|
2010-09-15 12:42:33 +00:00
|
|
|
switch (t) {
|
|
|
|
|
case SENTINEL: {
|
|
|
|
|
CHECK_EQ(info.ptr_info, this) << "Wrong sentinel found";
|
|
|
|
|
CHECK_GE(--search->sentinel_pushed_, 0);
|
|
|
|
|
search->set_search_depth(0);
|
|
|
|
|
search->set_search_left_depth(0);
|
|
|
|
|
|
|
|
|
|
if (info.int_info == magic_code) {
|
|
|
|
|
end_loop = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SIMPLE_MARKER:
|
|
|
|
|
break;
|
|
|
|
|
case CHOICE_POINT:
|
|
|
|
|
break;
|
|
|
|
|
case REVERSIBLE_ACTION: {
|
|
|
|
|
Demon* d = reinterpret_cast<Demon*>(info.ptr_info);
|
|
|
|
|
d->Run(this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fail_stamp_++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Closes the current search without backtrack.
|
|
|
|
|
void Solver::JumpToSentinelWhenNested() {
|
2011-12-15 11:50:32 +00:00
|
|
|
CHECK_GT(SolveDepth(), 1) << "calling JumpToSentinel from top level";
|
2010-09-15 12:42:33 +00:00
|
|
|
Search* c = searches_.back();
|
2012-02-14 14:16:12 +00:00
|
|
|
Search* p = ParentSearch();
|
2010-09-15 12:42:33 +00:00
|
|
|
bool found = false;
|
|
|
|
|
while (!c->marker_stack_.empty()) {
|
2012-02-14 14:16:12 +00:00
|
|
|
StateMarker* const m = c->marker_stack_.back();
|
2010-09-15 12:42:33 +00:00
|
|
|
if (m->type_ == REVERSIBLE_ACTION) {
|
|
|
|
|
p->marker_stack_.push_back(m);
|
|
|
|
|
} else {
|
|
|
|
|
if (m->type_ == SENTINEL) {
|
|
|
|
|
CHECK_EQ(c->marker_stack_.size(), 1) << "Sentinel found too early";
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
delete m;
|
|
|
|
|
}
|
|
|
|
|
c->marker_stack_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
c->set_search_depth(0);
|
|
|
|
|
c->set_search_left_depth(0);
|
|
|
|
|
CHECK_EQ(found, true) << "Sentinel not found";
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
namespace {
|
2010-09-15 12:42:33 +00:00
|
|
|
class ReverseDecision : public Decision {
|
|
|
|
|
public:
|
|
|
|
|
explicit ReverseDecision(Decision* const d) : decision_(d) {
|
|
|
|
|
CHECK(d != NULL);
|
|
|
|
|
}
|
|
|
|
|
virtual ~ReverseDecision() {}
|
|
|
|
|
|
|
|
|
|
virtual void Apply(Solver* const s) {
|
|
|
|
|
decision_->Refute(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Refute(Solver* const s) {
|
|
|
|
|
decision_->Apply(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Accept(DecisionVisitor* const visitor) const {
|
|
|
|
|
decision_->Accept(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual string DebugString() const {
|
|
|
|
|
string str = "Reverse(";
|
|
|
|
|
str += decision_->DebugString();
|
|
|
|
|
str += ")";
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2011-06-28 09:27:03 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
private:
|
|
|
|
|
Decision* const decision_;
|
|
|
|
|
};
|
2011-08-11 07:26:19 +00:00
|
|
|
} // namespace
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
// Search for the next solution in the search tree.
|
|
|
|
|
bool Solver::NextSolution() {
|
|
|
|
|
Search* const search = searches_.back();
|
|
|
|
|
Decision* fd = NULL;
|
2012-02-14 14:16:12 +00:00
|
|
|
const int solve_depth = SolveDepth();
|
|
|
|
|
const bool top_level = solve_depth <= 1;
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2012-02-14 14:16:12 +00:00
|
|
|
if (solve_depth == 0 && !search->decision_builder()) {
|
2010-10-09 11:18:45 +00:00
|
|
|
LOG(WARNING) << "NextSolution() called without a NewSearch before";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
if (top_level) { // Manage top level state.
|
|
|
|
|
switch (state_) {
|
|
|
|
|
case PROBLEM_INFEASIBLE:
|
|
|
|
|
return false;
|
2010-10-06 16:04:31 +00:00
|
|
|
case NO_MORE_SOLUTIONS:
|
2010-09-15 12:42:33 +00:00
|
|
|
return false;
|
2010-10-06 16:04:31 +00:00
|
|
|
case AT_SOLUTION: {
|
2010-09-15 12:42:33 +00:00
|
|
|
if (BacktrackOneLevel(&fd)) { // No more solutions.
|
2010-10-06 16:04:31 +00:00
|
|
|
state_ = NO_MORE_SOLUTIONS;
|
2010-09-15 12:42:33 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
state_ = IN_SEARCH;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case OUTSIDE_SEARCH: {
|
2011-08-11 05:15:18 +00:00
|
|
|
state_ = IN_ROOT_NODE;
|
2010-09-15 12:42:33 +00:00
|
|
|
search->BeginInitialPropagation();
|
|
|
|
|
CP_TRY(search) {
|
|
|
|
|
ProcessConstraints();
|
|
|
|
|
search->EndInitialPropagation();
|
|
|
|
|
PushSentinel(ROOT_NODE_SENTINEL);
|
|
|
|
|
state_ = IN_SEARCH;
|
|
|
|
|
search->ClearBuffer();
|
2010-09-16 14:16:51 +00:00
|
|
|
} CP_ON_FAIL {
|
2010-12-06 16:29:32 +00:00
|
|
|
queue_->AfterFailure();
|
2010-09-15 12:42:33 +00:00
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
state_ = PROBLEM_INFEASIBLE;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case IN_SEARCH: // Usually after a RestartSearch
|
|
|
|
|
break;
|
2011-07-15 18:51:39 +00:00
|
|
|
case IN_ROOT_NODE:
|
|
|
|
|
LOG(FATAL) << "Should not happen";
|
|
|
|
|
break;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
volatile bool finish = false;
|
|
|
|
|
volatile bool result = false;
|
|
|
|
|
DecisionBuilder* const db = search->decision_builder();
|
|
|
|
|
|
|
|
|
|
while (!finish) {
|
|
|
|
|
CP_TRY(search) {
|
|
|
|
|
if (fd != NULL) {
|
|
|
|
|
StateInfo i1(fd,
|
|
|
|
|
1,
|
|
|
|
|
search->search_depth(),
|
|
|
|
|
search->left_search_depth()); // 1 for right branch
|
|
|
|
|
PushState(CHOICE_POINT, i1);
|
|
|
|
|
search->RefuteDecision(fd);
|
|
|
|
|
branches_++;
|
|
|
|
|
fd->Refute(this);
|
2011-03-31 12:28:12 +00:00
|
|
|
search->AfterDecision(fd, false);
|
2010-09-15 12:42:33 +00:00
|
|
|
search->RightMove();
|
|
|
|
|
fd = NULL;
|
|
|
|
|
}
|
|
|
|
|
Decision* d = NULL;
|
|
|
|
|
for (;;) {
|
|
|
|
|
search->BeginNextDecision(db);
|
|
|
|
|
d = db->Next(this);
|
|
|
|
|
search->EndNextDecision(db, d);
|
|
|
|
|
if (d == fail_decision_) {
|
|
|
|
|
Fail(); // fail now instead of after 2 branches.
|
|
|
|
|
}
|
|
|
|
|
if (d != NULL) {
|
|
|
|
|
DecisionModification modification = search->ModifyDecision();
|
|
|
|
|
switch (modification) {
|
|
|
|
|
case SWITCH_BRANCHES: {
|
|
|
|
|
d = RevAlloc(new ReverseDecision(d));
|
|
|
|
|
} // We reverse the decision and fall through the normal code.
|
|
|
|
|
case NO_CHANGE: {
|
|
|
|
|
decisions_++;
|
|
|
|
|
StateInfo i2(d,
|
|
|
|
|
0,
|
|
|
|
|
search->search_depth(),
|
|
|
|
|
search->left_search_depth()); // 0 for left branch
|
|
|
|
|
PushState(CHOICE_POINT, i2);
|
|
|
|
|
search->ApplyDecision(d);
|
|
|
|
|
branches_++;
|
|
|
|
|
d->Apply(this);
|
2011-03-31 12:28:12 +00:00
|
|
|
search->AfterDecision(d, true);
|
2010-09-15 12:42:33 +00:00
|
|
|
search->LeftMove();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case KEEP_LEFT: {
|
|
|
|
|
search->ApplyDecision(d);
|
|
|
|
|
d->Apply(this);
|
2011-03-31 12:28:12 +00:00
|
|
|
search->AfterDecision(d, true);
|
2010-09-15 12:42:33 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case KEEP_RIGHT: {
|
|
|
|
|
search->RefuteDecision(d);
|
|
|
|
|
d->Refute(this);
|
2011-03-31 12:28:12 +00:00
|
|
|
search->AfterDecision(d, false);
|
2010-09-15 12:42:33 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case KILL_BOTH: {
|
|
|
|
|
Fail();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-06 16:04:31 +00:00
|
|
|
if (search->AcceptSolution()) {
|
2011-03-03 21:38:41 +00:00
|
|
|
search->IncrementSolutionCounter();
|
2010-10-06 16:04:31 +00:00
|
|
|
if (!search->AtSolution() || !CurrentlyInSolve()) {
|
|
|
|
|
result = true;
|
|
|
|
|
finish = true;
|
|
|
|
|
} else {
|
|
|
|
|
Fail();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-09-15 12:42:33 +00:00
|
|
|
Fail();
|
|
|
|
|
}
|
2010-09-16 14:16:51 +00:00
|
|
|
} CP_ON_FAIL {
|
2010-12-06 16:29:32 +00:00
|
|
|
queue_->AfterFailure();
|
2010-09-15 12:42:33 +00:00
|
|
|
if (search->should_finish()) {
|
|
|
|
|
fd = NULL;
|
|
|
|
|
BacktrackToSentinel(top_level ?
|
|
|
|
|
ROOT_NODE_SENTINEL :
|
|
|
|
|
INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
result = false;
|
|
|
|
|
finish = true;
|
|
|
|
|
search->set_should_finish(false);
|
|
|
|
|
search->set_should_restart(false);
|
|
|
|
|
// We do not need to push back the sentinel as we are exiting anyway.
|
|
|
|
|
} else if (search->should_restart()) {
|
|
|
|
|
fd = NULL;
|
|
|
|
|
BacktrackToSentinel(top_level ?
|
|
|
|
|
ROOT_NODE_SENTINEL :
|
|
|
|
|
INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
search->set_should_finish(false);
|
|
|
|
|
search->set_should_restart(false);
|
|
|
|
|
PushSentinel(top_level ? ROOT_NODE_SENTINEL : INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
search->RestartSearch();
|
|
|
|
|
} else {
|
|
|
|
|
if (BacktrackOneLevel(&fd)) { // no more solutions.
|
|
|
|
|
result = false;
|
|
|
|
|
finish = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (result) {
|
|
|
|
|
search->ClearBuffer();
|
|
|
|
|
}
|
|
|
|
|
if (top_level) { // Manage state after NextSolution().
|
2010-10-06 16:04:31 +00:00
|
|
|
state_ = (result ? AT_SOLUTION : NO_MORE_SOLUTIONS);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::EndSearch() {
|
|
|
|
|
Search* const search = searches_.back();
|
2012-06-13 16:14:46 +00:00
|
|
|
if (search->backtrack_at_the_end_of_the_search()) {
|
2012-05-17 15:43:34 +00:00
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
} else {
|
|
|
|
|
CHECK_GT(searches_.size(), 2);
|
|
|
|
|
if (search->sentinel_pushed_ > 0) {
|
|
|
|
|
JumpToSentinelWhenNested();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
search->ExitSearch();
|
|
|
|
|
search->Clear();
|
2012-06-13 16:14:46 +00:00
|
|
|
if (2 == searches_.size()) { // Ending top level search.
|
|
|
|
|
// Restores the state.
|
2012-05-17 15:43:34 +00:00
|
|
|
state_ = OUTSIDE_SEARCH;
|
2012-06-13 16:14:46 +00:00
|
|
|
// Checks if we want to export the profile info.
|
2012-05-17 15:43:34 +00:00
|
|
|
if (!FLAGS_cp_profile_file.empty()) {
|
|
|
|
|
LOG(INFO) << "Exporting profile to " << FLAGS_cp_profile_file;
|
|
|
|
|
ExportProfilingOverview(FLAGS_cp_profile_file);
|
|
|
|
|
}
|
|
|
|
|
} else { // We clean the nested Search.
|
|
|
|
|
delete search;
|
|
|
|
|
searches_.pop_back();
|
2011-09-13 08:18:38 +00:00
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::CheckAssignment(Assignment* const solution) {
|
|
|
|
|
CHECK(solution);
|
2011-07-15 18:51:39 +00:00
|
|
|
if (state_ == IN_SEARCH || state_ == IN_ROOT_NODE) {
|
2012-05-17 15:43:34 +00:00
|
|
|
LOG(FATAL) << "CheckAssignment is only available at the top level.";
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
// Check state and go to OUTSIDE_SEARCH.
|
|
|
|
|
Search* const search = searches_.back();
|
|
|
|
|
search->set_created_by_solve(false); // default behavior.
|
|
|
|
|
|
|
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
state_ = OUTSIDE_SEARCH;
|
|
|
|
|
|
|
|
|
|
// Push monitors and enter search.
|
|
|
|
|
search->EnterSearch();
|
|
|
|
|
|
|
|
|
|
// Push sentinel and set decision builder.
|
2012-02-14 14:16:12 +00:00
|
|
|
DCHECK_EQ(0, SolveDepth());
|
|
|
|
|
DCHECK_EQ(2, searches_.size());
|
2010-09-15 12:42:33 +00:00
|
|
|
PushSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
search->BeginInitialPropagation();
|
|
|
|
|
CP_TRY(search) {
|
2011-07-15 18:51:39 +00:00
|
|
|
state_ = IN_ROOT_NODE;
|
2010-09-15 12:42:33 +00:00
|
|
|
DecisionBuilder * const restore = MakeRestoreAssignment(solution);
|
|
|
|
|
restore->Next(this);
|
|
|
|
|
ProcessConstraints();
|
|
|
|
|
search->EndInitialPropagation();
|
|
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
search->ClearBuffer();
|
2011-07-15 18:51:39 +00:00
|
|
|
state_ = OUTSIDE_SEARCH;
|
2010-09-15 12:42:33 +00:00
|
|
|
return true;
|
2010-09-16 14:16:51 +00:00
|
|
|
} CP_ON_FAIL {
|
2011-07-15 18:51:39 +00:00
|
|
|
const int index = constraint_index_ < constraints_list_.size() ?
|
|
|
|
|
constraint_index_ :
|
|
|
|
|
additional_constraints_parent_list_[additional_constraint_index_];
|
|
|
|
|
Constraint* const ct = constraints_list_[index];
|
2010-09-15 12:42:33 +00:00
|
|
|
if (ct->name().empty()) {
|
2011-01-20 14:16:31 +00:00
|
|
|
LOG(INFO) << "Failing constraint = " << ct->DebugString();
|
2010-09-15 12:42:33 +00:00
|
|
|
} else {
|
2011-01-20 14:16:31 +00:00
|
|
|
LOG(INFO) << "Failing constraint = " << ct->name() << ":"
|
|
|
|
|
<< ct->DebugString();
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
2010-12-06 16:29:32 +00:00
|
|
|
queue_->AfterFailure();
|
2010-09-15 12:42:33 +00:00
|
|
|
BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
|
|
|
|
|
state_ = PROBLEM_INFEASIBLE;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:19:59 +00:00
|
|
|
namespace {
|
|
|
|
|
class AddConstraintDecisionBuilder : public DecisionBuilder {
|
|
|
|
|
public:
|
|
|
|
|
explicit AddConstraintDecisionBuilder(Constraint* const ct)
|
|
|
|
|
: constraint_(ct) {
|
|
|
|
|
CHECK_NOTNULL(ct);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~AddConstraintDecisionBuilder() {}
|
|
|
|
|
|
|
|
|
|
virtual Decision* Next(Solver* const solver) {
|
|
|
|
|
solver->AddConstraint(constraint_);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual string DebugString() const {
|
|
|
|
|
return StringPrintf("AddConstraintDecisionBuilder(%s)",
|
|
|
|
|
constraint_->DebugString().c_str());
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
Constraint* const constraint_;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
DecisionBuilder* Solver::MakeConstraintAdder(Constraint* const ct) {
|
|
|
|
|
return RevAlloc(new AddConstraintDecisionBuilder(ct));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Solver::CheckConstraint(Constraint* const ct) {
|
|
|
|
|
return Solve(MakeConstraintAdder(ct));
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
bool Solver::SolveAndCommit(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
2012-05-30 09:18:17 +00:00
|
|
|
return SolveAndCommit(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
bool Solver::SolveAndCommit(DecisionBuilder* const db) {
|
2012-05-30 09:18:17 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
|
|
|
|
return SolveAndCommit(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
bool Solver::SolveAndCommit(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
2012-05-30 09:18:17 +00:00
|
|
|
return SolveAndCommit(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
bool Solver::SolveAndCommit(DecisionBuilder* const db,
|
|
|
|
|
SearchMonitor* const m1,
|
|
|
|
|
SearchMonitor* const m2,
|
|
|
|
|
SearchMonitor* const m3) {
|
2011-05-17 20:38:55 +00:00
|
|
|
std::vector<SearchMonitor*> monitors;
|
2010-09-15 12:42:33 +00:00
|
|
|
monitors.push_back(m1);
|
|
|
|
|
monitors.push_back(m2);
|
|
|
|
|
monitors.push_back(m3);
|
2012-05-30 09:18:17 +00:00
|
|
|
return SolveAndCommit(db, monitors);
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 15:43:34 +00:00
|
|
|
bool Solver::SolveAndCommit(DecisionBuilder* const db,
|
2012-05-30 09:18:17 +00:00
|
|
|
const std::vector<SearchMonitor*>& monitors) {
|
|
|
|
|
NewSearch(db, monitors);
|
2010-10-06 16:04:31 +00:00
|
|
|
searches_.back()->set_created_by_solve(true); // Overwrites default.
|
2012-06-13 16:14:46 +00:00
|
|
|
searches_.back()->set_backtrack_at_the_end_of_the_search(false);
|
2012-05-17 15:43:34 +00:00
|
|
|
NextSolution();
|
|
|
|
|
const bool solution_found = searches_.back()->solution_counter() > 0;
|
|
|
|
|
EndSearch();
|
|
|
|
|
return solution_found;
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::Fail() {
|
2010-10-08 23:55:59 +00:00
|
|
|
if (fail_intercept_) {
|
|
|
|
|
fail_intercept_->Run();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-11-03 10:27:53 +00:00
|
|
|
ConstraintSolverFailsHere();
|
2010-09-15 12:42:33 +00:00
|
|
|
fails_++;
|
|
|
|
|
searches_.back()->BeginFail();
|
|
|
|
|
searches_.back()->JumpBack();
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-19 15:24:32 +00:00
|
|
|
// ----- Cast Expression -----
|
|
|
|
|
|
|
|
|
|
IntExpr* Solver::CastExpression(IntVar* const var) const {
|
|
|
|
|
const IntegerCastInfo* const cast_info =
|
|
|
|
|
FindOrNull(cast_information_, var);
|
|
|
|
|
if (cast_info != NULL) {
|
|
|
|
|
return cast_info->expression;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// --- Propagation object names ---
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
string Solver::GetName(const PropagationBaseObject* object) {
|
2010-09-15 12:42:33 +00:00
|
|
|
const string* name = FindOrNull(propagation_object_names_, object);
|
|
|
|
|
if (name != NULL) {
|
|
|
|
|
return *name;
|
|
|
|
|
}
|
2011-11-07 15:31:18 +00:00
|
|
|
const IntegerCastInfo* const cast_info =
|
|
|
|
|
FindOrNull(cast_information_, object);
|
|
|
|
|
if (cast_info != NULL && cast_info->expression != NULL) {
|
2011-11-14 20:55:35 +00:00
|
|
|
if (cast_info->expression->HasName()) {
|
|
|
|
|
return StringPrintf("Var<%s>",
|
|
|
|
|
cast_info->expression->name().c_str());
|
2012-07-04 13:29:41 +00:00
|
|
|
} else if (FLAGS_cp_name_cast_variables) {
|
2011-11-14 20:55:35 +00:00
|
|
|
return StringPrintf("Var<%s>",
|
|
|
|
|
cast_info->expression->DebugString().c_str());
|
2012-07-04 13:29:41 +00:00
|
|
|
} else {
|
|
|
|
|
const string new_name =
|
|
|
|
|
StringPrintf("CastVar<%d>", anonymous_variable_index_++);
|
|
|
|
|
propagation_object_names_[object] = new_name;
|
|
|
|
|
return new_name;
|
2011-11-14 20:55:35 +00:00
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
}
|
2011-11-19 01:59:37 +00:00
|
|
|
const string base_name = object->BaseName();
|
|
|
|
|
if (FLAGS_cp_name_variables && !base_name.empty()) {
|
|
|
|
|
const string new_name =
|
|
|
|
|
StringPrintf("%s_%d", base_name.c_str(), anonymous_variable_index_++);
|
|
|
|
|
propagation_object_names_[object] = new_name;
|
|
|
|
|
return new_name;
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
return empty_name_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::SetName(const PropagationBaseObject* object, const string& name) {
|
2010-12-06 10:59:35 +00:00
|
|
|
if (parameters_.store_names
|
2010-09-15 12:42:33 +00:00
|
|
|
&& GetName(object).compare(name) != 0) { // in particular if name.empty()
|
|
|
|
|
propagation_object_names_[object] = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
bool Solver::HasName(const PropagationBaseObject* const object) const {
|
2011-11-19 01:59:37 +00:00
|
|
|
return ContainsKey(propagation_object_names_, object) ||
|
|
|
|
|
(!object->BaseName().empty() && FLAGS_cp_name_variables);
|
2011-08-11 05:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// ------------------ Useful Operators ------------------
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
std::ostream& operator <<(std::ostream& out, const Solver* const s) { // NOLINT
|
2010-09-15 12:42:33 +00:00
|
|
|
out << s->DebugString();
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
std::ostream& operator <<(std::ostream& out, const BaseObject* const o) { // NOLINT
|
2010-09-15 12:42:33 +00:00
|
|
|
out << o->DebugString();
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- PropagationBaseObject ---------
|
|
|
|
|
|
|
|
|
|
string PropagationBaseObject::name() const {
|
|
|
|
|
// TODO(user) : merge with GetName() code to remove a string copy.
|
|
|
|
|
return solver_->GetName(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PropagationBaseObject::set_name(const string& name) {
|
|
|
|
|
solver_->SetName(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
bool PropagationBaseObject::HasName() const {
|
|
|
|
|
return solver_->HasName(this);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
string PropagationBaseObject::BaseName() const {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-29 09:46:25 +00:00
|
|
|
// ---------- Decision Builder ----------
|
|
|
|
|
|
|
|
|
|
string DecisionBuilder::DebugString() const {
|
|
|
|
|
return "DecisionBuilder";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DecisionBuilder::AppendMonitors(Solver* const solver,
|
2011-08-11 05:15:18 +00:00
|
|
|
std::vector<SearchMonitor*>* const extras) {}
|
|
|
|
|
|
|
|
|
|
void DecisionBuilder::Accept(ModelVisitor* const visitor) const {}
|
2011-06-29 09:46:25 +00:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// ---------- Decision and DecisionVisitor ----------
|
|
|
|
|
|
|
|
|
|
void Decision::Accept(DecisionVisitor* const visitor) const {
|
|
|
|
|
visitor->VisitUnknownDecision();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DecisionVisitor::VisitSetVariableValue(IntVar* const var, int64 value) {}
|
2010-10-15 13:22:21 +00:00
|
|
|
void DecisionVisitor::VisitSplitVariableDomain(IntVar* const var,
|
|
|
|
|
int64 value,
|
|
|
|
|
bool lower) {}
|
2010-09-15 12:42:33 +00:00
|
|
|
void DecisionVisitor::VisitUnknownDecision() {}
|
2011-01-06 17:40:30 +00:00
|
|
|
void DecisionVisitor::VisitScheduleOrPostpone(IntervalVar* const var,
|
|
|
|
|
int64 est) {}
|
2011-12-16 21:02:59 +00:00
|
|
|
void DecisionVisitor::VisitRankFirstInterval(SequenceVar* const sequence,
|
|
|
|
|
int index) {}
|
|
|
|
|
|
|
|
|
|
void DecisionVisitor::VisitRankLastInterval(SequenceVar* const sequence,
|
|
|
|
|
int index) {}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
// ---------- ModelVisitor ----------
|
|
|
|
|
|
2011-09-05 13:45:29 +00:00
|
|
|
// Tags for constraints, arguments, extensions.
|
2011-07-11 20:13:14 +00:00
|
|
|
|
|
|
|
|
const char ModelVisitor::kAbs[] = "Abs";
|
2012-06-27 15:24:10 +00:00
|
|
|
const char ModelVisitor::kAbsEqual[] = "AbsEqual";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kAllDifferent[] = "AllDifferent";
|
|
|
|
|
const char ModelVisitor::kAllowedAssignments[] = "AllowedAssignments";
|
|
|
|
|
const char ModelVisitor::kBetween[] = "Between";
|
|
|
|
|
const char ModelVisitor::kConvexPiecewise[] = "ConvexPiecewise";
|
|
|
|
|
const char ModelVisitor::kCountEqual[] = "CountEqual";
|
|
|
|
|
const char ModelVisitor::kCumulative[] = "Cumulative";
|
|
|
|
|
const char ModelVisitor::kDeviation[] = "Deviation";
|
|
|
|
|
const char ModelVisitor::kDifference[] = "Difference";
|
2011-11-03 10:27:53 +00:00
|
|
|
const char ModelVisitor::kDisjunctive[] = "Disjunctive";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kDistribute[] = "Distribute";
|
|
|
|
|
const char ModelVisitor::kDivide[] = "Divide";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kDurationExpr[] = "DurationExpression";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kElement[] = "Element";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kElementEqual[] = "ElementEqual";
|
|
|
|
|
const char ModelVisitor::kEndExpr[] = "EndExpression";
|
|
|
|
|
const char ModelVisitor::kEquality[] = "Equal";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kFalseConstraint[] = "FalseConstraint";
|
|
|
|
|
const char ModelVisitor::kGreater[] = "Greater";
|
|
|
|
|
const char ModelVisitor::kGreaterOrEqual[] = "GreaterOrEqual";
|
2012-08-14 21:45:43 +00:00
|
|
|
const char ModelVisitor::kGlobalCardinality[] = "GlobalCardinality";
|
2012-08-17 23:30:10 +00:00
|
|
|
const char ModelVisitor::kIndexOf[] = "IndexOf";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kIntegerVariable[] = "IntegerVariable";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kIntervalBinaryRelation[] = "IntervalBinaryRelation";
|
|
|
|
|
const char ModelVisitor::kIntervalDisjunction[] = "IntervalDisjunction";
|
|
|
|
|
const char ModelVisitor::kIntervalUnaryRelation[] = "IntervalUnaryRelation";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kIntervalVariable[] = "IntervalVariable";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kIsBetween[] = "IsBetween;";
|
|
|
|
|
const char ModelVisitor::kIsDifferent[] = "IsDifferent";
|
|
|
|
|
const char ModelVisitor::kIsEqual[] = "IsEqual";
|
2012-07-04 22:05:58 +00:00
|
|
|
const char ModelVisitor::kIsGreater[] = "IsGreater";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kIsGreaterOrEqual[] = "IsGreaterOrEqual";
|
2012-07-04 22:05:58 +00:00
|
|
|
const char ModelVisitor::kIsLess[] = "IsLess";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kIsLessOrEqual[] = "IsLessOrEqual";
|
|
|
|
|
const char ModelVisitor::kIsMember[] = "IsMember;";
|
|
|
|
|
const char ModelVisitor::kLess[] = "Less";
|
|
|
|
|
const char ModelVisitor::kLessOrEqual[] = "LessOrEqual";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kLinkExprVar[] = "CastExpressionIntoVariable";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kMapDomain[] = "MapDomain";
|
|
|
|
|
const char ModelVisitor::kMax[] = "Max";
|
|
|
|
|
const char ModelVisitor::kMaxEqual[] = "MaxEqual";
|
|
|
|
|
const char ModelVisitor::kMember[] = "Member";
|
|
|
|
|
const char ModelVisitor::kMin[] = "Min";
|
|
|
|
|
const char ModelVisitor::kMinEqual[] = "MinEqual";
|
2012-08-14 21:45:43 +00:00
|
|
|
const char ModelVisitor::kModuloConstraint[] = "ModuloConstraint";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kNoCycle[] = "NoCycle";
|
|
|
|
|
const char ModelVisitor::kNonEqual[] = "NonEqual";
|
|
|
|
|
const char ModelVisitor::kOpposite[] = "Opposite";
|
|
|
|
|
const char ModelVisitor::kPack[] = "Pack";
|
|
|
|
|
const char ModelVisitor::kPathCumul[] = "PathCumul";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kPerformedExpr[] = "PerformedExpression";
|
2012-06-19 15:24:32 +00:00
|
|
|
const char ModelVisitor::kPower[] = "Power";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kProduct[] = "Product";
|
|
|
|
|
const char ModelVisitor::kScalProd[] = "ScalarProduct";
|
|
|
|
|
const char ModelVisitor::kScalProdEqual[] = "ScalarProductEqual";
|
|
|
|
|
const char ModelVisitor::kScalProdGreaterOrEqual[] =
|
|
|
|
|
"ScalarProductGreaterOrEqual";
|
|
|
|
|
const char ModelVisitor::kScalProdLessOrEqual[] = "ScalarProductLessOrEqual";
|
|
|
|
|
const char ModelVisitor::kSemiContinuous[] = "SemiContinuous";
|
2011-11-03 10:27:53 +00:00
|
|
|
const char ModelVisitor::kSequenceVariable[] = "SequenceVariable";
|
2012-04-10 14:21:22 +00:00
|
|
|
const char ModelVisitor::kSortingConstraint[] = "SortingConstraint";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kSquare[] = "Square";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kStartExpr[]= "StartExpression";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kSum[] = "Sum";
|
|
|
|
|
const char ModelVisitor::kSumEqual[] = "SumEqual";
|
|
|
|
|
const char ModelVisitor::kSumGreaterOrEqual[] = "SumGreaterOrEqual";
|
|
|
|
|
const char ModelVisitor::kSumLessOrEqual[] = "SumLessOrEqual";
|
|
|
|
|
const char ModelVisitor::kTransition[]= "Transition";
|
|
|
|
|
const char ModelVisitor::kTrueConstraint[] = "TrueConstraint";
|
2012-07-04 13:29:41 +00:00
|
|
|
const char ModelVisitor::kVarBoundWatcher[] = "VarBoundWatcher";
|
|
|
|
|
const char ModelVisitor::kVarValueWatcher[] = "VarValueWatcher";
|
2011-07-11 20:13:14 +00:00
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kCountAssignedItemsExtension[] = "CountAssignedItems";
|
|
|
|
|
const char ModelVisitor::kCountUsedBinsExtension[] = "CountUsedBins";
|
|
|
|
|
const char ModelVisitor::kInt64ToBoolExtension[] = "Int64ToBoolFunction";
|
|
|
|
|
const char ModelVisitor::kInt64ToInt64Extension[] = "Int64ToInt64Function";
|
|
|
|
|
const char ModelVisitor::kObjectiveExtension[] = "Objective";
|
|
|
|
|
const char ModelVisitor::kSearchLimitExtension[] = "SearchLimit";
|
|
|
|
|
const char ModelVisitor::kUsageEqualVariableExtension[] = "UsageEqualVariable";
|
|
|
|
|
const char ModelVisitor::kUsageLessConstantExtension[] = "UsageLessConstant";
|
|
|
|
|
const char ModelVisitor::kVariableGroupExtension[] = "VariableGroup";
|
|
|
|
|
const char ModelVisitor::kVariableUsageLessConstantExtension[] =
|
|
|
|
|
"VariableUsageLessConstant";
|
|
|
|
|
const char ModelVisitor::kWeightedSumOfAssignedEqualVariableExtension[] =
|
|
|
|
|
"WeightedSumOfAssignedEqualVariable";
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kActiveArgument[] = "active";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kAssumePathsArgument[] = "assume_paths";
|
|
|
|
|
const char ModelVisitor::kBranchesLimitArgument[] = "branches_limit";
|
|
|
|
|
const char ModelVisitor::kCapacityArgument[] = "capacity";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kCardsArgument[] = "cardinalities";
|
|
|
|
|
const char ModelVisitor::kCoefficientsArgument[] = "coefficients";
|
|
|
|
|
const char ModelVisitor::kCountArgument[] = "count";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kCumulativeArgument[] = "cumulative";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kCumulsArgument[] = "cumuls";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kDemandsArgument[] = "demands";
|
|
|
|
|
const char ModelVisitor::kDurationMinArgument[] = "duration_min";
|
|
|
|
|
const char ModelVisitor::kDurationMaxArgument[] = "duration_max";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kEarlyCostArgument[] = "early_cost";
|
|
|
|
|
const char ModelVisitor::kEarlyDateArgument[] = "early_date";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kEndMinArgument[] = "end_min";
|
|
|
|
|
const char ModelVisitor::kEndMaxArgument[] = "end_max";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kExpressionArgument[] = "expression";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kFailuresLimitArgument[] = "failures_limit";
|
|
|
|
|
const char ModelVisitor::kFinalStatesArgument[] = "final_states";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kFixedChargeArgument[] = "fixed_charge";
|
|
|
|
|
const char ModelVisitor::kIndex2Argument[] = "index2";
|
|
|
|
|
const char ModelVisitor::kIndexArgument[] = "index";
|
|
|
|
|
const char ModelVisitor::kInitialState[] = "initial_state";
|
|
|
|
|
const char ModelVisitor::kIntervalArgument[] = "interval";
|
|
|
|
|
const char ModelVisitor::kIntervalsArgument[] = "intervals";
|
|
|
|
|
const char ModelVisitor::kLateCostArgument[] = "late_cost";
|
|
|
|
|
const char ModelVisitor::kLateDateArgument[] = "late_date";
|
|
|
|
|
const char ModelVisitor::kLeftArgument[] = "left";
|
|
|
|
|
const char ModelVisitor::kMaxArgument[] = "max_value";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kMaximizeArgument[] = "maximize";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kMinArgument[] = "min_value";
|
2012-08-14 21:45:43 +00:00
|
|
|
const char ModelVisitor::kModuloArgument[] = "modulo";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kNextsArgument[] = "nexts";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kOptionalArgument[] = "optional";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kRangeArgument[] = "range";
|
|
|
|
|
const char ModelVisitor::kRelationArgument[] = "relation";
|
|
|
|
|
const char ModelVisitor::kRightArgument[] = "right";
|
2011-11-03 10:27:53 +00:00
|
|
|
const char ModelVisitor::kSequenceArgument[] = "sequence";
|
|
|
|
|
const char ModelVisitor::kSequencesArgument[] = "sequences";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kSmartTimeCheckArgument[] = "smart_time_check";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kSizeArgument[] = "size";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kSolutionLimitArgument[] = "solutions_limit";
|
|
|
|
|
const char ModelVisitor::kStartMinArgument[] = "start_min";
|
|
|
|
|
const char ModelVisitor::kStartMaxArgument[] = "start_max";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kStepArgument[] = "step";
|
|
|
|
|
const char ModelVisitor::kTargetArgument[] = "target_variable";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kTimeLimitArgument[] = "time_limit";
|
2011-07-11 20:13:14 +00:00
|
|
|
const char ModelVisitor::kTransitsArgument[] = "transits";
|
|
|
|
|
const char ModelVisitor::kTuplesArgument[] = "tuples";
|
|
|
|
|
const char ModelVisitor::kValueArgument[] = "value";
|
|
|
|
|
const char ModelVisitor::kValuesArgument[] = "values";
|
|
|
|
|
const char ModelVisitor::kVarsArgument[] = "variables";
|
2011-12-17 18:03:38 +00:00
|
|
|
const char ModelVisitor::kVariableArgument[] = "variable";
|
2011-07-11 20:13:14 +00:00
|
|
|
|
2012-08-31 16:17:59 +00:00
|
|
|
const char ModelVisitor::kCoverOperation[] = "cover";
|
2011-08-11 05:15:18 +00:00
|
|
|
const char ModelVisitor::kMirrorOperation[] = "mirror";
|
|
|
|
|
const char ModelVisitor::kRelaxedMaxOperation[] = "relaxed_max";
|
|
|
|
|
const char ModelVisitor::kRelaxedMinOperation[] = "relaxed_min";
|
2011-12-17 18:03:38 +00:00
|
|
|
const char ModelVisitor::kSumOperation[] = "sum";
|
|
|
|
|
const char ModelVisitor::kDifferenceOperation[] = "difference";
|
|
|
|
|
const char ModelVisitor::kProductOperation[] = "product";
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
// Methods
|
|
|
|
|
|
|
|
|
|
ModelVisitor::~ModelVisitor() {}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::BeginVisitModel(const string& type_name) {}
|
|
|
|
|
void ModelVisitor::EndVisitModel(const string& type_name) {}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::BeginVisitConstraint(const string& type_name,
|
|
|
|
|
const Constraint* const constraint) {}
|
|
|
|
|
void ModelVisitor::EndVisitConstraint(const string& type_name,
|
|
|
|
|
const Constraint* const constraint) {}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void ModelVisitor::BeginVisitExtension(const string& type) {
|
2011-07-11 20:13:14 +00:00
|
|
|
}
|
2011-08-11 05:15:18 +00:00
|
|
|
void ModelVisitor::EndVisitExtension(const string& type) {}
|
2011-07-11 20:13:14 +00:00
|
|
|
|
|
|
|
|
void ModelVisitor::BeginVisitIntegerExpression(const string& type_name,
|
|
|
|
|
const IntExpr* const expr) {}
|
|
|
|
|
void ModelVisitor::EndVisitIntegerExpression(const string& type_name,
|
|
|
|
|
const IntExpr* const expr) {}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitIntegerVariable(const IntVar* const variable,
|
|
|
|
|
const IntExpr* const delegate) {
|
|
|
|
|
if (delegate != NULL) {
|
|
|
|
|
delegate->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-17 18:03:38 +00:00
|
|
|
void ModelVisitor::VisitIntegerVariable(const IntVar* const variable,
|
|
|
|
|
const string& operation,
|
|
|
|
|
int64 value,
|
|
|
|
|
const IntVar* const delegate) {
|
|
|
|
|
if (delegate != NULL) {
|
|
|
|
|
delegate->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
void ModelVisitor::VisitIntervalVariable(const IntervalVar* const variable,
|
2011-12-17 18:03:38 +00:00
|
|
|
const string& operation,
|
2011-07-11 20:13:14 +00:00
|
|
|
const IntervalVar* const delegate) {
|
|
|
|
|
if (delegate != NULL) {
|
|
|
|
|
delegate->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
void ModelVisitor::VisitIntervalVariable(const IntervalVar* const variable,
|
2011-12-17 18:03:38 +00:00
|
|
|
const string& operation,
|
2011-08-11 07:26:19 +00:00
|
|
|
const IntervalVar* const * delegates,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
delegates[i]->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-03 10:27:53 +00:00
|
|
|
void ModelVisitor::VisitSequenceVariable(const SequenceVar* const variable) {
|
|
|
|
|
for (int i = 0; i < variable->size(); ++i) {
|
|
|
|
|
variable->Interval(i)->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-11 20:13:14 +00:00
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void ModelVisitor::VisitIntegerArgument(const string& arg_name, int64 value) {}
|
2011-07-11 20:13:14 +00:00
|
|
|
|
2011-07-14 23:37:47 +00:00
|
|
|
void ModelVisitor::VisitIntegerArrayArgument(const string& arg_name,
|
2011-08-11 05:15:18 +00:00
|
|
|
const int64* const values,
|
|
|
|
|
int size) {}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitIntegerMatrixArgument(const string& arg_name,
|
2012-03-15 16:22:13 +00:00
|
|
|
const IntTupleSet& tuples) {}
|
2011-07-11 20:13:14 +00:00
|
|
|
|
|
|
|
|
void ModelVisitor::VisitIntegerExpressionArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const IntExpr* const argument) {
|
|
|
|
|
argument->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitIntegerVariableArrayArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const IntVar* const * arguments,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
arguments[i]->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 20:36:27 +00:00
|
|
|
void ModelVisitor::VisitIntegerVariableArrayArgument(
|
|
|
|
|
const string& arg_name,
|
2012-05-30 11:53:36 +00:00
|
|
|
const std::vector<IntVar*>& arguments) {
|
2012-03-13 20:36:27 +00:00
|
|
|
VisitIntegerVariableArrayArgument(arg_name,
|
2012-05-30 11:53:36 +00:00
|
|
|
arguments.data(),
|
2012-03-13 20:36:27 +00:00
|
|
|
arguments.size());
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
void ModelVisitor::VisitIntervalArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const IntervalVar* const argument) {
|
|
|
|
|
argument->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitIntervalArrayArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const IntervalVar* const * arguments,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
arguments[i]->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-03 10:27:53 +00:00
|
|
|
void ModelVisitor::VisitSequenceArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const SequenceVar* const argument) {
|
|
|
|
|
argument->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitSequenceArrayArgument(
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
const SequenceVar* const * arguments,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
arguments[i]->Accept(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
// ----- Helpers -----
|
|
|
|
|
|
2011-07-14 23:37:47 +00:00
|
|
|
void ModelVisitor::VisitConstIntArrayArgument(const string& arg_name,
|
2011-07-11 20:13:14 +00:00
|
|
|
const ConstIntArray& values) {
|
2011-07-14 23:37:47 +00:00
|
|
|
VisitIntegerArrayArgument(arg_name, values.RawData(), values.size());
|
2011-07-11 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-17 23:20:02 +00:00
|
|
|
void ModelVisitor::VisitIntegerArrayArgument(const string& arg_name,
|
|
|
|
|
const std::vector<int64>& values) {
|
|
|
|
|
VisitIntegerArrayArgument(arg_name, values.data(), values.size());
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 05:15:18 +00:00
|
|
|
void ModelVisitor::VisitInt64ToBoolExtension(
|
|
|
|
|
ResultCallback1<bool, int64>* const callback,
|
|
|
|
|
int64 index_min,
|
|
|
|
|
int64 index_max) {
|
|
|
|
|
if (callback == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::vector<int64> cached_results;
|
|
|
|
|
for (int i = index_min; i <= index_max; ++i) {
|
|
|
|
|
cached_results.push_back(callback->Run(i));
|
|
|
|
|
}
|
|
|
|
|
// TODO(user): VisitBoolArrayArgument?
|
|
|
|
|
BeginVisitExtension(kInt64ToBoolExtension);
|
|
|
|
|
VisitIntegerArgument(kMinArgument, index_min);
|
|
|
|
|
VisitIntegerArgument(kMaxArgument, index_max);
|
|
|
|
|
VisitIntegerArrayArgument(kValuesArgument,
|
|
|
|
|
cached_results.data(),
|
|
|
|
|
cached_results.size());
|
|
|
|
|
EndVisitExtension(kInt64ToBoolExtension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelVisitor::VisitInt64ToInt64Extension(
|
|
|
|
|
Solver::IndexEvaluator1* const callback,
|
|
|
|
|
int64 index_min,
|
|
|
|
|
int64 index_max) {
|
|
|
|
|
if (callback == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::vector<int64> cached_results;
|
|
|
|
|
for (int i = index_min; i <= index_max; ++i) {
|
|
|
|
|
cached_results.push_back(callback->Run(i));
|
|
|
|
|
}
|
|
|
|
|
BeginVisitExtension(kInt64ToInt64Extension);
|
|
|
|
|
VisitIntegerArgument(kMinArgument, index_min);
|
|
|
|
|
VisitIntegerArgument(kMaxArgument, index_max);
|
|
|
|
|
VisitIntegerArrayArgument(kValuesArgument,
|
|
|
|
|
cached_results.data(),
|
|
|
|
|
cached_results.size());
|
|
|
|
|
EndVisitExtension(kInt64ToInt64Extension);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 07:26:19 +00:00
|
|
|
void ModelVisitor::VisitInt64ToInt64AsArray(
|
|
|
|
|
Solver::IndexEvaluator1* const callback,
|
|
|
|
|
const string& arg_name,
|
|
|
|
|
int64 index_max) {
|
|
|
|
|
if (callback == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::vector<int64> cached_results;
|
|
|
|
|
for (int i = 0; i <= index_max; ++i) {
|
|
|
|
|
cached_results.push_back(callback->Run(i));
|
|
|
|
|
}
|
|
|
|
|
VisitIntegerArrayArgument(arg_name,
|
|
|
|
|
cached_results.data(),
|
|
|
|
|
cached_results.size());
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
// ---------- Search Monitor ----------
|
|
|
|
|
|
|
|
|
|
void SearchMonitor::EnterSearch() {}
|
|
|
|
|
void SearchMonitor::RestartSearch() {}
|
|
|
|
|
void SearchMonitor::ExitSearch() {}
|
|
|
|
|
void SearchMonitor::BeginNextDecision(DecisionBuilder* const b) {}
|
|
|
|
|
void SearchMonitor::EndNextDecision(DecisionBuilder* const b,
|
|
|
|
|
Decision* const d) {}
|
|
|
|
|
void SearchMonitor::ApplyDecision(Decision* const d) {}
|
|
|
|
|
void SearchMonitor::RefuteDecision(Decision* const d) {}
|
2011-03-31 12:28:12 +00:00
|
|
|
void SearchMonitor::AfterDecision(Decision* const d, bool apply) {}
|
2010-09-15 12:42:33 +00:00
|
|
|
void SearchMonitor::BeginFail() {}
|
|
|
|
|
void SearchMonitor::EndFail() {}
|
|
|
|
|
void SearchMonitor::BeginInitialPropagation() {}
|
|
|
|
|
void SearchMonitor::EndInitialPropagation() {}
|
2010-10-06 16:04:31 +00:00
|
|
|
bool SearchMonitor::AcceptSolution() { return true; }
|
|
|
|
|
bool SearchMonitor::AtSolution() { return false; }
|
2010-09-15 12:42:33 +00:00
|
|
|
void SearchMonitor::NoMoreSolutions() {}
|
|
|
|
|
bool SearchMonitor::LocalOptimum() { return false; }
|
|
|
|
|
bool SearchMonitor::AcceptDelta(Assignment* delta,
|
|
|
|
|
Assignment* deltadelta) { return true; }
|
|
|
|
|
void SearchMonitor::AcceptNeighbor() {}
|
|
|
|
|
void SearchMonitor::FinishCurrentSearch() {
|
|
|
|
|
solver()->searches_.back()->set_should_finish(true);
|
|
|
|
|
}
|
|
|
|
|
void SearchMonitor::RestartCurrentSearch() {
|
|
|
|
|
solver()->searches_.back()->set_should_restart(true);
|
|
|
|
|
}
|
|
|
|
|
void SearchMonitor::PeriodicCheck() {}
|
2011-08-11 05:15:18 +00:00
|
|
|
void SearchMonitor::Accept(ModelVisitor* const visitor) const {}
|
2011-11-19 01:59:37 +00:00
|
|
|
// A search monitors adds itself on the active search.
|
2011-11-16 17:32:24 +00:00
|
|
|
void SearchMonitor::Install() {
|
|
|
|
|
solver()->searches_.back()->push_monitor(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- Propagation Monitor -----------
|
|
|
|
|
PropagationMonitor::PropagationMonitor(Solver* const s) : SearchMonitor(s) {}
|
|
|
|
|
|
|
|
|
|
PropagationMonitor::~PropagationMonitor() {}
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
// A propagation monitor listens to search events as well as
|
2011-11-16 17:32:24 +00:00
|
|
|
// propagation events.
|
|
|
|
|
void PropagationMonitor::Install() {
|
|
|
|
|
SearchMonitor::Install();
|
|
|
|
|
solver()->AddPropagationMonitor(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- Trace ----------
|
|
|
|
|
|
|
|
|
|
class Trace : public PropagationMonitor {
|
|
|
|
|
public:
|
2011-11-19 01:59:37 +00:00
|
|
|
explicit Trace(Solver* const s) : PropagationMonitor(s) {}
|
|
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
virtual ~Trace() {}
|
|
|
|
|
|
|
|
|
|
virtual void BeginConstraintInitialPropagation(
|
|
|
|
|
const Constraint* const constraint) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->BeginConstraintInitialPropagation(constraint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndConstraintInitialPropagation(
|
|
|
|
|
const Constraint* const constraint) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->EndConstraintInitialPropagation(constraint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void BeginNestedConstraintInitialPropagation(
|
|
|
|
|
const Constraint* const parent,
|
|
|
|
|
const Constraint* const nested) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->BeginNestedConstraintInitialPropagation(parent, nested);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndNestedConstraintInitialPropagation(
|
|
|
|
|
const Constraint* const parent,
|
|
|
|
|
const Constraint* const nested) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->EndNestedConstraintInitialPropagation(parent, nested);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RegisterDemon(const Demon* const demon) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RegisterDemon(demon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void BeginDemonRun(const Demon* const demon) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->BeginDemonRun(demon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndDemonRun(const Demon* const demon) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->EndDemonRun(demon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-06 23:39:19 +00:00
|
|
|
virtual void StartProcessingIntegerVariable(const IntVar* const var) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->StartProcessingIntegerVariable(var);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void EndProcessingIntegerVariable(const IntVar* const var) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->EndProcessingIntegerVariable(var);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
virtual void PushContext(const string& context) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->PushContext(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void PopContext() {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->PopContext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
// IntExpr modifiers.
|
|
|
|
|
virtual void SetMin(IntExpr* const expr, int64 new_min) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetMin(expr, new_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetMax(IntExpr* const expr, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetMax(expr, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetRange(IntExpr* const expr, int64 new_min, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetRange(expr, new_min, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IntVar modifiers.
|
|
|
|
|
virtual void SetMin(IntVar* const var, int64 new_min) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetMin(var, new_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetMax(IntVar* const var, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetMax(var, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetRange(IntVar* const var, int64 new_min, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetRange(var, new_min, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RemoveValue(IntVar* const var, int64 value) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RemoveValue(var, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetValue(IntVar* const var, int64 value) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetValue(var, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RemoveInterval(IntVar* const var, int64 imin, int64 imax) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RemoveInterval(var, imin, imax);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetValues(IntVar* const var,
|
|
|
|
|
const int64* const values,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetValues(var, values, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RemoveValues(IntVar* const var,
|
|
|
|
|
const int64* const values,
|
|
|
|
|
int size) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RemoveValues(var, values, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IntervalVar modifiers.
|
|
|
|
|
virtual void SetStartMin(IntervalVar* const var, int64 new_min) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetStartMin(var, new_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetStartMax(IntervalVar* const var, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetStartMax(var, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetStartRange(IntervalVar* const var,
|
|
|
|
|
int64 new_min,
|
|
|
|
|
int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetStartRange(var, new_min, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetEndMin(IntervalVar* const var, int64 new_min) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetEndMin(var, new_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetEndMax(IntervalVar* const var, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetEndMax(var, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetEndRange(IntervalVar* const var,
|
|
|
|
|
int64 new_min,
|
|
|
|
|
int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetEndRange(var, new_min, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetDurationMin(IntervalVar* const var, int64 new_min) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetDurationMin(var, new_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetDurationMax(IntervalVar* const var, int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetDurationMax(var, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetDurationRange(IntervalVar* const var,
|
|
|
|
|
int64 new_min,
|
|
|
|
|
int64 new_max) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetDurationRange(var, new_min, new_max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void SetPerformed(IntervalVar* const var, bool value) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->SetPerformed(var, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-21 02:52:58 +00:00
|
|
|
virtual void RankFirst(SequenceVar* const var, int index) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RankFirst(var, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RankNotFirst(SequenceVar* const var, int index) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RankNotFirst(var, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-16 21:02:59 +00:00
|
|
|
virtual void RankLast(SequenceVar* const var, int index) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RankLast(var, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RankNotLast(SequenceVar* const var, int index) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RankNotLast(var, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void RankSequence(SequenceVar* const var,
|
|
|
|
|
const std::vector<int>& rank_first,
|
|
|
|
|
const std::vector<int>& rank_last,
|
|
|
|
|
const std::vector<int>& unperformed) {
|
|
|
|
|
for (int i = 0; i < monitors_.size(); ++i) {
|
|
|
|
|
monitors_[i]->RankSequence(var, rank_first, rank_last, unperformed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-19 01:59:37 +00:00
|
|
|
// Does not take ownership of monitor.
|
2011-11-16 17:32:24 +00:00
|
|
|
void Add(PropagationMonitor* const monitor) {
|
|
|
|
|
if (monitor != NULL) {
|
|
|
|
|
monitors_.push_back(monitor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The trace will dispatch propagation events. It needs to listen to search
|
|
|
|
|
// events.
|
|
|
|
|
virtual void Install() {
|
|
|
|
|
SearchMonitor::Install();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 18:00:08 +00:00
|
|
|
virtual string DebugString() const {
|
|
|
|
|
return "Trace";
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 17:32:24 +00:00
|
|
|
private:
|
|
|
|
|
std::vector<PropagationMonitor*> monitors_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PropagationMonitor* BuildTrace(Solver* const s) {
|
|
|
|
|
return new Trace(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solver::AddPropagationMonitor(PropagationMonitor* const monitor) {
|
|
|
|
|
// TODO(user): Check solver state?
|
|
|
|
|
reinterpret_cast<class Trace*>(propagation_monitor_.get())->Add(monitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PropagationMonitor* Solver::GetPropagationMonitor() const {
|
|
|
|
|
return propagation_monitor_.get();
|
|
|
|
|
}
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
// ----------------- Constraint class -------------------
|
|
|
|
|
|
|
|
|
|
string Constraint::DebugString() const {
|
|
|
|
|
return "Constraint";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Constraint::PostAndPropagate() {
|
|
|
|
|
FreezeQueue();
|
|
|
|
|
Post();
|
|
|
|
|
InitialPropagate();
|
|
|
|
|
UnfreezeQueue();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
void Constraint::Accept(ModelVisitor* const visitor) const {
|
|
|
|
|
visitor->BeginVisitConstraint("unknown", this);
|
2012-08-14 18:00:08 +00:00
|
|
|
VLOG(1) << "Unknown constraint " << DebugString();
|
2011-07-11 20:13:14 +00:00
|
|
|
visitor->EndVisitConstraint("unknown", this);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 15:31:18 +00:00
|
|
|
bool Constraint::IsCastConstraint() const {
|
|
|
|
|
return ContainsKey(solver()->cast_constraints_, this);
|
2011-08-11 05:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-20 13:11:01 +00:00
|
|
|
IntVar* Constraint::Var() {
|
2012-03-19 17:14:57 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 20:13:14 +00:00
|
|
|
// ----- Class IntExpr -----
|
|
|
|
|
|
|
|
|
|
void IntExpr::Accept(ModelVisitor* const visitor) const {
|
|
|
|
|
visitor->BeginVisitIntegerExpression("unknown", this);
|
2012-08-14 18:00:08 +00:00
|
|
|
VLOG(1) << "Unknown expression " << DebugString();
|
2011-07-11 20:13:14 +00:00
|
|
|
visitor->EndVisitIntegerExpression("unknown", this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-16 15:34:10 +00:00
|
|
|
#undef CP_TRY // We no longer need those.
|
|
|
|
|
#undef CP_ON_FAIL
|
|
|
|
|
#undef CP_DO_FAIL
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
} // namespace operations_research
|