简体   繁体   中英

Vector inside vector (creating chromosomes)

I'm attempting to build a genetic algorithm that can take a certain amount of variables (say 4), and use these in a way so that you could have 2a + 3b + c*c + d = 16. I realise there are more efficient ways to calculate this, but I want to try and build a genetic algorithm to expand later.

I'm starting by trying to create "organisms" that can compete later. What I've done is this:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <random>

// Set population size

const int population_size = 10;
const int number_of_variables = 4;


int main()
{
// Generate random number

std::random_device rd;    
std::mt19937 rng(rd());    // random-number engine (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(-10, 10); 

// Set gene values.

std::vector<int>chromosome;
std::vector<int>variables;


for (int i = 0; i < number_of_variables; ++i)
{
    double rand_num = uni(rng);
    variables.push_back (rand_num);
    std::cout << variables[i] << "\n";
}



return 0;
}

What happens is it will fill up the number_of_variables vector, and output these just because that makes it clear for me that it's actually doing what I intend for it to do. What I want it to do however is to fill up each "chromosome" with one variables vector, so that for example chromosome 0 would have the values {1, 5, -5, 9} etc.

The following code obviously isn't working, but this is what I'd like it to do:

for (int j = 0; j < population_size; ++j)
{
    for (int i = 0; i < number_of_variables; ++i)
    {
        double rand_num = uni(rng);
        variables.push_back(rand_num);
    }

    chromosome.push_back(variables[j]);
    std::cout << chromosome[j] << "\n";
}

Meaning it'd fill up the variables randomly, then chromosome1 would take those 4 values that "variables" took, and repeat. What actually happens is that (I think) it only takes the first value from "variables" and copies that into "chromosome" rather than all 4.

If anyone could help it'd be very much appreciated, I realise this might be simply a rookie mistake that is laughably simply in the eyes of someone more experienced with vectors (which would probably be 99% of the people on this website, hah).

Anyway, thanks :)

#include <iostream>
#include <vector>
#include <random>

// Set population size

const int population_size = 10;
const int number_of_variables = 4;


int main()
{
// Generate random number

std::random_device rd;    
std::mt19937 rng(rd());    // random-number engine (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(-10, 10); 

// Set gene values.

std::vector< std::vector<int>>chromosome;


for( int kp = 0; kp < population_size; kp++ )
{
    std::vector<int>variables;
for (int i = 0; i < number_of_variables; ++i)
{
    double rand_num = uni(rng);
    variables.push_back (rand_num);

}
chromosome.push_back( variables );
}

// display entire population

for( auto c : chromosome )
{
    for( auto v : c )
    {
        std::cout << v << " ";
    }
    std::cout << "\n";
}

// display 4th member of population

for( auto v : chromosone[ 3 ] )
    {
        std::cout << v << " ";
    }
    std::cout << "\n";

return 0;
}

http://ideone.com/2jast J

You can place a vector inside a vector with the syntax:

std::vector<std::vector<int>>

but you will need to make the outer vector large enough for num_variables.

#include <vector>
#include <cstdlib>

using Individual = std::vector<int>;
using Population = std::vector<Individual>;
// short for std::vector<std::vector<int>>;

const size_t number_of_variables = 8;

int main() {
    Population population(10);

    for (auto& individual : population) {
        individual.resize(number_of_variables);
        for (size_t j = 0; j < number_of_variables; ++j) {
            individual[j] = j;  // replace with random number
        }
    }
}

Live demo: http://ideone.com/pfufGt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM