diff --git a/include/rosa/agent/experimental/AverageValue.hpp b/include/rosa/agent/experimental/AverageValue.hpp new file mode 100644 index 0000000..a889eee --- /dev/null +++ b/include/rosa/agent/experimental/AverageValue.hpp @@ -0,0 +1,23 @@ +#ifndef AVERAGEVALUE_HEADERFILE +#define AVERAGEVALUE_HEADERFILE + +class AverageValue { + +private: + float avgValue; + unsigned int injectedValuesCounter; + +public: + AverageValue(); + + void resetAverageValue(); + + void injectAndCalculateAverageValue(float value); + float getAverageValue(); + + unsigned int getInjectedValuesCounter(); + bool averageValueIsValid(); + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/CSV_Writer.hpp b/include/rosa/agent/experimental/CSV_Writer.hpp new file mode 100644 index 0000000..da48f4b --- /dev/null +++ b/include/rosa/agent/experimental/CSV_Writer.hpp @@ -0,0 +1,35 @@ +#ifndef CSV_WRITER_HEADERFILE +#define CSV_WRITER_HEADERFILE + +#include "Module.hpp" +#include + +#define MAX_LENGTH_OF_NAMES_TO_WRITE 50 + +class CSV_Writer : public Module { + + private: + FILE *fpointer_write; + //unsigned int num_of_datasets; + //float datasets[MAX_NUM_OF_DATA_SETS]; + + //private fuctions + void initialize_csv_writer(char* filepath_write); + + public: + + CSV_Writer(char* filepath_write); + CSV_Writer(char* name, char* filepath_write); + + bool write_field(int dataset); + bool write_field(float dataset); + bool write_field(char* dataset); + bool make_new_field(); + bool make_new_line(); + + bool write_row_data(unsigned int num_of_datasets, float* datasets); + + void close_file(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/CSVreaderModule.hpp b/include/rosa/agent/experimental/CSVreaderModule.hpp new file mode 100644 index 0000000..679f065 --- /dev/null +++ b/include/rosa/agent/experimental/CSVreaderModule.hpp @@ -0,0 +1,47 @@ +#ifndef CSV_READER_HEADERFILE +#define CSV_READER_HEADERFILE + +#include "Module.hpp" +#include + +class CSVreaderModule : public Module { + + private: + FILE *fpointer; + unsigned int num_of_datasets; + unsigned int list_of_datasets[MAX_NUM_OF_DATA_SETS]; + float data_read[MAX_NUM_OF_DATA_SETS]; + unsigned int row, start_row, column; + float input_data; + bool flag_csv_reader_configured; + unsigned int dataset_counter; + + //private fuctions + //void initialize_csvreader(char* filepath_read, unsigned int num_of_datasets, unsigned int* list_of_datasets, unsigned int start_row); + void initialize_csvreader(FILE* fpointer, unsigned int column, unsigned int start_row); + + public: + CSVreaderModule(); + CSVreaderModule(char* name); + CSVreaderModule(FILE* fpointer, unsigned int column, unsigned int start_row); + CSVreaderModule(char* name, FILE* fpointer, unsigned int column, unsigned int start_row); + /* + CSV_Reader(char* filepath_read, unsigned int column, unsigned int start_row); + CSV_Reader(char* name, char* filepath_read, unsigned int column, unsigned int start_row); + */ + + bool read_one_row(); + + + bool read_field(); + float get_value_of_field(unsigned int field); + + + //new functions + bool get_next_value(float* value); + + + void close_file(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Channel.hpp b/include/rosa/agent/experimental/Channel.hpp new file mode 100644 index 0000000..635a438 --- /dev/null +++ b/include/rosa/agent/experimental/Channel.hpp @@ -0,0 +1,72 @@ +#ifndef CHANNEL_HEADERFILE +#define CHANNEL_HEADERFILE + +#include +#include "rosa/agent/experimental/Message.hpp" +#include "rosa/agent/experimental/Module.hpp" + + +#define MAX_BUFFER_LENGTH 100 +#define OPEN NULL + +using namespace std; + +class Channel : public Module { + + private: + + list lInputMsgBufferUp; + list lOutputMsgBufferUp; + + list lInputMsgBufferDown; + list lOutputMsgBufferDown; + + unsigned int maxBufferLength; + unsigned int transferRate; + + void init_channel(); + + bool add_msgAtBegin (list* buffer, Message* message); + bool del_msgAtBegin (list* buffer); + + bool add_msgAtEnd (list* buffer, Message* message); + bool del_msgAtEnd (list* buffer); + + public: + Channel(); + Channel(char* name); + + bool set_maxBufferLength(unsigned int maxBufferLength); + unsigned int get_maxBufferLength(); + + unsigned int get_avlInputBufferUp(); + unsigned int get_avlOutputBufferUp(); + unsigned int get_avlInputBufferDown(); + unsigned int get_avlOutputBufferDown(); + + bool set_transferRate(unsigned int transferRate); + unsigned int get_transferRate(); + + bool send_MsgUp(Message* message); + bool send_MsgUp(float msg); + bool send_MsgUp(int msg); + bool get_MsgUp(float* msg); + bool get_MsgUp(int* msg); + bool isThereFloatMsgUp(); + bool isThereIntMsgUp(); + + bool send_MsgDown(Message* message); + bool send_MsgDown(float msg); + bool send_MsgDown(int msg); + bool get_MsgDown(float* msg); + bool get_MsgDown(int* msg); + bool isThereFloatMsgDown(); + bool isThereIntMsgDown(); + + bool trigger(); + bool transferMsgs(list* dest_buffer, list* src_buffer); + +}; + + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Domain.hpp b/include/rosa/agent/experimental/Domain.hpp new file mode 100644 index 0000000..44d92f9 --- /dev/null +++ b/include/rosa/agent/experimental/Domain.hpp @@ -0,0 +1,26 @@ +#ifndef DOMAIN_HEADERFILE +#define DOMAIN_HEADERFILE + +class Domain { + +private: + bool flagLowerBoundaryExist, flagUpperBoundaryExist; + float lowerBoundary, upperBoundary; + +public: + Domain(); + + void setLowerBoundary(float lowerBoundary); + void setUpperBoundary(float upperBoundary); + void setBoundaries(float lowerBoundary, float upperBoundary); + void unsetBoundaries(); + bool lowerBoundaryExist(); + float getLowerBoundary(); + bool getLowerBoundary(float *lowerBoundary); + bool upperBoundaryExist(); + float getUpperBoundary(); + bool getUpperBoundary(float *upperBoundary); + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/ExtremeValue.hpp b/include/rosa/agent/experimental/ExtremeValue.hpp new file mode 100644 index 0000000..95d266f --- /dev/null +++ b/include/rosa/agent/experimental/ExtremeValue.hpp @@ -0,0 +1,18 @@ +#ifndef EXTREMEVALUE_HEADERFILE +#define EXTREMEVALUE_HEADERFILE + +#include "MinimumValue.hpp" +#include "MaximumValue.hpp" + +class ExtremeValue : public MinimumValue, public MaximumValue { + +public: + ExtremeValue(); + + void resetExtremeValue(); + + void injectAndCalculateExtremeValue(float value); + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/LinearFunction.hpp b/include/rosa/agent/experimental/LinearFunction.hpp new file mode 100644 index 0000000..7f8e0e4 --- /dev/null +++ b/include/rosa/agent/experimental/LinearFunction.hpp @@ -0,0 +1,33 @@ +#ifndef LINEARFUNCTION_HEADERFILE +#define LINEARFUNCTION_HEADERFILE + +#include "Domain.hpp" + +class LinearFunction { + + private: + Domain domain; + float k, d; + + public: + LinearFunction(); + + Domain* setDomain(bool flagLowerBoundaryExist, float lowerBoundary, bool flagUpperBoundaryExist, float upperBoundary); //bounded both-sided + Domain* setDomain(bool flagLowerBoundaryExist, float lowerBoundary, bool flagUpperBoundaryExist); //bounded below + Domain* setDomain(bool flagLowerBoundaryExist, bool flagUpperBoundaryExist, float upperBoundary); //bounded above + Domain* setDomain(bool flagLowerBoundaryExist, bool flagUpperBoundaryExist); + + Domain* getDomain(); + + bool setKandD(float k, float d); + bool setKandD(float x1, float y1, float x2, float y2); + float getK(); + float getD(); + + float getY(float x); +}; + + + + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/LinearFunctionBlock.hpp b/include/rosa/agent/experimental/LinearFunctionBlock.hpp new file mode 100644 index 0000000..a670c25 --- /dev/null +++ b/include/rosa/agent/experimental/LinearFunctionBlock.hpp @@ -0,0 +1,34 @@ +#ifndef LINEARFUNCTIONBLOCK_HEADERFILE +#define LINEARFUNCTIONBLOCK_HEADERFILE + +#include +#include + +#include "rosa/agent/experimental/LinearFunction.hpp" +#include "rosa/agent/experimental/Unit.hpp" + +using namespace std; + +class LinearFunctionBlock : public Unit{ + +private: + //CHECK: list, vector or other container? + vector vLinearFunctions; + +public: + LinearFunctionBlock(); + //LinearFunctionBlock(char* name); + ~LinearFunctionBlock(); + + bool addLinearFunction(LinearFunction *linearFunction); + + //NOTE: Specific Function for CAH Project (DATE18) + void changeFunctionBlockIncr(float newBoundary); + void changeFunctionBlockDecr(float newBoundary); + + float getY(float x); + + void printFunctionBlock(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/MaximumValue.hpp b/include/rosa/agent/experimental/MaximumValue.hpp new file mode 100644 index 0000000..520ea3b --- /dev/null +++ b/include/rosa/agent/experimental/MaximumValue.hpp @@ -0,0 +1,19 @@ +#ifndef MAXIMUMVALUE_HEADERFILE +#define MAXIMUMVALUE_HEADERFILE + +class MaximumValue { + +private: + float maxValue; + bool flagMaxValueIsSet; + +public: + MaximumValue(); + + void resetMaximumValue(); + + void injectAndCalculateMaximumValue(float value); + float getMaximumValue(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Message.hpp b/include/rosa/agent/experimental/Message.hpp new file mode 100644 index 0000000..6e14395 --- /dev/null +++ b/include/rosa/agent/experimental/Message.hpp @@ -0,0 +1,23 @@ +#ifndef MESSAGE_HEADERFILE +#define MESSAGE_HEADERFILE + +class Message { + + private: + bool messageType; + float fMsg; + int iMsg; + + public: + //Message(); + Message(float msg); + Message(int msg); + + bool isMsgFloat(); + bool isMsgInt(); + + bool getMsg(float* msg); + bool getMsg(int* msg); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/MinimumValue.hpp b/include/rosa/agent/experimental/MinimumValue.hpp new file mode 100644 index 0000000..7228e91 --- /dev/null +++ b/include/rosa/agent/experimental/MinimumValue.hpp @@ -0,0 +1,19 @@ +#ifndef MINIMUMVALUE_HEADERFILE +#define MINIMUMVALUE_HEADERFILE + +class MinimumValue { + +private: + float minValue; + bool flagMinValueIsSet; + +public: + MinimumValue(); + + void resetMinimumValue(); + + void injectAndCalculateMinimumValue(float value); + float getMinimumValue(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Module.hpp b/include/rosa/agent/experimental/Module.hpp new file mode 100644 index 0000000..fb2e357 --- /dev/null +++ b/include/rosa/agent/experimental/Module.hpp @@ -0,0 +1,20 @@ +#ifndef MODULE_HEADERFILE +#define MODULE_HEADERFILE + +#include "Unit.hpp" + +class Module : public Unit { + + protected: + char name[MAX_LENGTH_NAME]; + + public: + Module(); + Module(char* name); + + void set_name(char* name); + char* get_name(); + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp b/include/rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp new file mode 100644 index 0000000..8ec94a6 --- /dev/null +++ b/include/rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp @@ -0,0 +1,75 @@ +#ifndef SLAVEAGENTSLOTOFAGENT_HEADERFILE +#define SLAVEAGENTSLOTOFAGENT_HEADERFILE + +#include "rosa/agent/experimental/SlotOfAgent.hpp" +//#include "State.h" + +class SlaveAgentSlotOfAgent : public SlotOfAgent { + +private: + // TODO: set- and get function for maxNumOf_mountedSensors; + float slaveAgentValue; + list lSlaveAgentHistory; + + bool flagSlaveAgentValueIsSet; + /* + //XXX - next 3 variables needed? + bool flagSlaveAgentValueHasChanged; + bool flagSlaveAgentValueChangeIsSet; + float slaveAgentValueChange; + */ + + /* + vector vStates; + State* activeState; + State* backupState; + + bool + ateToStatesVector(State* state); + */ + +public: + SlaveAgentSlotOfAgent(); + + void setSlaveAgentValue(float slaveAgentValue); + bool get_slaveAgentValue(float *slaveAgentValue); + + bool get_flagSlaveAgentValueIsSet(); + + // TODO: move these functions into -> HistoryHandler + bool saveValueInHistory(); + unsigned int getHistoryLength(); + bool deleteOldestHistoryEntry(); + unsigned int getNumberOfRelativesToActualValue(float threshold); + + // DATE18 + list getHistory(); + + void printHistory(); + + /* + bool get_flagSlaveAgentValueHasChanged(); + + bool get_slaveAgentValueChangingRate(float* slaveAgentValueChangingRate); + + + vector* getStatesVector(); + bool createNewStateAndMakeItActive(); + bool injectValueInActiveState(); + int getIndexOfRelatedState(unsigned int startIndex, float thresholdToAverage); + + bool relatedToActiveState(float thresholdToAverage); + bool valueIsRelated(unsigned int index, float thresholdToAverage); + + State* getActiveState(); + void deleteActiveState(); + bool setActiveState(unsigned int index); + unsigned int getNumberOfStates(); + */ + /* + vector* getStatesVector(); + State* getActiveState(); + */ +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Slot.hpp b/include/rosa/agent/experimental/Slot.hpp new file mode 100644 index 0000000..a109603 --- /dev/null +++ b/include/rosa/agent/experimental/Slot.hpp @@ -0,0 +1,19 @@ +#ifndef SLOT_HEADERFILE +#define SLOT_HEADERFILE + +#include "rosa/agent/experimental/Channel.hpp" +//#include "Slot.h" + +class Slot : public Unit { + + protected: + Channel* comPort; + + public: + Slot(); + + bool set_comPort(Channel* comPort); + Channel* get_comPort(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/SlotOfAgent.hpp b/include/rosa/agent/experimental/SlotOfAgent.hpp new file mode 100644 index 0000000..7831022 --- /dev/null +++ b/include/rosa/agent/experimental/SlotOfAgent.hpp @@ -0,0 +1,28 @@ +#ifndef SLOTOFAGENT_HEADERFILE +#define SLOTOFAGENT_HEADERFILE + +#include "rosa/agent/Confidence.hpp" +#include "rosa/agent/History.hpp" +#include "rosa/agent/experimental/Slot.hpp" + +class SlotOfAgent : public Slot { + + protected: + //History* historyModule; + //Confidence* confidenceModule; + //Abstraction + + public: + SlotOfAgent(); + /* + bool set_historyModule(HistoryModule* historyModule); + bool del_historyModule(); + HistoryModule* get_historyModule(); + + bool set_confidenceModule(ConfidenceModule* confidenceModule); + bool del_confidenceModule(); + ConfidenceModule* get_confidenceModule(); + */ +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/State.hpp b/include/rosa/agent/experimental/State.hpp new file mode 100644 index 0000000..077ff8a --- /dev/null +++ b/include/rosa/agent/experimental/State.hpp @@ -0,0 +1,109 @@ +#ifndef STATE_HEADERFILE +#define STATE_HEADERFILE + +#include "rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp" +#include "rosa/agent/experimental/SubState.hpp" +#include +#include "rosa/agent/experimental/LinearFunctionBlock.hpp" + +class State { + + private: + + vector vInputSubStates; + vector vOutputSubStates; + + //unsigned int discreteAveragePartitionSize; + unsigned int discreteAveragePartitionCounter; + + /* + StatisticValue continuousStatisticValue; + vector vDiscreteAveragePartition; + + bool addDiscreteAveragePartition(); + */ + + bool addSubState(vector* vSubStates, SlaveAgentSlotOfAgent* slot); + + bool variablesAreRelated(vector* vSubStates, float thresholdToBeRelated); + + bool checkSubStatesForNotDrifting(vector* vSubStates, unsigned int discreteAveragePartitionSize, /*unsigned int compareDistanceDiscreteAveragePartition,*/ float thresholdNotDrift); + + float confValidState; + float confInvalidState; + + bool stateIsValid; + + 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* vSubStates, unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift); + float checkAllVariablesForDriftingFuzzy(unsigned int discreteAveragePartitionSize, LinearFunctionBlock* Drift); + float variablesAreRelatedFuzzy(vector* 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(); + + private: + float getConfVarAreSim2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime); + float getConfVarAreDif2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime); + + + + /* + + + bool injectValue(float value); + bool valueIsRelated(float value, float thresholdToAverage); + + bool isNew(); + + unsigned int getNumberOfInjections(); + + void deleteState(); + */ + + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/StateHandler.hpp b/include/rosa/agent/experimental/StateHandler.hpp index d2ff364..e41bc61 100644 --- a/include/rosa/agent/experimental/StateHandler.hpp +++ b/include/rosa/agent/experimental/StateHandler.hpp @@ -1,193 +1,184 @@ -//===-- rosa/agent/experimental/StateHandler.hpp ----------------*- C++ -*-===// -// -// The RoSA Framework -// -//===----------------------------------------------------------------------===// -/// -/// \file rosa/agent/experimental/StateHandler.hpp -/// -/// \author Maximilian Götzinger (maxgot@utu.fi) -/// -/// \date 2017 -/// -/// \brief Definition of *experimental* *abstraction* *functionality*. -/// -//===----------------------------------------------------------------------===// - -#ifndef ROSA_AGENT_EXPERIMENTAL_STATEHANDLER_HPP -#define ROSA_AGENT_EXPERIMENTAL_STATEHANDLER_HPP - -//#include "rosa/agent/Functionality.h" - -#include "rosa/support/debug.hpp" - -#include -#include - -namespace rosa { -namespace agent { - -/* -/// Abstracts values from a type to another one. -/// -/// \tparam T type to abstract from -/// \tparam A type to abstract to -template class Abstraction : public Functionality { -protected: - /// Value to abstract to by default. - const A Default; - -public: - /// Creates an instance. - /// - /// \param Default value to abstract to by default - Abstraction(const A Default) noexcept : Default(Default) {} - - /// Destroys \p this object. - ~Abstraction(void) = default; - - /// Abstracts a value from type \p T to type \p A. - /// - /// \note The default implementation always returns - /// \c rosa::agent::Abstraction::Default, hence the actual argument is - /// ignored. - /// - /// \return the abstracted value - virtual A operator()(const T &) const noexcept { return Default; } -}; +#ifndef STATEHANDLER_HEADERFILE +#define STATEHANDLER_HEADERFILE -/// Implements \c rosa::agent::Abstraction as a \c std::map from a type to -/// another one. -/// -/// \note This implementation is supposed to be used to abstract between -/// enumeration types, which is statically enforced. -/// -/// \tparam T type to abstract from -/// \tparam A type to abstract to -template -class MapAbstraction : public Abstraction, private std::map { - // Make sure the actual type arguments are enumerations. - STATIC_ASSERT((std::is_enum::value && std::is_enum::value), - "mapping not enumerations"); - - // Bringing into scope inherited members. - using Abstraction::Default; - using std::map::end; - using std::map::find; - -public: - /// Creates an instance by initializing the underlying \c std::map. - /// - /// \param Map the mapping to do abstraction according to - /// \param Default value to abstract to by default - MapAbstraction(const std::map &Map, const A Default) noexcept - : Abstraction(Default), - std::map(Map) {} - - /// Destroys \p this object. - ~MapAbstraction(void) = default; - - /// Abstracts a value from type \p T to type \p A based on the set mapping. - /// - /// Results in the value associated by the set mapping to the argument, or - /// \c rosa::agent::MapAbstraction::Default if the actual argument is not - /// associated with anything by the set mapping. - /// - /// \param V value to abstract - /// - /// \return the abstracted value based on the set mapping - A operator()(const T &V) const noexcept override { - const auto I = find(V); - return I == end() ? Default : *I; - } -}; +#include "rosa/agent/experimental/Module.hpp" +#include "rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp" +#include "rosa/agent/experimental/State.hpp" -/// Implements \c rosa::agent::Abstraction as a \c std::map from ranges of a -/// type to values of another type. -/// -/// \note This implementation is supposed to be used to abstract ranges of -/// arithmetic types into enumerations, which is statically enforced. -/// -/// \invariant The keys in the underlying \c std::map define valid ranges -/// such that `first <= second` and there are no overlapping ranges defined by -/// the keys. -/// -/// \tparam T type to abstract from -/// \tparam A type to abstract to -template -class RangeAbstraction : public Abstraction, - private std::map, A> { - // Make sure the actual type arguments are matching our expectations. - STATIC_ASSERT((std::is_arithmetic::value), "abstracting not arithmetic"); - STATIC_ASSERT((std::is_enum::value), "abstracting not to enumeration"); - - // Bringing into scope inherited members. - using Abstraction::Default; - using std::map, A>::begin; - using std::map, A>::end; - using std::map, A>::find; - -public: - /// Creates an instance by Initializing the unserlying \c std::map. - /// - /// \param Map the mapping to do abstraction according to - /// \param Default value to abstract to by default - /// - /// \pre Each key defines a valid range such that `first <= second` and - /// there are no overlapping ranges defined by the keys. - RangeAbstraction(const std::map, A> &Map, const A &Default) - : Abstraction(Default), std::map, A>(Map) { - // Sanity check. - ASSERT(std::all_of( - begin(), end(), [this](const std::pair, A> &P) { - return P.first.first <= P.first.second && - std::all_of(++find(P.first), end(), - [&P](const std::pair, A> &R) { - // \note Values in \c Map are sorted. - return P.first.first < P.first.second && - P.first.second <= R.first.first || - P.first.first == P.first.second && - P.first.second < R.first.first; - }); - })); - } - - /// Destroys \p this object. - ~RangeAbstraction(void) = default; - - /// Abstracts a value from type \p T to type \p A based on the set mapping. - /// - /// Results in the value associated by the set mapping to the argument, or - /// \c rosa::agent::RangeAbstraction::Default if the actual argument is not - /// included in any of the ranges in the set mapping. - /// - /// \param V value to abstract - /// - /// \return the abstracted value based on the set mapping - A operator()(const T &V) const noexcept override { - auto I = begin(); - bool Found = false; // Indicates if \c I refers to a matching range. - bool Failed = false; // Indicates if it is pointless to continue searching. - while (!Found && !Failed && I != end()) { - if (V < I->first.first) { - // No match so far and \p V is below the next range, never will match. - // \note Keys are sorted in the map. - Failed = true; - } else if (I->first.first <= V && V < I->first.second) { - // Matching range found. - Found = true; - } else { - // Cannot conclude in this step, move to the next range. - ++I; - } - } - ASSERT(!Found || I != end()); - return Found ? I->second : Default; - } -}; -*/ +//#include "rosa/agent/experimental/StateVariable.hpp" +#include + +//XXX - only for now +#include "rosa/agent/experimental/CSV_Writer.hpp" +#include "rosa/agent/experimental/LinearFunctionBlock.hpp" + + +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; + + + unsigned int maxStateHistoryLength; + + private: + + //DATE18 + //XXX - >0,5? + //discreate Average Partition Size adjustable? + float confidenceStableInput; + float confidenceStableOutput; + float confidenceStable; + float confStableAdjustableThreshold; + + float confidenceUnstableInput; + float confidenceUnstableOutput; + float confidenceUnstable; + float confidenceUnstableAdjustableThreshold; + + float confidenceSameStateInput; + float confSameStateInputAdjustableThreshold; + float confidenceSameStateOutput; + float confSameStateOutputAdjustableThreshold; + + float confidenceValidState; + float confValidStateAdjustableThreshold; + + unsigned int brokenCounter; + float confidenceBroken; + float confidenceBrokenAdjustableThreshold; + + unsigned int driftCounter; + float confidenceDrift; + float confidenceDriftAdjustableThreshold; + + + + //XXX - Maybe Object "StateVariable" between StateHandler and Slot?! + vector vInputVariables; + vector vOutputVariables; + + vector vStates; + State* activeState; + unsigned int minNumToBeValidState; + + bool flagVariablesWereStable; + unsigned int slidingWindowBufferSize; + unsigned int minNumOfRelatedValuesToBeStable; + float thresholdToBeStable; + + float thresholdToBeRelated; + + unsigned int discreteAveragePartitionSize; + unsigned int compareDistanceDiscreteAveragePartition; + float thresholdNotDrift; + + + + void initStateHandler(); + + bool addVariable(vector* vVariables, SlaveAgentSlotOfAgent* slot); + bool variablesAreStable(vector* vVariables); + + //DATE18 + float getConfVariableIsStable(SlaveAgentSlotOfAgent* variable); + float getConfVariablesAreStable(vector* vVariables); + float getConfVariableIsUnstable(SlaveAgentSlotOfAgent* variable); + float getConfVariablesAreUnstable(vector* vVariables); -} // End namespace agent -} // End namespace rosa + //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* vVariables, LinearFunctionBlock* confDeviation, LinearFunctionBlock* confTime, float* conf, float* unconf); + + + State* makeNewState(); + + bool addActiveStateToStateVector(); + //bool addStateAndMakeItActive(); + + bool makeNewActiveState(); + + State* findRelatedState(); + bool findRelatedStateAndMakeItActive(); + + void eraseStatesWithLessInjections(); + + + //XXX - only for now: + CSV_Writer* csv_writer; + + + public: + StateHandler(); + StateHandler(char* name); + + 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 + void closeCsvFile(); + + void setCSVwriter(char* output_file_name); + + + + /* + private: + vector 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(); + + bool trigger(); + */ +}; -#endif // ROSA_AGENT_EXPERIMENTAL_STATEHANDLER_HPP +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/StatisticValue.hpp b/include/rosa/agent/experimental/StatisticValue.hpp new file mode 100644 index 0000000..adf5a77 --- /dev/null +++ b/include/rosa/agent/experimental/StatisticValue.hpp @@ -0,0 +1,18 @@ +#ifndef STATISTICVALUE_HEADERFILE +#define STATISTICVALUE_HEADERFILE + +#include "rosa/agent/experimental/AverageValue.hpp" +#include "rosa/agent/experimental/ExtremeValue.hpp" + +class StatisticValue : public AverageValue, public ExtremeValue { + +public: + StatisticValue(); + + void resetStatisticValue(); + + void injectAndCalculateStatisticValue(float value); + +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/SubState.hpp b/include/rosa/agent/experimental/SubState.hpp new file mode 100644 index 0000000..fed73ac --- /dev/null +++ b/include/rosa/agent/experimental/SubState.hpp @@ -0,0 +1,64 @@ +#ifndef SUBSTATE_HEADERFILE +#define SUBSTATE_HEADERFILE + +#include "rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp" +#include "rosa/agent/experimental/StatisticValue.hpp" + +#include "rosa/agent/experimental/LinearFunctionBlock.hpp" + +class SubState { + + private: + SlaveAgentSlotOfAgent* slot; + StatisticValue statisticValue; + vector vDiscreteAverage; + + bool lastDiscreteAverageBlockIsCompleted(unsigned int discreteAveragePartitionSize); + + list lSampleHistory; + list lBestConfidencesDeviation; + list lWorstConfidencesDeviation; + + + //DATE18 + float confidenceValidState; + float confidenceInvalidState; + + 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(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/Unit.hpp b/include/rosa/agent/experimental/Unit.hpp new file mode 100644 index 0000000..ec557f5 --- /dev/null +++ b/include/rosa/agent/experimental/Unit.hpp @@ -0,0 +1,40 @@ +#ifndef UNIT_HEADERFILE +#define UNIT_HEADERFILE + +#define MAX_LENGTH_NAME 50 +#define NO_NAME "unnamed" + +#define MAX_NUM_OF_MOUNTED_SLAVEAGENTS 10 +#define MAX_NUM_OF_MOUNTED_SENSORS 10 + +#define MAX_NUM_OF_DATA_SETS 100 + +#define MOUNTED true +#define UNMOUNTED false + +#define ACTIVE true +#define INACTIVE false + +#define YES true +#define NO false + +#define BOUND true +#define NO_BOUND false + +#define RATES_OF_CHANGE true +#define NO_RATES_OF_CHANGE false + +class Unit { + + protected: + static unsigned int num_of_units; + unsigned int id; + + public: + Unit(); + + void set_id(unsigned int value); + unsigned int get_id(); +}; + +#endif \ No newline at end of file diff --git a/include/rosa/agent/experimental/minmaxzeug.hpp b/include/rosa/agent/experimental/minmaxzeug.hpp new file mode 100644 index 0000000..9ef823b --- /dev/null +++ b/include/rosa/agent/experimental/minmaxzeug.hpp @@ -0,0 +1,10 @@ +#ifndef header_minmaxzeug +#define header_minmaxzeug + +float maxValueOf2Values(float value1, float value2); +float minValueOf2Values(float value1, float value2); + +float fuzzyOR(float value1, float value2); +float fuzzyAND(float value1, float value2); + +#endif // !header_minmaxzeug diff --git a/include/rosa/agent/experimental/relationChecker.hpp b/include/rosa/agent/experimental/relationChecker.hpp new file mode 100644 index 0000000..ab44eaa --- /dev/null +++ b/include/rosa/agent/experimental/relationChecker.hpp @@ -0,0 +1,10 @@ +#ifndef RELATIONCHECKER_HEADERFILE +#define RELATIONCHECKER_HEADERFILE + +float deviationValueReferenceValue(float sampleValue, float historyValue); +bool valueIsRelatedToReferenceValue(float referenceValue, float value, float threshold); +bool valueIsRelatedToReferenceValueOrBetweenMinAndMax(float referenceValue, float minimumValue, float maximumValue, float value, float threshold); + + + +#endif \ No newline at end of file diff --git a/lib/agent/CMakeLists.txt b/lib/agent/CMakeLists.txt index 4a3d5d1..99e27e4 100644 --- a/lib/agent/CMakeLists.txt +++ b/lib/agent/CMakeLists.txt @@ -1,18 +1,60 @@ set(LIB_INCLUDE_DIR ${ROSA_MAIN_INCLUDE_DIR}/rosa/agent) add_library(ROSAAgent ${LIB_INCLUDE_DIR}/namespace.h namespace.cpp ${LIB_INCLUDE_DIR}/Functionality.h Functionality.cpp ${LIB_INCLUDE_DIR}/Abstraction.hpp Abstraction.cpp ${LIB_INCLUDE_DIR}/History.hpp History.cpp ${LIB_INCLUDE_DIR}/Confidence.hpp Confidence.cpp ${LIB_INCLUDE_DIR}/experimental/namespace.h experimental/namespace.cpp + ${LIB_INCLUDE_DIR}/experimental/AverageValue.hpp + experimental/AverageValue.cpp + ${LIB_INCLUDE_DIR}/experimental/Channel.hpp + experimental/Channel.cpp + ${LIB_INCLUDE_DIR}/experimental/CSVreaderModule.hpp + experimental/CSVreaderModule.cpp + ${LIB_INCLUDE_DIR}/experimental/CSV_Writer.hpp + experimental/CSV_Writer.cpp + ${LIB_INCLUDE_DIR}/experimental/Domain.hpp + experimental/Domain.cpp + ${LIB_INCLUDE_DIR}/experimental/ExtremeValue.hpp + experimental/ExtremeValue.cpp + ${LIB_INCLUDE_DIR}/experimental/LinearFunction.hpp + experimental/LinearFunction.cpp + ${LIB_INCLUDE_DIR}/experimental/LinearFunctionBlock.hpp + experimental/LinearFunctionBlock.cpp + ${LIB_INCLUDE_DIR}/experimental/MaximumValue.hpp + experimental/MaximumValue.cpp + ${LIB_INCLUDE_DIR}/experimental/Message.hpp + experimental/Message.cpp + ${LIB_INCLUDE_DIR}/experimental/MinimumValue.hpp + experimental/MinimumValue.cpp + ${LIB_INCLUDE_DIR}/experimental/minmaxzeug.hpp + experimental/minmaxzeug.cpp + ${LIB_INCLUDE_DIR}/experimental/Module.hpp + experimental/Module.cpp + ${LIB_INCLUDE_DIR}/experimental/relationChecker.hpp + experimental/relationChecker.cpp + ${LIB_INCLUDE_DIR}/experimental/State.hpp + experimental/State.cpp + ${LIB_INCLUDE_DIR}/experimental/SlaveAgentSlotOfAgent.hpp + experimental/SlaveAgentSlotOfAgent.cpp + ${LIB_INCLUDE_DIR}/experimental/Slot.hpp + experimental/Slot.cpp + ${LIB_INCLUDE_DIR}/experimental/SlotOfAgent.hpp + experimental/SlotOfAgent.cpp ${LIB_INCLUDE_DIR}/experimental/StateHandler.hpp experimental/StateHandler.cpp + ${LIB_INCLUDE_DIR}/experimental/StatisticValue.hpp + experimental/StatisticValue.cpp + ${LIB_INCLUDE_DIR}/experimental/SubState.hpp + experimental/SubState.cpp + ${LIB_INCLUDE_DIR}/experimental/Unit.hpp + experimental/Unit.cpp ) diff --git a/lib/agent/experimental/AverageValue.cpp b/lib/agent/experimental/AverageValue.cpp new file mode 100644 index 0000000..f692216 --- /dev/null +++ b/lib/agent/experimental/AverageValue.cpp @@ -0,0 +1,39 @@ +#include "rosa/agent/experimental/AverageValue.hpp" + +#include + +AverageValue :: AverageValue() { + resetAverageValue(); +} + +void AverageValue :: resetAverageValue() { + avgValue = 0; + injectedValuesCounter = 0; +} + +void AverageValue ::injectAndCalculateAverageValue(float value) { + if(injectedValuesCounter > 0) { + avgValue = (avgValue*injectedValuesCounter + value) / (injectedValuesCounter + 1); + } + else { + avgValue = value; + } + injectedValuesCounter++; +} + +float AverageValue ::getAverageValue() { + return avgValue; +} + +unsigned int AverageValue :: getInjectedValuesCounter() { + return injectedValuesCounter; +} + +bool AverageValue::averageValueIsValid() { + //printf(" ------------------- hier\n"); + if (injectedValuesCounter > 0) { + //printf(" --- injVal %u\n", injectedValuesCounter); + return true; + } + return false; +} \ No newline at end of file diff --git a/lib/agent/experimental/CSV_Writer.cpp b/lib/agent/experimental/CSV_Writer.cpp new file mode 100644 index 0000000..f12e605 --- /dev/null +++ b/lib/agent/experimental/CSV_Writer.cpp @@ -0,0 +1,86 @@ +#include "rosa/agent/experimental/CSV_Writer.hpp" +//#include +//#include + +#define STRINGLENGTH 5000 + +void CSV_Writer :: initialize_csv_writer(char* filepath_write) { + //fpointer_write = fopen(filepath_write, "w"); + fopen_s(&fpointer_write, filepath_write, "w"); + + //if (fpointer_write == NULL) { + //printf("Path: %s", filepath_write); + //} +} + +CSV_Writer :: CSV_Writer(char* filepath_write) { + set_name((char *)NO_NAME); + initialize_csv_writer(filepath_write); +} + +CSV_Writer :: CSV_Writer(char* name, char* filepath_write) { + set_name(name); + initialize_csv_writer(filepath_write); +} + + + +bool CSV_Writer :: write_field(int dataset) { + if(fpointer_write) { + fprintf(fpointer_write, "%i", dataset); + return true; + } + return false; +} + +bool CSV_Writer :: write_field(float dataset) { + if(fpointer_write) { + fprintf(fpointer_write, "%f", dataset); + return true; + } + return false; +} + +bool CSV_Writer :: write_field(char* dataset) { + if(fpointer_write) { + fprintf(fpointer_write, "%s", dataset); + return true; + } + return false; +} + +bool CSV_Writer :: make_new_field() { + if(fpointer_write) { + fprintf(fpointer_write, ","); + return true; + } + return false; +} + +bool CSV_Writer :: make_new_line() { + if(fpointer_write) { + fprintf(fpointer_write, "\n"); + return true; + } + return false; +} + + +bool CSV_Writer :: write_row_data(unsigned int num_of_datasets, float* datasets) { + + if(fpointer_write) { + for(unsigned int d_ix=0; d_ix +#include + +#define STRINGLENGTH 5000 + +/* +void CSV_Reader :: initialize_csvreader(char* filepath_read, unsigned int num_of_datasets, unsigned int* list_of_datasets, unsigned int start_row) { + //fpointer_read = fopen(filepath_read, "r"); + fopen_s(&fpointer_read, filepath_read, "r"); + + this->num_of_datasets = num_of_datasets; + //TODO: sort.. falls es nicht in aufsteigender folge kommt + for(unsigned int d_ix=0; d_ixlist_of_datasets[d_ix] = list_of_datasets[d_ix]; + } + + row = 1; + this->start_row = start_row; +} +*/ + + void CSVreaderModule :: initialize_csvreader(FILE* fpointer, unsigned int column, unsigned int start_row) { + //fpointer_read = fopen(filepath_read, "r"); + + //TODO: file ponter schon aus CSV-Reader Creator Funktion übergeben.. dann kann man nämlich ausgeben, wenn da was nicht erstellt werden kann + //bool file_opened = fopen_s(&fpointer_read, filepath_read, "r"); + //TODO: etwas mit flag + + this->fpointer = fpointer; + this->row = 1; + this->column = column; + this->start_row = start_row; + + flag_csv_reader_configured = true; +} + + +/* +CSV_Reader :: CSV_Reader(char* filepath_read, unsigned int num_of_data_sets, unsigned int* list_of_datasets, unsigned int start_row) { + set_name(NO_NAME); + initialize_csvreader(filepath_read, num_of_data_sets, list_of_datasets, start_row); +} + +CSV_Reader :: CSV_Reader(char* name, char* filepath_read, unsigned int num_of_data_sets, unsigned int* list_of_datasets, unsigned int start_row) { + set_name(name); + initialize_csvreader(filepath_read, num_of_data_sets, list_of_datasets, start_row); +} +*/ + +CSVreaderModule :: CSVreaderModule() { + set_name((char *)NO_NAME); + flag_csv_reader_configured = false; +} + +CSVreaderModule :: CSVreaderModule(char* name) { + set_name(name); + flag_csv_reader_configured = false; +} + +CSVreaderModule :: CSVreaderModule(FILE* fpointer, unsigned int column, unsigned int start_row) { + set_name((char *)NO_NAME); + + if(fpointer) { + initialize_csvreader(fpointer, column, start_row); + } + else { + flag_csv_reader_configured = false; + } +} + +CSVreaderModule :: CSVreaderModule(char* name, FILE* fpointer, unsigned int column, unsigned int start_row) { + set_name(name); + + if(fpointer) { + initialize_csvreader(fpointer, column, start_row); + } + else { + flag_csv_reader_configured = false; + } +} +/* +CSV_Reader :: CSV_Reader(char* name, char* filepath_read, int column, int start_row) { + +} +*/ + + + +//XXX: Wird nicht mehr benutzt +//TODO: überarbeiten nach folgendem Beispiel https://msdn.microsoft.com/en-us/library/ftsafwz3.aspx +bool CSVreaderModule :: read_one_row() { + if(fpointer) + { + char readrow[STRINGLENGTH] = "";//, electedfield[STRINGLENGTH] = ""; + + //TODO: move following for-loop to "initialize_csvreader(...) + for(;row + +#define MAX_BUFFER_LENGTH 100 + +void Channel :: init_channel() { + maxBufferLength = MAX_BUFFER_LENGTH; + transferRate = MAX_BUFFER_LENGTH; +} + +Channel :: Channel() { + init_channel(); +} + +Channel :: Channel(char* name) { + set_name(name); + init_channel(); +} + +bool Channel :: set_maxBufferLength(unsigned int maxBufferLength) { + if(maxBufferLength <= MAX_BUFFER_LENGTH) { + this->maxBufferLength = maxBufferLength; + return true; + } + return false; +} + +unsigned int Channel :: get_maxBufferLength() { + return maxBufferLength; +} + +unsigned int Channel :: get_avlInputBufferUp() { + return maxBufferLength-lInputMsgBufferUp.size(); +} + +unsigned int Channel :: get_avlOutputBufferUp() { + return maxBufferLength-lOutputMsgBufferUp.size(); +} + +unsigned int Channel :: get_avlInputBufferDown() { + return maxBufferLength-lInputMsgBufferDown.size(); +} + +unsigned int Channel :: get_avlOutputBufferDown() { + return maxBufferLength-lOutputMsgBufferDown.size(); +} + +bool Channel :: set_transferRate(unsigned int transferRate) { + if(transferRate <= MAX_BUFFER_LENGTH) { + this->transferRate = transferRate; + return true; + } + return false; +} + +unsigned int Channel :: get_transferRate() { + return transferRate; +} + + + +bool Channel :: add_msgAtBegin (list* buffer, Message* message) { + //try { + buffer->push_front(message); + /*} + catch(bad_alloc& error) { + delete message; + return false; + }*/ + return true; +} + +bool Channel :: del_msgAtBegin (list* buffer) { + //try { + /* + printf("a\n"); + getchar(); + delete buffer->back(); + printf("b\n"); + getchar(); + */ + buffer->pop_front(); + /* + printf("c\n"); + getchar(); + */ + /*} + catch(bad_alloc& error) { + return false; + }*/ + return true; +} + +bool Channel :: add_msgAtEnd (list* buffer, Message* message) { + //try { + buffer->push_back(message); + /*} + catch(bad_alloc& error) { + delete message; + return false; + }*/ + return true; +} + +bool del_msgAtEnd (list* buffer) { + //try { + buffer->pop_back(); + /*} + catch(bad_alloc& error) { + return false; + }*/ + return true; +} + +bool Channel :: send_MsgUp(Message* message) { + if(message != NULL) { + if(transferRate == 0) { + //TODO: at the moment only one packet (in the front) gets deleted if buffer is full. However, the whole message (instruction+value) should be deleted in case of a full buffer + if(lOutputMsgBufferUp.size() == maxBufferLength) { + del_msgAtBegin(&lOutputMsgBufferUp); + } + return add_msgAtEnd(&lOutputMsgBufferUp, message); + } + else { + if(lInputMsgBufferUp.size() == maxBufferLength) { + //TODO: at the moment only one packet (in the front) gets deleted if buffer is full. However, the whole message (instruction+value) should be deleted in case of a full buffer + del_msgAtBegin(&lInputMsgBufferUp); + } + return add_msgAtEnd(&lInputMsgBufferUp, message); + } + } + return false; +} + +bool Channel :: send_MsgUp(float msg) { + Message* message = new Message(msg); + return send_MsgUp(message); +} + +bool Channel :: send_MsgUp(int msg) { + Message* message = new Message(msg); + return send_MsgUp(message); +} + +bool Channel :: get_MsgUp(float* msg) { + if(isThereFloatMsgUp()) { + float tempMsg; + if(lOutputMsgBufferUp.front()->getMsg(&tempMsg)) { + *msg = tempMsg; + del_msgAtBegin(&lOutputMsgBufferUp); + return true; + } + } + return false; +} + +bool Channel :: get_MsgUp(int* msg) { + if(isThereIntMsgUp()) { + int tempMsg; + if(lOutputMsgBufferUp.front()->getMsg(&tempMsg)) { + *msg = tempMsg; + del_msgAtBegin(&lOutputMsgBufferUp); + return true; + } + } + return false; +} + +bool Channel :: isThereFloatMsgUp() { + if(lOutputMsgBufferUp.size() > 0 ) { + if(lOutputMsgBufferUp.front() != NULL) { + return lOutputMsgBufferUp.front()->isMsgFloat(); + } + } + return false; +} + +bool Channel :: isThereIntMsgUp() { + if(lOutputMsgBufferUp.size() > 0 ) { + if(lOutputMsgBufferUp.front() != NULL) { + return lOutputMsgBufferUp.front()->isMsgInt(); + } + } + return false; +} + +bool Channel :: send_MsgDown(Message* message) { + if(message != NULL) { + if(transferRate == 0) { + if(lOutputMsgBufferDown.size() == maxBufferLength) { + del_msgAtBegin(&lOutputMsgBufferDown); + } + return add_msgAtEnd(&lOutputMsgBufferDown, message); + } + else { + if(lInputMsgBufferDown.size() == maxBufferLength) { + del_msgAtBegin(&lInputMsgBufferDown); + } + return add_msgAtEnd(&lInputMsgBufferDown, message); + } + } + return false; +} + +bool Channel :: send_MsgDown(float msg) { + Message* message = new Message(msg); + return send_MsgDown(message); +} + +bool Channel :: send_MsgDown(int msg) { + Message* message = new Message(msg); + return send_MsgDown(message); +} + +bool Channel :: get_MsgDown(float* msg) { + if(isThereFloatMsgDown()) { + float tempMsg; + if(lOutputMsgBufferDown.front()->getMsg(&tempMsg)) { + *msg = tempMsg; + del_msgAtBegin(&lOutputMsgBufferDown); + return true; + } + } + return false; +} + +bool Channel :: get_MsgDown(int* msg) { + if(isThereIntMsgDown()) { + int tempMsg; + if(lOutputMsgBufferDown.front()->getMsg(&tempMsg)) { + *msg = tempMsg; + del_msgAtBegin(&lOutputMsgBufferDown); + return true; + } + } + return false; +} + +bool Channel :: isThereFloatMsgDown() { + if(lOutputMsgBufferDown.size() > 0 ) { + if(lOutputMsgBufferDown.front() != NULL) { + return lOutputMsgBufferDown.front()->isMsgFloat(); + } + } + return false; +} + +bool Channel :: isThereIntMsgDown() { + if(lOutputMsgBufferDown.size() > 0 ) { + if(lOutputMsgBufferDown.front() != NULL) { + return lOutputMsgBufferDown.front()->isMsgInt(); + } + } + return false; +} + +bool Channel :: transferMsgs(list* dest_buffer, list* src_buffer) { + unsigned int NumOfMsgsToMove; + + if(transferRate <= src_buffer->size()) { + NumOfMsgsToMove = transferRate; + } + else { + NumOfMsgsToMove = src_buffer->size(); + } + + if(NumOfMsgsToMove <= maxBufferLength-dest_buffer->size()) { + for(unsigned int i=0; ifront())) { + if(!del_msgAtBegin(src_buffer)) { + return false; + } + } + else { + return false; + } + } + return true; + } + return false; +} + +bool Channel :: trigger() { + bool flag_worked = transferMsgs(&lOutputMsgBufferUp, &lInputMsgBufferUp) && transferMsgs(&lOutputMsgBufferUp, &lInputMsgBufferUp); + //printf("Channel %s: in_up %u, out_up %u, in_dn %u, out_dn %u,\n", name, lInputMsgBufferUp.size(), lOutputMsgBufferUp.size(), lInputMsgBufferDown.size(), lOutputMsgBufferDown.size()); + return flag_worked; + +} \ No newline at end of file diff --git a/lib/agent/experimental/Domain.cpp b/lib/agent/experimental/Domain.cpp new file mode 100644 index 0000000..a964176 --- /dev/null +++ b/lib/agent/experimental/Domain.cpp @@ -0,0 +1,60 @@ +#include "rosa/agent/experimental/Domain.hpp" + +Domain::Domain() { + this->flagLowerBoundaryExist = false; + this->lowerBoundary = 0; + this->flagUpperBoundaryExist = false; + this->upperBoundary = 0; +} + +void Domain::setLowerBoundary(float lowerBoundary) { + flagLowerBoundaryExist = true; + this->lowerBoundary = lowerBoundary; +} + +void Domain::setUpperBoundary(float upperBoundary) { + flagUpperBoundaryExist = true; + this->upperBoundary = upperBoundary; +} + +void Domain::setBoundaries(float lowerBoundary, float upperBoundary) { + setLowerBoundary(lowerBoundary); + setUpperBoundary(upperBoundary); +} + +void Domain::unsetBoundaries() { + this->flagLowerBoundaryExist = false; + this->flagUpperBoundaryExist = false; +} + +bool Domain::lowerBoundaryExist() { + return flagLowerBoundaryExist; +} + +float Domain::getLowerBoundary() { + return lowerBoundary; +} + +bool Domain::getLowerBoundary(float *lowerBoundary) { + if (flagLowerBoundaryExist) { + *lowerBoundary = this->lowerBoundary; + return true; + } + return false; +} + +bool Domain::upperBoundaryExist() { + return flagUpperBoundaryExist; +} + +float Domain::getUpperBoundary() { + return upperBoundary; +} + +bool Domain::getUpperBoundary(float *upperBoundary) { + if (flagUpperBoundaryExist) { + *upperBoundary = this->upperBoundary; + return true; + } + return false; +} \ No newline at end of file diff --git a/lib/agent/experimental/ExtremeValue.cpp b/lib/agent/experimental/ExtremeValue.cpp new file mode 100644 index 0000000..bde5706 --- /dev/null +++ b/lib/agent/experimental/ExtremeValue.cpp @@ -0,0 +1,15 @@ +#include "rosa/agent/experimental/ExtremeValue.hpp" + +ExtremeValue :: ExtremeValue() { + +} + +void ExtremeValue :: resetExtremeValue() { + resetMinimumValue(); + resetMaximumValue(); +} + +void ExtremeValue :: injectAndCalculateExtremeValue(float value) { + injectAndCalculateMinimumValue(value); + injectAndCalculateMaximumValue(value); +} \ No newline at end of file diff --git a/lib/agent/experimental/LinearFunction.cpp b/lib/agent/experimental/LinearFunction.cpp new file mode 100644 index 0000000..96e25f4 --- /dev/null +++ b/lib/agent/experimental/LinearFunction.cpp @@ -0,0 +1,89 @@ +#include "rosa/agent/experimental/LinearFunction.hpp" +#include + + +LinearFunction::LinearFunction() { + //printf(" > Linear Function created\n"); +} + +//bounded both-sided +Domain* LinearFunction::setDomain(bool flagLowerBoundaryExist, float lowerBoundary, bool flagUpperBoundaryExist, float upperBoundary) { + if (flagLowerBoundaryExist && flagUpperBoundaryExist && upperBoundary >= lowerBoundary) { + domain.setBoundaries(lowerBoundary, upperBoundary); + //printf("%f - %f\n", domain.getLowerBoundary(), domain.getUpperBoundary()); + return &domain; + } + return NULL; +} + +//bounded below +Domain* LinearFunction::setDomain(bool flagLowerBoundaryExist, float lowerBoundary, bool flagUpperBoundaryExist) { + if (flagLowerBoundaryExist && !flagUpperBoundaryExist) { + domain.setLowerBoundary(lowerBoundary); + //printf("%f - oo\n", domain.getLowerBoundary()); + return &domain; + } + return NULL; +} + +//bounded above +Domain* LinearFunction::setDomain(bool flagLowerBoundaryExist, bool flagUpperBoundaryExist, float upperBoundary) { + if (!flagLowerBoundaryExist && flagUpperBoundaryExist) { + domain.setUpperBoundary(upperBoundary); + //printf("oo - %f\n", domain.getUpperBoundary()); + return &domain; + } + return NULL; +} + +Domain* LinearFunction::setDomain(bool flagLowerBoundaryExist, bool flagUpperBoundaryExist) { + if (!flagLowerBoundaryExist && !flagUpperBoundaryExist) { + domain.unsetBoundaries(); + return &domain; + } + return NULL; +} + +Domain* LinearFunction::getDomain() { + return &domain; +} + +bool LinearFunction::setKandD(float k, float d) { + this->k = k; + this->d = d; + + return true; +} + +bool LinearFunction::setKandD(float x1, float y1, float x2, float y2) { + if (x1 < x2) { + k = (y2 - y1) / (x2 - x1); + } + else if (x2 < x1) { + k = (y1 - y2) / (x1 - x2); + } + else { + k = 1; + d = 0; + return false; + } + + d = y1 - k*x1; + + return true; +} + +float LinearFunction::getK() { + return k; +} + +float LinearFunction::getD() { + return d; +} + +float LinearFunction::getY(float x) { + float y; + y = k*x + d; + + return y; +} \ No newline at end of file diff --git a/lib/agent/experimental/LinearFunctionBlock.cpp b/lib/agent/experimental/LinearFunctionBlock.cpp new file mode 100644 index 0000000..cf853ef --- /dev/null +++ b/lib/agent/experimental/LinearFunctionBlock.cpp @@ -0,0 +1,114 @@ +#include "rosa/agent/experimental/LinearFunctionBlock.hpp" + +LinearFunctionBlock::LinearFunctionBlock() { + //printf(" > Linear Function Block created\n"); +} +/* +LinearFunctionBlock::LinearFunctionBlock(char* name){ + //printf(" > %s (id:%u) created\n", name, id); +} +*/ +LinearFunctionBlock::~LinearFunctionBlock() { + //TODO: delete allocated memory +} + +//NOTE: for this time being, linear functions have to be filled beginning from lowest x value +bool LinearFunctionBlock::addLinearFunction(LinearFunction *linearFunction) { + if (vLinearFunctions.empty()) { + //printf("empty\n"); + if (!(linearFunction->getDomain()->lowerBoundaryExist())) { + vLinearFunctions.push_back(linearFunction); + //printf(" - added function\n"); + return true; + } + } + else + { + //printf("nicht empty\n"); + if (vLinearFunctions.back()->getDomain()->upperBoundaryExist() && linearFunction->getDomain()->lowerBoundaryExist()) { + //printf("last function ub = %f, new function lb = %f\n", lLinearFunctions.back()->getDomain()->getUpperBoundary(), linearFunction->getDomain()->getLowerBoundary()); + if (vLinearFunctions.back()->getDomain()->getUpperBoundary() == linearFunction->getDomain()->getLowerBoundary()) { + vLinearFunctions.push_back(linearFunction); + //printf(" - added function\n"); + return true; + } + } + } + + printf(" - couldn't add function\n"); + return false; +} + +//NOTE: Specific Function for CAH Project (DATE18) +void LinearFunctionBlock::changeFunctionBlockIncr(float newBoundary) { + vLinearFunctions[1]->setDomain(true, (float)0, true, newBoundary); + vLinearFunctions[1]->setKandD((float)0, (float)0, newBoundary, (float)1); + vLinearFunctions[2]->setDomain(true, newBoundary, false); +} + +void LinearFunctionBlock::changeFunctionBlockDecr(float newBoundary) { + vLinearFunctions[1]->setDomain(true, (float)0, true, newBoundary); + vLinearFunctions[1]->setKandD((float)0, (float)1, newBoundary, (float)0); + vLinearFunctions[2]->setDomain(true, newBoundary, false); +} + + + +//TODO: jump discontinuity -> user must have the probability to set the value there +float LinearFunctionBlock::getY(float x) { + for (auto &linearFunction : vLinearFunctions) { + if (linearFunction->getDomain()->lowerBoundaryExist() && linearFunction->getDomain()->upperBoundaryExist()) { + if (x >= linearFunction->getDomain()->getLowerBoundary() && x <= linearFunction->getDomain()->getUpperBoundary()) { + return linearFunction->getY(x); + } + } + else if (linearFunction->getDomain()->lowerBoundaryExist()) { + if (x >= linearFunction->getDomain()->getLowerBoundary()) { + return linearFunction->getY(x); + } + } + else if (linearFunction->getDomain()->upperBoundaryExist()) { + if (x <= linearFunction->getDomain()->getUpperBoundary()) { + return linearFunction->getY(x); + } + } + else { + return linearFunction->getY(x); + } + } + + printf("DEFAULT!!!!!!!!!!!\n"); + getchar(); + //TODO: default return value is maybe not the best + return 0; +} + + + +void LinearFunctionBlock :: printFunctionBlock() { + + /* + printf("Name: %s", name); + + + for (auto &lf : vLinearFunctions) { + + float *lowerBoundary; + float *upperBoundary; + + if (lf->getDomain()->getLowerBoundary(lowerBoundary)) + printf("(x,y) %f, %f - ", lowerBoundary, lf->getY(*lowerBoundary)); + else + printf("oo - "); + + if (lf->getDomain()->getUpperBoundary(upperBoundary)) + printf("%f, %f", upperBoundary, lf->getY(*upperBoundary)); + else + printf("oo"); + + printf("\n"); + + } + */ + +} \ No newline at end of file diff --git a/lib/agent/experimental/MaximumValue.cpp b/lib/agent/experimental/MaximumValue.cpp new file mode 100644 index 0000000..7a4358d --- /dev/null +++ b/lib/agent/experimental/MaximumValue.cpp @@ -0,0 +1,27 @@ +#include "rosa/agent/experimental/MaximumValue.hpp" + +MaximumValue ::MaximumValue() { + resetMaximumValue(); +} + +void MaximumValue :: resetMaximumValue() { + flagMaxValueIsSet = false; +} + +void MaximumValue:: injectAndCalculateMaximumValue(float value) { + if (flagMaxValueIsSet == true) { + if (value > maxValue) { + maxValue = value; + } + } + else { + flagMaxValueIsSet = true; + maxValue = value; + } +} + +float MaximumValue:: getMaximumValue() { + return maxValue; +} + + diff --git a/lib/agent/experimental/Message.cpp b/lib/agent/experimental/Message.cpp new file mode 100644 index 0000000..fc6bbc7 --- /dev/null +++ b/lib/agent/experimental/Message.cpp @@ -0,0 +1,58 @@ +#include "rosa/agent/experimental/Message.hpp" + +#include + +#define MSG_IS_FLOAT true +#define MSG_IS_INT false + +/* +Message :: Message() { + +} +*/ + +Message :: Message(float msg) { + messageType = MSG_IS_FLOAT; + fMsg = msg; +} + +Message :: Message(int msg) { + messageType = MSG_IS_INT; + iMsg = msg; +} + +bool Message :: isMsgFloat() { + if(messageType == MSG_IS_FLOAT) { + return true; + } + + if(messageType == MSG_IS_INT) { + return false; + } + + + return false; +} + +bool Message :: isMsgInt() { + if(messageType == MSG_IS_INT) { + return true; + } + return false; +} + +bool Message :: getMsg(float* msg) { + if(messageType == MSG_IS_FLOAT) { + *msg = fMsg; + return true; + } + return false; +} + +bool Message :: getMsg(int* msg) { + if(messageType == MSG_IS_INT) { + *msg = iMsg; + return true; + } + return false; +} \ No newline at end of file diff --git a/lib/agent/experimental/MinimumValue.cpp b/lib/agent/experimental/MinimumValue.cpp new file mode 100644 index 0000000..43c9428 --- /dev/null +++ b/lib/agent/experimental/MinimumValue.cpp @@ -0,0 +1,25 @@ +#include "rosa/agent/experimental/MinimumValue.hpp" + +MinimumValue :: MinimumValue() { + resetMinimumValue(); +} + +void MinimumValue :: resetMinimumValue() { + flagMinValueIsSet = false; +} + +void MinimumValue :: injectAndCalculateMinimumValue(float value) { + if (flagMinValueIsSet == true) { + if (value < minValue) { + minValue = value; + } + } + else { + flagMinValueIsSet = true; + minValue = value; + } +} + +float MinimumValue :: getMinimumValue() { + return minValue; +} diff --git a/lib/agent/experimental/Module.cpp b/lib/agent/experimental/Module.cpp new file mode 100644 index 0000000..ba223ac --- /dev/null +++ b/lib/agent/experimental/Module.cpp @@ -0,0 +1,19 @@ +#include "rosa/agent/experimental/Module.hpp" +#include + +Module :: Module() { + set_name((char*)NO_NAME); +} + +Module :: Module(char* name) { + set_name(name); +} + + +void Module :: set_name(char* name) { + strncpy_s (this->name, name, MAX_LENGTH_NAME); +} + +char* Module :: get_name() { + return this->name; +} \ No newline at end of file diff --git a/lib/agent/experimental/SlaveAgentSlotOfAgent.cpp b/lib/agent/experimental/SlaveAgentSlotOfAgent.cpp new file mode 100644 index 0000000..af1fd61 --- /dev/null +++ b/lib/agent/experimental/SlaveAgentSlotOfAgent.cpp @@ -0,0 +1,214 @@ +#include "rosa/agent/experimental/SlaveAgentSlotOfAgent.hpp" + +//#include "printError.h" +#include "rosa/agent/experimental/relationChecker.hpp" + +SlaveAgentSlotOfAgent ::SlaveAgentSlotOfAgent() { + flagSlaveAgentValueIsSet = false; + /* + flagSlaveAgentValueHasChanged = false; + flagSlaveAgentValueChangeIsSet = false; + + activeState = NULL; + backupState = NULL; + */ +} + +void SlaveAgentSlotOfAgent ::setSlaveAgentValue(float slaveAgentValue) { + this->slaveAgentValue = slaveAgentValue; + flagSlaveAgentValueIsSet = true; + + /* + if(flagSlaveAgentValueIsSet == false) { + this->slaveAgentValue = slaveAgentValue; + flagSlaveAgentValueIsSet = true; + flagSlaveAgentValueHasChanged = true; + } + else { + if(this->slaveAgentValue != slaveAgentValue) { + flagSlaveAgentValueHasChanged = true; + flagSlaveAgentValueChangeIsSet = true; + slaveAgentValueChange = slaveAgentValue - + this->slaveAgentValue; this->slaveAgentValue = slaveAgentValue; + } + else { + flagSlaveAgentValueHasChanged = false; + flagSlaveAgentValueChangeIsSet = true; + slaveAgentValueChange = 0; + } + } + */ + // printf("slaveAgentSlot updated with: %f\n", this->slaveAgentValue); +} + +bool SlaveAgentSlotOfAgent ::get_slaveAgentValue(float *slaveAgentValue) { + if (flagSlaveAgentValueIsSet == true) { + *slaveAgentValue = this->slaveAgentValue; + return true; + } + return false; +} + +bool SlaveAgentSlotOfAgent ::get_flagSlaveAgentValueIsSet() { + return flagSlaveAgentValueIsSet; +} + +// TODO: move these functions into -> HistoryHandler +bool SlaveAgentSlotOfAgent::saveValueInHistory() { + if (flagSlaveAgentValueIsSet) { + //try { + // printf("history saving - value: %f\n", slaveAgentValue); + lSlaveAgentHistory.push_back(slaveAgentValue); + return true; + /*} catch (bad_alloc &error) { + printf("bad_alloc caught: %s", error.what()); + }*/ + } + return false; +} +unsigned int SlaveAgentSlotOfAgent::getHistoryLength() { + return lSlaveAgentHistory.size(); +} +bool SlaveAgentSlotOfAgent::deleteOldestHistoryEntry() { + if (!lSlaveAgentHistory.empty()) { + lSlaveAgentHistory.pop_front(); + return true; + } + return false; +} + +unsigned int +SlaveAgentSlotOfAgent::getNumberOfRelativesToActualValue(float threshold) { + unsigned int numberOfRelativesToActualValue = 0; + for (auto &entry : lSlaveAgentHistory) { + if (valueIsRelatedToReferenceValue(slaveAgentValue, entry, threshold)) { + numberOfRelativesToActualValue++; + } + } + return numberOfRelativesToActualValue; +} + +// DATE18 +list SlaveAgentSlotOfAgent::getHistory() { return lSlaveAgentHistory; } + +void SlaveAgentSlotOfAgent::printHistory() { + // printf("History: "); + // for (auto &entry : lSlaveAgentHistory) { + // printf("%f, ", entry); + //} + // printf("\n"); +} + +/* +bool SlaveAgentSlotOfAgent :: get_flagSlaveAgentValueHasChanged() { + return flagSlaveAgentValueHasChanged; +} + +bool SlaveAgentSlotOfAgent :: get_slaveAgentValueChangingRate(float* +slaveAgentValueChangingRate) { if(flagSlaveAgentValueChangeIsSet) { + *slaveAgentValueChangingRate = this->slaveAgentValueChange; + return true; + } + return false; +} + + +bool SlaveAgentSlotOfAgent :: addStateToStatesVector(State* state) { + if (state != NULL) { + try { + vStates.push_back(state); + return true; + } + catch (bad_alloc& error) { + printError("bad_alloc caught: ", error.what()); + delete state; + } + + } + return false; +} + + +vector* SlaveAgentSlotOfAgent :: getStatesVector() { + return &vStates; +} + + +bool SlaveAgentSlotOfAgent :: createNewStateAndMakeItActive() { + State* state = new State(); + if (addStateToStatesVector(state)) { + backupState = activeState; + activeState = state; + return true; + } + return false; +} + +bool SlaveAgentSlotOfAgent :: injectValueInActiveState() { + if (activeState == NULL) { + if (createNewStateAndMakeItActive()) { + return activeState->injectValue(slaveAgentValue); + } + } + return activeState->injectValue(slaveAgentValue); +} + +int SlaveAgentSlotOfAgent :: getIndexOfRelatedState(unsigned int startIndex, +float thresholdToAverage) { if (startIndex >= vStates.size()) return -2; + + int index = 0; + for (vector::iterator state = vStates.begin() + startIndex; +state < vStates.end(); state++, index++) { if +((*state)->valueIsRelated(slaveAgentValue, thresholdToAverage)) { return index; + } + } + + return -1; +} + +bool SlaveAgentSlotOfAgent :: relatedToActiveState(float thresholdToAverage) { + printf("a\n"); + if (activeState == NULL) { + printf("b\n"); + createNewStateAndMakeItActive(); + return true; + } + + return activeState->valueIsRelated(slaveAgentValue, thresholdToAverage); +} + +bool SlaveAgentSlotOfAgent :: valueIsRelated(unsigned int index, float +thresholdToAverage) { return vStates[index]->valueIsRelated(slaveAgentValue, +thresholdToAverage); +} + +State* SlaveAgentSlotOfAgent :: getActiveState() { + return activeState; +} + +void SlaveAgentSlotOfAgent :: deleteActiveState() { + //if(vStates.empty() == false) + vStates.back()->deleteState(); + + vStates.pop_back(); + + if (backupState != NULL) { + activeState = backupState; + backupState = NULL; + } +} + +bool SlaveAgentSlotOfAgent :: setActiveState(unsigned int index) { + if (vStates.size() > index) { + activeState = vStates.at(index); + return true; + } + return false; +} + +unsigned int SlaveAgentSlotOfAgent::getNumberOfStates() { + if (!vStates.empty()) + printf("%u - ", activeState->getNumberOfInjections()); + return vStates.size(); +} +*/ \ No newline at end of file diff --git a/lib/agent/experimental/Slot.cpp b/lib/agent/experimental/Slot.cpp new file mode 100644 index 0000000..3459f48 --- /dev/null +++ b/lib/agent/experimental/Slot.cpp @@ -0,0 +1,20 @@ +#include "rosa/agent/experimental/Slot.hpp" + +#include + + +Slot :: Slot() { + +} + +bool Slot :: set_comPort(Channel* comPort) { + if(comPort != NULL) { + this->comPort = comPort; + return true; + } + return false; +} + +Channel* Slot :: get_comPort() { + return comPort; +} \ No newline at end of file diff --git a/lib/agent/experimental/SlotOfAgent.cpp b/lib/agent/experimental/SlotOfAgent.cpp new file mode 100644 index 0000000..511e856 --- /dev/null +++ b/lib/agent/experimental/SlotOfAgent.cpp @@ -0,0 +1,48 @@ +#include "rosa/agent/experimental/SlotOfAgent.hpp" + +#include + +SlotOfAgent :: SlotOfAgent() { + +} +/* +bool SlotOfAgent :: set_historyModule(HistoryModule* historyModule) { + if(historyModule != NULL) { + this->historyModule = historyModule; + return true; + } + return false; +} + +bool SlotOfAgent :: del_historyModule() { + if(historyModule != NULL) { + historyModule = NULL; + return true; + } + return false; +} + +HistoryModule* SlotOfAgent :: get_historyModule() { + return historyModule; +} + +bool SlotOfAgent :: set_confidenceModule(ConfidenceModule* confidenceModule) { + if(confidenceModule != NULL) { + this->confidenceModule = confidenceModule; + return true; + } + return false; +} + +bool SlotOfAgent :: del_confidenceModule() { + if(confidenceModule != NULL) { + confidenceModule = NULL; + return true; + } + return false; +} + +ConfidenceModule* SlotOfAgent :: get_confidenceModule() { + return confidenceModule; +} +*/ \ No newline at end of file diff --git a/lib/agent/experimental/State.cpp b/lib/agent/experimental/State.cpp new file mode 100644 index 0000000..ba2af99 --- /dev/null +++ b/lib/agent/experimental/State.cpp @@ -0,0 +1,451 @@ +#include "rosa/agent/experimental/State.hpp" + +//#include "printError.h" +#include "rosa/agent/experimental/relationChecker.hpp" + +#include "rosa/agent/experimental/minmaxzeug.hpp" + +#define INJECTIONPARTITIONING 10 + +State::State() { + //discreteAveragePartitionSize = INJECTIONPARTITIONING; + discreteAveragePartitionCounter = 0; + stateIsValid = 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* 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) { + printf("bad_alloc caught: %s", 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; + } + + //printf(" >>> Inject Values (partCounter: %u)\n", discreteAveragePartitionCounter); + //getchar(); + + } + + return flagWorkedForAll; +} + +bool State::injectValuesAndMakeNewDiscreteAveragePartition(unsigned int discreteAveragePartitionSize) { + discreteAveragePartitionCounter = 0; + return injectValues(discreteAveragePartitionSize); +} + + +bool State::variablesAreRelated(vector* 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* 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* 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))); + + //printf("confDrift = %f, deviationValueReferenceValue = %f\n", confidenceDrift, deviationValueReferenceValue(subState->getDiscreteAverageOfLastBlock(discreteAveragePartitionSize), subState->getDiscreteAverageOfFirstBlock(discreteAveragePartitionSize))); + + 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* vSubStates, LinearFunctionBlock* SameState) { + + float confRelatedMin = 1; + + for (auto &subState : *vSubStates) { + + float confRelated = subState->valueIsRelatedFuzzy(SameState); + //printf("conf %f\n", confRelated); + + 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()); + } + + //printf("confValidState %f\nconfInvalidState %f\n", confValidState, confInvalidState); + //getchar(); + + if (confValidState > confInvalidState) { + //printf("VALID STATE\n"); + stateIsValid = true; + return true; + } + + 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()) { + //printf("historyLength: %u\n", vInputSubStates.front()->getSampleHistoryLength()); + + return vInputSubStates.front()->getSampleHistoryLength(); + } + return 0; +} + +bool State::isStateValid() { + return stateIsValid; +} + +float State::getConfStateValid() { + return confValidState; +} + +float State::getConfStateInvalid() { + return confInvalidState; +} + + +//new +float State::getConfVarAreSim2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfSim2StateDev, LinearFunctionBlock* FuncBlockConfSim2StateTime) { + + float lowestConfOfAllVarAreRelated = 1; + + for (auto &subState : *vSubStates) + lowestConfOfAllVarAreRelated = fuzzyAND(lowestConfOfAllVarAreRelated, subState->getConfVarIsSim2State(FuncBlockConfSim2StateDev, FuncBlockConfSim2StateTime)); + + return lowestConfOfAllVarAreRelated; +} + +float State::getConfVarAreDif2State(vector* vSubStates, LinearFunctionBlock* FuncBlockConfDif2StateDev, LinearFunctionBlock* FuncBlockConfDif2StateTime) { + float highestConfOfAllVarAreNotRelated = 0; + + for (auto &subState : *vSubStates) + highestConfOfAllVarAreNotRelated = fuzzyOR(highestConfOfAllVarAreNotRelated, subState->getConfVarIsDif2State(FuncBlockConfDif2StateDev, FuncBlockConfDif2StateTime)); + + return highestConfOfAllVarAreNotRelated; +} + + + + + + + + + + + + + + + + + + +/* +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++; + + 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); +} + +*/ \ No newline at end of file diff --git a/lib/agent/experimental/StateHandler.cpp b/lib/agent/experimental/StateHandler.cpp index 4a0c93e..8decaa6 100644 --- a/lib/agent/experimental/StateHandler.cpp +++ b/lib/agent/experimental/StateHandler.cpp @@ -1,20 +1,1734 @@ -//===-- agent/experimental/StateHandler.cpp ---------------------*- C++ -*-===// -// -// The RoSA Framework -// -//===----------------------------------------------------------------------===// -/// -/// \file agent/experimental/StateHandler.cpp -/// -/// \author Maximilian Götzinger (maxgot@utu.fi) -/// -/// \date 2017 -/// -/// \brief Implementation for rosa/agent/experimental/StateHandler.h. -/// -/// \note Empty implementation, source file here to have a compile database -/// entry for rosa/agent/experimental/StateHandler.h. -/// -//===----------------------------------------------------------------------===// - #include "rosa/agent/experimental/StateHandler.hpp" + +#include +//#include "printError.h" +//#include "rlutil.h" + +#include "rosa/agent/experimental/relationChecker.hpp" + +#include "rosa/agent/experimental/minmaxzeug.hpp" + + +#include +#include + +//CHANGE ALSO BOTH FUZZY FUNCTION!!! +#define MAX_STATE_HISTORY_LENGTH 10 //10 + +//#define STOP_WHEN_BROKEN +//#define STOP_AFTER_BROKEN +//#define STOP_WHEN_DRIFT +//#define STOP_WHEN_STATE_VALID + + + + + + + + + + + + + + + + + + +//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 + +#define INJECTIONPARTITIONING 5 +#define CMPDISTANCE 3 +#define THDRIFT (float)0.08 //0.8 + +#define MINNUMTOBEVALIDSTATE 11 //11 //8 //10 + + + +//using namespace rlutil; + + +void StateHandler::initStateHandler() { + + flagVariablesWereStable = false; + + slidingWindowBufferSize = SLIDINGWINDOWSIZE; + minNumOfRelatedValuesToBeStable = STABLENUMBER; + thresholdToBeStable = STABLETHRESHOLD; + + thresholdToBeRelated = RELATEDTHRESHOLD; + + discreteAveragePartitionSize = INJECTIONPARTITIONING; + compareDistanceDiscreteAveragePartition = CMPDISTANCE; + thresholdNotDrift = THDRIFT; + + minNumToBeValidState = MINNUMTOBEVALIDSTATE; + + activeState = NULL; + + maxStateHistoryLength = MAX_STATE_HISTORY_LENGTH; + + + //time_t rawtime; + //struct tm * timeinfo; + //char output_file_name[200]; + //char datetime[80]; + + //time(&rawtime); + //timeinfo = localtime(&rawtime); + + //strftime(datetime, sizeof(datetime), "%Y-%m-%d_%I-%M-%S", timeinfo); + //sprintf_s(output_file_name, "C:\\csv-data\\output_%s.csv", datetime); + + //XXX - only for now: + + + //csv_writer = new CSV_Writer("CSV Writer", output_file_name); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\output.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\Bearing-DefectWithoutLoad.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\Bearing-DefectWithLoad.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\SpeedChange.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\NormalOperationChangingLoad.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\NormalOperation.csv"); + + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut4pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut5pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut6pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut7pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut8pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut9pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut10pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\WearOut20pc.csv"); + //csv_writer = new CSV_Writer("CSV Writer", "C:\\csv-data\\Wearing-out-HedyehNew.csv"); + + + + + //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; + + + + //printf("jetzt"); + //getchar(); +} + +StateHandler::StateHandler() { + set_name((char*)NO_NAME); + initStateHandler(); +} + +StateHandler::StateHandler(char* name) { + set_name(name); + initStateHandler(); +} + +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* 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) { + printf("bad_alloc caught: %s", error.what()); + }*/ + } + } + return false; +} + +bool StateHandler::addInputVariable(SlaveAgentSlotOfAgent* slot) { + return addVariable(&vInputVariables, slot); +} + +bool StateHandler::addOutputVariable(SlaveAgentSlotOfAgent* slot) { + return addVariable(&vOutputVariables, slot); +} + +bool StateHandler::setSlidingWindowBufferSize(unsigned int slidingWindowBufferSize) { + if (slidingWindowBufferSize >= minNumOfRelatedValuesToBeStable) { + this->slidingWindowBufferSize = slidingWindowBufferSize; + return true; + } + return false; +} + +bool StateHandler::setMinNumOfRelatedValuesToBeStable(unsigned int minNumOfRelatedValuesToBeStable) { + if (minNumOfRelatedValuesToBeStable <= slidingWindowBufferSize) { + this->minNumOfRelatedValuesToBeStable = minNumOfRelatedValuesToBeStable; + return true; + } + return false; +} + +bool StateHandler::setThresholdToBeStable(float thresholdToBeStable) { + if (thresholdToBeStable >= 0 && thresholdToBeStable <= 1) { + this->thresholdToBeStable = thresholdToBeStable; + return true; + } + return false; +} + +bool StateHandler::setThresholdToBeRelated(float thresholdToBeRelated) { + if (thresholdToBeRelated >= 0 && thresholdToBeRelated <= 1) { + this->thresholdToBeRelated = thresholdToBeRelated; + return true; + } + return false; +} + + + + +bool StateHandler::variablesAreStable(vector* 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; +} + +//Sorting with bigger Value in Front +struct descending +{ + template + 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 lHistoryTemporary = variable->getHistory(); + vector vDeviations; + + for (auto &h : lHistoryTemporary) + vDeviations.push_back(deviationValueReferenceValue(sample, h)); + + sort(begin(vDeviations), 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; +} + +//DATE18 +float StateHandler::getConfVariablesAreStable(vector* vVariables) { + float worstConfOfAllVariables = 1; + + for (auto &slot : *vVariables) + worstConfOfAllVariables = minValueOf2Values(worstConfOfAllVariables, getConfVariableIsStable(slot)); + + return worstConfOfAllVariables; +} + +//DATE18 +float StateHandler::getConfVariableIsUnstable(SlaveAgentSlotOfAgent* variable) { + float bestConfOf1Var = 0; + float sample; + if (variable->get_slaveAgentValue(&sample)) { + + list lHistoryTemporary = variable->getHistory(); + vector 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; +} + +//DATE18 - Is there one unstable variable? +float StateHandler::getConfVariablesAreUnstable(vector* vVariables) { + float bestConfOfAllVariables = 0; + + for (auto &slot : *vVariables) + bestConfOfAllVariables = maxValueOf2Values(bestConfOfAllVariables, getConfVariableIsUnstable(slot)); + + return bestConfOfAllVariables; +} + + + +/* +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 lHistoryTemporary = variable->getHistory(); + vector 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; +} +*/ + +/* +bool StateHandler::getConfAndUnconfVariablesAreMatching(vector* 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; +} +*/ + + + + + +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() { + + //printf(" >> Save Active State\n"); + + 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) { + printf("bad_alloc caught: %s", error.what()); + delete activeState; + }*/ + } + return false; +} + +/* +bool StateHandler::addStateAndMakeItActive() { + State* state = addState(); + if (state != NULL) { + activeState = state; + return true; + } + return false; +} +*/ + +bool StateHandler::makeNewActiveState() { + State* state = makeNewState(); + if (state != NULL) { + activeState = state; + return true; + } + return false; +} + + +State* StateHandler::findRelatedState() { + for (auto &state : vStates) { + if (state->inputVariablesAreRelated(thresholdToBeRelated) && state->outputVariablesAreRelated(thresholdToBeRelated)) { + return state; + } + } + return NULL; +} + +bool StateHandler::findRelatedStateAndMakeItActive() { + State* state = findRelatedState(); + if (state != NULL) { + activeState = state; + return true; + } + return false; +} + +void StateHandler::eraseStatesWithLessInjections() { + if (activeState != NULL) { + if (activeState->getNumOfInjections() < minNumToBeValidState) { + activeState = NULL; + } + } + + for (vector::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); + } + } + */ + +} + + + + +//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 printBroken() { + brokenCounter++; + //setColor(TXTCOLOR_LIGHTRED); + printf(" >> BROKEN\n"); + //setColor(TXTCOLOR_GREY); + test = true; +} + +//XXX - only for now +unsigned int old_cycle = 1; + +int brokentest = 0; + +void StateHandler::trigger(unsigned int cycle) { + //printf("cycle: %u\n", cycle); + + + bool flagGotValues = true; + //printf("Input Sample Values:\n"); + for (auto &slot : vInputVariables) { + float sampleValue; + if (!(slot->get_slaveAgentValue(&sampleValue))) + flagGotValues = false; + //printf("In, %s: %f\n", slot->get_comPort()->get_name(), sampleValue); + + if (cycle == 1) + csv_writer->write_field(slot->get_comPort()->get_name()); + else + csv_writer->write_field(sampleValue); + csv_writer->make_new_field(); + } + //printf("Output Sample Values:\n"); + for (auto &slot : vOutputVariables) { + float sampleValue; + if (!(slot->get_slaveAgentValue(&sampleValue))) + flagGotValues = false; + //printf("Out, %s: %f\n", slot->get_comPort()->get_name(), sampleValue); + + if (cycle == 1) + csv_writer->write_field(slot->get_comPort()->get_name()); + else + csv_writer->write_field(sampleValue); + csv_writer->make_new_field(); + } + + if (!flagGotValues) { + csv_writer->write_field((char*)"State Nr"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf State Valid"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf State Invalid"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf Input unchanged"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf Input changed"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf Output unchanged"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Conf Output changed"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"Status"); + csv_writer->make_new_field(); + + csv_writer->write_field((char*)"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; + + //printf(" > new active state\n"); + 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(); + + 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(); + + + csv_writer->write_field(3); //OK + csv_writer->make_new_field(); + + csv_writer->write_field(0); //Status Conf + 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); + + //float confInputIsSteady = confInputVarAreSim2ActiveState - confInputVarAreDif2ActiveState; + //float confOutputIsSteady = confOutputVarAreSim2ActiveState - confOutputVarAreDif2ActiveState; + + //printf("input (sim/dif) %f/%f\noutput (sim/dif) %f/%f\n", confInputVarAreSim2ActiveState, confInputVarAreDif2ActiveState, confOutputVarAreSim2ActiveState, confOutputVarAreDif2ActiveState); + + + //same state + if ((confInputVarAreSim2ActiveState > confInputVarAreDif2ActiveState) && (confOutputVarAreSim2ActiveState > confOutputVarAreDif2ActiveState)) { + brokenCounter = 0; + + //printf(" > same state\n"); + 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) { + //setColor(TXTCOLOR_YELLOW); + //printf("DRIFT\n"); +#ifdef STOP_WHEN_DRIFT + 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(fuzzyAND(confidenceDrift, activeState->getConfStateValid()), fuzzyAND(confInputVarAreSim2ActiveState, confOutputVarAreSim2ActiveState)); + csv_writer->write_field(conf); + + } + else { + //setColor(TXTCOLOR_LIGHTGREEN); + //printf("OK\n"); + //setColor(TXTCOLOR_GREY); + + //print ok + csv_writer->write_field(3); + 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(); + + + + } + //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(); + + //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(); + + + + + + + + brokenCounter++; + //printf("brokenCounter: %u\n", brokenCounter); + + confidenceBroken = FuncBlockConfBrokenSamples->getY((float) brokenCounter); + float confidenceOK = 1 - confidenceBroken; + + if (confidenceBroken > 0.5) { + //setColor(TXTCOLOR_LIGHTRED); + //printf("BROKEN\n"); + //setColor(TXTCOLOR_GREY); +#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(1); + 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(3); + 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(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(3); + 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(); +#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(3); + 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(); + + + + } + + } + + //printf("STATES: %u\n", vStates.size()); + } + } + + csv_writer->make_new_line(); + + if (brokentest) + getchar(); + + + + + + + + + + + + + + + + + + + + + /* + //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); + + + //In- and Outputs are unchanged + if ((confidenceSameStateInput > confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput > confSameStateOutputAdjustableThreshold)) { + + printf(" > same state\n"); + + //inject values + activeState->injectValues(discreteAveragePartitionSize); + //calculate the confidence to have a validState + confidenceValidState = ValidState->getY((float)activeState->getNumOfInjections()); + + //TODO DATE + //check for drifting!!! + //printDrift(); + + + } + //In- and Outputs have changed + else if ((confidenceSameStateInput <= confSameStateInputAdjustableThreshold) && (confidenceSameStateOutput <= confSameStateOutputAdjustableThreshold)) { + + printf(" > change state\n"); + getchar(); + //active state is/was valid + if (confidenceValidState > confValidStateAdjustableThreshold) { + + printf("speicher\n"); + getchar(); + + + 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; + } + + + + //xxx - only for now + //csv_writer->make_new_line(); + */ +} + + +void StateHandler::closeCsvFile() { + if(csv_writer != NULL) + csv_writer->close_file(); +} + + +void StateHandler::setCSVwriter(char* output_file_name) { + csv_writer = new CSV_Writer((char*)"CSV Writer", output_file_name); +} + + + +/* +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::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; +} +*/ \ No newline at end of file diff --git a/lib/agent/experimental/StatisticValue.cpp b/lib/agent/experimental/StatisticValue.cpp new file mode 100644 index 0000000..a42137b --- /dev/null +++ b/lib/agent/experimental/StatisticValue.cpp @@ -0,0 +1,15 @@ +#include "rosa/agent/experimental/StatisticValue.hpp" + +StatisticValue :: StatisticValue() { + +} + +void StatisticValue :: resetStatisticValue() { + resetAverageValue(); + resetExtremeValue(); +} + +void StatisticValue ::injectAndCalculateStatisticValue(float value) { + injectAndCalculateAverageValue(value); + injectAndCalculateExtremeValue(value); +} \ No newline at end of file diff --git a/lib/agent/experimental/SubState.cpp b/lib/agent/experimental/SubState.cpp new file mode 100644 index 0000000..aff7da1 --- /dev/null +++ b/lib/agent/experimental/SubState.cpp @@ -0,0 +1,325 @@ +#include "rosa/agent/experimental/SubState.hpp" + +//#include "printError.h" +#include "rosa/agent/experimental/relationChecker.hpp" + +#include "rosa/agent/experimental/minmaxzeug.hpp" +#include +#include +#include + +SubState::SubState() { + confidenceValidState = 0; + confidenceInvalidState = 1; +} + +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) { + printf("bad_alloc caught: %s", 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); + + //save actual value in history + //try { + lSampleHistory.push_front(sampleValue); + /*} + catch (bad_alloc& error) { + printf("bad_alloc caught: %s", 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())); + + + 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; + if (slot->get_slaveAgentValue(&sampleValue)) { + + vector vDeviations; + + for (auto &h : lSampleHistory) + vDeviations.push_back(deviationValueReferenceValue(sampleValue, h)); + + 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)); + histSampleCounter++; + } + + highestConfOf1Var = fuzzyOR(highestConfOf1Var, fuzzyAND(lowestConfOfSamplesIncluded, FuncBlockConfSim2StateTime->getY((float)histSampleCounter))); + } + } + + return highestConfOf1Var; +} + +//Sorting with bigger Value in Front +struct descending +{ + template + 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 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)); + 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(); +} diff --git a/lib/agent/experimental/Unit.cpp b/lib/agent/experimental/Unit.cpp new file mode 100644 index 0000000..bcdffcc --- /dev/null +++ b/lib/agent/experimental/Unit.cpp @@ -0,0 +1,16 @@ +#include "rosa/agent/experimental/Unit.hpp" + +unsigned int Unit :: num_of_units = 0; + +Unit :: Unit() { + this->id = num_of_units; + num_of_units++; +} + +void Unit :: set_id(unsigned int id) { + this->id = id; +} + +unsigned int Unit :: get_id() { + return this->id; +} diff --git a/lib/agent/experimental/minmaxzeug.cpp b/lib/agent/experimental/minmaxzeug.cpp new file mode 100644 index 0000000..6cce2a3 --- /dev/null +++ b/lib/agent/experimental/minmaxzeug.cpp @@ -0,0 +1,26 @@ +#include "rosa/agent/experimental/minmaxzeug.hpp" + +//DATE18 +float maxValueOf2Values(float value1, float value2) { + if (value1 > value2) + return value1; + else + return value2; +} + +//DATE18 +float minValueOf2Values(float value1, float value2) { + if (value1 < value2) + return value1; + else + return value2; +} + + +float fuzzyOR(float value1, float value2) { + return maxValueOf2Values(value1, value2); +} + +float fuzzyAND(float value1, float value2) { + return minValueOf2Values(value1, value2); +} \ No newline at end of file diff --git a/lib/agent/experimental/relationChecker.cpp b/lib/agent/experimental/relationChecker.cpp new file mode 100644 index 0000000..b66c73c --- /dev/null +++ b/lib/agent/experimental/relationChecker.cpp @@ -0,0 +1,66 @@ +#include "rosa/agent/experimental/relationChecker.hpp" + +#include + +//DATE18 +float deviationValueReferenceValue(float sampleValue, float historyValue) { + + //printf("hier\n"); + + float diff; + + if (sampleValue < 0) { + sampleValue = sampleValue * (-1); + } + + + if (historyValue < 0) { + historyValue = historyValue * (-1); + } + + if (sampleValue > historyValue) + diff = sampleValue - historyValue; + else + diff = historyValue - sampleValue; + + //printf("sample %f, reference %f, diff %f, ", sampleValue, historyValue, diff); + + + if (diff == 0) { + //printf("deviation %f\n", 0); + return 0; + } + else { + //printf("deviation %f\n", (diff / sampleValue)); + return (diff / sampleValue); + } +} + +bool valueIsRelatedToReferenceValue(float referenceValue, float value, float threshold) { + + float diff; + + if (referenceValue > value) + diff = referenceValue - value; + else + diff = value - referenceValue; + + + //printf("referenceValue %f, value %f, diff %f, threshold %f\n", referenceValue, value, diff, threshold); + + if (diff == 0 || (diff / referenceValue <= threshold)) { + return true; + } + + printf("\nRelated Calculation:\nAverage: %f\nActualValue: %f\nDiff: %f\ndeviation: %f\nthreshold: %f\n", referenceValue, value, diff, diff / referenceValue, threshold); + + return false; +} + +bool valueIsRelatedToReferenceValueOrBetweenMinAndMax(float referenceValue, float minimumValue, float maximumValue, float value, float threshold) { + if (value >= minimumValue && value <= maximumValue) { + return true; + } + + return valueIsRelatedToReferenceValue(referenceValue, value, threshold); +} \ No newline at end of file