#ifndef PROBLEM_HPP #define PROBLEM_HPP #include #include using namespace std; /* ********************************************************************************* Problem ********************************************************************************* Classes ------- State - Domain of the search Space Goal_Result - Domain of result space (Boolean,Real) Path_Cost_Value - Domain of the path cost (void,Real,Vector) Initial State - State Goal - Goal_Result Operator - State -> State Operator_Generator - State -> list (One by one) Goal_Test - State -> Goal_Result Path_Cost - State X Operator -> Path_Cost_Value Problem - (Operatore_Generator,Goal_Test,Path_Cost) Strategy - State -> Operator Strategy_Sequence - State -> Strategy ********************************************************************************* */ class State { public: virtual ~State(void) {}; }; template class Operator { public : virtual ~Operator(void) {}; virtual T* Operate(T *a) { return a;}; }; template class Operator_Generator { public: virtual ~Operator_Generator(void) {}; virtual void Init(T *t) = 0; virtual Operator *Next(void) =0 ; virtual bool IsEnd(void) = 0; }; template class Goal_Test { public: virtual ~Goal_Test(void) {}; virtual G Goal_Value(T *a) = 0; // virtual G *Goal_Compare(G *cmp,G *a,G *b) =0; // This function compares which Goal is closer to cmp;a or b and returns it }; template class Path_Cost { public: virtual ~Path_Cost(void) {}; virtual P Cost(T *a,Operator *o) = 0; }; template class Problem { public: virtual ~Problem(void) {}; public: Operator_Generator *gen; Goal_Test *gt; Path_Cost *pc; }; template class Strategy { public: Problem *problem; public: virtual ~Strategy(void) {}; virtual void InitState(T *state) = 0; virtual Operator *Next(void) = 0; // Given the caller and the list of search node - What is the goal value expected for me. }; template class Strategy_Sequence { public: virtual ~Strategy_Sequence(void) {}; virtual Strategy *Next_Strategy(T *p) = 0; }; template class Goal_Accumulator { public: virtual ~Goal_Accumulator(void) {} // Return true if the state,operator and value are kept in the accumulator virtual void Put(T *state,Operator *o,G v) = 0; virtual T * Get_State (void) = 0; virtual Operator * Get_Operator(void) = 0 ; virtual G Get_Value(void) = 0; }; template class Single_Goal_Accumulator : public Goal_Accumulator { public: G value; T *state; Operator *oper; bool first; public: Single_Goal_Accumulator(void) { state=NULL; oper=NULL; first=true; } virtual ~Single_Goal_Accumulator(void) { Reset(); } virtual void Reset(void) { first=true; if (state!=NULL) delete state; if (oper!=NULL) delete oper; state=NULL; oper=NULL; } virtual void Put(T *s,Operator *o,G v) { if (first) { first=false; state=s; oper=o; value=v; return; } P pred; if (pred(v,value)) { delete state; delete oper; state=s; value=v; oper=o; } else { delete o; delete s; } } virtual G Get_Value(void) { return value; } virtual T * Get_State(void) { T *ret=state; state=NULL; return ret; } virtual Operator *Get_Operator(void) { Operator *ret=oper; oper=NULL; return ret; } }; template class Multi_Goal_Accumulator : public Goal_Accumulator { public: int size; T* *states; Operator* *operators; G* values; int count; int current; public: Multi_Goal_Accumulator(int s) { size=s; states=new T* [size]; operators=new Operator* [size]; values=new G [size]; for (int i=0;i *o,G v) { P pred; if (count=count;}; virtual G Get_Value(void){ return values[current];}; virtual T *Get_State(void) { T *ret=states[current]; states[current]=NULL; return ret; } virtual Operator *Get_Operator(void) { Operator *ret=operators[current]; operators[current]=NULL; return ret; } }; template class Max_Goal_Accumulator : public Single_Goal_Accumulator > {}; template class Min_Goal_Accumulator : public Single_Goal_Accumulator > {}; template class Successor_Goal_Test : public Goal_Test { public: Operator_Generator *generator; Goal_Accumulator *goal_acc; Goal_Test *tester; public: Successor_Goal_Test(Operator_Generator *g,Goal_Accumulator *ga,Goal_Test *t) { generator=g; goal_acc=ga; tester=t; } virtual G Goal_Value(T *a) { for (generator->Init(a);!generator->IsEnd();) { Operator *op=generator->Next(); T *next_state=op->Operate(a); G value=tester->Goal_Value(next_state); goal_acc->Put(next_state,op,value); } return goal_acc->Get_Value(); } }; template class Minimax_Goal_Test : public Successor_Goal_Test { public: int depth; Goal_Accumulator *other_acc; public: Minimax_Goal_Test(int d,Operator_Generator *g,Goal_Accumulator *ga,Goal_Accumulator *o,Goal_Test *end) : Successor_Goal_Test(g,ga,end) { depth=d; other_acc=o; } virtual G Goal_Value(T *a) { if (depth==0) return this->tester->Goal_Value(a); Goal_Test *endtester=this->tester; this->tester=new Minimax_Goal_Test(depth-1,this->generator,other_acc,this->goal_acc,this->tester); G ret_val=Successor_Goal_Test::Goal_Value(a); delete this->tester; this->tester=endtester; delete this->next; return ret_val; } }; template class Multi_Minimax_Goal_Test : public Successor_Goal_Test { public: int branch; int depth; Goal_Accumulator *other; public: Multi_Minimax_Goal_Test(int d,int b, Operator_Generator *gen, Goal_Accumulator *mine, Goal_Accumulator *o, Goal_Test *t) : Successor_Goal_Test(gen,mine,t) { depth=d; branch=b; other=o; } virtual G Goal_Value(T *a) { Goal_Accumulator *old=this->goal_acc; Multi_Goal_Accumulator > *mga=new Multi_Goal_Accumulator >(branch); this->goal_acc=mga; Successor_Goal_Test::Goal_Value(a); this->goal_acc=old; for (mga->Init();!mga->IsEnd();mga->Next()) { T *state=mga->Get_State(); Multi_Minimax_Goal_Test *next=new Multi_Minimax_Goal_Test(this->d-1,this->b,this->gen,other,this->goal_acc,this->tester); next->Goal_Value(state); this->goal_acc->Put(this->accumulator->value,this->acc->state,this->acc->op); } this->goal_acc->Put(mga->Get_State(),mga->Get_Operator(),mga->Get_Result()); return this->goal_acc->Get_Value(); } }; #endif