diff options
author | Sven Gothel <[email protected]> | 2022-11-11 02:15:37 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-11-11 02:15:37 +0100 |
commit | 485df55b7b0a65b0dd195a933ecafbc910e65e8f (patch) | |
tree | 3b85b65bfc35bf34b0378ada9935c0103a68c2a9 /src | |
parent | e499976bec9779f63667c568d2938b441e634845 (diff) |
Cleanup #3: Align Liste<> for std::vector<>; Misc fixes related to Liste<> usage
Misc fixes related to Liste<> usage
- Splicing() size_type erase loop
- Translocation() 'save' was stored in reverse order
Diffstat (limited to 'src')
-rw-r--r-- | src/gen-salesman/sales.cpp | 4 | ||||
-rw-r--r-- | src/gen-salesman/sales_gen.cpp | 42 | ||||
-rw-r--r-- | src/gen-salesman/sales_gen.hpp | 233 | ||||
-rwxr-xr-x | src/gentech/gentech.cpp | 98 | ||||
-rw-r--r-- | src/jaulibs/dblmenge.cpp | 2 | ||||
-rw-r--r-- | src/jaulibs/hashstr.cpp | 6 | ||||
-rw-r--r-- | src/jaulibs/liste.h | 141 | ||||
-rw-r--r-- | src/jaulibs/menge.h | 33 | ||||
-rw-r--r-- | src/jaulibs/sortlist.h | 29 |
9 files changed, 318 insertions, 270 deletions
diff --git a/src/gen-salesman/sales.cpp b/src/gen-salesman/sales.cpp index b1506e6..44df091 100644 --- a/src/gen-salesman/sales.cpp +++ b/src/gen-salesman/sales.cpp @@ -111,7 +111,7 @@ double SalesGame::Play(const Chromosom& Lsg, bool Gfx) const Chromosom::size_type ChromPos = 1;
double result = 0;
- for ( ;ChromPos < Lsg.laenge(); ChromPos++) {
+ for ( ;ChromPos < Lsg.size(); ChromPos++) {
if( Lsg[ChromPos] > Lsg[ChromPos-1] ) {
result += lineno[ Lsg[ChromPos-1] ][ Lsg[ChromPos] - Lsg[ChromPos-1] - 1 ];
} else {
@@ -127,7 +127,7 @@ double SalesGame::Play(const Chromosom& Lsg, bool Gfx) const } else {
// Protokolliert den zurueckgelegten Weg
std::cout << std::endl << std::endl << "Die Loesung: " << std::endl;
- for (Chromosom::size_type i = 1, ChromPos = 0; ChromPos < Lsg.laenge(); ChromPos++, i++)
+ for (Chromosom::size_type i = 1, ChromPos = 0; ChromPos < Lsg.size(); ChromPos++, i++)
{
std::cout << names[Lsg[ChromPos]] << " -> ";
if (! (i % 4)) std::cout << std::endl;
diff --git a/src/gen-salesman/sales_gen.cpp b/src/gen-salesman/sales_gen.cpp index 8b3c0ad..16cfb8b 100644 --- a/src/gen-salesman/sales_gen.cpp +++ b/src/gen-salesman/sales_gen.cpp @@ -32,9 +32,9 @@ SalesChromosom::SalesChromosom ( SalesChromosomen & env, size_type StartChromoso << "Chromsomenlaenge groesser als Nukleotideauswahl !!";
}
for (size_type i=0; i < StartChromosomLength; ++i) {
- long Rand;
- while (istElement (Rand = Random (env.UserNukleoMinVal, env.UserNukleoMaxVal)) );
- fuegeEin (Rand);
+ NukleoTyp Rand;
+ while( contains( Rand = Random (env.UserNukleoMinVal, env.UserNukleoMaxVal) ) ) ;
+ push_back(Rand);
}
assert (size() == StartChromosomLength);
}
@@ -67,7 +67,7 @@ SalesChromosomen::SalesChromosomen( for (int i=StartChromosomNumber ; i > 0 ; i--) {
SalesChromosom Gamma (*this, StartChromosomLength);
assert (Gamma.size() == StartChromosomLength);
- fuegeEin (Gamma);
+ push_back(Gamma);
}
assert (size()==StartChromosomNumber);
}
@@ -110,7 +110,7 @@ double SalesChromosomen::Fitness (const Chromosom &Lsg) }
int SalesChromosomen::Evolution(double GoalFitness, const std::string& chrptrPtkFile,
- double BirthRate, int Bigamie )
+ double BirthRate, int Bigamie, size_type NoImprovingCrossingOvers )
{
(void) GoalFitness;
double CutFitness = 0; // Der Cut beim Sterben
@@ -195,7 +195,7 @@ void SalesChromosomen::validate(const Chromosom &c ) { Menge<NukleoTyp> test;
for (size_type j = 0; j < c.size(); j++) {
- test.fuegeEin (c[j]);
+ test.insert(c[j]);
}
if( test.card() != c.size() ) {
std::cout << "\ndest:\n" << c
@@ -254,7 +254,7 @@ void SalesChromosomen::CreateNewSymChromosom (Chromosom &dest, size_type m, size // Gene im Intervall puffern
for (n = von; n < bis; n++)
- intervall.fuegeEin (THIS[w][n], intervall.size());
+ intervall.push_back( THIS[w][n] );
// Gene aus 'w1' in 'dest' suchen und loeschen
for (n = 0; n < dest.size() && intervall.size() > 0; n++)
@@ -262,8 +262,8 @@ void SalesChromosomen::CreateNewSymChromosom (Chromosom &dest, size_type m, size size_type l = 0;
while( l < intervall.size() ) {
if (intervall[l] == dest[n]) {
- dest.loesche (n);
- intervall.loesche (l);
+ dest.erase(n);
+ intervall.erase(l);
l = 0; // Neuen Suchdurchlauf initiieren
} else {
++l;
@@ -274,7 +274,7 @@ void SalesChromosomen::CreateNewSymChromosom (Chromosom &dest, size_type m, size // Intervall von 'dest' mit Genen von 'w' fuellen
for (n = von; n < bis; n++) {
- dest.fuegeEin (THIS[w][n], n);
+ dest.insert(n, THIS[w][n]);
}
}
} while( !done );
@@ -294,7 +294,7 @@ void SalesChromosomen::CreateNewSymChromosom (Chromosom &dest, size_type m, size }
/* UNIQUE NUKLEOTIDS */
-void SalesChromosomen::CrossingOver (size_type m, size_type w)
+void SalesChromosomen::CrossingOver (size_type m, size_type w) noexcept
// Order Crossing Over
//
{
@@ -306,7 +306,7 @@ void SalesChromosomen::CrossingOver (size_type m, size_type w) // Kreuzungspunkte sortiert eintragen.
for (size_type i = 0; i < CrossVal; ++i) {
- CrossPoints.fuegeEin( Random (0 , THIS[w].size()));
+ CrossPoints.insert( Random (0 , THIS[w].size()));
}
SalesChromosom NeuA (*this);
@@ -314,7 +314,7 @@ void SalesChromosomen::CrossingOver (size_type m, size_type w) int SplicedCode;
CreateNewSymChromosom (NeuA, m, w, CrossPoints);
- fuegeEin (NeuA, size());
+ push_back( NeuA );
SplicedCode=NeuA.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
@@ -324,7 +324,7 @@ void SalesChromosomen::CrossingOver (size_type m, size_type w) // eingebundene Chromosom einsetzen !!!
THIS[size()-1].SetFitness(Fitness(NeuA));
CreateNewSymChromosom (NeuB, w, m, CrossPoints);
- fuegeEin (NeuB, size());
+ push_back( NeuB );
SplicedCode=NeuB.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
@@ -384,13 +384,9 @@ void SalesChromosomen::Mutation() pos = Random (l_pos, h_pos);
} while( nukleotide_idx == pos );
}
- THIS[chromosom_idx].fuegeEin( THIS[chromosom_idx][nukleotide_idx], pos );
- if( pos <= nukleotide_idx ) {
- THIS[chromosom_idx].loesche (nukleotide_idx+1);
- } else {
- THIS[chromosom_idx].loesche (nukleotide_idx);
- }
- assert( THIS[chromosom_idx].size() == chromosom_len );
+ 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;
@@ -414,7 +410,7 @@ void SalesChromosomen::Mutation() }
}
-int SalesChromosomen::Echo()
+bool SalesChromosomen::Echo() const
{
if (Generation == 1 ) {
printf(" Generation: BestGeneration: AverageDistance: BestDistance:"
@@ -434,7 +430,7 @@ int SalesChromosomen::Echo() printf ("\n\nGenerationen / Evolutionsdauer : %3zu / %3zu s\n",
(size_t)Generation, (size_t)(EvolutionEnd-EvolutionStart));
}
- return 1;
+ return true;
}
void SalesChromosomen::CalcWholeFitness()
diff --git a/src/gen-salesman/sales_gen.hpp b/src/gen-salesman/sales_gen.hpp index 17123a6..27fba83 100644 --- a/src/gen-salesman/sales_gen.hpp +++ b/src/gen-salesman/sales_gen.hpp @@ -23,129 +23,126 @@ class SalesChromosomen ;
class SalesChromosom : public Chromosom {
+ public:
+ // Der Copykonstruktor ...
+ SalesChromosom (const Chromosom& a )
+ : Chromosom(a) {}
- public:
- // Der Copykonstruktor ...
- SalesChromosom (const Chromosom& a )
- : Chromosom(a) {}
+ // Der Konstruktor fuer die Konstruktion eines Chromosomes
+ SalesChromosom ( SalesChromosomen & env, size_type StartChromosomLength=0 ) ;
- // Der Konstruktor fuer die Konstruktion eines Chromosomes
- SalesChromosom ( SalesChromosomen & env, size_type StartChromosomLength=0 ) ;
-
- // Der Konstruktor zum einlesen eines gespeicherten Chromosomes !
- // Die Fitness wird hier nicht berechnet !!!
- SalesChromosom ( SalesChromosomen & env, const std::string& FileName );
+ // Der Konstruktor zum einlesen eines gespeicherten Chromosomes !
+ // Die Fitness wird hier nicht berechnet !!!
+ SalesChromosom ( SalesChromosomen & env, const std::string& FileName );
};
class SalesChromosomen : public Chromosomen {
-
- friend class SalesChromosom;
-
- public:
- using Chromosomen::size_type;
-
- // Der Konstruktor mit allen Einstellungen zum Loesen des Problems.
- SalesChromosomen(
- size_type MaxChromosomen,
- size_type StartChromosomNumber,
- size_type StartChromosomLength,
- size_type Nukleotide=3,
- SpliceCodeInfo *ptrSpliceCodeInfo=nullptr,
- size_type InversionFreq=0,
- size_type TranslocationFreq=0,
- size_type AsymXOverFreq=0,
- size_type CrossVal=1,
- size_type MutationFreq=10000, // [1]
- size_type NoImprovingCrossingOvers=100
- ) ;
- SalesChromosomen(
- size_type MaxChromosomen,
- const std::string& StartGenFile,
- size_type Nukleotide=3,
- SpliceCodeInfo *ptrSpliceCodeInfo=nullptr,
- size_type InversionFreq=0,
- size_type TranslocationFreq=0,
- size_type AsymXOverFreq=0,
- size_type CrossVal=1,
- size_type MutationFreq=10000,
- size_type NoImprovingCrossingOvers=100
- ) ;
- // Die Protokoll-Funktion zur Laufzeit
- virtual int Echo();
-
- // Protokolliere
- void Protokoll ( void ) ;
-
- // Beschreibung der Parameter s. "gentech.h"
- virtual int Evolution (
- double GoalFitness, const std::string& chrptrPtkFile,
- double BirthRate=0.6, int Bigamie=0
- );
- // Fitness fuer das uebergebene Chromosom.
- // Wert [0..1] !
- double Fitness(const Chromosom&);
- void CalcWholeFitness() ;
- double GetWorstDistance() const { return WorstDistance; }
- double GetBestDistance() const { return BestDistance; }
-
- // Und das Spiel selbst als Element ...
- SalesGame TheGame;
-
- protected:
- // Erste Fitness-Initialisierung fuer Evolution,
- // da der Konstruktor die virtuale Fitnessfunktion noch nicht kennt !
- void InitFitness(void)
- {
- AssignWorstDistance();
- Chromosomen::InitFitness();
- }
-
- // Ueberladen der SterbeFunktion. Ausser dem Loeschen des 'Chromosom's
- // wird noch ueberpreft, ob dieses Chromosom die Loesung der laengsten
- // Wegstrecke darstellt
- void Kill(size_type i) {
- if ((*this)[i].GetFitness() < std::numeric_limits<double>::epsilon()) {
- Flag |= 1; // Chromosom mit WorstDistance wird geloescht
- }
- loesche (i);
- }
-
- virtual void CrossingOver (size_type m, size_type w);
-
- void Mutation();
-
- int NoImproving;
- int NoImprovingCrossingOvers;
-
- void CreateNewSymChromosom ( Chromosom &dest, size_type m, size_type w,
- SortListe<size_type, size_type>& CrossPoints ) override;
-
- void validate(const Chromosom &c);
-
- // Zeigt im 0-ten Bit, ob sich WorstDistance veraendert hat.
- // Dann muss die Fitness der gesammten Population neu berechnet
- // werden
- long Flag;
- // Speichert die Laenge der laengsten Strecke
- double WorstDistance;
- // Speichert die Laenge der kuerzesten Strecke
- double BestDistance;
- // Bestimmen des laengsten geschlossenen Pfades
- void AssignWorstDistance () {
- size_type i = 0;
- double Distance;
-
- // Variablen zuruecksetzen
- WorstDistance = 0;
- // und neu berechnen
- while (i < size()) {
- if ((Distance = TheGame.Play ((*this)[i], false)) > WorstDistance)
- WorstDistance = Distance;
- i++;
- }
- Flag &= ~1; // 0-te Bit wird geloescht
- }
-
+ friend class SalesChromosom;
+
+ public:
+ using Chromosomen::size_type;
+
+ // Der Konstruktor mit allen Einstellungen zum Loesen des Problems.
+ SalesChromosomen(
+ size_type MaxChromosomen,
+ size_type StartChromosomNumber,
+ size_type StartChromosomLength,
+ size_type Nukleotide=3,
+ SpliceCodeInfo *ptrSpliceCodeInfo=nullptr,
+ size_type InversionFreq=0,
+ size_type TranslocationFreq=0,
+ size_type AsymXOverFreq=0,
+ size_type CrossVal=1,
+ size_type MutationFreq=10000, // [1]
+ size_type NoImprovingCrossingOvers=100
+ ) ;
+ SalesChromosomen(
+ size_type MaxChromosomen,
+ const std::string& StartGenFile,
+ size_type Nukleotide=3,
+ SpliceCodeInfo *ptrSpliceCodeInfo=nullptr,
+ size_type InversionFreq=0,
+ size_type TranslocationFreq=0,
+ size_type AsymXOverFreq=0,
+ size_type CrossVal=1,
+ size_type MutationFreq=10000,
+ size_type NoImprovingCrossingOvers=100
+ ) ;
+ // Die Protokoll-Funktion zur Laufzeit
+ bool Echo() const override;
+
+ // Beschreibung der Parameter s. "gentech.h"
+ int Evolution(double GoalFitness, const std::string& chrptrPtkFile,
+ double BirthRate=0.6, int Bigamie=0,
+ size_type NoImprovingCrossingOvers = 100 ) override;
+
+ // Protokolliere
+ void Protokoll( void ) override;
+
+ // Fitness fuer das uebergebene Chromosom.
+ // Wert [0..1] !
+ double Fitness(const Chromosom&) override;
+ void CalcWholeFitness() override;
+ double GetWorstDistance() const { return WorstDistance; }
+ double GetBestDistance() const { return BestDistance; }
+
+ // Und das Spiel selbst als Element ...
+ SalesGame TheGame;
+
+ protected:
+ // Erste Fitness-Initialisierung fuer Evolution,
+ // da der Konstruktor die virtuale Fitnessfunktion noch nicht kennt !
+ void InitFitness(void)
+ {
+ AssignWorstDistance();
+ Chromosomen::InitFitness();
+ }
+
+ // Ueberladen der SterbeFunktion. Ausser dem Loeschen des 'Chromosom's
+ // wird noch ueberpreft, ob dieses Chromosom die Loesung der laengsten
+ // Wegstrecke darstellt
+ void Kill(size_type i) noexcept override {
+ if ((*this)[i].GetFitness() < std::numeric_limits<double>::epsilon()) {
+ Flag |= 1; // Chromosom mit WorstDistance wird geloescht
+ }
+ erase(i);
+ }
+
+ void CrossingOver(size_type m, size_type w) noexcept override;
+
+ void Mutation() override;
+
+ size_type NoImproving;
+ size_type NoImprovingCrossingOvers;
+
+ void CreateNewSymChromosom( Chromosom &dest, size_type m, size_type w,
+ SortListe<size_type, size_type>& CrossPoints ) override;
+
+ void validate(const Chromosom &c);
+
+ // Zeigt im 0-ten Bit, ob sich WorstDistance veraendert hat.
+ // Dann muss die Fitness der gesammten Population neu berechnet
+ // werden
+ long Flag;
+ // Speichert die Laenge der laengsten Strecke
+ double WorstDistance;
+ // Speichert die Laenge der kuerzesten Strecke
+ double BestDistance;
+ // Bestimmen des laengsten geschlossenen Pfades
+ void AssignWorstDistance () {
+ size_type i = 0;
+ double Distance;
+
+ // Variablen zuruecksetzen
+ WorstDistance = 0;
+ // und neu berechnen
+ while (i < size()) {
+ if ((Distance = TheGame.Play ((*this)[i], false)) > WorstDistance)
+ WorstDistance = Distance;
+ i++;
+ }
+ Flag &= ~1; // 0-te Bit wird geloescht
+ }
};
# endif
diff --git a/src/gentech/gentech.cpp b/src/gentech/gentech.cpp index 8fb3465..243e635 100755 --- a/src/gentech/gentech.cpp +++ b/src/gentech/gentech.cpp @@ -39,7 +39,7 @@ Chromosom::Chromosom ( Chromosomen & env, size_type StartChromosomLength ) // Zufaellig erzeugte Chromosomen
{
for ( Chromosom::size_type i=StartChromosomLength; i > 0; --i) {
- fuegeEin( Random(env.UserNukleoMinVal,env.UserNukleoMaxVal) );
+ push_back( Random(env.UserNukleoMinVal,env.UserNukleoMaxVal) );
}
assert (size() == StartChromosomLength);
}
@@ -125,7 +125,7 @@ void Chromosom::Load(const std::string& FileName) { if (fread (&len, sizeof (len), 1, file) == 0) { INT_ERR (__LINE__); }
for(size_type i=0; i<len; ++i) {
if( fread (&value, sizeof(value), 1, file) == 0 ) { INT_ERR (__LINE__); }
- fuegeEin ( value, i);
+ insert(i, value);
}
fclose(file);
}
@@ -199,9 +199,9 @@ bool Chromosom::FindIntron (size_type &von, size_type &bis) const return done;
}
-Chromosom::size_type Chromosom::Splicing (void)
+Chromosom::size_type Chromosom::Splicing()
{
- size_type i, NNukleotide=0, IntronStart=0, IntronEnd=size()-1;
+ size_type NNukleotide=0, IntronStart=0, IntronEnd=size()-1;
#ifndef NDEBUG
size_type len_old;
#endif
@@ -213,15 +213,12 @@ Chromosom::size_type Chromosom::Splicing (void) return 0;
}
- while(FindIntron (IntronStart, IntronEnd) == 1) {
+ while( FindIntron(IntronStart, IntronEnd) ) {
#ifndef NDEBUG
len_old=size();
#endif
assert( IntronStart <= IntronEnd );
- for (i = IntronEnd; i >= IntronStart; i--) {
- assert (size() > 0);
- loesche(i);
- }
+ erase( IntronStart, IntronEnd+1 );
assert ( len_old - ( IntronEnd - IntronStart + 1 ) == size() );
NNukleotide+=IntronEnd-IntronStart+1;
IntronEnd=size()-1;
@@ -240,23 +237,23 @@ void Chromosom::InsertIntrons (size_type von, size_type length) {
// StartSequenz einfuegen.
for (i=0; env.ptrSpliceCodeInfo->SpliceCode[i] != 0; ++i, ++von, --length) {
- fuegeEin ( GetUserNukleotidValue(env.ptrSpliceCodeInfo->SpliceCode[i]), von);
+ insert(von, GetUserNukleotidValue(env.ptrSpliceCodeInfo->SpliceCode[i]));
}
// Stelle fuer Rumpf merken.
iRumpf=von;
// EndSequenz einfuegen.
for (++i; env.ptrSpliceCodeInfo->SpliceCode[i] != 0; ++i, ++von, --length) {
- fuegeEin ( GetUserNukleotidValue (env.ptrSpliceCodeInfo->SpliceCode[i]), von);
+ insert(von, GetUserNukleotidValue (env.ptrSpliceCodeInfo->SpliceCode[i]));
}
// Rumpf fuellen.
while (length > 0) {
- fuegeEin (Random (env.UserNukleoMinVal, env.UserNukleoMaxVal), iRumpf);
+ insert(iRumpf, Random (env.UserNukleoMinVal, env.UserNukleoMaxVal));
length--;
}
}
}
-void Chromosom::Inversion (void)
+void Chromosom::Inversion()
{
Chromosom save(env);
size_type start, end, i;
@@ -267,7 +264,7 @@ void Chromosom::Inversion (void) end = Random (start+1, size()-1) ;
assert (end < size());
assert (end > start);
- for (i=start; i <= end; ++i) save.fuegeEin ((*this)[i]);
+ for (i=start; i <= end; ++i) { save.push_front((*this)[i]); }
assert (save.size()==end-start+1);
for (i=start+1; i <= end; ++i) {
assert( end-i < save.size() );
@@ -275,7 +272,7 @@ void Chromosom::Inversion (void) }
}
-void Chromosom::Translocation (void)
+void Chromosom::Translocation()
{
Chromosom save(env);
size_type start, end, dest, i;
@@ -288,14 +285,14 @@ void Chromosom::Translocation (void) assert (end > start);
// while( ( end = Random(0, size()-1) ) <= start) ; // FIXME: ????
for (i=start; i <= end; ++i) {
- save.fuegeEin((*this)[start]);
- loesche(start);
+ save.push_back((*this)[i]);
}
- assert( save.size() == end-start+1);
+ erase(start, end+1);
+ assert( save.size() == end-start+1 );
dest = Random( 0, size()-1 );
for (i=0; i<save.size(); ++i) {
- (*this).fuegeEin (save[i], dest+i);
+ (*this).insert(dest+i, save[i]);
}
}
@@ -401,10 +398,10 @@ Chromosomen::Chromosomen (NukleoTyp UserNukleoMinVal, if (fread(&ChromLen, sizeof(ChromLen), 1, file) == 0) { INT_ERR (__LINE__); } // FIXME: Use fixed width type
for(size_type i=0; i<ChromLen; ++i) {
if (fread(&value, sizeof(value), 1, file) == 0) { INT_ERR(__LINE__); }
- Gamma.fuegeEin (value, i);
+ Gamma.push_back(value);
}
assert (Gamma.size()==ChromLen);
- fuegeEin (Gamma, j);
+ push_back(Gamma);
}
assert (size()==ChromQuantity);
fclose (file);
@@ -468,7 +465,7 @@ Chromosomen::Chromosomen ( NukleoTyp UserNukleoMinVal, NukleoTyp UserNukleoMaxVa for (size_type i=0; i<StartChromosomNumber ; ++i) {
Chromosom Gamma (*this, StartChromosomLength);
assert (Gamma.size() == StartChromosomLength);
- fuegeEin (Gamma);
+ push_back(Gamma);
}
assert (size()==StartChromosomNumber);
}
@@ -612,23 +609,23 @@ void Chromosomen::NewGeneration(double BirthRate, bool Bigamie) if (!Bigamie && ElternPaare.size() < ChromBetterZeroNumber) {
do {
i=RouletteSelect();
- } while( ElternPaare.istElement(i) ) ;
- ElternPaare.fuegeEin(i);
+ } while( ElternPaare.contains(i) ) ;
+ ElternPaare.push_back(i);
l--;
} else {
if (!Bigamie) {
// the hard way ...
do {
i = Random(0, size()-1);
- } while( ElternPaare.istElement(i) ) ;
- ElternPaare.fuegeEin(i);
+ } while( ElternPaare.contains(i) ) ;
+ ElternPaare.push_back(i);
l--;
} else {
// Bigamistische Beziehung JA, aber sexuell !!!
int w, m;
- ElternPaare.fuegeEin ((w = Random (0, size()-1))) ;
+ ElternPaare.push_back((w = Random (0, size()-1))) ;
while (w == (m=Random (0, size()-1))) ;
- ElternPaare.fuegeEin (m) ;
+ ElternPaare.push_back(m) ;
l-=2;
}
}
@@ -679,21 +676,21 @@ void Chromosomen::NewGeneration(bool Bigamie) if( !Bigamie && ElternPaare.size() < ChromBetterZeroNumber ) {
do {
i = RouletteSelect();
- } while ( ElternPaare.istElement(i) ) ;
- ElternPaare.fuegeEin(i);
+ } while ( ElternPaare.contains(i) ) ;
+ ElternPaare.push_back(i);
l--;
} else {
if( !Bigamie ) {
do {
i = Random(0, size()-1);
- } while( ElternPaare.istElement (i) ) ;
- ElternPaare.fuegeEin(i);
+ } while( ElternPaare.contains(i) ) ;
+ ElternPaare.push_back(i);
l--;
} else { // Bigamistische Beziehung JA, aber sexuell !!!
size_type w, m;
- ElternPaare.fuegeEin( ( w = Random(0, size()-1) ) );
+ ElternPaare.push_back( ( w = Random(0, size()-1) ) );
while( w == ( m = Random(0, size()-1) ) ) ;
- ElternPaare.fuegeEin(m);
+ ElternPaare.push_back(m);
l-=2;
}
}
@@ -801,7 +798,7 @@ void Chromosomen::CreateNewSymChromosom ( Chromosom &dest, size_type m, size_typ assert ( von == dest.size() );
// Chromosomenabschnitt uebertragen [von..bis[
for (size_type n=von; n<bis; n++) {
- dest.fuegeEin(THIS[ch][n],n);
+ dest.insert(n, THIS[ch][n]);
}
// Indize zwischen den Geschlechtern alternieren
ch = ( ch==m ? w : m ) ;
@@ -824,7 +821,7 @@ void Chromosomen::CrossingOver (size_type m, size_type w) // Kreuzungspunkte sortiert eintragen.
for ( size_type i = 0; i < CrossVal; ++i ) {
- CrossPoints.fuegeEin( Random(0 , THIS[shortest].size()) );
+ CrossPoints.insert( Random(0 , THIS[shortest].size()) );
}
Chromosom NeuA (*this, 0);
@@ -832,7 +829,7 @@ void Chromosomen::CrossingOver (size_type m, size_type w) size_type SplicedCode;
CreateNewSymChromosom ( NeuA, m, w, CrossPoints );
- fuegeEin ( NeuA, size() );
+ push_back( NeuA );
SplicedCode=NeuA.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
@@ -843,7 +840,7 @@ void Chromosomen::CrossingOver (size_type m, size_type w) THIS[size()-1].SetFitness(Fitness(NeuA));
CreateNewSymChromosom ( NeuB, w, m, CrossPoints );
- fuegeEin ( NeuB, size() );
+ push_back( NeuB );
SplicedCode=NeuB.Splicing();
if(SplicedCode>0) {
SplicedChromosoms++;
@@ -858,22 +855,21 @@ void Chromosomen::CrossingOver (size_type m, size_type w) XOverNumber = 0;
// Assymetrisches XOver : Nur ein neues Element !
Chromosom Neu (*this, 0);
- size_type i, n;
// Kopieren
Neu=THIS[m];
// anhaengen
- for (n=0, i=Neu.size(); n<THIS[w].size(); n++, ++i) {
- Neu.fuegeEin(THIS[w][n],i);
+ for (size_type n=0; n<THIS[w].size(); n++) {
+ Neu.push_back( THIS[w][n] );
}
Neu.SetFitness(Fitness(Neu));
- fuegeEin ( Neu, size() );
+ push_back( Neu );
}
}
-void Chromosomen::Mutation (void)
+void Chromosomen::Mutation()
{
static size_type next_nukleotide_idx = MutationFreq + Random( 0, (size_type)(MutationFreq/MutationFreqVar) ) ;
MutationsThisGeneration=0;
@@ -906,7 +902,7 @@ void Chromosomen::Mutation (void) }
-void Chromosomen::InversionsMutation (void)
+void Chromosomen::InversionsMutation()
{
static size_type next_chromosomen_idx = InversionFreq;
InversionsThisGeneration=0;
@@ -926,7 +922,7 @@ void Chromosomen::InversionsMutation (void) next_chromosomen_idx = chromosomen_idx;
}
-void Chromosomen::TranslocationsMutation (void)
+void Chromosomen::TranslocationsMutation()
{
static size_type next_chromosomen_idx = TranslocationFreq;
TranslocationsThisGeneration=0;
@@ -946,7 +942,7 @@ void Chromosomen::TranslocationsMutation (void) next_chromosomen_idx = chromosomen_idx;
}
-void Chromosomen::CalcWholeFitness (void)
+void Chromosomen::CalcWholeFitness()
{
double Total=0, TempFitness;
size_type BestChrom=npos, ChromLen;
@@ -1006,9 +1002,9 @@ double Chromosomen::GetXWorstFitness(size_type count) const if (count == 0) { return -1; } // Falsche Anzahl uebergeben
for (size_type i=0; i<size(); ++i) {
if( Worst.size() < count ) {
- Worst.fuegeEin (THIS[i].GetFitness());
+ Worst.insert(THIS[i].GetFitness());
} else if( THIS[i].GetFitness() < Worst[count-1] ) {
- Worst.fuegeEin (THIS[i].GetFitness());
+ Worst.insert(THIS[i].GetFitness());
}
}
if (Worst.size() >= count) {
@@ -1033,7 +1029,7 @@ Chromosomen::size_type Chromosomen::GetBestChromosom() const return ib;
}
-const Chromosom &Chromosomen::GetTheBestEverChromosom (void)
+const Chromosom &Chromosomen::GetTheBestEverChromosom()
// Hier kein kein Slicing mehr, da beim abspeichern des besten
// Chromosoms in der Variablen 'TheBestEver' der Code schon
// gesplict abgespeichert wird.
@@ -1070,7 +1066,7 @@ void Chromosomen::CalcParameter(void) }
}
-void Chromosomen::Protokoll (void)
+void Chromosomen::Protokoll()
{
double SpliceCodePerChrom ;
@@ -1125,7 +1121,7 @@ void Chromosomen::Protokoll (void) }
}
-bool Chromosomen::Echo (void) const
+bool Chromosomen::Echo() const
{
if (Generation == 1) {
printf(" Generation: BestGeneration: AverageFitness: BestFitness:"
diff --git a/src/jaulibs/dblmenge.cpp b/src/jaulibs/dblmenge.cpp index 38a9986..f8dde97 100644 --- a/src/jaulibs/dblmenge.cpp +++ b/src/jaulibs/dblmenge.cpp @@ -22,7 +22,7 @@ #include <cmath> #include <limits> -template<> Menge<double>::size_type Menge<double>::istElement(const double& x) +template<> Menge<double>::size_type Menge<double>::indexOf(const double& x) const noexcept { for (size_type i = 0; i < card(); i++) { if( ::fabs( (*this)[i] - x ) < std::numeric_limits<double>::epsilon() * ::fabs(x) ) { diff --git a/src/jaulibs/hashstr.cpp b/src/jaulibs/hashstr.cpp index 5286422..78099fa 100644 --- a/src/jaulibs/hashstr.cpp +++ b/src/jaulibs/hashstr.cpp @@ -161,7 +161,7 @@ int HashStrClassListe::fuegeEin(const StrClass& a) HashArray[HPos]=new SortListe<StrClass>(); if( HashArray[HPos] == nullptr ) INT_ERR(__LINE__); } - size_type SPos=HashArray[HPos]->fuegeEin(a); + size_type SPos=HashArray[HPos]->insert(a); assert(HEAP_CHECK()>=1); AktualisiereAnzahlElementePlus(HPos); anzahl++; @@ -181,8 +181,8 @@ void HashStrClassListe::loesche(size_type i) } assert(HEAP_CHECK()>=1); assert(HashArray[HPos]!=nullptr); - if(SPos>=HashArray[HPos]->laenge()) INT_ERR(__LINE__); - HashArray[HPos]->loesche(SPos); + if(SPos>=HashArray[HPos]->size()) INT_ERR(__LINE__); + HashArray[HPos]->erase(SPos); assert(HEAP_CHECK()>=1); AktualisiereAnzahlElementeMinus(HPos); anzahl--; diff --git a/src/jaulibs/liste.h b/src/jaulibs/liste.h index 76e4214..03a8ee7 100644 --- a/src/jaulibs/liste.h +++ b/src/jaulibs/liste.h @@ -54,6 +54,7 @@ #include <iostream> #include <limits> #include <type_traits> + #include <stdexcept> #include <cstdlib> #include <cstdio> @@ -175,10 +176,28 @@ virtual ~Liste() noexcept { dest(); } Liste(const Liste& m) noexcept : m_start(0), m_count(m.m_count), m_current_elem(0), m_current_idx(0) { copy(m); } - virtual void insert(const size_type pos, const value_type& x) noexcept { - fuegeEin(x, pos); + /** + * Like std::vector::push_back(), copy + * @param x the value to be added at the tail. + */ + constexpr void push_back(const value_type& x) noexcept { + insert(size(), x); } - virtual bool fuegeEin(const value_type& x, size_type idx = 0) noexcept { + + /** + * Like std::vector::push_front(), copy + * @param x the value to be added at the front. + */ + constexpr void push_front(const value_type& x) noexcept { + insert(0, x); + } + + /** + * Like std::vector::insert(), copy + * @param pos iterator before which the content will be inserted. pos may be the end() iterator + * @param x element value to insert + */ + constexpr void insert(size_type idx, const value_type& x) noexcept { const size_type old_idx = m_current_idx; if( idx > m_count ) { fprintf(stderr,"\n Falscher Index."); @@ -201,11 +220,11 @@ } else { --idx; // Das Element 'ein' vor dem Ziel anfahren ! if( idx < old_idx ) { - for (size_type k = old_idx; k > idx; k--) { + for (size_type k = old_idx; k > idx; --k) { iter_bw(); } } else if( idx > old_idx ) { - for (size_type k = old_idx; k < idx; k++) { + for (size_type k = old_idx; k < idx; ++k) { iter_fw(); } } @@ -220,37 +239,37 @@ #ifdef __LISTE_PARANOIA__ ParanoiaCheck(); #endif - return true; } - bool fuegeEin(const Liste& AL, size_type idx = 0) noexcept { + void insert(const size_type idx, const Liste& AL) noexcept { for (size_type j = 0; j < AL.size(); ++j) { - if(!fuegeEin (AL[j], idx+j)) { - return false; - } + fuegeEin (AL[j], idx+j); } #ifdef __LISTE_PARANOIA__ ParanoiaCheck(); #endif - return true; } - virtual bool loesche(size_type i) noexcept { + /** + * Like std::vector::erase(), removes the elements at pos. + * @return iterator following the last removed element. + */ + constexpr void erase(const size_type idx) noexcept { const size_type oldInd = m_current_idx; - if (i >= m_count ) + if (idx >= m_count ) { fprintf(stderr,"\n Falscher Index."); fprintf(stderr,"\n Es wurde auf ein Element an %zu.Stelle einer Liste mit %zu Elementen zu loeschen.\n", - (size_t)i, (size_t)size()); + (size_t)idx, (size_t)size()); INT_ERR(__LINE__); } - if (i < oldInd) { - for (size_type k = oldInd; k > i; k--) { + if (idx < oldInd) { + for (size_type k = oldInd; k > idx; k--) { iter_bw(); } - } else if (i > oldInd) { - for(size_type k = oldInd; k < i; k++) { + } else if (idx > oldInd) { + for(size_type k = oldInd; k < idx; k++) { iter_fw(); } } @@ -279,7 +298,18 @@ #ifdef __LISTE_PARANOIA__ ParanoiaCheck(); #endif - return true; + } + + /** + * removes the elements in the range [first_idx, last_idx). + * @return number of deleted elements + */ + constexpr size_type erase (const size_type first_idx, const size_type last_idx) { + size_type count = 0; + for(size_type i = first_idx; i<last_idx; ++i, ++count) { + erase(first_idx); + } + return count; } Liste& operator= (const Liste& m) { @@ -316,14 +346,43 @@ return !(Liste::operator == (L)); } - virtual const value_type& operator[](size_type i) const noexcept - { return get_element(i); } + /** + * Like std::vector::operator[](size_type), immutable reference. + */ + const_reference operator[](size_type i) const noexcept { + return get_element(i); + } + + /** + * Like std::vector::operator[](size_type), mutable reference. + */ + reference operator[](size_type i) noexcept { + return get_element(i); + } + + /** + * Like std::vector::at(size_type), immutable reference. + */ + const_reference at(size_type i) const { + return get_element(i); + } - virtual value_type& operator[](size_type i) noexcept - { return get_element(i); } + /** + * Like std::vector::at(size_type), mutable reference. + */ + reference at(size_type i) { + return get_element(i); + } + + /** + * Like std::vector::empty(). + */ + constexpr bool empty() const noexcept { return 0 == m_count; } - size_type laenge() const noexcept { return m_count; } - size_type size() const noexcept { return m_count; } + /** + * Like std::vector::size(). + */ + constexpr size_type size() const noexcept { return m_count; } virtual void Ausgabe (std::ostream& OS) const noexcept { const size_type Zeilenlaenge = 10; @@ -340,24 +399,22 @@ OS << " >"; } - bool istElement(const value_type& x, size_type i=0) const noexcept { - for ( ; i < m_count; i++) { - if ((*this)[i] == x) { - return true; - } - } - return false; + bool contains(const value_type& x) const noexcept { + return npos != indexOf(x); } /** - * Returns index of given element if found or size() - * @param a + * Returns index of given element if found or npos + * @param x * @return */ - size_type Referenz2Index(const value_type& a) const noexcept { - size_type i = 0; - while(i < size() && &a!=&(*this)[i]) { i--; } - return i; + size_type indexOf(const value_type& x) const noexcept { + for (size_type i=0; i < m_count; i++) { + if ((*this)[i] == x) { + return i; + } + } + return npos; } #ifdef __LISTE_PARANOIA__ @@ -411,15 +468,11 @@ } } - value_type& get_element(const size_type idx) const noexcept { + value_type& get_element(const size_type idx) const { const size_type oldInd = m_current_idx; if ( idx >= m_count ) { - fprintf(stderr,"\n Falscher Index."); - fprintf(stderr,"\n Es wurde auf ein Element mit index %zu einer Liste mit %zd Elementen zugegriffen.\n", - (size_t)idx, (size_t)size()); - INT_ERR(__LINE__); + throw std::out_of_range("Index "+std::to_string(idx)+" >= size "+std::to_string(m_count)); } - if (idx < oldInd) { for (size_type k = oldInd; k > idx; k--) { iter_bw(); diff --git a/src/jaulibs/menge.h b/src/jaulibs/menge.h index 8666359..9da42a2 100644 --- a/src/jaulibs/menge.h +++ b/src/jaulibs/menge.h @@ -41,9 +41,14 @@ */ static constexpr const size_type npos = std::numeric_limits<size_type>::max(); - bool leer() const { return l.laenge() == 0; } - size_type card() const { return l.laenge(); } - size_type istElement(const T& x) { + bool empty() const noexcept { return l.size() == 0; } + size_type card() const noexcept { return l.size(); } + + bool contains(const T& x) const noexcept { + return npos != indexOf(x); + } + + size_type indexOf(const T& x) const noexcept { for (size_type i = 0; i < card(); ++i) { if (l[i] == x) { return i; @@ -51,20 +56,26 @@ } return npos; } - bool fuegeEin(const T& x) { - return ( npos != istElement(x) ) ? false : l.fuegeEin(x); + + bool insert(const T& x) { + if( contains(x) ) { + return false; + } + l.push_back(x); + return true; } - bool loesche(const T& x) { - const int pos = istElement(x); + bool erase(const T& x) { + const int pos = indexOf(x); if( npos != pos ) { - return l.loesche(pos); + l.erase(pos); + return true; } else { return false; } } void inhalt() const { std::cout << "{ "; - if (!leer()) { + if (!empty()) { std::cout << l[0]; for (int i=1; i<card(); i++) { std::cout << ", " << l[i]; @@ -76,8 +87,8 @@ Menge operator*(Menge& a) const { Menge<T> axb; for (int i=0; i<card(); i++) { - if (a.istElement(l[i]) >= 0) { - axb.fuegeEin(l[i]); + if( a.contains( l[i] ) ) { + axb.insert(l[i]); } } return axb; diff --git a/src/jaulibs/sortlist.h b/src/jaulibs/sortlist.h index 08f0607..d1bfdd1 100644 --- a/src/jaulibs/sortlist.h +++ b/src/jaulibs/sortlist.h @@ -46,19 +46,17 @@ typedef Size_type size_type; // Spezielle Zugriffsrechte fuer SortListe - using Liste<value_type, size_type>::loesche; + using Liste<value_type, size_type>::erase; using Liste<value_type, size_type>::operator !; using Liste<value_type, size_type>::operator[]; - using Liste<value_type, size_type>::laenge; using Liste<value_type, size_type>::size; - using Liste<value_type, size_type>::Referenz2Index; - using Liste<value_type, size_type>::istElement; + using Liste<value_type, size_type>::contains; using Liste<value_type, size_type>::npos; enum order_t { UP, DOWN }; SortListe(order_t order=UP) : Liste<value_type>(), m_order(order) { } SortListe(const SortListe& m) : Liste<value_type>(m), m_order(m.m_order) {} - virtual ~SortListe() { } + ~SortListe() override { } SortListe& operator=(const SortListe &m) { @@ -67,22 +65,19 @@ return *this; } // fuegeEin RETURN : neuer Index, oder npos fuer Fehler - size_type fuegeEin(const value_type& a) { - size_type u=0, o=laenge()>0 ? laenge()-1 : 0; + size_type insert(const value_type& a) { + size_type u=0, o=size()>0 ? size()-1 : 0; size_type i=findeIndex(a, u, o); - if ( npos == i ) - { - if( Liste<value_type>::fuegeEin(a, o) == 0 ) return npos; - else return o; - } - else - { - if( Liste<value_type>::fuegeEin(a, i) == 0 ) return npos; - else return i; + if ( npos == i ) { + Liste<value_type>::insert(o, a); + return o; + } else { + Liste<value_type>::insert(i, a); + return i; } } size_type findeElement(const value_type& x) const { - size_type u=0, o=laenge()>0 ? laenge()-1 : 0; + size_type u=0, o=size()>0 ? size()-1 : 0; return findeIndex(x, u, o); } |