Page MenuHomePhorge

No OneTemporary

Size
12 KB
Referenced Files
None
Subscribers
None
diff --git a/Version_Max_07_05_2018_CMake/src/SubState.cpp b/Version_Max_07_05_2018_CMake/src/SubState.cpp
index 1616c44..7e650a0 100755
--- a/Version_Max_07_05_2018_CMake/src/SubState.cpp
+++ b/Version_Max_07_05_2018_CMake/src/SubState.cpp
@@ -1,395 +1,403 @@
#include "SubState.h"
#include "printError.h"
#include "relationChecker.h"
#include "minmaxzeug.h"
#include <iostream>
#include <vector>
-#include <algorithm>
+#include <algorithm>
+
+#ifndef _WIN32
+#include <math.h>
+#endif // ! _WIN32
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);
}
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;
float confDev;
//new revision CAM - MAKE IT MORE BEAUTIFUL - Die If Anweisung und der Else zweig sind neu ... der inhalt vom if war original)
+#ifdef _WIN32
if (std::isinf(deviation) == false) {
+#else
+ if (isinf(deviation) == false) {
+#endif // _WIN32
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;
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();
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, May 17, 8:08 AM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
141521
Default Alt Text
(12 KB)

Event Timeline