-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FactorGraph.cpp
144 lines (117 loc) · 3.44 KB
/
FactorGraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Shell Hu
* Copyright (C) 2013 Shell Hu
*/
#include <shogun/structure/FactorGraph.h>
using namespace shogun;
CFactorGraph::CFactorGraph()
{
SG_UNSTABLE("CFactorGraph::CFactorGraph()", "\n");
register_parameters();
}
CFactorGraph::CFactorGraph(SGVector<int32_t> card)
: m_cardinalities(card)
{
register_parameters();
}
CFactorGraph::CFactorGraph(const CFactorGraph &fg)
{
register_parameters();
m_cardinalities = fg.get_cardinalities();
// No need to unref and ref in this case
// TODO test if need to copy element by element
m_factors = fg.get_factors();
m_datasources = fg.get_factor_data_sources();
}
CFactorGraph::~CFactorGraph()
{
SG_UNREF(m_factors);
SG_UNREF(m_datasources);
if (m_factors != NULL)
SG_DEBUG("CFactorGraph::~CFactorGraph(): m_factors->ref_count() = %d.\n", m_factors->ref_count());
#ifdef USE_REFERENCE_COUNTING
if (m_datasources != NULL)
SG_DEBUG("CFactorGraph::~CFactorGraph(): m_datasources->ref_count() = %d.\n", m_datasources->ref_count());
SG_DEBUG("CFactorGraph::~CFactorGraph(): this->ref_count() = %d.\n", this->ref_count());
#endif
}
void CFactorGraph::register_parameters()
{
SG_ADD(&m_cardinalities, "m_cardinalities", "Cardinalities", MS_NOT_AVAILABLE);
SG_ADD((CSGObject**)&m_factors, "m_factors", "Factors", MS_NOT_AVAILABLE);
SG_ADD((CSGObject**)&m_datasources, "m_datasources", "Factor data sources", MS_NOT_AVAILABLE);
m_factors = NULL;
m_datasources = NULL;
m_factors = new CDynamicObjectArray();
m_datasources = new CDynamicObjectArray();
#ifdef USE_REFERENCE_COUNTING
if (m_factors != NULL)
SG_DEBUG("CFactorGraph::register_parameters(): m_factors->ref_count() = %d.\n", m_factors->ref_count());
#endif
SG_REF(m_factors);
SG_REF(m_datasources);
}
CDynamicObjectArray* CFactorGraph::get_factors() const
{
SG_REF(m_factors);
return m_factors;
}
SGVector<int32_t> CFactorGraph::get_cardinalities() const
{
return m_cardinalities;
}
void CFactorGraph::set_cardinalities(SGVector<int32_t> cards)
{
m_cardinalities = cards.clone();
}
CDynamicObjectArray* CFactorGraph::get_factor_data_sources() const
{
SG_REF(m_datasources);
return m_datasources;
}
void CFactorGraph::compute_energies()
{
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
fac->compute_energies();
SG_UNREF(fac);
}
}
float64_t CFactorGraph::evaluate_energy(const SGVector<int32_t> state) const
{
ASSERT(state.size() == m_cardinalities.size());
float64_t energy = 0.0;
for (int32_t fi = 0; fi < m_factors->get_num_elements(); ++fi)
{
CFactor* fac = dynamic_cast<CFactor*>(m_factors->get_element(fi));
energy += fac->evaluate_energy(state);
SG_UNREF(fac);
}
return energy;
}
float64_t CFactorGraph::evaluate_energy(const CFactorGraphObservation* obs) const
{
return evaluate_energy(obs->get_data());
}
void CFactorGraph::add_factor(CFactor* factor)
{
m_factors->push_back(factor);
}
void CFactorGraph::add_data_source(CFactorDataSource* datasource)
{
m_datasources->push_back(datasource);
}
CFactorGraph* CFactorGraph::duplicate() const
{
return new CFactorGraph(*this);
}
int32_t CFactorGraph::get_num_vectors() const
{
return m_factors->get_num_elements();
}