Algoritmi genetici pentru rezolvarea problemelor prin - Sorin ...
Algoritmi genetici pentru rezolvarea problemelor prin - Sorin ...
Algoritmi genetici pentru rezolvarea problemelor prin - Sorin ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Population.cpp<br />
84<br />
85<br />
86<br />
87<br />
88<br />
89<br />
90<br />
91<br />
92<br />
93<br />
94<br />
95<br />
96<br />
97<br />
98<br />
99<br />
100<br />
101<br />
102<br />
103<br />
104<br />
105<br />
106<br />
107<br />
108<br />
109<br />
110<br />
111<br />
112<br />
113<br />
114<br />
115<br />
116<br />
117<br />
118<br />
119<br />
120<br />
121<br />
122<br />
123<br />
124<br />
125<br />
126<br />
127<br />
128<br />
129<br />
130<br />
131<br />
132<br />
133<br />
134<br />
135<br />
136<br />
137<br />
138<br />
139<br />
140<br />
141<br />
142<br />
143<br />
144<br />
145<br />
146<br />
147<br />
148<br />
149<br />
150<br />
151<br />
152<br />
153<br />
154<br />
155<br />
156<br />
157<br />
158<br />
159<br />
160<br />
161<br />
162<br />
163<br />
164<br />
165<br />
166<br />
Population::Population(const Population& population):<br />
_ga(population._ga)<br />
{<br />
operator=(population);<br />
};<br />
const Population& Population::operator=(const Population& population)<br />
{<br />
if (this != &population)<br />
{<br />
_best_genome = population._best_genome;<br />
const_cast (_ga) = population._ga;<br />
_genomes = population._genomes;<br />
_population_size = population._population_size;<br />
_next_generation = population._next_generation;<br />
assert (_ga == population._ga);<br />
};<br />
};<br />
for (int i = 0; i < population._genomes.size(); i++)<br />
{<br />
const_cast (_genomes.at(i)._population) = this;<br />
};<br />
return (*this);<br />
void Population::pre_step()<br />
{<br />
assert (NULL == _next_generation);<br />
_next_generation = new Population(NULL, _population_size, Genome::GENOME_ZERO);<br />
assert (NULL != _next_generation);<br />
assert(_genomes.size() == _population_size);<br />
typedef pair DOUBLE2INT;<br />
vector d2i;<br />
d2i.reserve(_genomes.size());<br />
for (int i = 0; i < _genomes.size(); i++)<br />
{<br />
d2i.push_back(make_pair(_genomes.at(i).getGAFitness(), i));<br />
};<br />
vector::iterator i1 = d2i.begin();<br />
vector::iterator i2 = d2i.end();<br />
sort (i1, i2, greater());<br />
double max_sum = 0;<br />
for (i = 0; i < _genomes.size(); i++)<br />
{<br />
max_sum += d2i.at(i).first;<br />
};<br />
int k = 0;<br />
double sum = 0;<br />
for (i = 0; (i < _population_size) && (k < _population_size); i++)<br />
{<br />
sum += d2i.at(i).first;<br />
const int delta = int(1 + double(sum * _population_size) / max_sum) - k;<br />
assert (0 _genomes.at(j) = _genomes.at(d2i.at(i).second);<br />
};<br />
};<br />
Page 2 of 3