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