Хотя я знаком с ООП и Python, я новичок в C#, поэтому изо всех сил пытаюсь заставить свой код работать. Я постоянно сталкиваюсь с этой ошибкой:
Код: Выделить всё
Unhandled exception. MathNet.Numerics.Optimization.IncompatibleObjectiveException: Gradient not supported in objective function, but required for BFGS minimization.
at MathNet.Numerics.Optimization.BfgsMinimizer.FindMinimum(IObjectiveFunction objective, Vector`1 initialGuess)
at Proj.PortfolioOptimizer.OptimizePortfolio(Double[] risks, Double[] returns, Double[] durations, Double targetRisk, Double targetReturn, Double targetDuration) in /Users/ally/Desktop/NewProj/NewProj/ClasseOptimisation.cs:line 44
at Program.$(String[] args) in /Users/ally/Desktop/NewProj/NewProj/Program.cs:line 38
Интернет-справка говорит, что необходимо обеспечить и то, и другое. Evaluate и EvaluateAt возвращают градиент, как мне кажется.
Вот мои классы ObjectiveFunction и ObjectiveFunctionPoint, оба наследуемые от IObjectiveFunction и IObjectiveFunctionPoint (которые, насколько я понял, являются интерфейсами).< /p>
Код: Выделить всё
public class ObjectiveFunction : IObjectiveFunction
{
private readonly List _durations;
private Vector _point;
private double _value;
private Vector _gradient;
public ObjectiveFunction(List durations)
{
_durations = durations ?? throw new ArgumentNullException(nameof(durations));
}
public void EvaluateAt(Vector point)
{
_point = point;
_value = Value(point);
_gradient = Gradient(point);
}
public Vector Point => _point;
double IObjectiveFunctionEvaluation.Value => _value;
Vector IObjectiveFunctionEvaluation.Gradient => _gradient;
public Matrix Hessian => null;
bool IObjectiveFunctionEvaluation.IsGradientSupported => true;
bool IObjectiveFunctionEvaluation.IsHessianSupported => false;
// Méthode pour calculer la valeur de la fonction objective
public double Value(Vector point)
{
double totalDuration = 0;
for (int i = 0; i < point.Count; i++)
{
totalDuration += point[i] * _durations[i];
}
return totalDuration;
}
// Méthode pour calculer le gradient de la fonction objective
public Vector Gradient(Vector point)
{
var gradient = Vector.Build.Dense(point.Count);
for (int i = 0; i < point.Count; i++)
{
gradient[i] = _durations[i];
}
return gradient;
}
public IObjectiveFunction CreateNew()
{
return new ObjectiveFunction(_durations);
}
public IObjectiveFunction Fork()
{
throw new NotImplementedException();
}
}
public class ObjectiveFunctionPoint : IObjectiveFunctionPoint, IObjectiveFunctionEvaluation
{
public Vector Point { get; private set; }
public double Value { get; private set; }
public Vector Gradient { get; private set; }
public Matrix Hessian => null; // Hessienne non supportée dans cet exemple
public ObjectiveFunctionPoint(Vector point, double value, Vector gradient)
{
Point = point;
Value = value;
Gradient = gradient;
}
double IObjectiveFunctionEvaluation.Value => Value;
Vector IObjectiveFunctionEvaluation.Gradient => Gradient;
bool IObjectiveFunctionEvaluation.IsGradientSupported => true;
bool IObjectiveFunctionEvaluation.IsHessianSupported => false;
public IObjectiveFunction CreateNew()
{
throw new NotImplementedException();
}
}
public interface IObjectiveFunctionPoint
{
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... dexception
Мобильная версия