-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
140 lines (128 loc) · 3.01 KB
/
solver.cpp
File metadata and controls
140 lines (128 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#include "solver.hpp"
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <typeindex>
#include "tracer.hpp"
CPSolver::CPSolver()
: _sm(new Trailer),
_store(new Storage(_sm))
{
_varId = 0;
_propagations = 0;
_nbProp = 0;
}
CPSolver::~CPSolver()
{
_iVars.clear();
_store.dealloc();
_sm.dealloc();
std::cout << "CPSolver::~CPSolver(" << this << ")" << std::endl;
}
void CPSolver::post(Constraint::Ptr c, bool enforceFixPoint)
{
if (!c)
return;
++_nbProp;
c->post();
if (enforceFixPoint)
fixpoint();
}
void CPSolver::post(ConstraintDesc::Ptr c, bool enforceFixPoint)
{
if (!c)
return;
++_nbProp;
auto constraint = c->create();
constraint->post();
if (enforceFixPoint)
fixpoint();
}
void CPSolver::registerVar(AVar::Ptr avar)
{
avar->setId(_varId++);
_iVars.push_back(avar);
}
void CPSolver::notifyFixpoint()
{
for(auto& body : _onFix)
body();
}
void CPSolver::fixpoint()
{
TRYFAIL
notifyFixpoint();
while (!_queue.empty()) {
auto c = _queue.deQueue();
c->setScheduled(false);
if (c->isActive())
{
c->propagate();
_propagations += 1;
}
}
assert(_queue.size() == 0);
ONFAIL
while (!_queue.empty()) {
_queue.deQueue()->setScheduled(false);
}
assert(_queue.size() == 0);
failNow();
ENDFAIL
}
CPSemSolver::CPSemSolver()
: _tracer(new Tracer(_sm)),
_inSearch(false) { }
CPSemSolver::~CPSemSolver()
{
delete _tracer;
}
//If it calls post with a constraint, we trust that this is not in the search, and so we don't need to add it to the memoryTrail
void CPSemSolver::post(Constraint::Ptr c, bool enforceFixPoint)
{
if (!c)
return;
++_nbProp;
c->post();
if (enforceFixPoint)
fixpoint();
}
void CPSemSolver::post(ConstraintDesc::Ptr c, bool enforceFixPoint)
{
if (!c)
return;
++_nbProp;
if (_inSearch) {
auto saved = c->clone();
_tracer->addCommand(saved);
}
auto constraint = c->create();
constraint->post();
if (enforceFixPoint)
fixpoint();
}
Tracer* CPSemSolver::tracer()
{
return _tracer;
}
void CPSemSolver::startSearch()
{
_inSearch = true;
_rootCheckpoint = _tracer->captureCheckpoint();
_tracer->restoreCheckpoint(_rootCheckpoint,this);
//Restore immediately otherwise will have an empty list at the start which we don't really want
}