Skip to content

Commit

Permalink
Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 25, 2024
1 parent bef0a99 commit 64762dd
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/algs/ags/solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace
const std::vector<double>& leftBound, const std::vector<double>& rightBound)
{
mFunctions = functions;
mConstraintsNumber = mFunctions.size() - 1;
mDimension = leftBound.size();
mConstraintsNumber = static_cast<unsigned int>(mFunctions.size() - 1);
mDimension = static_cast<unsigned int>(leftBound.size());
mLeftBound = leftBound;
mRightBound = rightBound;
}
Expand Down
6 changes: 3 additions & 3 deletions src/algs/bobyqa/bobyqa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1979,12 +1979,12 @@ static nlopt_result bobyqb_(int *n, int *npt, double *x,
int ksav;
double gqsq, dist, sumw, sumz, diffa, diffb, diffc = 0.0, hdiag;
int kbase;
double alpha, delta, adelt, denom, fsave, bdtol, delsq;
double alpha = 0.0, delta, adelt = 0.0, denom, fsave, bdtol, delsq;
int nresc, nfsav;
double ratio, dnorm, vquad, pqold, tenth;
double ratio = 0.0, dnorm, vquad, pqold, tenth;
int itest;
double sumpq, scaden;
double errbig, cauchy, fracsq, biglsq, densav;
double errbig, cauchy = 0.0, fracsq, biglsq, densav;
double bdtest;
double crvmin, frhosq;
double distsq;
Expand Down
2 changes: 1 addition & 1 deletion src/algs/cdirect/cdirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ nlopt_result cdirect(int n, nlopt_func f, void *f_data,
int i;

d.f = f; d.f_data = f_data; d.lb = lb; d.ub = ub;
d.x = (double *) malloc(sizeof(double) * n * (stop->xtol_abs ? 4 : 3));
d.x = (double *) calloc(n * (stop->xtol_abs ? 4 : 3), sizeof(double));
if (!d.x) return NLOPT_OUT_OF_MEMORY;

for (i = 0; i < n; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/algs/cdirect/hybrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ nlopt_result cdirect_hybrid(int n, nlopt_func f, void *f_data,
int i;

d.f = f; d.f_data = f_data; d.lb = lb; d.ub = ub;
d.x = (double *) malloc(sizeof(double) * n * (stop->xtol_abs ? 4 : 3));
d.x = (double *) calloc(n * (stop->xtol_abs ? 4 : 3), sizeof(double));
if (!d.x) return NLOPT_OUT_OF_MEMORY;

for (i = 0; i < n; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions src/algs/crs/crs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static int crs_compare(double *k1, double *k2)
{
if (*k1 < *k2) return -1;
if (*k1 > *k2) return +1;
return k1 - k2; /* tie-breaker */
return (int)(k1 - k2); /* tie-breaker */
}

/* set x to a random trial value, as defined by CRS:
Expand All @@ -67,7 +67,7 @@ static void random_trial(crs_data *d, double *x, rb_node *best)

/* initialize x to x_0 = best point */
memcpy(x, best->k + 1, sizeof(double) * n);
i0 = (best->k - ps) / n1;
i0 = (int)((best->k - ps) / n1);

jn = nlopt_iurand(n); /* which of remaining n points is "x_n",
i.e. which to reflect through ...
Expand Down
7 changes: 5 additions & 2 deletions src/algs/neldermead/nldrmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static int simplex_compare(double *k1, double *k2)
{
if (*k1 < *k2) return -1;
if (*k1 > *k2) return +1;
return k1 - k2; /* tie-breaker */
return (int)(k1 - k2); /* tie-breaker */
}

/* return 1 if a and b are approximately equal relative to floating-point
Expand Down Expand Up @@ -202,7 +202,10 @@ nlopt_result nldrmd_minimize_(int n, nlopt_func f, void *f_data,
for (i = 0; i < n; ++i) c[i] *= ninv;

/* x convergence check: find xcur = max radius from centroid */
memset(xcur, 0, sizeof(double)*n);
if (n > 0)
{
memset(xcur, 0, sizeof(double)*n);
}
for (i = 0; i < n + 1; ++i) {
double *xi = pts + i*(n+1) + 1;
for (j = 0; j < n; ++j) {
Expand Down
2 changes: 1 addition & 1 deletion src/algs/slsqp/slsqp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ static void lsq_(int *m, int *meq, int *n, int *nl,
im, ip, iu, iw;
double diag;
int mineq;
double xnorm;
double xnorm = 0.0;

/* MINIMIZE with respect to X */
/* ||E*X - F|| */
Expand Down
6 changes: 3 additions & 3 deletions src/algs/stogo/tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ bool TBox::CloseToMin(RVector &vec, double *objval, double eps_cl) {
return FALSE;
}

unsigned int TBox::NStationary() {
int TBox::NStationary() {
// Returns the number of trials in a box
return TList.size() ;
return static_cast<int>(TList.size());
}

void TBox::split(RTBox B1, RTBox B2) {
Expand All @@ -176,7 +176,7 @@ void TBox::split(RTBox B1, RTBox B2) {
B1.lb=lb; B1.ub=ub;
B2.lb=lb; B2.ub=ub;
w=LongestSide(&i);
ns=TList.size();
ns = static_cast<int>(TList.size());
switch (ns) {
case 0: case 1:
// Bisection
Expand Down
2 changes: 1 addition & 1 deletion src/algs/stogo/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class TBox: public VBox {
void ClearBox();
bool CloseToMin(RVector&, double*, double);

unsigned int NStationary(); // Returns the number of stationary points
int NStationary(); // Returns the number of stationary points

void split(RTBox, RTBox); // Split a box
void dispTrials();
Expand Down
8 changes: 4 additions & 4 deletions src/api/nlopt-in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ namespace nlopt {
d->mf = mf;
d->f_data = f_data;

mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d,
mythrow(nlopt_add_inequality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}

Expand Down Expand Up @@ -453,7 +453,7 @@ namespace nlopt {
d->mf = mf;
d->f_data = f_data;

mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d,
mythrow(nlopt_add_equality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}

Expand Down Expand Up @@ -489,7 +489,7 @@ namespace nlopt {
d->munge_destroy = md;
d->munge_copy = mc;

mythrow(nlopt_add_inequality_mconstraint(o, tol.size(), mymfunc, d,
mythrow(nlopt_add_inequality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}
void add_equality_mconstraint(mfunc mf, void *f_data,
Expand All @@ -501,7 +501,7 @@ namespace nlopt {
d->munge_destroy = md;
d->munge_copy = mc;

mythrow(nlopt_add_equality_mconstraint(o, tol.size(), mymfunc, d,
mythrow(nlopt_add_equality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
tol.empty() ? NULL : &tol[0]));
}

Expand Down
12 changes: 6 additions & 6 deletions src/api/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ static int elimdim_wrapcheck(nlopt_opt opt)

/*********************************************************************/

#define POP(defaultpop) (opt->stochastic_population > 0 ? opt->stochastic_population : (nlopt_stochastic_population > 0 ? nlopt_stochastic_population : (defaultpop)))
#define POP(defaultpop) (opt->stochastic_population > 0 ? (int)opt->stochastic_population : (nlopt_stochastic_population > 0 ? nlopt_stochastic_population : (defaultpop)))

/* unlike nlopt_optimize() below, only handles minimization case */
static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
Expand Down Expand Up @@ -532,7 +532,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
#ifdef NLOPT_CXX
if (!finite_domain(n, lb, ub))
RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop, algorithm == NLOPT_GD_STOGO ? 0 : (int) POP(2 * n)))
if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop, algorithm == NLOPT_GD_STOGO ? 0 : POP(2 * (int)n)))
return NLOPT_FAILURE;
break;
#else
Expand Down Expand Up @@ -605,7 +605,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
case NLOPT_GN_CRS2_LM:
if (!finite_domain(n, lb, ub))
RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, (int) POP(0), 0);
return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, POP(0), 0);

case NLOPT_G_MLSL:
case NLOPT_G_MLSL_LDS:
Expand Down Expand Up @@ -646,7 +646,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
nlopt_set_xtol_rel(local_opt, 1e-7);
}
push_force_stop_child(opt, local_opt);
ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop, local_opt, (int) POP(0), algorithm >= NLOPT_GN_MLSL_LDS && algorithm != NLOPT_G_MLSL);
ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop, local_opt, POP(0), algorithm >= NLOPT_GN_MLSL_LDS && algorithm != NLOPT_G_MLSL);
pop_force_stop_child(opt);
if (!opt->local_opt)
nlopt_destroy(local_opt);
Expand Down Expand Up @@ -675,7 +675,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
nlopt_set_ftol_abs(dual_opt, nlopt_get_param(opt, "dual_ftol_abs", LO(ftol_abs, 0.0)));
nlopt_set_xtol_rel(dual_opt, nlopt_get_param(opt, "dual_xtol_rel", 0.0));
nlopt_set_xtol_abs1(dual_opt, nlopt_get_param(opt, "dual_xtol_abs", 0.0));
nlopt_set_maxeval(dual_opt, nlopt_get_param(opt, "dual_maxeval", LO(maxeval, 100000)));
nlopt_set_maxeval(dual_opt, (int)nlopt_get_param(opt, "dual_maxeval", LO(maxeval, 100000)));
#undef LO
if (algorithm == NLOPT_LD_MMA)
ret = mma_minimize(n, f, f_data, opt->m, opt->fc, lb, ub, x, minf, &stop, dual_opt, inner_maxeval, (unsigned)verbosity, rho_init, opt->dx);
Expand Down Expand Up @@ -793,7 +793,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
case NLOPT_GN_ISRES:
if (!finite_domain(n, lb, ub))
RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
return isres_minimize(ni, f, f_data, (int) (opt->m), opt->fc, (int) (opt->p), opt->h, lb, ub, x, minf, &stop, (int) POP(0));
return isres_minimize(ni, f, f_data, (int) (opt->m), opt->fc, (int) (opt->p), opt->h, lb, ub, x, minf, &stop, POP(0));

case NLOPT_GN_ESCH:
if (!finite_domain(n, lb, ub))
Expand Down
6 changes: 4 additions & 2 deletions src/swig/nlopt-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ __version__ = str(_nlopt.version_major())+'.'+str(_nlopt.version_minor())+'.'+st
{
npy_intp sz = $1.size();
$result = PyArray_SimpleNew(1, &sz, NPY_DOUBLE);
std::memcpy(array_data($result), $1.empty() ? NULL : &$1[0],
sizeof(double) * sz);
if (!$1.empty())
{
std::memcpy(array_data($result), &$1[0], sizeof(double) * sz);
}
}

//////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/util/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ double nlopt_seconds(void)
gettimeofday(&tv, NULL);
return (tv.tv_sec - start.tv_sec) + 1.e-6 * (tv.tv_usec - start.tv_usec);
#elif defined(HAVE_TIME)
return time(NULL);
return (double)time(NULL);
#elif defined(_WIN32) || defined(__WIN32__)
static THREADLOCAL ULONGLONG start;
FILETIME ft;
Expand Down Expand Up @@ -82,7 +82,7 @@ unsigned long nlopt_time_seed(void)
gettimeofday(&tv, NULL);
return (tv.tv_sec ^ tv.tv_usec);
#elif defined(HAVE_TIME)
return time(NULL);
return (unsigned long)time(NULL);
#elif defined(_WIN32) || defined(__WIN32__)
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
Expand Down
2 changes: 1 addition & 1 deletion test/cpp_functor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class LinearRegression {
LinearRegression(QuadraticForm quadratic_form_) :
quadratic_form(std::move(quadratic_form_)) {}

double operator()(unsigned n, const double* x, double* grad) const
double operator()(unsigned /*n*/, const double* x, double* grad) const
{
const double result = quadratic_form.compute_form(x);
if (!!grad) {
Expand Down

0 comments on commit 64762dd

Please sign in to comment.