Awali
Another Weighted Automata library
zmax.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_ZMAX_HH
18 # define AWALI_WEIGHTSET_ZMAX_HH
19 
20 # include <limits>
21 # include <ostream>
22 # include <string>
23 # include <sstream>
24 # include <utility>
25 
26 #include <awali/sttc/misc/raise.hh>
27 #include <awali/common/enums.hh>
28 #include <awali/sttc/misc/stream.hh> // eat
32 
33 namespace awali {
34  namespace sttc {
40  class zmax
41  {
42  public:
43  using self_type = zmax;
44 
45  static std::string sname()
46  {
47  return "zmax";
48  }
49 
50  std::string vname(bool = true) const
51  {
52  return sname();
53  }
54 
56  static zmax make(std::istream& is)
57  {
58  eat(is, sname());
59  return {};
60  }
61 
62  using value_t = int;
63 
64  static value_t
65  add(const value_t l, const value_t r)
66  {
67  return std::max(l, r);
68  }
69 
70  static value_t
71  mul(const value_t l, const value_t r)
72  {
73  return (is_zero(l) || is_zero(r) ? zero()
74  : l + r);
75  }
76 
77  static value_t
78  rdiv(const value_t l, const value_t r)
79  {
80  require(!is_zero(r), "div: division by zero");
81  return l - r;
82  }
83 
84  static value_t
85  ldiv(const value_t l, const value_t r)
86  {
87  return rdiv(r, l);
88  }
89 
90  value_t
91  star(const value_t v) const
92  {
93  if (0 >= v)
94  return one();
95  else
96  raise(sname(), ": star: invalid value: ", format(*this, v));
97  }
98 
99  value_t
100  plus(const value_t v) const
101  {
102  if (0 >= v)
103  return v;
104  else
105  raise(sname(), ": star: invalid value: ", format(*this, v));
106  }
107 
108  static value_t
109  one()
110  {
111  return 0;
112  }
113 
114  static value_t
116  {
118  }
119 
120  static bool
121  equals(const value_t l, const value_t r)
122  {
123  return l == r;
124  }
125 
127  static bool less_than(value_t lhs, value_t rhs)
128  {
129  return lhs < rhs;
130  }
131 
132  constexpr static bool is_special(value_t)
133  {
134  return false;
135  }
136 
137  static bool
138  is_zero(const value_t v)
139  {
140  return v == zero();
141  }
142 
143  static bool
144  is_one(const value_t v)
145  {
146  return v == one();
147  }
148 
149  static constexpr bool is_commutative_semiring() { return true; }
150 
151  static constexpr bool show_one() { return true; }
152  static constexpr star_status_t star_status() { return star_status_t::TOPS; }
153 
154  static value_t
156  {
157  return v;
158  }
159 
160  static size_t hash(value_t v)
161  {
162  return utils::hash_value(v);
163  }
164 
165  static value_t
167  {
168  return v;
169  }
170 
171  static value_t
173  {
174  return v ? one() : zero();
175  }
176 
177  static value_t
178  conv(std::istream& stream)
179  {
180  int sign = 1;
181  switch (int i = stream.peek())
182  {
183  case '-':
184  stream.ignore();
185  if ( stream.peek() == 'o' ) {
186  stream.ignore();
187  if ( stream.get() == 'o')
188  return zero();
189  else
190  throw std::domain_error(sname() + ": invalid value: -o" + str_escape(i));
191  }
192  default:
193  if (stream >> i)
194  return i*sign;
195  else
196  sttc::fail_reading(stream, sname() + ": invalid value");
197  }
198  }
199 
200  static value_t
201  parse(const std::string & s, size_t& p) {
202  if (p>=3 && s[p-3]=='-' && s[p-2]=='o' && s[p-1]=='o'){
203  p -= 3;
204  return zero();
205  }
206  size_t i=p;
207  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
208  ;
209  if(i==p)
210  throw parse_exception("Wrong Z-max value");
211  if(i>0 && (s[i-1]=='-' || s[i-1]=='+'))
212  --i;
213  std::istringstream st(s.substr(i, p-i));
214  value_t x;
215  st >> x;
216  p=i;
217  return x;
218  }
219 
220  static std::ostream&
221  print(const value_t v, std::ostream& o,
222  const std::string& format = "text")
223  {
224  if(format == "json") {
225  if (is_zero(v))
226  o << "\"-oo\"";
227  else
228  o<< v;
229  return o;
230  }
231  if (is_zero(v))
232  o << (format == "latex" ? "-\\infty" : "-oo");
233  else
234  o << v;
235  return o;
236  }
237 
238  std::ostream&
239  print_set(std::ostream& o, const std::string& format = "text") const
240  {
241  if (format == "latex")
242  o << "\\mathbb{Z}_{max}";
243  else if (format == "text")
244  o << "Z-max-plus";
245  else
246  raise("invalid format: ", format);
247  return o;
248  }
249 
250  template<unsigned version = version::fsm_json>
251  value_t
252  static value_from_json(json::node_t const* p)
253  {
254  version::check_fsmjson<version>();
255  switch (version) {
256  case 0: /* Never occurs due to above check. */
257  case 1:
258  default:
259  try {
260  return p->to_int();
261  } catch (json::coercion_exception&) {}
262  try {
263  if (p->to_string() == "-oo")
264  return zero();
265  } catch (json::coercion_exception&) {}
266  std::stringstream ss;
267  ss << "[zmax] At position " << p->path_to_root()
268  << " node cannot be coerced to a Z-max-plus value.";
269  throw json::coercion_exception(ss.str());
270  }
271  }
272 
273  template<unsigned version = version::fsm_json>
274  json::node_t*
276  {
277  switch (version) {
278  case 0:
279  throw parse_exception("[zmax] Unsupported fsm-json version:"
280  + std::to_string(version));
281  case 1:
282  default:
283  if (v == zero())
284  return new json::string_t("-oo");
285  return new json::int_t(v);
286  }
287  }
288 
289  template<unsigned version = version::fsm_json>
290  json::node_t*
291  to_json() const
292  {
293  switch (version) {
294  case 0:
295  throw parse_exception("[zmin] Unsupported fsm-json version:"
296  + std::to_string(version));
297  case 1:
298  default:
299  return new json::object_t("semiring",new json::string_t("Z-max-plus"));
300  }
301  }
302 
303 
304  };
305 
306  inline zmax join(const zmax&, const zmax&) { return {}; }
307 
308  inline zmax join(const b&, const zmax&) { return {}; }
309  inline zmax join(const zmax&, const b&) { return {}; }
310 
311 }}//end of ns awali::stc
312 
313 #endif // !AWALI_WEIGHTSET_ZMAX_HH
Exception used when trying to coerce a node to a given type.
Definition: node.hh:143
Definition: node.hh:488
Definition: node.hh:193
path_t path_to_root() const
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:321
Definition: node.hh:367
Definition: node.hh:529
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of floating Numbers.
Definition: r.hh:35
The Z-max-plus semiring.
Definition: zmax.hh:41
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: zmax.hh:239
static value_t zero()
Definition: zmax.hh:115
static value_t one()
Definition: zmax.hh:109
static value_t conv(std::istream &stream)
Definition: zmax.hh:178
static value_t conv(self_type, value_t v)
Definition: zmax.hh:166
json::node_t * value_to_json(value_t v) const
Definition: zmax.hh:275
static value_t parse(const std::string &s, size_t &p)
Definition: zmax.hh:201
constexpr static bool is_special(value_t)
Definition: zmax.hh:132
static bool is_one(const value_t v)
Definition: zmax.hh:144
std::string vname(bool=true) const
Definition: zmax.hh:50
static zmax make(std::istream &is)
Build from the description in is.
Definition: zmax.hh:56
int value_t
Definition: zmax.hh:62
static value_t conv(b, b::value_t v)
Definition: zmax.hh:172
static std::string sname()
Definition: zmax.hh:45
static constexpr bool show_one()
Definition: zmax.hh:151
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: zmax.hh:221
value_t star(const value_t v) const
Definition: zmax.hh:91
static value_t rdiv(const value_t l, const value_t r)
Definition: zmax.hh:78
static value_t transpose(const value_t v)
Definition: zmax.hh:155
static constexpr bool is_commutative_semiring()
Definition: zmax.hh:149
static value_t add(const value_t l, const value_t r)
Definition: zmax.hh:65
static size_t hash(value_t v)
Definition: zmax.hh:160
static bool equals(const value_t l, const value_t r)
Definition: zmax.hh:121
static constexpr star_status_t star_status()
Definition: zmax.hh:152
value_t plus(const value_t v) const
Definition: zmax.hh:100
static bool is_zero(const value_t v)
Definition: zmax.hh:138
static value_t mul(const value_t l, const value_t r)
Definition: zmax.hh:71
json::node_t * to_json() const
Definition: zmax.hh:291
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: zmax.hh:127
static value_t ldiv(const value_t l, const value_t r)
Definition: zmax.hh:85
static value_t value_from_json(json::node_t const *p)
Definition: zmax.hh:252
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ TOPS
Definition: enums.hh:167
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
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
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