aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
UAIMRFWriter_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
46
47namespace gum {
48
49 /*
50 * Default constructor.
51 */
52 template < typename GUM_SCALAR >
54 GUM_CONSTRUCTOR(UAIMRFWriter);
55 }
56
57 /*
58 * Destructor.
59 */
60 template < typename GUM_SCALAR >
62 GUM_DESTRUCTOR(UAIMRFWriter);
63 }
64
65 /*
66 * Writes a Markov net in the given output stream.
67 *
68 * @param output The output stream.
69 * @param MRF The Markov net writen in the stream.
70 * @throws IOError Raised if an I/O error occurs.
71 */
72 template < typename GUM_SCALAR >
73 INLINE void UAIMRFWriter< GUM_SCALAR >::write(std::ostream& output,
75 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
76
77 output << _preambule_(MN) << std::endl;
78
79 for (const auto& kv: MN.factors())
80 output << _factorBloc_(MN, *kv.second) << std::endl;
81
82 output << std::endl;
83
84 output.flush();
85
86 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
87 }
88
89 /*
90 * Writes a Markov net in the file referenced by filePath.
91 * If the file doesn't exists, it is created.
92 * If the file exists, it's content will be erased.
93 *
94 * @param filePath The path to the file used to write the Markov net.
95 * @param MRF The Markov net writen in the file.
96 * @throw IOError Raised if an I/O error occurs.
97 */
98 template < typename GUM_SCALAR >
99 INLINE void UAIMRFWriter< GUM_SCALAR >::write(const std::string& filePath,
101 std::ofstream output(filePath.c_str(), std::ios_base::trunc);
102
103 write(output, MN);
104
105 output.close();
106
107 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
108 }
109
110 template < typename GUM_SCALAR >
111 INLINE std::string
113 std::stringstream str;
114
115 str << "MARKOV" << std::endl;
116
117 str << MN.size() << " # nbr Nodes" << std::endl;
118
119 for (auto node: MN.nodes())
120 str << MN.variable(node).domainSize() << " ";
121 str << std::endl;
122
123 str << MN.factors().size() << " # nbr Factors " << std::endl; // number of cliques
124
125 for (const auto& kv: MN.factors()) {
126 const auto& nodeset = kv.first;
127 str << nodeset.size() << " ";
128 for (auto k: nodeset) {
129 str << k << " ";
130 }
131 str << std::endl;
132 }
133
134 return str.str();
135 }
136
137 template < typename GUM_SCALAR >
138 INLINE std::string
140 const Tensor< GUM_SCALAR >& clikpot) {
141 std::stringstream str;
142
143 str << clikpot.domainSize() << " # {";
144
145 for (Idx k = 0; k < clikpot.nbrDim(); k++) {
146 str << MN.idFromName(clikpot.variable(k).name());
147 if (k == clikpot.nbrDim() - 1) str << "}";
148 else str << ", ";
149 }
150 Instantiation I(clikpot);
151 for (I.setFirst(); !I.end(); ++I) {
152 if (I.val(0) == 0) str << std::endl << " ";
153 str << clikpot[I] << " ";
154 }
155 str << std::endl;
156
157 return str.str();
158 }
159
160} /* namespace gum */
161
162#endif // DOXYGEN_SHOULD_SKIP_THIS
Definition file for UAI exportation class.
Class representing the minimal interface for Markov random field.
Class for assigning/browsing values to tuples of discrete variables.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Definition tensor.h:85
<agrum/MRF/io/UAI/UAIMRFWriter.h>
void write(std::ostream &output, const IMarkovRandomField< GUM_SCALAR > &MN) final
Writes an Markov net in the given ouput stream.
~UAIMRFWriter()
Destructor.
std::string _preambule_(const IMarkovRandomField< GUM_SCALAR > &MN)
Returns the header of the BIF file.
std::string _factorBloc_(const IMarkovRandomField< GUM_SCALAR > &MN, const Tensor< GUM_SCALAR > &clikpot)
UAIMRFWriter()
Default constructor.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46