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