Browse Source

Fixing non CLS-compliant code and malformed XML-comments

v3
Erik Ovegard 10 years ago
parent
commit
71ae7bae99
  1. 3
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  2. 1
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  3. 17
      src/Numerics/Optimization/LineSearch/WeakWolfeLineSearch.cs
  4. 90
      src/Numerics/Optimization/ObjectiveFunctions/ForwardDifferenceGradientObjectiveFunction.cs
  5. 32
      src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunction.cs
  6. 88
      src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunctionBase.cs
  7. 8
      src/UnitTests/OptimizationTests/TestFunctionAdapters.cs

3
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -1720,7 +1720,8 @@ namespace MathNet.Numerics.LinearAlgebra
/// matrix and a given other matrix being the 'x' of atan2 and the /// matrix and a given other matrix being the 'x' of atan2 and the
/// 'this' matrix being the 'y' /// 'this' matrix being the 'y'
/// </summary> /// </summary>
/// <param name="other"></param> /// <param name="other">The other matrix 'y'</param>
/// <param name="result">The matrix with the result and 'x'</param>
/// <returns></returns> /// <returns></returns>
public void PointwiseAtan2(Matrix<T> other, Matrix<T> result) public void PointwiseAtan2(Matrix<T> other, Matrix<T> result)
{ {

1
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -1028,6 +1028,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary> /// </summary>
/// <param name="f">Function which takes a scalar and a vector, modifies the vector in place and returns void</param> /// <param name="f">Function which takes a scalar and a vector, modifies the vector in place and returns void</param>
/// <param name="x">The scalar to be passed to the function</param> /// <param name="x">The scalar to be passed to the function</param>
/// <param name="result">The vector where the result will be placed</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception> /// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
protected void PointwiseBinary(Action<T, Vector<T>> f, T x, Vector<T> result) protected void PointwiseBinary(Action<T, Vector<T>> f, T x, Vector<T> result)
{ {

17
src/Numerics/Optimization/LineSearch/WeakWolfeLineSearch.cs

@ -1,4 +1,4 @@
// <copyright file="BfgsTest.cs" company="Math.NET"> // <copyright file="WeakWolfeLineSearch.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project // Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
@ -36,12 +36,12 @@ namespace MathNet.Numerics.Optimization.LineSearch
/// <summary> /// <summary>
/// Search for a step size alpha that satisfies the weak wolfe conditions. The weak Wolfe /// Search for a step size alpha that satisfies the weak wolfe conditions. The weak Wolfe
/// Conditions are /// Conditions are
/// i) Armijo Rule: f(x_k + alpha_k p_k) <= f(x_k) + c1 alpha_k p_k^T g(x_k) /// i) Armijo Rule: f(x_k + alpha_k p_k) &lt;= f(x_k) + c1 alpha_k p_k^T g(x_k)
/// ii) Curvature Condition: p_k^T g(x_k + alpha_k p_k) >= c2 p_k^T g(x_k) /// ii) Curvature Condition: p_k^T g(x_k + alpha_k p_k) &gt;= c2 p_k^T g(x_k)
/// where g(x) is the gradient of f(x), 0 < c1 < c2 < 1. /// where g(x) is the gradient of f(x), 0 &lt; c1 &lt; c2 &lt; 1.
/// ///
/// Implementation is based on http://www.math.washington.edu/~burke/crs/408/lectures/L9-weak-Wolfe.pdf /// Implementation is based on http://www.math.washington.edu/~burke/crs/408/lectures/L9-weak-Wolfe.pdf
/// ///
/// references: /// references:
/// http://en.wikipedia.org/wiki/Wolfe_conditions /// http://en.wikipedia.org/wiki/Wolfe_conditions
/// http://www.math.washington.edu/~burke/crs/408/lectures/L9-weak-Wolfe.pdf /// http://www.math.washington.edu/~burke/crs/408/lectures/L9-weak-Wolfe.pdf
@ -54,7 +54,10 @@ namespace MathNet.Numerics.Optimization.LineSearch
// Validation in base class // Validation in base class
} }
protected override MinimizationResult.ExitCondition WolfeExitCondition { get { return MinimizationResult.ExitCondition.WeakWolfeCriteria; } } protected override MinimizationResult.ExitCondition WolfeExitCondition
{
get { return MinimizationResult.ExitCondition.WeakWolfeCriteria; }
}
protected override bool WolfeCondition(double stepDd, double initialDd) protected override bool WolfeCondition(double stepDd, double initialDd)
{ {

90
src/Numerics/Optimization/ObjectiveFunctions/ForwardDifferenceGradientObjectiveFunction.cs

@ -19,66 +19,66 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public class ForwardDifferenceGradientObjectiveFunction : IObjectiveFunction public class ForwardDifferenceGradientObjectiveFunction : IObjectiveFunction
{ {
public IObjectiveFunction InnerObjectiveFunction { get; protected set; } public IObjectiveFunction InnerObjectiveFunction { get; protected set; }
protected Vector<double> _lower_bound; protected Vector<double> LowerBound { get; set; }
protected Vector<double> _upper_bound; protected Vector<double> UpperBound { get; set; }
protected Vector<double> _point; protected bool ValueEvaluated { get; set; } = false;
protected bool _value_evaluated = false; protected bool GradientEvaluated { get; set; } = false;
protected bool _gradient_evaluated = false; private Vector<double> _gradient;
protected Vector<double> _gradient;
public double MinimumIncrement { get; set; }
public double RelativeIncrement { get; set; }
public double MinimumIncrement;
public double RelativeIncrement;
public ForwardDifferenceGradientObjectiveFunction(IObjectiveFunction valueOnlyObj, Vector<double> lowerBound, Vector<double> upperBound, double relativeIncrement=1e-5, double minimumIncrement=1e-8) public ForwardDifferenceGradientObjectiveFunction(IObjectiveFunction valueOnlyObj, Vector<double> lowerBound, Vector<double> upperBound, double relativeIncrement=1e-5, double minimumIncrement=1e-8)
{ {
this.InnerObjectiveFunction = valueOnlyObj; InnerObjectiveFunction = valueOnlyObj;
_lower_bound = lowerBound; LowerBound = lowerBound;
_upper_bound = upperBound; UpperBound = upperBound;
_gradient = new LinearAlgebra.Double.DenseVector(_lower_bound.Count); _gradient = new LinearAlgebra.Double.DenseVector(LowerBound.Count);
this.RelativeIncrement = relativeIncrement; RelativeIncrement = relativeIncrement;
this.MinimumIncrement = minimumIncrement; MinimumIncrement = minimumIncrement;
} }
protected void EvaluateValue() protected void EvaluateValue()
{ {
_value_evaluated = true; ValueEvaluated = true;
} }
protected void EvaluateGradient() protected void EvaluateGradient()
{ {
if (!_value_evaluated) if (!ValueEvaluated)
this.EvaluateValue(); EvaluateValue();
var tmp_point = _point.Clone(); var tmp_point = Point.Clone();
var tmp_obj = this.InnerObjectiveFunction.CreateNew(); var tmp_obj = InnerObjectiveFunction.CreateNew();
for (int ii = 0; ii < _gradient.Count; ++ii) for (int ii = 0; ii < _gradient.Count; ++ii)
{ {
var orig_point = tmp_point[ii]; var orig_point = tmp_point[ii];
var rel_incr = orig_point * this.RelativeIncrement; var rel_incr = orig_point * RelativeIncrement;
var h = Math.Max(rel_incr, this.MinimumIncrement); var h = Math.Max(rel_incr, MinimumIncrement);
var mult = 1; var mult = 1;
if (orig_point + h > _upper_bound[ii]) if (orig_point + h > UpperBound[ii])
mult = -1; mult = -1;
tmp_point[ii] = orig_point + mult*h; tmp_point[ii] = orig_point + mult*h;
tmp_obj.EvaluateAt(tmp_point); tmp_obj.EvaluateAt(tmp_point);
double bumped_value = tmp_obj.Value; double bumped_value = tmp_obj.Value;
_gradient[ii] = (mult * bumped_value - mult * this.InnerObjectiveFunction.Value) / h; _gradient[ii] = (mult * bumped_value - mult * InnerObjectiveFunction.Value) / h;
tmp_point[ii] = orig_point; tmp_point[ii] = orig_point;
} }
_gradient_evaluated = true; GradientEvaluated = true;
} }
public Vector<double> Gradient public Vector<double> Gradient
{ {
get get
{ {
if (!_gradient_evaluated) if (!GradientEvaluated)
this.EvaluateGradient(); EvaluateGradient();
return _gradient; return _gradient;
} }
protected set { _gradient = value; }
} }
public Matrix<double> Hessian public Matrix<double> Hessian
@ -105,47 +105,41 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
} }
} }
public Vector<double> Point public Vector<double> Point { get; protected set; }
{
get
{
return _point;
}
}
public double Value public double Value
{ {
get get
{ {
if (!_value_evaluated) if (!ValueEvaluated)
this.EvaluateValue(); EvaluateValue();
return this.InnerObjectiveFunction.Value; return this.InnerObjectiveFunction.Value;
} }
} }
public IObjectiveFunction CreateNew() public IObjectiveFunction CreateNew()
{ {
var tmp = new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.CreateNew(), _lower_bound, _upper_bound, this.RelativeIncrement, this.MinimumIncrement); var tmp = new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.CreateNew(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement);
return tmp; return tmp;
} }
public void EvaluateAt(Vector<double> point) public void EvaluateAt(Vector<double> point)
{ {
_point = point; Point = point;
_value_evaluated = false; ValueEvaluated = false;
_gradient_evaluated = false; GradientEvaluated = false;
this.InnerObjectiveFunction.EvaluateAt(point); InnerObjectiveFunction.EvaluateAt(point);
} }
public IObjectiveFunction Fork() public IObjectiveFunction Fork()
{ {
var tmp = new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.Fork(), _lower_bound, _upper_bound, this.RelativeIncrement, this.MinimumIncrement); return new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.Fork(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement)
tmp._point = _point?.Clone(); {
tmp._gradient_evaluated = _gradient_evaluated; Point = Point?.Clone(),
tmp._value_evaluated = _value_evaluated; GradientEvaluated = GradientEvaluated,
tmp._gradient = _gradient?.Clone(); ValueEvaluated = ValueEvaluated,
_gradient = _gradient?.Clone()
return tmp; };
} }
} }
} }

32
src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunction.cs

@ -1,4 +1,34 @@
using System; // <copyright file="LazyObjectiveFunction.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
namespace MathNet.Numerics.Optimization.ObjectiveFunctions namespace MathNet.Numerics.Optimization.ObjectiveFunctions

88
src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunctionBase.cs

@ -1,4 +1,34 @@
using MathNet.Numerics.LinearAlgebra; // <copyright file="LazyObjectiveFunctionBase.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.LinearAlgebra;
namespace MathNet.Numerics.Optimization.ObjectiveFunctions namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
@ -6,14 +36,14 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
Vector<double> _point; Vector<double> _point;
protected bool _hasFunctionValue; protected bool HasFunctionValue { get; set; }
protected double _functionValue; protected double FunctionValue { get; set; }
protected bool _hasGradientValue; protected bool HasGradientValue { get; set; }
protected Vector<double> _gradientValue; protected Vector<double> GradientValue { get; set; }
protected bool _hasHessianValue; protected bool HasHessianValue { get; set; }
protected Matrix<double> _hessianValue; protected Matrix<double> HessianValue { get; set; }
protected LazyObjectiveFunctionBase(bool gradientSupported, bool hessianSupported) protected LazyObjectiveFunctionBase(bool gradientSupported, bool hessianSupported)
{ {
@ -27,13 +57,13 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
// we need to deep-clone values since they may be updated inplace on evaluation // we need to deep-clone values since they may be updated inplace on evaluation
LazyObjectiveFunctionBase fork = (LazyObjectiveFunctionBase)CreateNew(); LazyObjectiveFunctionBase fork = (LazyObjectiveFunctionBase)CreateNew();
fork._point = _point == null ? null : _point.Clone(); fork._point = _point?.Clone();
fork._hasFunctionValue = _hasFunctionValue; fork.HasFunctionValue = HasFunctionValue;
fork._functionValue = _functionValue; fork.FunctionValue = FunctionValue;
fork._hasGradientValue = _hasGradientValue; fork.HasGradientValue = HasGradientValue;
fork._gradientValue = _gradientValue == null ? null : _gradientValue.Clone(); ; fork.GradientValue = GradientValue?.Clone();
fork._hasHessianValue = _hasHessianValue; fork.HasHessianValue = HasHessianValue;
fork._hessianValue = _hessianValue == null ? null : _hessianValue.Clone(); fork.HessianValue = HessianValue?.Clone();
return fork; return fork;
} }
@ -43,9 +73,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public void EvaluateAt(Vector<double> point) public void EvaluateAt(Vector<double> point)
{ {
_point = point; _point = point;
_hasFunctionValue = false; HasFunctionValue = false;
_hasGradientValue = false; HasGradientValue = false;
_hasHessianValue = false; HasHessianValue = false;
} }
protected abstract void EvaluateValue(); protected abstract void EvaluateValue();
@ -69,16 +99,16 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
get get
{ {
if (!_hasFunctionValue) if (!HasFunctionValue)
{ {
EvaluateValue(); EvaluateValue();
} }
return _functionValue; return FunctionValue;
} }
protected set protected set
{ {
_functionValue = value; FunctionValue = value;
_hasFunctionValue = true; HasFunctionValue = true;
} }
} }
@ -86,16 +116,16 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
get get
{ {
if (!_hasGradientValue) if (!HasGradientValue)
{ {
EvaluateGradient(); EvaluateGradient();
} }
return _gradientValue; return GradientValue;
} }
protected set protected set
{ {
_gradientValue = value; GradientValue = value;
_hasGradientValue = true; HasGradientValue = true;
} }
} }
@ -103,16 +133,16 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
{ {
get get
{ {
if (!_hasHessianValue) if (!HasHessianValue)
{ {
EvaluateHessian(); EvaluateHessian();
} }
return _hessianValue; return HessianValue;
} }
protected set protected set
{ {
_hessianValue = value; HessianValue = value;
_hasHessianValue = true; HasHessianValue = true;
} }
} }
} }

8
src/UnitTests/OptimizationTests/TestFunctionAdapters.cs

@ -35,9 +35,9 @@ namespace MathNet.Numerics.UnitTests.OptimizationTests
{ {
if (this.IsGradientSupported) if (this.IsGradientSupported)
{ {
if (this._gradientValue == null) if (this.GradientValue == null)
this.Gradient = new DenseVector(this.TestFunction.ParameterDimension); this.Gradient = new DenseVector(this.TestFunction.ParameterDimension);
this.TestFunction.SsqGradientByRef(this.Point, _gradientValue); this.TestFunction.SsqGradientByRef(this.Point, GradientValue);
} }
} }
@ -45,9 +45,9 @@ namespace MathNet.Numerics.UnitTests.OptimizationTests
{ {
if (this.IsHessianSupported) if (this.IsHessianSupported)
{ {
if (this._hessianValue == null) if (this.HessianValue == null)
this.Hessian = new DenseMatrix(this.TestFunction.ParameterDimension, this.TestFunction.ParameterDimension); this.Hessian = new DenseMatrix(this.TestFunction.ParameterDimension, this.TestFunction.ParameterDimension);
this.TestFunction.SsqHessianByRef(this.Point, _hessianValue); this.TestFunction.SsqHessianByRef(this.Point, HessianValue);
} }
} }
} }

Loading…
Cancel
Save