Reputation: 11112
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.
Upvotes: 2
Views: 2286
Reputation: 121971
std::vector
has several constructors and one of the variants accepts the initial number of elements to be stored in the vector
.
Specify the size of the vector
in the population
constructor's initializer list:
population::population(int numberOfPopulation) :
population_number(numberOfPopulation),
chromosome_population(numberOfPopulation)
{
}
Give this approach, the population_number
member variable is unnecessary as it can be obtained by chromosome_population.size()
.
Specifying an initial size on the vector
will mean that it contains numberOfPopulation
null pointers. Before accessing elements in the vector
you need to create objects, in this case using new
. If the elements are copyable and polymorphic behaviour is not required then recommend using vector<chromosome>
instead. If you must use dynamically allocated elements in the vector
then you must allocate first:
chromosome_population[i] = new chromosome();
and remember to delete
when 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 call delete
on 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 of push_back()
is:
chromosome_population.push_back(new chromosome());
If you specify an initial size of the vector
at construction, calling push_back()
will add elements after the initial (null pointers in this case) elements in the vector
.
Upvotes: 6