Awali
Another Weighted Automata library
maxmin.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_WEIGHTSET_MAXMIN_HH
18 # define AWALI_WEIGHTSET_MAXMIN_HH
19 
20 # include <ostream>
21 # include <string>
22 # include <sstream>
23 # include <utility>
24 
25 #include <awali/sttc/misc/raise.hh>
26 #include <awali/common/enums.hh>
27 # include <limits>
28 #include <awali/sttc/misc/stream.hh> // eat
33 
34 namespace awali {
35  namespace sttc {
41  class maxmin
42  {
43  public:
44  using self_type = maxmin;
45 
46  static std::string sname()
47  {
48  return "maxmin";
49  }
50 
51  std::string vname(bool = true) const
52  {
53  return sname();
54  }
55 
57  static maxmin make(std::istream& is)
58  {
59  eat(is, sname());
60  return {};
61  }
62 
63  using value_t = double;
64  using locally_finite_t = bool;
65  static const locally_finite_t locally_finite=true;
66 
67  static value_t
68  add(const value_t l, const value_t r)
69  {
70  return std::max(l, r);
71  }
72 
73  static value_t
74  mul(const value_t l, const value_t r)
75  {
76  return std::min(l, r);
77  }
78 
79  static value_t
80  rdiv(const value_t l, const value_t r)
81  {
82  raise(sname(), "No division");
83  }
84 
85  static value_t
86  ldiv(const value_t l, const value_t r)
87  {
88  raise(sname(), "No division");
89  }
90 
91  value_t
92  star(const value_t v) const
93  {
94  return one();
95  }
96 
97  value_t
98  plus(const value_t v) const
99  {
100  return v;
101  }
102 
103  static value_t
104  one()
105  {
107  }
108 
109  static value_t
111  {
113  }
114 
115  static bool
116  equals(const value_t l, const value_t r)
117  {
118  return l == r;
119  }
120 
122  static bool less_than(value_t lhs, value_t rhs)
123  {
124  return lhs < rhs;
125  }
126 
127  constexpr static bool is_special(value_t)
128  {
129  return false;
130  }
131 
132  static bool
133  is_zero(const value_t v)
134  {
135  return v == zero();
136  }
137 
138  static bool
139  is_one(const value_t v)
140  {
141  return v == one();
142  }
143 
144  static constexpr bool is_commutative_semiring() { return true; }
145 
146  static constexpr bool show_one() { return true; }
147  static constexpr star_status_t star_status() { return star_status_t::STARRABLE; }
148 
149  static value_t
151  {
152  return v;
153  }
154 
155  static size_t hash(value_t v)
156  {
157  return utils::hash_value(v);
158  }
159 
160  static value_t
162  {
163  return v;
164  }
165 
166  static value_t
168  {
169  return v ? one() : zero();
170  }
171 
172  static value_t
173  conv(std::istream& stream)
174  {
175  switch (int i = stream.peek())
176  {
177  case 'o':
178  stream.ignore();
179  if ((i = stream.get()) == 'o')
180  return one();
181  else
182  throw std::domain_error(sname() + ": invalid value: o" + str_escape(i));
183  case '-':
184  if ((i = stream.get()) == 'o')
185  if ((i = stream.get()) == 'o')
186  return zero();
187  else
188  throw std::domain_error(sname() + ": invalid value: -o" + str_escape(i));
189  else
190  if (stream >> i)
191  return -i;
192  else
193  sttc::fail_reading(stream, sname() + ": invalid value");
194  default:
195  if (stream >> i)
196  return i;
197  else
198  sttc::fail_reading(stream, sname() + ": invalid value");
199  }
200  }
201 
202  static value_t
203  parse(const std::string & s, size_t& p) {
204  // TODO !!
205  size_t i=p;
206  for(; i>0 && ((s[i-1]>='0' && s[i-1]<='9') || s[i-1]=='e' || s[i-1]=='E' || s[i-1]=='.' || s[i-1]=='+' || s[i-1]=='-'); --i)
207  ;
208  if(i==p)
209  throw parse_exception("Wrong R value");
210  std::istringstream st(s.substr(i, p-i));
211  value_t x;
212  st >> x;
213  p=i;
214  return x;
215  }
216 
217  static std::ostream&
218  print(const value_t v, std::ostream& o,
219  const std::string& format = "text")
220  {
221  if (is_zero(v)) {
222  if(format == "dot")
223  o << "_";
224  else
225  o << "\"-oo\"";
226  }
227  else
228  if (is_one(v)) {
229  if(format == "dot")
230  o << "T";
231  else
232  o << "\"oo\"";
233  }
234  else
235  o << v;
236  return o;
237  }
238 
239  std::ostream&
240  print_set(std::ostream& o, const std::string& format = "text") const
241  {
242  if (format == "latex")
243  o << "\\mathbb{R}_{\\max,\\min}";
244  else if (format == "text")
245  o << "Fuzzy";
246  else
247  raise("invalid format: ", format);
248  return o;
249  }
250 
251 
252  template<unsigned version = version::fsm_json>
253  static
255  {
256  switch (version) {
257  case 0:
258  throw parse_exception("[Fuzzy] Unsupported fsm-json version:"
259  + std::to_string(version));
260  case 1:
261  default:
262  json::object_t* obj = new json::object_t();
263  obj->push_back("semiring",new json::string_t("Fuzzy"));
264  return (obj);
265  }
266  }
267 
268 
269  template<unsigned version = version::fsm_json>
271  const
272  {
273  switch (version) {
274  case 0:
275  throw parse_exception("[Fuzzy] Unsupported fsm-json version:"
276  + std::to_string(version));
277  case 1:
278  default:
279  return new json::float_t(v);
280  }
281  }
282 
283  template<unsigned version = version::fsm_json>
285  const
286  {
287  switch (version) {
288  case 0:
289  case 1:
290  default:
291  try {
292  return p->to_double();
293  } catch (json::coercion_exception&) {}
294  try {
295  std::string str = p->to_string();
296  if (str == "oo")
297  return zero();
298  if (str == "-oo")
299  return one();
300  } catch (json::coercion_exception&) {}
302  "Cannot be coerced to a value of Z-min-max",
303  "maxmin::value_from_json",
304  p);
305  }
306  }
307 
308  };
309 
310  inline maxmin join(const maxmin&, const maxmin&) { return {}; }
311 
312  inline maxmin join(const b&, const maxmin&) { return {}; }
313  inline maxmin join(const maxmin&, const b&) { return {}; }
314 
315 }}//end of ns awali::stc
316 
317 #endif // !AWALI_WEIGHTSET_MAXMIN_HH
Exception used when trying to coerce a node to a given type.
Definition: node.hh:143
Definition: node.hh:508
Definition: node.hh:193
virtual double to_double() const
Coerces this node_t to a double
Definition: node.hh:343
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
Definition: node.hh:367
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:529
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The max-min semiring over floating numbers.
Definition: maxmin.hh:42
static std::string sname()
Definition: maxmin.hh:46
static json::node_t * to_json()
Definition: maxmin.hh:254
static constexpr star_status_t star_status()
Definition: maxmin.hh:147
static constexpr bool is_commutative_semiring()
Definition: maxmin.hh:144
static value_t zero()
Definition: maxmin.hh:110
static value_t conv(b, b::value_t v)
Definition: maxmin.hh:167
value_t value_from_json(json::node_t const *p) const
Definition: maxmin.hh:284
static value_t one()
Definition: maxmin.hh:104
double value_t
Definition: maxmin.hh:63
constexpr static bool is_special(value_t)
Definition: maxmin.hh:127
value_t plus(const value_t v) const
Definition: maxmin.hh:98
static value_t transpose(const value_t v)
Definition: maxmin.hh:150
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: maxmin.hh:240
value_t star(const value_t v) const
Definition: maxmin.hh:92
json::node_t * value_to_json(value_t v) const
Definition: maxmin.hh:270
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: maxmin.hh:122
static bool is_zero(const value_t v)
Definition: maxmin.hh:133
static value_t mul(const value_t l, const value_t r)
Definition: maxmin.hh:74
bool locally_finite_t
Definition: maxmin.hh:64
static size_t hash(value_t v)
Definition: maxmin.hh:155
static value_t conv(std::istream &stream)
Definition: maxmin.hh:173
static const locally_finite_t locally_finite
Definition: maxmin.hh:65
static value_t conv(self_type, value_t v)
Definition: maxmin.hh:161
static value_t add(const value_t l, const value_t r)
Definition: maxmin.hh:68
static bool is_one(const value_t v)
Definition: maxmin.hh:139
static bool equals(const value_t l, const value_t r)
Definition: maxmin.hh:116
static value_t ldiv(const value_t l, const value_t r)
Definition: maxmin.hh:86
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: maxmin.hh:218
static value_t parse(const std::string &s, size_t &p)
Definition: maxmin.hh:203
static value_t rdiv(const value_t l, const value_t r)
Definition: maxmin.hh:80
static maxmin make(std::istream &is)
Build from the description in is.
Definition: maxmin.hh:57
static constexpr bool show_one()
Definition: maxmin.hh:146
std::string vname(bool=true) const
Definition: maxmin.hh:51
The semiring of floating Numbers.
Definition: r.hh:35
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ STARRABLE
The star of every element exists.
Definition: enums.hh:165
std::string to_string(identities i)
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:449
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
std::size_t hash_value(const T &v)
Definition: hash.hh:76
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
ATTRIBUTE_CONST int min(int a, int b)
Definition: arith.hh:48
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26