Skip to content

Commit

Permalink
Further cleanups #14
Browse files Browse the repository at this point in the history
  • Loading branch information
anders9ustafsson committed Aug 26, 2023
1 parent 5e3fafc commit fef7779
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 119 deletions.
100 changes: 29 additions & 71 deletions csnumerics/Optimizers/Bobyqa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,89 +144,53 @@ public Bobyqa(int n, BobyqaObjectiveFunctionDelegate calfun, double[]? xl = null
/// </summary>
public int MaximumFunctionCalls
{
get
{
return this._maxfun;
}
set
{
this._maxfun = value;
}
get => _maxfun;
set => _maxfun = value;
}

/// <summary>
/// Gets or sets the print level to the logger.
/// </summary>
public int PrintLevel
{
get
{
return this._iprint;
}
set
{
this._iprint = value;
}
get => _iprint;
set => _iprint = value;
}

/// <summary>
/// Gets or sets the logger to which the optimizer log information should be sent.
/// </summary>
public TextWriter? Logger
{
get
{
return this._logger;
}
set
{
this._logger = value;
}
get => _logger;
set => _logger = value;
}

/// <summary>
/// Gets or sets the number of interpolation conditions.
/// </summary>
public int InterpolationConditions
{
get
{
return this._npt;
}
set
{
this._npt = value;
}
get => _npt;
set => _npt = value;
}

/// <summary>
/// Gets or sets the final value of the trust region radius.
/// </summary>
public double TrustRegionRadiusEnd
{
get
{
return this._rhoend;
}
set
{
this._rhoend = value;
}
get => _rhoend;
set => _rhoend = value;
}

/// <summary>
/// Gets or sets the start value of the trust region radius.
/// </summary>
public double TrustRegionRadiusStart
{
get
{
return this._rhobeg;
}
set
{
this._rhobeg = value;
}
get => _rhobeg;
set => _rhobeg = value;
}

#endregion
Expand Down Expand Up @@ -499,9 +468,8 @@ private static OptimizationSummary BOBYQB(Func<int, double[], double> 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)
Expand Down Expand Up @@ -566,8 +534,7 @@ private static OptimizationSummary BOBYQB(Func<int, double[], double> 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)
Expand Down Expand Up @@ -973,7 +940,7 @@ private static OptimizationSummary BOBYQB(Func<int, double[], double> 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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -1836,9 +1802,7 @@ private static void RESCUE(Func<int, double[], double> 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;
Expand Down Expand Up @@ -1906,32 +1870,26 @@ private static void RESCUE(Func<int, double[], double> 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;
w[ndim + knew] = ZERO;
--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]);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
66 changes: 20 additions & 46 deletions csnumerics/Optimizers/Cobyla.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ReSharper disable CommentTypo
/*
* Copyright (c) 2012-2022, Cureos AB.
* All rights reserved.
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -108,74 +113,44 @@ public Cobyla(int n, int m, CalcfcDelegate calcfc)
/// </summary>
public int MaximumFunctionCalls
{
get
{
return this._maxfun;
}
set
{
this._maxfun = value;
}
get => _maxfun;
set => _maxfun = value;
}

/// <summary>
/// Gets or sets the print level to the logger.
/// </summary>
public int PrintLevel
{
get
{
return this._iprint;
}
set
{
this._iprint = value;
}
get => _iprint;
set => _iprint = value;
}

/// <summary>
/// Gets or sets the logger to which the optimizer log information should be sent.
/// </summary>
public TextWriter? Logger
{
get
{
return this._logger;
}
set
{
this._logger = value;
}
get => _logger;
set => _logger = value;
}

/// <summary>
/// Gets or sets the final value of the trust region radius.
/// </summary>
public double TrustRegionRadiusStart
{
get
{
return this._rhobeg;
}
set
{
this._rhobeg = value;
}
get => _rhobeg;
set => _rhobeg = value;
}

/// <summary>
/// Gets or sets the start value of the trust region radius.
/// </summary>
public double TrustRegionRadiusEnd
{
get
{
return this._rhoend;
}
set
{
this._rhoend = value;
}
get => _rhoend;
set => _rhoend = value;
}

#endregion
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -1347,8 +1321,8 @@ internal static T[] PART<T>(this IList<T> 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion csnumerics/Optimizers/IOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Cureos.Numerics.Optimizers
/// <summary>
/// General interface for optimizers in the CS Numerics class library.
/// </summary>
interface IOptimizer
public interface IOptimizer
{
#region PROPERTIES

Expand Down
2 changes: 1 addition & 1 deletion csnumerics/Optimizers/ITrustRegionOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Cureos.Numerics.Optimizers
/// <summary>
/// Interface for derivative-free optimizers following the basic principles of M.J.D. Powell's trust region optimizers.
/// </summary>
internal interface ITrustRegionOptimizer : IOptimizer
public interface ITrustRegionOptimizer : IOptimizer
{
/// <summary>
/// Gets or sets the final value of the trust region radius.
Expand Down

0 comments on commit fef7779

Please sign in to comment.