2016-04-27 6 views
1

私はある量の変数(例えば4)をとることができる遺伝的アルゴリズムを構築しようとしており、2a + 3b + c * c + d = 16これを計算するより効率的な方法があることは分かっていますが、後で拡張する遺伝的アルゴリズムを構築してみたいと思います。ベクター内のベクター(染色体を作成する)

私は、後で競合する「生物」を作り出すことから始めます。たまたまそれがnumber_of_variablesベクトルをいっぱいになり、出力はこれらだけでは、実際にそれが何をするために私が意図するものをやっていることを私のためにそれが明確になるため何

#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; 
} 

:私がやったことはこれです。しかし、私がしたいことは、各染色体を1つの変数ベクトルで塗りつぶすことです。例えば染色体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は、「変数」を取ったこと、これらの4つの値を取り、繰り返します、ランダム変数を埋めるだろう意味。実際に起こることは、「変数」から最初の値しか取らず、すべてを4ではなく「染色体」にコピーすることです。

誰かが非常に感謝してくれるのであれば、単にベクトル(これはおそらくこのウェブサイト上の人々の99%になるでしょう)で経験豊かな人の目に見えて単純にルーキーなミスであるかもしれません。とにかく

、感謝:)

+0

あなただけの2次元配列をお探しですか? 'std :: vector >'?また、変数は 'ベクトル'である必要がありますか? –

+0

2D配列を読んで、それはそうかもしれないよ!私は数式を使えば、2a = 3も得ることができます。だから私は二重はそれのために必要であると信じている、いいえ? – Milan

答えて

1
#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

+0

ありがとう、それはまさに私が意味するものです!私はそれを理解しています。もう一度ありがとう、それは私に頭痛の多くを救った。 – Milan

+0

さて、私はそれを完全に理解していると思ったが、明らかにそうではなかった。あなただけの例[4]のように表示したいすべてのベクトルを表示するのではなく、どうしますか?私はstd :: cout << individual [4]と思った。それは明らかに効き目がないのです。: – Milan

+0

個人で[4] = chromsome [4]途中で個人的に名前を付けるのが理にかなっています。 – Milan

1

あなたは構文で、ベクター内のベクトルを置くことができます。

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 
     } 
    } 
} 

ライブデモ:http://ideone.com/pfufGt

関連する問題