YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
binexparser.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_MANAGER_BINEXPARSER
7#define YARP_MANAGER_BINEXPARSER
8
9#include <string>
10#include <cstdio>
11
14#include <yarp/manager/graph.h>
15#include <yarp/conf/api.h>
16
17namespace yarp::manager {
18
19
24
25
26static int node_id = 0;
27
28class BinaryNode : public Node
29{
30
31public:
32 BinaryNode(const char* opd) : Node(static_cast<NodeType>(OPERAND)) {
33 char str[128];
34 sprintf(str, "%s%d", opd, node_id++);
35 setLabel(str);
36 strName = opd;
37 value = false;
38 }
39
40 BinaryNode(const char* opt,
41 BinaryNode* left,
42 BinaryNode* right ) : Node(static_cast<NodeType>(OPERATOR)) {
43
44 char str[128];
45 sprintf(str, "%s%d", opt, node_id++);
46 setLabel(str);
47 strName = opt;
48 value = false;
49 if(left) {
50 addSuc(left, 0);
51 }
52 if(right) {
53 addSuc(right, 0);
54 }
55 }
56
57 ~BinaryNode() override = default;
58
59 Node* clone() override {
60 BinaryNode* binode = new BinaryNode(*this);
61 return binode;
62 }
63
65 if(sucCount()<1) {
66 return nullptr;
67 }
68 return (BinaryNode*) getLinkAt(0).to();
69 }
70
72 if(sucCount()<2) {
73 return nullptr;
74 }
75 return (BinaryNode*) getLinkAt(1).to();
76 }
77
78 bool getValue() { return value; }
79 void setValue(bool val) { value = val; }
80
81 const char* getName() {return strName.c_str(); }
82
83protected:
84
85private:
86 bool value;
87 std::string strName;
88
89};
90
92
94{
95
96public:
99
100 bool parse(std::string _exp);
101 bool exportDotGraph(const char* szFileName);
102 void addRestrictedOperand(const char* opnd) {
103 if(opnd) {
104 validOperands.push_back(opnd);
105 }
106 }
107
108 const std::map<std::string, bool> &getOperands() {
109 return operands;
110 }
111 const std::vector<std::vector<int> > &getTruthTable() {
112 return truthTable;
113 }
114
115private:
116
117 bool evalTree(BinaryNodePtr node, std::map<std::string, bool>& opnd);
118
119 bool checkExpression(std::string& strexp);
120 void parseExpression(std::string &strexp, BinaryNodePtr& node);
121 void parseNot(std::string &strexp, BinaryNodePtr& node);
122 void parseFactor(std::string &strexp, BinaryNodePtr& node);
123 std::string getNextOperand(std::string &strexp);
124 std::string popNextOperand(std::string &strexp);
125 void createTruthTable(const int n);
126 void printTruthTable(std::string lopr);
127 //bool train(int max_itr=1000, double train_rate=1.0);
128
129private:
130 std::string expression;
131 std::string leftOpr;
132 Graph binTree;
133 // mapping operands to their real value
134 std::map<std::string, bool> operands;
135 std::vector<std::string> validOperands;
136 std::vector<std::string> invalidOperands;
137 std::vector<std::vector<int> > truthTable;
138 //std::vector<double> alphas;
139 //std::vector<double> errors;
140 //double bias;
141};
142
144{
145public:
146 LinkTrainer(int max_itr=1000, double train_rate=1.0) :
147 maxIteration(max_itr),
148 trainRate(train_rate),
149 bias(0.0)
150 {}
151
152 virtual ~LinkTrainer() {}
153
154 bool train(const std::vector<std::vector<int> > &truthTable);
155
156 const std::vector<double> &getAlphas() { return alphas; }
157 const std::vector<double> &getErrors() { return errors; }
158 double getBias() {return bias; }
159
160private:
161 int maxIteration;
162 double trainRate;
163 std::vector<double> alphas;
164 std::vector<double> errors;
165 double bias;
166
167};
168
169} // namespace yarp::manager
170
171
172#endif // __YARP_MANAGER_BINEXPARSER____
const std::vector< std::vector< int > > & getTruthTable()
void addRestrictedOperand(const char *opnd)
bool parse(std::string _exp)
const std::map< std::string, bool > & getOperands()
bool exportDotGraph(const char *szFileName)
~BinaryNode() override=default
Node * clone() override
Definition binexparser.h:59
BinaryNode(const char *opd)
Definition binexparser.h:32
void setValue(bool val)
Definition binexparser.h:79
BinaryNode(const char *opt, BinaryNode *left, BinaryNode *right)
Definition binexparser.h:40
Class Graph.
Definition graph.h:27
bool train(const std::vector< std::vector< int > > &truthTable)
const std::vector< double > & getErrors()
LinkTrainer(int max_itr=1000, double train_rate=1.0)
const std::vector< double > & getAlphas()
a Node of a Graph
Definition node.h:64
Link & getLinkAt(int index)
Definition node.h:95
bool addSuc(Node *node, float weight, bool _virtual=false)
class Node
Definition node.cpp:15
void setLabel(const char *szLabel)
Definition node.h:92
BinaryNode * BinaryNodePtr
Definition binexparser.h:91
enum yarp::manager::__BinNodeType BinNodeType
enum yarp::manager::__NodeType NodeType
static int node_id
Definition binexparser.h:26