forked from hlsyounes/vhpop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plans.h
369 lines (291 loc) · 12.3 KB
/
plans.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// 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
//
// Partial plans, and their components.
#ifndef PLANS_H
#define PLANS_H
#include "chain.h"
#include "flaws.h"
#include "orderings.h"
struct Parameters;
struct BindingList;
struct Literal;
struct Atom;
struct Negation;
struct Effect;
struct EffectList;
struct Action;
struct Problem;
struct Bindings;
struct ActionEffectMap;
struct FlawSelectionOrder;
/* ====================================================================== */
/* Link */
/*
* Causal link.
*/
struct Link {
/* Constructs a causal link. */
Link(size_t from_id, StepTime effect_time, const OpenCondition& open_cond);
/* Constructs a causal link. */
Link(const Link& l);
/* Deletes this causal link. */
~Link();
/* Returns the id of step that link goes from. */
size_t from_id() const { return from_id_; }
/* Returns the time of effect satisfying link. */
StepTime effect_time() const { return effect_time_; }
/* Returns the id of step that link goes to. */
size_t to_id() const { return to_id_; }
/* Returns the condition satisfied by link. */
const Literal& condition() const { return *condition_; }
/* Returns the time of the condition satisfied by this link. */
FormulaTime condition_time() const { return condition_time_; }
private:
/* Id of step that link goes from. */
size_t from_id_;
/* Time of effect satisfying link. */
StepTime effect_time_;
/* Id of step that link goes to. */
size_t to_id_;
/* Condition satisfied by link. */
const Literal* condition_;
/* Time of condition satisfied by link. */
FormulaTime condition_time_;
};
/* Equality operator for links. */
inline bool operator==(const Link& l1, const Link& l2) {
return &l1 == &l2;
}
/* ====================================================================== */
/* Step */
/*
* Plan step.
*/
struct Step {
/* Constructs a step instantiated from an action. */
Step(size_t id, const Action& action)
: id_(id), action_(&action) {}
/* Constructs a step. */
Step(const Step& s)
: id_(s.id_), action_(s.action_) {}
/* Returns the step id. */
size_t id() const { return id_; }
/* Returns the action that this step is instantiated from. */
const Action& action() const { return *action_; }
private:
/* Step id. */
size_t id_;
/* Action that this step is instantiated from. */
const Action* action_;
};
/* ====================================================================== */
/* Plan */
/*
* Plan.
*/
struct Plan {
/* Id of goal step. */
static const size_t GOAL_ID;
/* Returns plan for given problem. */
static const Plan* plan(const Problem& problem, const Parameters& params,
bool last_problem);
/* Cleans up after planning. */
static void cleanup();
/* Deletes this plan. */
~Plan();
/* Returns the steps of this plan. */
const Chain<Step>* steps() const { return steps_; }
/* Returns the number of unique steps in this plan. */
size_t num_steps() const { return num_steps_; }
/* Returns the links of this plan. */
const Chain<Link>* links() const { return links_; }
/* Returns the number of links in this plan. */
size_t num_links() const { return num_links_; }
/* Returns the ordering constraints of this plan. */
const Orderings& orderings() const { return *orderings_; }
/* Returns the bindings of this plan. */
const Bindings* bindings() const;
/* Returns the potentially threatened links of this plan. */
const Chain<Unsafe>* unsafes() const { return unsafes_; }
/* Returns the number of potentially threatened links in this plan. */
size_t num_unsafes() const { return num_unsafes_; }
/* Returns the open conditions of this plan. */
const Chain<OpenCondition>* open_conds() const { return open_conds_; }
/* Returns the number of open conditions in this plan. */
size_t num_open_conds() const { return num_open_conds_; }
/* Returns the mutex threats of this plan. */
const Chain<MutexThreat>* mutex_threats() const { return mutex_threats_; }
/* Checks if this plan is complete. */
bool complete() const;
/* Returns the primary rank of this plan, where a lower rank
signifies a better plan. */
float primary_rank() const;
/* Returns the serial number of this plan. */
size_t serial_no() const;
#ifdef DEBUG
/* Returns the depth of this plan. */
size_t depth() const { return depth_; }
#endif
/* Counts the number of refinements for the given threat, and returns
true iff the number of refinements does not exceed the given
limit. */
bool unsafe_refinements(int& refinements, int& separable,
int& promotable, int& demotable,
const Unsafe& unsafe, int limit) const;
/* Checks if the given threat is separable. */
int separable(const Unsafe& unsafe) const;
/* Checks if the given open condition is threatened. */
bool unsafe_open_condition(const OpenCondition& open_cond) const;
/* Counts the number of refinements for the given open condition, and
returns true iff the number of refinements does not exceed the
given limit. */
bool open_cond_refinements(int& refinements, int& addable, int& reusable,
const OpenCondition& open_cond, int limit) const;
/* Counts the number of add-step refinements for the given literal
open condition, and returns true iff the number of refinements
does not exceed the given limit. */
bool addable_steps(int& refinements, const Literal& literal,
const OpenCondition& open_cond, int limit) const;
/* Counts the number of reuse-step refinements for the given literal
open condition, and returns true iff the number of refinements
does not exceed the given limit. */
bool reusable_steps(int& refinements, const Literal& literal,
const OpenCondition& open_cond, int limit) const;
private:
/* List of plans. */
struct PlanList : public std::vector<const Plan*> {
};
/* Chain of steps. */
const Chain<Step>* steps_;
/* Number of unique steps in plan. */
size_t num_steps_;
/* Chain of causal links. */
const Chain<Link>* links_;
/* Number of causal links. */
size_t num_links_;
/* Ordering constraints of this plan. */
const Orderings* orderings_;
/* Binding constraints of this plan. */
const Bindings* bindings_;
/* Chain of potentially threatened links. */
const Chain<Unsafe>* unsafes_;
/* Number of potentially threatened links. */
size_t num_unsafes_;
/* Chain of open conditions. */
const Chain<OpenCondition>* open_conds_;
/* Number of open conditions. */
const size_t num_open_conds_;
/* Chain of mutex threats. */
const Chain<MutexThreat>* mutex_threats_;
/* Rank of this plan. */
mutable std::vector<float> rank_;
/* Plan id (serial number). */
mutable size_t id_;
#ifdef DEBUG
/* Depth of this plan in the search space. */
size_t depth_;
#endif
/* Returns the initial plan representing the given problem, or NULL
if goals of problem are inconsistent. */
static const Plan* make_initial_plan(const Problem& problem);
/* Constructs a plan. */
Plan(const Chain<Step>* steps, size_t num_steps,
const Chain<Link>* links, size_t num_links,
const Orderings& orderings, const Bindings& bindings,
const Chain<Unsafe>* unsafes, size_t num_unsafes,
const Chain<OpenCondition>* open_conds, size_t num_open_conds,
const Chain<MutexThreat>* mutex_threats, const Plan* parent);
/* Returns the next flaw to work on. */
const Flaw& get_flaw(const FlawSelectionOrder& flaw_order) const;
/* Returns the refinements for the next flaw to work on. */
void refinements(PlanList& plans,
const FlawSelectionOrder& flaw_order) const;
/* Handles an unsafe link. */
void handle_unsafe(PlanList& plans, const Unsafe& unsafe) const;
/* Handles an unsafe link through separation. */
int separate(PlanList& plans, const Unsafe& unsafe,
const BindingList& unifier, bool test_only = false) const;
/* Handles an unsafe link through demotion. */
int demote(PlanList& plans, const Unsafe& unsafe,
bool test_only = false) const;
/* Handles an unsafe link through promotion. */
int promote(PlanList& plans, const Unsafe& unsasfe,
bool test_only = false) const;
/* Adds a plan to the given plan list with an ordering added. */
void new_ordering(PlanList& plans, size_t before_id, StepTime t1,
size_t after_id, StepTime t2,
const Unsafe& unsafe) const;
/* Handles a mutex threat. */
void handle_mutex_threat(PlanList& plans,
const MutexThreat& mutex_threat) const;
/* Handles a mutex threat through separation. */
void separate(PlanList& plans, const MutexThreat& mutex_threat,
const BindingList& unifier) const;
/* Handles a mutex threat through demotion. */
void demote(PlanList& plans, const MutexThreat& mutex_threat) const;
/* Handles a mutex threat through promotion. */
void promote(PlanList& plans, const MutexThreat& mutex_threat) const;
/* Adds a plan to the given plan list with an ordering added. */
void new_ordering(PlanList& plans, size_t before_id, StepTime t1,
size_t after_id, StepTime t2,
const MutexThreat& mutex_threat) const;
/* Handles an open condition. */
void handle_open_condition(PlanList& plans,
const OpenCondition& open_cond) const;
/* Handles a disjunctive open condition. */
int handle_disjunction(PlanList& plans, const Disjunction& disj,
const OpenCondition& open_cond,
bool test_only = false) const;
/* Handles an inequality open condition. */
int handle_inequality(PlanList& plans, const Inequality& neq,
const OpenCondition& open_cond,
bool test_only = false) const;
/* Handles a literal open condition by adding a new step. */
void add_step(PlanList& plans, const Literal& literal,
const OpenCondition& open_cond,
const ActionEffectMap& achievers) const;
/* Handles a literal open condition by reusing an existing step. */
void reuse_step(PlanList& plans, const Literal& literal,
const OpenCondition& open_cond,
const ActionEffectMap& achievers) const;
/* Adds plans to the given plan list with a link from the given step
to the given open condition added. */
int new_link(PlanList& plans, const Step& step, const Effect& effect,
const Literal& literal, const OpenCondition& open_cond,
bool test_only = false) const;
/* Adds plans to the given plan list with a link from the given step
to the given open condition added using the closed world
assumption. */
int new_cw_link(PlanList& plans, const EffectList& effects,
const Negation& negation, const OpenCondition& open_cond,
bool test_only = false) const;
/* Returns a plan with a link added from the given effect to the
given open condition. */
int make_link(PlanList& plans, const Step& step, const Effect& effect,
const Literal& literal, const OpenCondition& open_cond,
const BindingList& unifier, bool test_only = false) const;
friend bool operator<(const Plan& p1, const Plan& p2);
friend std::ostream& operator<<(std::ostream& os, const Plan& p);
};
/* Less than operator for plans. */
bool operator<(const Plan& p1, const Plan& p2);
/* Output operator for plans. */
std::ostream& operator<<(std::ostream& os, const Plan& p);
#endif /* PLANS_H */