diff --git a/csnumerics/Optimizers/Bobyqa.cs b/csnumerics/Optimizers/Bobyqa.cs index 5ab38c3..ad1b23d 100644 --- a/csnumerics/Optimizers/Bobyqa.cs +++ b/csnumerics/Optimizers/Bobyqa.cs @@ -28,6 +28,11 @@ using System; using System.IO; +// ReSharper disable IdentifierTypo +// ReSharper disable StringLiteralTypo +// ReSharper disable CommentTypo +// ReSharper disable CompareOfFloatsByEqualityOperator + namespace Cureos.Numerics.Optimizers { // ReSharper disable InconsistentNaming @@ -139,14 +144,8 @@ public Bobyqa(int n, BobyqaObjectiveFunctionDelegate calfun, double[]? xl = null /// public int MaximumFunctionCalls { - get - { - return this._maxfun; - } - set - { - this._maxfun = value; - } + get => _maxfun; + set => _maxfun = value; } /// @@ -154,14 +153,8 @@ public int MaximumFunctionCalls /// public int PrintLevel { - get - { - return this._iprint; - } - set - { - this._iprint = value; - } + get => _iprint; + set => _iprint = value; } /// @@ -169,14 +162,8 @@ public int PrintLevel /// public TextWriter? Logger { - get - { - return this._logger; - } - set - { - this._logger = value; - } + get => _logger; + set => _logger = value; } /// @@ -184,14 +171,8 @@ public TextWriter? Logger /// public int InterpolationConditions { - get - { - return this._npt; - } - set - { - this._npt = value; - } + get => _npt; + set => _npt = value; } /// @@ -199,14 +180,8 @@ public int InterpolationConditions /// public double TrustRegionRadiusEnd { - get - { - return this._rhoend; - } - set - { - this._rhoend = value; - } + get => _rhoend; + set => _rhoend = value; } /// @@ -214,14 +189,8 @@ public double TrustRegionRadiusEnd /// public double TrustRegionRadiusStart { - get - { - return this._rhobeg; - } - set - { - this._rhobeg = value; - } + get => _rhobeg; + set => _rhobeg = value; } #endregion @@ -499,9 +468,8 @@ private static OptimizationSummary BOBYQB(Func calfun, in // initial XOPT is set too. The branch to label 720 occurs if MAXFUN is // less than NPT. GOPT will be updated if KOPT is different from KBASE. - int nf, kopt; PRELIM(calfun,n, npt, x, xl, xu, rhobeg, iprint, maxfun, xbase, xpt, fval, gopt, hq, pq, bmat, zmat, ndim, sl, su, - out nf, out kopt, logger); + out var nf, out var kopt, logger); var xoptsq = ZERO; for (var i = 1; i <= n; ++i) @@ -566,8 +534,7 @@ private static OptimizationSummary BOBYQB(Func calfun, in L_60: var gnew = new double[1 + n]; - double dsq, crvmin; - TRSBOX(n, npt, xpt, xopt, gopt, hq, pq, sl, su, delta, xnew, d, gnew, out dsq, out crvmin); + TRSBOX(n, npt, xpt, xopt, gopt, hq, pq, sl, su, delta, xnew, d, gnew, out var dsq, out var crvmin); var dnorm = Math.Min(delta, Math.Sqrt(dsq)); if (dnorm < HALF * rho) @@ -973,7 +940,7 @@ private static OptimizationSummary BOBYQB(Func calfun, in // moved. Also update the second derivative terms of the model. var w = new double[1 + ndim]; - UPDATE(n, npt, bmat, zmat, ndim, vlag, beta, denom, knew, w); + UPDATE(n, npt, bmat, zmat, vlag, beta, denom, knew, w); var pqold = pq[knew]; pq[knew] = ZERO; @@ -1369,8 +1336,7 @@ private static void ALTMOV(int n, int npt, double[,] xpt, double[] xopt, double[ } } - // Search along each of the other lines through XOPT and another point. - + // Search along each of the other lines through XOPT and another point. } else { @@ -1836,9 +1802,7 @@ private static void RESCUE(Func calfun, int n, int npt, ptsaux[2, j] = Math.Max(-delta, sl[j]); if (ptsaux[1, j] + ptsaux[2, j] < ZERO) { - var temp = ptsaux[1, j]; - ptsaux[1, j] = ptsaux[2, j]; - ptsaux[2, j] = temp; + (ptsaux[1, j], ptsaux[2, j]) = (ptsaux[2, j], ptsaux[1, j]); } if (Math.Abs(ptsaux[2, j]) < HALF * Math.Abs(ptsaux[1, j])) ptsaux[2, j] = HALF * ptsaux[1, j]; for (var i = 1; i <= ndim; ++i) bmat[i, j] = ZERO; @@ -1906,15 +1870,11 @@ private static void RESCUE(Func calfun, int n, int npt, { for (var j = 1; j <= n; ++j) { - var temp = bmat[kold, j]; - bmat[kold, j] = bmat[knew, j]; - bmat[knew, j] = temp; + (bmat[kold, j], bmat[knew, j]) = (bmat[knew, j], bmat[kold, j]); } for (var j = 1; j <= nptm; ++j) { - var temp = zmat[kold, j]; - zmat[kold, j] = zmat[knew, j]; - zmat[knew, j] = temp; + (zmat[kold, j], zmat[knew, j]) = (zmat[knew, j], zmat[kold, j]); } ptsid[kold] = ptsid[knew]; ptsid[knew] = ZERO; @@ -1922,16 +1882,14 @@ private static void RESCUE(Func calfun, int n, int npt, --nrem; if (knew != kopt) { - var temp = vlag[kold]; - vlag[kold] = vlag[knew]; - vlag[knew] = temp; + (vlag[kold], vlag[knew]) = (vlag[knew], vlag[kold]); // Update the BMAT and ZMAT matrices so that the status of the KNEW-th // interpolation point can be changed from provisional to original. The // branch to label 350 occurs if all the original points are reinstated. // The nonnegative values of W(NDIM+K) are required in the search below. - UPDATE(n, npt, bmat, zmat, ndim, vlag, beta, denom, knew, w); + UPDATE(n, npt, bmat, zmat, vlag, beta, denom, knew, w); if (nrem == 0) return; for (var k = 1; k <= npt; ++k) w[ndim + k] = Math.Abs(w[ndim + k]); } @@ -2612,7 +2570,7 @@ private static void TRSBOX(int n, int npt, double[,] xpt, double[] xopt, double[ goto L_120; } - private static void UPDATE(int n, int npt, double[,] bmat, double[,] zmat, int ndim, double[] vlag, double beta, double denom, int knew, double[] w) + private static void UPDATE(int n, int npt, double[,] bmat, double[,] zmat, double[] vlag, double beta, double denom, int knew, double[] w) { // The arrays BMAT and ZMAT are updated, as required by the new position // of the interpolation point that has the index KNEW. The vector VLAG has @@ -2692,8 +2650,8 @@ private static void UPDATE(int n, int npt, double[,] bmat, double[,] zmat, int n private static string ToString(double[] x, int n) { var xstr = new string[n]; - for (var i = 0; i < n; ++i) xstr[i] = String.Format("{0,13:F6}", x[1 + i]); - return String.Concat(xstr); + for (var i = 0; i < n; ++i) xstr[i] = $"{x[1 + i],13:F6}"; + return string.Concat(xstr); } #endregion diff --git a/csnumerics/Optimizers/Cobyla.cs b/csnumerics/Optimizers/Cobyla.cs index 9d4da22..da4407f 100644 --- a/csnumerics/Optimizers/Cobyla.cs +++ b/csnumerics/Optimizers/Cobyla.cs @@ -1,3 +1,4 @@ +// ReSharper disable CommentTypo /* * Copyright (c) 2012-2022, Cureos AB. * All rights reserved. @@ -27,6 +28,10 @@ using System; using System.Collections.Generic; using System.IO; +// ReSharper disable CompareOfFloatsByEqualityOperator +// ReSharper disable CommentTypo +// ReSharper disable IdentifierTypo +// ReSharper disable StringLiteralTypo namespace Cureos.Numerics.Optimizers { @@ -108,14 +113,8 @@ public Cobyla(int n, int m, CalcfcDelegate calcfc) /// public int MaximumFunctionCalls { - get - { - return this._maxfun; - } - set - { - this._maxfun = value; - } + get => _maxfun; + set => _maxfun = value; } /// @@ -123,14 +122,8 @@ public int MaximumFunctionCalls /// public int PrintLevel { - get - { - return this._iprint; - } - set - { - this._iprint = value; - } + get => _iprint; + set => _iprint = value; } /// @@ -138,14 +131,8 @@ public int PrintLevel /// public TextWriter? Logger { - get - { - return this._logger; - } - set - { - this._logger = value; - } + get => _logger; + set => _logger = value; } /// @@ -153,14 +140,8 @@ public TextWriter? Logger /// public double TrustRegionRadiusStart { - get - { - return this._rhobeg; - } - set - { - this._rhobeg = value; - } + get => _rhobeg; + set => _rhobeg = value; } /// @@ -168,14 +149,8 @@ public double TrustRegionRadiusStart /// public double TrustRegionRadiusEnd { - get - { - return this._rhoend; - } - set - { - this._rhoend = value; - } + get => _rhoend; + set => _rhoend = value; } #endregion @@ -192,9 +167,8 @@ public OptimizationSummary FindMinimum(double[] x0) var nf = _maxfun; var status = COBYLA(_calcfc, _n, _m, x0, _rhobeg, _rhoend, _iprint, ref nf, _logger); - double f; var con = new double[_m]; - _calcfc(_n, _m, x0, out f, con); + _calcfc(_n, _m, x0, out var f, con); return new OptimizationSummary(status, nf, x0, f, con); } @@ -314,7 +288,7 @@ private static OptimizationStatus COBYLB( const double gamma = 0.5; const double delta = 1.1; - double f, resmax, total; + double resmax, total; var np = n + 1; var mp = m + 1; @@ -369,7 +343,7 @@ private static OptimizationStatus COBYLB( ++nfvals; - calcfc(n, m, x, out f, con); + calcfc(n, m, x, out var f, con); resmax = 0.0; for (var k = 1; k <= m; ++k) resmax = Math.Max(resmax, -con[k]); @@ -1347,8 +1321,8 @@ internal static T[] PART(this IList src, int from, int to) internal static string FORMAT(this double[] x) { var xStr = new string[x.Length]; - for (var i = 0; i < x.Length; ++i) xStr[i] = String.Format("{0,13:F6}", x[i]); - return String.Concat(xStr); + for (var i = 0; i < x.Length; ++i) xStr[i] = $"{x[i],13:F6}"; + return string.Concat(xStr); } } diff --git a/csnumerics/Optimizers/IOptimizer.cs b/csnumerics/Optimizers/IOptimizer.cs index a281cdf..f9de152 100644 --- a/csnumerics/Optimizers/IOptimizer.cs +++ b/csnumerics/Optimizers/IOptimizer.cs @@ -26,7 +26,7 @@ namespace Cureos.Numerics.Optimizers /// /// General interface for optimizers in the CS Numerics class library. /// - interface IOptimizer + public interface IOptimizer { #region PROPERTIES diff --git a/csnumerics/Optimizers/ITrustRegionOptimizer.cs b/csnumerics/Optimizers/ITrustRegionOptimizer.cs index 875f477..3ff9a63 100644 --- a/csnumerics/Optimizers/ITrustRegionOptimizer.cs +++ b/csnumerics/Optimizers/ITrustRegionOptimizer.cs @@ -24,7 +24,7 @@ namespace Cureos.Numerics.Optimizers /// /// Interface for derivative-free optimizers following the basic principles of M.J.D. Powell's trust region optimizers. /// - internal interface ITrustRegionOptimizer : IOptimizer + public interface ITrustRegionOptimizer : IOptimizer { /// /// Gets or sets the final value of the trust region radius.