Skip to content

Commit

Permalink
replace sprintf by std::stringstream; add another IpoptData::Append_i…
Browse files Browse the repository at this point in the history
…nfo_string
  • Loading branch information
svigerske committed Dec 11, 2024
1 parent 40440a5 commit 432632c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ More detailed information about incremental changes can be found in the
- New option `mumps_mpi_communicator` to specify the MPI communicator when using
an MPI-enabled build of MUMPS [#790, by Alex Tyler Chapman].
- Updated build system to current autotools versions; initial support for icx/ifx and flang
- Removed use of `vsprintf`.
- Removed use of `vsprintf` and `sprintf`. Added `IpoptData::Append_info_string(std::string,double)`.

### 3.14.16 (2024-04-22)

Expand Down
4 changes: 2 additions & 2 deletions contrib/sIPOPT/src/SensAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "SensAlgorithm.hpp"
#include "SensUtils.hpp"
#include "IpSmartPtr.hpp"

#include "IpVector.hpp"
#include "IpUtils.hpp"

namespace Ipopt
{
Expand Down Expand Up @@ -243,7 +243,7 @@ SensAlgorithmExitStatus SensAlgorithm::ComputeSensitivityMatrix(void)
}
}

sprintf(buffer, "Column %" IPOPT_INDEX_FORMAT, idx_ipopt[Scol]);
Snprintf(buffer, sizeof(buffer), "Column %" IPOPT_INDEX_FORMAT, idx_ipopt[Scol]);

sens_step_calc_->SetSchurDriver(driver_vec_[0]);

Expand Down
8 changes: 2 additions & 6 deletions src/Algorithm/Inexact/IpInexactLSAcceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,11 @@ char InexactLSAcceptor::UpdateForNextIteration(
if( last_nu_ != nu_ )
{
info_alpha_primal_char = 'n';
char snu[40];
sprintf(snu, " nu=%8.2e", nu_);
IpData().Append_info_string(snu);
IpData().Append_info_string(" nu", nu_);
}
if( flexible_penalty_function_ && last_nu_low_ != nu_low_ )
{
char snu[40];
sprintf(snu, " nl=%8.2e", nu_low_);
IpData().Append_info_string(snu);
IpData().Append_info_string(" nl", nu_low_);
if( info_alpha_primal_char == 'k' )
{
info_alpha_primal_char = 'l';
Expand Down
13 changes: 13 additions & 0 deletions src/Algorithm/IpIpoptData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "IpRegOptions.hpp"
#include "IpTimingStatistics.hpp"

#include <sstream>
#include <iomanip>

namespace Ipopt
{

Expand Down Expand Up @@ -519,6 +522,16 @@ class IPOPTLIB_EXPORT IpoptData: public ReferencedObject
{
info_string_ += add_str;
}
/// @since 3.14.17
void Append_info_string(
const std::string& add_str,
double value
)
{
std::ostringstream sstream;
sstream << add_str << '=' << std::setw(8) << std::setprecision(2) << std::scientific << value;
info_string_ += sstream.str();
}
const std::string& info_string() const
{
return info_string_;
Expand Down
7 changes: 2 additions & 5 deletions src/Algorithm/IpLoqoMuOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ bool LoqoMuOracle::CalculateMu(
" Barrier parameter proposed by LOQO rule is %lf\n", mu);

/*
char ssigma[40];
sprintf(ssigma, " sigma=%8.2e", sigma);
IpData().Append_info_string(ssigma);
sprintf(ssigma, " xi=%8.2e ", IpCq().curr_centrality_measure());
IpData().Append_info_string(ssigma);
IpData().Append_info_string(" sigma", sigma);
IpData().Append_info_string(" xi", IpCq().curr_centrality_measure());
*/

new_mu = Max(Min(mu_max, mu), mu_min);
Expand Down
4 changes: 1 addition & 3 deletions src/Algorithm/IpPenaltyLSAcceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ char PenaltyLSAcceptor::UpdateForNextIteration(
if( last_nu_ != nu_ )
{
info_alpha_primal_char = 'n';
char snu[40];
sprintf(snu, " nu=%8.2e", nu_);
IpData().Append_info_string(snu);
IpData().Append_info_string(" nu", nu_);
}
else
{
Expand Down
7 changes: 2 additions & 5 deletions src/Algorithm/IpProbingMuOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,8 @@ bool ProbingMuOracle::CalculateMu(
IpData().set_delta_aff(step);
IpData().SetHaveAffineDeltas(true);

char ssigma[40];
sprintf(ssigma, " sigma=%8.2e", sigma);
IpData().Append_info_string(ssigma);
//sprintf(ssigma, " xi=%8.2e ", IpCq().curr_centrality_measure());
//IpData().Append_info_string(ssigma);
IpData().Append_info_string(" sigma", sigma);
//IpData().Append_info_string(" xi", IpCq().curr_centrality_measure());

new_mu = Max(Min(mu, mu_max), mu_min);
return true;
Expand Down

0 comments on commit 432632c

Please sign in to comment.