Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
GH-688: Cobyla constraint definitions only work with constant values
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Jul 15, 2017
1 parent f8f3c6d commit 27c9eb1
Show file tree
Hide file tree
Showing 3 changed files with 670 additions and 535 deletions.
Expand Up @@ -29,6 +29,7 @@ namespace Accord.Math.Optimization
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;

/// <summary>
/// Constraint with only linear terms.
Expand Down Expand Up @@ -329,13 +330,61 @@ protected NonlinearConstraint()
}

var left = expression.Left;

// Try to determine the right side of the constraint expression. Normally
// this should be a constant, a variable (field), or a property. Other cases
// are not supported for the moment.

value = Double.NaN;
bool rightSideParsed = false;

var right = expression.Right as ConstantExpression;
if (right != null)
{
// Right side is a constant
value = (Double)right.Value;
rightSideParsed = true;
}
else
{
// Right side is not a constant, try to determine what it is
var variableRight = expression.Right as MemberExpression;
if (variableRight != null)
{
// Right side is a member variable: a field, a property or something else
var field = variableRight.Member as FieldInfo;
if (field != null)
{
// Right side is a field
var constant = variableRight.Expression as ConstantExpression;
value = (double)field.GetValue(constant.Value);
rightSideParsed = true;
}
else
{
// Right side is not a field, try to determine what it is
var prop = variableRight.Member as PropertyInfo;
if (prop != null)
{
// Right side is a property
var constant = variableRight.Expression as ConstantExpression;
value = (double)prop.GetValue(constant.Value);
rightSideParsed = true;
}
}
}
}

if (!rightSideParsed)
{
throw new ArgumentException("The right side of the expression could not be parsed. Please post the code you are trying to execute as a new issue in Accord.NET's issue tracker.");
}

var functionExpression = Expression.Lambda(left, constraint.Parameters.ToArray());

function = functionExpression.Compile() as Func<double[], double>;

value = (Double)right.Value;

}


Expand Down
Expand Up @@ -65,6 +65,18 @@ namespace Accord.Statistics.Models.Regression
/// Nonlinear Regression.
/// </summary>
///
/// <example>
/// <para>
/// The first example shows how to fit a non-linear regression with <see cref="LevenbergMarquardt"/>.</para>
/// <code source="Unit Tests\Accord.Tests.Statistics\Models\Regression\NonlinearLeastSquaresTest.cs" region="doc_learn_lm" />
///
/// <para>
/// The second example shows how to fit a non-linear regression with <see cref="GaussNewton"/>.</para>
/// <code source="Unit Tests\Accord.Tests.Statistics\Models\Regression\NonlinearLeastSquaresTest.cs" region="doc_learn_gn" />
/// </example>
///
/// <seealso cref="NonlinearLeastSquares"/>
///
[Serializable]
public class NonlinearRegression : TransformBase<double[], double>, ICloneable
{
Expand Down

0 comments on commit 27c9eb1

Please sign in to comment.