-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
278 lines (271 loc) · 8.05 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
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
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <random>
#include <vector>
#include <algorithm>
#include <fstream>
//CONSTANTS AND GLOBAL VARIABLES
const int GENERATIONS = 100;
const int INITPOPSIZE = 10000;
const double KEEPRATIO = 0.5;
const int POPSIZE = INITPOPSIZE*KEEPRATIO;
const double ALPHA = 0.25;
const double MUTATIONAMOUNT = 20;
const double MUTATIONPERCENT = 0.01;
const int AVGFITCUTOFF = 2000;
int numAtoms;
int numNa;
int numCl;
/*
Initializing random device and various distributions.
Molecule bond lengths should be along the scale of magnitude for an Angstrom making our random guesses fall in the interval (0.01,10).
*/
std::random_device rd;
std::uniform_real_distribution<float> dist(-10,10);
std::uniform_real_distribution<float> perc(0.0,1.0);
std::uniform_real_distribution<float> mut((1-MUTATIONPERCENT),(1+MUTATIONPERCENT));
/*
Common activation function used in Neural Networks.
This function penalizes all negative members the same and has a high slope towads positive members.
This might cause problems for a problem with a large solution space, but this is not an issue for
us as explained above.
The prefactor was chosen arbitrarily.
*/
float expLinUnit(float x){
if(x>=0){
return (exp(x)-1);
}else{
return x;
}
}
/*
Function that returns the Euclidean norm in R^3
*/
float euclidNorm(float x_1, float y_1, float z_1, float x_2, float y_2, float z_2){
float dx = x_1-x_2;
float dy = y_1-y_2;
float dz = z_1-z_2;
return sqrt((dx*dx)+(dy*dy)+(dz*dz));
}
/*
Structure that contains the name of the atom and position.
*/
struct atom{
std::string name;
float x,y,z;
};
/*
Structure that will contain all the information for each member of the population.
This includes a vector of atoms and the cooresponding fitness score.
The potential is in terms of eVs and Angstroms for ease of use.
*/
struct member{
float fitness;
std::vector<atom> particles;
//Calculate potentials for each atom and calculate fitness for this member
void foo(){
float potential = 0;
for(int i=0;i<numAtoms;i++){
for(int j=i+1;j<numAtoms;j++){
bool kronecker = (particles[i].name != particles[j].name);
int eta = pow(-1,kronecker);
float r = euclidNorm(particles[i].x,particles[i].y,particles[i].z,particles[j].x,particles[j].y,particles[j].z);
if(r <= 0.1){
potential += 9999;
}else{
//std::cout<<r<<std::endl;
float ep = 55.26349406;
float k = 1/(4*M_PI*ep*pow(10,-4));
potential += eta*(k/r) + 1.09*pow(10,3)*exp((-r)/0.321);// + pow((0.1/r),12);
}
}
}
fitness = expLinUnit(-potential);
}
};
/*
Function that generates the initial population.
*/
std::vector<member> generatePopulation(){
std::vector<member> p;
for(int i = 0; i < INITPOPSIZE; i++){
std::vector<atom> a;
for(int j=0;j<numNa;j++){a.emplace_back(atom{"Na"});}
for(int j=0;j<numCl;j++){a.emplace_back(atom{"Cl"});}
//generating random positions with some added rules to decrease the degress of freedom of the system
for(int j=0;j<numAtoms;j++){
switch(j){
case 0:
a[j].x, a[j].y, a[j].z = 0;
break;
case 1:
a[j].x = dist(rd);
a[j].y, a[j].z = 0;
break;
case 2:
a[j].x = dist(rd);
a[j].y = dist(rd);
a[j].z = 0;
break;
default:
a[j].x = dist(rd);
a[j].y = dist(rd);
a[j].z = dist(rd);
}
}
p.emplace_back(member{0,a});
}
for(auto& m : p){m.foo();}
return p;
}
/*
Implementation of the BLX-alpha crossover method of real values.
This function returns a random number in the range [min-alpha*range, max+alpha*range].
*/
float BLXalphaCross(float q_1, float q_2){
float u, l;
if(q_1 >= q_2){
u = q_1;
l = q_2;
}else if(q_2 > q_1){
u = q_2;
l = q_1;
}
float lb = l-ALPHA*(u-l);
float ub = u+ALPHA*(u-l);
return (lb + perc(rd)*(ub-lb));
}
/*
Function that handles both selection and crossover.
The roulette wheel selection method is used.
The best member of the population does not mate to change, but does mate with others.
*/
void selectionCrossover(std::vector<member>& p){
float sum = 0;
for(int i=0;i<POPSIZE;i++){
if(p[i].fitness >=0){sum += p[i].fitness;}
}
//std::cout<<sum<<std::endl;
for(int i=1;i<POPSIZE;i++){
float rand = perc(rd)*sum;
float check = 0;
//std::cout << rand << std::endl;
for(int j=0;j<POPSIZE;j++){
check += p[j].fitness;
if(check >= rand){
for(int a=0;a<numAtoms;a++){
switch(a){
case 0:
p[i].particles[a].x = 0;
p[i].particles[a].y = 0;
p[i].particles[a].z = 0;
break;
case 1:
p[i].particles[a].x = BLXalphaCross(p[j].particles[a].x,p[i].particles[a].x);
p[i].particles[a].y = 0;
p[i].particles[a].z = 0;
break;
case 2:
p[i].particles[a].x = BLXalphaCross(p[j].particles[a].x,p[i].particles[a].x);
p[i].particles[a].y = BLXalphaCross(p[j].particles[a].y,p[i].particles[a].y);
p[i].particles[a].z = 0;
break;
default:
p[i].particles[a].x = BLXalphaCross(p[j].particles[a].x,p[i].particles[a].x);
p[i].particles[a].y = BLXalphaCross(p[j].particles[a].y,p[i].particles[a].y);
p[i].particles[a].z = BLXalphaCross(p[j].particles[a].z,p[i].particles[a].z);
}
}
p[i].foo();
break;
}
}
}
}
/*
Sort function from the algorithms header file put into a function to keep from writing multiple times.
*/
void sort(std::vector<member>& p){
std::sort(
p.begin(),
p.end(),
[](const auto& l,const auto& r){
return l.fitness > r.fitness;
});
}
/*
This function mutates a random number of members, besides the best, depending on the constant MUTATIONPERCENTAGE.
*/
void mutate(std::vector<member>& p){
for(int i=0;i<MUTATIONAMOUNT;i++){
int rand = 1+perc(rd)*(POPSIZE-1);
for(int j=0;j<numAtoms;j++){
switch(j){
case 0:
break;
case 1:
p[rand].particles[j].x *= mut(rd);
break;
case 2:
p[rand].particles[j].x *= mut(rd);
p[rand].particles[j].y *= mut(rd);
break;
default:
p[rand].particles[j].x *= mut(rd);
p[rand].particles[j].y *= mut(rd);
p[rand].particles[j].z *= mut(rd);
}
}
}
}
/*
Outputs data to console and text files.
*/
void recordData(std::vector<member>& p,int g,std::ofstream &xyz,std::ofstream &consolelog){
float avg = 0;
int num = 0;
xyz << numAtoms << std::endl;
xyz << "test" << std::endl;
for(int i=0;i<numAtoms;i++){
xyz << p[0].particles[i].name << "\t" << p[0].particles[i].x << "\t" << p[0].particles[i].y << "\t" << p[0].particles[i].z << std::endl;
}
for(auto& m : p){
avg += m.fitness;
num++;
}
avg = avg/num;
std::cout << "Generation: " << g << std::endl;
std::cout << "Avg Fitness: " << avg << std::endl;
}
int main(){
//Initializing output files
std::ofstream xyz;
xyz.open("config.xyz");
std::ofstream consolelog;
consolelog.open("consolelog.txt");
//Taking user input to determine the desired system
std::vector<atom> a;
std::cout << "Number of Sodium atoms: " << std::endl;
std::cin >> numNa;
std::cout << "Number of Chlorine atoms: " << std::endl;
std::cin >> numCl;
numAtoms = numNa + numCl;
//Generating a random population, culling the worst members, and outputting the best initial configuration
std::vector<member> initPopulation = generatePopulation();
sort(initPopulation);
std::vector<member> population;
std::copy(initPopulation.begin(), initPopulation.begin()+POPSIZE, std::back_inserter(population));
recordData(population, 0, xyz, consolelog);
for(int g=1;g<=GENERATIONS;g++){
sort(population);
selectionCrossover(population);
//for(auto& m : Population){std::cout << m.fitness << " " << m.r << std::endl;}
mutate(population);
sort(population);
recordData(population,g,xyz,consolelog);
}
//std::cout << population[0].fitness << " " << population[0].particles[0].x << " "<< population[0].particles[1].x<< " " << population[0].particles[2].x<< std::endl;
xyz.close();
consolelog.close();
}