[agrum] adding some indentations in generated o3prm files

parent 5562915b
......@@ -22,8 +22,9 @@
#include <agrum/PRM/o3prm/O3prmBNWriter.h>
namespace gum {
#define O3PRM_INDENT " "
namespace gum {
/*
* Default constructor.
*/
......@@ -73,161 +74,155 @@ namespace gum {
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractAttribute( const IBayesNet<GUM_SCALAR>& bn, NodeId node ) {
O3prmBNWriter<GUM_SCALAR>::__extractAttribute( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
std::stringstream str;
str << __extractType(bn, node) << " ";
str << __extractName(bn, node) << " ";
if (bn.dag().parents(node).size() > 0) {
str << "dependson " << __extractParents(bn, node) << " ";
str << O3PRM_INDENT;
str << __extractType( bn, node ) << " ";
str << __extractName( bn, node ) << " ";
if ( bn.dag().parents( node ).size() > 0 ) {
str << "dependson " << __extractParents( bn, node ) << " ";
}
str << " {" << __extractCPT(bn, node) << "};" << std::endl;
str << " {" << __extractCPT( bn, node ) << "};" << std::endl;
return str.str();
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractParents(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
O3prmBNWriter<GUM_SCALAR>::__extractParents( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
std::stringstream str;
auto var =&( bn.variable(node));
for (auto parent: bn.cpt(node).variablesSequence()) {
if (var != parent) {
auto var = &( bn.variable( node ) );
for ( auto parent : bn.cpt( node ).variablesSequence() ) {
if ( var != parent ) {
str << parent->name() << ", ";
}
}
return str.str().substr(0, str.str().size() - 2);
return str.str().substr( 0, str.str().size() - 2 );
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractCPT(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
Instantiation inst( bn.cpt(node) );
Instantiation jnst;
for ( auto var = inst.variablesSequence().rbegin();
var != inst.variablesSequence().rend();
--var ) {
jnst.add( **var );
}
O3prmBNWriter<GUM_SCALAR>::__extractCPT( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
std::stringstream str;
str << "[" << std::endl;
for (jnst.begin(); !jnst.end(); jnst.inc()) {
inst.setVals( jnst );
str << bn.cpt(node)[inst] << ", ";
}
str << std::endl;
return str.str().substr(0, str.str().size()-3) + "]";
bool first = true;
Instantiation inst( bn.cpt( node ) );
//try {
//auto cpt = std::vector<GUM_SCALAR>();
//auto inst = Instantiation(bn.cpt(node));
//for (inst.setFirst(); ! inst.end(); inst.inc()) {
// cpt.push_back(bn.cpt(node)[inst]);
//}
//std::stringstream str;
//str << "[" << std::endl;
//for (size_t mod = 0; mod < bn.variable(node).domainSize(); ++mod) {
// for (size_t i = mod; i < cpt.size();i += bn.variable(node).domainSize()) {
// str << cpt[i] << ", ";
// }
// str << std::endl;
//}
//return str.str().substr(0, str.str().size()-3) + "]";
//} catch (gum::Exception& e) {
// GUM_SHOWERROR(e);
// throw e;
//}
str << "[";
if ( inst.nbrDim() == 1 ) {
// 1D potential
for ( inst.setFirst(); !inst.end(); inst.inc() ) {
if ( !first ) {
str << ", ";
} else {
first = false;
}
str << bn.cpt( node )[inst];
}
} else {
// (>1)D potential (with parents)
Instantiation jnst;
for ( auto var = inst.variablesSequence().rbegin();
var != inst.variablesSequence().rend();
--var ) {
jnst.add( **var );
}
inst.setFirst();
auto currentval = inst.val( 0 )+1;
for ( jnst.begin(); !jnst.end(); jnst.inc() ) {
inst.setVals( jnst );
if ( !first ) {
str << ", ";
} else {
first = false;
}
if ( currentval != inst.val( 0 ) ) { // begining line
str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
currentval = inst.val( 0 );
}
str << bn.cpt( node )[inst];
}
str << std::endl << O3PRM_INDENT;
}
// const auto& cpt = bn.cpt(node);
// auto inst = Instantiation(cpt);
// auto var = Instantiation();
// var.add(bn.variable(node));
// std::stringstream str;
// str << "[" << std::endl;
// //for (inst.setFirst(); ! inst.end(); inst.inc()) {
// // str << cpt[inst] << ", ";
// //}
// //str << std::endl;
// for (var.setFirst(); ! var.end(); var.inc()) {
// inst.setFirst();
// inst.setVals(var);
// for (;! inst.end(); inst.incOut(var)) {
// str << cpt[inst] << ", ";
// }
// str << std::endl;
// }
// return str.str().substr(0, str.str().size()-3) + "]";
str << "]";
return str.str();
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractType(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
switch (bn.variable(node).varType()) {
case gum::DiscreteVariable::VarType::Discretized:
{
auto double_var = dynamic_cast<const DiscretizedVariable<double>*>(&(bn.variable(node)));
if ( double_var != nullptr ) {
return __extractDiscretizedType<DiscretizedVariable<double>>(double_var);
} else {
auto float_var = dynamic_cast<const DiscretizedVariable<float>*>(&(bn.variable(node)));
if ( float_var != nullptr ) {
return __extractDiscretizedType<DiscretizedVariable<float>>(float_var);
}
O3prmBNWriter<GUM_SCALAR>::__extractType( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
switch ( bn.variable( node ).varType() ) {
case gum::DiscreteVariable::VarType::Discretized: {
auto double_var = dynamic_cast<const DiscretizedVariable<double>*>(
&( bn.variable( node ) ) );
if ( double_var != nullptr ) {
return __extractDiscretizedType<DiscretizedVariable<double>>(
double_var );
} else {
auto float_var = dynamic_cast<const DiscretizedVariable<float>*>(
&( bn.variable( node ) ) );
if ( float_var != nullptr ) {
return __extractDiscretizedType<DiscretizedVariable<float>>(
float_var );
}
GUM_ERROR(InvalidArgument, "DiscretizedVariable ticks are neither doubles or floats");
}
case gum::DiscreteVariable::VarType::Range:
{
return __extractRangeType(bn, node);
}
default:
{
return __extractLabelizedType(bn, node);
}
GUM_ERROR( InvalidArgument,
"DiscretizedVariable ticks are neither doubles or floats" );
}
case gum::DiscreteVariable::VarType::Range: {
return __extractRangeType( bn, node );
}
default: { return __extractLabelizedType( bn, node ); }
}
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractRangeType(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
const auto& var = static_cast<const RangeVariable&>(bn.variable(node));
O3prmBNWriter<GUM_SCALAR>::__extractRangeType( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
const auto& var = static_cast<const RangeVariable&>( bn.variable( node ) );
std::stringstream str;
str << "int (" << var.minVal() << ", " << var.maxVal() << ")";
return str.str();
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractLabelizedType(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
INLINE std::string O3prmBNWriter<GUM_SCALAR>::__extractLabelizedType(
const IBayesNet<GUM_SCALAR>& bn, NodeId node ) {
std::stringstream str;
str << "labels(";
for (auto l: bn.variable(node).labels()) {
for ( auto l : bn.variable( node ).labels() ) {
str << l << ", ";
}
return str.str().substr(0, str.str().size() - 2) + ")";
return str.str().substr( 0, str.str().size() - 2 ) + ")";
}
template <typename GUM_SCALAR>
template <typename VARTYPE>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractDiscretizedType(const VARTYPE* var) {
O3prmBNWriter<GUM_SCALAR>::__extractDiscretizedType( const VARTYPE* var ) {
std::stringstream str;
if ( var->ticks().size() > 3 ) {
str << "real(" << var->ticks()[0];
for (size_t i = 1; i < var->ticks().size() - 1; ++i) {
for ( size_t i = 1; i < var->ticks().size() - 1; ++i ) {
str << ", " << var->ticks()[i];
}
str << ")";
return str.str();
}
GUM_ERROR( InvalidArgument, "discretized variable does not have enough ticks");
GUM_ERROR( InvalidArgument,
"discretized variable does not have enough ticks" );
}
template <typename GUM_SCALAR>
INLINE std::string
O3prmBNWriter<GUM_SCALAR>::__extractName(const IBayesNet<GUM_SCALAR>& bn, NodeId node) {
if (!bn.variable(node).name().empty()) {
return bn.variable(node).name();
O3prmBNWriter<GUM_SCALAR>::__extractName( const IBayesNet<GUM_SCALAR>& bn,
NodeId node ) {
if ( !bn.variable( node ).name().empty() ) {
return bn.variable( node ).name();
} else {
std::stringstream str;
str << node;
......
......@@ -26,9 +26,9 @@
#include <cxxtest/testsuite_utils.h>
#include <agrum/BN/BayesNet.h>
#include <agrum/multidim/instantiation.h>
#include <agrum/PRM/o3prm/O3prmBNReader.h>
#include <agrum/PRM/o3prm/O3prmBNWriter.h>
#include <agrum/multidim/instantiation.h>
#include <agrum/variables/labelizedVariable.h>
// The graph used for the tests:
......@@ -41,161 +41,109 @@
namespace gum_tests {
class O3prmBNWriterTestSuite : public CxxTest::TestSuite {
class O3prmBNWriterTestSuite : public CxxTest::TestSuite {
public:
gum::BayesNet<double>* bn;
gum::NodeId i1, i2, i3, i4, i5;
void setUp() {
bn = new gum::BayesNet<double>();
gum::RangeVariable n1("1", "", 0, 1);
gum::DiscretizedVariable<double> n2("2", "");
n2.addTick(0.0).addTick(0.5).addTick(1.0);
gum::LabelizedVariable n3( "3", "", 2 );
gum::LabelizedVariable n4( "4", "", 2 ), n5( "5", "", 3 );
i1 = bn->add( n1 );
i2 = bn->add( n2 );
i3 = bn->add( n3 );
i4 = bn->add( n4 );
i5 = bn->add( n5 );
bn->addArc( i1, i3 );
bn->addArc( i1, i4 );
bn->addArc( i3, i5 );
bn->addArc( i4, i5 );
bn->addArc( i2, i4 );
bn->addArc( i2, i5 );
fill( *bn );
}
void tearDown() { delete bn; }
void testConstuctor() {
gum::O3prmBNWriter<double>* writer = nullptr;
TS_GUM_ASSERT_THROWS_NOTHING( writer = new gum::O3prmBNWriter<double>() );
delete writer;
}
void testWriter_ostream() {
gum::O3prmBNWriter<double> writer;
// Uncomment this to check the ouput
// TS_GUM_ASSERT_THROWS_NOTHING(writer.write(std::cerr, *bn));
}
void testWriter_string() {
gum::O3prmBNWriter<double> writer;
std::string file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_GUM_ASSERT_THROWS_NOTHING( writer.write( file, *bn ) );
file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_ASSERT_EQUALS( chmod( file.c_str(), 0444 ), 0 );
try {
writer.write( file, *bn );
TS_ASSERT( false );
} catch ( gum::IOError& ) {
TS_ASSERT( true );
}
TS_ASSERT_EQUALS( chmod( file.c_str(), 0666 ), 0 );
}
void testReadAfterWrite() {
gum::O3prmBNWriter<double> writer;
std::string file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_GUM_ASSERT_THROWS_NOTHING( writer.write( file, *bn ) );
file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
gum::BayesNet<double> bn2;
gum::O3prmBNReader<double> reader( &bn2, file, "BayesNet" );
gum::Size res = 0;
TS_GUM_ASSERT_THROWS_NOTHING( res = reader.proceed() );
TS_ASSERT_EQUALS( res, (gum::Size)0 );
for (auto node: bn->dag()) {
auto name = bn->variable(node).name();
const auto& cpt_1 = bn->cpt(node);
auto i = gum::Instantiation(cpt_1);
const auto& cpt_2 = bn2.cpt("baye." + name);
auto j = gum::Instantiation(cpt_2);
TS_ASSERT_EQUALS(i.domainSize(), j.domainSize());
if (i.domainSize() == j.domainSize()) {
for (i.setFirst(); !i.end(); i.inc()) {
for (auto i_var: i.variablesSequence()) {
const auto& j_var = bn2.variable("baye." + i_var->name());
j.chgVal(j_var, i.val(*i_var));
}
TS_ASSERT_DELTA(cpt_1[i], cpt_2[j], 1e-6);
}
gum::BayesNet<double>* bn;
gum::NodeId i1, i2, i3, i4, i5;
void setUp() {
bn = new gum::BayesNet<double>();
gum::RangeVariable n1( "1", "", 0, 1 );
gum::DiscretizedVariable<double> n2( "2", "" );
n2.addTick( 0.0 ).addTick( 0.5 ).addTick( 1.0 );
gum::LabelizedVariable n3( "3", "", 2 );
gum::LabelizedVariable n4( "4", "", 2 ), n5( "5", "", 3 );
i1 = bn->add( n1 );
i2 = bn->add( n2 );
i3 = bn->add( n3 );
i4 = bn->add( n4 );
i5 = bn->add( n5 );
bn->addArc( i1, i3 );
bn->addArc( i1, i4 );
bn->addArc( i3, i5 );
bn->addArc( i4, i5 );
bn->addArc( i2, i4 );
bn->addArc( i2, i5 );
fill( *bn );
}
void tearDown() { delete bn; }
void testConstuctor() {
gum::O3prmBNWriter<double>* writer = nullptr;
TS_GUM_ASSERT_THROWS_NOTHING( writer = new gum::O3prmBNWriter<double>() );
delete writer;
}
void testWriter_ostream() {
gum::O3prmBNWriter<double> writer;
// Uncomment this to check the ouput
// TS_GUM_ASSERT_THROWS_NOTHING(writer.write(std::cerr, *bn));
}
void testWriter_string() {
gum::O3prmBNWriter<double> writer;
std::string file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_GUM_ASSERT_THROWS_NOTHING( writer.write( file, *bn ) );
file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_ASSERT_EQUALS( chmod( file.c_str(), 0444 ), 0 );
try {
writer.write( file, *bn );
TS_ASSERT( false );
} catch ( gum::IOError& ) {
TS_ASSERT( true );
}
TS_ASSERT_EQUALS( chmod( file.c_str(), 0666 ), 0 );
}
void testReadAfterWrite() {
gum::O3prmBNWriter<double> writer;
std::string file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
TS_GUM_ASSERT_THROWS_NOTHING( writer.write( file, *bn ) );
file = GET_RESSOURCES_PATH( "o3prm/BNO3PRMIO_file.o3prm" );
gum::BayesNet<double> bn2;
gum::O3prmBNReader<double> reader( &bn2, file, "BayesNet" );
gum::Size res = 0;
TS_GUM_ASSERT_THROWS_NOTHING( res = reader.proceed() );
TS_ASSERT_EQUALS( res, (gum::Size)0 );
for ( auto node : bn->dag() ) {
auto name = bn->variable( node ).name();
const auto& cpt_1 = bn->cpt( node );
auto i = gum::Instantiation( cpt_1 );
const auto& cpt_2 = bn2.cpt( "baye." + name );
auto j = gum::Instantiation( cpt_2 );
TS_ASSERT_EQUALS( i.domainSize(), j.domainSize() );
if ( i.domainSize() == j.domainSize() ) {
for ( i.setFirst(); !i.end(); i.inc() ) {
for ( auto i_var : i.variablesSequence() ) {
const auto& j_var = bn2.variable( "baye." + i_var->name() );
j.chgVal( j_var, i.val( *i_var ) );
}
TS_ASSERT_DELTA( cpt_1[i], cpt_2[j], 1e-6 );
}
}
}
}
private:
// Builds a BN to test the inference
void fill( gum::BayesNet<double>& bn ) {
const gum::Potential<double>& p1 = bn.cpt( i1 );
{
// FILLING PARAMS
const double t[2] = {0.2, 0.8};
int n = 2;
const std::vector<double> v( t, t + n );
p1.fillWith( v );
}
const gum::Potential<double>& p2 = bn.cpt( i2 );
{
// FILLING PARAMS
const double t[2] = {0.3, 0.7};
int n = 2;
const std::vector<double> v( t, t + n );
p2.fillWith( v );
}
const gum::Potential<double>& p3 = bn.cpt( i3 );
{
// FILLING PARAMS
const double t[4] = {0.1, 0.9, 0.9, 0.1};
int n = 4;
const std::vector<double> v( t, t + n );
p3.fillWith( v );
}
const gum::Potential<double>& p4 = bn.cpt( i4 );
{
// FILLING PARAMS
const double t[8] = {0.4, 0.6, 0.5, 0.5, 0.5, 0.5, 1.0, 0.0};
int n = 8;
const std::vector<double> v( t, t + n );
p4.fillWith( v );
}
const gum::Potential<double>& p5 = bn.cpt( i5 );
{
// FILLING PARAMS
const double t[24] = {0.3, 0.6, 0.1, 0.5, 0.5, 0.0, 0.5, 0.5,
0.0, 1.0, 0.0, 0.0, 0.4, 0.6, 0.0, 0.5,
0.5, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0};
int n = 24;
const std::vector<double> v( t, t + n );
p5.fillWith( v );
}
}
};
// Builds a BN to test the inference
void fill( gum::BayesNet<double>& bn ) {
bn.cpt( i1 ).fillWith( {0.2, 0.8} );
bn.cpt( i2 ).fillWith( {0.3, 0.7} );
bn.cpt( i3 ).fillWith( {0.1, 0.9, 0.9, 0.1} );
bn.cpt( i4 ).fillWith( {0.4, 0.6, 0.5, 0.5, 0.5, 0.5, 1.0, 0.0} );
bn.cpt( i5 ).fillWith( {0.3, 0.6, 0.1, 0.5, 0.5, 0.0, 0.5, 0.5,
0.0, 1.0, 0.0, 0.0, 0.4, 0.6, 0.0, 0.5,
0.5, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0} );
}
};
}
// 0.3, 0.6, 0.1,
// 0.5, 0.5, 0.0,
// 0.5, 0.5, 0.0,
// 1.0, 0.0, 0.0,
// 0.4, 0.6, 0.0,
// 0.5, 0.5, 0.0,
// 0.5, 0.5, 0.0,
// 0.0, 0.0, 1.0
// 0.3 0.5 1 0.4 0.5 0.5 0
// 0.6 0.5 0 0.6 0.5 0.5 0
// 0.1 0.0 0 0.0 0.0 0.0 1
class BayesNet {
int (0, 1) 1 {[
0.2, 0.8]};
int (0, 1) 1 {[0.2, 0.8]};
real(0, 0.5, 1) 2 {[
0.3, 0.7]};
real(0, 0.5, 1) 2 {[0.3, 0.7]};
labels(0, 1) 3 dependson 1 {[
0.1, 0.9, 0.9, 0.1]};
labels(0, 1) 3 dependson 1 {[
0.1, 0.9,
0.9, 0.1
]};
labels(0, 1) 4 dependson 1, 2 {[
0.4, 0.5, 0.5, 1, 0.6, 0.5, 0.5, 0]};
labels(0, 1) 4 dependson 1, 2 {[
0.4, 0.5, 0.5, 1,
0.6, 0.5, 0.5, 0
]};
labels(0, 1, 2) 5 dependson 3, 4, 2 {[
0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 1, 0, 0.6, 0.6, 0.5, 0.5, 0.5, 0.5, 0, 0, 0.1, 0, 0, 0, 0, 0, 0, 1]};
labels(0, 1, 2) 5 dependson 3, 4, 2 {[
0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 1, 0,
0.6, 0.6, 0.5, 0.5, 0.5, 0.5, 0, 0,
0.1, 0, 0, 0, 0, 0, 0, 1
]};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment