Skip to content

Commit ffd6f0b

Browse files
committed
templated policies, tentatively working. next step is matched_step
1 parent fc5b6f5 commit ffd6f0b

16 files changed

+110
-71
lines changed

src/deadend.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "regression.h"
44

55

6-
FSAP::FSAP(PR2State *s, PR2OperatorProxy o) : PolicyItem(s), op(&o) {}
6+
FSAP::FSAP(PR2State *s, PR2OperatorProxy *o) : PolicyItem(s), op(o) {}
77

88

99
void FSAP::dump() const {
@@ -78,7 +78,7 @@ bool generalize_deadend(PR2State &state) {
7878

7979
void update_deadends(vector< DeadendTuple* > &failed_states) {
8080

81-
list<PolicyItem *> fsaps;
81+
list<FSAP *> fsaps;
8282
list<PolicyItem *> deadends;
8383

8484
PR2State *dummy_state = new PR2State();
@@ -109,9 +109,9 @@ void update_deadends(vector< DeadendTuple* > &failed_states) {
109109
for (auto item : reg_items) {
110110

111111
RegressableOperator *ro = (RegressableOperator*)item;
112-
112+
PR2OperatorProxy *ro_op_ptr = new PR2OperatorProxy(ro->op);
113113
fsaps.push_back(new FSAP(failed_state->regress(ro->op, dummy_state),
114-
ro->op));
114+
ro_op_ptr));
115115

116116
}
117117

@@ -138,9 +138,10 @@ void update_deadends(vector< DeadendTuple* > &failed_states) {
138138
// If we have a specified previous state and action, use that to
139139
// build a forbidden state-action pair
140140
if (NULL != failed_state_prev) {
141+
PR2OperatorProxy *prev_op_ptr = new PR2OperatorProxy(*prev_op);
141142
fsaps.push_back(new FSAP(
142-
failed_state->regress(*prev_op, failed_state_prev),
143-
*prev_op));
143+
failed_state->regress(*prev_op, failed_state_prev),
144+
prev_op_ptr));
144145
}
145146
}
146147

@@ -154,9 +155,11 @@ void update_deadends(vector< DeadendTuple* > &failed_states) {
154155

155156
// Add a pointer from the operator to the newly created fsaps
156157
for (auto fsap : fsaps)
157-
PR2.deadend.nondetop2fsaps[((FSAP*)fsap)->get_index()]->push_back((FSAP*)fsap);
158+
PR2.deadend.nondetop2fsaps[fsap->get_index()]->push_back(fsap);
158159

159160
PR2.deadend.policy->update_policy(fsaps);
161+
for (auto fsap : PR2.deadend.policy->all_items)
162+
std::cout << fsap->get_index() << endl;
160163
PR2.deadend.states->update_policy(deadends);
161164
}
162165

src/deadend.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@ struct DeadendTuple {
2828
~DeadendTuple() {};
2929
};
3030

31-
struct FSAP : PolicyItem {
32-
33-
PR2OperatorProxy *op; // The nondet action id we are forbidding
34-
35-
FSAP(PR2State *s, PR2OperatorProxy o);
36-
FSAP(PR2State *s) : PolicyItem(s), op(NULL) {}
37-
38-
~FSAP() {}
39-
40-
bool operator< (const FSAP& other) const;
41-
42-
string get_name();
43-
int get_index();
44-
void dump() const;
45-
};
46-
4731
struct Deadend : FSAP {
4832
Deadend(PR2State *s) : FSAP(s) {};
4933
void dump() const;

src/fd_integration/fsap_penalized_ff_heuristic.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,12 @@ int FSAPPenalizedFFHeuristic::compute_heuristic(const State &state) {
327327

328328
// Make sure we don't mark an operator as preferred if it's forbidden
329329
forbidden_ops.clear();
330-
vector<PolicyItem *> reg_items;
330+
vector<FSAP *> reg_items;
331331
PR2State * ps = new PR2State(state);
332332
PR2.deadend.policy->generate_entailed_items(*ps, reg_items);
333333
delete ps;
334334
for (auto item : reg_items)
335-
forbidden_ops.insert(((FSAP*)item)->get_index());
335+
forbidden_ops.insert(item->get_index());
336336

337337
// Collecting the relaxed plan also sets the preferred operators.
338338
for (size_t i = 0; i < goal_propositions.size(); ++i)

src/fd_integration/fsap_penalized_ff_heuristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class State;
1414
class PR2State;
15-
class FSAP;
15+
struct FSAP;
1616

1717
using namespace std;
1818

src/fd_integration/pr2_proxies.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class PR2OperatorProxy : public OperatorProxy {
9898
return -1;
9999
}
100100
// might be necessary as PR2GoalProxy needs to override
101-
virtual EffectsProxy get_all_effects() const {
101+
EffectsProxy get_all_effects() const {
102102
return get_effects();
103103
}
104104
// // might be necessary as PR2GoalProxy needs to override

src/fd_integration/pr2_search_algorithm.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ void DeadendAwareSuccessorGenerator::generate_applicable_ops(const PR2State &_cu
125125

126126
PR2State curr = PR2State(_curr);
127127

128-
vector<PolicyItem *> reg_items;
128+
vector<FSAP *> reg_items;
129129
vector<OperatorID> orig_ops;
130-
map<int, PolicyItem *> fsap_map;
130+
map<int, FSAP *> fsap_map;
131131

132132
PR2.generate_orig_applicable_ops(_curr, orig_ops);
133133
PR2.deadend.policy->generate_entailed_items(curr, reg_items);
134134

135135
set<int> forbidden;
136136
for (auto item : reg_items) {
137137

138-
int index = ((FSAP*)item)->get_index();
138+
int index = item->get_index();
139139

140140
forbidden.insert(index);
141141

@@ -159,7 +159,7 @@ void DeadendAwareSuccessorGenerator::generate_applicable_ops(const PR2State &_cu
159159
// Combind all of the FSAPs
160160
PR2State *newDE = new PR2State();
161161
for (unsigned i = 0; i < ruled_out.size(); i++) {
162-
newDE->combine_with(*(((FSAP*)(fsap_map[ruled_out[i]]))->state));
162+
newDE->combine_with(*((fsap_map[ruled_out[i]])->state));
163163
}
164164

165165
// Also rule out all of the unapplicable actions

src/fond_search.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ void strengthen_and_mark(PR2SearchStatus * status,
474474
int successor_id_for_dst) {
475475

476476
// Strengthen the solsteps all the way back
477-
list<PolicyItem *> new_steps;
477+
list<SolutionStep *> new_steps;
478478

479479
PR2.solution.incumbent->network->fixed_point_regression(
480480
previous_step, // src

src/partial_state_graph.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void PSGraph::fixed_point_regression(SolutionStep * src,
1212
PR2SearchNode * dst_node,
1313
int successor_id_for_dst,
1414
map< SolutionStep*, set< PR2SearchNode * > * > &solstep2searchnode,
15-
list<PolicyItem *> &new_steps,
15+
list<SolutionStep *> &new_steps,
1616
bool make_connection) {
1717

1818
#ifndef NDEBUG

src/partial_state_graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct PSGraph {
2626
PR2SearchNode * dst_node,
2727
int successor_id_for_dst,
2828
map< SolutionStep* , set< PR2SearchNode * > * > &solstep2searchnode,
29-
list<PolicyItem *> &new_steps,
29+
list<SolutionStep *> &new_steps,
3030
bool make_connection = false);
3131

3232
void fixed_point_marking(SolutionStep * node);

src/policy.cc

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,29 @@ vector< pair<int,int> > * PolicyItem::varvals() {
3030
return state->varvals();
3131
}
3232

33-
34-
Policy::~Policy() {
33+
template <typename T>
34+
Policy<T>::~Policy() {
3535
if (root)
3636
delete root;
3737

3838
for (auto item : all_items)
3939
delete item;
4040
}
41+
template Policy<PolicyItem>::~Policy();
42+
template Policy<FSAP>::~Policy();
43+
template Policy<SolutionStep>::~Policy();
4144

4245
// TODO: Revisit this comparison -- just doing a pointer comparison for now
43-
bool fsap_compare(PolicyItem* first, PolicyItem* second) {
46+
bool fsap_compare(PolicyItem *first, PolicyItem *second)
47+
{
4448
return ((const FSAP*)first) < ((const FSAP*)second);
4549
}
4650
bool solstep_compare(PolicyItem* first, PolicyItem* second) {
4751
return *((const SolutionStep*)first) < *((const SolutionStep*)second);
4852
}
49-
void Policy::write_policy(string fname, bool fsap) {
53+
54+
template <typename T>
55+
void Policy<T>::write_policy(string fname, bool fsap) {
5056

5157
if (!fsap)
5258
all_items.sort(fsap ? fsap_compare : solstep_compare);
@@ -76,24 +82,39 @@ void Policy::write_policy(string fname, bool fsap) {
7682

7783
outfile.close();
7884
}
85+
template void Policy<PolicyItem>::write_policy(string fname, bool fsap);
86+
template void Policy<FSAP>::write_policy(string fname, bool fsap);
87+
template void Policy<SolutionStep>::write_policy(string fname, bool fsap);
7988

80-
void Policy::dump(bool fsap) const {
89+
template <typename T>
90+
void Policy<T>::dump(bool fsap) const {
8191
cout << (fsap ? "FSAP " : "") << "Policy:" << endl;
8292
for (auto item : all_items)
8393
item->dump();
8494
}
95+
template void Policy<PolicyItem>::dump(bool fsap) const;
96+
template void Policy<SolutionStep>::dump(bool fsap) const;
8597

86-
void Policy::generate_cpp_input(ofstream &outfile) const {
98+
template <typename T>
99+
void Policy<T>::generate_cpp_input(ofstream &outfile) const {
87100
root->generate_cpp_input(outfile);
88101
}
102+
template void Policy<PolicyItem>::generate_cpp_input(ofstream &outfile) const;
103+
template void Policy<FSAP>::generate_cpp_input(ofstream &outfile) const;
104+
template void Policy<SolutionStep>::generate_cpp_input(ofstream &outfile) const;
89105

90-
void Policy::add_item(PolicyItem *item) {
91-
list<PolicyItem *> reg_items;
106+
template <typename T>
107+
void Policy<T>::add_item(T *item) {
108+
list<T *> reg_items;
92109
reg_items.push_back(item);
93110
update_policy(reg_items);
94111
}
112+
template void Policy<PolicyItem>::add_item(PolicyItem *item);
113+
template void Policy<FSAP>::add_item(FSAP *item);
114+
template void Policy<SolutionStep>::add_item(SolutionStep *item);
95115

96-
void Policy::update_policy(list<PolicyItem *> &reg_items) {
116+
template <typename T>
117+
void Policy<T>::update_policy(list<T *> &reg_items) {
97118

98119
list<MatchtreeItem *> mtis;
99120
for (auto item : reg_items)
@@ -105,28 +126,36 @@ void Policy::update_policy(list<PolicyItem *> &reg_items) {
105126
else
106127
root = new MatchtreeSwitch(mtis, vars_seen);
107128
all_items.insert(all_items.end(), reg_items.begin(), reg_items.end());
108-
109129
}
130+
template void Policy<PolicyItem>::update_policy(list<PolicyItem *> &reg_items);
131+
template void Policy<FSAP>::update_policy(list<FSAP *> &reg_items);
132+
template void Policy<SolutionStep>::update_policy(list<SolutionStep *> &reg_items);
110133

111-
bool Policy::check_consistent_match(const PR2State &curr) {
134+
template <typename T>
135+
bool Policy<T>::check_consistent_match(const PR2State &curr) {
112136
if (root)
113137
return root->check_consistent_match(curr);
114138
else
115139
return false;
116140
}
117141

118-
bool Policy::check_entailed_match(const PR2State &curr) {
142+
template <typename T>
143+
bool Policy<T>::check_entailed_match(const PR2State &curr) {
119144
if (root)
120145
return root->check_entailed_match(curr);
121146
else
122147
return false;
123148
}
149+
template bool Policy<PolicyItem>::check_entailed_match(const PR2State &curr);
150+
template bool Policy<FSAP>::check_entailed_match(const PR2State &curr);
151+
template bool Policy<SolutionStep>::check_entailed_match(const PR2State &curr);
124152

125-
void Policy::rebuild() {
153+
template <typename T>
154+
void Policy<T>::rebuild() {
126155

127156
// We are only going to keep the items that are active
128157
list<MatchtreeItem *> mtis;
129-
list<PolicyItem *> new_items;
158+
list<T *> new_items;
130159
for (auto item : all_items) {
131160
if (item->is_active) {
132161
mtis.push_back((MatchtreeItem *)item);
@@ -143,3 +172,7 @@ void Policy::rebuild() {
143172
all_items.swap(new_items);
144173

145174
}
175+
template void Policy<PolicyItem>::rebuild();
176+
template void Policy<FSAP>::rebuild();
177+
template void Policy<SolutionStep>::rebuild();
178+

0 commit comments

Comments
 (0)