This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
externTestEval.cpp
193 lines (167 loc) · 6.1 KB
/
externTestEval.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
/*
Copyright (c) 2017 Institute Jožef Stefan, Jamova cesta 39, SI-1000, Ljubljana, Slovenija
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Please cite the following works (bibtex source below):
- DepolliAvbeljTrobec2008 for the simulator and simulation-based optimization
- DepolliTrobecFilipic2013 for the AMS-DEMO optimizer
- TrobecDepolliAvbelj2009 for the simulator
@article{DepolliAvbeljTrobec2008,
author = {Depolli, Matjaž and Avbelj, Viktor and Trobec, Roman},
title = {Computer-Simulated Alternative Modes of {U}-Wave Genesis},
journal = {Journal of Cardiovascular Electrophysiology},
volume = {19},
number = {1},
publisher = {Blackwell Publishing Inc},
issn = {1540-8167},
url = {http://dx.doi.org/10.1111/j.1540-8167.2007.00978.x},
doi = {10.1111/j.1540-8167.2007.00978.x},
pages = {84--89},
keywords = {U wave, ECG, action potential, repolarization, myocardium, computer simulation},
year = {2008}
}
@article{DepolliTrobecFilipic2013,
author = {Depolli, Matjaž and Trobec, Roman and Filipič, Bogdan},
title = {Asynchronous master-slave parallelization of differential evolution for multiobjective optimization},
journal = {Evolutionary Computation},
volume = {21},
number = {2},
pages = {261-291},
doi = {10.1162/EVCO_a_00076},
issn = {1063-6560},
url = {http://www.mitpressjournals.org/doi/abs/10.1162/EVCO_a_00076},
year = {2013}
}
@inproceedings{TrobecDepolliAvbelj2009,
title = {Simulation of {ECG} repolarization phase with improved model of cell action potentials},
author = {Trobec, Roman and Depolli, Matja{\v{z}} and Avbelj, Viktor},
booktitle = {International Joint Conference on Biomedical Engineering Systems and Technologies},
pages = {325--332},
year = {2009},
organization = {Springer}
}
*/
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
#include <iostream>
template<class T>
T sqr(T a) {
return a*a;
}
template<class Function>
class FunctionFit {
std::vector<double> targetValues;
std::vector<double> parameters;
std::vector<double> result;
size_t pairSize;
std::string directory;
public:
void loadTarget(const char* fname) {
std::ifstream binFile((fname + std::string(".bin")).c_str(), std::ios::binary);
if (binFile) {
// load from binary file
size_t tempSize;
binFile.read((char*)&pairSize, 4);
binFile.read((char*)&tempSize, 4);
targetValues.resize(tempSize);
for (size_t i = 0; i < targetValues.size(); ++i)
binFile.read((char*)&targetValues[i], sizeof(double));
} else {
// load from text file
pairSize = 2;
std::ifstream txtFile(fname);
double x, y;
while (txtFile) {
txtFile >> x;
txtFile.ignore(1);
txtFile >> y;
if (!txtFile)
break;
txtFile.ignore(1);
targetValues.push_back(x);
targetValues.push_back(y);
}
std::ofstream binOutFile((fname + std::string(".bin")).c_str(), std::ios::binary);
binOutFile.write((char*)&pairSize, 4);
size_t tempSize = targetValues.size();
binOutFile.write((char*)&tempSize, 4);
for (size_t i = 0; i < targetValues.size(); ++i)
binOutFile.write((char*)&targetValues[i], sizeof(double));
}
}
void loadInput() {
std::string fname("input.txt");
if (directory != "")
if ((directory[directory.size()-1] == '\\') || (directory[directory.size()-1] == '/'))
fname = directory + fname;
else
fname = directory + "/" + fname;
std::ifstream file(fname.c_str());
parameters.reserve(100);
for(;;) {
if (file.peek() == '#')
file.ignore(1000, '\n');
else {
double temp;
file >> temp;
if (!file)
break;
else
parameters.push_back(temp);
}
}
}
void writeOutput() {
std::string fname("output.txt");
if (directory != "")
if ((directory[directory.size()-1] == '\\') || (directory[directory.size()-1] == '/'))
fname = directory + fname;
else
fname = directory + "/" + fname;
std::ofstream file(fname.c_str());
file << "# writen by externTestEval\n";
for (size_t i = 0; i < result.size(); ++i) {
file << result[i] << " ";
}
}
void eval(const std::string& dir) {
directory = dir;
loadInput();
Function func;
result.clear();
result.resize(2, 0.0);
for (size_t i = 0; i < targetValues.size(); i += 2) {
result[0] += (sqr(func(parameters, targetValues[i]) - targetValues[i+1]) / targetValues[i+1]);
result[1] += func(parameters, targetValues[i]) - targetValues[i+1];
}
writeOutput();
}
};
struct TestFunc {
double operator() (const std::vector<double>& params, double in) {
return params[0] + params[1]*pow(M_E, params[2] * in);
}
};
int main(int argc, char ** argv) {
FunctionFit<TestFunc> fit;
std::string dir("");
if (argc > 1)
dir = argv[1];
fit.loadTarget("target.txt");
fit.eval(dir);
}