向量中向量(创建染色体)
Vector inside vector (creating chromosomes)
我正在尝试构建一个遗传算法,它可以采用一定数量的变量(比如 4),并以某种方式使用这些变量,这样你就可以得到 2a + 3b + c*c + d = 16。我意识到有更有效的方法来计算这个,但我想尝试构建一个遗传算法以供以后扩展。
我开始尝试创建 "organisms" 以后可以竞争。我所做的是:
#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;
}
它会填充 number_of_variables 向量,并输出这些,因为这让我清楚地知道它实际上正在做我打算让它做的事情。然而,我想要它做的是用一个变量向量填充每个 "chromosome",这样,例如 0 号染色体将具有值 {1、5、-5、9} 等。
下面的代码显然不起作用,但这是我希望它做的:
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";
}
意味着它会随机填充变量,然后 chromosome1 会取 "variables" 取的那 4 个值,并重复。实际发生的是(我认为)它只从 "variables" 中获取第一个值并将其复制到 "chromosome" 而不是全部 4.
如果有人能提供帮助,我将不胜感激,我意识到这可能只是一个菜鸟错误,在对矢量更有经验的人(这可能是 99% 的人这个网站,哈哈)。
无论如何,谢谢:)
#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;
}
您可以使用以下语法将向量放在向量中:
std::vector<std::vector<int>>
但是您需要使外部向量足够大以 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
}
}
}
我正在尝试构建一个遗传算法,它可以采用一定数量的变量(比如 4),并以某种方式使用这些变量,这样你就可以得到 2a + 3b + c*c + d = 16。我意识到有更有效的方法来计算这个,但我想尝试构建一个遗传算法以供以后扩展。
我开始尝试创建 "organisms" 以后可以竞争。我所做的是:
#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;
}
它会填充 number_of_variables 向量,并输出这些,因为这让我清楚地知道它实际上正在做我打算让它做的事情。然而,我想要它做的是用一个变量向量填充每个 "chromosome",这样,例如 0 号染色体将具有值 {1、5、-5、9} 等。
下面的代码显然不起作用,但这是我希望它做的:
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";
}
意味着它会随机填充变量,然后 chromosome1 会取 "variables" 取的那 4 个值,并重复。实际发生的是(我认为)它只从 "variables" 中获取第一个值并将其复制到 "chromosome" 而不是全部 4.
如果有人能提供帮助,我将不胜感激,我意识到这可能只是一个菜鸟错误,在对矢量更有经验的人(这可能是 99% 的人这个网站,哈哈)。
无论如何,谢谢:)
#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;
}
您可以使用以下语法将向量放在向量中:
std::vector<std::vector<int>>
但是您需要使外部向量足够大以 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
}
}
}