Hello I’m trying to create a genetic algorithm with C++ and I tried to use vector as the container the problem is I didn’t know how to set the size of the vector because the vector have a class argument like this
class population
{
friend class chromosome;
private:
int population_number;
int best_index[2];
vector <chromosome *> chromosome_population;
public:
population(int numberOfPopulation);
population(int numberOfPopulation,int numberOfSynapse);
~population();
int worst_chromosome();
void mating();
void crossover(int parent_1,int parent_2);
};
this is the population class and here’s the chromosome class
class chromosome
{
friend class population;
private:
int chromosome_id;
float fitness;
vector <gen *> gen_chromosome;
public:
chromosome();
~chromosome();
void fitness_function();
void mutation_translocation();
int get_chromosome_size();
};
how can I set the vector length in the population class constructor? I’ve tried to use vector.pushback and vector.resize but both will give me error because the argument doesn’t match. Actually I’m understand why it become error but I didn’t know how to match the argument inside the vector pushback here’s my population constructor
population::population(int numberOfPopulation)
{
srand (time(NULL));
population_number = numberOfPopulation;
for(int a=0;a<population_number;a++)
{
chromosome_population.push_back();
}
cout<<chromosome_population.size();
for(int i=0;i<population_number;i++)
{
chromosome_population[i]->chromosome_id = i;
int chromosome_length = rand() % 10 + 1;
for(int j=0;j<chromosome_length;j++)
{
chromosome_population[i]->gen_chromosome[j]->basa_biner = rand()%1;
chromosome_population[i]->fitness = (rand()%99)+1;
}
}
}
If is there any other information you want you can tell me in the comment and I’ll add the information you needed. Thanks before.
std::vectorhas several constructors and one of the variants accepts the initial number of elements to be stored in thevector.Specify the size of the
vectorin thepopulationconstructor’s initializer list:Give this approach, the
population_numbermember variable is unnecessary as it can be obtained bychromosome_population.size().Specifying an initial size on the
vectorwill mean that it containsnumberOfPopulationnull pointers. Before accessing elements in thevectoryou need to create objects, in this case usingnew. If the elements are copyable and polymorphic behaviour is not required then recommend usingvector<chromosome>instead. If you must use dynamically allocated elements in thevectorthen you must allocate first:and remember to
deletewhen no longer required.It also desirable to use a form of smart pointer instead of raw pointers. An advantage of using the smart pointer is that when the
vector<unique_ptr<chromosome>>goes out of scope the elements will be destructed for you, without having to explicitly calldeleteon each of the elements. See What C++ Smart Pointer Implementations are available? for a useful listing of the available smart pointers.Note that
vector::push_back()accepts an argument, with same type as its element. So the correct invocation ofpush_back()is:If you specify an initial size of the
vectorat construction, callingpush_back()will add elements after the initial (null pointers in this case) elements in thevector.