This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH-820: Augmented Lagrangian to support linear constraints
- Loading branch information
Showing
7 changed files
with
185 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Sources/Accord.Math/Optimization/Constrained/Constraints/ConstraintExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Accord Math Library | ||
// The Accord.NET Framework | ||
// http://accord-framework.net | ||
// | ||
// Copyright © César Souza, 2009-2017 | ||
// cesarsouza at gmail.com | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
// | ||
|
||
namespace Accord.Math.Optimization | ||
{ | ||
using System; | ||
|
||
/// <summary> | ||
/// Extension methods on the <see cref="IConstraint"/> interface. | ||
/// </summary> | ||
public static class ConstraintExtensions | ||
{ | ||
|
||
/// <summary> | ||
/// Gets how much the constraint is being violated. | ||
/// </summary> | ||
/// <param name="constraint">The constraint.</param> | ||
/// <param name="input">The function point.</param> | ||
/// <returns> | ||
/// How much the constraint is being violated at the given point. Positive | ||
/// value means the constraint is not being violated with the returned slack, | ||
/// while a negative value means the constraint is being violated by the returned | ||
/// amount. | ||
/// </returns> | ||
public static double GetViolation(this IConstraint constraint, double[] input) | ||
{ | ||
double fx = constraint.Function(input); | ||
|
||
switch (constraint.ShouldBe) | ||
{ | ||
case ConstraintType.EqualTo: | ||
return Math.Abs(fx - constraint.Value); | ||
|
||
case ConstraintType.GreaterThanOrEqualTo: | ||
return fx - constraint.Value; | ||
|
||
case ConstraintType.LesserThanOrEqualTo: | ||
return constraint.Value - fx; | ||
} | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets whether this constraint is being violated | ||
/// (within the current tolerance threshold). | ||
/// </summary> | ||
/// <param name="constraint">The constraint.</param> | ||
/// <param name="input">The function point.</param> | ||
/// <returns> | ||
/// True if the constraint is being violated, false otherwise. | ||
/// </returns> | ||
public static bool IsViolated(this IConstraint constraint, double[] input) | ||
{ | ||
return constraint.GetViolation(input) + constraint.Tolerance < 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.