aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
UAIMNWriter_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) et Christophe GONZALES(@AMU)
4  * (@AMU) info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 # include <agrum/MN/io/UAI/UAIMNWriter.h>
25 
26 namespace gum {
27 
28  /*
29  * Default constructor.
30  */
31  template < typename GUM_SCALAR >
32  INLINE UAIMNWriter< GUM_SCALAR >::UAIMNWriter() {
34  }
35 
36  /*
37  * Destructor.
38  */
39  template < typename GUM_SCALAR >
42  }
43 
44  /*
45  * Writes a Markov net in the given ouput stream.
46  *
47  * @param output The output stream.
48  * @param MN The Markov net writen in the stream.
49  * @throws IOError Raised if an I/O error occurs.
50  */
51  template < typename GUM_SCALAR >
52  INLINE void
54  const IMarkovNet< GUM_SCALAR >& MN) {
55  if (!output.good()) {
56  GUM_ERROR(IOError, "Stream states flags are not all unset.");
57  }
58 
59  output << preambule__(MN) << std::endl;
60 
61  for (const auto& kv: MN.factors())
63 
64  output << std::endl;
65 
66  output.flush();
67 
68  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
69  }
70 
71  /*
72  * Writes a Markov net in the file referenced by filePath.
73  * If the file doesn't exists, it is created.
74  * If the file exists, it's content will be erased.
75  *
76  * @param filePath The path to the file used to write the Markov net.
77  * @param MN The Markov net writen in the file.
78  * @throw IOError Raised if an I/O error occurs.
79  */
80  template < typename GUM_SCALAR >
81  INLINE void
83  const IMarkovNet< GUM_SCALAR >& MN) {
85 
86  write(output, MN);
87 
88  output.close();
89 
90  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
91  }
92 
93  template < typename GUM_SCALAR >
97 
98  str << "MARKOV" << std::endl;
99 
100  str << MN.size() << " # nbr Nodes" << std::endl;
101 
102  for (auto node: MN.nodes())
103  str << MN.variable(node).domainSize() << " ";
104  str << std::endl;
105 
106  str << MN.factors().size() << " # nbr Factors "
107  << std::endl; // number of cliques
108 
109  for (const auto& kv: MN.factors()) {
110  const auto& nodeset = kv.first;
111  str << nodeset.size() << " ";
112  for (auto k: nodeset) {
113  str << k << " ";
114  }
115  str << std::endl;
116  }
117 
118  return str.str();
119  }
120 
121  template < typename GUM_SCALAR >
123  const IMarkovNet< GUM_SCALAR >& MN,
124  const Potential< GUM_SCALAR >& clikpot) {
126 
127  str << clikpot.domainSize() << " # {";
128 
129  for (Idx k = 0; k < clikpot.nbrDim(); k++) {
131  if (k == clikpot.nbrDim() - 1)
132  str << "}";
133  else
134  str << ", ";
135  }
137  for (I.setFirst(); !I.end(); ++I) {
138  if (I.val(0) == 0) str << std::endl << " ";
139  str << clikpot[I] << " ";
140  }
141  str << std::endl;
142 
143  return str.str();
144  }
145 
146 } /* namespace gum */
147 
148 #endif // DOXYGEN_SHOULD_SKIP_THIS
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669