forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
485 lines (394 loc) · 10.9 KB
/
State.cpp
File metadata and controls
485 lines (394 loc) · 10.9 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/************************************************************************
* Copyright 2008, Strathclyde Planning Group,
* Department of Computer and Information Sciences,
* University of Strathclyde, Glasgow, UK
* http://planning.cis.strath.ac.uk/
*
* Maria Fox, Richard Howey and Derek Long - VAL
* Stephen Cresswell - PDDL Parser
*
* This file is part of VAL, the PDDL validator.
*
* VAL 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.
*
* VAL 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 VAL. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
/*-----------------------------------------------------------------------------
VAL - The Automatic Plan Validator for PDDL+
$Date: 2009-02-05 10:50:23 $
$Revision: 1.2 $
Maria Fox, Richard Howey and Derek Long - PDDL+ and VAL
Stephen Cresswell - PDDL Parser
maria.fox@cis.strath.ac.uk
derek.long@cis.strath.ac.uk
stephen.cresswell@cis.strath.ac.uk
richard.howey@cis.strath.ac.uk
By releasing this code we imply no warranty as to its reliability
and its use is entirely at your own risk.
Strathclyde Planning Group
http://planning.cis.strath.ac.uk
----------------------------------------------------------------------------*/
#include "State.h"
#include "Plan.h"
#include "main.h"
#include "Validator.h"
#include "Proposition.h"
#include "Exceptions.h"
#include "LaTeXSupport.h"
#include "RobustAnalyse.h"
#include <sstream>
using std::stringstream;
using std::cerr;
//#define list std::list
//#define map std::map
namespace VAL {
vector<StateObserver *> State::sos;
void State::setNew(const effect_lists * is)
{
logState.clear();
feValue.clear();
changedPNEs.clear();
for(list<simple_effect*>::const_iterator i = is->add_effects.begin();
i != is->add_effects.end();++i)
{
logState[vld->pf.buildLiteral(*i)] = true;
};
for(pc_list<assignment*>::const_iterator i1 = is->assign_effects.begin();
i1 != is->assign_effects.end();++i1)
{
const FuncExp * fe = vld->fef.buildFuncExp((*i1)->getFTerm());
// DPL 2/2/06: Need to remember which PNEs changed in order to trigger
// processes properly in the initial state.
changedPNEs.insert(fe);
FEScalar feNewValue = dynamic_cast<const num_expression *>((*i1)->getExpr())->double_value();
feValue[fe] = feNewValue;
//setup initial value for LaTeX graph
if(LaTeX)
{
FEGraph * feg = getValidator()->getGraph(fe);
//setup initial value if nec
if( (feg->initialTime == -1) )
{
feg->initialTime = time;
feg->initialValue = feNewValue;
};
};
};
};
State::State(Validator * const v,const effect_lists* is) :
tolerance(v->getTolerance()), vld(v), time(0.0)
{
setNew(is);
};
bool State::evaluate(const SimpleProposition * p) const
{
LogicalState::const_iterator i = logState.find(p);
if(i != logState.end())
{
return i->second;
}
else
{
return false;
};
};
FEScalar State::evaluateFE(const FuncExp * fe) const
{
NumericalState::const_iterator i = feValue.find(fe);
if(i!=feValue.end())
{
return i->second;
}
else
{
/* cerr << "Attempt to access undefined expression: " << *fe << "\n";
if(LaTeX)
{
*report << "\\error{Attempt to access undefined expression: " << *fe << "}\n";
};
*/
// Throw? Yes !
//exit(0);
BadAccessError bae;
throw bae;
};
};
FEScalar State::evaluate(const expression * e,const Environment & bs) const
{
if(dynamic_cast<const div_expression *>(e))
{
return evaluate(dynamic_cast<const div_expression*>(e)->getLHS(),bs) /
evaluate(dynamic_cast<const div_expression*>(e)->getRHS(),bs);
};
if(dynamic_cast<const minus_expression *>(e))
{
return evaluate(dynamic_cast<const minus_expression*>(e)->getLHS(),bs) -
evaluate(dynamic_cast<const minus_expression*>(e)->getRHS(),bs);
};
if(dynamic_cast<const mul_expression *>(e))
{
return evaluate(dynamic_cast<const mul_expression*>(e)->getLHS(),bs) *
evaluate(dynamic_cast<const mul_expression*>(e)->getRHS(),bs);
};
if(dynamic_cast<const plus_expression *>(e))
{
return evaluate(dynamic_cast<const plus_expression*>(e)->getLHS(),bs) +
evaluate(dynamic_cast<const plus_expression*>(e)->getRHS(),bs);
};
if(dynamic_cast<const num_expression*>(e))
{
return dynamic_cast<const num_expression*>(e)->double_value();
};
if(dynamic_cast<const uminus_expression*>(e))
{
return -(evaluate(dynamic_cast<const uminus_expression*>(e)->getExpr(),bs));
};
if(dynamic_cast<const func_term*>(e))
{
const FuncExp * fexp = vld->fef.buildFuncExp(dynamic_cast<const func_term*>(e),bs);
return fexp->evaluate(this);
//if(feValue.find(fexp) != feValue.end())
//return feValue.find(fexp)->second;
/*
if(LaTeX)
{
*report << "\\error{Attempt to access undefined expression: " << *fexp << "}\n";
}
else if(Verbose) cout << "Attempt to inspect undefined value: " << *fexp << "\n";
*/
//BadAccessError bae;
//throw bae;
};
if(const special_val_expr * sp = dynamic_cast<const special_val_expr *>(e))
{
if(sp->getKind() == E_TOTAL_TIME)
{
if(vld->durativePlan()) return time;
return vld->simpleLength();
};
if(sp->getKind() == E_DURATION_VAR)
{
return bs.duration;
};
if(sp->getKind() == E_HASHT)
{
if(LaTeX)
*report << "The use of \\#t is not valid in this context!\n";
else if(Verbose)
cout << "The use of #t is not valid in this context!\n";
SyntaxTooComplex stc;
throw stc;
}
};
if(const violation_term * vt = dynamic_cast<const violation_term *>(e))
{
return vld->violationsFor(vt->getName());
};
UnrecognisedCondition uc;
throw uc;
};
bool
State::progress(const Happening * h)
{
DerivedGoal::resetLists(this);
resetChanged();
if(TestingPNERobustness) JudderPNEs = true;
bool canHappen = h->canHappen(this);
if(canHappen || ContinueAnyway)
{
time = h->getTime();
if(TestingPNERobustness) JudderPNEs = false;
return h->applyTo(this) && canHappen;
}
else
{
return false;
};
};
bool
State::progressCtsEvent(const Happening * h)
{
DerivedGoal::resetLists(this);
resetChanged();
if(TestingPNERobustness) JudderPNEs = true;
bool canHappen = h->canHappen(this);
if(canHappen || ContinueAnyway)
{
time = h->getTime();
if(TestingPNERobustness) JudderPNEs = false;
return h->applyTo(this) && canHappen;
}
else
{
return false;
};
};
void
State::add(const SimpleProposition * a)
{
if(LaTeX)
*report << " \\> \\adding{"<<*a<<"}\\\\\n";
else if(Verbose)
cout << "Adding " << *a << "\n";
logState[a] = true;
};
void
State::del(const SimpleProposition * a)
{
if(LaTeX)
*report << " \\> \\deleting{"<<*a<<"}\\\\\n";
else if(Verbose)
cout << "Deleting " << *a << "\n";
logState[a] = false;
};
void
State::addChange(const SimpleProposition * a)
{
if(LaTeX)
*report << " \\> \\adding{"<<*a<<"}\\\\\n";
else if(Verbose)
cout << "Adding " << *a << "\n";
if(!(logState[a])) changedLiterals.insert(a);
logState[a] = true;
};
void
State::delChange(const SimpleProposition * a)
{
if(LaTeX)
*report << " \\> \\deleting{"<<*a<<"}\\\\\n";
else if(Verbose)
cout << "Deleting " << *a << "\n";
if(logState[a]) changedLiterals.insert(a);
logState[a] = false;
};
void
State::updateChange(const FuncExp * fe,assign_op aop,FEScalar value)
{
FEScalar initialValue = feValue[fe];
update(fe,aop,value);
if(feValue[fe] != initialValue) changedPNEs.insert(fe);
};
State & State::operator=(const State & s)
{
logState = s.logState;
feValue = s.feValue;
time = s.time;
changedLiterals = s.changedLiterals;
changedPNEs = s.changedPNEs;
return *this;
};
void
State::update(const FuncExp * fe,assign_op aop,FEScalar value)
{
bool setInitialValue = false;
FEGraph * feg = 0;
if(LaTeX)
{
feg = getValidator()->getGraph(fe);
//setup initial value if nec
if( (feg->initialTime == -1) )
{
map<const FuncExp*,FEScalar>::const_iterator i = feValue.find(fe);
if(i != feValue.end())
{
feg->initialTime = 0;
feg->initialValue = fe->evaluate(this);
}
else
{
feg->initialTime = time;
setInitialValue = true;
};
};
};
if(Verbose && !LaTeX) *report << "Updating " << *fe << " (" << feValue[fe] << ") by " << value << " ";
FEScalar feValueInt = feValue[fe];
switch(aop)
{
case E_ASSIGN:
if(LaTeX)
{
*report << " \\> \\assignment{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "assignment\n";
feValue[fe] = value;
break;
case E_ASSIGN_CTS:
if(LaTeX)
{
*report << " \\> \\assignmentcts{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "assignment\n";
feValue[fe] = value;
return;
case E_INCREASE:
if(LaTeX)
{
*report << " \\> \\increase{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "increase\n";
feValue[fe] += value;
break;
case E_DECREASE:
if(LaTeX)
{
*report << " \\> \\decrease{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "decrease\n";
feValue[fe] -= value;
break;
case E_SCALE_UP:
if(LaTeX)
{
*report << " \\> \\scaleup{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "scale up\n";
feValue[fe] *= value;
break;
case E_SCALE_DOWN:
if(LaTeX)
{
*report << " \\> \\scaledown{"<<*fe<<"}{"<<feValue[fe]<<"}{"<<value<<"}\\\\\n";
}
else if(Verbose) cout << "scale down\n";
feValue[fe] /= value;
break;
default:
return;
};
//handle discontinuities in graphs
if(LaTeX)
{
if(setInitialValue)
{
feValueInt = feValue[fe];
feg->initialValue = feValueInt;
};
if( (feValueInt != feValue[fe]) || setInitialValue )
{
//check value is already defined, may be communitive updates at the same time
map<double,pair<double,double> >::iterator j = feg->discons.find(time);
if(j == feg->discons.end())
{
feg->discons[time] = make_pair(feValueInt,feValue[fe]);
feg->happenings.insert(time);
}
else
{
j->second.second = feValue[fe];
};
};
};
return;
};
};