-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGAOptimize.C
More file actions
176 lines (153 loc) · 4.8 KB
/
Copy pathGAOptimize.C
File metadata and controls
176 lines (153 loc) · 4.8 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include "Evaluate.h"
#include "GAOptimize.h"
#include "Generation.h"
#include "GAInput.h"
#include "ReadIntegral.h"
using namespace std;
#include <boost/function.hpp>
#include <boost/bind.hpp>
#ifndef SERIAL
#include <boost/mpi.hpp>
namespace mpi = boost::mpi;
#endif
#include <newmat.h>
#include <newmatutils.h>
namespace genetic
{
GAInput gainput;
int Gene::m_length = 0;
boost::function<double(const Gene&)> Cell::Evaluate;
};
genetic::Cell genetic::gaordering(ifstream& confFile, ifstream& dumpFile, std::vector<int> fiedlerorder, bool simple)
{
#ifndef SERIAL
mpi::communicator world;
#endif
Matrix K;
double ksum = 0.0;
#ifndef SERIAL
if(world.rank() == 0)
{
#endif
if(confFile.is_open()) gainput.Configure(confFile);
if (simple) {
ReadKmatrix(dumpFile, K);
} else {
ReadIntegral(dumpFile, K);
}
Gene::Length() = K.Nrows();
if(gainput.max_cells == 0) gainput.max_cells = 2 * Gene::Length();
for(int i = 0; i < K.Nrows(); ++i)
for(int j = i + 1; j < K.Ncols(); ++j) ksum += K.element(i, j);
#ifndef SERIAL
}
mpi::broadcast(world, fiedlerorder, 0);
mpi::broadcast(world, gainput, 0);
mpi::broadcast(world, Gene::Length(), 0);
mpi::broadcast(world, K, 0);
mpi::broadcast(world, ksum, 0);
#endif
Cell::Evaluate = boost::bind(genetic::Evaluate, 1.0/ksum, gainput.exponent, _1, K);
Cell best;
#ifndef SERIAL
int ntask = 1 + gainput.max_community / world.size();
Cell comm_best = gaoptimize(genetic::gainput.random_seed+world.rank(), fiedlerorder);
cout << "Order #" << world.rank() << ": " << comm_best << endl;
for(int i = 1; i < ntask; ++i)
{
Cell comm_cell = gaoptimize(genetic::gainput.random_seed + i * world.size() + world.rank(), fiedlerorder);
cout << "Order #" << i * world.size() + world.rank() << ": " << comm_cell << endl;
if(comm_cell < comm_best) comm_best = comm_cell;
}
if(world.rank() == 0)
mpi::reduce(world, comm_best, best, mpi::minimum<Cell>(), 0);
else
mpi::reduce(world, comm_best, mpi::minimum<Cell>(), 0);
#else
int ntask = gainput.max_community;
best = gaoptimize(genetic::gainput.random_seed, fiedlerorder);
cout << "Order #" << 0 << ": " << best << endl;
for(int i = 1; i < ntask; ++i)
{
Cell comm_cell = gaoptimize(genetic::gainput.random_seed+i, fiedlerorder);
cout << "Order #" << i << ": " << comm_cell << endl;
if(comm_cell < best) best = comm_cell;
}
#endif
return best;
}
genetic::Cell genetic::gaordering_bcs(ifstream& confFile, ifstream& dumpFile, std::vector<int> fiedlerorder, bool simple)
{
#ifndef SERIAL
mpi::communicator world;
#endif
Matrix K;
double ksum = 0.0;
#ifndef SERIAL
if(world.rank() == 0)
{
#endif
if(confFile.is_open()) gainput.Configure(confFile);
if (simple) {
ReadKmatrix(dumpFile, K);
} else {
ReadIntegral_BCS(dumpFile, K);
}
Gene::Length() = K.Nrows();
if(gainput.max_cells == 0) gainput.max_cells = 2 * Gene::Length();
for(int i = 0; i < K.Nrows(); ++i)
for(int j = i + 1; j < K.Ncols(); ++j) ksum += K.element(i, j);
#ifndef SERIAL
}
mpi::broadcast(world, fiedlerorder, 0);
mpi::broadcast(world, gainput, 0);
mpi::broadcast(world, Gene::Length(), 0);
mpi::broadcast(world, K, 0);
mpi::broadcast(world, ksum, 0);
#endif
Cell::Evaluate = boost::bind(genetic::Evaluate, 1.0/ksum, gainput.exponent, _1, K);
Cell best;
#ifndef SERIAL
int ntask = 1 + gainput.max_community / world.size();
Cell comm_best = gaoptimize(genetic::gainput.random_seed+world.rank(), fiedlerorder);
cout << "Order #" << world.rank() << ": " << comm_best << endl;
for(int i = 1; i < ntask; ++i)
{
Cell comm_cell = gaoptimize(genetic::gainput.random_seed + i * world.size() + world.rank(), fiedlerorder);
cout << "Order #" << i * world.size() + world.rank() << ": " << comm_cell << endl;
if(comm_cell < comm_best) comm_best = comm_cell;
}
if(world.rank() == 0)
mpi::reduce(world, comm_best, best, mpi::minimum<Cell>(), 0);
else
mpi::reduce(world, comm_best, mpi::minimum<Cell>(), 0);
#else
int ntask = gainput.max_community;
best = gaoptimize(genetic::gainput.random_seed, fiedlerorder);
cout << "Order #" << 0 << ": " << best << endl;
for(int i = 1; i < ntask; ++i)
{
Cell comm_cell = gaoptimize(genetic::gainput.random_seed+i, fiedlerorder);
cout << "Order #" << i << ": " << comm_cell << endl;
if(comm_cell < best) best = comm_cell;
}
#endif
return best;
}
genetic::Cell genetic::gaoptimize(const int& seed, std::vector<int> fiedlerorder)
{
srand(seed);
Generation ancestor;
if (gainput.fiedler==1)
ancestor.AddFiedler(fiedlerorder);
for(int g = 0; g < gainput.max_generation; ++g)
{
Generation nextgen;
nextgen.Generate(ancestor);
ancestor = nextgen;
}
return ancestor.Min();
}