Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F1498263
Reliability-agents.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Size
12 KB
Referenced Files
None
Subscribers
None
Reliability-agents.cpp
View Options
//===- 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.
///
//===----------------------------------------------------------------------===//
#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
;
/// Helper structs for conversion
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
.
first
),
std
::
get
<
at
+
1
>
(
I
.
first
)});
}
}
;
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
.
first
),
std
::
get
<
1
>
(
I
.
first
)});
}
};
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
);
}
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
<<
" master-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
<<
" master-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
<<
" master-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
SlaveAgent
=
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
};
}));
//------------------------- lookup copy of rel------------------------------
//--------------------------------------------------------------------------
std
::
cout
<<
"---------------------------------------------------------------"
"---------------------------------
\n
"
;
std
::
cout
<<
"------------------------------------High level "
"Test---------------------------------------------
\n
"
;
std
::
cout
<<
"Configured in a way that the Master thinks that both Sensors "
"should have the same State.
\n
While feeding both the
\"
opposite
\"
"
"values one acending the other decending from the maximum.
\n
"
;
ReliabilityForHighLevelAgents
<
StateType
,
ReliabilityType
>
*
highlevel
=
new
ReliabilityForHighLevelAgents
<
StateType
,
ReliabilityType
>
();
std
::
unique_ptr
<
CrossReliability
<
StateType
,
ReliabilityType
>>
CrossReliability1
(
new
CrossReliability
<
StateType
,
ReliabilityType
>
());
std
::
unique_ptr
<
Abstraction
<
long
,
double
>>
func1
(
new
PartialFunction
<
long
,
double
>
(
{
{{
0
,
1
},
std
::
make_shared
<
LinearFunction
<
long
,
double
>>
(
1
,
0
)},
{{
1
,
2
},
std
::
make_shared
<
LinearFunction
<
long
,
double
>>
(
2
,
-1.0
)},
},
0
));
CrossReliability1
->
addCrossReliabilityProfile
(
0
,
1
,
func1
);
CrossReliability1
->
setCrossReliabilityMethod
(
CrossReliability
<
StateType
,
ReliabilityType
>::
AVERAGE
);
CrossReliability1
->
setCrossReliabilityParameter
(
1
);
std
::
unique_ptr
<
CrossConfidence
<
StateType
,
ReliabilityType
>>
CrossConfidence1
(
new
CrossConfidence
<
StateType
,
ReliabilityType
>
());
std
::
unique_ptr
<
Abstraction
<
long
,
double
>>
func2
(
new
PartialFunction
<
long
,
double
>
(
{
{{
0
,
1
},
std
::
make_shared
<
LinearFunction
<
long
,
double
>>
(
1
,
0
)},
{{
1
,
2
},
std
::
make_shared
<
LinearFunction
<
long
,
double
>>
(
2
,
-1.0
)},
},
0
));
CrossConfidence1
->
addCrossReliabilityProfile
(
0
,
1
,
func2
);
CrossConfidence1
->
setCrossReliabilityMethod
(
CrossConfidence
<
StateType
,
ReliabilityType
>::
AVERAGE
);
CrossConfidence1
->
setCrossReliabilityParameter
(
1
);
highlevel
->
setCrossConfidence
(
CrossConfidence1
);
highlevel
->
setCrossReliability
(
CrossReliability1
);
highlevel
->
addStates
(
0
,
states
);
highlevel
->
addStates
(
1
,
states
);
for
(
int
a
=
0
;
a
<
21
;
a
++
)
{
auto
out1
=
lowlevel
->
operator
()(
a
),
out2
=
lowlevel2
->
operator
()((
int
)
21
-
a
);
std
::
cout
<<
"s1: "
<<
out1
<<
"
\n
s2:"
<<
out2
<<
"
\n
"
;
std
::
vector
<
std
::
tuple
<
rosa
::
id_t
,
StateType
,
ReliabilityType
>>
tmp2
;
tmp2
.
push_back
({
0
,
out1
.
score
,
out1
.
Reliability
});
tmp2
.
push_back
({
1
,
out2
.
score
,
out2
.
Reliability
});
auto
out_o
=
highlevel
->
operator
()(
tmp2
);
std
::
cout
<<
"it: "
<<
a
<<
"
\t
rel: "
<<
out_o
.
CrossReliability
<<
"
\n
"
;
std
::
cout
<<
"
\t
subs:
\n
"
;
for
(
auto
q
:
out_o
.
CrossConfidence
)
{
std
::
cout
<<
"
\t\t
id:"
<<
q
.
first
<<
"
\n
"
;
/*
for(auto z: q.second)
{
std::cout << "\t\t\t score: " << z.score << "\tRel: " << z.Reliability
<< "\n"; tmp.push_back({z.score,z.Reliability});
}
*/
for
(
auto
z
:
q
.
second
)
{
std
::
cout
<<
"
\t\t\t
score: "
<<
z
.
score
<<
"
\t
Rel: "
<<
z
.
Reliability
<<
"
\n
"
;
}
if
(
q
.
first
==
0
)
lowlevel
->
feedback
(
q
.
second
);
else
lowlevel2
->
feedback
(
q
.
second
);
}
}
delete
lowlevel1
;
delete
lowlevel2
;
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Sun, Mar 1, 10:25 PM (22 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
287468
Default Alt Text
Reliability-agents.cpp (12 KB)
Attached To
Mode
R20 SoC_Rosa_repo
Attached
Detach File
Event Timeline
Log In to Comment