YARP
Yet Another Robot Platform
arbitrator.cpp
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 
11 
12 #include <yarp/sig/Matrix.h>
13 #include <yarp/sig/Vector.h>
14 
15 #ifdef WITH_YARPMATH
16 #include <cmath>
17 #include <yarp/math/Math.h>
18 //#include <gsl/gsl_version.h>
19 //#include <gsl/gsl_math.h>
20 //#include <gsl/gsl_eigen.h>
21 #endif
22 
23 
24 
25 using namespace std;
26 using namespace yarp::sig;
27 using namespace yarp::manager;
28 
29 
33 void Arbitrator::addRule(const char* con, const char* rule)
34 {
35  if(con && rule)
36  {
37  rules[con] = rule;
38  std::map<std::string, double> w;
39  alphas[con] = w;
40  biases[con] = 1.0;
41  }
42 }
43 
44 void Arbitrator::removeRule(const char* con)
45 {
46  rules.erase(rules.find(con));
47  alphas.erase(alphas.find(con));
48  biases.erase(biases.find(con));
49 
50  /*
51  // removing weights of the links to this con
52  std::map<std::string, std::map<std::string, double> >::iterator itr;
53  for(itr=alphas.begin(); itr!=alphas.end(); itr++)
54  {
55  std::map<std::string, double>& w = itr->second;
56  std::map<std::string, double>::iterator jtr;
57  for(jtr=w.begin(); jtr!=w.end(); jtr++)
58  if(jtr->first == string(con))
59  w.erase(jtr);
60  }
61  */
62 }
63 
64 
65 bool Arbitrator::trainWeights(const char* opnd)
66 {
67  __CHECK_NULLPTR(opnd);
68 
69  ErrorLogger* logger = ErrorLogger::Instance();
70 
71  string rule = getRule(opnd);
72  if(!rule.length())
73  {
74  std::map<std::string, double> w;
75  alphas[opnd] = w;
76  biases[opnd] = 1.0;
77  return true;
78  }
79 
80  BinaryExpParser parser;
81  std::map<string, string>::iterator itr;
82  for(itr=rules.begin(); itr!=rules.end(); itr++)
83  parser.addRestrictedOperand((itr->first).c_str());
84 
85  // parsing the compact logic
86  rule = string(opnd) + " : " + rule;
87  if(!parser.parse(rule))
88  return false;
89 
90  // trining the weights
91  LinkTrainer trainer;
92  if(trainer.train(parser.getTruthTable()))
93  {
94  biases[opnd] = trainer.getBias();
95 
96  std::vector<double> alf = trainer.getAlphas();
97  std::map<std::string, double> w;
98 
99  std::map<std::string, bool>::iterator itr;
100  std::map<std::string, bool> operands = parser.getOperands();
101  int i=0;
102  for(itr=operands.begin(); itr!=operands.end(); itr++)
103  {
104  w[itr->first] = alf[i];
105  i++;
106  }
107  alphas[opnd] = w;
108  }
109  else
110  {
111  logger->addError("Maximum number of iterations is reached without finding any solution. Check for the correctness of the expression logic.");
112  return false;
113  }
114 
115  return true;
116 }
117 
118 bool Arbitrator::trainWeights()
119 {
120  biases.clear();
121  alphas.clear();
122  bool bAllOk = true;
123  std::map<string, string>::iterator itr;
124  for(itr=rules.begin(); itr!=rules.end(); itr++)
125  bAllOk &= trainWeights((itr->first).c_str());
126 
127  return bAllOk;
128 }
129 
130 bool Arbitrator::validate()
131 {
132  ErrorLogger* logger = ErrorLogger::Instance();
133 
134  if(!trainWeights())
135  return false;
136 
137 #ifdef WITH_YARPMATH
138 //#if (GSL_MAJOR_VERSION >= 2 || (GSL_MAJOR_VERSION >= 1 && GSL_MINOR_VERSION >= 14))
139  int n = alphas.size();
140  if(n == 0)
141  return true;
142 
143  yarp::sig::Matrix A(n, n);
144  std::map<std::string, std::map<std::string, double> >::iterator itr; // iterating over rows
145  std::map<std::string, std::map<std::string, double> >::iterator jtr; // iterating over cols
146 
147  int row = 0;
148  for(itr=alphas.begin(); itr!=alphas.end(); itr++)
149  {
150  std::map<std::string, double>& w = itr->second;
151  int col = 0;
152  for(jtr=alphas.begin(); jtr!=alphas.end(); jtr++)
153  {
154  std::string opnd = jtr->first;
155  if(w.find(opnd) != w.end())
156  A(row,col) = w[opnd];
157  else
158  A(row,col) = 0.0;
159  col++;
160  }
161  row++;
162  }
163  //printf("%s\n\n", A.toString(1).c_str());
164 
165  yarp::sig::Vector real;
166  yarp::sig::Vector img;
167  yarp::math::eigenValues(A, real, img);
168  bool bStable = true;
169  for(size_t i=0; i<real.size(); i++)
170  {
171  if((float)fabs(real[i]) >= 1.0)
172  {
173  bStable = false;
174  logger->addError("Inconsistency in logical expressions. This will result an unstable arbitration system!");
175  break;
176  }
177  }
178  return bStable;
179 
180  /*
181  gsl_vector_complex *eval = gsl_vector_complex_alloc(n);
182  gsl_matrix_complex *evec = gsl_matrix_complex_alloc(n, n);
183  gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(n);
184 
185  gsl_eigen_nonsymmv ((gsl_matrix *)A.getGslMatrix(), eval, evec, w);
186 
187  bool bStable = true;
188  for(int i=0; i<n; i++)
189  {
190  gsl_complex eval_i = gsl_vector_complex_get (eval, i);
191 
192  if((float)fabs(GSL_REAL(eval_i)) >= 1.0)
193  {
194  bStable = false;
195  logger->addError("Inconsistency in logical expressions. This will result an unstable arbitration system!");
196  break;
197  }
198  //printf ("eigenvalue = %.2f + %.2fi\n", GSL_REAL(eval_i), GSL_IMAG(eval_i));
199  }
200 
201  gsl_eigen_nonsymmv_free(w);
202  gsl_vector_complex_free(eval);
203  gsl_matrix_complex_free(evec);
204  return bStable;
205  */
206 
207 //#else //GSL_VERSION
208 // logger->addWarning("The version of GNU Scientific Library (GSL) used in libYarpMath is insufficient (GSL_VERSION < 1.14). Your compact logical expression might result an unstable arbitration system!");
209 // return true;
210 //#endif //GSL_VERSION
211 
212 #else //WITH_YARPMATH
213  logger->addWarning("Yarpmanager is compiled without libYarpMath. Your compact logical expression might result an unstable arbitration system!");
214  return true;
215 #endif //WITH_YARPMATH
216 
217 }
Vector.h
contains the definition of a Vector type
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::manager::BinaryExpParser::getOperands
const std::map< std::string, bool > & getOperands()
Definition: binexparser.h:112
yarp::manager
Definition: application.h:24
Matrix.h
contains the definition of a Matrix type
yarp::manager::ErrorLogger
Singleton class ErrorLogger.
Definition: utility.h:60
yarp::sig::VectorOf< double >
Math.h
yarp::manager::BinaryExpParser::addRestrictedOperand
void addRestrictedOperand(const char *opnd)
Definition: binexparser.h:106
yarp::manager::ErrorLogger::addError
void addError(const char *szError)
Definition: utility.cpp:121
yarp::manager::BinaryExpParser::parse
bool parse(std::string _exp)
Definition: binexparser.cpp:39
yarp::manager::LinkTrainer
Definition: binexparser.h:148
yarp::manager::ErrorLogger::addWarning
void addWarning(const char *szWarning)
Definition: utility.cpp:108
__CHECK_NULLPTR
#define __CHECK_NULLPTR(_ptr)
Definition: ymm-types.h:83
yarp::manager::LinkTrainer::getBias
double getBias()
Definition: binexparser.h:162
yarp::manager::BinaryExpParser
Definition: binexparser.h:98
yarp::manager::LinkTrainer::getAlphas
const std::vector< double > & getAlphas()
Definition: binexparser.h:160
binexparser.h
yarp::manager::BinaryExpParser::getTruthTable
const std::vector< std::vector< int > > & getTruthTable()
Definition: binexparser.h:115
yarp::sig::VectorOf::size
size_t size() const
Definition: Vector.h:355
arbitrator.h
yarp::math::eigenValues
bool eigenValues(const yarp::sig::Matrix &in, yarp::sig::Vector &real, yarp::sig::Vector &img)
Computes eigenvalues of the n-by-n real nonsymmetric matrix (defined in Math.h).
Definition: math.cpp:600
yarp::manager::LinkTrainer::train
bool train(const std::vector< std::vector< int > > &truthTable)
Definition: binexparser.cpp:396
yarp::sig::Matrix
A class for a Matrix.
Definition: Matrix.h:46