aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
mddOperatorStrategy_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
51
52// =========================================================================
53#include <algorithm>
54#include <memory>
55#include <queue>
56#include <thread>
57#include <utility>
58#include <vector>
59// =========================================================================
61
63// =========================================================================
67// =========================================================================
69// =========================================================================
70
72#define RECAST(x) reinterpret_cast< const MultiDimFunctionGraph< GUM_ELEMENT >* >(x)
73
74namespace gum {
75
76
77 /* **************************************************************************************************
78 * **/
79 /* ** **/
80 /* ** Constructors / Destructors **/
81 /* ** **/
82 /* **************************************************************************************************
83 * **/
84
85 // ===========================================================================
86 // Default constructor
87 // ===========================================================================
88 template < typename GUM_ELEMENT >
92
93 // ===========================================================================
94 // Default destructor
95 // ===========================================================================
96 template < typename GUM_ELEMENT >
100
101 /* **************************************************************************************************
102 * **/
103 /* ** **/
104 /* ** Graph Function Operations Methods **/
105 /* ** **/
106 /* **************************************************************************************************
107 * **/
108
109 // ==========================================================================
115 // ==========================================================================
116 template < typename GUM_ELEMENT >
119 Idx actionId,
120 const FMDP< GUM_ELEMENT >* fmdp,
121 const gum::VariableSet& elVarSeq) {
124 qAction->copy(*Vold);
125
126 const DiscreteVariable* xip = this->lastVar_(qAction);
127
128 while (this->shouldEleminateVar_(xip, fmdp)) {
129 const DiscreteVariable* xi = fmdp->mapMainPrime().first(xip);
130 const MultiDimFunctionGraph< GUM_ELEMENT >* pxi = RECAST(fmdp->transition(actionId, xi));
131 // a frame variable (without transition) keeps its value
132 std::unique_ptr< MultiDimFunctionGraph< GUM_ELEMENT > > frame;
133 if (pxi == nullptr) {
134 frame.reset(this->frameTransition_(xi, xip));
135 pxi = frame.get();
136 }
138 pxi,
139 &elVarSeq,
140 xip,
141 (GUM_ELEMENT)0);
143 delete qAction;
144 qAction = temp;
145 xip = this->lastVar_(qAction);
146 }
147
148 return qAction;
149 }
150
151 // ==========================================================================
152 // ==========================================================================
153 template < typename GUM_ELEMENT >
157 Idx del) {
158 MultiDimFunctionGraph< GUM_ELEMENT >* ret = maximize2MultiDimFunctionGraphs(f1, f2);
159 this->deleteFunctionGraph_(f1, f2, del);
160 return ret;
161 }
162
163 // ==========================================================================
164 // ==========================================================================
165 template < typename GUM_ELEMENT >
169 Idx del) {
170 MultiDimFunctionGraph< GUM_ELEMENT >* ret = minimize2MultiDimFunctionGraphs(f1, f2);
171 this->deleteFunctionGraph_(f1, f2, del);
172 return ret;
173 }
174
175 // ==========================================================================
176 // ==========================================================================
177 template < typename GUM_ELEMENT >
186
187 // ==========================================================================
188 // ==========================================================================
189 template < typename GUM_ELEMENT >
204
205 // ==========================================================================
210 // ==========================================================================
211 template < typename GUM_ELEMENT >
220
221 // ==========================================================================
226 // ==========================================================================
227 template < typename GUM_ELEMENT >
236
237 template < typename GUM_ELEMENT >
239 const FMDP< GUM_ELEMENT >* fmdp) {
240 return v == nullptr ? false : fmdp->mapMainPrime().existsSecond(v);
241 }
242
243 template < typename GUM_ELEMENT >
245 const MultiDimFunctionGraph< GUM_ELEMENT >* function) {
246 return function->variablesSequence().size() == 0
247 ? nullptr
248 : function->variablesSequence().atPos(function->variablesSequence().size() - 1);
249 }
250
251 template < typename GUM_ELEMENT >
256
257 template < typename GUM_ELEMENT >
263
264 template < typename GUM_ELEMENT >
270
271} // end of namespace gum
A class to store the optimal actions.
Definition actionSet.h:98
Class to handle efficiently argMaxSet.
Definition argMaxSet.h:78
Base class for discrete random variable.
This class is used to implement factored decision process.
Definition fmdp.h:75
const Bijection< const DiscreteVariable *, const DiscreteVariable * > & mapMainPrime() const
Returns the map binding main variables and prime variables.
Definition fmdp_tpl.h:409
const MultiDimImplementation< GUM_ELEMENT > * transition(Idx actionId, const DiscreteVariable *v) const
Returns transition associated to given in parameter variable and the given action.
Definition fmdp_tpl.h:226
void deleteFunctionGraph_(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del)
MultiDimFunctionGraph< GUM_ELEMENT > * frameTransition_(const DiscreteVariable *x, const DiscreteVariable *xp)
Returns a new function graph P(x'|x) = [x' == x].
~MDDOperatorStrategy() override
Default destructor.
MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > * getArgMaxFunctionInstance() override
const DiscreteVariable * lastVar_(const MultiDimFunctionGraph< GUM_ELEMENT > *function)
Returns the last var in the var order for given graph function Called by the evalQaction.
bool shouldEleminateVar_(const DiscreteVariable *v, const FMDP< GUM_ELEMENT > *fmdp)
Indicates if whether or not given var is to be eliminated. Called by the evalQaction.
MultiDimFunctionGraph< ActionSet, SetTerminalNodePolicy > * getAggregatorInstance() override
MultiDimFunctionGraph< GUM_ELEMENT > * regress(const MultiDimFunctionGraph< GUM_ELEMENT > *Vold, Idx actionId, const FMDP< GUM_ELEMENT > *fmdp, const gum::VariableSet &elVarSeq) override
Computes Qaction for given actionid.
MultiDimFunctionGraph< GUM_ELEMENT > * minimize(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del=3) override
MultiDimFunctionGraph< GUM_ELEMENT > * maximize(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del=3) override
MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > * argmaximize(const MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > *f1, const MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > *f2, Idx del=3) override
MultiDimFunctionGraph< GUM_ELEMENT > * subtract(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del=0) override
Subtract current VFunction from old VFunction to see if threshold is reached or not.
MultiDimFunctionGraph< GUM_ELEMENT > * multiply(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del=3) override
MultiDimFunctionGraph< GUM_ELEMENT > * add(const MultiDimFunctionGraph< GUM_ELEMENT > *f1, const MultiDimFunctionGraph< GUM_ELEMENT > *f2, Idx del=1) override
Adds reward to given function( whether a qAction or vFunction).
MultiDimFunctionGraph< GUM_ELEMENT, ExactTerminalNodePolicy > * getFunctionInstance() override
MDDOperatorStrategy()
Default constructor.
Class used to perform Function Graph Operations.
MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
Class implementingting a function graph.
void copy(const MultiDimContainer< GUM_ELEMENT > &src) override
static MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
Class used to perform Function Graph Operations in the FMDP Framework.
Definition regress.h:69
MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
Implementation of a Terminal Node Policy that maps nodeid to a set of value.
This files contains several function objects that are not (yet) defined in the STL.
Size Idx
Type for indexes.
Definition types.h:79
MultiDimFunctionGraph< T > * multiply2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for multiplying two multiDimArrays
MultiDimFunctionGraph< T > * subtract2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for subtracting two multiDimArrays
MultiDimFunctionGraph< T > * add2MultiDimFunctionGraphs(const MultiDimFunctionGraph< T > *t1, const MultiDimFunctionGraph< T > *t2)
a specialized function for summing two multiDimArrays
Useful macros for maths.
Headers of the MDDOperatorStrategy planer class.
#define RECAST(x)
For shorter line and hence more comprehensive code only.
Class used to compute the operation between two decision diagrams.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
Class used to compute the operation between two decision diagrams.
<agrum/FMDP/planning/actionSet.h>
Definition actionSet.h:76
Header of the Tensor class.