yeah
This commit is contained in:
69
tests/g-01.cpp
Normal file
69
tests/g-01.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "../src/Genetics/Genetics.h"
|
||||
|
||||
#include <iostream>
|
||||
class S: public Shin::Genetics::Individual
|
||||
{
|
||||
public:
|
||||
S(const double &a, const double &b) :Sa(a),Q(b)
|
||||
{
|
||||
};
|
||||
void mutate()
|
||||
{
|
||||
if(rand()%2==1)
|
||||
{
|
||||
Sa=(Sa*(double)(80.0+(double)(rand()%61)))/100.0+(40-rand()%80);
|
||||
}
|
||||
if(rand()%2==1)
|
||||
{
|
||||
Sa=-Sa;
|
||||
}
|
||||
if(rand()%2==1)
|
||||
{
|
||||
Q=(Q*(double)(60.0+(double)(rand()%81)))/100.0+(40-rand()%80);
|
||||
}
|
||||
if(rand()%2==1)
|
||||
{
|
||||
Q=-Q;
|
||||
}
|
||||
};
|
||||
S combine(S &s)
|
||||
{
|
||||
double a;
|
||||
double b;
|
||||
if( rand()%2)
|
||||
a=s.Sa;
|
||||
else
|
||||
a=Sa;
|
||||
if( rand()%2)
|
||||
b=s.Q;
|
||||
else
|
||||
b=Q;
|
||||
return S(a,b);
|
||||
}
|
||||
|
||||
double getFitness() const
|
||||
{
|
||||
return (double)1.0/(double)(Sa);
|
||||
//double s=abs(Sa-98545)+abs(Q+85);
|
||||
//return Sa*100-Q*5;
|
||||
//return 985258-s;
|
||||
//return s < 0?0:s;
|
||||
}
|
||||
double Sa;
|
||||
double Q;
|
||||
void dump()
|
||||
{
|
||||
std::cout<< " Sa: " << Sa <<" Q: " << Q << " fitness: " <<getFitness() << "\n";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Shin::Genetics::Genetics<S> g;
|
||||
g.addIndividual(S(1,0));
|
||||
g.addIndividual(S(1,50));
|
||||
g.addIndividual(S(50,50));
|
||||
S &s=g.getSolution(10000,99999999);
|
||||
s.dump();
|
||||
}
|
||||
Reference in New Issue
Block a user