← Documentation
ADS-IDAC Engine 5.0.0
EventSymptomMatrix.cpp
Go to the documentation of this file.
1// EventSymptomMatrix.cpp: implementation of the EventSymptomMatrix class.
2//
4
6#include "KBHardWiredUnit.h"
7#include "../Branch/BranchRecord.h"
8#include <boost/serialization/export.hpp>
9#include "../Global/SimulationContext.h"
10#include <boost/archive/binary_oarchive.hpp>
11#include <boost/archive/binary_iarchive.hpp>
12#include <cstdint>
13#include <stdexcept>
14BOOST_CLASS_EXPORT_IMPLEMENT(EventSymptomMatrix)
15using namespace std;
16
17void EventSymptomMatrix::state_save(boost::archive::binary_oarchive& ar) const {
18 const uint32_t n_sym = static_cast<uint32_t>(symptoms.size());
19 const uint32_t n_evt = static_cast<uint32_t>(events.size());
20 ar << abnormal_confidence << n_sym << n_evt;
21 for (const auto& s : symptoms) { const double c = s->getConfidence(); ar << c; }
22 for (const auto& e : events) { const double c = e->getConfidence(); ar << c; }
23}
24void EventSymptomMatrix::state_load(boost::archive::binary_iarchive& ar) {
25 uint32_t n_sym = 0, n_evt = 0;
26 ar >> abnormal_confidence >> n_sym >> n_evt;
27 if (n_sym != symptoms.size() || n_evt != events.size())
28 throw std::runtime_error("EventSymptomMatrix state overlay: unit counts mismatch against the seeded matrix");
29 double c = 0.0;
30 for (auto& s : symptoms) { ar >> c; s->setConfidence(c); }
31 for (auto& e : events) { ar >> c; e->setConfidence(c); }
32}
33
34
36// Construction/Destruction
38
40{
41 num_symptom = 0;
42 num_event = 0;
44}
45
47{
49}
51{
52 num_symptom = 0;
53 num_event = 0;
55 symptoms.clear();
56 events.clear();
57}
65 string str;
66 int i;
67
68 ifstream fin(_file_name.c_str(), ios::in);
69 if(!fin.is_open()){
70 cout << "File: " << _file_name << " cound not be found" << endl;
71 ofstream fout("err messages.txt", ios::app);
72 fout << r5pars_.timesno << " File: " << _file_name << " cound not be found\n";
73 fout.close();
74 r5pars_.sim_err = true;
75 g_sim.Branch_center.setTempESMessage("SPDS::readInfoFromFile err.");
77 }
78 else {
79 fin >> str >> num_symptom;
80 for(i = 0; i < num_symptom; i++){
81 auto symptoms_ptr = make_unique<SymptomUnit>();
82 symptoms_ptr->readInfoFromFile(fin);
83 symptoms.push_back(std::move(symptoms_ptr));
84 }
85 fin >> str >> num_event;
86 for(i = 0; i < num_event; i++){
87 auto events_ptr = make_unique<DiagnosisEventUnit>();
88 events_ptr->readInfoFromFile(num_symptom, fin);
89 events.push_back(std::move(events_ptr));
90 }
91 fin.close();
92 }
93}
94
95
97 bool found_hwd;
98 double x;
99 decltype(_kb.hard_wired_diagnosis)::iterator hwd_itr;//hard_wired_diagnosis;
100 string symptom_name;
101 for(auto& symptom : symptoms){
102 symptom_name = symptom->getSymptomName();
103 found_hwd = false;
104 hwd_itr = _kb.hard_wired_diagnosis.begin();
105 do{
106 if(symptom_name == (*hwd_itr)->getInfoName()){//found symptom
107 found_hwd = true;
108 // get symptom confidence from hardwired diagnosis and convert from percent to probability
109 x = ((*hwd_itr)->getConfidenceLevel());
110 symptom->setConfidence(x);
111 }
112 else{
113 hwd_itr++;
114 }
115 }while(!found_hwd && hwd_itr != _kb.hard_wired_diagnosis.end());
116 if(!found_hwd){//could not find the symptom
117 //error message
118 ofstream fout("err messages.txt", ios::app);
119 cout << "EventSymptomMatrix::updateRationalSymptomConfidence symptom: " << symptom_name << " could not be found." << endl;
120 fout << r5pars_.timesno << " EventSymptomMatrix::updateRationalSymptomConfidence symptom: " << symptom_name << " could not be found.\n";
121 fout.close();
122 r5pars_.sim_err = true;
123 g_sim.Branch_center.setTempESMessage("EventSymptomMatrix::updateRationalSymptomConfidence err.");
125 }
126 }
127}
129 DiagnosisEventUnit *events_pt;
130 int i, j, num_perceived_symptoms, num_symptoms_explained;
131 double x,symptom_confidence, relationship_value, upper, lower, correction;
132 //abnormal_confidence = 0.0;
133 num_perceived_symptoms = 0;
134 for (i=0;i<symptoms.size();i++) {
135 if (symptoms.at(i)->getConfidence() > 0.1) {
136 num_perceived_symptoms = num_perceived_symptoms + 1;
137 }
138 }
139 for(j = 0; j < events.size(); j++){
140 upper = 1.0;
141 lower = 1.0;
142 num_symptoms_explained = 0;
143 events_pt = events.at(j).get();
144 for(i = 0; i < symptoms.size(); i++){
145 symptom_confidence = symptoms.at(i)->getConfidence();
146 relationship_value = events_pt->symptom_event_relationship.at(i);
147 if ((relationship_value * symptom_confidence ) > 0.01) {
148 num_symptoms_explained = num_symptoms_explained + 1;
149 }
150 upper = fmin(upper, fmax(0.0, fmin(1.0, symptom_confidence + 1.0 - relationship_value)));
151 if(relationship_value > 0.0001){
152 x = 1.0;
153 }
154 else{
155 x = 0.0;
156 }
157// x = (relationship_value > 0.0001 );
158 if (x > symptom_confidence) {
159 lower = fmin(lower, symptom_confidence);
160 }
161 }
162 if (events_pt->getCategoryName() != "Imbalance") {
163 correction = num_symptoms_explained / (num_perceived_symptoms + 0.0001);
164 } else {
165 correction = 1.0;
166 }
167 events_pt->setLowerBound(lower);
168 events_pt->setUpperBound(upper);
169 events_pt->setConfidence((lower + upper) * correction /2.0);
170 // update overall plant assessment
171 if (events_pt->getCategoryName() == "DBA" || events_pt->getCategoryName() == "AOO") {
173 }
174 }
175}
176
178 bool found = false;
179 double confidence = 0.0;
180 if (_str == "Not_Applicable")
181 {
182 confidence = 1.0;
183 } else {
184 auto event_itr = this->events.begin();
185 while (event_itr != this->events.end() && ! found) {
186 if ((*event_itr)->getDiagnosisName() == _str) {
187 found = true;
188 confidence = (*event_itr)->getConfidence();
189 }
190 else {
191 event_itr ++;
192 }
193 }
194 if(!found) { //could not find a match to the requested event
195 //error message
196 ofstream fout("err messages.txt", ios::app);
197 cout << "EventSymptomMatrix::getEventRationalConfidence: " << _str << " could not be found." << endl;
198 fout << r5pars_.timesno << " EventSymptomMatrix::getEventRationalConfidence: " << _str << " could not be found.\n";
199 }
200 }
201 return confidence;
202}
203
205}
207}
208
212}
213
215 int i;
216 string str1;
217 for(i = 0; i < symptoms.size(); i++){
218 str1 = _str + symptoms.at(i)->getSymptomName();
219 fout << '\t' << str1;
220 }
221 for(i = 0; i < events.size(); i++){
222 str1 = _str + events.at(i)->getDiagnosisName();
223 fout << '\t' << str1;
224// << '\t' << str1
225// << '\t' << str1;
226 }
227}
228
230 int i;
231 for(i = 0; i < symptoms.size(); i++){
232 fout << "\tConfidence";
233 }
234 for(i = 0; i < events.size(); i++){
235// fout << "\tConfidence\tLower_B\tUpper_B";
236 fout << "\tConfidence";
237 }
238}
239
241 num_symptom = _esm.getNumSymptom();
242 num_event = _esm.getNumEvent();
244 for(auto& src : _esm.symptoms){
245 auto symptoms_pt = make_unique<SymptomUnit>();
246 symptoms_pt->copySymptomUnit(*src);
247 this->symptoms.push_back(std::move(symptoms_pt));
248 }
249 for(auto& src : _esm.events){
250 auto events_pt = make_unique<DiagnosisEventUnit>();
251 events_pt->copyDiagnosisEventUnit(*src);
252 this->events.push_back(std::move(events_pt));
253 }
254}
255
257 fout << "EventSymptomMatrix "
258 << num_symptom << ' '
259 << num_event << ' '
260 << abnormal_confidence << ' '
261 << symptoms.size() << ' '
262 << events.size() << '\n';
263 for(auto& symptom : symptoms){
264 symptom->saveSymptomUnitToFile(fout);
265 }
266 for(auto& event : events){
267 event->saveDiagnosisEventUnitToFile(fout);
268 }
269}
270
272 int tn_symptom, tn_event, i;
273 string str;
274 fin >> str
275 >> num_symptom
276 >> num_event
278 >> tn_symptom
279 >> tn_event;
280 if (str != "EventSymptomMatrix"){
281 ofstream fout("err messages.txt", ios::app);
282 cout << "EventSymptomMatrix::retrieveEventSymptomMatrixFromFile retrieval error in previous function" << endl;
283 fout << r5pars_.timesno << " EventSymptomMatrix::retrieveEventSymptomMatrixFromFile error in previous function\n";
284 fout.close();
285 r5pars_.sim_err = true;
286 g_sim.Branch_center.setTempESMessage("EventSymptomMatrix::retrieveEventSymptomMatrixFromFile Prior err.");
288 return;
289 }
290 for(i = 0; i < tn_symptom; i++){
291 auto symptoms_pt = make_unique<SymptomUnit>();
292 symptoms_pt->retrieveSymptomUnitFromFile(fin);
293 this->symptoms.push_back(std::move(symptoms_pt));
294 }
295 for(i = 0; i < tn_event; i++){
296 auto events_pt = make_unique<DiagnosisEventUnit>();
297 events_pt->retrieveDiagnosisEventUnitFromFile(fin);
298 this->events.push_back(std::move(events_pt));
299 }
300}
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
document e
void setTempTypeOfConsequence(Consequence)
Stores the consequence of the end state the running sequence will get.
void setTempESMessage(std::string)
Stores the description of the end state the running sequence will get if it is terminated (set by mod...
std::vector< double > symptom_event_relationship
std::string getCategoryName()
void copyEventSymptomMatrix(EventSymptomMatrix &)
double getEventRationalConfidence(std::string)
void updateBothRationalConfidence(KB &)
void setAbnormalConfidence(double)
void outputEventSymptomMatrixLabelToFile(std::string, std::ofstream &)
void readInfoFromFile(std::string)
void saveEventSymptomMatrixToFile(std::ofstream &)
std::vector< std::unique_ptr< DiagnosisEventUnit > > events
void outputEventSymptomMatrixUnitToFile(std::ofstream &)
void state_save(boost::archive::binary_oarchive &ar) const
void retrieveEventSymptomMatrixFromFile(std::ifstream &)
std::vector< std::unique_ptr< SymptomUnit > > symptoms
void updateRationalSymptomConfidence(KB &)
void state_load(boost::archive::binary_iarchive &ar)
Definition: KB.h:46
std::vector< std::unique_ptr< KBHardWiredUnit > > hard_wired_diagnosis
Definition: KB.h:84
struct @0 r5pars_
BranchRecord Branch_center
Dynamic event tree state.