#### Archived

This topic is now archived and is closed to further replies.

# GA in a more literal sense

This topic is 5638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am writing a GA for a class that I am not sure will keep its current chromasome. Is there any way in C++ to automatically generate a chromosome from a class? e.g.: [cpp] class Crud { unsigned int a; unsigned char b; public: std::vector<bool> GenerateChromasome(); }; ... std::vector<bool> Crud::GenerateChromasome() { unsigned int i; unsigned int temp_a = a; unsigned char temp_b = b; std::vector<bool> chroma; for(i = 0; i < sizeof(unsigned int); ++i) { chroma.push(temp_a%2); temp_a/=2; } for(i = 0; i < sizeof(unsigned char); ++i) { chroma.push(temp_b%2) temp_b/=2; } } [/cpp] But I want the chromasome function generated. Is this possible through templates or inheritance? I don''t see a way apart from running scripts on my code. :-/ Thanks a lot for your help. -flang

##### Share on other sites
What exactly do you mean? Is your problem that it changes every time it''s called? (I guessed that from your "not sure will keep its current chromosome" line.) If so, just move the chroma object into the class and check that it''s empty before filling it - that way it''ll only be generated once for each instance of the class. Better still, initialise it in the constructor.

If you really want to evolve the code within the GenerateChromasome function you''d need to use some sort of scripting, although to be honest I can''t see an application where you''d need that.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

##### Share on other sites
I''m not sure I understand what you mean. Could you clarify?

You code seems a bit odd too. You''re looping from 0 to the size of a char, which is 1. Always will be 1. You run this loop 4 times.. Why not just say (i = 0; i < 4) and fill in the code?

I''m quite curious about what you''re trying though.

Cheers,
Will

##### Share on other sites
I suspect that what you mean is (and please correct me if I''m wrong)...

That during the course of running your GA you want to vary either a) the length of the chromosome; b) the encoding alphabet of the chromosome; or c) both.

This would constitute a dynamic encoding scheme for your objective function. Perhaps you could provide more details about your optimisation/search problem so that we could better assist you in determining the appropriate methodology for a dynamic encoding.

Cheers,

Timkin

##### Share on other sites
Well spotted mistake in the sizeof... :-(

What I meant was that I would like to have a way of easily programming Chromasomes. Since the process of breeding is pretty simple on the top level, it seems to me that the breeding should be something quite easy to program. Or rather, should be made easy. So instead of what I initially put, it would be ''nice'' to have something akin to:

class something
{
private:
std::string name;
unsigned int id;

private genes:
unsigned int mass;
unsigned int volume;
unsigned int colour;
};

where ''genes'' is a bit like QT''s ''slot'' and ''signal'' so it has code generated for encoding and decoding chromasomes, as well as crossing.

Would this be:
a.) a good idea
b.) airly straight forward

Thanks a bunch..
-flang

##### Share on other sites
Here''s one way:

  //------------------------------------------------------------------------// When creating your genes, inherit from this base class to make sure// the interface is acceptable. // Also make sure the default constructor for a gene creates a ''random'' gene. //------------------------------------------------------------------------class GeneBase{public:    virtual void Mutate()=0;  virtual bool operator==(const GeneBase &rhs) const = 0;  virtual ~GeneBase(){} };template<class Gene>std::vector<Gene> CreateRandomChromosome(const int length){  std::vector<Gene> chromosome;  for (int gene=0; gene<length; ++gene)  {    chromosome.push_back(Gene());  }  return chromosome}

ai-junkie.com

##### Share on other sites
I already have something resembling that... Which is reassuring since it means I am on the right track.

Timkin: I am not really searching anything, but trying out a dynamic breeding system for my own personal pokemon/tomagotchi game.

And a learning experience (read: I don''t intend to polish it or make it ''good'' -unless I show it to friends and they go spastic and think its the coolest thing ever and demand I polish it up and distrobute it.)

##### Share on other sites
You could always consider making a structure for an individual chromosome, and then creating a doubly-linked list to store Chromosomes.

Write Add, Remove, Move, and Insert functions for the linked list, and you''re done.

Will

##### Share on other sites
But that wouldn''t at all generate chromosomes based on class information.

##### Share on other sites
quote:
Original post by flangazor

where ''genes'' is a bit like QT''s ''slot'' and ''signal'' so it has code generated for encoding and decoding chromasomes, as well as crossing.

Each of the genes should not contain information about how to encode or decode the chromosome, unless you want to dedicate a particular gene to that function.

Either, your chromosome class should contain methods for encoding or decoding the information within the class structure, or the chromosome class should look for a particular gene and encode/decode the information in the chromosome according to directions from this structure gene.

However, this latter approach really isn''t necessary for a GA, even one for the purpose you have described (artificial breeding). You can do everything just as well by hiding this information within the chromosome class.

Cheers,

Timkin

• 9
• 16
• 9
• 13
• 41