Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 63f7a94

Browse files
LongShortTermVoting
1 parent 874426c commit 63f7a94

File tree

4 files changed

+219
-11
lines changed

4 files changed

+219
-11
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <ArduinoUnit.h>
2+
#include <EloquentArduino.h>
3+
#include <eloquentarduino/data_structures/LongShortTermVoting.h>
4+
5+
6+
using namespace Eloquent::DataStructures;
7+
8+
9+
test(not_enough_votes) {
10+
LongShortTermVoting<3, 3> voting(2, 2);
11+
12+
voting.vote(1);
13+
14+
assertFalse(voting.agree());
15+
}
16+
17+
test(no_short_term_quorum) {
18+
LongShortTermVoting<3, 3> voting(2, 2);
19+
20+
voting.vote(1);
21+
voting.vote(2);
22+
voting.vote(3);
23+
24+
assertFalse(voting.agree());
25+
}
26+
27+
test(no_long_term_quorum) {
28+
LongShortTermVoting<3, 3> voting(2, 2);
29+
30+
voting.vote(1);
31+
voting.vote(1);
32+
voting.vote(2);
33+
34+
assertFalse(voting.agree());
35+
}
36+
37+
test(quorum) {
38+
LongShortTermVoting<3, 3> voting(2, 2);
39+
40+
// 1st long term vote
41+
voting.vote(1);
42+
voting.vote(1);
43+
voting.vote(2);
44+
// 2nd long term vote
45+
// now votes become [1, 2, 1]
46+
voting.vote(1);
47+
// 3rd long term voting
48+
// now votes become [2, 1, 1]
49+
voting.vote(1);
50+
51+
assertTrue(voting.agree());
52+
assertEqual(voting.decision(), 1);
53+
}
54+
55+
56+
void setup() {
57+
Serial.begin(115200);
58+
}
59+
60+
61+
void loop() {
62+
Test::run();
63+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <ArduinoUnit.h>
2+
#include <EloquentArduino.h>
3+
#include <eloquentarduino/data_structures/Voting.h>
4+
5+
6+
using namespace Eloquent::DataStructures;
7+
8+
9+
test(not_enough_votes) {
10+
Voting<5> voting;
11+
12+
voting.vote(1);
13+
14+
assertFalse(voting.agree(0.5));
15+
}
16+
17+
test(no_quorum) {
18+
Voting<3> voting;
19+
20+
voting.vote(1);
21+
voting.vote(2);
22+
voting.vote(3);
23+
24+
assertFalse(voting.agree(2));
25+
assertFalse(voting.agree(0.5));
26+
}
27+
28+
test(absolute_quorum) {
29+
Voting<3> voting;
30+
31+
voting.vote(1);
32+
voting.vote(1);
33+
voting.vote(2);
34+
35+
assertTrue(voting.agree(2));
36+
assertEqual(voting.decision(), 1);
37+
}
38+
39+
test(relative_quorum) {
40+
Voting<3> voting;
41+
42+
voting.vote(1);
43+
voting.vote(1);
44+
voting.vote(2);
45+
46+
assertTrue(voting.agree(0.5));
47+
assertEqual(voting.decision(), 1);
48+
}
49+
50+
test(rolling_votes) {
51+
Voting<3> voting;
52+
53+
voting.vote(1);
54+
voting.vote(1);
55+
voting.vote(2);
56+
voting.vote(2);
57+
voting.vote(3);
58+
59+
assertTrue(voting.agree(2));
60+
assertEqual(voting.decision(), 2);
61+
}
62+
63+
void setup() {
64+
Serial.begin(115200);
65+
}
66+
67+
68+
void loop() {
69+
Test::run();
70+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#pragma once
2+
3+
#include "Voting.h"
4+
5+
6+
namespace Eloquent {
7+
namespace DataStructures {
8+
/**
9+
* Output a robust value based on the combination of two cascading voting
10+
* @tparam votes
11+
*/
12+
template<uint8_t shortTermVotes, uint8_t longTermVotes>
13+
class LongShortTermVoting {
14+
public:
15+
16+
/**
17+
*
18+
* @param shortQuorum
19+
* @param longQuorum
20+
*/
21+
LongShortTermVoting(float shortQuorum, float longQuorum) :
22+
_shortQuorum(shortQuorum),
23+
_longQuorum(longQuorum) {
24+
25+
}
26+
27+
28+
/**
29+
* Reset state
30+
*/
31+
void clear() {
32+
_shortTerm.clear();
33+
_shortTerm.clear();
34+
}
35+
36+
/**
37+
* Add vote to voting system
38+
*/
39+
void vote(uint8_t vote) {
40+
_shortTerm.vote(vote);
41+
42+
// when the short term has an output, vote for the long term
43+
if (_shortTerm.agree(_shortQuorum)) {
44+
_longTerm.vote(_shortTerm.decision());
45+
}
46+
}
47+
48+
/**
49+
* Test if quorum was achieved
50+
*/
51+
bool agree() {
52+
_decision = 255;
53+
54+
if (_longTerm.agree(_longQuorum) && _longTerm.decision() == _shortTerm.decision()) {
55+
_decision = _longTerm.decision();
56+
57+
return true;
58+
}
59+
60+
return false;
61+
}
62+
63+
/**
64+
* Get robust decision, if any
65+
*/
66+
uint8_t decision() {
67+
return _decision;
68+
}
69+
70+
protected:
71+
uint8_t _decision;
72+
float _shortQuorum;
73+
float _longQuorum;
74+
Voting<shortTermVotes> _shortTerm;
75+
Voting<longTermVotes> _longTerm;
76+
};
77+
}
78+
}

‎src/eloquentarduino/data_structures/Voting.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ namespace Eloquent {
3131

3232
/**
3333
* Test if quorum was achieved
34-
* @param quorum how many votes should agree
34+
* @param quorum how many votes should agree (if < 1, it is considered as a percent)
3535
* @param row how many votes "in row" should agree
3636
*/
37-
bool agree(uint8_t quorum, uint8_t row = 0) {
37+
bool agree(float quorum, uint8_t row = 0) {
3838
_decision = 255;
3939

4040
if (_idx < votes)
4141
return false;
4242

43+
if (quorum < 0)
44+
return false;
45+
46+
if (quorum < 1)
47+
quorum *= votes;
48+
4349
bool agree = false;
4450

4551
for (uint8_t i = 0; i < votes - quorum; i++) {
@@ -71,15 +77,6 @@ namespace Eloquent {
7177
return agree;
7278
}
7379

74-
/**
75-
* Test if quorum was achieved
76-
* @param quorum how many votes should agree (in percent)
77-
* @param row how many votes "in row" should agree
78-
*/
79-
bool agree(float quorum, uint8_t row = 0) {
80-
return agree((uint8_t) round(votes * quorum), row);
81-
}
82-
8380
/**
8481
* Get robust decision, if any
8582
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /