-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
190 lines (174 loc) · 6.29 KB
/
main.cpp
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
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int populationSize =40, numOfIterations = 100;
const double Pc = 0.5, Pm = 0.05;
int cnt=0;
class Chromosome {
private:
ll degree=0;
long double fitness{};
long double totalError{};
vector<pair<long double,long double>> allItems;
int itemNum{};
public:
vector<long double> genes;
Chromosome() {
itemNum=++cnt;
}
virtual ~Chromosome() = default;
void setItems(ll d) {
this->degree=d;
genes.resize(this->degree);
}
void calcFitness(const vector<pair<long double,long double>>& items){
allItems=items;
long double sum,finalError=0.0;
for(const auto & item : items){
sum=genes[0];
for(int j=1;j<genes.size();j++)
sum+=(genes[j]*pow(item.first,j));
sum-=item.second;
sum*=sum;
finalError+=sum;
}
this->totalError=(finalError/items.size());
this->fitness=(1/this->totalError);
}
[[nodiscard]] long double getFitness() const {
return fitness;
}
[[nodiscard]] long double getTotalError() const {
return totalError;
}
[[nodiscard]] vector<pair<long double, long double>> getAllItems() const {
return allItems;
}
};
//sort by values by inverse
bool compareVal(const Chromosome& c1,const Chromosome& c2){
return c1.getFitness()>c2.getFitness();
}
class GA{
private:
vector<Chromosome>* solutions;
vector<Chromosome> offsprings;
int numOfOffsprings; // for divide offsprings and population
//utility functions
Chromosome tournament(){
auto & sol=*solutions;
Chromosome bestChromosome,chromosome;
std::random_device rd; // random number
std::mt19937 gen(rd()); // the generator
std::uniform_int_distribution<> distribution(0,(int) sol.size()-3); // define the range
bestChromosome=sol[distribution(gen)];
for(int i=1;i<this->numOfOffsprings;i++){
chromosome=sol[distribution(gen)];
chromosome.calcFitness(sol[0].getAllItems());bestChromosome.calcFitness(sol[0].getAllItems());
if(chromosome.getFitness()<bestChromosome.getFitness()) bestChromosome=chromosome;
}
return bestChromosome;
}
void elitism(){
auto & sol=*solutions;
sort(sol.begin(),sol.end(), compareVal); // min error in the last
for(int i=0;i<offsprings.size();i++)
sol[i]=offsprings[i];
}
public:
explicit GA(vector<Chromosome>& solutions) {
this->solutions=&solutions;
this->numOfOffsprings=(int)this->solutions->size()/2;
offsprings.resize(this->numOfOffsprings);
}
void initializePopulation(){
for(auto & solution : *solutions){
for(auto && gene : solution.genes){
gene=((float(rand())/float(RAND_MAX))*(10+10))-10;
}
}
}
void selection(){
auto & sol=*solutions;
sort(sol.begin(),sol.end(), compareVal); // min error in the last
for(int i=0;i<numOfOffsprings;i++)
offsprings[i]=this->tournament();
}
void crossOver(){
for(int i=0;i<offsprings.size();i++){
for(int j=i+1;j<offsprings.size();j++){
double randNum=((double) rand() / (RAND_MAX));
if(randNum<Pc){
int randNumIdx=rand()%(offsprings[i].genes.size()-2);
for(int k=randNumIdx;k<offsprings[i].genes.size();k++)
swap(offsprings[i].genes[k],offsprings[j].genes[k]);
}
}
}
}
void mutation(ll t){
for(auto & offspring : offsprings){
long double maxVal=-1e9,minVal=1e9;
for(auto && gene : offspring.genes)
if(gene>maxVal) maxVal=gene;
for(auto && gene : offspring.genes)
if(gene<minVal) minVal=gene;
for(auto && gene : offspring.genes){
double randNum=((double)rand()/(RAND_MAX));
if(randNum<Pm) {
long double dLower=gene-minVal,dUpper=maxVal-gene,y=0,newVal;
double randNum1=((double) rand() / (RAND_MAX));
y=randNum1<=0.5?dLower:dUpper;
newVal=y*(1-pow(randNum1, pow(1-t/numOfIterations,2)));
gene=(y==dLower)?gene-newVal:gene+newVal;
}
}
}
}
void replacement(){
elitism();
}
};
int main() {
srand(time(nullptr));
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ll numOftestCases,numOfItems,degree,iterationsSize,testNum=1;cin>>numOftestCases;
while(numOftestCases--){
iterationsSize=numOfIterations;
cin>>numOfItems>>degree;
vector<pair<long double,long double>> items(numOfItems);
for(auto i=0;i<numOfItems;i++) cin>>items[i].first>>items[i].second;
vector<Chromosome> solutions(populationSize);
for (int i = 0; i < populationSize; i++)
solutions[i].setItems(degree+1);
GA geneticAlgorithm(solutions);
//First Step (Initialize Pool Of Solutions)
geneticAlgorithm.initializePopulation();
while(iterationsSize--){
//Second Step (Individual Evaluation)
for (int i = 0; i < populationSize; i++) solutions[i].calcFitness(items);
//Third Step (Selection)
geneticAlgorithm.selection();
//Fourth Step (Crossover)
geneticAlgorithm.crossOver();
//Fifth Step (Mutation)
geneticAlgorithm.mutation(testNum);
//Sixth Step (Reproduction)
geneticAlgorithm.replacement();
}
sort(solutions.begin(),solutions.end(), compareVal);
//printing
cout<<"Testcase "<<testNum++<<":\nThe Coefficients: [ ";
for(auto && gene : solutions[solutions.size()-1].genes){
cout<<std::fixed<<std::setprecision(2);
gene!=solutions[solutions.size()-1].genes[solutions[solutions.size()-1].genes.size()-1]?
cout<<gene<<" , ":
cout<<gene<<" ";
}
cout<<"]"<<endl<<"Mean Square Error: "<<std::fixed<<std::setprecision(20)
<<solutions[solutions.size()-1].getFitness()<<endl<<endl;
}
return 0;
}