#include #include #include #include #include #include //#include #define cout STD_COUT #define cerr STD_CERR #define istream STD_ISTREAM #define ostream STD_OSTREAM #define ifstream STD_IFSTREAM void ListInitializer(GAGenome &); void ArrayInitializer(GAGenome &); float Objective(GAGenome & ); class SubhaGenome : public GAGenome { public: GADefineIdentity("SubhaGenome", 201); static void SubhaInitializer(GAGenome&); static int SubhaMutator(GAGenome&, float); static float SubhaComparator(const GAGenome&, const GAGenome&); static int SubhaCrossover(const GAGenome&, const GAGenome&, GAGenome*, GAGenome*); public: SubhaGenome(int, GAGenome::Evaluator f=NULL, void* u=NULL); SubhaGenome(const SubhaGenome & orig); SubhaGenome& operator=(const GAGenome& g); virtual ~SubhaGenome(); virtual GAGenome* clone(GAGenome::CloneMethod) const ; virtual void copy(const GAGenome & c); virtual int equal(const GAGenome& g) const; virtual int read(istream & is); virtual int write(ostream & os) const; GA1DArrayGenome & factorstr() const {return *fac;} GAListGenome & priority() const {return *pri;} protected: GA1DArrayGenome *fac; GAListGenome*pri; }; int SubhaGenome::write(ostream & os) const { int i,j; for(i=0; ilength(); i++){ os << (fac->gene(i) ) <<"\t"; //os << "\n"; } os << "\n" << *pri->head() << "\t"; for(i=1;i<10;i++) { os <<*pri->next()<< "\t"; } return os.fail() ? 1 : 0; } SubhaGenome:: SubhaGenome(int length,GAGenome::Evaluator f, void* u) : GAGenome( SubhaInitializer, SubhaMutator, SubhaComparator) { evaluator(f); userData(u); crossover(SubhaCrossover); fac = new GA1DArrayGenome (length, f, u); pri = new GAListGenome ( f, u); } SubhaGenome::SubhaGenome(const SubhaGenome & orig) { fac = new GA1DArrayGenome(orig.factorstr()); pri= new GAListGenome(orig.priority()); copy(orig); } SubhaGenome& SubhaGenome::operator=(const GAGenome& g) { copy(g); return *this; } SubhaGenome::~SubhaGenome() { delete fac; delete pri; } GAGenome* SubhaGenome::clone(GAGenome::CloneMethod) const { return new SubhaGenome(*this); } void SubhaGenome::copy(const GAGenome & c){ if(&c != this && sameClass(c)){ GAGenome::copy(c); SubhaGenome & bc = (SubhaGenome &)c; fac->copy(*(bc.fac)); pri->copy(*(bc.pri)); } } int SubhaGenome::equal(const GAGenome& g) const { SubhaGenome& genome = (SubhaGenome&)g; return ((*fac == *genome.fac) && (*pri == *genome.pri)); } int SubhaGenome::read(istream & is) { is >> *fac>> *pri; return is.fail() ? 1 : 0; } void SubhaGenome::SubhaInitializer(GAGenome & c) { SubhaGenome & child = (SubhaGenome &)c; child.factorstr().initializer(ArrayInitializer); child.priority().initializer(ListInitializer); child.factorstr().initialize(); child.priority().initialize(); child._evaluated = gaFalse; } int SubhaGenome::SubhaMutator(GAGenome & c, float pmut) { SubhaGenome & child = (SubhaGenome &)c; int nmut = child.factorstr().mutate(pmut) + child.priority().mutate(pmut); if(nmut) child._evaluated = gaFalse; return nmut; } float SubhaGenome::SubhaComparator(const GAGenome& a, const GAGenome& b) { SubhaGenome& sis = (SubhaGenome &)a; SubhaGenome& bro = (SubhaGenome &)b; return 0.5 * (sis.factorstr().compare(bro) + sis.priority().compare(bro)); } int SubhaGenome:: SubhaCrossover(const GAGenome& a, const GAGenome& b, GAGenome* c, GAGenome* d){ SubhaGenome& mom = (SubhaGenome&)a; SubhaGenome& dad = (SubhaGenome&)b; int n=0; // GAGenome.Crossover=PartialMatchCrossover; GAGenome::SexualCrossover faccross = mom.fac->sexual(); GAGenome:: SexualCrossover pricross = mom.pri->sexual(); //GAGenome::SexualCrossover pricross (GAGenome::SexualCrossover PartialMatchCrossover); if(c && d){ SubhaGenome& sis = (SubhaGenome&)*c; SubhaGenome& bro = (SubhaGenome&)*d; // sis.priority()::SexualCrossover Crossover (sis.priority::PartialMatchCrossover); // bro.priority().Crossover(GAListGenome::PartialMatchCrossover); (*faccross)(mom.factorstr(), dad.factorstr(), &sis.factorstr(), &bro.factorstr()); (*pricross)(mom.priority(),dad.priority(), &sis.priority(), &bro.priority()); sis._evaluated = gaFalse; bro._evaluated = gaFalse; n = 2; } else if(c){ SubhaGenome& sis = (SubhaGenome&)*c; (*faccross)(mom.factorstr(), dad.factorstr(), &sis.factorstr(), 0); (*pricross)(mom.priority(), dad.priority(), &sis.priority(), 0); sis._evaluated = gaFalse; n = 1; } else if(d){ SubhaGenome& bro = (SubhaGenome&)*d; (*faccross)(mom.factorstr(), dad.factorstr(), 0, &bro.factorstr()); (*pricross)(mom.priority(), dad.priority(), 0, &bro.priority()); bro._evaluated = gaFalse; n = 1; } return n; } typedef struct _SubhaData { short ** fac; float * pri; } SubhaData; // In this objective function we try to match the pattern in the 2D part of the // genome and match the sequence of values in the binary-to-decimal part of the // genome. The overall score is the sum of both parts. float Objective(GAGenome & g) { SubhaGenome & genome = (SubhaGenome &)g; GA1DArrayGenome & fac = genome.factorstr(); GAListGenome & pri = genome.priority(); int i; short **pattern = ((SubhaData *)g.userData())->fac; float *sequence = ((SubhaData *)g.userData())->pri; float val1=0.0; val1=*pri.head() * fac.gene(0) ; for(i=1; i int GAListGenome::write(ostream & os) const { int *cur, *head; GAListIter tmpiter(*this); if((head=tmpiter.head()) != 0) os << *head << " "; for(cur=tmpiter.next(); cur && cur != head; cur=tmpiter.next()) os << *cur << " "; return os.fail() ? 1 : 0; } // force instantiations for compilers that do not do auto instantiation // for some compilers (e.g. metrowerks) this must come after any // specializations or you will get 'multiply-defined errors when you compile. #if !defined(GALIB_USE_AUTO_INST) #include #include GALIB_INSTANTIATION_PREFIX GAList; GALIB_INSTANTIATION_PREFIX GAListGenome; #endif void ListInitializer(GAGenome & c) { GAListGenome &child=(GAListGenome &)c; while(child.head()) child.destroy(); // destroy any pre-existing list int n=10; child.insert(0,GAListBASE::HEAD); // the head node contains a '0' for(int i=1; i &child1=(GA1DArrayGenome &) c1; int low[10] ={0,2,5,4,5,2,6,3,4,9} ; int high[10] ={10,12,15,14,15,12,16,13,14,19}; for(int i=0;i<10;i++) { child1[i]= GARandomInt(low[i],high[i]) ; } } int main(int argc, char *argv[]) { printf("Hello, world\n"); for(int ii=1; ii