NANA
population.cpp
1#include "../include/GA/population.h"
2#include <cmath>
3#include <algorithm>
4#include <iostream>
5
6
7
8namespace NANA {
9namespace GA {
10
16static double myFun(const double& x) {
17 return x * std::sin(10.0 * NA_PI * x) + 1.0;
18}
19
20
21
22Popultation::Popultation(int groupNumber, int mutate) :
23 m_groupNumber(groupNumber),
24 m_mutate(mutate) {
25 GeneFloat initGene(-1.0, 2.0, 1e-6);
26 m_members.clear();
27 m_result.resize(groupNumber);
28 for (int i = 0; i < groupNumber; ++i) {
29 m_members.push_back(initGene);
31 m_members[i].initGene();
32 }
33}
34
35
36static bool CResultCmp(Popultation::CResult const& A, Popultation::CResult const& B) {
37 return A.val > B.val;
38}
39
40void Popultation::run(int iterNum) {
41
42 while (0 != (iterNum--)) {
44 for (int i = 0; i < m_groupNumber; ++i) {
45 double x = m_members[i].translate();
46 m_result[i].val = myFun(x);
47 m_result[i].index = i;
48 }
49 std::sort(m_result.begin(), m_result.end(), CResultCmp);
50#if 1
51 static int k = 0;
52 if (k++ % 10 == 0) {
54 double x = m_members[m_result[0].index].translate();
55 std::cout << "第"<<k<<"代最优秀的个体:" << x << "当前目标函数值:" << m_result[0].val << std::endl;
56 }
57
58#endif
60 this->choose();
62 this->update();
63 }
64}
65
67{
69 m_lives.clear();
70 m_lives.push_back(m_result[0].index);
72 int s1 = 1;
73 int s2 = m_groupNumber / 10;
74 for (int i = s1; i < s2; ++i) {
75 int randNum = rand() % 100;
77 if (randNum < 90) {
78 m_lives.push_back(m_result[i].index);
79 }
80 }
82 s1 = s2;
83 s2 = m_groupNumber / 30;
84 for (int i = s1; i < s2; ++i) {
85 int randNum = rand() % 100;
87 if (randNum < 70) {
88 m_lives.push_back(m_result[i].index);
89 }
90 }
91 s1 = s2;
92 s2 = m_groupNumber;
94 for (int i = s1; i < s2; ++i) {
95 int randNum = rand() % 100;
97 if (randNum < 50) {
98 m_lives.push_back(m_result[i].index);
99 }
100 }
101}
102
104 int nSize = static_cast<int>(m_lives.size());
106
107 m_oldmembers = m_members;
109 int remainingSpace = m_groupNumber - nSize;
110 int outSize = remainingSpace >> 1;
111 remainingSpace -= outSize;
112 int s1 = 0;
113 int s2 = outSize;
114 for (int i = s1; i < s2; ++i) {
115 m_members[i].initGene();
116 }
118 s1 = s2;
119 s2 += remainingSpace;
120 for (int i = s1; i < s2; ++i) {
121 m_members[i] = m_oldmembers[m_result[i].index];
122 }
124 s1 = s2;
125 s2 = m_groupNumber - 1;
126 int id, fid, mid, temp, j;
127 std::list<int>::iterator iter = m_lives.begin();
128 if (1 == (nSize % 2)) {
129 id = rand() % nSize;
130 for (j = 0; j < (id - 1); j++)
131 iter++;
132 m_lives.erase(iter);
133 --nSize;
134 fid = m_result[id].index;
135 m_members[s1++] = m_oldmembers[fid];
136 }
137
138 do {
139 id = rand() % nSize;
140 iter = m_lives.begin();
141 for (j = 0; j < (id - 1); j++)
142 iter++;
143 m_lives.erase(iter);
144 --nSize;
145 fid = m_result[id].index;
146 nSize = static_cast<int>(m_lives.size());
147 id = rand() % nSize;
148 iter = m_lives.begin();
149 for (j = 0; j < (id - 1); j++)
150 iter++;
151 m_lives.erase(iter);
152 --nSize;
153 mid = m_result[id].index;
154 GeneFloat::cross(m_oldmembers[fid], m_oldmembers[mid], m_members[s1++]);
155 temp = rand() % 100;
156 if (temp < m_mutate) {
157 m_members[s1 - 1].mutate();
158 }
159 //父本做母本,母本做父本
160 GeneFloat::cross(m_oldmembers[mid], m_oldmembers[fid], m_members[s1++]);
161 temp = rand() % 100;
162 if (temp < m_mutate) {
163 m_members[s1 - 1].mutate();
164 }
165 } while (s1 < s2);
166}
167
168
169}
170}
171
172
173
int m_mutate
百分之
Definition: population.h:56
int m_groupNumber
种群数量
Definition: population.h:55
std::vector< CResult > m_result
上一代的成员
Definition: population.h:59
std::list< int > m_lives
活下来的个体
Definition: population.h:60
void update()
更新到下一代
Definition: population.cpp:103
void choose()
天择(从种群中选择优秀的个体,认为是存活下来的个体)
Definition: population.cpp:66
Popultation(int groupNumber, int mutate)
Definition: population.cpp:22
std::vector< GeneFloat > m_members
成员
Definition: population.h:57
void run(int iterNum)
迭代多少次
Definition: population.cpp:40
#define NA_PI
圆周率
Definition: nadef.hpp:35
记录结果和序号
Definition: population.h:28