-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObjectiveFunction.hpp
157 lines (113 loc) · 5.62 KB
/
ObjectiveFunction.hpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: ObjectiveFunction.hpp
* Author: mattadmin
*
* Created on March 28, 2019, 9:32 AM
*/
#ifndef OBJECTIVEFUNCTION_HPP
#define OBJECTIVEFUNCTION_HPP
#include "third_party/ATL/lib/Optimization.hpp"
#include "Output.hpp"
namespace mas {
template<typename REAL_T>
class MASObjectiveFunction : public atl::ObjectiveFunction<REAL_T> {
public:
mas::MAS<REAL_T> mas_instance;
std::string data_path = "";
std::string config_path = "";
std::string ouput_path = "mas_output.json";
typedef typename mas::VariableTrait<REAL_T>::variable variable;
virtual void Initialize() {
#ifdef MAS_COMMAND_TOOL
mas_instance.Initialize(config_path, data_path);
#endif
for (int i = 0; i < mas_instance.info.estimated_parameters.size(); i++) {
this->RegisterParameter(*mas_instance.info.estimated_parameters[i], mas_instance.info.estimated_phase[i]);
}
for (int i = 0; i < mas_instance.info.random_variables.size(); i++) {
this->RegisterRandomVariable(*(mas_instance.info.random_variables[i]));
}
}
void SetVarianceCovariance() {
mas_instance.variance_covaiance = this->GetVarianceCovariance();
mas_instance.std_dev.Resize(mas_instance.variance_covaiance.GetRows());
for (int i = 0; i < mas_instance.std_dev.GetSize(); i++) {
mas_instance.std_dev(i) = std::sqrt(mas_instance.variance_covaiance(i, i));
}
}
virtual void Finalize() {
this->SetVarianceCovariance();
mas_instance.Finalize();
// mas_instance.Report();
mas::JSONOutputGenerator<REAL_T> json;
std::ofstream output(this->ouput_path.data());
output << json.GenerateOutput(mas_instance);
}
virtual void OutputVarCovar() {
std::ofstream out;
out.open("variance_covariance.txt");
std::ofstream csv_out;
csv_out.open("variance_covariance.csv");
std::cout.precision(6);
std::cout.unsetf(std::ios_base::fixed);
if (mas_instance.info.estimated_parameters.size() > 1) {
auto Mat_varcovar = this->GetVarianceCovariance();
std::cout << "\nVariance-Covariance Matrix for the Estimated Parameters\n";
std::cout << std::setw(20) << std::left << "Parameter" << " " << std::setw(40) << std::left << "Name" << std::setw(20) << "Value" << std::setw(20) << "StdDev";
out << "\nVariance-Covariance Matrix for the Estimated Parameters\n";
out << std::setw(20) << std::left << "Parameter" << " " << std::setw(40) << std::left << "Name" << std::setw(20) << "Value" << std::setw(20) << "StdDev";
csv_out << "\nVariance-Covariance Matrix for the Estimated Parameters\n";
csv_out << "Parameter ,Name,Value,StdDev,";
for (int i = 1; i <= mas_instance.info.estimated_parameters.size(); ++i) {
std::cout << std::setw(20) << i;
out << std::setw(20) << i;
csv_out << i << ",";
}
std::cout << "\n";
out << "\n";
csv_out << "\n";
for (int i = 0; i < mas_instance.info.estimated_parameters.size(); i++) {
std::cout << std::setw(20) << std::left << (i + 1) << " ";
std::cout << std::setw(40) << std::left << mas_instance.info.estimated_parameters[i]->GetName();
std::cout << std::setw(20) << mas_instance.info.estimated_parameters[i]->info->value;
std::cout << std::setw(20) << std::sqrt(Mat_varcovar(i, i));
out << std::setw(20) << std::left << (i + 1) << " ";
out << std::setw(40) << std::left << mas_instance.info.estimated_parameters[i]->GetName();
out << std::setw(20) << mas_instance.info.estimated_parameters[i]->info->value;
out << std::setw(20) << std::sqrt(Mat_varcovar(i, i));
// std::cout << std::setw(20) << atl::Variable<double>::tape.Value(mas.info.estimated_parameters[i]->info->id);
csv_out << (i + 1) << ",";
csv_out << mas_instance.info.estimated_parameters[i]->GetName() << ",";
csv_out << mas_instance.info.estimated_parameters[i]->info->value << ",";
csv_out << std::sqrt(Mat_varcovar(i, i)) << ",";
for (int j = 0; j < mas_instance.info.estimated_parameters.size(); ++j) {
std::cout << std::setw(20) << Mat_varcovar(i, j);
out << std::setw(20) << Mat_varcovar(i, j);
csv_out << Mat_varcovar(i, j) << ",";
}
std::cout << "\n";
out << "\n";
csv_out << "\n";
}
std::cout << "\n\n";
out << "\n\n";
}
}
virtual const atl::Variable<REAL_T> Evaluate() {
variable f;
mas_instance.phase = this->phase_m;
mas_instance.Run(f);
return f;
}
virtual void Objective_Function(atl::Variable<REAL_T>& f) {
mas_instance.phase = this->phase_m;
mas_instance.Run(f);
}
};
}
#endif /* OBJECTIVEFUNCTION_HPP */