aGrUM 3.0.0
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-2026 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-2026 *
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
41#pragma once
42
43
44#include <agrum/MRF/io/UAI/UAIMRFWriter.h> // to ease IDE parser
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
48
49namespace gum {
50
51 /*
52 * Default constructor.
53 */
54 template < GUM_Numeric GUM_SCALAR >
56 GUM_CONSTRUCTOR(UAIMRFWriter);
57 }
58
59 /*
60 * Destructor.
61 */
62 template < GUM_Numeric GUM_SCALAR >
64 GUM_DESTRUCTOR(UAIMRFWriter);
65 }
66
67 /*
68 * Writes a Markov net in the given output stream.
69 *
70 * @param output The output stream.
71 * @param MRF The Markov net writen in the stream.
72 * @throws IOError Raised if an I/O error occurs.
73 */
74 template < GUM_Numeric GUM_SCALAR >
75 void UAIMRFWriter< GUM_SCALAR >::write(std::ostream& output,
77 if (!output.good()) { GUM_ERROR(IOError, "Input/Output error : stream not writable.") }
78
79 output << _preambule_(MN) << std::endl;
80
81 for (const auto& kv: MN.factors())
82 output << _factorBloc_(MN, *kv.second) << std::endl;
83
84 output << std::endl;
85
86 output.flush();
87
88 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
89 }
90
91 /*
92 * Writes a Markov net in the file referenced by filePath.
93 * If the file doesn't exists, it is created.
94 * If the file exists, it's content will be erased.
95 *
96 * @param filePath The path to the file used to write the Markov net.
97 * @param MRF The Markov net writen in the file.
98 * @throw IOError Raised if an I/O error occurs.
99 */
100 template < GUM_Numeric GUM_SCALAR >
101 void UAIMRFWriter< GUM_SCALAR >::write(std::string_view filePath,
103 std::ofstream output(std::filesystem::path{filePath}, std::ios_base::trunc);
104
105 write(output, MN);
106
107 output.close();
108
109 if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed.") }
110 }
111
112 template < GUM_Numeric GUM_SCALAR >
114 std::stringstream str;
115
116 str << "MARKOV" << std::endl;
117
118 str << std::format("{} # nbr Nodes\n", MN.size());
119
120 for (auto node: MN.nodes())
121 str << MN.variable(node).domainSize() << " ";
122 str << std::endl;
123
124 str << std::format("{} # nbr Factors \n", MN.factors().size()); // number of cliques
125
126 for (const auto& kv: MN.factors()) {
127 const auto& nodeset = kv.first;
128 str << nodeset.size() << " ";
129 for (auto k: nodeset) {
130 str << k << " ";
131 }
132 str << std::endl;
133 }
134
135 return str.str();
136 }
137
138 template < GUM_Numeric GUM_SCALAR >
140 const Tensor< GUM_SCALAR >& clikpot) {
141 std::stringstream str;
142
143 str << std::format("{} # {{", 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 a Markov net to the given output stream.
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.
~UAIMRFWriter() override
Destructor.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46