forked from hlsyounes/vhpop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
effects.cc
188 lines (169 loc) · 5.8 KB
/
effects.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (C) 2002--2005 Carnegie Mellon University
// Copyright (C) 2019 Google Inc
//
// This file is part of VHPOP.
//
// VHPOP is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// VHPOP is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VHPOP; if not, write to the Free Software Foundation,
// Inc., #59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "effects.h"
#include <algorithm>
#include <stack>
#include "bindings.h"
#include "formulas.h"
#include "problems.h"
#include "types.h"
/* ====================================================================== */
/* Effect */
/* Constructs an effect. */
Effect::Effect(const Literal& literal, EffectTime when)
: condition_(&Formula::TRUE), link_condition_(&Formula::TRUE),
literal_(&literal), when_(when) {
Formula::register_use(condition_);
Formula::register_use(link_condition_);
Formula::register_use(literal_);
}
/* Deletes this effect. */
Effect::~Effect() {
Formula::unregister_use(condition_);
Formula::unregister_use(link_condition_);
Formula::unregister_use(literal_);
}
/* Adds a universally quantified variable to this effect. */
void Effect::add_parameter(Variable parameter) {
parameters_.push_back(parameter);
}
/* Sets the condition of this effect. */
void Effect::set_condition(const Formula& condition) {
if (condition_ != &condition) {
Formula::register_use(&condition);
Formula::unregister_use(condition_);
condition_ = &condition;
}
}
/* Sets the link condition of this effect. */
void Effect::set_link_condition(const Formula& link_condition) const {
if (link_condition_ != &link_condition) {
Formula::register_use(&link_condition);
Formula::unregister_use(link_condition_);
link_condition_ = &link_condition;
}
}
/* Tests if this effect quantifies the given variable. */
bool Effect::quantifies(Variable variable) const {
return (find(parameters_.begin(), parameters_.end(), variable)
!= parameters_.end());
}
/* Fills the provided list with instantiations of this effect. */
void Effect::instantiations(EffectList& effects, size_t& useful,
const std::map<Variable, Term>& subst,
const Problem& problem) const {
size_t n = arity();
if (n == 0) {
const Formula& inst_cond = condition().instantiation(subst, problem);
if (!inst_cond.contradiction()) {
const Effect* inst_effect = instantiation(subst, problem, inst_cond);
effects.push_back(inst_effect);
if (!inst_effect->link_condition().contradiction()) {
useful++;
}
}
} else {
std::map<Variable, Term> args(subst);
std::vector<const std::vector<Object>*> arguments(n);
std::vector<std::vector<Object>::const_iterator> next_arg;
for (size_t i = 0; i < n; i++) {
const Type& t = TermTable::type(parameter(i));
arguments[i] = &problem.terms().compatible_objects(t);
if (arguments[i]->empty()) {
return;
}
next_arg.push_back(arguments[i]->begin());
}
std::stack<const Formula*> conds;
conds.push(&condition().instantiation(args, problem));
Formula::register_use(conds.top());
for (size_t i = 0; i < n; ) {
args.insert(std::make_pair(parameter(i), *next_arg[i]));
std::map<Variable, Term> pargs;
pargs.insert(std::make_pair(parameter(i), *next_arg[i]));
const Formula& inst_cond = conds.top()->instantiation(pargs, problem);
conds.push(&inst_cond);
Formula::register_use(conds.top());
if (i + 1 == n || inst_cond.contradiction()) {
if (!inst_cond.contradiction()) {
const Effect* inst_effect = instantiation(args, problem, inst_cond);
effects.push_back(inst_effect);
if (!inst_effect->link_condition().contradiction()) {
useful++;
}
}
for (int j = i; j >= 0; j--) {
Formula::unregister_use(conds.top());
conds.pop();
args.erase(parameter(j));
next_arg[j]++;
if (next_arg[j] == arguments[j]->end()) {
if (j == 0) {
i = n;
break;
} else {
next_arg[j] = arguments[j]->begin();
}
} else {
i = j;
break;
}
}
} else {
i++;
}
}
while (!conds.empty()) {
Formula::unregister_use(conds.top());
conds.pop();
}
}
}
/* Returns an instantiation of this effect. */
const Effect* Effect::instantiation(const std::map<Variable, Term>& args,
const Problem& problem,
const Formula& condition) const {
Effect* inst_eff = new Effect(literal().substitution(args), when());
inst_eff->set_condition(condition);
inst_eff->set_link_condition(link_condition().instantiation(args, problem));
return inst_eff;
}
/* Prints this effect on the given stream. */
void Effect::print(std::ostream& os) const {
os << '(';
for (std::vector<Variable>::const_iterator vi = parameters_.begin();
vi != parameters_.end(); vi++) {
os << *vi << ' ';
}
switch (when()) {
case Effect::AT_START:
os << "at start ";
break;
case Effect::AT_END:
os << "at end ";
break;
}
os << '[';
condition().print(os, 0, Bindings::EMPTY);
os << ',';
link_condition().print(os, 0, Bindings::EMPTY);
os << "->";
literal().print(os, 0, Bindings::EMPTY);
os << ']' << ')';
}