Repository navigation
Expand file tree
/
Copy pathPlan.cpp
More file actions
2336 lines (1803 loc) · 63.4 KB
/
Copy pathPlan.cpp
File metadata and controls
2336 lines (1803 loc) · 63.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/>.
*
************************************************************************/
/*-----------------------------------------------------------------------------
VAL - The Automatic Plan Validator for PDDL+
$Date: 2009-02-05 10:50:21 $
$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 "Action.h"
#include "Validator.h"
#include "main.h"
#include "Polynomial.h"
#include "Exceptions.h"
#include "ptree.h"
#include "RobustAnalyse.h"
using std::make_pair;
using std::ptr_fun;
using std::find;
using std::remove;
using std::sort;
using std::find_if;
using std::copy;
using std::for_each;
using std::bind1st;
using std::mem_fun;
//#define vector std::vector
//#define map std::map
//#define list std::list
namespace VAL {
Happening::Happening(Validator * v,const vector<pair<double,Action*> > & as,double timeEndPlan) : vld(v),
time(0.0), actions(), eventHappening(false), realHappening(false), afterPlan(false)
{
time = as.begin()->first;
afterPlan = time > timeEndPlan;
std::transform(as.begin(),as.end(),std::back_inserter(actions),select2nd<pair<double,Action*> >());
realHappening = (find_if(actions.begin(),actions.end(),mem_fun(&Action::isRegAction)) != actions.end());
};
Happening::Happening(Validator * v,double timeToExecute,const vector<pair<double,Action*> > & as) :
vld(v), time(v->getState().getTime()+timeToExecute),actions(),
eventHappening(false), realHappening(false), afterPlan(false)
{
std::transform(as.begin(),as.end(),std::back_inserter(actions),select2nd<pair<double,Action*> >());
realHappening = (find_if(actions.begin(),actions.end(),mem_fun(&Action::isRegAction)) != actions.end());
};
//for creating event happenings
Happening::Happening(Validator * v, vector<const Action*> acts,bool eve): vld(v),
time(v->getState().getTime()), actions(acts), eventHappening(eve), realHappening(false), afterPlan(false)
{
//afterPlan = time > timeEndPlan; may need later for processes, then events may be after the main plan
//std::transform(acts.begin(),acts.end(),std::back_inserter(actions));
};
//for creating event happenings
Happening::Happening(Validator * v, vector<const Action*> acts,double t,bool eve): vld(v),
time(t), actions(acts), eventHappening(eve), realHappening(false), afterPlan(false)
{
//afterPlan = time > timeEndPlan; may need later for processes, then events may be after the main plan
//std::transform(acts.begin(),acts.end(),std::back_inserter(actions));
};
bool
Happening::canHappen(const State * s) const
{
if(!eventHappening)
{
if(LaTeX)
*report << "\\atime{"<<time<<"} \\> \\checkhappening";
else if(Verbose) cout << "Checking next happening (time " << time << ")\n";
}
else
{
if(LaTeX)
*report << "\\atime{"<<time<<"} \\> \\eventtriggered \\\\\n";
else if(Verbose) cout << "EVENT triggered at (time " << time << ")\n";
if(Verbose)
{
for(vector<const Action*>::const_iterator act = actions.begin();act != actions.end();++act)
{
(*act)->displayEventInfomation();
};
};
return true; //no need to check event preconditions, these are all satisfied! (if not then there is an error calculating the events!)
};
bool isOK = true;
for(vector<const Action*>::const_iterator a = actions.begin();a != actions.end();++a)
{
if(!(*a)->confirmPrecondition(s))
{
if(LaTeX)
{
*report << "Plan failed because of unsatisfied precondition in:\\\\\n \\> " << (*a)<< "\\\\\n";
}
else if(Verbose) cout << "Plan failed because of unsatisfied precondition in:\n" << (*a)<< "\n";
if(Verbose || ErrorReport) (*a)->addErrorRecord(time,s);
if(!ContinueAnyway) return false;
isOK = false;
};
};
if(!isOK) return false;
if(LaTeX) *report << "\\happeningOK\\\\\n";
return true;
};
Happening::~Happening()
{
for(vector<const Action *>::const_iterator i = actions.begin();i != actions.end();++i)
{
delete (*i);
};
};
// Is it best to return a bool or throw an exception?
bool
Happening::applyTo(State * s) const
{
/* This function will update the state:
*
* - for each action, need to determine which conditional effects are active;
* - must also mark ownership of propositions and functional expressions;
* - confirm no interaction;
* - set the time in new state.
*/
// First establish ownership of preconditions.
Ownership own(vld);
vector<const Action *> eventsForMutexCheck = vld->getEvents().getEventsForMutexCheck(); //this is in case an event is triggered at the same time as an action
if(!eventsForMutexCheck.empty())
{
bool repeated;
string eventName;
vector<const Action*>::iterator b = eventsForMutexCheck.begin();
for(;b != eventsForMutexCheck.end();)
{
repeated = false;
eventName = (*b)->getName0();
for(vector<const Action*>::const_iterator c = actions.begin();c != actions.end();++c)
{
if((*c)->getName0() == eventName) repeated = true; //there will be only a few possible values of b and c...
};
if(repeated) b = eventsForMutexCheck.erase(b);
else ++b;
};
for(vector<const Action*>::const_iterator b2 = eventsForMutexCheck.begin();b2 != eventsForMutexCheck.end();++b2)
{
(*b2)->markOwnedPreconditions(own);
};
};
//if one action do not mark since cant conflict
bool markPreCons = ((actions.size() + eventsForMutexCheck.size()) > 1);
if(markPreCons)
{
for(vector<const Action*>::const_iterator a = actions.begin();a != actions.end();++a)
{
(*a)->markOwnedPreconditions(own);
};
};
EffectsRecord effs;
for(vector<const Action*>::const_iterator a1 = actions.begin();a1 != actions.end();++a1)
{
if(!(*a1)->constructEffects(own,effs,s,markPreCons)) return false;
};
EffectsRecord effsDummy; //do not want to apply the effects now only check they do not conflict
for(vector<const Action*>::const_iterator b1 = eventsForMutexCheck.begin();b1 != eventsForMutexCheck.end();++b1)
{
if(!(*b1)->constructEffects(own,effsDummy,s,markPreCons)) return false;
};
//display polys/fns
if(LaTeX)
{
if(actions.size() != 0)
{
vector<const Action*>::const_iterator a2 = actions.begin();
if(CtsEffectAction * cea = dynamic_cast<CtsEffectAction*>(const_cast<Action*>(*a2)))
{
cea->displayCtsFtns();
};
};
};
effs.enact(s);
s->nowUpdated(this);
return true;
};
void Happening::adjustContext(ExecutionContext & ec) const
{
for(vector<const Action*>::const_iterator a = actions.begin();a != actions.end();++a)
{
(*a)->adjustContext(ec);
};
};
void Happening::adjustContextInvariants(ExecutionContext & ec) const
{
for(vector<const Action*>::const_iterator a = actions.begin();a != actions.end();++a)
{
(*a)->adjustContextInvariants(ec);
};
};
void Happening::adjustActiveCtsEffects(ActiveCtsEffects & ace) const
{
for(vector<const Action*>::const_iterator a = actions.begin();a != actions.end();++a)
{
(*a)->adjustActiveCtsEffects(ace);
};
ace.ctsEffectsProcessed = false;
};
void ExecutionContext::addCondAction(const CondCommunicationAction * ca)
{
invariants.actions.push_back(ca);
};
bool ExecutionContext::removeCondAction(const CondCommunicationAction * ca)
{
vector<const Action *>::iterator i = find(invariants.actions.begin(),invariants.actions.end(),ca);
if(i != invariants.actions.end())
{
invariants.actions.erase(i);
return true;
};
return false;
};
ExecutionContext::ExecutionContext(Validator * v) :
invariants(v)
{};
void ExecutionContext::addInvariant(const InvariantAction * a)
{
if(a != 0) invariants.actions.push_back(a);
};
void ExecutionContext::removeInvariant(const InvariantAction * a)
{
invariants.actions.erase(remove(invariants.actions.begin(),invariants.actions.end(),a),
invariants.actions.end());
};
void ExecutionContext::setTime(double t)
{
invariants.time = t;
};
ExecutionContext::~ExecutionContext()
{
invariants.actions.clear();
};
bool ExecutionContext::hasInvariants() const
{
return !(invariants.actions.empty());
};
void ActiveFE::addParentFE(const ActiveFE * afe)
{
//check if activeFE with this FE already exists, if not add it
vector<const ActiveFE*>::const_iterator i = find(parentFEs.begin(),parentFEs.end(),afe);
if(i == parentFEs.end()) parentFEs.push_back(afe);
};
void ActiveFE::removeParentFE(const ActiveFE * a)
{
parentFEs.erase(remove(parentFEs.begin(),parentFEs.end(),a),parentFEs.end());
};
void ExecutionContext::setActiveCtsEffects(ActiveCtsEffects * ace)
{
for(vector<const Action*>::const_iterator a = invariants.actions.begin();a != invariants.actions.end();++a)
{
if(InvariantAction * ia = dynamic_cast<InvariantAction*>(const_cast<Action*>(*a)))
ia->setActiveCtsEffects( ace );
else if(CondCommunicationAction * cca = dynamic_cast<CondCommunicationAction*>(const_cast<Action*>(*a)))
cca->setActiveCtsEffects( ace );
};
};
ActiveCtsEffects::ActiveCtsEffects(Validator * v) :
ctsEffects(v), ctsUpdateHappening(v), vld(v), eventTime(0)
{};
void ActiveCtsEffects::addCtsEffect(const CtsEffectAction * a)
{
if(a != 0) ctsEffects.actions.push_back(a);
};
void ActiveCtsEffects::removeCtsEffect(const CtsEffectAction * a)
{
ctsEffects.actions.erase(remove(ctsEffects.actions.begin(),ctsEffects.actions.end(),a),
ctsEffects.actions.end());
};
void ActiveCtsEffects::setTime(double t)
{
ctsEffects.time = t;
};
void ActiveCtsEffects::setLocalUpdateTime(double t)
{
localUpdateTime = t;
};
const Happening * ActiveCtsEffects::getCtsEffectUpdate() const
{
//return a new happening with only one ctseffectaction that has a pointer to the ace so that they may all be updated
ctsUpdateHappening.actions.clear();
ctsUpdateHappening.time = ctsEffects.time;
vector<const Action*>::const_iterator a = ctsEffects.actions.begin();
dynamic_cast<CtsEffectAction*>( const_cast<Action*>(*a) )->setActiveCtsEffects( const_cast<ActiveCtsEffects*>(this) );
ctsUpdateHappening.actions.push_back(*a);
return &ctsUpdateHappening;
};
bool ActiveCtsEffects::isFEactive(const FuncExp * fe) const
{
map<const FuncExp *,ActiveFE*>::const_iterator i = activeFEs.find(fe);
if(i != activeFEs.end()) return true;
return false;
};
struct FACtsEhandler {
Validator * vld;
ActiveCtsEffects * ace;
const effect_lists * effs;
Environment & bds;
var_symbol_table::const_iterator i;
const var_symbol_table::const_iterator endpt;
vector<const_symbol *> cs;
FACtsEhandler(Validator * v,ActiveCtsEffects * a,const forall_effect * eff,const Environment & bs) :
vld(v), ace(a), effs(eff->getEffects()), bds(*bs.copy(v)),
i(eff->getVars()->begin()), endpt(eff->getVars()->end()),
cs(vld->range(i->second)) {};
bool handle()
{
if(i == endpt)
{
Environment * env = bds.copy(vld);
for(list<assignment*>::const_iterator ae = effs->assign_effects.begin();
ae != effs->assign_effects.end();++ae)
{
ace->addActiveFE(*ae,*env);
};
return true;
};
var_symbol_table::const_iterator j = i++;
vector<const_symbol *> ds = i != endpt?vld->range(i->second):vector<const_symbol *>();
ds.swap(cs);
for(vector<const_symbol *>::iterator k = ds.begin();k != ds.end();++k)
{
bds[j->second] = *k;
if(!handle()) return false;
};
return true;
};
};
void ActiveCtsEffects::addActiveFE(assignment * e,const Environment & bs)
{
ActiveFE * afe;
const FuncExp * lhs = vld->fef.buildFuncExp(e->getFTerm(),bs);
bool increase = true;
//cout << *lhs <<"\n";
//check if activeFE with this FE already exists, if not add it
map<const FuncExp *,ActiveFE*>::const_iterator i = activeFEs.find(lhs);
if(i != activeFEs.end())
{
afe = i->second;
}
else
{
afe = new ActiveFE(lhs);
activeFEs[lhs] = afe;
//cout<<*lhs<<" \\\\\n added as active fe \n";
};
if(e->getOp() == E_DECREASE) increase = false;
//record the expressions that update the FE and if its increasing
afe->exprns.push_back(pair<pair<const expression *,bool>,const Environment *>(pair<const expression *,bool>(e->getExpr(),increase),&bs) );
};
void ActiveCtsEffects::addActiveFEs(bool reCalc)
{
if(ctsEffectsProcessed && !reCalc) return;
for(map<const FuncExp *, ActiveFE*>::iterator i = activeFEs.begin(); i != activeFEs.end(); ++i)
{ //cout << " deleting"<< *(i->first) <<"\n";
delete i->second;
};
activeFEs.clear();
//first create list of effects, cts effects will all be assign effects(or cond assign)
for(vector<const Action*>::iterator a = ctsEffects.actions.begin();
a != ctsEffects.actions.end();++a)
{
effect_lists * effects = new effect_lists();
for(list<assignment*>::const_iterator ae = (*a)->getEffects()->assign_effects.begin();
ae != (*a)->getEffects()->assign_effects.end();++ae)
{
effects->assign_effects.push_back(*ae);
};
for(list<forall_effect*>::const_iterator ae1 = (*a)->getEffects()->forall_effects.begin();
ae1 != (*a)->getEffects()->forall_effects.end();++ae1)
{
effects->forall_effects.push_back(*ae1);
};
//status will be true when displaying plan so if there is a possiblity of nonlinear effects an exception will be thrown
//loop thro' conditional effects if they are active add them to the effect list
for(vector<const CondCommunicationAction*>::const_iterator ca = dynamic_cast<const CtsEffectAction *>(*a)->condActions.begin();
ca != dynamic_cast<const CtsEffectAction *>(*a)->condActions.end();++ca)
{
if((*ca)->isActive())
{
for(list<assignment*>::const_iterator j = (*ca)->getEffects()->assign_effects.begin();
j != (*ca)->getEffects()->assign_effects.end();++j)
{
effects->assign_effects.push_back(*j);
};
for(list<forall_effect*>::const_iterator j1 = (*ca)->getEffects()->forall_effects.begin();
j1 != (*ca)->getEffects()->forall_effects.end();++j1)
{
effects->forall_effects.push_back(*j1);
};
};
};
//loop thro' effects and create active FE objects
for(list<assignment*>::const_iterator e = effects->assign_effects.begin();
e != effects->assign_effects.end();++e)
{
addActiveFE(*e,(*a)->bindings);
};
//loop thro' for all effects and create active FE objects
for(list<forall_effect*>::const_iterator e1 = effects->forall_effects.begin();
e1 != effects->forall_effects.end();++e1)
{
FACtsEhandler faceh(vld,this,*e1,(*a)->bindings);
faceh.handle();
};
effects->assign_effects.clear();
effects->forall_effects.clear();
delete effects;
};//end of creating active FEs from actions
//add parentFEs now
for(map<const FuncExp *,ActiveFE*>::iterator a1 = activeFEs.begin();
a1 != activeFEs.end();++a1)
{
for(vector<pair<pair<const expression*,bool>,const Environment *> >::const_iterator i = a1->second->exprns.begin();
i != a1->second->exprns.end();++i)
{
a1->second->addParentFEs(this,(*i).first.first,(*i).second );
};
};
//build cts functions for each active FE now
buildAFECtsFtns();
ctsEffectsProcessed = true;
};
void ActiveFE::addParentFEs(const ActiveCtsEffects * ace,const expression * e,const Environment * bs)
{
if(dynamic_cast<const num_expression *>(e)) return;
if(const func_term * fexpression = dynamic_cast<const func_term *>(e))
{
const FuncExp * fexp = ace->vld->fef.buildFuncExp(fexpression,*bs );
//check that the FE is changing, it may it constant for this interval
for(map<const FuncExp *,ActiveFE*>::const_iterator j = ace->activeFEs.begin();j != ace->activeFEs.end();++j)
{
if(j->second->fe == fexp)
{
//cout<<j->second->fe<<" is parent of "<<this->fe<<"\n";
addParentFE(j->second);
break;
};
};
return;
};
if(const binary_expression * bexp = dynamic_cast<const binary_expression *>(e))
{
addParentFEs(ace,bexp->getLHS(),bs);
addParentFEs(ace,bexp->getRHS(),bs);
return;
};
if(const uminus_expression * uexp = dynamic_cast<const uminus_expression *>(e))
{
addParentFEs(ace,uexp->getExpr(),bs);
return;
};
if(dynamic_cast<const special_val_expr *>(e))
{
return;
};
if(Verbose) *report << "Unrecognised expression type\n";
UnrecognisedCondition uc;
throw uc;
};
bool ActiveFE::appearsInEprsn(const ActiveCtsEffects * ace,const expression * e,const Environment * bs) const
{
if(const func_term * fexpression = dynamic_cast<const func_term *>(e))
{
const FuncExp * fexp = ace->vld->fef.buildFuncExp(fexpression,*bs );
if(fe == fexp) return true;
};
if(const binary_expression * bexp = dynamic_cast<const binary_expression *>(e))
{
if(appearsInEprsn(ace,bexp->getLHS(),bs)) return true;
if(appearsInEprsn(ace,bexp->getRHS(),bs)) return true;
};
if(const uminus_expression * uexp = dynamic_cast<const uminus_expression *>(e))
{
if(appearsInEprsn(ace,uexp->getExpr(),bs)) return true;
};
return false;
};
void ActiveCtsEffects::buildAFECtsFtns()
{
//mark these PNEs as changed ctsly, in order to judder for robustness analysis
if(Robust) for(map<const FuncExp*,ActiveFE*>::iterator i = activeFEs.begin();i != activeFEs.end();++i) const_cast<FuncExp*>(i->first)->setChangedCtsly();
//sort out exp (etc) functions first then...
//loop thro active FEs in correct order given by dependicies in diff equns (topological sort)
//build polys from expresions and then integrate
vector<ActiveFE*> topSortActiveFEs;
vector<ActiveFE*> listToTopSortTempActiveFEs;
vector<ActiveFE*> listToTopSortActiveFEs;
vector<ActiveFE*> loopDepTempActiveFEs;
vector<ActiveFE*> loopDepActiveFEs;
vector<ActiveFE*> loopDepActiveFEs2;
bool inList;
//sift out loop dependencies for exp functions (df/dt = f)
for(map<const FuncExp*,ActiveFE*>::iterator i = activeFEs.begin();i != activeFEs.end();++i)
{
bool selfDepend = false;
for(vector<const ActiveFE*>::iterator j = i->second->parentFEs.begin(); j != i->second->parentFEs.end();++j)
{
if(*j == i->second)
{
selfDepend = true;
break;
};
};
if(selfDepend) loopDepTempActiveFEs.push_back(i->second);
else listToTopSortTempActiveFEs.push_back(i->second);
};
//sift out dependencies for diff equations that are self dependent and depend on another from the list
for(vector<ActiveFE*>::iterator i = loopDepTempActiveFEs.begin();i != loopDepTempActiveFEs.end();++i)
{
bool otherDepend = false;
for(vector<const ActiveFE*>::iterator j = (*i)->parentFEs.begin(); j != (*i)->parentFEs.end();++j)
{
//check if it depends on another FE from loopDep list
for(vector<ActiveFE*>::iterator k = loopDepTempActiveFEs.begin();k != loopDepTempActiveFEs.end();++k)
{
if( *k == *j && *i != *j) { otherDepend = true; break;};
};
if(otherDepend) break;
};
if(otherDepend) loopDepActiveFEs2.push_back(*i);
else loopDepActiveFEs.push_back(*i);
};
//sift out loop dependencies for exp functions from above list (d f_2/dt = f_1, where f_1 from above list)
for(vector<ActiveFE*>::iterator i = listToTopSortTempActiveFEs.begin();i != listToTopSortTempActiveFEs.end();++i)
{
if( (*i)->parentFEs.size() > 0)
{
//is in loopDepActiveFEs
inList = false;
for(vector<ActiveFE*>::iterator j = loopDepActiveFEs.begin();j != loopDepActiveFEs.end();++j)
{
for(vector<const ActiveFE*>::iterator k = (*i)->parentFEs.begin();k != (*i)->parentFEs.end();++k)
{
if( *k == *j) { inList = true; break;};
};
if(inList) break;
};
if(inList)
{
loopDepActiveFEs2.push_back(*i);
//cout << "\\\\\\> Exp fn 2 "<< (*i)->fe <<"\\\\\n";
}
else
{
//cout << "\\> Top sort fn 2 "<< (*i)->fe <<"\\\\";
listToTopSortActiveFEs.push_back(*i);
};
}
else
{
//cout << "\\> Top sort fn default 2 "<< (*i)->fe <<"par szie="<<(*i)->parentFEs.size()<<"\\\\";
listToTopSortActiveFEs.push_back(*i);
};
};
//do topological sort on remaining AFEs
for(vector<ActiveFE*>::iterator i = listToTopSortActiveFEs.begin();i != listToTopSortActiveFEs.end();++i)
{
(*i)->colour = 0; //white
};
for(vector<ActiveFE*>::iterator i = listToTopSortActiveFEs.begin();i != listToTopSortActiveFEs.end();++i)
{
visitActiveFE(*i,topSortActiveFEs);
};
//define polys from top sorted list should be no errors
for(vector<ActiveFE*>::iterator i = topSortActiveFEs.begin();i != topSortActiveFEs.end();++i)
{
if( (*i)->ctsFtn == 0) (*i)->ctsFtn = buildPoly(*i);
};
//define exps now and numerical solutions
for(vector<ActiveFE*>::iterator i = loopDepActiveFEs.begin();i != loopDepActiveFEs.end();++i)
{
// cout << *((*i)->fe) << " is the expression and " << (*i)->exprns.size() << " its size\n";
// for(vector<pair< pair<const expression *,bool> ,const Environment *> >::const_iterator j = (*i)->exprns.begin();
// j != (*i)->exprns.end();++j)
// {
// cout << *(j->first.first) << " " << j->first.second << "\n";
//
// }
if( (*i)->canResolveToExp(activeFEs,vld))
{
// cout << "We're going to try to build an Exponential here\n";
if( (*i)->ctsFtn == 0) (*i)->ctsFtn = buildExp(*i);
}
else
{
if((*i)->ctsFtn == 0) (*i)->ctsFtn = buildNumericalSoln(*i);
};
};
//define other exps now
for(vector<ActiveFE*>::iterator i = loopDepActiveFEs2.begin();i != loopDepActiveFEs2.end();++i)
{
if( (*i)->ctsFtn == 0) (*i)->ctsFtn = buildExp(*i);
};
};
bool ActiveFE::canResolveToExp(const map<const FuncExp*,ActiveFE*> activeFEs,Validator * vld) const
{
if(exprns.size() == 1) return true;
// Add a new case to handle the situation in which we have two expressions, but one is a constant
// multiple of #t
if(exprns.size() == 2)
{
if(isConstLinearChangeExpr(exprns[0],activeFEs,vld) || isConstLinearChangeExpr(exprns[1],activeFEs,vld))
return true;
};
return false;
};
bool isConstLinearChangeExpr(const ExprnPair & exp,const map<const FuncExp *,ActiveFE *> activeFEs,Validator * vld)
{
const expression * ex = getRateExpression(exp.first.first);
return isConstant(ex,exp.second,activeFEs,vld);
};
bool isConstant(const expression * exp,const Environment * env,const map<const FuncExp *,ActiveFE *> activeFEs,Validator * vld)
{
const func_term * ft = dynamic_cast<const func_term *>(exp);
if(ft)
{
const FuncExp * ftt = vld->fef.buildFuncExp(ft,*env);
if(activeFEs.find(ftt) != activeFEs.end())
{
// cout << "Found non-constant FuncTerm " << *ftt << "\n";
return false;
}
else return true;
};
const num_expression * nt = dynamic_cast<const num_expression *>(exp);
if(nt)
{
return true;
};
const binary_expression * be = dynamic_cast<const binary_expression *>(exp);
if(be)
{
return isConstant(be->getLHS(),env,activeFEs,vld) &&
isConstant(be->getRHS(),env,activeFEs,vld);
};
return false;
};
void ActiveCtsEffects::visitActiveFE(ActiveFE * afe,vector<ActiveFE*> & topSAFEs)
{
if(afe->colour == 1) //check if already been visted and thus a loop
{
HighOrderDiffEqunError hodee;
throw hodee;
};
if(afe->colour != 0) return;
afe->colour = 1; //grey
for(vector<const ActiveFE*>::iterator i = afe->parentFEs.begin();i != afe->parentFEs.end();++i)
{
visitActiveFE(const_cast<ActiveFE*>(*i),topSAFEs);
};
afe->colour = 2; //black
topSAFEs.push_back(afe);
return;
};
//extract just the rate of change for cts effect
// - if the syntax of cts effects changes then this will need to be changed
const expression* getRateExpression(const expression* aExpression)
{
const expression * rateExprn;
if(const mul_expression * me = dynamic_cast<const mul_expression *>(aExpression))
{
if(dynamic_cast<const special_val_expr *>(me->getLHS()))
{
rateExprn = me->getRHS();
}
else if(dynamic_cast<const special_val_expr *>(me->getRHS()))
{
rateExprn = me->getLHS();
}
else
{
DiffEqunError dee;
throw dee;
};
}
else
{
DiffEqunError dee;
throw dee;
};
return rateExprn;
};
const Polynomial * ActiveCtsEffects::buildPoly(const ActiveFE * afe)
{
Polynomial thePoly;
//loop thro exprns and create poly from each, the sum integrated is our poly
for(vector<pair<pair<const expression*,bool>,const Environment *> >::const_iterator i = afe->exprns.begin();
i != afe->exprns.end();++i)
{
//extract just the rate of change for cts effect
// - if the syntax of cts effects changes then this will need to be changed
if(dynamic_cast<const mul_expression *>(i->first.first))
{
thePoly += getPoly(getRateExpression(i->first.first),i->first.second,this,i->second);
}
else if(const special_val_expr * sve = dynamic_cast<const special_val_expr *>(i->first.first))
{
if(sve->getKind() == E_HASHT)
{
Polynomial timet;
timet.setCoeff(0,1);
if(i->first.second)
thePoly += timet;
else
thePoly -= timet;
}
else
{
thePoly += getPoly(i->first.first,i->first.second,this,i->second);
};
}
else
{
thePoly += getPoly(i->first.first,i->first.second,this,i->second);
};
};
thePoly = thePoly.integrate();
//add boundary condition
double bc = afe->fe->evaluate(&(vld->getState()));
thePoly.setCoeff(0,bc);
//cout << "Boundary for "<<*(afe->fe)<<" is "<<bc<<" -- "<<thePoly <<"\n";
const Polynomial * newPoly = new Polynomial(thePoly);