Page MenuHomePhorge

No OneTemporary

Size
142 KB
Referenced Files
None
Subscribers
None
This document is not UTF8. It was detected as ISO-8859-1 (Latin 1) and converted to UTF8 for display.
diff --git a/Version_Max_07_05_2018_CMake/src/State.cpp b/Version_Max_07_05_2018_CMake/src/State.cpp
index 07e036f..d8a52cf 100755
--- a/Version_Max_07_05_2018_CMake/src/State.cpp
+++ b/Version_Max_07_05_2018_CMake/src/State.cpp
@@ -1,487 +1,514 @@
#include "State.h"
#include "printError.h"
#include "relationChecker.h"
#include "minmaxzeug.h"
#define INJECTIONPARTITIONING 10
-#define PRINT
+//#define PRINT
State::State() {
//discreteAveragePartitionSize = INJECTIONPARTITIONING;
discreteAveragePartitionCounter = 0;
stateIsValid = false;
+ stateIsValidAfterReentrence = false;
}
/*
bool State::setDiscreteAveragePartitionSize(unsigned int discreteAveragePartitionSize) {
if (discreteAveragePartitionSize > 0) {
this->discreteAveragePartitionSize = discreteAveragePartitionSize;
return true;
}
return false;
}
unsigned int State::getDiscreteAveragePartitionSize() {
return discreteAveragePartitionSize;
}
*/
bool State::addSubState(vector<SubState*>* vSubStates, SlaveAgentSlotOfAgent* slot) {
SubState* subState = new (nothrow) SubState();
if (subState != NULL) {
subState->setSlot(slot);
try {
vSubStates->push_back(subState);
return true;
}
catch (bad_alloc& error) {
printError("bad_alloc caught: ", error.what());
delete subState;
}
}
return false;
}
bool State::addInputSubState(SlaveAgentSlotOfAgent* slot) {
return addSubState(&vInputSubStates, slot);;
}
bool State::addOutputSubState(SlaveAgentSlotOfAgent* slot) {
return addSubState(&vOutputSubStates, slot);
}
void State::resetDiscreteAveragePartitionCounter() {
discreteAveragePartitionCounter = 0;
}
bool State::addNewdiscreteAveragePartition() {
bool flagWorkedForAll = true;
for (auto &subState : vInputSubStates) {
if (!subState->addNewDiscreteAverage())
flagWorkedForAll = false;
}
for (auto &subState : vOutputSubStates) {
if (!subState->addNewDiscreteAverage())
flagWorkedForAll = false;
}
return flagWorkedForAll;
}
bool State::injectValues(unsigned int discreteAveragePartitionSize) {
bool flagWorkedForAll = true;
-
+
if (discreteAveragePartitionCounter == 0) {
for (auto &subState : vInputSubStates) {
subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize);
}
for (auto &subState : vOutputSubStates) {
subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize);
}
flagWorkedForAll = addNewdiscreteAveragePartition();
}
if (flagWorkedForAll) {
discreteAveragePartitionCounter++;
// XXX - >= or > ??
if (discreteAveragePartitionCounter >= discreteAveragePartitionSize) {
discreteAveragePartitionCounter = 0;
}
for (auto &subState : vInputSubStates) {
if (subState->injectValue())
flagWorkedForAll = false;
}
for (auto &subState : vOutputSubStates) {
if (subState->injectValue())
flagWorkedForAll = false;
}
#ifdef PRINT
printf(" >>> Inject Values (partCounter: %u)\n", discreteAveragePartitionCounter);
#endif // PRINT
//getchar();
}
return flagWorkedForAll;
}
bool State::injectValuesAndMakeNewDiscreteAveragePartition(unsigned int discreteAveragePartitionSize) {
discreteAveragePartitionCounter = 0;
return injectValues(discreteAveragePartitionSize);
}
bool State::variablesAreRelated(vector<SubState*>* vSubStates, float thresholdToBeRelated) {
bool flagAllValuesAreRelated = true;
-
+
for (auto &subState : *vSubStates) {
if (!subState->valueIsRelated(thresholdToBeRelated)) {
flagAllValuesAreRelated = false;
}
}
return flagAllValuesAreRelated;
}
bool State::inputVariablesAreRelated(float thresholdToBeRelated) {
return variablesAreRelated(&vInputSubStates, thresholdToBeRelated);
}
bool State::outputVariablesAreRelated(float thresholdToBeRelated) {
return variablesAreRelated(&vOutputSubStates, thresholdToBeRelated);
}
unsigned int State::getNumOfInjections() {
if (!vInputSubStates.empty()) {
return vInputSubStates.front()->getNumOfInjections();
}
return 0;
}
bool State::checkSubStatesForNotDrifting(vector<SubState*>* vSubStates, unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift) {
for (auto &subState : *vSubStates) {
if (subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > 1) {
//printf("completed blocks = %u\n", subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize));
//getchar();
if (!valueIsRelatedToReferenceValue(subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), thresholdNotDrift)) {
//if (!valueIsRelatedToReferenceValue(subState->getDiscreteAverageOfBlockBeforeLastBlock(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition), subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), thresholdNotDrift)) {
-
+
return false;
}
}
}
//getchar();
return true;
}
bool State::checkAllVariablesForNotDrifting(unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift) {
-
+
return checkSubStatesForNotDrifting(&vInputSubStates, discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift) && checkSubStatesForNotDrifting(&vOutputSubStates, discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift);
}
//DATE18
float State::checkSubStatesForDriftingFuzzy(vector<SubState*>* vSubStates, unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift) {
float confidenceDriftMax = 0;
for (auto &subState : *vSubStates) {
-
+
if (subState->getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > 1) {
-
+
float confidenceDrift = Drift->getY(deviationValueReferenceValue(subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize)));
#ifdef PRINT
printf("confDrift = %f, deviationValueReferenceValue = %f\n", confidenceDrift, deviationValueReferenceValue(subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize)));
#endif // PRINT
if (confidenceDrift > confidenceDriftMax)
confidenceDriftMax = confidenceDrift;
}
}
return confidenceDriftMax;
}
//DATE18
float State::checkAllVariablesForDriftingFuzzy(unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift) {
float confidenceDriftInput = checkSubStatesForDriftingFuzzy(&vInputSubStates, discreteAveragePartitionSize, Drift);
float confidenceDriftOutput = checkSubStatesForDriftingFuzzy(&vOutputSubStates, discreteAveragePartitionSize, Drift);
if (confidenceDriftInput > confidenceDriftOutput)
return confidenceDriftInput;
else
return confidenceDriftOutput;
}
//DATE18
float State::variablesAreRelatedFuzzy(vector<SubState*>* vSubStates, LinearFunctionBlock* SameState) {
float confRelatedMin = 1;
for (auto &subState : *vSubStates) {
-
+
float confRelated = subState->valueIsRelatedFuzzy(SameState);
#ifdef PRINT
printf("conf %f\n", confRelated);
#endif // PRINT
if (confRelated < confRelatedMin)
confRelatedMin = confRelated;
}
return confRelatedMin;
}
float State::inputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState) {
return variablesAreRelatedFuzzy(&vInputSubStates, SameState);
}
float State::outputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState) {
return variablesAreRelatedFuzzy(&vOutputSubStates, SameState);
}
bool State::insertValueInState(LinearFunctionBlock* FuncBlockConfValStateDev, LinearFunctionBlock* FuncBlockConfInvStateDev, LinearFunctionBlock* FuncBlockConfValStateTime, LinearFunctionBlock* FuncBlockConfInvStateTime, unsigned int historySize, unsigned int discreteAveragePartitionSize) {
//bool insertionWorked = true;
if (discreteAveragePartitionCounter == 0) {
for (auto &subState : vInputSubStates)
subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize);
for (auto &subState : vOutputSubStates)
subState->deleteLastDiscreteAverageBlockIfNotCompleted(discreteAveragePartitionSize);
//insertionWorked = addNewdiscreteAveragePartition();
addNewdiscreteAveragePartition();
}
discreteAveragePartitionCounter++;
if (discreteAveragePartitionCounter >= discreteAveragePartitionSize)
discreteAveragePartitionCounter = 0;
-
+
confValidState = 1;
confInvalidState = 0;
for (auto &subState : vInputSubStates) {
//if (!(subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize)))
//insertionWorked = false;
subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize);
-
+
confValidState = fuzzyAND(confValidState, subState->getConfidenceValidState());
confInvalidState = fuzzyOR(confInvalidState, subState->getConfidenceInvalidState());
}
for (auto &subState : vOutputSubStates) {
//if (!(subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize)))
//insertionWorked = false;
subState->insertValueInSubState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, historySize);
-
+
confValidState = fuzzyAND(confValidState, subState->getConfidenceValidState());
confInvalidState = fuzzyOR(confInvalidState, subState->getConfidenceInvalidState());
}
#ifdef PRINT
printf("confValidState %f\nconfInvalidState %f\n", confValidState, confInvalidState);
#endif // PRINT
//getchar();
if (confValidState > confInvalidState) {
#ifdef PRINT
printf("VALID STATE\n");
#endif // PRINT
stateIsValid = true;
+ stateIsValidAfterReentrence = true;
return true;
}
+ else {
+ stateIsValidAfterReentrence = false;
+ return false;
+ }
return false;
//return insertionWorked;
}
bool State::insertValueInState(float confValid, float confInvalid, unsigned int historySize, unsigned int discreteAveragePartitionSize) {
-
+
return true;
}
float State::getConfInputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) {
return getConfVarAreSim2State(&vInputSubStates, FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
}
float State::getConfInputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) {
return getConfVarAreDif2State(&vInputSubStates, FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
}
float State::getConfOutputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) {
return getConfVarAreSim2State(&vOutputSubStates, FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
}
float State::getConfOutputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) {
return getConfVarAreDif2State(&vOutputSubStates, FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
}
unsigned int State::getLengthOfHistory() {
if (!vInputSubStates.empty()) {
#ifdef PRINT
printf("historyLength: %u\n", vInputSubStates.front()->getSampleHistoryLength());
#endif // PRINT
return vInputSubStates.front()->getSampleHistoryLength();
}
return 0;
}
bool State::isStateValid() {
return stateIsValid;
}
float State::getConfStateValid() {
return confValidState;
}
float State::getConfStateInvalid() {
return confInvalidState;
}
+//new revision cam
+bool State::isStateValidAfterReentrance() {
+ return stateIsValidAfterReentrence;
+}
+
+//new revision cam
+void State::resetNumOfInsertedSamples() {
+
+ for (auto &subState : vInputSubStates) {
+ subState->resetNumOfInsertedSamples();
+ }
+ for (auto &subState : vOutputSubStates) {
+ subState->resetNumOfInsertedSamples();
+ }
+
+ stateIsValidAfterReentrence = false;
+
+}
+
+
+
//new
float State::getConfVarAreSim2State(vector<SubState*>* vSubStates, LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) {
float lowestConfOfAllVarAreRelated = 1;
- for (auto &subState : *vSubStates)
+ for (auto &subState : *vSubStates)
lowestConfOfAllVarAreRelated = fuzzyAND(lowestConfOfAllVarAreRelated, subState->getConfVarIsSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime));
return lowestConfOfAllVarAreRelated;
}
float State::getConfVarAreDif2State(vector<SubState*>* vSubStates, LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) {
float highestConfOfAllVarAreNotRelated = 0;
for (auto &subState : *vSubStates)
highestConfOfAllVarAreNotRelated = fuzzyOR(highestConfOfAllVarAreNotRelated, subState->getConfVarIsDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime));
return highestConfOfAllVarAreNotRelated;
}
bool State::delete_All_Input_Substates()
{
SubState* cur_Sub_State;
unsigned int index_cur_Sub_State;
unsigned int size_vInSubStates = vInputSubStates.size();
for(index_cur_Sub_State = 0; index_cur_Sub_State < size_vInSubStates; index_cur_Sub_State++){
cur_Sub_State = vInputSubStates[index_cur_Sub_State];
delete cur_Sub_State;
}
vInputSubStates.clear();
return true; //added by Ali, it is an error in VS.
}
bool State::delete_All_Output_Substates()
{
SubState* cur_Sub_State;
unsigned int index_cur_Sub_State;
unsigned int size_vOutSubStates = vOutputSubStates.size();
for(index_cur_Sub_State = 0; index_cur_Sub_State < size_vOutSubStates; index_cur_Sub_State++){
cur_Sub_State = vOutputSubStates[index_cur_Sub_State];
delete cur_Sub_State;
}
vOutputSubStates.clear();
return true; //added by Ali, it is an error in VS.
}
State::~State()
{
delete_All_Input_Substates();
delete_All_Output_Substates();
}
/*
bool State :: setInjectionPartitioning(unsigned int injectionPartitioning) {
if (injectionPartitioning > 0) {
this->injectionPartitioning = injectionPartitioning;
return true;
}
return false;
}
unsigned int State :: getInjectionPartitioning() {
return injectionPartitioning;
}
bool State :: addDiscreteAveragePartition() {
AverageValue* avg = new AverageValue();
if (avg != NULL) {
try {
vDiscreteAveragePartition.push_back(avg);
return true;
}
catch (bad_alloc& error) {
printError("bad_alloc caught: ", error.what());
delete avg;
}
}
return false;
}
bool State :: injectValue(float value) {
AverageValue* avg = NULL;
continuousStatisticValue.injectAndCalculateExtremeValue(value);
- //injectionCounter++;
+ //injectionCounter++;
if (injectionPartitionCounter == 0) {
if (addDiscreteAveragePartition()) {
injectionPartitionCounter++;
avg = vDiscreteAveragePartition.back();
}
}
else {
avg = vDiscreteAveragePartition.back();
}
if (avg != NULL) {
avg->injectAndCalculateAverageValue(value);
if (injectionPartitionCounter > injectionPartitioning) {
injectionPartitionCounter = 0;
}
return true;
}
return false;
}
bool State :: valueIsRelated(float value, float thresholdToAverage) {
float diff;
float avg = continuousStatisticValue.getAverageValue();
printf("value: %f, avg: %f, th: %f\n", value, avg, thresholdToAverage);
if (value > avg)
diff = value - avg;
else
diff = avg - value;
if (diff / avg <= thresholdToAverage)
return true;
return false;
}
bool State :: isNew() {
if (continuousStatisticValue.getInjectedValuesCounter() == 0)
return true;
return false;
}
unsigned int State :: getNumberOfInjections() {
return continuousStatisticValue.getInjectedValuesCounter();
}
void State :: deleteState() {
vDiscreteAveragePartition.swap(vDiscreteAveragePartition);
}
*/
diff --git a/Version_Max_07_05_2018_CMake/src/State.h b/Version_Max_07_05_2018_CMake/src/State.h
index 3bbba99..59f4821 100755
--- a/Version_Max_07_05_2018_CMake/src/State.h
+++ b/Version_Max_07_05_2018_CMake/src/State.h
@@ -1,114 +1,119 @@
#ifndef STATE_HEADERFILE
#define STATE_HEADERFILE
#include "SlaveAgentSlotOfAgent.h"
#include "SubState.h"
#include <vector>
#include "LinearFunctionBlock.h"
class State {
private:
vector<SubState*> vInputSubStates;
vector<SubState*> vOutputSubStates;
//unsigned int discreteAveragePartitionSize;
unsigned int discreteAveragePartitionCounter;
/*
StatisticValue continuousStatisticValue;
vector<AverageValue*> vDiscreteAveragePartition;
bool addDiscreteAveragePartition();
*/
bool addSubState(vector<SubState*>* vSubStates, SlaveAgentSlotOfAgent* slot);
bool variablesAreRelated(vector<SubState*>* vSubStates, float thresholdToBeRelated);
bool checkSubStatesForNotDrifting(vector<SubState*>* vSubStates, unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift);
float confValidState;
float confInvalidState;
bool stateIsValid;
+ //tells if state is valid after reentrance (this means this state was already known)
+ bool stateIsValidAfterReentrence;
+
bool delete_All_Input_Substates();
bool delete_All_Output_Substates();
public:
State();
/*
bool setDiscreteAveragePartitionSize(unsigned int discreteAverage);
unsigned int getDiscreteAveragePartitionSize();
*/
bool addInputSubState(SlaveAgentSlotOfAgent* slot);
bool addOutputSubState(SlaveAgentSlotOfAgent* slot);
void resetDiscreteAveragePartitionCounter();
bool addNewdiscreteAveragePartition();
bool injectValues(unsigned int discreteAveragePartitionSize);
bool injectValuesAndMakeNewDiscreteAveragePartition(unsigned int discreteAveragePartitionSize);
bool inputVariablesAreRelated(float thresholdToBeRelated);
bool outputVariablesAreRelated(float thresholdToBeRelated);
unsigned int getNumOfInjections();
bool checkAllVariablesForNotDrifting(unsigned int discreteAveragePartitionSize, unsigned int compareDistanceDiscreteAveragePartition, float thresholdNotDrift);
//DATE18
float checkSubStatesForDriftingFuzzy(vector<SubState*>* vSubStates, unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift);
float checkAllVariablesForDriftingFuzzy(unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift);
float variablesAreRelatedFuzzy(vector<SubState*>* vSubStates, LinearFunctionBlock* SameState);
float inputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState);
float outputVariablesAreRelatedFuzzy(LinearFunctionBlock* SameState);
bool insertValueInState(LinearFunctionBlock* FuncBlockConfValStateDev, LinearFunctionBlock* FuncBlockConfInvStateDev, LinearFunctionBlock* FuncBlockConfValStateTime, LinearFunctionBlock* ConfInvStateTime, unsigned int historySize, unsigned int discreteAveragePartitionSize);
bool insertValueInState(float confValid, float confInvalid, unsigned int historySize, unsigned int discreteAveragePartitionSize);
float getConfInputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime);
float getConfInputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime);
float getConfOutputVarAreSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime);
float getConfOutputVarAreDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime);
unsigned int getLengthOfHistory();
bool isStateValid();
float getConfStateValid();
float getConfStateInvalid();
~State();
+ bool isStateValidAfterReentrance();
+ void resetNumOfInsertedSamples();
private:
float getConfVarAreSim2State(vector<SubState*>* vSubStates, LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime);
float getConfVarAreDif2State(vector<SubState*>* vSubStates, LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime);
/*
bool injectValue(float value);
bool valueIsRelated(float value, float thresholdToAverage);
bool isNew();
unsigned int getNumberOfInjections();
void deleteState();
*/
};
#endif
diff --git a/Version_Max_07_05_2018_CMake/src/StateHandler.cpp b/Version_Max_07_05_2018_CMake/src/StateHandler.cpp
index 9262107..aa88186 100755
--- a/Version_Max_07_05_2018_CMake/src/StateHandler.cpp
+++ b/Version_Max_07_05_2018_CMake/src/StateHandler.cpp
@@ -1,1851 +1,1206 @@
#include "StateHandler.h"
#include "config.h"
-#include <algorithm>
#include "printError.h"
#include "rlutil.h"
+#include <algorithm>
#include "relationChecker.h"
-#include "minmaxzeug.h"
#include "file_util.h"
+#include "minmaxzeug.h"
-
-#include <iostream>
#include <ctime>
+#include <iostream>
-//CHANGE ALSO BOTH FUZZY FUNCTION!!!
-// is used for the history length of the number of values which will be compared
-//to the current value
-#define MAX_STATE_HISTORY_LENGTH 10 //10
-#define STOP_WHEN_BROKEN
-#define STOP_AFTER_BROKEN
-#define STOP_WHEN_DRIFT
-#define STOP_WHEN_STATE_VALID
+//#define PRINT
+#define WRITE_MORE_INFORMATION_IN_CSV
-#define PRINT
+#ifdef PRINT
+//#define STOP_WHEN_BROKEN
+//#define STOP_AFTER_BROKEN
+//#define STOP_WHEN_DRIFT
+//#define STOP_WHEN_STATE_VALID
+//#define STOP_WHEN_CHANGING_IN_OLD_STATE
+//#define STOP_EVERY_TIME
+//#define STOP_EVERY_TIME_AFTER_SAMPLENR 500000
+#endif
-//TODO: also change also hardcoded value in "SlaveAgentHandlerOfAgent.cpp"
-#define SLIDINGWINDOWSIZE 3 //3 //10
-#define STABLENUMBER 2 //2 //8
-#define STABLETHRESHOLD (float)0.04 //0.4 //0.03
-#define RELATEDTHRESHOLD (float)0.08 //0.08
+// STANDARD_STATE_HISTORY_LENGTH is used for the history length of the number of
+// values which will be compared to the current value
+// NOTE: Fuzzy Functions FuncBlockConfSim2StateTime, FuncBlockConfDif2StateTime,
+// FuncBlockConfValStateTime, and FuncBlockConfInvStateTime should have the same
+// length NOTE: You can set the History Length with the function
+// setMaxStateHistoryLength()
+#define STANDARD_STATE_HISTORY_LENGTH 10 // 10
#define INJECTIONPARTITIONING 5
-#define CMPDISTANCE 3
-#define THDRIFT (float)0.08 //0.8
-
-#define MINNUMTOBEVALIDSTATE 11 //11 //8 //10
-
-//three different status are for the system possible
-#define STATUS_BROKEN 1
-#define STATUS_DRIFT 2
-#define STATUS_OKAY 3
+// three different status are for the system possible
+#define STATUS_BROKEN 1
+#define STATUS_DRIFT 2
+#define STATUS_OKAY 3
using namespace rlutil;
-//TODO überladen mit Parameter CSV-Writer Name. :-) Bitte, nur für dich Maxi.
void StateHandler::initStateHandler() {
-
- csv_writer = NULL;
-
- flagVariablesWereStable = false;
- slidingWindowBufferSize = SLIDINGWINDOWSIZE;
- minNumOfRelatedValuesToBeStable = STABLENUMBER;
- thresholdToBeStable = STABLETHRESHOLD;
-
- thresholdToBeRelated = RELATEDTHRESHOLD;
+ csv_writer = NULL;
- discreteAveragePartitionSize = INJECTIONPARTITIONING;
- compareDistanceDiscreteAveragePartition = CMPDISTANCE;
- thresholdNotDrift = THDRIFT;
+ discreteAveragePartitionSize = INJECTIONPARTITIONING;
- minNumToBeValidState = MINNUMTOBEVALIDSTATE;
+ activeState = NULL;
- activeState = NULL;
+ // TODO: Question from Maxi: Do we need following (outcommented block)? I
+ // (maxi) commented it out.
+ /*
+ time_t rawtime;
+ struct tm timeinfo;
- maxStateHistoryLength = MAX_STATE_HISTORY_LENGTH;
+ // char output_file_name[200];
+ char datetime[80];
+ const std::string output_directory_name =
+ "../data/out/"; // "./output_data_csv/Opel/2018-08-09/"; there is no
+ // impact if we change it!
+ std::string output_file_name_str;
-
- time_t rawtime;
- struct tm timeinfo;
-
- //char output_file_name[200];
- char datetime[80];
- const std::string output_directory_name = "../data/out/"; // "./output_data_csv/Opel/2018-08-09/"; there is no impact if we change it!
- std::string output_file_name_str;
-
- time(&rawtime);
- //timeinfo = localtime(&rawtime);
-#ifdef _WIN32
+ time(&rawtime);
+ // timeinfo = localtime(&rawtime);
+ #ifdef _WIN32
localtime_s(&timeinfo, &rawtime);
-#else
- localtime_r(&rawtime, & timeinfo);
-#endif
- strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", &timeinfo);
-
-#ifndef INPUT_FILE_NAME
- output_file_name_str = output_directory_name + "output" + datetime + ".csv";
-#else
- output_file_name_str = output_directory_name + INPUT_FILE_NAME + datetime + ".csv";
-#endif //INPUT_FILE_NAME
-
- cout << output_file_name_str << endl;
-
- //XXX - only for now:
-
- //Ali printf("\n csv_Writer is not NULL, but it is not initialized!! \n");
- /*
- if (csv_writer == NULL) {
- csv_writer = new CSV_Writer("CSV Writer", (char*)output_file_name_str.c_str());
- }
- */
-
-
-
- //DATE18
- confidenceStableInput = 0;
- confidenceStableOutput = 0;
- confidenceStable = 0;
- confStableAdjustableThreshold = 0.5;
-
- confidenceUnstableInput = 0;
- confidenceUnstableOutput = 0;
- confidenceUnstable = 0;
- confidenceUnstableAdjustableThreshold = 0.5;
-
-
- confidenceSameStateInput = 0;
- confSameStateInputAdjustableThreshold = 0.5;
- confidenceSameStateOutput = 0;
- confSameStateOutputAdjustableThreshold = 0.5;
-
- confidenceValidState = 0;
- confValidStateAdjustableThreshold = 0.5;
-
- brokenCounter = 0;
- confidenceBroken = 0;
- confidenceBrokenAdjustableThreshold = 0.5;
-
- driftCounter = 0;
- confidenceDrift = 0;
- confidenceDriftAdjustableThreshold = 0.5;
-
-
+ #else
+ localtime_r(&rawtime, &timeinfo);
+ #endif
+ strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", &timeinfo);
+
+ #ifndef INPUT_FILE_NAME
+ output_file_name_str = output_directory_name + "output" + datetime + ".csv";
+ #else
+ output_file_name_str =
+ output_directory_name + INPUT_FILE_NAME + datetime + ".csv";
+ #endif // INPUT_FILE_NAME
+
+ #ifdef PRINT
+ cout << output_file_name_str << endl;
+ #endif // PRINT
+ */
}
StateHandler::StateHandler() {
- set_name(NO_NAME);
- initStateHandler();
+ set_name(NO_NAME);
+ initStateHandler();
}
StateHandler::StateHandler(std::string name) {
- set_name(name);
- initStateHandler();
+ set_name(name);
+ initStateHandler();
}
StateHandler::StateHandler(std::string name, std::string csvWriterPath) {
set_name(name);
initStateHandler();
csv_writer = new CSV_Writer("CSV Writer", csvWriterPath);
}
-bool StateHandler::setDiscreteAveragePartitionSize(unsigned int discreteAveragePartitionSize) {
- if (discreteAveragePartitionSize > 0) {
- this->discreteAveragePartitionSize = discreteAveragePartitionSize;
- return true;
- }
- return false;
+bool StateHandler::setDiscreteAveragePartitionSize(
+ unsigned int discreteAveragePartitionSize) {
+ if (discreteAveragePartitionSize > 0) {
+ this->discreteAveragePartitionSize = discreteAveragePartitionSize;
+ return true;
+ }
+ return false;
}
unsigned int StateHandler::getDiscreteAveragePartitionSize() {
- return discreteAveragePartitionSize;
-}
-
-bool StateHandler::addVariable(vector<SlaveAgentSlotOfAgent*>* vVariables, SlaveAgentSlotOfAgent* slot) {
- if (vVariables != NULL && slot != NULL) {
- if (find((*vVariables).begin(), (*vVariables).end(), slot) == (*vVariables).end()) {
- try {
- (*vVariables).push_back(slot);
- return true;
- }
- catch (bad_alloc& error) {
- printError("bad_alloc caught: ", error.what());
- }
- }
- }
- return false;
-}
-
-bool StateHandler::addInputVariable(SlaveAgentSlotOfAgent* slot) {
- return addVariable(&vInputVariables, slot);
+ return discreteAveragePartitionSize;
}
-bool StateHandler::addOutputVariable(SlaveAgentSlotOfAgent* slot) {
- return addVariable(&vOutputVariables, slot);
+bool StateHandler::addVariable(vector<SlaveAgentSlotOfAgent *> *vVariables,
+ SlaveAgentSlotOfAgent *slot) {
+ if (vVariables != NULL && slot != NULL) {
+ if (find((*vVariables).begin(), (*vVariables).end(), slot) ==
+ (*vVariables).end()) {
+ try {
+ (*vVariables).push_back(slot);
+ return true;
+ } catch (bad_alloc &error) {
+ printError("bad_alloc caught: ", error.what());
+ }
+ }
+ }
+ return false;
}
-bool StateHandler::delete_all_OuputVariables()
-{
- SlaveAgentSlotOfAgent* cur_sl_ag_sl_ag;
- unsigned int index_v_OutVar;
- unsigned int size_v_OutVar = vOutputVariables.size();
- for(index_v_OutVar = 0; index_v_OutVar < size_v_OutVar; index_v_OutVar++) {
- cur_sl_ag_sl_ag = vOutputVariables[index_v_OutVar];
- delete cur_sl_ag_sl_ag;
- }
- vOutputVariables.clear();
- return true; //added by Ali, it is an error in VS.
+bool StateHandler::addInputVariable(SlaveAgentSlotOfAgent *slot) {
+ return addVariable(&vInputVariables, slot);
}
-bool StateHandler::delete_all_InputVariables()
-{
- SlaveAgentSlotOfAgent* cur_sl_ag_sl_ag;
- unsigned int index_v_InpVar;
- unsigned int size_v_InpVar = vInputVariables.size();
- for(index_v_InpVar = 0; index_v_InpVar < size_v_InpVar; index_v_InpVar++) {
- cur_sl_ag_sl_ag = vInputVariables[index_v_InpVar];
- delete cur_sl_ag_sl_ag;
- }
- vInputVariables.clear();
- return true; //added by Ali, it is an error in VS.
+bool StateHandler::addOutputVariable(SlaveAgentSlotOfAgent *slot) {
+ return addVariable(&vOutputVariables, slot);
}
-bool StateHandler::delete_allStates()
-{
- State* cur_state;
- unsigned int index_v_State;
- unsigned int size_v_State = vStates.size();
- for(index_v_State = 0; index_v_State < size_v_State; index_v_State++) {
- cur_state = vStates[index_v_State];
- delete cur_state;
- }
- vStates.clear();
- return true; //added by Ali, it is an error in VS.
+bool StateHandler::delete_all_OuputVariables() {
+ SlaveAgentSlotOfAgent *cur_sl_ag_sl_ag;
+ unsigned int index_v_OutVar;
+ unsigned int size_v_OutVar = vOutputVariables.size();
+ for (index_v_OutVar = 0; index_v_OutVar < size_v_OutVar; index_v_OutVar++) {
+ cur_sl_ag_sl_ag = vOutputVariables[index_v_OutVar];
+ delete cur_sl_ag_sl_ag;
+ }
+ vOutputVariables.clear();
+ return true; // added by Ali, it is an error in VS.
}
-bool StateHandler::setSlidingWindowBufferSize(unsigned int slidingWindowBufferSize) {
- if (slidingWindowBufferSize >= minNumOfRelatedValuesToBeStable) {
- this->slidingWindowBufferSize = slidingWindowBufferSize;
- return true;
- }
- return false;
+bool StateHandler::delete_all_InputVariables() {
+ SlaveAgentSlotOfAgent *cur_sl_ag_sl_ag;
+ unsigned int index_v_InpVar;
+ unsigned int size_v_InpVar = vInputVariables.size();
+ for (index_v_InpVar = 0; index_v_InpVar < size_v_InpVar; index_v_InpVar++) {
+ cur_sl_ag_sl_ag = vInputVariables[index_v_InpVar];
+ delete cur_sl_ag_sl_ag;
+ }
+ vInputVariables.clear();
+ return true; // added by Ali, it is an error in VS.
}
-bool StateHandler::setMinNumOfRelatedValuesToBeStable(unsigned int minNumOfRelatedValuesToBeStable) {
- if (minNumOfRelatedValuesToBeStable <= slidingWindowBufferSize) {
- this->minNumOfRelatedValuesToBeStable = minNumOfRelatedValuesToBeStable;
- return true;
- }
- return false;
+bool StateHandler::delete_allStates() {
+ State *cur_state;
+ unsigned int index_v_State;
+ unsigned int size_v_State = vStates.size();
+ for (index_v_State = 0; index_v_State < size_v_State; index_v_State++) {
+ cur_state = vStates[index_v_State];
+ delete cur_state;
+ }
+ vStates.clear();
+ return true; // added by Ali, it is an error in VS.
}
-bool StateHandler::setThresholdToBeStable(float thresholdToBeStable) {
- if (thresholdToBeStable >= 0 && thresholdToBeStable <= 1) {
- this->thresholdToBeStable = thresholdToBeStable;
- return true;
- }
- return false;
-}
+// Sorting with bigger Value in Front
+struct descending {
+ template <class T> bool operator()(T const &a, T const &b) const {
+ return a > b;
+ }
+};
-bool StateHandler::setThresholdToBeRelated(float thresholdToBeRelated) {
- if (thresholdToBeRelated >= 0 && thresholdToBeRelated <= 1) {
- this->thresholdToBeRelated = thresholdToBeRelated;
- return true;
- }
- return false;
+State *StateHandler::makeNewState() {
+ State *state = new (nothrow) State();
+ if (state != NULL) {
+ bool flagLoadVariablesWorked = true;
+ for (auto &slot : vInputVariables) {
+ if (!state->addInputSubState(slot))
+ flagLoadVariablesWorked = false;
+ }
+ for (auto &slot : vOutputVariables) {
+ if (!state->addOutputSubState(slot))
+ flagLoadVariablesWorked = false;
+ }
+ if (!flagLoadVariablesWorked) {
+ delete state;
+ return NULL;
+ }
+ } else {
+ return NULL;
+ }
+
+ return state;
}
+bool StateHandler::addActiveStateToStateVector() {
+#ifdef PRINT
+ printf(" >> Save Active State\n");
+#endif // PRINT
+ if (activeState != NULL) {
+ for (auto &state : vStates) {
+ if (state == activeState)
+ return true;
+ }
+#ifdef STOP_WHEN_STATE_VALID
+ getchar();
+#endif // STOP_WHEN_STATE_VALID
-
-bool StateHandler::variablesAreStable(vector<SlaveAgentSlotOfAgent*>* vVariables) {
- bool flagAllVariablesAreStable = true;
- for (auto &slot : *vVariables) {
- if (slot->getHistoryLength() >= slidingWindowBufferSize - 1) { //-1 because actual value is not in the history
- if (slot->getNumberOfRelativesToActualValue(thresholdToBeStable) < minNumOfRelatedValuesToBeStable) { //-1 because actual value is also on of minNumOfRelatedValuesToBeStable
- flagAllVariablesAreStable = false;
- }
- }
- else {
- return false;
- }
- }
-
- return flagAllVariablesAreStable;
+ try {
+ vStates.push_back(activeState);
+ return true;
+ } catch (bad_alloc &error) {
+ printError("bad_alloc caught: ", error.what());
+ delete activeState;
+ }
+ }
+ return false;
}
-//Sorting with bigger Value in Front
-struct descending
-{
- template<class T>
- bool operator()(T const &a, T const &b) const { return a > b; }
-};
-
-//DATE18
-float StateHandler::getConfVariableIsStable(SlaveAgentSlotOfAgent* variable) {
- float bestConfOf1Var = 0;
- float sample;
- if (variable->get_slaveAgentValue(&sample)) {
-
- list<float> lHistoryTemporary = variable->getHistory();
- vector<float> vDeviations;
-
- for (auto &h : lHistoryTemporary)
- vDeviations.push_back(deviationValueReferenceValue(sample, h));
-
- std::sort(std::begin(vDeviations), std::end(vDeviations));
-
- //all adaptabilities within the history of one variable
- for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) {
-
- float worstConfOfHistSampleSet = 1;
- unsigned int histSampleCounter = 0;
-
- for (auto &deviation : vDeviations) {
- if (histSampleCounter >= numOfHistSamplesIncluded)
- break;
-
- worstConfOfHistSampleSet = minValueOf2Values(worstConfOfHistSampleSet, StabDeviation->getY(deviation));
-
- histSampleCounter++;
- }
-
- bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(worstConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter)));
- }
- }
-
- return bestConfOf1Var;
+bool StateHandler::makeNewActiveState() {
+ State *state = makeNewState();
+ if (state != NULL) {
+ activeState = state;
+ return true;
+ }
+ return false;
}
-//DATE18
-float StateHandler::getConfVariablesAreStable(vector<SlaveAgentSlotOfAgent*>* vVariables) {
- float worstConfOfAllVariables = 1;
-
- for (auto &slot : *vVariables)
- worstConfOfAllVariables = minValueOf2Values(worstConfOfAllVariables, getConfVariableIsStable(slot));
-
- return worstConfOfAllVariables;
+void StateHandler ::reset_States() {
+ this->delete_allStates();
+ this->activeState = NULL;
}
-//DATE18
-float StateHandler::getConfVariableIsUnstable(SlaveAgentSlotOfAgent* variable) {
- float bestConfOf1Var = 0;
- float sample;
- if (variable->get_slaveAgentValue(&sample)) {
-
- list<float> lHistoryTemporary = variable->getHistory();
- vector<float> vDeviations;
-
- for (auto &h : lHistoryTemporary)
- vDeviations.push_back(deviationValueReferenceValue(sample, h));
-
- sort(begin(vDeviations), end(vDeviations), descending());
-
- //all adaptabilities within the history of one variable
- for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) {
-
- //float bestConfOfHistSampleSet = 1;
- float bestConfOfHistSampleSet = 0;
- unsigned int histSampleCounter = 0;
-
- for (auto &deviation : vDeviations) {
- if (histSampleCounter >= numOfHistSamplesIncluded)
- break;
-
- //bestConfOfHistSampleSet = minValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation));
- bestConfOfHistSampleSet = maxValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation));
-
- histSampleCounter++;
- }
-
- bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(bestConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter)));
- }
- }
-
- return bestConfOf1Var;
+void StateHandler ::reset_States_and_Slave_Agents() {
+ reset_States();
+ this->delete_all_InputVariables();
+ this->delete_all_OuputVariables();
}
-//DATE18 - Is there one unstable variable?
-float StateHandler::getConfVariablesAreUnstable(vector<SlaveAgentSlotOfAgent*>* vVariables) {
- float bestConfOfAllVariables = 0;
-
- for (auto &slot : *vVariables)
- bestConfOfAllVariables = maxValueOf2Values(bestConfOfAllVariables, getConfVariableIsUnstable(slot));
-
- return bestConfOfAllVariables;
+void StateHandler::setMaxStateHistoryLength(
+ unsigned int maxStateHistoryLength) {
+ this->maxStateHistoryLength = maxStateHistoryLength;
}
-
-
-/*
-bool StateHandler::getConfAndUnconfVariableIsMatching(State* state, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf) {
- float bestUnconfOf1Var = 0;
- float worstConfOf1Var = 1;
-
- if (state != NULL) {
-
-
- }
-
-
- float sample;
- if (variable->get_slaveAgentValue(&sample)) {
-
- list<float> lHistoryTemporary = variable->getHistory();
- vector<float> vDeviations;
-
- for (auto &h : lHistoryTemporary)
- vDeviations.push_back(deviationValueReferenceValue(sample, h));
-
- sort(begin(vDeviations), end(vDeviations), descending());
-
- //all adaptabilities within the history of one variable
- for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) {
-
- //float bestConfOfHistSampleSet = 1;
- float bestConfOfHistSampleSet = 0;
- unsigned int histSampleCounter = 0;
-
- for (auto &deviation : vDeviations) {
- if (histSampleCounter >= numOfHistSamplesIncluded)
- break;
-
- //bestConfOfHistSampleSet = minValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation));
- bestConfOfHistSampleSet = maxValueOf2Values(bestConfOfHistSampleSet, UnstabDeviation->getY(deviation));
-
- histSampleCounter++;
- }
-
- bestConfOf1Var = maxValueOf2Values(bestConfOf1Var, minValueOf2Values(bestConfOfHistSampleSet, StabSamples->getY((float)histSampleCounter)));
- }
- }
-
- return bestConfOf1Var;
-
-
- return 0;
+void StateHandler::setSimilarToStateDeviationFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfSim2StateDev) {
+ this->FuncBlockConfSim2StateDev = FuncBlockConfSim2StateDev;
+}
+LinearFunctionBlock *StateHandler::getSimilarToStateDeviationFuzzyFunction() {
+ return this->FuncBlockConfSim2StateDev;
}
-*/
-/*
-bool StateHandler::getConfAndUnconfVariablesAreMatching(vector<SlaveAgentSlotOfAgent*>* vVariables, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf) {
-
- float bestUnconfOfAllVariables = 0;
- float worstConfOfAllVariables = 1;
-
- for (auto &variable :* vVariables) {
- bestUnconfOfAllVariables = maxValueOf2Values(bestUnconfOfAllVariables, getConfAndUnconfVariableIsMatching(variable, confDeviation, confTime, conf, unconf));
- worstConfOfAllVariables = minValueOf2Values(worstConfOfAllVariables, getConfAndUnconfVariableIsMatching(variable, confDeviation, confTime, conf, unconf));
- }
-
- *conf = worstConfOfAllVariables;
- *unconf = bestUnconfOfAllVariables;
- return true;
+void StateHandler::setSimilarToStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfSim2StateTime) {
+ this->FuncBlockConfSim2StateTime = FuncBlockConfSim2StateTime;
}
-*/
-
-
-
-
-
-State* StateHandler::makeNewState() {
- State* state = new (nothrow) State();
- if (state != NULL) {
- bool flagLoadVariablesWorked = true;
- for (auto &slot : vInputVariables) {
- if (!state->addInputSubState(slot))
- flagLoadVariablesWorked = false;
- }
- for (auto &slot : vOutputVariables) {
- if (!state->addOutputSubState(slot))
- flagLoadVariablesWorked = false;
- }
- if (!flagLoadVariablesWorked) {
- delete state;
- return NULL;
- }
- }
- else {
- return NULL;
- }
-
- return state;
+LinearFunctionBlock *StateHandler::getSimilarToStateTimeFuzzyFunction() {
+ return this->FuncBlockConfSim2StateTime;
}
-bool StateHandler::addActiveStateToStateVector() {
-#ifdef PRINT
- printf(" >> Save Active State\n");
-#endif //PRINT
- if (activeState != NULL) {
- for (auto &state : vStates) {
- if (state == activeState)
- return true;
- }
-
-#ifdef STOP_WHEN_STATE_VALID
- getchar();
-#endif // STOP_WHEN_STATE_VALID
-
- try {
- vStates.push_back(activeState);
- return true;
- }
- catch (bad_alloc& error) {
- printError("bad_alloc caught: ", error.what());
- delete activeState;
- }
- }
- return false;
+void StateHandler::setDifferentToStateFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfDif2StateDev) {
+ this->FuncBlockConfDif2StateDev = FuncBlockConfDif2StateDev;
}
-
-/*
-bool StateHandler::addStateAndMakeItActive() {
- State* state = addState();
- if (state != NULL) {
- activeState = state;
- return true;
- }
- return false;
+LinearFunctionBlock *StateHandler::getDifferentToStateDeviationFuzzyFunction() {
+ return this->FuncBlockConfDif2StateDev;
}
-*/
-bool StateHandler::makeNewActiveState() {
- State* state = makeNewState();
- if (state != NULL) {
- activeState = state;
- return true;
- }
- return false;
+void StateHandler::setDifferentToStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfDif2StateTime) {
+ this->FuncBlockConfDif2StateTime = FuncBlockConfDif2StateTime;
}
-
-
-State* StateHandler::findRelatedState() {
- for (auto &state : vStates) {
- if (state->inputVariablesAreRelated(thresholdToBeRelated) && state->outputVariablesAreRelated(thresholdToBeRelated)) {
- return state;
- }
- }
- return NULL;
+LinearFunctionBlock *StateHandler::getDifferentToStateTimeFuzzyFunction() {
+ return this->FuncBlockConfDif2StateTime;
}
-bool StateHandler::findRelatedStateAndMakeItActive() {
- State* state = findRelatedState();
- if (state != NULL) {
- activeState = state;
- return true;
- }
- return false;
+void StateHandler::setSimilarityInStateDeviationFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfValStateDev) {
+ this->FuncBlockConfValStateDev = FuncBlockConfValStateDev;
+}
+LinearFunctionBlock *
+StateHandler::getSimilarityInStateDeviationFuzzyFunction() {
+ return this->FuncBlockConfValStateDev;
}
-void StateHandler::eraseStatesWithLessInjections() {
- if (activeState != NULL) {
- if (activeState->getNumOfInjections() < minNumToBeValidState) {
- activeState = NULL;
- }
- }
-
- for (vector<State*>::iterator state = vStates.begin(); state < vStates.end(); state++) {
- if ((*state)->getNumOfInjections() < minNumToBeValidState) {
- //TODO: also delete all subStates (etc.) of the State? Because: Memory Leakage.
- vStates.erase(state);
- state--;
- }
- }
-
-
-
-
-
-
-
-
- /*
- for (auto &state : vStates) {
- //TODO: also delete all subStates (etc.) of the State? Because: Memory Leakage.
- if (state->getNumOfInjections() < minNumToBeValidState) {
- vStates.erase(state);
- }
- }
- */
-
+void StateHandler::setSimilarityInStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfValStateTime) {
+ this->FuncBlockConfValStateTime = FuncBlockConfValStateTime;
+}
+LinearFunctionBlock *StateHandler::getSimilarityInStateTimeFuzzyFunction() {
+ return this->FuncBlockConfValStateTime;
}
-void StateHandler :: reset_States()
-{
- this->delete_allStates();
- this->activeState = NULL;
+void StateHandler::setUnsimilarityInStateDeviationFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfInvStateDev) {
+ this->FuncBlockConfInvStateDev = FuncBlockConfInvStateDev;
+}
+LinearFunctionBlock *
+StateHandler::getUnsimilarityInStateDeviationFuzzyFunction() {
+ return this->FuncBlockConfInvStateDev;
}
-void StateHandler :: reset_States_and_Slave_Agents()
-{
- reset_States();
- this->delete_all_InputVariables();
- this->delete_all_OuputVariables();
+void StateHandler::setUnsimilarityInStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfInvStateTime) {
+ this->FuncBlockConfInvStateTime = FuncBlockConfInvStateTime;
+}
+LinearFunctionBlock *StateHandler::getUnsimilarityInStateTimeFuzzyFunction() {
+ return this->FuncBlockConfInvStateTime;
}
+void StateHandler::setDriftFuzzyFunction(
+ LinearFunctionBlock *FuncBlockDriftDeviation) {
+ this->FuncBlockDriftDeviation = FuncBlockDriftDeviation;
+}
+LinearFunctionBlock *StateHandler::getDriftFuzzyFunction() {
+ return this->FuncBlockDriftDeviation;
+}
-void StateHandler ::setMaxStateHistoryLength(
- unsigned int maxStateHistoryLength) {
- this->maxStateHistoryLength = maxStateHistoryLength;
+void StateHandler::setBrokenFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfBrokenSamples) {
+ this->FuncBlockConfBrokenSamples = FuncBlockConfBrokenSamples;
+}
+LinearFunctionBlock *StateHandler::getBrokenFuzzyFunction() {
+ return this->FuncBlockConfBrokenSamples;
}
+void StateHandler::setSimilarToStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfSim2StateDev,
+ LinearFunctionBlock *FuncBlockConfSim2StateTime) {
+ this->FuncBlockConfSim2StateDev = FuncBlockConfSim2StateDev;
+ this->FuncBlockConfSim2StateTime = FuncBlockConfSim2StateTime;
+}
+void StateHandler::setDifferentToStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfDif2StateDev,
+ LinearFunctionBlock *FuncBlockConfDif2StateTime) {
+ this->FuncBlockConfDif2StateDev = FuncBlockConfDif2StateDev;
+ this->FuncBlockConfDif2StateTime = FuncBlockConfDif2StateTime;
+}
+void StateHandler::setSimilarityInStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfValStateDev,
+ LinearFunctionBlock *FuncBlockConfValStateTime) {
+ this->FuncBlockConfValStateDev = FuncBlockConfValStateDev;
+ this->FuncBlockConfValStateTime = FuncBlockConfValStateTime;
+}
+void StateHandler::setUnsimilarityInStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfInvStateDev,
+ LinearFunctionBlock *FuncBlockConfInvStateTime) {
+ this->FuncBlockConfInvStateDev = FuncBlockConfInvStateDev;
+ this->FuncBlockConfInvStateTime = FuncBlockConfInvStateTime;
+}
+void StateHandler::setDriftAndBrokenFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockDriftDeviation,
+ LinearFunctionBlock *FuncBlockConfBrokenSamples) {
+ this->FuncBlockDriftDeviation = FuncBlockDriftDeviation;
+ this->FuncBlockConfBrokenSamples = FuncBlockConfBrokenSamples;
+}
+StateHandler ::~StateHandler() {
-StateHandler :: ~StateHandler()
-{
- delete_all_OuputVariables();
- delete_all_InputVariables();
- delete_allStates();
+ delete_all_OuputVariables();
+ delete_all_InputVariables();
+ delete_allStates();
- //delete csv_writer;
+ // delete csv_writer;
}
+void StateHandler::printOK() {
+ setColor(TXTCOLOR_LIGHTGREEN);
+ printf("OK\n");
+ setColor(TXTCOLOR_GREY);
+}
-//XXX - only for now
-bool test = true;
-unsigned int brokenCounter = 0, driftCounter = 0;
-
-void printDrift() {
- driftCounter++;
- setColor(TXTCOLOR_YELLOW);
- printf(" >> DRIFT\n");
- setColor(TXTCOLOR_GREY);
- test = true;
+void StateHandler::printDrift() {
+ setColor(TXTCOLOR_YELLOW);
+ printf(" >> DRIFT\n");
+ setColor(TXTCOLOR_GREY);
}
-void printBroken() {
- brokenCounter++;
- setColor(TXTCOLOR_LIGHTRED);
- printf(" >> BROKEN\n");
- setColor(TXTCOLOR_GREY);
- test = true;
+void StateHandler::printBroken() {
+ setColor(TXTCOLOR_LIGHTRED);
+ printf(" >> BROKEN\n");
+ setColor(TXTCOLOR_GREY);
}
-//XXX - only for now
+// XXX - only for now
unsigned int old_cycle = 1;
int brokentest = 0;
+bool flagPrintHeader = false;
+
/*
* makes a new state and reports if there is a anomaly = hearth piece of CAM :-)
*/
void StateHandler::trigger(unsigned int cycle) {
#ifdef PRINT
- printf("cycle: %u\n", cycle);
+ printf("cycle: %u\n", cycle);
#endif // PRINT
-
- bool flagGotValues = true;
+ bool flagGotValues = true;
#ifdef PRINT
- printf("Input Sample Values:\n");
+ printf("Input Sample Values:\n");
#endif // PRINT
- for (auto &slot : vInputVariables) {
- float sampleValue;
- if (!(slot->get_slaveAgentValue(&sampleValue)))
- flagGotValues = false; //program never executes this line of code
+ for (auto &slot : vInputVariables) {
+ float sampleValue;
+ if (!(slot->get_slaveAgentValue(&sampleValue)))
+ flagGotValues = false;
#ifdef PRINT
- printf("In, %s: %f\n", slot->get_comPort()->get_name().c_str(), sampleValue);
+ printf("In, %s: %f\n", slot->get_comPort()->get_name().c_str(),
+ sampleValue);
#endif // PRINT
-
- if (cycle == 1)
- csv_writer->write_field(slot->get_comPort()->get_name().c_str());
- else
- csv_writer->write_field(sampleValue);
- csv_writer->make_new_field();
- }
+ }
#ifdef PRINT
- printf("Output Sample Values:\n");
+ printf("Output Sample Values:\n");
#endif // PRINT
- for (auto &slot : vOutputVariables) {
- float sampleValue;
- if (!(slot->get_slaveAgentValue(&sampleValue)))
- flagGotValues = false; //program never executes this line of code
+ for (auto &slot : vOutputVariables) {
+ float sampleValue;
+ if (!(slot->get_slaveAgentValue(&sampleValue)))
+ flagGotValues = false;
#ifdef PRINT
- printf("Out, %s: %f\n", slot->get_comPort()->get_name().c_str(), sampleValue);
+ printf("Out, %s: %f\n", slot->get_comPort()->get_name().c_str(),
+ sampleValue);
#endif // PRINT
-
- if (cycle == 1)
- csv_writer->write_field(slot->get_comPort()->get_name().c_str());
- else
- csv_writer->write_field(sampleValue);
- csv_writer->make_new_field();
- }
-
- if (cycle == 1){
- csv_writer->write_field("State Nr");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf State Valid");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf State Invalid");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf Input unchanged");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf Input changed");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf Output unchanged");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf Output changed");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Status");
- csv_writer->make_new_field();
-
- csv_writer->write_field("Conf Status");
- csv_writer->make_new_field();
- }
- else {
- //in the beginning, a active state has to be created
- if (activeState == NULL && vStates.empty()) {
- brokenCounter = 0;
-#ifdef PRINT
- printf(" > new active state\n");
-#endif // PRINT
- makeNewActiveState();
- if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize))
- addActiveStateToStateVector();
- //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime
- float newBoundary = (float)activeState->getLengthOfHistory();
- FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
- FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
+ }
- csv_writer->write_field((int)vStates.size() + 1);
- csv_writer->make_new_field();
+ if (flagPrintHeader == false) {
+ csv_writer->write_field("Nr of States");
+ csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateValid());
- csv_writer->make_new_field();
+ csv_writer->write_field("State Nr");
+ csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateInvalid());
- csv_writer->make_new_field();
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
- csv_writer->write_field(0); //confInputVarAreSim2ActiveState
- csv_writer->make_new_field();
+ csv_writer->write_field("Conf State Valid");
+ csv_writer->make_new_field();
- csv_writer->write_field(0); //confInputVarAreDif2ActiveState
- csv_writer->make_new_field();
+ csv_writer->write_field("Conf State Invalid");
+ csv_writer->make_new_field();
- csv_writer->write_field(0); //confOutputVarAreSim2ActiveState
- csv_writer->make_new_field();
+ csv_writer->write_field("Conf Input unchanged");
+ csv_writer->make_new_field();
- csv_writer->write_field(0); //confOutputVarAreDif2ActiveState
- csv_writer->make_new_field();
+ csv_writer->write_field("Conf Input changed");
+ csv_writer->make_new_field();
+ csv_writer->write_field("Conf Output unchanged");
+ csv_writer->make_new_field();
- csv_writer->write_field(STATUS_OKAY);
- csv_writer->make_new_field();
+ csv_writer->write_field("Conf Output changed");
+ csv_writer->make_new_field();
+#endif // WRITE_MORE_INFORMATION_IN_CSV
- csv_writer->write_field(0); //Status Conf
- csv_writer->make_new_field();
+ csv_writer->write_field("Status");
+ csv_writer->make_new_field();
+ csv_writer->write_field("Conf Status");
+ // csv_writer->make_new_field();
- }
- //there is an active state and/or other states
- else {
- float confInputVarAreSim2ActiveState = activeState->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- float confInputVarAreDif2ActiveState = activeState->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
- float confOutputVarAreSim2ActiveState = activeState->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- float confOutputVarAreDif2ActiveState = activeState->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
+ if (flagGotValues == true)
+ csv_writer->make_new_line();
- //float confInputIsSteady = confInputVarAreSim2ActiveState - confInputVarAreDif2ActiveState;
- //float confOutputIsSteady = confOutputVarAreSim2ActiveState - confOutputVarAreDif2ActiveState;
+ flagPrintHeader = true;
+ }
- printf("input (sim/dif) %f/%f\noutput (sim/dif) %f/%f\n", confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState, confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState);
+ if (!flagGotValues) {
+ } else {
- //same state
- if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) {
- brokenCounter = 0;
-#ifdef PRINT
- printf(" > same state\n");
+ // printf("neu\n");
- //printf("\nPROOF:\nconfInputVarAreSim2ActiveState = %f\nconfInputVarAreDif2ActiveState = %f\nconfOutputVarAreSim2ActiveState = %f\nconfOutputVarAreDif2ActiveState = %f\n", confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState, confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState);
+ // in the beginning, a active state has to be created
+ if (activeState == NULL && vStates.empty()) {
+ brokenCounter = 0;
+#ifdef PRINT
+ printf(" > new active state\n");
#endif // PRINT
- if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize))
- addActiveStateToStateVector();
- //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime
- float newBoundary = (float)activeState->getLengthOfHistory();
- FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
- FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
-
- //print state number
- if(activeState->isStateValid())
- csv_writer->write_field((int)vStates.size());
- else
- csv_writer->write_field((int)vStates.size()+1);
- csv_writer->make_new_field();
-
- //print conf valid
- csv_writer->write_field(activeState->getConfStateValid());
- csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateInvalid());
- csv_writer->make_new_field();
-
- //print conf statechange
- csv_writer->write_field(confInputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confInputVarAreDif2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreDif2ActiveState);
- csv_writer->make_new_field();
-
-
- confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation);
- float confidenceNoDrift = 1 - confidenceDrift;
-
- /*
- //print conf drift
- csv_writer->write_field(confidenceNoDrift);
- csv_writer->make_new_field();
- csv_writer->write_field(confidenceDrift);
- csv_writer->make_new_field();
- */
-
- if (confidenceDrift > 0.5) {
+ makeNewActiveState();
+ if (activeState->insertValueInState(
+ FuncBlockConfValStateDev, FuncBlockConfInvStateDev,
+ FuncBlockConfValStateTime, FuncBlockConfInvStateTime,
+ maxStateHistoryLength, discreteAveragePartitionSize))
+ addActiveStateToStateVector();
+ // NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime
+ float newBoundary = (float)activeState->getLengthOfHistory();
+ FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
+ FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
+
+ csv_writer->write_field((int)vStates.size());
+ csv_writer->make_new_field();
+
+ ptrdiff_t stateNr = find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ csv_writer->write_field((int)stateNr);
+ csv_writer->make_new_field();
+
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
+ csv_writer->write_field(activeState->getConfStateValid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(activeState->getConfStateInvalid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(0); // confInputVarAreSim2ActiveState
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(0); // confInputVarAreDif2ActiveState
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(0); // confOutputVarAreSim2ActiveState
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(0); // confOutputVarAreDif2ActiveState
+ csv_writer->make_new_field();
+#endif // WRITE_MORE_INFORMATION_IN_CSV
+
+ csv_writer->write_field(STATUS_OKAY); // OK
+ csv_writer->make_new_field();
+
+ // NOTE: Status Conf = 0 because conf valid = 0
+ csv_writer->write_field(0); // Status Conf
+ // csv_writer->make_new_field();
+
+ }
+ // there is an active state and/or other states
+ // XXXX - There is no if regarding an active state?!?!?
+ else {
+ float confInputVarAreSim2ActiveState =
+ activeState->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev,
+ FuncBlockConfSim2StateTime);
+ float confInputVarAreDif2ActiveState =
+ activeState->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev,
+ FuncBlockConfDif2StateTime);
+ float confOutputVarAreSim2ActiveState =
+ activeState->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev,
+ FuncBlockConfSim2StateTime);
+ float confOutputVarAreDif2ActiveState =
+ activeState->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev,
+ FuncBlockConfDif2StateTime);
+
+
+ // printf("input (sim/dif) %f/%f\noutput (sim/dif) %f/%f\n",
+ // confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState,
+ // confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState);
+
+ // bool flagActiveStateWasValid = false;
+
+ // if no change has detected -> same state
+ if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) &&
+ (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) {
+ brokenCounter = 0;
#ifdef PRINT
- setColor(TXTCOLOR_YELLOW);
- printf("DRIFT\n");
- setColor(TXTCOLOR_GREY);
+ printf(" > same state\n");
+
+ // printf("\nPROOF:\nconfInputVarAreSim2ActiveState =
+ // %f\nconfInputVarAreDif2ActiveState =
+ // %f\nconfOutputVarAreSim2ActiveState =
+ // %f\nconfOutputVarAreDif2ActiveState = %f\n",
+ // confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState,
+ // confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState);
+#endif // PRINT
+ if (activeState->insertValueInState(
+ FuncBlockConfValStateDev, FuncBlockConfInvStateDev,
+ FuncBlockConfValStateTime, FuncBlockConfInvStateTime,
+ maxStateHistoryLength, discreteAveragePartitionSize))
+ addActiveStateToStateVector();
+ // NEW - Adjust FuncBlockConfSim2StateTime and
+ // FuncBlockConfDif2StateTime
+ float newBoundary = (float)activeState->getLengthOfHistory();
+ FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
+ FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
+
+ csv_writer->write_field((int)vStates.size());
+ csv_writer->make_new_field();
+
+ ptrdiff_t stateNr = find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ csv_writer->write_field((int)stateNr);
+ csv_writer->make_new_field();
+
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
+ // write conf valid
+ csv_writer->write_field(activeState->getConfStateValid());
+ csv_writer->make_new_field();
+ csv_writer->write_field(activeState->getConfStateInvalid());
+ csv_writer->make_new_field();
+
+ // write conf statechange
+ csv_writer->write_field(confInputVarAreSim2ActiveState);
+ csv_writer->make_new_field();
+ csv_writer->write_field(confInputVarAreDif2ActiveState);
+ csv_writer->make_new_field();
+ csv_writer->write_field(confOutputVarAreSim2ActiveState);
+ csv_writer->make_new_field();
+ csv_writer->write_field(confOutputVarAreDif2ActiveState);
+ csv_writer->make_new_field();
+#endif // WRITE_MORE_INFORMATION_IN_CSV
+
+ confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(
+ discreteAveragePartitionSize, FuncBlockDriftDeviation);
+ float confidenceNoDrift = 1 - confidenceDrift;
+
+ //AAAAAAAAA (New: "&& activeState->isStateValidAfterReentrance()")
+ if (confidenceDrift > 0.5 && activeState->isStateValidAfterReentrance()) {
+#ifdef PRINT
+ printDrift();
#endif // PRINT
#ifdef STOP_WHEN_DRIFT
- getchar();
+ getchar();
#endif // STOP_WHEN_DRIFT
- setColor(TXTCOLOR_GREY);
- //print drift
- csv_writer->write_field(STATUS_DRIFT);
- csv_writer->make_new_field();
+ // print drift
+ csv_writer->write_field(STATUS_DRIFT);
+ csv_writer->make_new_field();
- //calc and print conf
- float conf = fuzzyAND(fuzzyAND(confidenceDrift, activeState->getConfStateValid()), fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState));
- csv_writer->write_field(conf);
+ // calc and print conf
+ // AAAAAAAAAAAAAAAA
+ float conf = fuzzyAND(
+ fuzzyAND(confInputVarAreSim2ActiveState,
+ confOutputVarAreSim2ActiveState),
+ fuzzyAND(confidenceDrift, activeState->getConfStateValid()));
+ csv_writer->write_field(conf);
- }
- else {
+ // printf("drift bei (same state): %i\n", cycle);
+ } else {
#ifdef PRINT
- setColor(TXTCOLOR_LIGHTGREEN);
- printf("OK\n");
- setColor(TXTCOLOR_GREY);
+ printOK();
#endif // PRINT
- //print ok
- csv_writer->write_field(STATUS_OKAY);
- csv_writer->make_new_field();
-
- //calc and print conf
- float conf = fuzzyAND(fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift));
- csv_writer->write_field(conf);
- }
- csv_writer->make_new_field();
-
-
+ // print ok
+ csv_writer->write_field(STATUS_OKAY);
+ csv_writer->make_new_field();
+
+ // calc and print conf
+ //AAAAAAAAAAA
+ //NOTE: next equation is without confOK because confOK = 1 anyway (because broken timer = 0)
+ float conf =
+ fuzzyAND(fuzzyOR(fuzzyAND(confInputVarAreSim2ActiveState,
+ confOutputVarAreSim2ActiveState),
+ fuzzyAND(confInputVarAreDif2ActiveState,
+ confOutputVarAreDif2ActiveState)),
+ fuzzyAND(activeState->getConfStateValid(),
+ confidenceNoDrift));
+
+ csv_writer->write_field(conf);
+ }
+ // csv_writer->make_new_field();
+ } // same state
+ // state change
+ else {
+
+ // search in vector for matching state //TODO in future: look for the
+ // best matching, Not for the first matching
+ bool flagFoundMatchingState = false;
+ bool flagFoundHalfMatchingState = false;
+ bool flagHalfMatchingStateIsActiveState = false;
+ bool flagHalfMatchingStateIsAnotherState = false;
+
+ float confInputVarAreSim2State;
+ float confInputVarAreDif2State;
+ float confOutputVarAreSim2State;
+ float confOutputVarAreDif2State;
+
+ State *activeStateBackup = activeState;
+
+ bool flagActiveStateWasValid = activeState->isStateValid();
+
+ float backupConfInputVarAreSim2ActiveState;
+ float backupConfInputVarAreDif2ActiveState;
+ float backupConfOutputVarAreSim2ActiveState;
+ float backupConfOutputVarAreDif2ActiveState;
+
+ float backupConfInputVarAreSim2State;
+ float backupConfInputVarAreDif2State;
+ float backupConfOutputVarAreSim2State;
+ float backupConfOutputVarAreDif2State;
+
+ float highestConfidenceDifferenceMatch = 0;
+
+ // compare with all states
+ for (auto &state : vStates) {
+
+ confInputVarAreSim2State = state->getConfInputVarAreSim2State(
+ FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
+ confInputVarAreDif2State = state->getConfInputVarAreDif2State(
+ FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
+ confOutputVarAreSim2State = state->getConfOutputVarAreSim2State(
+ FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
+ confOutputVarAreDif2State = state->getConfOutputVarAreDif2State(
+ FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
+
+ // state matches full with another state
+ if ((confInputVarAreSim2State > confInputVarAreDif2State) &&
+ (confOutputVarAreSim2State > confOutputVarAreDif2State)) {
+ // lastActiveState == activeState;
+
+ activeState = state;
+ flagFoundMatchingState = true;
+
+ backupConfInputVarAreSim2State = confInputVarAreSim2State;
+ backupConfInputVarAreDif2State = confInputVarAreDif2State;
+ backupConfOutputVarAreSim2State = confOutputVarAreSim2State;
+ backupConfOutputVarAreDif2State = confOutputVarAreDif2State;
- }
- //state change
- else {
- //was Valid
- if (activeState->isStateValid()) {
-
- //only one sub set changed
- if (((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState <= confOutputVarAreDif2ActiveState)) || ((confInputVarAreSim2ActiveState <= confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState))) {
-
-
- //print state number
- if (activeState->isStateValid())
- csv_writer->write_field((int)vStates.size());
- else
- csv_writer->write_field((int)vStates.size() + 1);
- csv_writer->make_new_field();
+#ifdef PRINT
+ ptrdiff_t stateNr = find(vStates.begin(), vStates.end(), state) -
+ vStates.begin() + 1;
+ setColor(TXTCOLOR_LIGHTGREEN);
+ printf("- Passt voll zu: %i\n", stateNr);
+ setColor(TXTCOLOR_GREY);
+#endif // PRINT
- //print conf valid
- csv_writer->write_field(activeState->getConfStateValid());
- csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateInvalid());
- csv_writer->make_new_field();
+ break;
+ }
+ // state matches half with another state
+ else if ((confInputVarAreSim2State > confInputVarAreDif2State) ||
+ (confOutputVarAreSim2State > confOutputVarAreDif2State)) {
- //print conf statechange
- csv_writer->write_field(confInputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confInputVarAreDif2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreDif2ActiveState);
- csv_writer->make_new_field();
+ flagFoundHalfMatchingState = true;
+ // half matchin state is the active state
+ if (state == activeStateBackup) {
+#ifdef PRINT
+ ptrdiff_t stateNr = find(vStates.begin(), vStates.end(), state) -
+ vStates.begin() + 1;
+ setColor(TXTCOLOR_YELLOW);
+ printf("- Passt halb zu: %i (active State)\n", stateNr);
+ setColor(TXTCOLOR_GREY);
+#endif // PRINT
+ flagHalfMatchingStateIsActiveState = true;
+ backupConfInputVarAreSim2ActiveState = confInputVarAreSim2State;
+ backupConfInputVarAreDif2ActiveState = confInputVarAreDif2State;
+ backupConfOutputVarAreSim2ActiveState = confOutputVarAreSim2State;
+ backupConfOutputVarAreDif2ActiveState = confOutputVarAreDif2State;
+ }
+ // half matchin state is the another state
+ else {
+#ifdef PRINT
+ ptrdiff_t stateNr = find(vStates.begin(), vStates.end(), state) -
+ vStates.begin() + 1;
+ setColor(TXTCOLOR_YELLOW);
+ printf("- Passt halb zu: %i (another State)\n", stateNr);
+ setColor(TXTCOLOR_GREY);
+#endif // PRINT
+ flagHalfMatchingStateIsAnotherState = true;
+
+ float diffConfInputVarAreSim2State =
+ confInputVarAreSim2State - confInputVarAreDif2State;
+ float diffConfOutputVarAreSim2State =
+ confOutputVarAreSim2State - confOutputVarAreDif2State;
+
+ if (diffConfInputVarAreSim2State >
+ highestConfidenceDifferenceMatch) {
+
+ activeState = state;
+ highestConfidenceDifferenceMatch = diffConfInputVarAreSim2State;
+
+ backupConfInputVarAreSim2State = confInputVarAreSim2State;
+ backupConfInputVarAreDif2State = confInputVarAreDif2State;
+ backupConfOutputVarAreSim2State = confOutputVarAreSim2State;
+ backupConfOutputVarAreDif2State = confOutputVarAreDif2State;
+ }
+
+ if (diffConfOutputVarAreSim2State >
+ highestConfidenceDifferenceMatch) {
+ activeState = state;
+ highestConfidenceDifferenceMatch =
+ diffConfOutputVarAreSim2State;
+
+ backupConfInputVarAreSim2State = confInputVarAreSim2State;
+ backupConfInputVarAreDif2State = confInputVarAreDif2State;
+ backupConfOutputVarAreSim2State = confOutputVarAreSim2State;
+ backupConfOutputVarAreDif2State = confOutputVarAreDif2State;
+ }
+ }
+ }
+ }
+
+ // another state matches In- AND Outputs
+ if (flagFoundMatchingState == true) {
+
+ // new revision cam
+ activeState->resetNumOfInsertedSamples();
+
+ if (flagActiveStateWasValid == false) {
+ delete activeStateBackup;
+ // activeState = NULL;
+#ifdef PRINT
+ printf(" > delete active state\n");
+#endif // PRINT
+ }
- brokenCounter++;
- printf("brokenCounter: %u\n", brokenCounter);
-
- confidenceBroken = FuncBlockConfBrokenSamples->getY((float) brokenCounter);
- float confidenceOK = 1 - confidenceBroken;
+ // Found a matching state
+ brokenCounter = 0;
- if (confidenceBroken > 0.5) {
#ifdef PRINT
- setColor(TXTCOLOR_LIGHTRED);
- printf("BROKEN\n");
- setColor(TXTCOLOR_GREY);
+ printf(" > change to existing state\n");
+#endif // PRINT
+#ifdef STOP_WHEN_CHANGING_IN_OLD_STATE
+ getchar();
+#endif // STOP_WHEN_CHANGING_IN_OLD_STATE
+
+ // update state
+ // insert in activeState
+ if (activeState->insertValueInState(
+ FuncBlockConfValStateDev, FuncBlockConfInvStateDev,
+ FuncBlockConfValStateTime, FuncBlockConfInvStateTime,
+ maxStateHistoryLength, discreteAveragePartitionSize))
+ addActiveStateToStateVector();
+ // NEW - Adjust FuncBlockConfSim2StateTime and
+ // FuncBlockConfDif2StateTime
+ float newBoundary = (float)activeState->getLengthOfHistory();
+ FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
+ FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
+
+ // printf("HIER3\n");
+
+ csv_writer->write_field((int)vStates.size());
+ csv_writer->make_new_field();
+ ptrdiff_t stateNr =
+ find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ csv_writer->write_field((int)stateNr);
+ csv_writer->make_new_field();
+
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
+ csv_writer->write_field(activeState->getConfStateValid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(activeState->getConfStateInvalid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(backupConfInputVarAreSim2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfInputVarAreDif2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfOutputVarAreSim2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfOutputVarAreDif2State);
+ csv_writer->make_new_field();
+
+#endif // WRITE_MORE_INFORMATION_IN_CSV
+
+ confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(
+ discreteAveragePartitionSize, FuncBlockDriftDeviation);
+ float confidenceNoDrift = 1 - confidenceDrift;
+ // AAAAAAAAA (New: "&& activeState->isStateValidAfterReentrance()")
+ if (confidenceDrift > 0.5 &&
+ activeState->isStateValidAfterReentrance()) {
+#ifdef PRINT
+ printDrift();
#endif // PRINT
-#ifdef STOP_AFTER_BROKEN
- brokentest = 1;
-#endif // STOP_AFTER_BROKEN
-#ifdef STOP_WHEN_BROKEN
- getchar();
-#endif // STOP_WHEN_BROKEN
-
- //print broken
- csv_writer->write_field(STATUS_BROKEN);
- csv_writer->make_new_field();
-
- //calculate and print conf
- float conf = fuzzyAND(fuzzyOR(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(confidenceBroken, activeState->getConfStateValid()));
- csv_writer->write_field(conf);
- //csv_writer->make_new_field();
- }
- else {
- //print ok
- csv_writer->write_field(STATUS_OKAY);
- csv_writer->make_new_field();
-
- //calculate and print conf
- float conf = fuzzyAND(fuzzyOR(fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState), fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState)), fuzzyAND(confidenceOK, activeState->getConfStateValid()));
- csv_writer->write_field(conf);
- }
-
-
-
-
-
-
- }
- //In- and output changed
- else {
- brokenCounter = 0;
-
- printf(" > delete active state\n");
- activeState = NULL;
- printf(" > new active state\n");
-
- // search in vector for matching state //TODO in future: look for the best matching, Not for the first matching
- bool flagFoundMatchingState = false;
- float confInputVarAreSim2ActiveState;
- float confInputVarAreDif2ActiveState;
- float confOutputVarAreSim2ActiveState;
- float confOutputVarAreDif2ActiveState;
- for (auto &state : vStates) {
- confInputVarAreSim2ActiveState = state->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- confInputVarAreDif2ActiveState = state->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
- confOutputVarAreSim2ActiveState = state->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- confOutputVarAreDif2ActiveState = state->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
-
- if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) {
- activeState = state;
- flagFoundMatchingState = true;
- }
- }
-
- if (flagFoundMatchingState == false) {
- makeNewActiveState();
- confInputVarAreSim2ActiveState = 0;
- confInputVarAreDif2ActiveState = 0;
- confOutputVarAreSim2ActiveState = 0;
- confOutputVarAreDif2ActiveState = 0;
- }
-
- //insert in activeState
- if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize))
- addActiveStateToStateVector();
- //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime
- float newBoundary = (float)activeState->getLengthOfHistory();
- FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
- FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
-
-
- //print state number
- if (activeState->isStateValid())
- csv_writer->write_field((int)vStates.size());
- else
- csv_writer->write_field((int)vStates.size() + 1);
- csv_writer->make_new_field();
-
- //print conf valid
- csv_writer->write_field(activeState->getConfStateValid());
- csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateInvalid());
- csv_writer->make_new_field();
-
- //print conf statechange
- csv_writer->write_field(confInputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confInputVarAreDif2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreDif2ActiveState);
- csv_writer->make_new_field();
-
-
-
-
- confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation);
- float confidenceNoDrift = 1 - confidenceDrift;
-
- if (confidenceDrift > 0.5) {
- setColor(TXTCOLOR_YELLOW);
- printf("DRIFT\n");
-#ifdef STOP_WHEN_DRIFT
- getchar();
-#endif // STOP_WHEN_DRIFT
- setColor(TXTCOLOR_GREY);
-
- //print drift
- csv_writer->write_field(STATUS_DRIFT);
- csv_writer->make_new_field();
-
- //calc and print conf
- float conf = fuzzyAND(confidenceDrift, activeState->getConfStateValid());
- csv_writer->write_field(conf);
-
- }
- else {
- setColor(TXTCOLOR_LIGHTGREEN);
- printf("OK\n");
- setColor(TXTCOLOR_GREY);
-
- //print ok
- csv_writer->write_field(STATUS_OKAY);
- csv_writer->make_new_field();
-
- //calc and print conf
- float conf = fuzzyAND(fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift));
- csv_writer->write_field(conf);
- }
- csv_writer->make_new_field();
- }
-
-
- }
- //was NOT Valid
- else {
-
- brokenCounter = 0;
-
- printf(" > delete active state\n");
- delete activeState;
- activeState = NULL;
- printf(" > new active state\n");
-
-
- // search in vector for matching state //TODO in future: look for the best matching, Not for the first matching
- bool flagFoundMatchingState = false;
- float confInputVarAreSim2ActiveState;
- float confInputVarAreDif2ActiveState;
- float confOutputVarAreSim2ActiveState;
- float confOutputVarAreDif2ActiveState;
- for (auto &state : vStates) {
- confInputVarAreSim2ActiveState = state->getConfInputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- confInputVarAreDif2ActiveState = state->getConfInputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
- confOutputVarAreSim2ActiveState = state->getConfOutputVarAreSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime);
- confOutputVarAreDif2ActiveState = state->getConfOutputVarAreDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime);
-
- if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) {
- activeState = state;
- flagFoundMatchingState = true;
- }
- }
-
- if (flagFoundMatchingState == false) {
- makeNewActiveState();
- confInputVarAreSim2ActiveState = 0;
- confInputVarAreDif2ActiveState = 0;
- confOutputVarAreSim2ActiveState = 0;
- confOutputVarAreDif2ActiveState = 0;
- }
-
- //insert in active state
- if (activeState->insertValueInState(FuncBlockConfValStateDev, FuncBlockConfInvStateDev, FuncBlockConfValStateTime, FuncBlockConfInvStateTime, maxStateHistoryLength, discreteAveragePartitionSize))
- addActiveStateToStateVector();
- //NEW - Adjust FuncBlockConfSim2StateTime and FuncBlockConfDif2StateTime
- float newBoundary = (float)activeState->getLengthOfHistory();
- FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
- FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
-
-
- //print state number
- if (activeState->isStateValid())
- csv_writer->write_field((int)vStates.size());
- else
- csv_writer->write_field((int)vStates.size() + 1);
- csv_writer->make_new_field();
-
- //print conf valid
- csv_writer->write_field(activeState->getConfStateValid());
- csv_writer->make_new_field();
- csv_writer->write_field(activeState->getConfStateInvalid());
- csv_writer->make_new_field();
-
- //print conf statechange
- csv_writer->write_field(confInputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confInputVarAreDif2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreSim2ActiveState);
- csv_writer->make_new_field();
- csv_writer->write_field(confOutputVarAreDif2ActiveState);
- csv_writer->make_new_field();
-
-
-
-
- confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation);
- float confidenceNoDrift = 1 - confidenceDrift;
-
- if (confidenceDrift > 0.5) {
- setColor(TXTCOLOR_YELLOW);
- printf("DRIFT\n");
#ifdef STOP_WHEN_DRIFT
- getchar();
+ getchar();
#endif // STOP_WHEN_DRIFT
- setColor(TXTCOLOR_GREY);
-
- //print drift
- csv_writer->write_field(2);
- csv_writer->make_new_field();
-
- //calc and print conf
- float conf = fuzzyAND(confidenceDrift, activeState->getConfStateValid());
- csv_writer->write_field(conf);
-
- }
- else {
- setColor(TXTCOLOR_LIGHTGREEN);
- printf("OK\n");
- setColor(TXTCOLOR_GREY);
-
- //print ok
- csv_writer->write_field(STATUS_OKAY);
- csv_writer->make_new_field();
- //calc and print conf
- float conf = fuzzyAND(fuzzyAND(confInputVarAreDif2ActiveState, confOutputVarAreDif2ActiveState), fuzzyAND(activeState->getConfStateValid(), confidenceNoDrift));
- csv_writer->write_field(conf);
- }
- csv_writer->make_new_field();
+ // print drift
+ csv_writer->write_field(STATUS_DRIFT);
+ csv_writer->make_new_field();
+ // calc and print conf
+ //AAAAAAAAAAAAAAAA
+ float conf = fuzzyAND(
+ fuzzyAND(backupConfInputVarAreSim2State,
+ backupConfOutputVarAreSim2State),
+ fuzzyAND(confidenceDrift, activeState->getConfStateValid()));
+ csv_writer->write_field(conf);
+ // printf("drift bei (flagFoundMatchingState): %i\n", cycle);
- }
-
- }
-
+ } else {
#ifdef PRINT
- printf("STATES: %u\n", (int)vStates.size());
+ printOK();
#endif // PRINT
- }
- }
-
- csv_writer->make_new_line();
-
- if (brokentest)
- getchar();
-
-
-
-
-
-
-
-
-
-
+ // print ok
+ csv_writer->write_field(STATUS_OKAY);
+ csv_writer->make_new_field();
+
+ // calc and print conf
+ //AAAAA1
+ //NOTE: next equation is without confOK because confOK = 1 anyway (because broken timer = 0)
+ float conf =
+ fuzzyAND(fuzzyOR(fuzzyAND(backupConfInputVarAreSim2State,
+ backupConfOutputVarAreSim2State),
+ fuzzyAND(backupConfInputVarAreDif2State,
+ backupConfOutputVarAreDif2State)),
+ fuzzyAND(activeState->getConfStateValid(),
+ confidenceNoDrift));
+ csv_writer->write_field(conf);
+ }
+ // csv_writer->make_new_field();
+
+ }
+ // ONLY In- OR Outputs match one state
+ else if (flagFoundHalfMatchingState == true) {
+
+ // active state is half matching
+ if (flagHalfMatchingStateIsActiveState == true) {
+ activeState = activeStateBackup;
+
+ brokenCounter++;
+
+ backupConfInputVarAreSim2State =
+ backupConfInputVarAreSim2ActiveState;
+ backupConfInputVarAreDif2State =
+ backupConfInputVarAreDif2ActiveState;
+ backupConfOutputVarAreSim2State =
+ backupConfOutputVarAreSim2ActiveState;
+ backupConfOutputVarAreDif2State =
+ backupConfOutputVarAreDif2ActiveState;
+ }
+ // only other state(s) is half matching
+ else if (flagHalfMatchingStateIsAnotherState == true) {
+
+ // printf("IS ER HIER?\n");
+
+ activeState->resetNumOfInsertedSamples();
+
+ if (flagActiveStateWasValid == false) {
+ delete activeStateBackup;
+ // activeState = NULL;
+#ifdef PRINT
+ printf(" > delete active state\n");
+#endif // PRINT
+ }
+ } else {
+ printf("Error in halfmatching\n");
+ getchar();
+ }
+
+ // new revision cam
+ if (activeState->isStateValid() == false ||
+ activeState->isStateValidAfterReentrance() == false) {
+ brokenCounter = 0;
+ }
+
+ csv_writer->write_field((int)vStates.size());
+ csv_writer->make_new_field();
+ ptrdiff_t stateNr =
+ find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ csv_writer->write_field((int)stateNr);
+ csv_writer->make_new_field();
+
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
+ csv_writer->write_field(activeState->getConfStateValid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(activeState->getConfStateInvalid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(backupConfInputVarAreSim2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfInputVarAreDif2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfOutputVarAreSim2State);
+ csv_writer->make_new_field();
+ csv_writer->write_field(backupConfOutputVarAreDif2State);
+ csv_writer->make_new_field();
+
+#endif // WRITE_MORE_INFORMATION_IN_CSV
+
+ // check broken counter
+#ifdef PRINT
+ printf("brokenCounter: %u\n", brokenCounter);
+#endif // PRINT
+ confidenceBroken =
+ FuncBlockConfBrokenSamples->getY((float)brokenCounter);
+ float confidenceOK = 1 - confidenceBroken;
+ if (confidenceBroken > 0.5) {
+#ifdef PRINT
+ setColor(TXTCOLOR_LIGHTRED);
+ printf("BROKEN\n");
+ setColor(TXTCOLOR_GREY);
+#endif // PRINT
+#ifdef STOP_AFTER_BROKEN
+ brokentest = 1;
+#endif // STOP_AFTER_BROKEN
+#ifdef STOP_WHEN_BROKEN
+ getchar();
+#endif // STOP_WHEN_BROKEN
+ // print broken
+ csv_writer->write_field(STATUS_BROKEN);
+ csv_writer->make_new_field();
+ // calculate and print conf
+ float conf = fuzzyAND(
+ fuzzyOR(confInputVarAreDif2ActiveState,
+ confOutputVarAreDif2ActiveState),
+ fuzzyAND(confidenceBroken, activeState->getConfStateValid()));
+ csv_writer->write_field(conf);
+ // csv_writer->make_new_field();
+ } else {
+ confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(
+ discreteAveragePartitionSize, FuncBlockDriftDeviation);
- /*
- //XXX - only for now
- for (unsigned int i = 1; i < (cycle - old_cycle); i++) {
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- csv_writer->make_new_line();
- //printf("%u\n", i);
- }
- old_cycle = cycle;
-
- confidenceStableInput = getConfVariablesAreStable(&vInputVariables);
- confidenceStableOutput = getConfVariablesAreStable(&vOutputVariables);
- confidenceStable = minValueOf2Values(confidenceStableInput, confidenceStableOutput);
- printf("confidence stable: %f\n", confidenceStable);
-
-
- confidenceUnstableInput = getConfVariablesAreUnstable(&vInputVariables);
- confidenceUnstableOutput = getConfVariablesAreUnstable(&vOutputVariables);
- printf("unstable In: %f, Out: %f\n", confidenceUnstableInput, confidenceUnstableOutput);
- confidenceUnstable = maxValueOf2Values(confidenceUnstableInput, confidenceUnstableOutput);
- printf("confidence unstable: %f\n", confidenceUnstable);
-
- if (confidenceUnstableInput > 0) {
- printf("jetzt\n");
- getchar();
- }
-
-
- //TEST
- if (confidenceStable > confidenceUnstable) {
- setColor(TXTCOLOR_LIGHTBLUE);
- printf("jetzt\n");
- setColor(TXTCOLOR_GREY);
-
- getchar();
- }
-
- //getchar();
-
-
- if (confidenceStable > confidenceUnstable) {
- //if (false) {
- printf(" > stable\n");
-
- //for the beginning (there is no state available/created) -> create state
- if (activeState == NULL && vStates.empty()) {
- printf(" > new state\n");
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
- confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections());
- }
- //there is an active state
- else if (activeState != NULL) {
-
- //caclulate confidences of deciding for same state
- float confidenceSameStateInput = activeState->inputVariablesAreRelatedFuzzy(SameState);
- float confidenceSameStateOutput = activeState->outputVariablesAreRelatedFuzzy(SameState);
- printf("ConfSameState\nIn: %f\nout: %f\n", confidenceSameStateInput, confidenceSameStateOutput);
-
+ float confidenceNoDrift = 1 - confidenceDrift;
- //In- and Outputs are unchanged
- if ((confidenceSameStateInput > confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput > confSameStateOutputAdjustableThreshold)) {
+ // AAAAAAAAA (New: "&& activeState->isStateValidAfterReentrance()")
+ if (confidenceDrift > 0.5 &&
+ activeState->isStateValidAfterReentrance()) {
+#ifdef PRINT
+ printDrift();
+#endif // PRINT
+#ifdef STOP_WHEN_DRIFT
+ getchar();
+#endif // STOP_WHEN_DRIFT
- printf(" > same state\n");
+ // print drift
+ csv_writer->write_field(STATUS_DRIFT);
+ csv_writer->make_new_field();
- //inject values
- activeState->injectValues(discreteAveragePartitionSize);
- //calculate the confidence to have a validState
- confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections());
+ // calc and print conf
+ //AAAAAAAAAAAAA
+ float conf = fuzzyAND(
+ fuzzyAND(backupConfInputVarAreSim2State,
+ backupConfOutputVarAreSim2State),
+ fuzzyAND(confidenceDrift, activeState->getConfStateValid()));
+ csv_writer->write_field(conf);
- //TODO DATE
- //check for drifting!!!
- //printDrift();
+ } else {
+#ifdef PRINT
+ printOK();
+#endif // PRINT
-
- }
- //In- and Outputs have changed
- else if ((confidenceSameStateInput <= confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput <= confSameStateOutputAdjustableThreshold)) {
+ // print ok
+ csv_writer->write_field(STATUS_OKAY);
+ csv_writer->make_new_field();
+
+ // calc and print conf
+ //AAAAAAAAAA
+ float conf =
+ fuzzyAND(fuzzyOR(fuzzyAND(backupConfInputVarAreSim2State,
+ backupConfOutputVarAreSim2State),
+ fuzzyAND(backupConfInputVarAreDif2State,
+ backupConfOutputVarAreDif2State)),
+ fuzzyAND(activeState->getConfStateValid(),
+ fuzzyAND(confidenceNoDrift, confidenceOK)));
+ csv_writer->write_field(conf);
+ }
+ }
+ // csv_writer->make_new_field();
+ }
+ // No State matches -> make new state
+ else {
+ // was active state valid? -> If not -> delete it
+ if (flagActiveStateWasValid == false) {
+ delete activeStateBackup;
+ // activeState = NULL;
+#ifdef PRINT
+ printf(" > delete active state\n");
+#endif // PRINT
+ }
- printf(" > change state\n");
- getchar();
- //active state is/was valid
- if (confidenceValidState > confValidStateAdjustableThreshold) {
+ brokenCounter = 0;
- printf("speicher\n");
- getchar();
+ // create new state and update it
+ makeNewActiveState();
+ confInputVarAreSim2ActiveState = 0;
+ confInputVarAreDif2ActiveState = 0;
+ confOutputVarAreSim2ActiveState = 0;
+ confOutputVarAreDif2ActiveState = 0;
+#ifdef PRINT
+ printf(" > new active state\n");
+#endif // PRINT
+ // insert in activeState
+ if (activeState->insertValueInState(
+ FuncBlockConfValStateDev, FuncBlockConfInvStateDev,
+ FuncBlockConfValStateTime, FuncBlockConfInvStateTime,
+ maxStateHistoryLength, discreteAveragePartitionSize))
+ addActiveStateToStateVector();
+ // NEW - Adjust FuncBlockConfSim2StateTime and
+ // FuncBlockConfDif2StateTime
+ float newBoundary = (float)activeState->getLengthOfHistory();
+ FuncBlockConfSim2StateTime->changeFunctionBlockIncr(newBoundary);
+ FuncBlockConfDif2StateTime->changeFunctionBlockDecr(newBoundary);
+
+ csv_writer->write_field((int)vStates.size());
+ csv_writer->make_new_field();
+
+ ptrdiff_t stateNr =
+ find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ csv_writer->write_field((int)stateNr);
+ csv_writer->make_new_field();
+
+#ifdef WRITE_MORE_INFORMATION_IN_CSV
+ csv_writer->write_field(activeState->getConfStateValid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(activeState->getConfStateInvalid());
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(confInputVarAreSim2ActiveState);
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(confInputVarAreDif2ActiveState);
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(confOutputVarAreSim2ActiveState);
+ csv_writer->make_new_field();
+
+ csv_writer->write_field(confOutputVarAreDif2ActiveState);
+ csv_writer->make_new_field();
+#endif // WRITE_MORE_INFORMATION_IN_CSV
+
+ csv_writer->write_field(STATUS_OKAY); // OK
+ csv_writer->make_new_field();
+
+ // NOTE: Status Conf = 0 because conf valid = 0
+ csv_writer->write_field(0);
+ // csv_writer->make_new_field();
+ }
+ }
+#ifdef PRINT
+ printf("isStateValid = %i\nValidAfterReentrance = %i\n",
+ activeState->isStateValid(),
+ activeState->isStateValidAfterReentrance());
+
+ ptrdiff_t pos = find(vStates.begin(), vStates.end(), activeState) -
+ vStates.begin() + 1;
+ printf("cycle: %u, \n STATES: %u\n ActualState: %u\n", cycle,
+ vStates.size(), pos);
+#endif // PRINT
+ }
+ }
- addActiveStateToStateVector();
-
- //TODO DATE
- //search for matching state
- //or
- printf(" > new state\n");
- //create an new active state
- makeNewActiveState();
-
- //inject values
- activeState->injectValues(discreteAveragePartitionSize);
- //calculate the confidence to have a validState
- confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections());
-
- //TODO DATE
- //check for drifting!!!
- //printDrift();
-
- }
- }
- //Only in- or outputs have changed
- else {
- //active state is/was valid
- if (confidenceValidState > confValidStateAdjustableThreshold) {
-
- addActiveStateToStateVector();
-
- printf(" > broken\n");
- brokenCounter++;
- confidenceBroken = BrokenCounterSamples->getY(brokenCounter);
-
- //getchar();
-
- //TODO DATE??
- //Save State
- }
- }
- }
- //there is no active state, but there is/are state(s)
- else {
-
- printf(" > old or new state\n");
-
- //getchar();
- //TODO DATE
- //search for matching state
- //or
- printf(" > new state\n");
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
- confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections());
-
- //TODO DATE
- //check for drifting!!!
- //printDrift();
- }
-
-
- if (activeState != NULL) {
- confidenceDrift = activeState->checkAllVariablesForDriftingFuzzy(discreteAveragePartitionSize, DriftDeviation);
- }
-
- //getchar();
- }
- //unstable
- else {
-
- printf(" > unstable\n");
-
- //there is/was an active state
- if (activeState != NULL) {
- //delete activeState;
-
- if (confidenceValidState > confValidStateAdjustableThreshold)
- addActiveStateToStateVector();
-
- activeState = NULL;
- }
- }
-
- //DATE TODO
- //STABLE CONFIDENCE MITEINBEZIEHEN
- if ((confidenceBroken >= confidenceBroken) && (confidenceBroken > confidenceBrokenAdjustableThreshold)) {
- setColor(TXTCOLOR_LIGHTRED);
- printf(" >> BROKEN - confidence %f\n", confidenceBroken);
- setColor(TXTCOLOR_GREY);
-
- getchar();
- }
- else if (confidenceDrift > confidenceDriftAdjustableThreshold) {
- setColor(TXTCOLOR_YELLOW);
- printf(" >> DRIFT - confidence %f\n", confidenceDrift);
- setColor(TXTCOLOR_GREY);
-
- //XXXXXXXXXX ????????????????????????????????????
- if (brokenCounter > 0)
- brokenCounter--;
-
- getchar();
- }
- else {
- setColor(TXTCOLOR_LIGHTGREEN);
-
- float confidenceOK;
- if (confidenceDrift > confidenceBroken)
- confidenceOK = 1 - confidenceDrift;
- else
- confidenceOK = 1 - confidenceBroken;
-
- printf(" >> SYSTEM OK - confidence %f\n", confidenceOK);
- setColor(TXTCOLOR_GREY);
- }
-
-
-
- printf("brokenCounter %u\n", brokenCounter);
- printf("number of states: %i\n", vStates.size());
- */
-
-
-
-
-
-
-
-
- /*
- if (variablesAreStable(&vInputVariables) && variablesAreStable(&vOutputVariables)) {
- printf(" > stable\n");
-
- //XXX - only for now
- csv_writer->write_field(2); //stable
- csv_writer->make_new_field();
-
- //getchar();
-
- if (activeState == NULL && vStates.empty()) {
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
-
- //XXX - only for now
- csv_writer->write_field(1); //new active state
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- }
- else {
- if (activeState != NULL) {
-
- printf("\nbeginning here:\n");
-
- bool flagInputUnchanged = activeState->inputVariablesAreRelated(thresholdToBeRelated);
- bool flagOutputUnchanged = activeState->outputVariablesAreRelated(thresholdToBeRelated);
-
- //input and/or output unchanged?
- if (flagInputUnchanged && flagOutputUnchanged) {
- activeState->injectValues(discreteAveragePartitionSize);
- if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) {
- printDrift();
-
- //XXX - only for now
- csv_writer->make_new_field();
- csv_writer->write_field(1); //drift
- csv_writer->make_new_field();
- }
- //XXX - only for now
- else {
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- }
- }
- else {
- if (activeState->getNumOfInjections() >= minNumToBeValidState) {
- if ((!flagInputUnchanged && flagOutputUnchanged) || (flagInputUnchanged && !flagOutputUnchanged)) {
- printBroken();
- getchar();
-
- //XXX - only for now
- csv_writer->make_new_field();
- csv_writer->write_field(2); //broken
- csv_writer->make_new_field();
- }
- else {
- addActiveStateToStateVector();
- if (!findRelatedStateAndMakeItActive()) {
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
-
- //XXX - only for now
- csv_writer->write_field(1); //new active state
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- }
- else {
- //next line is new
- activeState->resetDiscreteAveragePartitionCounter();
-
- //XXX - only for now
- csv_writer->write_field(2); //change to existing state
- csv_writer->make_new_field();
-
-
- activeState->injectValues(discreteAveragePartitionSize);
- if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) {
- printDrift();
-
- //XXX - only for now
- csv_writer->write_field(1); //drift
- csv_writer->make_new_field();
- }
- //XXX - only for now
- else {
- csv_writer->make_new_field();
- }
- }
- }
- }
- else {
- delete activeState;
- if (!findRelatedStateAndMakeItActive()) {
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
-
- //XXX - only for now
- csv_writer->write_field(1); //new active state
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- }
- else {
- //next line is new
- activeState->resetDiscreteAveragePartitionCounter();
-
- //XXX - only for now
- csv_writer->write_field(2); //change to existing state
- csv_writer->make_new_field();
-
- activeState->injectValues(discreteAveragePartitionSize);
- if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) {
- printDrift();
-
- //XXX - only for now
- csv_writer->write_field(1); //drift
- csv_writer->make_new_field();
- }
- //XXX - only for now
- else {
- csv_writer->make_new_field();
- }
- }
- }
- }
- }
- else {
- if (!findRelatedStateAndMakeItActive()) {
- makeNewActiveState();
- activeState->injectValues(discreteAveragePartitionSize);
-
- //XXX - only for now
- csv_writer->write_field(1); //new active state
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- }
- else {
- //next line is new
- activeState->resetDiscreteAveragePartitionCounter();
-
- //XXX - only for now
- csv_writer->write_field(2); //change to existing state
- csv_writer->make_new_field();
-
- activeState->injectValues(discreteAveragePartitionSize);
- if (!activeState->checkAllVariablesForNotDrifting(discreteAveragePartitionSize, compareDistanceDiscreteAveragePartition, thresholdNotDrift)) {
- printDrift();
-
- //XXX - only for now
- csv_writer->write_field(1); //drift
- csv_writer->make_new_field();
- }
- //XXX - only for now
- else {
- csv_writer->make_new_field();
- }
- }
- }
- }
-
-
-
-
-
- if (activeState != NULL) {
- printf(" -- an activeState exist: \n");
- printf(" --- injections: %u\n", activeState->getNumOfInjections());
-
- //XXX - only for now
- csv_writer->write_field((int)activeState->getNumOfInjections()); //number of injections
- csv_writer->make_new_line();
- }
- //XXX - only for now
- else {
- csv_writer->make_new_field();
- }
-
-
- printf(" -- Number of States (excl. activeState): %u\n", vStates.size());
- for (auto &s : vStates) {
- printf(" --- injections: %u\n", s->getNumOfInjections());
- }
- printf(" ... BrokenCounter: %u\n", brokenCounter);
- printf(" ... driftCounter: %u\n", driftCounter);
- printf("cycle: %u\n", cycle);
-
-
- if (test) {
- test = false;
- //getchar();
- }
-
- flagVariablesWereStable = true;
- }
-
- else {
-
- printf(" > unstable\n");
- //XXX - only for now
- csv_writer->write_field(1); //unstable
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- csv_writer->make_new_field();
- csv_writer->make_new_line();
-
-
- if (flagVariablesWereStable)
- test = true;
-
- //search for states with less injections in all states
- if (flagVariablesWereStable) {
- if (activeState != NULL) {
-
- if (activeState->getNumOfInjections() >= minNumToBeValidState) {
- addActiveStateToStateVector();
-
- }
- else {
- delete activeState;
- }
- activeState = NULL;
-
-
- //getchar();
-
- }
- }
-
- flagVariablesWereStable = false;
- }
+ csv_writer->make_new_line();
+ if (brokentest)
+ getchar();
+#ifdef STOP_EVERY_TIME
+ getchar();
+#endif // STOP_EVERY_TIME
- //xxx - only for now
- //csv_writer->make_new_line();
- */
+#ifdef STOP_EVERY_TIME_AFTER_SAMPLENR
+ if (cycle >= STOP_EVERY_TIME_AFTER_SAMPLENR)
+ getchar();
+#endif // STOP_EVERY_TIME_AFTER_SAMPLENR
}
-
void StateHandler::closeCsvFile() {
- if(csv_writer != NULL)
- csv_writer->close_file();
+ if (csv_writer != NULL)
+ csv_writer->close_file();
}
-string StateHandler::create_Output_File_Name(string cfg_parameter)
-{
- time_t rawtime;
- struct tm * timeinfo;
- //char output_file_name[200];
- char datetime[80];
- std::string output_file_name_str;
+string StateHandler::create_Output_File_Name(string cfg_parameter) {
+ time_t rawtime;
+ struct tm *timeinfo;
+ // char output_file_name[200];
+ char datetime[80];
+ std::string output_file_name_str;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+
+ strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", timeinfo);
- strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", timeinfo);
-
#ifndef INPUT_FILE_NAME
- output_file_name_str = output_directory_name + "output" + datetime + cfg_parameter + ".csv";
+ output_file_name_str =
+ output_directory_name + "output" + datetime + cfg_parameter + ".csv";
#else
- string out_dir = "../data/out/";
- output_file_name_str = out_dir + INPUT_FILE_NAME + datetime + cfg_parameter + ".csv";
+ string out_dir = "../data/out/";
+ output_file_name_str =
+ out_dir + INPUT_FILE_NAME + datetime + cfg_parameter + ".csv";
#endif // INPUT_FILE_NAME
-
- return output_file_name_str;
-}
-void StateHandler::set_CSV_Writer_parameter(string cfg_parameter)
-{
- string cur_Output_File_Name = this->create_Output_File_Name(cfg_parameter);
- csv_writer->reset_fpointer(cur_Output_File_Name);
+ return output_file_name_str;
}
-
-
-/*
-void StateHandler :: initStateHandler() {
- //activeState = NULL;
- thresholdToAverage = THRESHOLDTOAVG;
- minNumOfChangedForValidStateChange = MINNUMCHANGEDFORVALIDSTATECHANGE;
-
- minimumInjectionsForBeingState = MININJFORBEINGSTATE;
-}
-
-StateHandler :: StateHandler() {
- set_name(NO_NAME);
- initStateHandler();
-}
-
-StateHandler :: StateHandler(char* name) {
- set_name(name);
- initStateHandler();
-}
-
-bool StateHandler :: setMinimumInjectionsForBeingState(unsigned int minimumInjectionsForBeingState) {
- if (minimumInjectionsForBeingState > 0) {
- this->minimumInjectionsForBeingState = minimumInjectionsForBeingState;
- return true;
- }
- return false;
-}
-
-unsigned int StateHandler :: getMinimumInjectionsForBeingState() {
- return minimumInjectionsForBeingState;
-}
-
-bool StateHandler :: add_slot(SlaveAgentSlotOfAgent* slot) {
- if(slot != NULL) {
- try {
- vSlots.push_back(slot);
- return true;
- }
- catch(bad_alloc& error) {
- printError("bad_alloc caught: ", error.what());
- delete slot;
- }
- }
- return false;
-}
-
-void StateHandler :: setThresholdToAverage(float thresholdToAverage) {
- this->thresholdToAverage = thresholdToAverage;
-}
-
-float StateHandler :: getThresholdToAverage() {
- return thresholdToAverage;
-}
-
-void StateHandler::set_minNumOfChangedForValidStateChange(unsigned int minNumOfChangedForValidStateChange) {
- this->minNumOfChangedForValidStateChange = minNumOfChangedForValidStateChange;
-}
-
-unsigned int StateHandler::get_minNumOfChangedForValidStateChange() {
- return minNumOfChangedForValidStateChange;
-}
-
-
-bool StateHandler :: trigger() {
-
- bool flagWorked = true;
-
- printf("NumOfStates: ");
- for (auto &slot : vSlots) {
- printf("%u, ", slot->getNumberOfStates());
- }
- printf("\n");
-
-
- //Check all input values if they have changed more than threshold ...and count how many changed
- unsigned int numberOfChanges = 0;
- for (auto &slot : vSlots) {
- float value;
- if (slot->get_slaveAgentValue(&value)) {
-
- State* activeState = slot->getActiveState();
- if (activeState != NULL) {
- printf("act - ");
- if (activeState->isNew()) {
- printf("new - ");
- //numberOfChanges++;
- }
- else if (activeState->valueIsRelated(value, thresholdToAverage)) {
- printf("rel - ");
- }
- else {
- printf("nrel - ");
- numberOfChanges++;
- }
- }
-
- else {
- printf("nact - ");
- }
- }
- }
- printf("\n");
-
-
-
-
-
-
-
-
-
- printf(" >> Number of Changes: %u\n", numberOfChanges);
- //nothing has changes more than threshold
-
- if (numberOfChanges == 0) {
- printf("\n\n >>> inject in active state\n");
- for (auto &slot : vSlots) {
- slot->injectValueInActiveState();
- }
- }
- else if(numberOfChanges >= minNumOfChangedForValidStateChange) {
- printf("\n\n >>> new (or another) state\n");
-
- for (auto &slot : vSlots) {
- State* activeState = slot->getActiveState();
- if (activeState != NULL) {
- if (activeState->getNumberOfInjections() < minimumInjectionsForBeingState) {
- slot->deleteActiveState();
- printf(" >> delete State\n");
- }
- }
- }
-
- //search for existing state
- bool flagRelated = false;
- if (vSlots.empty() == false) {
- int ix = vSlots.front()->getIndexOfRelatedState(0, thresholdToAverage);
- while (ix > -2) {
- if (ix >= 0) {
- //TODO: maybe another state fits a bit better.. approach -> euklidean distance?
- flagRelated = true;
- for (vector<SlaveAgentSlotOfAgent*>::iterator slot = vSlots.begin() + 1; slot < vSlots.end(); slot++) {
- if ((*slot)->valueIsRelated(ix, thresholdToAverage) == false) {
- flagRelated = false;
- }
- }
- if (flagRelated == true) {
- for (auto &slot : vSlots) {
- slot->setActiveState(ix);
- }
- break;
- }
- ix = vSlots.front()->getIndexOfRelatedState(ix+1, thresholdToAverage);
- }
- }
- }
-
- if (flagRelated == false) {
- printf(" >> No related state found\n");
-
- printf("\n\n >>> inject in active state\n");
- for (auto &slot : vSlots) {
- slot->injectValueInActiveState();
- }
- }
-
-
- }
-
-
- printf("ende\n");
-
- return false;
+void StateHandler::set_CSV_Writer_parameter(string cfg_parameter) {
+ string cur_Output_File_Name = this->create_Output_File_Name(cfg_parameter);
+ csv_writer->reset_fpointer(cur_Output_File_Name);
}
-*/
diff --git a/Version_Max_07_05_2018_CMake/src/StateHandler.h b/Version_Max_07_05_2018_CMake/src/StateHandler.h
index e8e64a2..81ebb73 100755
--- a/Version_Max_07_05_2018_CMake/src/StateHandler.h
+++ b/Version_Max_07_05_2018_CMake/src/StateHandler.h
@@ -1,198 +1,163 @@
#ifndef STATEHANDLER_HEADERFILE
#define STATEHANDLER_HEADERFILE
#include "Module.h"
#include "SlaveAgentSlotOfAgent.h"
#include "State.h"
#include "StateVariable.h"
#include <vector>
//XXX - only for now
#include "CSV_Writer.h"
#include "LinearFunctionBlock.h"
using namespace std;
class StateHandler : public Module {
- public:
- LinearFunctionBlock* StabSamples;
- LinearFunctionBlock* StabDeviation;
- LinearFunctionBlock* UnstabDeviation;
- LinearFunctionBlock* SameState;
- LinearFunctionBlock* DriftDeviation;
- LinearFunctionBlock* BrokenCounterSamples;
- LinearFunctionBlock* ValidState;
-
- //NEU:
- //compare
- LinearFunctionBlock* FuncBlockConfSim2StateDev;
- LinearFunctionBlock* FuncBlockConfDif2StateDev;
- LinearFunctionBlock* FuncBlockConfSim2StateTime;
- LinearFunctionBlock* FuncBlockConfDif2StateTime;
- //insert
- LinearFunctionBlock* FuncBlockConfValStateDev;
- LinearFunctionBlock* FuncBlockConfInvStateDev;
- LinearFunctionBlock* FuncBlockConfValStateTime;
- LinearFunctionBlock* FuncBlockConfInvStateTime;
-
- LinearFunctionBlock* FuncBlockConfBrokenSamples;
-
-
-
-
-
private:
- //DATE18
- //XXX - >0,5?
- //discreate Average Partition Size adjustable?
- float confidenceStableInput;
- float confidenceStableOutput;
- float confidenceStable;
- float confStableAdjustableThreshold;
+ // compare
+ //Similar to State
+ LinearFunctionBlock *FuncBlockConfSim2StateDev;
+ LinearFunctionBlock *FuncBlockConfSim2StateTime;
+ //Different to State
+ LinearFunctionBlock *FuncBlockConfDif2StateDev;
+ LinearFunctionBlock *FuncBlockConfDif2StateTime;
- float confidenceUnstableInput;
- float confidenceUnstableOutput;
- float confidenceUnstable;
- float confidenceUnstableAdjustableThreshold;
+ // insert
+ //Similarity with in a state
+ LinearFunctionBlock *FuncBlockConfValStateDev;
+ LinearFunctionBlock *FuncBlockConfValStateTime;
+ //Unsimilarity with in a state
+ LinearFunctionBlock *FuncBlockConfInvStateDev;
+ LinearFunctionBlock *FuncBlockConfInvStateTime;
- float confidenceSameStateInput;
- float confSameStateInputAdjustableThreshold;
- float confidenceSameStateOutput;
- float confSameStateOutputAdjustableThreshold;
+ //Drift- and broken Fuzzy functions
+ LinearFunctionBlock *FuncBlockConfBrokenSamples;
+ LinearFunctionBlock *FuncBlockDriftDeviation;
- float confidenceValidState;
- float confValidStateAdjustableThreshold;
- unsigned int brokenCounter;
- float confidenceBroken;
- float confidenceBrokenAdjustableThreshold;
+ //TODO: would be better to have some multidisciplinary writer thing outside of the state handler where all modules can pass something to write out
+ CSV_Writer *csv_writer;
- unsigned int driftCounter;
float confidenceDrift;
- float confidenceDriftAdjustableThreshold;
-
-
+ float confidenceBroken;
- //XXX - Maybe Object "StateVariable" between StateHandler and Slot?!
- vector<SlaveAgentSlotOfAgent*> vInputVariables;
- vector<SlaveAgentSlotOfAgent*> vOutputVariables;
+ unsigned int brokenCounter = 0;
- vector<State*> vStates;
- State* activeState;
- unsigned int minNumToBeValidState;
-
- bool flagVariablesWereStable;
- unsigned int slidingWindowBufferSize;
- unsigned int minNumOfRelatedValuesToBeStable;
- float thresholdToBeStable;
+ // XXX - Maybe Object "StateVariable" between StateHandler and Slot?!
+ vector<SlaveAgentSlotOfAgent *> vInputVariables;
+ vector<SlaveAgentSlotOfAgent *> vOutputVariables;
- float thresholdToBeRelated;
+ vector<State *> vStates;
+ State *activeState;
unsigned int discreteAveragePartitionSize;
- unsigned int compareDistanceDiscreteAveragePartition;
- float thresholdNotDrift;
-
unsigned int maxStateHistoryLength;
- void initStateHandler();
-
- bool addVariable(vector<SlaveAgentSlotOfAgent*>* vVariables, SlaveAgentSlotOfAgent* slot);
- bool variablesAreStable(vector<SlaveAgentSlotOfAgent*>* vVariables);
-
- //DATE18
- float getConfVariableIsStable(SlaveAgentSlotOfAgent* variable);
- float getConfVariablesAreStable(vector<SlaveAgentSlotOfAgent*>* vVariables);
- float getConfVariableIsUnstable(SlaveAgentSlotOfAgent* variable);
- float getConfVariablesAreUnstable(vector<SlaveAgentSlotOfAgent*>* vVariables);
-
- //bool getConfAndUnconfVariableIsMatching(SlaveAgentSlotOfAgent* variable, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf);
- //bool getConfAndUnconfVariableIsMatching(State* state, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf);
- //bool getConfAndUnconfVariablesAreMatching(vector<SlaveAgentSlotOfAgent*>* vVariables, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf);
-
-
- State* makeNewState();
-
- string create_Output_File_Name(string cfg_Parameter);
-
- bool addActiveStateToStateVector();
- //bool addStateAndMakeItActive();
-
- bool makeNewActiveState();
-
- State* findRelatedState();
- bool findRelatedStateAndMakeItActive();
-
- void eraseStatesWithLessInjections();
bool delete_all_OuputVariables();
- bool delete_all_InputVariables();
- bool delete_allStates();
- //XXX - only for now:
- CSV_Writer *csv_writer;
+ bool delete_all_InputVariables();
+ bool delete_allStates();
+ void initStateHandler();
+ bool addVariable(vector<SlaveAgentSlotOfAgent*>* vVariables, SlaveAgentSlotOfAgent* slot);
+
+ State *makeNewState();
+ bool makeNewActiveState();
+ bool addActiveStateToStateVector();
+
+ string create_Output_File_Name(string cfg_Parameter);
+ void printOK();
+ void printDrift();
+ void printBroken();
public:
StateHandler();
StateHandler(std::string name);
+ // TODO: would be better to have some multidisciplinary writer thing outside of the state handler where all modules can pass something to write out
StateHandler(std::string name, std::string csvWriterPath);
bool setDiscreteAveragePartitionSize(unsigned int discreteAverage);
unsigned int getDiscreteAveragePartitionSize();
bool addInputVariable(SlaveAgentSlotOfAgent* slot);
bool addOutputVariable(SlaveAgentSlotOfAgent* slot);
- bool setSlidingWindowBufferSize(unsigned int slidingWindowBufferSize);
- bool setMinNumOfRelatedValuesToBeStable(unsigned int minNumOfRelatedValuesToBeStable);
- bool setThresholdToBeStable(float thresholdToBeStable);
- bool setThresholdToBeRelated(float thresholdToBeRelated);
+
void trigger(unsigned int cycle);
- //XXX - only for now
+ // TODO: would be better to have some multidisciplinary writer thing outside of the state handler where all modules can pass something to write out
void closeCsvFile();
void set_CSV_Writer_parameter(string cfg_parameter);
void reset_States();
void reset_States_and_Slave_Agents();
void setMaxStateHistoryLength(unsigned int maxStateHistoryLength);
- ~StateHandler();
-
-
+ void setSimilarToStateDeviationFuzzyFunction(
+ LinearFunctionBlock* FuncBlockConfSim2StateDev);
+ LinearFunctionBlock* getSimilarToStateDeviationFuzzyFunction();
+
+ void setSimilarToStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfSim2StateTime);
+ LinearFunctionBlock* getSimilarToStateTimeFuzzyFunction();
+
+ void setDifferentToStateFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfDif2StateDev);
+ LinearFunctionBlock *getDifferentToStateDeviationFuzzyFunction();
+
+ void setDifferentToStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfDif2StateTime);
+ LinearFunctionBlock *getDifferentToStateTimeFuzzyFunction();
+
+ void setSimilarityInStateDeviationFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfValStateDev);
+ LinearFunctionBlock *getSimilarityInStateDeviationFuzzyFunction();
+
+ void setSimilarityInStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfValStateTime);
+ LinearFunctionBlock *getSimilarityInStateTimeFuzzyFunction();
+
+ void setUnsimilarityInStateDeviationFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfInvStateDev);
+ LinearFunctionBlock *getUnsimilarityInStateDeviationFuzzyFunction();
+
+ void setUnsimilarityInStateTimeFuzzyFunction(
+ LinearFunctionBlock *FuncBlockConfInvStateTime);
+ LinearFunctionBlock *getUnsimilarityInStateTimeFuzzyFunction();
+
+ void setDriftFuzzyFunction(LinearFunctionBlock *FuncBlockDriftDeviation);
+ LinearFunctionBlock *getDriftFuzzyFunction();
+
+ void setBrokenFuzzyFunction(LinearFunctionBlock *FuncBlockConfBrokenSamples);
+ LinearFunctionBlock *getBrokenFuzzyFunction();
+
+ void setSimilarToStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfSim2StateDev,
+ LinearFunctionBlock *FuncBlockConfSim2StateTime);
+ void setDifferentToStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfDif2StateDev,
+ LinearFunctionBlock *FuncBlockConfDif2StateTime);
+ void setSimilarityInStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfValStateDev,
+ LinearFunctionBlock *FuncBlockConfValStateTime);
+ void setUnsimilarityInStateFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockConfInvStateDev,
+ LinearFunctionBlock *FuncBlockConfInvStateTime);
+ void setDriftAndBrokenFuzzyFunctions(
+ LinearFunctionBlock *FuncBlockDriftDeviation,
+ LinearFunctionBlock *FuncBlockConfBrokenSamples);
- /*
- private:
- vector<SlaveAgentSlotOfAgent*> vSlots;
-
- unsigned int minNumOfChangedForValidStateChange;
-
- unsigned int minimumInjectionsForBeingState;
-
- void initStateHandler();
-
- public:
- StateHandler();
- StateHandler(char* name);
-
- bool setMinimumInjectionsForBeingState(unsigned int minimumInjectionsForBeingState);
- unsigned int getMinimumInjectionsForBeingState();
-
- //TODO: function for deleting slot and function with the whole vector as parameter
- bool add_slot(SlaveAgentSlotOfAgent* slot);
-
- void set_minNumOfChangedForValidStateChange(unsigned int minNumOfChangedForValidStateChange);
- unsigned int get_minNumOfChangedForValidStateChange();
+ ~StateHandler();
- bool trigger();
- */
};
#endif
diff --git a/Version_Max_07_05_2018_CMake/src/SubState.cpp b/Version_Max_07_05_2018_CMake/src/SubState.cpp
index 4a62dda..1616c44 100755
--- a/Version_Max_07_05_2018_CMake/src/SubState.cpp
+++ b/Version_Max_07_05_2018_CMake/src/SubState.cpp
@@ -1,347 +1,395 @@
#include "SubState.h"
#include "printError.h"
#include "relationChecker.h"
#include "minmaxzeug.h"
#include <iostream>
#include <vector>
#include <algorithm>
SubState::SubState() {
confidenceValidState = 0;
confidenceInvalidState = 1;
+
+ numOfInsertedSamples = 0;
}
void SubState::setSlot(SlaveAgentSlotOfAgent* slot) {
this->slot = slot;
}
SlaveAgentSlotOfAgent* SubState::getSlot() {
return slot;
}
bool SubState::addNewDiscreteAverage() {
AverageValue* averageValue = new (nothrow) AverageValue();
if (averageValue != NULL) {
try {
vDiscreteAverage.push_back(averageValue);
//printf("vDiscreteAverage size = %u\n", vDiscreteAverage.size());
return true;
}
catch (bad_alloc& error) {
printError("bad_alloc caught: ", error.what());
delete averageValue;
}
}
return false;
}
bool SubState::injectValue() {
float value;
if (slot->get_slaveAgentValue(&value)) {
statisticValue.injectAndCalculateStatisticValue(value);
if (!vDiscreteAverage.empty()) {
vDiscreteAverage.back()->injectAndCalculateAverageValue(value);
return true;
}
}
return false;
}
bool SubState::valueIsRelated(float thresholdToBeRelated) {
float value;
if (slot->get_slaveAgentValue(&value)) {
return valueIsRelatedToReferenceValueOrBetweenMinAndMax(statisticValue.getAverageValue(), statisticValue.getMinimumValue(), statisticValue.getMaximumValue(), value, thresholdToBeRelated);
}
return false;
}
unsigned int SubState::getNumOfInjections() {
return statisticValue.getInjectedValuesCounter();
}
bool SubState::lastDiscreteAverageBlockIsCompleted(unsigned int discreteAveragePartitionSize) {
if (!vDiscreteAverage.empty()) {
if (vDiscreteAverage.back()->getInjectedValuesCounter() < discreteAveragePartitionSize) {
return false;
}
}
return true;
}
unsigned int SubState::getNumberOfCompletedDiscreteAverageBlocks(unsigned int discreteAveragePartitionSize) {
unsigned int numberOfDiscreteAverageBlocks = vDiscreteAverage.size();
//printf("vDiscreteAverage.size() = %u\n", numberOfDiscreteAverageBlocks);
if (!lastDiscreteAverageBlockIsCompleted(discreteAveragePartitionSize)) {
numberOfDiscreteAverageBlocks--;
}
return vDiscreteAverage.size();
}
float SubState::getDiscreteAverageOfFirstBlock(unsigned int discreteAveragePartitionSize) {
if (getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > 0) {
return vDiscreteAverage.front()->getAverageValue();
}
//TODO: error handling - return 0 is not acceptable
return 0;
}
float SubState::getDiscreteAverageOfLastBlock(unsigned int discreteAveragePartitionSize) {
if (lastDiscreteAverageBlockIsCompleted(discreteAveragePartitionSize)) {
return vDiscreteAverage.back()->getAverageValue();
}
else if (vDiscreteAverage.size() > 1) {
return vDiscreteAverage.at(vDiscreteAverage.size()-1)->getAverageValue();
}
//TODO: error handling - return 0 is not acceptable
return 0;
}
float SubState::getDiscreteAverageOfBlockBeforeLastBlock(unsigned int discreteAveragePartitionSize, unsigned int jumpBackDistance) {
if (getNumberOfCompletedDiscreteAverageBlocks(discreteAveragePartitionSize) > jumpBackDistance) {
if (lastDiscreteAverageBlockIsCompleted(discreteAveragePartitionSize)) {
return vDiscreteAverage.at(vDiscreteAverage.size() - jumpBackDistance)->getAverageValue();
}
else {
return vDiscreteAverage.at(vDiscreteAverage.size() - (jumpBackDistance + 1))->getAverageValue();
}
}
else {
return vDiscreteAverage.front()->getAverageValue();
}
}
void SubState::deleteLastDiscreteAverageBlockIfNotCompleted(unsigned int discreteAveragePartitionSize) {
if (!vDiscreteAverage.empty()) {
if (vDiscreteAverage.back()->getInjectedValuesCounter() < discreteAveragePartitionSize) {
vDiscreteAverage.pop_back();
}
}
}
//DATE18
float SubState::valueIsRelatedFuzzy(LinearFunctionBlock* SameState) {
//XXX - Original war: valueIsRelatedToReferenceValueOrBetweenMinAndMax!
float sampleValue;
if (slot->get_slaveAgentValue(&sampleValue)) {
printf("geht hinein - sample: %f, average: %f\n", sampleValue, statisticValue.getAverageValue());
return SameState->getY(deviationValueReferenceValue(sampleValue, statisticValue.getAverageValue()));
}
printf("leider hier\n");
//todo: isn't the best error handling
return 0;
}
bool SubState::insertValueInSubState(LinearFunctionBlock* FuncBlockConfValStateDev, LinearFunctionBlock* FuncBlockConfInvStateDev, LinearFunctionBlock* FuncBlockConfValStateTime, LinearFunctionBlock* FuncBlockConfInvStateTime, unsigned int historySize) {
bool insertionWorked = true;
float sampleValue;
if (slot->get_slaveAgentValue(&sampleValue)) {
//statistic value
statisticValue.injectAndCalculateStatisticValue(sampleValue);
//DABs
if (vDiscreteAverage.empty())
insertionWorked = false;
else
vDiscreteAverage.back()->injectAndCalculateAverageValue(sampleValue);
float worstConfidenceDeviation = 1;
float bestConfidenceDeviation = 0;
for (auto &historyValue : lSampleHistory) {
bestConfidenceDeviation = maxValueOf2Values(bestConfidenceDeviation, FuncBlockConfInvStateDev->getY(deviationValueReferenceValue(sampleValue, historyValue)));
worstConfidenceDeviation = minValueOf2Values(worstConfidenceDeviation, FuncBlockConfValStateDev->getY(deviationValueReferenceValue(sampleValue, historyValue)));
}
lBestConfidencesDeviation.push_front(bestConfidenceDeviation);
lWorstConfidencesDeviation.push_front(worstConfidenceDeviation);
+ numOfInsertedSamples++;
+ if (numOfInsertedSamples > historySize) {
+ //printf("davor - numOfInsertedSamples = %i\n", numOfInsertedSamples);
+ numOfInsertedSamples = historySize;
+ //printf("danach - numOfInsertedSamples = %i\n", numOfInsertedSamples);
+ }
+
+
//save actual value in history
try {
lSampleHistory.push_front(sampleValue);
}
catch (bad_alloc& error) {
printError("bad_alloc caught: ", error.what());
insertionWorked = false;
}
//delete last history- and deviation entry if history is full
while (lSampleHistory.size() > historySize) {
lSampleHistory.pop_back();
lBestConfidencesDeviation.pop_back();
lWorstConfidencesDeviation.pop_back();
}
//calculate the confidence with that the actual value fits to all of the history values
bestConfidenceDeviation = 0;
worstConfidenceDeviation = 1;
for (auto &confDev : lBestConfidencesDeviation)
bestConfidenceDeviation = minValueOf2Values(bestConfidenceDeviation, confDev);
for (auto &confDev : lWorstConfidencesDeviation)
worstConfidenceDeviation = maxValueOf2Values(worstConfidenceDeviation, confDev);
//printf("confidence invalid time: %f\n", FuncBlockConfInvStateTime->getY((float)lSampleHistory.size()));
+ //new revision CAM - the next two rows were the original
+ //confidenceValidState = minValueOf2Values(worstConfidenceDeviation, FuncBlockConfValStateTime->getY((float)lSampleHistory.size()));
+ //confidenceInvalidState = maxValueOf2Values(bestConfidenceDeviation, FuncBlockConfInvStateTime->getY((float)lSampleHistory.size()));
+ confidenceValidState = minValueOf2Values(worstConfidenceDeviation, FuncBlockConfValStateTime->getY((float)numOfInsertedSamples));
+ confidenceInvalidState = maxValueOf2Values(bestConfidenceDeviation, FuncBlockConfInvStateTime->getY((float)numOfInsertedSamples));
+
+ //printf("numOfInsertedSamples = %i - confidenceValidState = %f - confidenceInvalidState = %f\n", numOfInsertedSamples, confidenceValidState, confidenceInvalidState);
+
- confidenceValidState = minValueOf2Values(worstConfidenceDeviation, FuncBlockConfValStateTime->getY((float)lSampleHistory.size()));
- confidenceInvalidState = maxValueOf2Values(bestConfidenceDeviation, FuncBlockConfInvStateTime->getY((float)lSampleHistory.size()));
}
return insertionWorked;
/*
float value;
if (slot->get_slaveAgentValue(&value)) {
statisticValue.injectAndCalculateStatisticValue(value);
if (!vDiscreteAverage.empty()) {
vDiscreteAverage.back()->injectAndCalculateAverageValue(value);
return true;
}
}
return false;
*/
}
float SubState::getConfidenceValidState() {
return confidenceValidState;
}
float SubState::getConfidenceInvalidState() {
return confidenceInvalidState;
}
float SubState::getConfVarIsSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) {
float highestConfOf1Var = 0;
float sampleValue;
float deviation = 0.0;
if (slot->get_slaveAgentValue(&sampleValue)) {
vector<float> vDeviations;
vDeviations.clear();
for (auto &h : lSampleHistory) {
deviation = deviationValueReferenceValue(sampleValue, h);
vDeviations.push_back(deviation);
}
sort(begin(vDeviations), end(vDeviations));
//all adaptabilities within the history of one variable
for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) {
float lowestConfOfSamplesIncluded = 1;
unsigned int histSampleCounter = 0;
for (auto &deviation : vDeviations) {
if (histSampleCounter >= numOfHistSamplesIncluded)
break;
- lowestConfOfSamplesIncluded = fuzzyAND(lowestConfOfSamplesIncluded, FuncBlockConfSim2StateDev->getY(deviation));
+
+
+ float confDev;
+
+ //new revision CAM - MAKE IT MORE BEAUTIFUL - Die If Anweisung und der Else zweig sind neu ... der inhalt vom if war original)
+ if (std::isinf(deviation) == false) {
+ confDev = FuncBlockConfSim2StateDev->getY(deviation);
+ }
+ else {
+ confDev = 0;
+ }
+
+ lowestConfOfSamplesIncluded = fuzzyAND(lowestConfOfSamplesIncluded, confDev);
histSampleCounter++;
+
}
highestConfOf1Var = fuzzyOR(highestConfOf1Var, fuzzyAND(lowestConfOfSamplesIncluded, FuncBlockConfSim2StateTime->getY((float)histSampleCounter)));
}
}
return highestConfOf1Var;
}
//Sorting with bigger Value in Front
struct descending
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
float SubState::getConfVarIsDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) {
//float highestConfOf1Var = 0;
float highestConfOf1Var = 1;
float sampleValue;
if (slot->get_slaveAgentValue(&sampleValue)) {
vector<float> vDeviations;
for (auto &h : lSampleHistory)
vDeviations.push_back(deviationValueReferenceValue(sampleValue, h));
sort(begin(vDeviations), end(vDeviations), descending());
//all adaptabilities within the history of one variable
for (unsigned int numOfHistSamplesIncluded = 1; numOfHistSamplesIncluded <= vDeviations.size(); numOfHistSamplesIncluded++) {
float highestConfOfSamplesIncluded = 0;
unsigned int histSampleCounter = 0;
for (auto &deviation : vDeviations) {
if (histSampleCounter >= numOfHistSamplesIncluded)
break;
- highestConfOfSamplesIncluded = fuzzyOR(highestConfOfSamplesIncluded, FuncBlockConfDif2StateDev->getY(deviation));
+
+
+ float confDev;
+
+ //new revision CAM - MAKE IT MORE BEAUTIFUL - Die If Anweisung und der Else zweig sind neu ... der inhalt vom if war original)
+ if (std::isinf(deviation) == false) {
+ confDev = FuncBlockConfDif2StateDev->getY(deviation);
+ }
+ else {
+ confDev = 1;
+ }
+
+
+
+ highestConfOfSamplesIncluded = fuzzyOR(highestConfOfSamplesIncluded, confDev);
histSampleCounter++;
}
//highestConfOf1Var = fuzzyOR(highestConfOf1Var, fuzzyOR(highestConfOfSamplesIncluded, FuncBlockConfDif2StateTime->getY((float)histSampleCounter)));
highestConfOf1Var = fuzzyAND(highestConfOf1Var, fuzzyOR(highestConfOfSamplesIncluded, FuncBlockConfDif2StateTime->getY((float)histSampleCounter)));
}
}
return highestConfOf1Var;
}
unsigned int SubState::getSampleHistoryLength() {
return lSampleHistory.size();
}
+void SubState::resetNumOfInsertedSamples() {
+ numOfInsertedSamples = 0;
+}
+
+
void SubState::remove_Average_Values()
{
AverageValue* cur_Average_Value;
unsigned int cur_index_Average_Value;
unsigned int size_vec_Avergae_Values = vDiscreteAverage.size();
for(cur_index_Average_Value = 0; cur_index_Average_Value < size_vec_Avergae_Values; cur_index_Average_Value++){
cur_Average_Value = vDiscreteAverage[cur_index_Average_Value];
delete cur_Average_Value;
}
vDiscreteAverage.clear();
}
SubState::~SubState()
{
remove_Average_Values();
}
diff --git a/Version_Max_07_05_2018_CMake/src/SubState.h b/Version_Max_07_05_2018_CMake/src/SubState.h
index 6a2310f..81d3b6b 100755
--- a/Version_Max_07_05_2018_CMake/src/SubState.h
+++ b/Version_Max_07_05_2018_CMake/src/SubState.h
@@ -1,68 +1,72 @@
#ifndef SUBSTATE_HEADERFILE
#define SUBSTATE_HEADERFILE
#include "SlaveAgentSlotOfAgent.h"
#include "StatisticValue.h"
#include "LinearFunctionBlock.h"
class SubState {
private:
SlaveAgentSlotOfAgent* slot;
StatisticValue statisticValue;
vector<AverageValue*> vDiscreteAverage;
bool lastDiscreteAverageBlockIsCompleted(unsigned int discreteAveragePartitionSize);
list<float> lSampleHistory;
list<float> lBestConfidencesDeviation;
list<float> lWorstConfidencesDeviation;
//DATE18
float confidenceValidState;
float confidenceInvalidState;
void remove_Average_Values();
+ unsigned int numOfInsertedSamples;
+
public:
SubState();
void setSlot(SlaveAgentSlotOfAgent* slot);
SlaveAgentSlotOfAgent* getSlot();
bool addNewDiscreteAverage();
bool injectValue();
bool valueIsRelated(float thresholdToBeRelated);
unsigned int getNumOfInjections();
unsigned int getNumberOfCompletedDiscreteAverageBlocks(unsigned int discreteAveragePartitionSize);
float getDiscreteAverageOfFirstBlock(unsigned int discreteAveragePartitionSize);
float getDiscreteAverageOfLastBlock(unsigned int discreteAveragePartitionSize);
float getDiscreteAverageOfBlockBeforeLastBlock(unsigned int discreteAveragePartitionSize, unsigned int jumpBackDistance);
void deleteLastDiscreteAverageBlockIfNotCompleted(unsigned int discreteAveragePartitionSize);
//DATE18
float valueIsRelatedFuzzy(LinearFunctionBlock* SameState);
bool insertValueInSubState(LinearFunctionBlock* FuncBlockConfValStateDev, LinearFunctionBlock* FuncBlockConfInvStateDev, LinearFunctionBlock* FuncBlockConfValStateTime, LinearFunctionBlock* FuncBlockConfInvStateTime, unsigned int historySize);
float getConfidenceValidState();
float getConfidenceInvalidState();
float getConfVarIsSim2State(LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime);
float getConfVarIsDif2State(LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime);
unsigned int getSampleHistoryLength();
+ void resetNumOfInsertedSamples();
+
~SubState();
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Sat, May 17, 2:22 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
141547
Default Alt Text
(142 KB)

Event Timeline