1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
// salesgen.cpp 6.3.1994
//
// Diese Modul wurde fuer eine Hausarbeit im Fach
// Objektorietierter Programierung (OOP) an der FH Bielefeld
// unter der Betreuung von Prof. Dr. Bunse erstellt.
//
// Autoren der Hausarbeit : Sven Goethel und Christian Mueller
//
//
// Jegliches Copyright aller Dateien ist im Besitz der Autoren.
// Sven Goethel * Stapenhorststr.35a * 33615 Bielefeld * 0521/139228
// Bielefeld, den 11.3.1994.
#include <climits>
#include "sales_gen.hpp"
#include <gentech/random.hpp>
#include "menge.h"
SalesChromosom::SalesChromosom ( SalesChromosomen & env, const std::string& FileName )
: Chromosom(env, FileName) {}
SalesChromosom::SalesChromosom ( SalesChromosomen & env, size_type StartChromosomLength)
: Chromosom(env, 0)
// Zufaellig erzeugte Chromosomen
{
/* UNIQUE NUKLEONS */
if( StartChromosomLength > (size_type)( env.UserNukleoMaxVal - env.UserNukleoMinVal + 1 ) ) {
std::cerr << "\nFehler !! "
<< "Chromsomenlaenge groesser als Nukleotideauswahl !!";
}
for (size_type i=0; i < StartChromosomLength; ++i) {
NukleoTyp Rand;
while( contains( Rand = Random (env.UserNukleoMinVal, env.UserNukleoMaxVal) ) ) ;
push_back(Rand);
}
assert (size() == StartChromosomLength);
}
SalesChromosomen::SalesChromosomen(
size_type MaxChromosomen,
size_type StartChromosomNumber,
size_type StartChromosomLength,
size_type Nukleotide,
SpliceCodeInfo *ptrSpliceCodeInfo,
size_type InversionFreq,
size_type TranslocationFreq,
size_type AsymXOverFreq,
size_type CrossVal,
size_type MutationFreq,
size_type NoImprovingCrossingOvers )
: Chromosomen ( SalesGame::CODE_MIN_VAL, SalesGame::CODE_MAX_VAL,
MaxChromosomen, 0 /* use SalesChromosomes here */,
StartChromosomLength, Nukleotide, ptrSpliceCodeInfo,
InversionFreq, TranslocationFreq, AsymXOverFreq,
CrossVal, MutationFreq ),
TheGame( 1 ),
NoImproving(1),
NoImprovingCrossingOvers(NoImprovingCrossingOvers),
Flag (0),
WorstDistance (0),
BestDistance (-1)
{
// StartGene zufaellig setzen !
for (int i=StartChromosomNumber ; i > 0 ; i--) {
SalesChromosom Gamma (*this, StartChromosomLength);
assert (Gamma.size() == StartChromosomLength);
push_back(Gamma);
}
assert (size()==StartChromosomNumber);
}
SalesChromosomen::SalesChromosomen(
size_type MaxChromosomen,
const std::string& StartGenFile,
size_type Nukleotide,
SpliceCodeInfo *ptrSpliceCodeInfo,
size_type InversionFreq,
size_type TranslocationFreq,
size_type AsymXOverFreq,
size_type CrossVal,
size_type MutationFreq,
size_type NoImprovingCrossingOvers )
: Chromosomen ( SalesGame::CODE_MIN_VAL, SalesGame::CODE_MAX_VAL,
MaxChromosomen, StartGenFile,
Nukleotide, ptrSpliceCodeInfo,
InversionFreq, TranslocationFreq, AsymXOverFreq,
CrossVal, MutationFreq ),
TheGame( 1 ),
NoImproving(1),
NoImprovingCrossingOvers(NoImprovingCrossingOvers),
Flag (0),
WorstDistance (0),
BestDistance (-1)
{ }
double SalesChromosomen::Fitness (const Chromosom &Lsg)
{
double Distance = TheGame.Play (Lsg, false);
if (Distance > WorstDistance) {
WorstDistance = Distance;
Flag |= 1;
}
assert (WorstDistance != 0);
return (WorstDistance - Distance) / WorstDistance;
}
int SalesChromosomen::Evolution(double GoalFitness, const std::string& chrptrPtkFile,
double BirthRate, int Bigamie, size_type NoImprovingCrossingOvers )
{
(void) GoalFitness;
double CutFitness = 0; // Der Cut beim Sterben
bool stop = false;
InitFitness() ;
if( !chrptrPtkFile.empty() ) {
if ((fileptrPtk=fopen (chrptrPtkFile.c_str(), "wt")) == nullptr) {
INT_ERR (__LINE__);
}
} else {
fileptrPtk = nullptr;
}
EvolutionStart=time(nullptr);
Generation=1;
SplicedChromosoms=0;
IntroCodeLenSum=0;
Protokoll();
if( !Echo() ) { stop = true; }
if( BirthRate <= 0.0 || BirthRate > 1.0 ) { stop=1; }
while( NoImproving < NoImprovingCrossingOvers && !stop )
{
Generation++;
SplicedChromosoms=0;
IntroCodeLenSum=0;
if( BirthRate < 1.0 ) {
NewGeneration (BirthRate, Bigamie);
// Fuer LetDie !!!
CutFitness = GetXWorstFitness (size()-MaxChromosomen);
} else {
NewGeneration (Bigamie);
CutFitness = -.5; // Eltern Fitness auf -1 gesetzt
}
// Sterben !!!
LetDie(CutFitness);
// Die Mutation
Mutation();
// Fitness berechnen. Vorher Abfrage ob sich 'WorstDistance' geaendert
// hat. Wenn ja, muss die Fitness fuer die gesammte Population berechnet
// werden
if( Flag & 1 ) {
InitFitness();
Flag &= ~1;
} else {
CalcWholeFitness();
}
// Weitere Mutationen
InversionsMutation();
TranslocationsMutation();
GenerationEnd=time(nullptr);
if( Flag & 2 ) {
NoImproving=0;
Flag &= ~2;
} else {
NoImproving++ ;
}
Protokoll();
if( !Echo() ) { stop=1; }
}
EvolutionEnd=time(nullptr);
Echo();
Protokoll();
if (fileptrPtk != nullptr) { fclose(fileptrPtk); }
TheGame.Play(TheBestEver, true);
return Generation;
}
void SalesChromosomen::validate(const Chromosom &c ) {
// Ueberpruefung, ob jedes Element nur einmal vorhanden ist
Menge<NukleoTyp> test;
for (size_type j = 0; j < c.size(); j++) {
test.insert(c[j]);
}
if( test.card() != c.size() ) {
std::cout << "\ndest:\n" << c
<< std::endl << "test.size() = " << test.card()
<< std::endl;
}
assert (test.card() == c.size());
}
/* UNIQUE NUKLEOTIDS */
void SalesChromosomen::CreateNewSymChromosom (Chromosom &dest, size_type m, size_type w,
SortListe<size_type, size_type>& CrossPoints)
// Programiert nach Pseudocode aus:
// Eberhard Schoeneburg, Frank Heinzmann, Sven Feddersen:
// Genetische Algorithmen und Evolutionsstrategien,
// Addison-Wesley, ISBN 3-89319-493-2,
// Seite 274/275
{
// i : Indize des Crosspoints
// von, bis : Zu Uebertragender Chromosomenabschnitt [von..bis[
// ch : Alternierendes Indize zwischen den beiden Chromosomen
// mit den Geschlechtern 'm' und 'w'
// done : Abbruch Indikator
bool done = false;
size_type i, von, bis;
// Startwerte !!
i = bis = 0;
dest = THIS[m];
# ifndef NDEBUG
if (THIS[w].size() != THIS[m].size() ||
THIS[w].size() != dest.size())
std::cout << std::endl << "THIS[w].size() = " << THIS[w].size()
<< std::endl << "THIS[m].size() = " << THIS[m].size()
<< std::endl << "dest.size() = " << dest.size();
# endif
do {
// An dem letzten exklusiven Ende fortfahren
von = bis;
if (i < CrossPoints.size()) {
// Chromosomenabschnitt-Ende holen.
bis = CrossPoints[i];
i++;
} else {
// ...und den Rest uebertragen
bis = THIS[m].size();
done=true;
}
if (i % 2) {
size_type n;
Liste<NukleoTyp> intervall;
assert (intervall.size() == 0);
// Gene im Intervall puffern
for (n = von; n < bis; n++)
intervall.push_back( THIS[w][n] );
// Gene aus 'w1' in 'dest' suchen und loeschen
for (n = 0; n < dest.size() && intervall.size() > 0; n++)
{
size_type l = 0;
while( l < intervall.size() ) {
if (intervall[l] == dest[n]) {
dest.erase(n);
intervall.erase(l);
l = 0; // Neuen Suchdurchlauf initiieren
} else {
++l;
}
}
}
assert (intervall.size() == 0);
// Intervall von 'dest' mit Genen von 'w' fuellen
for (n = von; n < bis; n++) {
dest.insert(n, THIS[w][n]);
}
}
} while( !done );
#ifndef NDEBUG
validate( dest );
if (dest.size() != THIS[w].size() ) {
std::cout << "\ndest:\n" << dest
<< std::endl << "THIS[m].size() = " << THIS[m].size()
<< std::endl << "THIS[w].size() = " << THIS[w].size()
<< std::endl;
}
assert ( dest.size() == THIS[m].size() &&
dest.size() == THIS[w].size() );
#endif
}
/* UNIQUE NUKLEOTIDS */
void SalesChromosomen::CrossingOver (size_type m, size_type w) noexcept
// Order Crossing Over
//
{
if (CrossVal == 0) { return; }
// Symmetrisches XOver !!!
SortListe<size_type, size_type> CrossPoints;
assert (CrossPoints.size()==0);
// Kreuzungspunkte sortiert eintragen.
for (size_type i = 0; i < CrossVal; ++i) {
CrossPoints.insert( Random (0 , THIS[w].size()));
}
SalesChromosom NeuA (*this);
SalesChromosom NeuB (*this);
int SplicedCode;
CreateNewSymChromosom (NeuA, m, w, CrossPoints);
push_back( NeuA );
SplicedCode=NeuA.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
IntroCodeLenSum+=SplicedCode;
}
// Die Fitness des gespleissten Chromosomes in das ungespleisste
// eingebundene Chromosom einsetzen !!!
THIS[size()-1].SetFitness(Fitness(NeuA));
CreateNewSymChromosom (NeuB, w, m, CrossPoints);
push_back( NeuB );
SplicedCode=NeuB.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
IntroCodeLenSum+=SplicedCode;
}
THIS[size()-1].SetFitness(Fitness(NeuB));
}
void SalesChromosomen::Mutation()
{
static size_type next_nukleotide_idx = MutationFreq + Random( 0, (size_type)(MutationFreq/MutationFreqVar) ) ;
MutationsThisGeneration=0;
if (MutationFreq > 0) {
size_type nukleotide_idx = next_nukleotide_idx;
for (size_type chromosom_idx = 0; chromosom_idx < size(); ++chromosom_idx) {
bool mutated = false;
const size_type chromosom_len = THIS[chromosom_idx].size();
while( nukleotide_idx < chromosom_len-1 ) {
// Die Move-Mutation.
/*
// Ungewichtete Mutation
while ((Rand = Random (0, THIS[i].size()))
== NukleotidsPassedInChromosom-1);
THIS[i].fuegeEin (THIS[i][NukleotidsPassedInChromosom-1], Rand);
if (Rand > NukleotidsPassedInChromosom)
THIS[i].loesche (NukleotidsPassedInChromosom-1);
else
THIS[i].loesche (NukleotidsPassedInChromosom);
*/
// Gewichtete Mutation
// MutationsStaerke proportional zur Stagnation der Besten Fitness !!!
size_type MutationStrength = (size_type) (
( (double)NoImproving * chromosom_len ) /
(double)(NoImprovingCrossingOvers << 1) );
// mindestens einen...
MutationStrength = std::max<size_type>(3, MutationStrength);
size_type pos;
{
size_type l_pos, h_pos; // clipped to chromosom_len
if( MutationStrength <= nukleotide_idx ) {
l_pos = nukleotide_idx - MutationStrength;
if( nukleotide_idx + MutationStrength < chromosom_len ) {
h_pos = nukleotide_idx + MutationStrength;
} else {
h_pos = chromosom_len - 1;
}
} else {
l_pos = nukleotide_idx + 1;
if( nukleotide_idx + 2 * MutationStrength < chromosom_len ) {
h_pos = nukleotide_idx + 2 * MutationStrength;
} else {
h_pos = chromosom_len - 1;
}
}
do {
pos = Random (l_pos, h_pos);
} while( nukleotide_idx == pos );
}
const NukleoTyp n = THIS[chromosom_idx][pos];
THIS[chromosom_idx][pos] = THIS[chromosom_idx][nukleotide_idx];
THIS[chromosom_idx][nukleotide_idx] = n;
MutationsThisGeneration++;
mutated = true;
nukleotide_idx += MutationFreq + Random( 0, (size_type)(MutationFreq/MutationFreqVar) );
}
if( nukleotide_idx >= chromosom_len-1 ) {
nukleotide_idx -= chromosom_len-1;
}
if( mutated ) {
// Neue Fitness berechnen
Chromosom Neu( THIS[chromosom_idx] );
Neu.Splicing();
THIS[chromosom_idx].SetFitness(Fitness(Neu));
#ifndef NDEBUG
validate( THIS[chromosom_idx] );
#endif
} else {
}
}
next_nukleotide_idx = nukleotide_idx;
}
}
bool SalesChromosomen::Echo() const
{
if (Generation == 1 ) {
printf(" Generation: BestGeneration: AverageDistance: BestDistance:"
" WorstDistance:\n");
}
printf("\r%11zu%16zu%16ld%16ld%16ld",
(size_t)GetGeneration(), (size_t)TheBestEversGeneration,
(long)(GetWorstDistance()-GetAverageFitness()*GetWorstDistance()),
(long)GetBestDistance(), (long)GetWorstDistance() );
if (EvolutionEnd > 0) {
if (Generation > 1) {
printf ("\n\nAvrg. Generationsdauer : %f s / Generation\n",
((double)(EvolutionEnd-EvolutionStart))/((double)(GetGeneration()-1)) );
}
printf ("\n\nGenerationen / Evolutionsdauer : %3zu / %3zu s\n",
(size_t)Generation, (size_t)(EvolutionEnd-EvolutionStart));
}
return true;
}
void SalesChromosomen::CalcWholeFitness()
{
double Total=0, TempFitness;
size_type BestChrom=npos, ChromLen;
size_type ChromLenSum=0;
ChromosomenLenMin = std::numeric_limits<size_type>::max();
ChromosomenLenMax = std::numeric_limits<size_type>::min();
BestFitness = -1;
ChromBetterZeroNumber = 0;
for (size_type i = 0; i < size(); ++i) {
ChromLen = THIS[i].size();
ChromLenSum += ChromLen;
if( ChromosomenLenMin > ChromLen ) { ChromosomenLenMin=ChromLen; }
if( ChromosomenLenMax < ChromLen ) { ChromosomenLenMax=ChromLen; }
if( ( TempFitness = THIS[i].GetFitness() ) > 2*std::numeric_limits<double>::epsilon() ) {
ChromBetterZeroNumber++ ;
}
Total += TempFitness ;
if( GetBestDistance() > 0.5 + WorstDistance - TempFitness * WorstDistance ||
GetBestDistance() < 0.0 )
{
BestFitness = TempFitness;
BestChrom = i;
if( !(TheBestEver == THIS[BestChrom]) ) {
TheBestEver = THIS[BestChrom];
TheBestEversGeneration = Generation;
TheBestEver.Splicing();
Flag |= 2; // NoImproving to ZERO
}
BestDistance = WorstDistance - TempFitness * WorstDistance;
}
}
AverageFitness = Total / size();
FitnessSum = Total;
ChromosomenLenAvrg = (double)ChromLenSum/(double)size();
}
void SalesChromosomen::Protokoll()
{
double SpliceCodePerChrom ;
SpliceCodePerChrom = (SplicedChromosoms > 0) ?
((double)IntroCodeLenSum/(double)SplicedChromosoms)
: 0.0
;
if (fileptrPtk != nullptr) {
if (EvolutionEnd == 0) {
fprintf (fileptrPtk, "=======================================================\n\n\n");
fprintf (fileptrPtk, "\nGeneration / Generierungsdauer : %3zu / %zu s\n",
(size_t)Generation, (size_t)(GenerationEnd-GenerationStart) );
fprintf (fileptrPtk, "Populationsgroesse : %3zu\n",
(size_t)size());
fprintf (fileptrPtk, "Chromosomen Laenge Minimum : %3zu\n",
(size_t)ChromosomenLenMin);
fprintf (fileptrPtk, "Chromosomen Laenge Maximum : %3zu\n",
(size_t)ChromosomenLenMax);
fprintf (fileptrPtk, "Chromosomen Laenge Durchschnitt : %10.6lf\n",
ChromosomenLenAvrg);
fprintf (fileptrPtk, "Gespleisste Chromosomen : %3zu\n",
(size_t)SplicedChromosoms);
fprintf (fileptrPtk, "Gespleisstes Code pro Chromosom : %10.6lf\n",
SpliceCodePerChrom);
fprintf (fileptrPtk, "\nBestFitness : %10.6lf\n",
BestFitness);
fprintf (fileptrPtk, "Average Fitness : %10.6lf\n",
AverageFitness);
fprintf (fileptrPtk, "\nBest Distance : %10.0lf\n",
GetBestDistance());
fprintf (fileptrPtk, "Average Distance : %10ld\n",
(long)(GetWorstDistance()-GetAverageFitness()*GetWorstDistance()));
fprintf (fileptrPtk, "Worst Distance : %10.0lf\n",
GetWorstDistance());
fprintf (fileptrPtk, "\nMutationen dieser Generation : %3zu\n",
(size_t)MutationsThisGeneration);
fprintf (fileptrPtk, "Inversionen dieser Generation : %3zu\n",
(size_t)InversionsThisGeneration);
fprintf (fileptrPtk, "Translokationen dieser Generation : %3zu\n",
(size_t)TranslocationsThisGeneration);
fprintf (fileptrPtk, "\nTheBestEverFitness : %10.6lf\n",
TheBestEver.GetFitness());
fprintf (fileptrPtk, "TheBestEversAverageFitness : %10.6lf\n",
TheBestEversAverageFitness);
fprintf (fileptrPtk, "TheBestEversGeneration : %3zu\n\n",
(size_t)TheBestEversGeneration);
} else {
if(Generation>1) {
fprintf (fileptrPtk, "Avrg. Generationsdauer : %f s / Generation\n",
((double) (EvolutionEnd-EvolutionStart))/((double)(GetGeneration()-1)) );
}
fprintf (fileptrPtk, "\nGenerationen / Evolutionsdauer : %3zu / %zu s\n",
(size_t)Generation, (size_t)(EvolutionEnd-EvolutionStart));
}
}
}
|