Page MenuHomePhorge

No OneTemporary

Size
36 KB
Referenced Files
None
Subscribers
None
diff --git a/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp b/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp
index 975b95f..65032f8 100644
--- a/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp
+++ b/examples/agent-functionalities/Reliability-functionality-agent-context/Reliability-agents.cpp
@@ -1,334 +1,334 @@
//===- examples/agent-functionalities/Reliability-functionality.cpp *C++-*-===//
//
// The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file examples/agent-functionalities/Reliability-functionality.cpp
///
/// \author Daniel Schnoell (daniel.schnoell@tuwien.ac.at )
///
/// \date 2019
///
/// \brief A simple example on defining Relianility Functionalities inside a
/// master slave context.
-/// \note This is not finished
+/// \note This is not finished. and is currently inactive
///
//===----------------------------------------------------------------------===//
#define Reliability_trace_level 5
#include "rosa/config/version.h"
#include "rosa/support/log.h"
#include "rosa/agent/CrossReliability.h"
#include "rosa/agent/RangeConfidence.hpp"
#include "rosa/agent/Reliability.h"
#include "rosa/deluxe/DeluxeContext.hpp"
#include <map>
#include <vector>
typedef double SensorValueType;
typedef long StateType;
typedef double ReliabilityType;
#include "./helper.h" // just stuff from Rel-func to increase readability
using namespace rosa::agent;
using namespace rosa;
using namespace rosa::deluxe;
using namespace rosa::terminal;
#define NumberOfSimulationCycles 10
// -------------------------------------------------------------------------------
// Bunch of recusive templates to simplify usage
// -------------------------------------------------------------------------------
template <std::size_t, typename... types> struct conversion;
template <std::size_t size, typename... TypesA, template <typename...> class A,
typename... TypesB, template <typename...> class B>
struct conversion<size, A<TypesA...>, B<TypesB...>> {
using type = typename conversion<size - 1, std::tuple<TypesA...>,
std::tuple<TypesB..., TypesA...>>::type;
};
template <typename... TypesA, template <typename...> class A,
typename... TypesB, template <typename...> class B>
struct conversion<0, A<TypesA...>, B<TypesB...>> {
using type = DeluxeTuple<TypesB...>;
};
template <std::size_t size, typename... Types>
using unrolled_data_type =
typename conversion<size, std::tuple<Types...>, std::tuple<>>::type;
//--------------------------------------------------------------------------------
template <std::size_t max, std::size_t at> struct __convert_to_vector {
void operator()(std::vector<ConfOrRel<StateType, ReliabilityType>> &feedback,
unrolled_data_type<max / 2, StateType, ReliabilityType> I) {
__convert_to_vector<max, at - 2>()(feedback, I);
feedback.push_back({std::get<at>(I), std::get<at + 1>(I)});
}
};
template <std::size_t max> struct __convert_to_vector<max, 0> {
void operator()(std::vector<ConfOrRel<StateType, ReliabilityType>> &feedback,
unrolled_data_type<max / 2, StateType, ReliabilityType> I) {
feedback.push_back({std::get<0>(I), std::get<1>(I)});
}
};
template <std::size_t number>
void convert_to_vector(
std::vector<ConfOrRel<StateType, ReliabilityType>> &feedback,
unrolled_data_type<number, StateType, ReliabilityType> I) {
__convert_to_vector<number * 2, number * 2 - 2>()(feedback, I);
}
//----------------------------------------------------------------------------
// template <std::size_t size, typename... types> struct unrole_vector {};
//
// template <std::size_t size, template <typename...> v, typename... vT,
// template <typename...> class T, typename... TT, typename... types>
// struct unrole_vector<size, v<vT...>,T<TT...>, types...> {
// void operator()(c vec, types... A) {
// unrole_vector<size - 1, v<vT...>, T<TT...>, auto>()(vec,
// vec.at(size).score,
// vec.at(size).score);
// }
//};
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(void) {
const std::size_t number_of_states = 3;
std::unique_ptr<DeluxeContext> C = DeluxeContext::create("Deluxe");
//---------------------- Sensors -------------------------------------
//--------------------------------------------------------------------
const std::string SensorName1 = "Sensor1";
const std::string SensorName2 = "Sensor2";
const std::string SensorName3 = "Sensor3";
AgentHandle Sensor1 = C->createSensor<uint32_t, SensorValueType>(
SensorName1, [&SensorName1](std::pair<uint32_t, bool> I) {
LOG_INFO_STREAM << "\n******\n"
<< SensorName1 << " sensor-input "
<< (I.second ? "<New>" : "<Old>")
<< " value: " << I.first << "\n******\n";
});
AgentHandle Sensor2 = C->createSensor<uint32_t, SensorValueType>(
SensorName2, [&SensorName2](std::pair<uint32_t, bool> I) {
LOG_INFO_STREAM << "\n******\n"
<< SensorName2 << " sensor-input "
<< (I.second ? "<New>" : "<Old>")
<< " value: " << I.first << "\n******\n";
});
AgentHandle Sensor3 = C->createSensor<uint32_t, SensorValueType>(
SensorName3, [&SensorName3](std::pair<uint32_t, bool> I) {
LOG_INFO_STREAM << "\n******\n"
<< SensorName3 << " sensor-input "
<< (I.second ? "<New>" : "<Old>")
<< " value: " << I.first << "\n******\n";
});
//------------------------- lowlevel agents --------------------------------
//--------------------------------------------------------------------------
const std::string LowLevelAgentName1 = "LowLevelAgent1";
const std::string LowLevelAgentName2 = "LowLevelAgent2";
const std::string LowLevelAgentName3 = "LowLevelAgent3";
using conf = unrolled_data_type<
number_of_states, StateType,
ReliabilityType>; // this is the confidence expressed as one tuple it
// uses the format
// (first.state,first.rel,second.sate...)
using LowLevelAgentMasterResult = void; // no return
using LowLevelReturnFromMaster = std::pair<conf, bool>;
using FloatMasterHandler =
std::function<LowLevelAgentMasterResult(LowLevelReturnFromMaster)>;
using FloatResult = Optional<DeluxeTuple<StateType, ReliabilityType>>;
using FloatHandler =
std::function<FloatResult(std::pair<DeluxeTuple<SensorValueType>, bool>)>;
auto lowlevel1 = create_lowlevel_func();
auto lowlevel2 = create_lowlevel_func();
auto lowlevel3 = create_lowlevel_func();
AgentHandle SlaveAgent1 = C->createAgent(
LowLevelAgentName1,
// Master-input handler.
FloatMasterHandler(
[&LowLevelAgentName1,
lowlevel1](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult {
LOG_INFO_STREAM << "inside: " << LowLevelAgentName1 << "feedback\n";
if (I.second) {
std::vector<ConfOrRel<StateType, ReliabilityType>> feedback;
convert_to_vector<number_of_states>(feedback, I.first);
lowlevel1->feedback(feedback);
}
}),
// Slave-input handler.
FloatHandler(
[&LowLevelAgentName1, lowlevel1](
std::pair<DeluxeTuple<SensorValueType>, bool> I) -> FloatResult {
LOG_INFO_STREAM
<< "\n******\n"
<< LowLevelAgentName1 << " " << (I.second ? "<New>" : "<Old>")
<< " value: " << std::get<0>(I.first) << "\n******\n";
auto tmp = lowlevel1->operator()(std::get<0>(I.first));
DeluxeTuple<long, double> ret(tmp.score, tmp.Reliability);
return {ret};
}));
AgentHandle SlaveAgent2 = C->createAgent(
LowLevelAgentName2,
// Master-input handler.
FloatMasterHandler(
[&LowLevelAgentName2,
lowlevel2](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult {
LOG_INFO_STREAM << "inside: " << LowLevelAgentName2 << "feedback\n";
if (I.second) {
std::vector<ConfOrRel<StateType, ReliabilityType>> feedback;
convert_to_vector<number_of_states>(feedback, I.first);
lowlevel2->feedback(feedback);
}
}),
// Slave-input handler.
FloatHandler(
[&LowLevelAgentName2, lowlevel2](
std::pair<DeluxeTuple<SensorValueType>, bool> I) -> FloatResult {
LOG_INFO_STREAM
<< "\n******\n"
<< LowLevelAgentName2 << " " << (I.second ? "<New>" : "<Old>")
<< " value: " << std::get<0>(I.first) << "\n******\n";
auto tmp = lowlevel2->operator()(std::get<0>(I.first));
DeluxeTuple<long, double> ret(tmp.score, tmp.Reliability);
return {ret};
}));
AgentHandle SlaveAgent3 = C->createAgent(
LowLevelAgentName3,
// Master-input handler.
FloatMasterHandler(
[&LowLevelAgentName3,
lowlevel3](LowLevelReturnFromMaster I) -> LowLevelAgentMasterResult {
LOG_INFO_STREAM << "inside: " << LowLevelAgentName3 << "feedback\n";
if (I.second) {
std::vector<ConfOrRel<StateType, ReliabilityType>> feedback;
convert_to_vector<number_of_states>(feedback, I.first);
lowlevel3->feedback(feedback);
}
}),
// Slave-input handler.
FloatHandler(
[&LowLevelAgentName3, lowlevel3](
std::pair<DeluxeTuple<SensorValueType>, bool> I) -> FloatResult {
LOG_INFO_STREAM
<< "\n******\n"
<< LowLevelAgentName3 << " " << (I.second ? "<New>" : "<Old>")
<< " value: " << std::get<0>(I.first) << "\n******\n";
auto tmp = lowlevel3->operator()(std::get<0>(I.first));
DeluxeTuple<long, double> ret(tmp.score, tmp.Reliability);
return {ret};
}));
//------------------------- high level rel ---------------------------------
//--------------------------------------------------------------------------
auto highlevel = create_highlevel_func();
using conf1 = Optional<unrolled_data_type<
number_of_states, StateType,
ReliabilityType>>; // this is the confidence expressed as one tuple it
// uses the format
// (first.state,first.rel,second.sate...)
using MasterResult =
std::tuple<Optional<DeluxeTuple<ReliabilityType>>, conf1, conf1, conf1>;
using SlaveOutputs = DeluxeTuple<StateType, ReliabilityType>;
using MasterHandler = std::function<MasterResult(
std::pair<SlaveOutputs, bool>, std::pair<SlaveOutputs, bool>,
std::pair<SlaveOutputs, bool>)>;
AgentHandle MasterAgent = C->createAgent(
"Master Agent",
MasterHandler([&](std::pair<SlaveOutputs, bool> I0,
std::pair<SlaveOutputs, bool> I1,
std::pair<SlaveOutputs, bool> I2) -> MasterResult {
ReliabilityForHighLevelAgents<StateType, ReliabilityType>::InputType
input;
input.push_back({0, std::get<0>(I0.first), std::get<1>(I0.first)});
input.push_back({1, std::get<0>(I1.first), std::get<1>(I1.first)});
input.push_back({2, std::get<0>(I2.first), std::get<1>(I2.first)});
auto out = highlevel->operator()(input);
DeluxeTuple<ReliabilityType> rel(out.CrossReliability);
conf c[3];
for (std::size_t at = 0; at < 3; at++) {
std::get<0>(c[at]) = out.CrossConfidence.at(at).at(0).score;
std::get<1>(c[at]) = out.CrossConfidence.at(at).at(0).Reliability;
std::get<2>(c[at]) = out.CrossConfidence.at(at).at(1).score;
std::get<3>(c[at]) = out.CrossConfidence.at(at).at(1).Reliability;
std::get<4>(c[at]) = out.CrossConfidence.at(at).at(2).score;
std::get<5>(c[at]) = out.CrossConfidence.at(at).at(2).Reliability;
}
return {{rel}, {c[0]}, {c[1]}, {c[2]}};
}));
// -------------------------------------------------------------------------
// Sensors and connecting
// -------------------------------------------------------------------------
std::vector<SensorValueType> FloatValues(NumberOfSimulationCycles);
std::generate(FloatValues.begin(), FloatValues.end(),
[f = 0.5f](void) mutable {
f += 0.3f;
return std::floor(f) + 0.5f;
});
C->registerSensorValues(Sensor1, FloatValues.begin(), FloatValues.end());
C->registerSensorValues(Sensor2, FloatValues.begin(), FloatValues.end());
C->registerSensorValues(Sensor3, FloatValues.begin(), FloatValues.end());
// Connection
C->connectSensor(SlaveAgent1, 0, Sensor1, "Sensor 1");
C->connectSensor(SlaveAgent2, 0, Sensor2, "Sensor 2");
C->connectSensor(SlaveAgent3, 0, Sensor3, "Sensor 3");
C->connectAgents(MasterAgent, 0, SlaveAgent1, "Slave1");
C->connectAgents(MasterAgent, 1, SlaveAgent2, "Slave2");
C->connectAgents(MasterAgent, 2, SlaveAgent3, "Slave3");
// Logger
AgentHandle LoggerAgent = C->createAgent(
"Logger Agent", std::function<Optional<DeluxeTuple<unit_t>>(
std::pair<DeluxeTuple<ReliabilityType>, bool>)>(
[](std::pair<DeluxeTuple<ReliabilityType>, bool> Sum)
-> Optional<DeluxeTuple<unit_t>> {
if (Sum.second) {
LOG_INFO_STREAM
<< "Result: Rel: " << std::get<0>(Sum.first)
<< "\n";
}
return {};
}));
C->connectAgents(LoggerAgent, 0, MasterAgent, "Sum Agent Channel");
// -------------------------------------------------------------------------------
// Simulate
// -------------------------------------------------------------------------------
C->simulate(NumberOfSimulationCycles);
delete highlevel;
delete lowlevel1;
delete lowlevel2;
}
\ No newline at end of file
diff --git a/include/rosa/agent/CrossCombinator.h b/include/rosa/agent/CrossCombinator.h
index 05b33f5..eafb2d0 100644
--- a/include/rosa/agent/CrossCombinator.h
+++ b/include/rosa/agent/CrossCombinator.h
@@ -1,540 +1,551 @@
//===-- rosa/delux/CrossCombinator.h ----------------------------*- C++ -*-===//
//
// The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file rosa/agent/CrossCombinator.h
///
/// \author Daniel Schnoell
///
/// \date 2019
/// \note based on Maximilian Goetzinger(maxgot @utu.fi) code in
/// CAM_Dirty_include SA-EWS2_Version... inside Agent.cpp
///
/// \brief
///
/// \todo there is 1 exception that needs to be handled correctly.
/// \note the default search function is extremely slow maybe this could be done
/// via template for storage class and the functions/methods to efficiently find
/// the correct LinearFunction
//===----------------------------------------------------------------------===//
#ifndef ROSA_AGENT_CROSSCOMBINATOR_H
#define ROSA_AGENT_CROSSCOMBINATOR_H
#include "rosa/agent/Abstraction.hpp"
#include "rosa/agent/Functionality.h"
#include "rosa/agent/ReliabilityConfidenceCombinator.h"
#include "rosa/core/forward_declarations.h" // needed for id_t
#include "rosa/support/log.h" // needed for error "handling"
// nedded headers
#include <string>
#include <type_traits> //assert
#include <vector>
// for static methods
#include <algorithm>
#include <numeric>
namespace rosa {
namespace agent {
template <typename id, typename IdentifierType, typename ReliabilityType>
std::vector<std::pair<id_t, IdentifierType>> &operator<<(
std::vector<std::pair<id_t, IdentifierType>> &me,
std::vector<std::tuple<id, IdentifierType, ReliabilityType>> Values) {
for (auto tmp : Values) {
std::pair<id, IdentifierType> tmp2;
tmp2.first = std::get<0>(tmp);
tmp2.second = std::get<1>(tmp);
me.push_back(tmp2);
}
return me;
}
/// This is the Combinator class for cross Reliabilities. It has many functions
/// with different purposes
/// \brief It takes the Identifiers and Reliabilities of all given ids and
/// calculates the Reliability of them together. Also it can creates the
/// feedback that is needed by the \c ReliabilityAndConfidenceCombinator, which
/// is a kind of confidence.
///
/// \tparam IdentifierType Data type of the Identifier ( Typically double
/// or float) \tparam ReliabilityType Data type of the Reliability ( Typically
/// long or int)
///
/// \note This class is commonly in a master slave relationship as master with
/// \c ReliabilityAndConfidenceCombinator. The \c operator()() combines the
/// Reliability of all connected Slaves and uses that as its own Reliability
/// also creates the feedback for the Slaves.
///
/// \note more information about how the Reliability and feedback is
/// created at \c operator()() , \c getCombinedCrossReliability() , \c
/// getCombinedInputReliability() , \c getOutputReliability() [ this is the
/// used Reliability ], \c getCrossConfidence() [ this is the feedback
/// for all Slaves ]
///
/// a bit more special Methods \c CrossConfidence() ,\c CrossReliability()
template <typename IdentifierType, typename ReliabilityType>
class CrossCombinator {
public:
static_assert(std::is_arithmetic<IdentifierType>::value,
"HighLevel: IdentifierType has to be an arithmetic type\n");
static_assert(std::is_arithmetic<ReliabilityType>::value,
"HighLevel: ReliabilityType has to be an arithmetic type\n");
// ---------------------------------------------------------------------------
// useful definitions
// ---------------------------------------------------------------------------
/// typedef To shorten the writing.
/// \c ConfOrRel
using ConfOrRel = ConfOrRel<IdentifierType, ReliabilityType>;
/// To shorten the writing.
using Abstraction =
typename rosa::agent::Abstraction<IdentifierType, ReliabilityType>;
/// The return type for the \c operator()() Method
struct returnType {
ReliabilityType CrossReliability;
std::map<id_t, std::vector<ConfOrRel>> CrossConfidence;
};
// -------------------------------------------------------------------------
// Relevant Methods
// -------------------------------------------------------------------------
/// Calculates the Reliability and the CrossConfidences for each id for all
/// of there Identifiers.
///
/// \param Values It gets the Identifiers and Reliabilities of
/// all connected Slaves inside a vector.
///
/// \return it returns a struct \c returnType containing the \c
/// getCombinedCrossReliability() and \c getCrossConfidence()
returnType operator()(
std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>> Values) {
return {getOutputReliability(Values), getCrossConfidence(Values)};
}
/// returns the combined Cross Reliability via \c
/// CombinedCrossRelCombinationMethod \c
/// setCombinedCrossRelCombinationMethod() for all ids \c
/// CrossReliability() \param Values the used Values
ReliabilityType getCombinedCrossReliability(
- std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>> Values) {
+ const std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>>
+ &Values) noexcept {
ReliabilityType combinedCrossRel = -1;
std::vector<std::pair<id_t, IdentifierType>> Agents;
Agents << Values;
for (auto Value : Values) {
id_t id = std::get<0>(Value);
IdentifierType sc = std::get<1>(Value);
// calculate the cross reliability for this slave agent
ReliabilityType realCrossReliabilityOfSlaveAgent =
CrossReliability({id, sc}, Agents);
if (combinedCrossRel != -1)
combinedCrossRel = CombinedCrossRelCombinationMethod(
combinedCrossRel, realCrossReliabilityOfSlaveAgent);
else
combinedCrossRel = realCrossReliabilityOfSlaveAgent;
}
return combinedCrossRel;
}
/// returns the combined via \c CombinedInputRelCombinationMethod \c
/// setCombinedInputRelCombinationMethod() input reliability \param Values
/// the used Values
ReliabilityType getCombinedInputReliability(
- std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>> Values) {
+ const std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>>
+ &Values) noexcept {
ReliabilityType combinedInputRel = -1;
std::vector<std::pair<id_t, IdentifierType>> Agents;
Agents << Values;
for (auto Value : Values) {
ReliabilityType rel = std::get<2>(Value);
if (combinedInputRel != -1)
combinedInputRel =
CombinedInputRelCombinationMethod(combinedInputRel, rel);
else
combinedInputRel = rel;
}
return combinedInputRel;
}
/// returns the combination via \c OutputReliabilityCombinationMethod \c
/// setOutputReliabilityCombinationMethod() of the Cross reliability and
/// input reliability \param Values the used Values
ReliabilityType getOutputReliability(
- std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>> Values) {
+ const std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>>
+ &Values) noexcept {
return OutputReliabilityCombinationMethod(
getCombinedInputReliability(Values),
getCombinedCrossReliability(Values));
}
/// returns the crossConfidence for all ids \c CrossConfidence()
/// \param Values the used Values
std::map<id_t, std::vector<ConfOrRel>> getCrossConfidence(
- std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>> Values) {
+ const std::vector<std::tuple<id_t, IdentifierType, ReliabilityType>>
+ &Values) noexcept {
std::vector<std::pair<id_t, IdentifierType>> Agents;
std::map<id_t, std::vector<ConfOrRel>> output;
std::vector<ConfOrRel> output_temporary;
Agents << Values;
for (auto Value : Values) {
id_t id = std::get<0>(Value);
output_temporary.clear();
for (IdentifierType thoIdentifier : Identifiers[id]) {
ConfOrRel data;
data.Identifier = thoIdentifier;
data.Reliability = CrossConfidence(id, thoIdentifier, Agents);
output_temporary.push_back(data);
}
output.insert({id, output_temporary});
}
return output;
}
/// Calculates the Cross Confidence
/// \brief it uses the Identifier value and calculates
/// the Confidence of a given agent( represented by their id ) for a given
/// Identifiers in connection to all other given agents
///
/// \note all combination of agents and there corresponding Cross Reliability
/// function have to be specified
ReliabilityType
- CrossConfidence(id_t MainAgent, IdentifierType TheoreticalValue,
- std::vector<std::pair<id_t, IdentifierType>> &SlaveAgents) {
+ CrossConfidence(const id_t &MainAgent, const IdentifierType &TheoreticalValue,
+ const std::vector<std::pair<id_t, IdentifierType>>
+ &SlaveAgents) noexcept {
ReliabilityType crossReliabiability;
std::vector<ReliabilityType> values;
for (std::pair<id_t, IdentifierType> SlaveAgent : SlaveAgents) {
if (SlaveAgent.first == MainAgent)
continue;
if (TheoreticalValue == SlaveAgent.second)
crossReliabiability = 1;
else
crossReliabiability =
1 / (crossReliabilityParameter *
std::abs(TheoreticalValue - SlaveAgent.second));
// profile reliability
ReliabilityType crossReliabilityFromProfile =
getCrossReliabilityFromProfile(
MainAgent, SlaveAgent.first,
std::abs(TheoreticalValue - SlaveAgent.second));
values.push_back(
std::max(crossReliabiability, crossReliabilityFromProfile));
}
return Method(values);
}
/// Calculates the Cross Reliability
/// \brief it uses the Identifier value and calculates
/// the Reliability of a given agent( represented by their id ) in connection
/// to all other given agents
///
/// \note all combination of agents and there corresponding Cross Reliability
/// function have to be specified
ReliabilityType
- CrossReliability(std::pair<id_t, IdentifierType> &&MainAgent,
- std::vector<std::pair<id_t, IdentifierType>> &SlaveAgents) {
+ CrossReliability(const std::pair<id_t, IdentifierType> &MainAgent,
+ const std::vector<std::pair<id_t, IdentifierType>>
+ &SlaveAgents) noexcept {
ReliabilityType crossReliabiability;
std::vector<ReliabilityType> values;
for (std::pair<id_t, IdentifierType> SlaveAgent : SlaveAgents) {
if (SlaveAgent.first == MainAgent.first)
continue;
if (MainAgent.second == SlaveAgent.second)
crossReliabiability = 1;
else
crossReliabiability =
1 / (crossReliabilityParameter *
std::abs(MainAgent.second - SlaveAgent.second));
// profile reliability
ReliabilityType crossReliabilityFromProfile =
getCrossReliabilityFromProfile(
MainAgent.first, SlaveAgent.first,
std::abs(MainAgent.second - SlaveAgent.second));
values.push_back(
std::max(crossReliabiability, crossReliabilityFromProfile));
}
return Method(values);
}
// --------------------------------------------------------------------------
// Defining the class
// --------------------------------------------------------------------------
/// adds a Cross Reliability Profile used to get the Reliability of the
/// Identifier difference
///
/// \param idA The id of the one \c Agent ( ideally the id of \c Unit to make
/// it absolutely unique )
///
/// \param idB The id of the other \c Agent
///
/// \param Function A shared pointer to an \c Abstraction it would use the
/// difference in Identifier for its input
- void addCrossReliabilityProfile(id_t idA, id_t idB,
- std::shared_ptr<Abstraction> &Function) {
+ void addCrossReliabilityProfile(
+ const id_t &idA, const id_t &idB,
+ const std::shared_ptr<Abstraction> &Function) noexcept {
Functions.push_back({true, idA, idB, Function});
}
/// sets the cross reliability parameter
- void setCrossReliabilityParameter(ReliabilityType val) {
+ void setCrossReliabilityParameter(const ReliabilityType &val) noexcept {
crossReliabilityParameter = val;
}
/// This is the adder for the Identifiers
/// \param id The id of the Agent of the Identifiers
/// \param Identifiers id specific Identifiers. This will be copied So that if
/// Slaves have different Identifiers they can be used correctly. \brief The
/// Identifiers of all connected slave Agents has to be known to be able to
/// iterate over them
- void addIdentifiers(id_t id, std::vector<IdentifierType> Identifiers) {
+ void addIdentifiers(const id_t &id,
+ const std::vector<IdentifierType> &Identifiers) noexcept {
this->Identifiers.insert({id, Identifiers});
}
// -------------------------------------------------------------------------
// Combinator Settings
// -------------------------------------------------------------------------
/// sets the used method to combine the values
/// \param Meth the method which should be used. predefined functions in the
/// struct \c predefinedMethods \c
/// CONJUNCTION() \c AVERAGE() \c DISJUNCTION()
void setCrossReliabilityCombinatorMethod(
- std::function<ReliabilityType(std::vector<ReliabilityType> values)>
- Meth) {
+ const std::function<ReliabilityType(std::vector<ReliabilityType> values)>
+ &Meth) noexcept {
Method = Meth;
}
/// sets the combination method for the combined cross reliability
/// \param Meth the method which should be used. predefined functions in the
/// struct \c predefinedMethods CombinedCrossRelCombinationMethod<method>()
void setCombinedCrossRelCombinationMethod(
- std::function<ReliabilityType(ReliabilityType, ReliabilityType)> Meth) {
+ const std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
+ &Meth) noexcept {
CombinedCrossRelCombinationMethod = Meth;
}
/// sets the combined input rel method
/// \param Meth the method which should be used. predefined functions in the
/// struct \c predefinedMethods CombinedInputRelCombinationMethod<method>()
void setCombinedInputRelCombinationMethod(
- std::function<ReliabilityType(ReliabilityType, ReliabilityType)> Meth) {
+ const std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
+ &Meth) noexcept {
CombinedInputRelCombinationMethod = Meth;
}
/// sets the used OutputReliabilityCombinationMethod
/// \param Meth the method which should be used. predefined functions in the
/// struct \c predefinedMethods OutputReliabilityCombinationMethod<method>()
void setOutputReliabilityCombinationMethod(
- std::function<ReliabilityType(ReliabilityType, ReliabilityType)> Meth) {
+ const std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
+ &Meth) noexcept {
OutputReliabilityCombinationMethod = Meth;
}
// -------------------------------------------------------------------------
// Predefined Functions
// -------------------------------------------------------------------------
/// This struct is a pseudo name space to have easier access to all predefined
/// methods while still not overcrowding the class it self
struct predefinedMethods {
/// predefined combination method
static ReliabilityType CONJUNCTION(std::vector<ReliabilityType> values) {
return *std::min_element(values.begin(), values.end());
}
/// predefined combination method
static ReliabilityType AVERAGE(std::vector<ReliabilityType> values) {
return std::accumulate(values.begin(), values.end(), 0.0) / values.size();
}
/// predefined combination method
static ReliabilityType DISJUNCTION(std::vector<ReliabilityType> values) {
return *std::max_element(values.begin(), values.end());
}
/// predefined combination Method
static ReliabilityType
CombinedCrossRelCombinationMethodMin(ReliabilityType A, ReliabilityType B) {
return std::min(A, B);
}
/// predefined combination Method
static ReliabilityType
CombinedCrossRelCombinationMethodMax(ReliabilityType A, ReliabilityType B) {
return std::max(A, B);
}
/// predefined combination Method
static ReliabilityType
CombinedCrossRelCombinationMethodMult(ReliabilityType A,
ReliabilityType B) {
return A * B;
}
/// predefined combination Method
static ReliabilityType
CombinedCrossRelCombinationMethodAverage(ReliabilityType A,
ReliabilityType B) {
return (A + B) / 2;
}
/// predefined combination Method
static ReliabilityType
CombinedInputRelCombinationMethodMin(ReliabilityType A, ReliabilityType B) {
return std::min(A, B);
}
/// predefined combination Method
static ReliabilityType
CombinedInputRelCombinationMethodMax(ReliabilityType A, ReliabilityType B) {
return std::max(A, B);
}
/// predefined combination Method
static ReliabilityType
CombinedInputRelCombinationMethodMult(ReliabilityType A,
ReliabilityType B) {
return A * B;
}
/// predefined combination Method
static ReliabilityType
CombinedInputRelCombinationMethodAverage(ReliabilityType A,
ReliabilityType B) {
return (A + B) / 2;
}
/// predefined combination method
static ReliabilityType
OutputReliabilityCombinationMethodMin(ReliabilityType A,
ReliabilityType B) {
return std::min(A, B);
}
/// predefined combination method
static ReliabilityType
OutputReliabilityCombinationMethodMax(ReliabilityType A,
ReliabilityType B) {
return std::max(A, B);
}
/// predefined combination method
static ReliabilityType
OutputReliabilityCombinationMethodMult(ReliabilityType A,
ReliabilityType B) {
return A * B;
}
/// predefined combination method
static ReliabilityType
OutputReliabilityCombinationMethodAverage(ReliabilityType A,
ReliabilityType B) {
return (A + B) / 2;
}
};
// -------------------------------------------------------------------------
// Cleanup
// -------------------------------------------------------------------------
~CrossCombinator() { Functions.clear(); }
// --------------------------------------------------------------------------
// Parameters
// --------------------------------------------------------------------------
private:
struct Functionblock {
bool exists = false;
id_t A;
id_t B;
std::shared_ptr<Abstraction> Funct;
};
std::map<id_t, std::vector<IdentifierType>> Identifiers;
/// From Maxi in his code defined as 1 can be changed by set
ReliabilityType crossReliabilityParameter = 1;
/// Stored Cross Reliability Functions
std::vector<Functionblock> Functions;
/// Method which is used to combine the generated values
std::function<ReliabilityType(std::vector<ReliabilityType>)> Method =
predefinedMethods::AVERAGE;
std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
CombinedCrossRelCombinationMethod =
predefinedMethods::CombinedCrossRelCombinationMethodMin;
std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
CombinedInputRelCombinationMethod =
predefinedMethods::CombinedInputRelCombinationMethodMin;
std::function<ReliabilityType(ReliabilityType, ReliabilityType)>
OutputReliabilityCombinationMethod =
predefinedMethods::OutputReliabilityCombinationMethodMin;
//--------------------------------------------------------------------------------
// helper function
/// very inefficient searchFunction
Functionblock (*searchFunction)(std::vector<Functionblock> vect,
const id_t nameA, const id_t nameB) =
[](std::vector<Functionblock> vect, const id_t nameA,
const id_t nameB) -> Functionblock {
for (Functionblock tmp : vect) {
if (tmp.A == nameA && tmp.B == nameB)
return tmp;
if (tmp.A == nameB && tmp.B == nameA)
return tmp;
}
return Functionblock();
};
/// evaluates the corresponding LinearFunction with the Identifier difference
/// \param nameA these two parameters are the unique identifiers
/// \param nameB these two parameters are the unique identifiers
/// for the LinerFunction
///
/// \note it doesn't matter if they are swapped
- ReliabilityType
- getCrossReliabilityFromProfile(id_t nameA, id_t nameB,
- IdentifierType IdentifierDifference) {
+ ReliabilityType getCrossReliabilityFromProfile(
+ const id_t &nameA, const id_t &nameB,
+ const IdentifierType &IdentifierDifference) noexcept {
Functionblock block = searchFunction(Functions, nameA, nameB);
if (!block.exists) {
LOG_ERROR(("CrossReliability: Block:" + std::to_string(nameA) + "," +
std::to_string(nameB) + "doesn't exist returning 0"));
return 0;
}
return block.Funct->operator()(IdentifierDifference);
}
};
} // End namespace agent
} // End namespace rosa
#endif // ROSA_AGENT_CROSSCOMBINATOR_H
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Sun, Apr 27, 2:46 PM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
134409
Default Alt Text
(36 KB)

Event Timeline