forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToFunction.cpp
More file actions
1110 lines (1032 loc) · 31.4 KB
/
ToFunction.cpp
File metadata and controls
1110 lines (1032 loc) · 31.4 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* 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/>.
*
************************************************************************/
#include "ToFunction.h"
#include "FastEnvironment.h"
#include "SASActions.h"
#include "instantiation.h"
#include "SimpleEval.h"
#define SASOUTPUT if(use_sasoutput)
using std::cerr;
using namespace VAL;
using namespace Inst;
namespace SAS {
bool use_sasoutput = false;
ostream & operator<<(ostream & o,const ValHolder & vh)
{
vh.write(o);
return o;
};
ostream & operator<<(ostream & o,const ValueStructure & vs)
{
vs.write(o);
return o;
};
ostream & operator << (ostream & o,const ValueElement & ve)
{
ve.write(o);
return o;
};
ostream & operator<<(ostream & o,const VElement & v)
{
v.write(o);
return o;
};
int PlaceHolder::idGen = 0;
bool findLinkedValue(ValHolder * vh,const vector<ValueElement *> & vs)
{
PlaceHolder * ph = dynamic_cast<PlaceHolder *>(vh);
if(!ph) return false;
for(PlaceHolder::const_iterator i = ph->begin();i != ph->end();++i)
{
for(vector<ValueElement*>::const_iterator j = vs.begin();j != vs.end();++j)
{
// if(!(*j)->getPS()) continue;
// cout << "Comparing " << **i << " and " << *((*j)->getPS()) << "\n";
if(*i == (*j)->getPS()) return true;
/* PropertyState::PSIterator p = (*i)->begin();
for(;p != (*i)->end();++p)
{
PropertyState::PSIterator q = ((*j)->getPS())->begin();
for(;q != (*j)->getPS()->end();++q)
{
if((*p)->equivalent(*q)) break;
};
if(q == (*j)->getPS()->end()) break;
};
if(p == (*i)->end())
{
cout << "Got a match\n";
cout << *i << " and " << (*j)->getPS() << "\n";
return true;
};
*/
};
};
return false;
};
vector<ValueElement *> constructValue(Range & range,TIMobjectSymbol * tob)
{
vector<ValueElement*> vs;
for(Range::const_iterator i = range.begin();i != range.end();++i)
{
// Now considering a state space
vector<VElement*> vals, tmpvals;
const PropertyState * ps = 0;
for(ValuesUnion::const_iterator j = i->begin();j != i->end();++j)
{
// Now considering a state
Values::const_iterator k = j->begin();
ps = (*k)->getState();
for(;k != j->end();++k)
{
// Now considering a property
// If the object is not in this state, we have to move on to the next state.
// Object is probably in this state if it has the corresponding initial property.
// If it has that then we can extract the initial proposition and the arguments
// we need. If it doesn't have it, then we know it is not in this state.
Property * wanted = (*k)->getProp();
if(!wanted)
{
// cout << "Looking\n";
if(findLinkedValue(*k,vs))
{
// cout << "Adding a value\n";
tmpvals.push_back(new PlaceHolderElement(dynamic_cast<const PlaceHolder*>(*k)));
};
continue;
};
proposition * got = tob->find(wanted);
if(!got) break;
if(got->args->size() == 1)
{
vals.push_back(new PElement(wanted));
}
else
{
int c = 0;
for(parameter_symbol_list::iterator pr = got->args->begin();
pr != got->args->end();++pr,++c)
{
if(c != wanted->aPosn())
vals.push_back(new ObElement(TOB(*pr)));
};
};
};
if(k == j->end() && !vals.empty())
{
// Then we found we were in this state and so we can't be in any other.
break;
};
ps = 0;
};
if(vals.empty())
{
// cout << "Really should consider another possibility\n";
vals = tmpvals;
};
vs.push_back(new ValueElement(ps,vals));
};
return vs;
};
void ValueStructure::initialise()
{
SASOUTPUT {
cout << "Initialising for " << pt->getName() << " in range: " << *this << "\n\n"; };
vector<VAL::const_symbol *> cs(theTC->range(pt));
for(vector<VAL::const_symbol*>::iterator i = cs.begin();i != cs.end();++i)
{
if((rngs[TOB(*i)] = constructValue(range,TOB(*i))).size() > 0)
{
SASOUTPUT {
cout << "Constructed an initial value for " << *TOB(*i) << ": (";
for_each(rngs[TOB(*i)].begin(),rngs[TOB(*i)].end()-1,ptrwriter<ValueElement>(cout,","));
cout << **(rngs[TOB(*i)].end()-1);
cout << ")\n"; };
}
else
{
SASOUTPUT {cout << "No initial value for " << *TOB(*i) << "\n";};
};
};
SASOUTPUT {cout << "\n";};
};
void FunctionStructure::initialise()
{
for(FunctionRep::iterator i = frep.begin();i != frep.end(); ++i)
{
i->second.initialise();
};
};
set<PropertySpace *> relevant(pddl_type * tp)
{
return TA->relevant(tp);
};
void constructValues(Values & vals,const PropertyState * pst,const pddl_type * pt,PropertySpace * psp)
{
for(PropertyState::PSIterator pp = pst->begin();pp != pst->end();++pp)
{
if((*pp)->familySize() == 1)
{
vals.push_back(new NullHolder(pt,pst,psp,*pp));
}
else
{
vector<pddl_type *> tps;
vector<pddl_typed_symbol *>::const_iterator tti = (*pp)->root()->tcBegin();
for(int ti = 0;ti < (*pp)->familySize();++ti,++tti)
{
if(ti != (*pp)->aPosn())
{
// Caution: watch out for either types!
tps.push_back((*tti)->type);
};
};
vals.push_back(new TypesHolder(pt,pst,psp,*pp,tps));
};
};
};
FunctionStructure::FunctionStructure() : levels(0), othercounts(1,0)
{
for(pddl_type_list::const_iterator i = current_analysis->the_domain->types->begin();
i != current_analysis->the_domain->types->end();++i)
{
if(theTC->isLeafType(*i))
{
set<PropertySpace *> s = relevant(*i);
if(!s.empty())
{
ValueStructure vstr(*i);
for(set<PropertySpace*>::iterator j = s.begin();j != s.end();++j)
{
if((int) (theTC->range(*i).size()) > (*j)->oend() - (*j)->obegin())
{
cout << "This space contains only a subset of the objects of the type\n"
<< "Suggest creating a sub-type\n";
};
ValuesUnion vu;
for(PropertySpace::SIterator ps = (*j)->begin();ps != (*j)->end();++ps)
{
Values vs;
constructValues(vs,*ps,*i,*j);
vu.push_back(vs,*ps);
};
vstr.add(vu);
};
frep.insert(make_pair(*i,vstr));
// cout << (*i)->getName() << " -> " << vstr << "\n";
}
else
{
noStates.push_back(*i);
// cout << "No state for " << (*i)->getName() << "\n";
};
};
};
};
int WildElement::idgen = 0;
struct getMe {
Property * p;
getMe(Property * pp) : p(pp) {};
bool operator()(const pair<Property*,proposition*> & pp)
{
// cout << "Seeing " << *(pp.first) << "\n";
return p->equivalent(pp.first);
/*
* if(p==pp.first) return true;
vector<Property*> ps = pp.first->matchers();
vector<Property*>::const_iterator j = std::find(ps.begin(),ps.end(),p);
return j != ps.end();
*/
};
};
int countRelevant(PropertySpace * ps,const vector<pair<Property *,proposition*> > & conds)
{
int c = 0;
for(vector<pair<Property *,proposition*> >::const_iterator i = conds.begin();i != conds.end();++i)
{
c += (find(i->first->begin(),i->first->end(),ps) != i->first->end());
};
return c;
};
vector<ValueElement *> constructValue(const ValueStructure & vvs,
const vector<pair<Property *,proposition*> > & conds,
const vector<ValueElement*> & pvals)
{
vector<ValueElement*> vs;
for(Range::const_iterator i = vvs.getRange().begin();i != vvs.getRange().end();++i)
{
// Now considering a state space
vector<VElement*> vals;
const PropertyState * ps = 0;
const PlaceHolder * potentiallyLinked = 0;
for(ValuesUnion::const_iterator j = i->begin();j != i->end();++j)
{
// Now considering a state
Values::const_iterator k = j->begin();
ps = (*k)->getState();
// int relevantConds = countRelevant((*k)->getSpace(),conds);
int cnt = 0;
for(;k != j->end();++k)
{
// Now considering a property
// If the object is not in this state, we have to move on to the next state.
// Object is probably in this state if it has the corresponding initial property.
// However there is a possibility that the object is in a different state that shares
// this property.
//
// If it has that then we can extract the initial proposition and the arguments
// we need.
//
// If it doesn't have it, then we can suspect that it is not in this state - but it might
// just be incompletely specified. The key is that the object must be in some state and
// we know that all the propositions will have to be accounted for by the state we identify.
// So, we'll count how many propositions are accounted for and then check that they were
// all handled by the end.
Property * wanted = (*k)->getProp();
if(!wanted)
{
potentiallyLinked = dynamic_cast<const PlaceHolder*>(*k);
if(findLinkedValue(*k,vs))
{
// cout << "Adding a value\n";
++cnt;
vals.push_back(new PlaceHolderElement(potentiallyLinked));
};
continue;
};
//cout << "I want " << *wanted << "\n";
// If the property is not wanted then we are looking at a special value case.
// This is not a really bad problem for preconditions, but it is much more of
// a problem for postconditions. To test it properly we could use a trick: put the
// associated states before this one, so that by the time we get here we know the
// associated state will be already stored - that way we can find it and correctly
// set this value!
//cout << "Want " << *wanted << "\n";
vector<pair<Property*,proposition*> >::const_iterator fnd =
find_if(conds.begin(),conds.end(),getMe(wanted));
if(fnd == conds.end())
{
// cout << "Failed to find it\n";
// Then we didn't find a match. So we put in a null and move on.
if(pvals.size() > vs.size() && pvals[vs.size()]->size() > vals.size())
{
vals.push_back((*(pvals[vs.size()]))[vals.size()]->copy());
}
else
{
vals.push_back(new WildElement(*i));
};
continue;
};
proposition * got = fnd->second;
++cnt;
if(got->args->size() == 1)
{
vals.push_back(new PElement(wanted));
}
else
{
int c = 0;
for(parameter_symbol_list::iterator pr = got->args->begin();
pr != got->args->end();++pr,++c)
{
if(c != wanted->aPosn())
vals.push_back(new VarElement(*pr));
};
};
};
//cout << "Found " << cnt << " of " << relevantConds << "\n";
if(cnt)// == relevantConds)
{
// Then we found we were in this state, accounting for all propositions,
// and so we can't be in any other, unless it shares the common set of propositions. We could
// carry on and see if we can find another match.
// if(cnt < (int)conds.size()) cout << "Still some conditions to account for\n";
break;
}
else
{
for(vector<VElement*>::const_iterator xx = vals.begin();xx != vals.end();++xx)
delete (*xx);
vals.clear();
};
ps = 0;
};
if(potentiallyLinked && !ps)
{
SASOUTPUT {cout << "Potential link for a value\n";};
};
vs.push_back(new ValueElement(ps,vals));
};
return vs;
};
class ConditionGatherer : public VisitController {
private:
vector<vector<pair<Property*,proposition *> > > gathered;
vector<vector<ValueElement*> > values;
vector<proposition *> theStatics;
vector<proposition *> others;
public:
ConditionGatherer(int n) : gathered(n) {};
ConditionGatherer(const ConditionGatherer & cg) :
gathered(cg.gathered.size()), values(cg.values),
theStatics(cg.theStatics), others()
{};
virtual void visit_simple_goal(simple_goal * p)
{p->getProp()->visit(this);};
virtual void visit_qfied_goal(qfied_goal * p)
{cout << "Cannot handle quantified preconditions yet!\n"; exit(0);};
virtual void visit_conj_goal(conj_goal * p)
{p->getGoals()->visit(this);};
virtual void visit_disj_goal(disj_goal * p)
{cout << "Cannot handle disjunctive preconditions yet!\n"; exit(0);};
virtual void visit_timed_goal(timed_goal * p)
{cout << "CAUTION: Temporal goal\n";
p->getGoal()->visit(this);
cout << "Done Temporal goal\n";};
virtual void visit_imply_goal(imply_goal * p)
{cout << "Cannot handle implicative preconditions yet!\n";exit(0);};
virtual void visit_neg_goal(neg_goal * p)
{cout << "Cannot handle negative preconditions yet (although should be able to manage !=)\n";};
virtual void visit_comparison(comparison * p)
{cout << "No metric or special comparisons yet!\n"; exit(0);};
virtual void visit_proposition(proposition * p)
{
if(EPS(p->head)->isStatic())
{
theStatics.push_back(p);
return;
};
if(p->args->empty())
{
others.push_back(p);
return;
};
parameter_symbol_list::const_iterator ps = p->args->begin();
for(unsigned int i = 0;i < p->args->size();++i,++ps)
{
cout << "Handle " << *(TPS(p->head)->property(i)) << "\n";
// The following assumes that a property will be single valued for all parameters (of any types)
// that may instantiate a particular position. This looks for any one single valued property
// matching the property from the proposition (which can be for a more general type than the
// properties that were originally marked as single valued).
vector<Property*> ms;
for(holding_pred_symbol::PIt pit = EPS(p->head)->getParent()->pBegin();
pit != EPS(p->head)->getParent()->pEnd();++pit)
{
ms.push_back(TPS(*pit)->property(i));
};
for(vector<Property*>::const_iterator prp = ms.begin();prp != ms.end();++prp)
{
cout << "Considering " << **prp << "\n";
if((*prp)->isSingleValued())
{
cout << "Think I should allocate this to parameter " <<
(static_cast<const IDsymbol<var_symbol>*>(*ps)->getId()) << "\n";
gathered[(static_cast<const IDsymbol<var_symbol>*>(*ps)->getId())].
push_back(make_pair(TPS(p->head)->property(i),p));
break;
};
};
};
};
virtual void visit_simple_effect(simple_effect * p)
{
p->prop->visit(this);
};
virtual void visit_effect_lists(effect_lists * p)
{
for(pc_list<simple_effect*>::iterator i = p->add_effects.begin();i != p->add_effects.end();++i)
{
(*i)->visit(this);
};
};
void collect(const operator_ * op,FunctionStructure * fs,bool stateForAll,VMap & valueFor)
{
vector<vector<ValueElement*> > prevalues(op->parameters->size());
if(!values.empty())
{
prevalues.clear();
prevalues.swap(values);
};
SASOUTPUT {cout << (stateForAll?"Precondition":"Postcondition")
<< " states for " << *TAS(op->name) << "\n";};
int c = 0;
for(var_symbol_list::const_iterator ps = op->parameters->begin();
ps != op->parameters->end();++ps,++c)
{
if(!stateForAll && !TAS(op->name)->hasRuleFor(c)) continue;
// cout << "For parameter " << (*ps)->getName() << " of type "
// << (*ps)->type->getName() << "\n";
vector<const pddl_type *> tps,tmptps;
if(!fs->hasFluent((*ps)->type))
{
//values.push_back(vector<ValueElement*>());
// cout << "This type has no state\n";
tmptps = theTC->leaves((*ps)->type);
for(vector<const pddl_type *>::const_iterator xx = tmptps.begin();xx != tmptps.end();++xx)
{
if(fs->hasFluent(*xx))
{
// cout << "Should consider type " << (*xx)->getName() << "\n";
tps.push_back(*xx);
};
};
}
else
{
/* Generally we should ascend the entire hierarchy looking for fluents.
*/
const pddl_type * ttp = (*ps)->type;
while(ttp)
{
if(fs->hasFluent(ttp))
{
tps.push_back(ttp);
// cout << "Pushing type " << ttp->getName() << " for " << (*ps)->getName() << "\n";
};
ttp = ttp->type;
};
};
if(tps.empty())
{
values.push_back(vector<ValueElement*>());
continue;
};
for(vector<const pddl_type *>::const_iterator atp = tps.begin();atp != tps.end();++atp)
{
values.push_back(constructValue(fs->forType(*atp),
gathered[(static_cast<const IDsymbol<var_symbol>*>(*ps)->getId())],
prevalues[values.size()]));
int cc = 0;
for(vector<ValueElement*>::const_iterator ve = values[values.size()-1].begin();
ve != values[values.size()-1].end();++ve,++cc)
{
if((*ve)->size() > 0)
{
SASOUTPUT {cout << (*atp)->getName() << " [" << cc << "] " << (*ps)->getName() << " = "
<< **ve << "\n";};
valueFor[*ps].push_back(new ValueRep(*atp,cc,*ve));
};
};
};
};
SASOUTPUT {
if(stateForAll && !theStatics.empty())
{
cout << "\nStatic conditions:\n";
for(vector<proposition*>::const_iterator s = theStatics.begin();s != theStatics.end();++s)
{
cout << "(" << (*s)->head->getName();
for(parameter_symbol_list::const_iterator pm = (*s)->args->begin();
pm !=(*s)->args->end();++pm)
{
cout << " " << (*pm)->getName();
};
cout << ")\n";
};
};
if(!others.empty())
{
cout << "Other conditions:\n";
for(vector<proposition*>::const_iterator s = others.begin();s != others.end();++s)
{
cout << "(" << (*s)->head->getName();
for(parameter_symbol_list::const_iterator pm = (*s)->args->begin();
pm !=(*s)->args->end();++pm)
{
cout << " " << (*pm)->getName();
};
cout << ")\n";
};
};
};
};
SASActionTemplate * completeAction(operator_ * op,const VMap & pre,const VMap & post,
ConditionGatherer & cg) const
{
return new SASActionTemplate(op,pre,post,theStatics,others,cg.others);
};
};
void FunctionStructure::processActions()
{
for(operator_list::const_iterator i = current_analysis->the_domain->ops->begin();
i != current_analysis->the_domain->ops->end();++i)
{
SASOUTPUT {cout << "===========\n" << *TAS((*i)->name) << "\n";};
/* for(TIMactionSymbol::RCiterator r = TAS((*i)->name)->begin();r != TAS((*i)->name)->end();++r)
{
cout << **r << "\n";
};
*/
ConditionGatherer pg((*i)->parameters->size());
(*i)->precondition->visit(&pg);
VMap pres;
pg.collect(*i,this,true,pres);
ConditionGatherer eff(pg);
(*i)->effects->visit(&eff);
VMap posts;
eff.collect(*i,this,false,posts);
sasActionTemplates[*i] = pg.completeAction(*i,pres,posts,eff);
};
SASOUTPUT {cout << "\n\n\n\n";
for(SASActionTemplates::const_iterator i = sasActionTemplates.begin();
i != sasActionTemplates.end();++i)
{
cout << *(i->second) << "\n";
};};
};
void FunctionStructure::buildLayers()
{
SimpleEvaluator::setInitialState();
for(operator_list::const_iterator os = current_analysis->the_domain->ops->begin();
os != current_analysis->the_domain->ops->end();++os)
{
cout << (*os)->name->getName() << "\n";
int s = instantiatedOp::howMany();
instantiatedOp::instantiate(*os,current_analysis->the_problem,*theTC);
cout << instantiatedOp::howMany() << " so far\n";
startOp[*os] = make_pair(s,instantiatedOp::howMany());
};
for(OpStore::iterator i = instantiatedOp::opsBegin();i != instantiatedOp::opsEnd();++i)
{
unsatisfiedPrecs.push_back(sasActionTemplates[(*i)->forOp()]->preCount());
};
};
struct ValueStruct {
const pddl_type * tp;
const ValuesUnion & vu;
const PropertyState * ps;
ValueStruct(const pddl_type * p,const ValuesUnion & v,const PropertyState * s) :
tp(p), vu(v), ps(s) {};
ValueStruct & operator=(const ValueStruct & vs)
{
tp = vs.tp;
const_cast<ValuesUnion &>(vu) = const_cast<ValuesUnion &>(vs.vu);
ps = vs.ps;
return *this;
};
};
void FunctionStructure::normalise()
{
for(operator_list::const_iterator op = current_analysis->the_domain->ops->begin();
op != current_analysis->the_domain->ops->end();++op)
{
int c = 0;
for(var_symbol_list::const_iterator ps = (*op)->parameters->begin();
ps != (*op)->parameters->end();++ps,++c)
{
if(!TAS((*op)->name)->hasRuleFor(c)) continue;
// cout << "For parameter " << (*ps)->getName() << " of type "
// << (*ps)->type->getName() << "\n";
vector<ValueStruct> toReduce,toLeave;
const PropertyState * pst = 0;
for(TIMactionSymbol::RCiterator r = TAS((*op)->name)->begin();r != TAS((*op)->name)->end();++r)
{
if((*r)->paramNum()==c)
{
vector<const pddl_type *> tps = theTC->leaves((*ps)->type);
if(tps.empty()) tps.push_back((*ps)->type);
vector<const pddl_type *> rtps;
for(vector<const pddl_type *>::const_iterator xx = tps.begin();xx != tps.end();++xx)
{
if(hasFluent(*xx))
{
//cout << "Should consider type " << (*xx)->getName() << "\n";
rtps.push_back(*xx);
};
};
for(vector<const pddl_type *>::const_iterator tp = rtps.begin();tp != rtps.end();++tp)
{
for(Range::const_iterator vu = frep.find(*tp)->second.getRange().begin();
vu != frep.find(*tp)->second.getRange().end();++vu)
{
ValuesUnion::const_iterator v = (*vu).begin();
for(;v != (*vu).end();++v)
{
if(v.forState() && (*r)->applicableIn(v.forState()))
{
break;
};
};
if(v != (*vu).end())
{
if((*r)->getLHS()->size() < v.forState()->size())
{
// cout << "Rule " << **r << " applies to partial state description in "
// << *(v.forState()) << "\n";
//restructure((*r)->getLHS(),v.forState(),*tp);
toReduce.push_back(ValueStruct(*tp,*vu,v.forState()));
if(!pst)
{
pst = (*r)->getLHS();
};
}
else
{
toLeave.push_back(ValueStruct(*tp,*vu,v.forState()));
};
};
};
};
};
};
if(!toReduce.empty())
{
restructure(toReduce,toLeave,pst);
};
// The following block seems unnecessary because the action processing handles the multiple subtypes
// case (although there could still be unforeseen problems with that, so we'll keep this here for
// the moment!
if(!hasFluent((*ps)->type))
{
// cout << "This type has no state\n";
vector<const pddl_type *> tps = theTC->leaves((*ps)->type);
vector<const pddl_type *> rtps;
for(vector<const pddl_type *>::const_iterator xx = tps.begin();xx != tps.end();++xx)
{
if(hasFluent(*xx))
{
//cout << "Should consider type " << (*xx)->getName() << "\n";
rtps.push_back(*xx);
};
};
if(!rtps.empty())
{
restructure(*op,*ps,rtps);
};
};
};
};
};
bool equivalent(const Values & v1,const Values & v2)
{
return v1.equivalent(v2);
};
struct alreadyIn {
const ValuesUnion & vals;
alreadyIn(const ValuesUnion & v) : vals(v) {};
bool operator()(const pair<const PropertyState *,Values> & v) const
{
for(ValuesUnion::const_iterator i = vals.begin();i != vals.end();++i)
{
if(v.second.equivalent(*i))
{
return true;
};
}
return false;
};
};
ValuesUnion::ValuesUnion(const ValuesUnion & v1,const ValuesUnion & v2) : valuesUnion(v1.valuesUnion)
{
remove_copy_if(v2.valuesUnion.begin(),v2.valuesUnion.end(),back_inserter(valuesUnion),
alreadyIn(v1));
};
bool ValuesUnion::intersectsWith(const ValuesUnion & v) const
{
for(ValuesUnion::const_iterator i = begin();i != end();++i)
{
for(ValuesUnion::const_iterator j = v.begin();j != v.end();++j)
{
if(equivalent(*i,*j)) return true;
};
};
return false;
};
void ValueStructure::liftFrom(ValueStructure & vs1,ValueStructure & vs2)
{
Range r1,r2;
for(Range::const_iterator i = vs1.getRange().begin();i != vs1.getRange().end();++i)
{
Range::const_iterator j = vs2.getRange().begin();
for(;j != vs2.getRange().end();++j)
{
if(i->intersectsWith(*j))
{
range.push_back(ValuesUnion(*i,*j));
break;
}
else
{
r2.push_back(*j);
};
};
if(j == vs2.getRange().end())
{
r1.push_back(*i);
};
vs2.range = r2;
};
vs1.range = r1;
};
void FunctionStructure::restructure(const operator_ * op,const var_symbol * prm,
const vector<const pddl_type *> & rtps)
{
SASOUTPUT {cout << "Looking for shared state structure in types: ";
for(vector<const pddl_type *>::const_iterator i = rtps.begin(); i != rtps.end(); ++i)
{
cout << (*i)->getName() << " ";
};
cout << "\n";};
if(rtps.size() > 2)
{
cerr << "Not sure how to handle so many sub-types for this abstraction process!\n"
<< "Review: FunctionStructure::restructure in ToFunction.cpp\n";
exit(0);
};
ValueStructure & vs1 = frep.find(rtps[0])->second;
ValueStructure & vs2 = frep.find(rtps[1])->second;
ValueStructure newvs(prm->type);
newvs.liftFrom(vs1,vs2);
frep.insert(make_pair(prm->type,newvs));
if(vs1.getRange().size()==0)
{
frep.erase(rtps[0]);
};
if(vs2.getRange().size()==0)
{
frep.erase(rtps[1]);
};
};
void FunctionStructure::restructure(const vector<ValueStruct> & toReduce,const vector<ValueStruct> & toLeave,
const PropertyState * ps)
{
/*
for(vector<ValueStruct>::const_iterator i = toLeave.begin();i != toLeave.end();++i)
{
cout << "Leave " << *(i->ps) << " for " << i->tp->getName() << "\n";
};
for(vector<ValueStruct>::const_iterator i = toReduce.begin();i != toReduce.end();++i)
{
cout << "Reduce " << *(i->ps) << " for " << i->tp->getName() << "\n";
};
*/
vector<Property*> toSeparate;
if(!toLeave.empty())
{
for(ValuesUnion::const_iterator i = toLeave[0].vu.begin();i != toLeave[0].vu.end();++i)
{
copy(i.forState()->begin(),i.forState()->end(),back_inserter(toSeparate));
};
// cout << "Closure of properties: ";
// for_each(toSeparate.begin(),toSeparate.end(),ptrwriter<Property>(cout," "));
// cout << "\n";
}
else
{
// cout << "Hmmm...we need to find the closure some other way!\n";
set<Property*> props;
copy(ps->begin(),ps->end(),inserter(props,props.begin()));
TA->close(props,toReduce[0].tp);
copy(props.begin(),props.end(),back_inserter(toSeparate));
// cout << "Have found: ";
// for_each(toSeparate.begin(),toSeparate.end(),ptrwriter<Property>(cout," "));
// cout << "\n";
};
for(vector<ValueStruct>::const_iterator i = toReduce.begin();i != toReduce.end();++i)
{
ValuesUnion newvu1,newvu2;
for(ValuesUnion::const_iterator j = i->vu.begin();j != i->vu.end();++j)
{
Values newvl1,newvl2;
vector<Property *> prps1,prps2;
// cout << "Want to intersect with " << *(j.forState()) << "\n";
for(PropertyState::PSIterator p = j.forState()->begin();p != j.forState()->end();++p)
{
// The j state is the one we want to split. The q values are the properties we are trying
// to separate from the j state.
vector<Property*>::const_iterator q = toSeparate.begin();
for(;q != toSeparate.end();++q)
{
//cout << "Compare " << **p << " and " << **q << "\n";
if((*p)->equivalent(*q))
{
// cout << **p << " and " << **q << " match\n";
prps2.push_back(*p);
break;
};
};
if(q == toSeparate.end())
{
prps1.push_back(*p);
};
};
const PropertyState * newpst;
const PropertyState * reducedPSt;
PropertySpace * psp = (*(j->begin()))->getSpace();
if(!prps2.empty())
{
newpst = PropertyState::getPS(TA,i->tp,prps2.begin(),prps2.end());
reducedPSt = PropertyState::getPS(TA,i->tp,prps1.begin(),prps1.end());
// cout << "Split states into: " << *newpst << " and " << *reducedPSt << "\n";
constructValues(newvl1,newpst,i->tp,psp);
newvu1.push_back(newvl1,newpst);
constructValues(newvl2,reducedPSt,i->tp,psp);
newvu2.push_back(newvl2,reducedPSt);
}
else
{
reducedPSt = j.forState();
newpst = j.forState();
// cout << "State " << *reducedPSt << " not split\n";
newvu2.push_back(*j,j.forState());
if(!newvu1.hasPlaceHolder(j.forState()))
{
newvl1.push_back(new PlaceHolder(i->tp,j.forState(),psp));
newvu1.push_back(newvl1,0);
};
};
};
// cout << "Planning to replace original ValuesUnion " << i->vu << " with " << newvu1 << " and "
// << newvu2 << "\n";
frep.find(i->tp)->second.update(i->vu,newvu2,newvu1);
};
};
void Range::update(const ValuesUnion & oldvu,const ValuesUnion & newvu1,const ValuesUnion & newvu2)
{
for(unsigned int i = 0;i < size();++i)
{
if(&(range[i]) == &oldvu)
{
range[i] = newvu1;
range.push_back(newvu2);
return;
};
};
};
void ValueStructure::update(const ValuesUnion & oldvu,const ValuesUnion & newvu1,const ValuesUnion & newvu2)
{
range.update(oldvu,newvu1,newvu2);
};
bool ValuesUnion::hasPlaceHolder(const PropertyState * ps)
{
for(vector<pair<const PropertyState *,Values> >::iterator i = valuesUnion.begin();i != valuesUnion.end();++i)
{
if(dynamic_cast<PlaceHolder*>((i->second)[0]))
{
(i->second)[0]->add(ps);
return true;
};