#include "itkParticleSwarmOptimizerSAXWriter.h"
#include "itksys/SystemTools.hxx"
namespace itk
{
int ParticleSwarmOptimizerSAXWriter::CanWriteFile( const char* name )
{
std::ofstream ofs( name );
int yes = ofs.is_open();
if (yes)
{
ofs.close();
}
return yes;
}
int ParticleSwarmOptimizerSAXWriter::WriteFile()
{
try
{
if ( !this->CanWriteFile( this->m_Filename.c_str() ) )
{
ExceptionObject
e( __FILE__, __LINE__ );
std::string message = "Cannot write to ";
message += this->m_Filename;
message += "!\n";
e.SetDescription( message.c_str() );
}
if ( this->m_InputObject == 0 )
{
itkExceptionMacro( "Object to be written is null!\n" );
}
std::ofstream ofs( this->m_Filename.c_str() );
ofs << "<optimizer type=\"ParticleSwarmOptimizer\"";
ofs << " NumberOfParticles=\"" << this->m_InputObject->GetNumberOfParticles() << "\"";
ofs << " MaximumNumberOfIterations=\"" << this->m_InputObject->GetMaximalNumberOfIterations() << "\"";
ofs << " InertiaCoefficient=\"" << this->m_InputObject->GetInertiaCoefficient() << "\"";
ofs << " GlobalCoefficient=\"" << this->m_InputObject->GetGlobalCoefficient() << "\"";
ofs << " PersonalCoefficient=\"" << this->m_InputObject->GetPersonalCoefficient() << "\"";
ofs << " FunctionConvergenceTolerance=\"" << this->m_InputObject->GetFunctionConvergenceTolerance() << "\"";
ofs << " ConvergedPercentageToStop=\"" << this->m_InputObject->GetPercentageParticlesConverged() << "\"";
ofs << ">";
ofs << "\n";
ofs << " <bound id=\"lower\"";
ofs << " value=\"";
for ( size_t i = 0; i < bounds.size(); i++ )
{
ofs << " " << bounds[i].first;
}
ofs << "\"";
ofs << "/>";
ofs << "\n";
ofs << " <bound id=\"upper\"";
ofs << " value=\"";
for ( size_t i = 0; i < bounds.size(); i++ )
{
ofs << " " << bounds[i].second;
}
ofs << "\"";
ofs << "/>";
ofs << "\n";
ofs << " <ParametersConvergenceTolerance>";
Array<double> ptols = this->m_InputObject->GetParametersConvergenceTolerance();
for ( unsigned int i = 0; i < ptols.GetSize(); i++ )
{
ofs << " " << ptols[ i ];
}
ofs << "</ParametersConvergenceTolerance>";
ofs << "\n";
ofs << "</optimizer>";
ofs << "\n";
ofs.close();
return 1;
}
catch (...)
{
return 0;
}
}
}