| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | Namespace MathEx.OptimizationL |
| |
|
| | Public Class PSO |
| |
|
| | Public Property Tolerance As Double = 0.0001 |
| |
|
| | Public Property MaxIterations As Integer = 1000 |
| |
|
| | Private _Iterations As Integer = 0 |
| |
|
| | Private fxb As Func(Of Double(), Double) |
| |
|
| | Private fxg As Func(Of Double(), Double()) |
| |
|
| | Private _error As Double |
| |
|
| | Private objval, objval0 As Double |
| |
|
| | Private Solutions As List(Of Double()) |
| |
|
| | Private FunctionValues As List(Of Double) |
| |
|
| | Public ReadOnly Property Iterations |
| | Get |
| | Return _Iterations |
| | End Get |
| | End Property |
| |
|
| | Sub New() |
| |
|
| | End Sub |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Public Function Solve(functionbody As Func(Of Double(), Double), functiongradient As Func(Of Double(), Double()), vars As Double(), Optional lbounds As Double() = Nothing, Optional ubounds As Double() = Nothing) As Double() |
| |
|
| | Dim obj As Double = 0.0# |
| |
|
| | Solutions = New List(Of Double()) |
| | FunctionValues = New List(Of Double) |
| |
|
| | fxb = functionbody |
| | fxg = functiongradient |
| |
|
| | If lbounds Is Nothing Then |
| | lbounds = vars.Clone() |
| | For i As Integer = 0 To lbounds.Length - 1 |
| | lbounds(i) = -1.0E+19 |
| | Next |
| | End If |
| |
|
| | If ubounds Is Nothing Then |
| | ubounds = vars.Clone() |
| | For i As Integer = 0 To ubounds.Length - 1 |
| | ubounds(i) = 1.0E+19 |
| | Next |
| | End If |
| |
|
| | Dim optimization As New LibOptimization.Optimization.clsOptPSO( |
| | New ObjectiveFunction(functionbody, functiongradient, 0.001, vars.Length)) |
| |
|
| | |
| | optimization.InitialPosition = vars |
| | optimization.InitialValueRangeLower = lbounds.Min |
| | optimization.InitialValueRangeUpper = ubounds.Max |
| |
|
| | |
| | optimization.Init() |
| | If optimization.IsRecentError() = True Then |
| | Throw New Exception("Optimization error") |
| | End If |
| |
|
| | Dim fval As Double |
| |
|
| | |
| | Dim it As Integer = 0 |
| | While (optimization.DoIteration(1) = False) |
| | it += 1 |
| | If it > MaxIterations Then |
| | Throw New Exception("Optimization error - max iterations reached") |
| | End If |
| | fval = optimization.Result.Eval |
| | If fval < Tolerance Then |
| | Exit While |
| | End If |
| | End While |
| |
|
| | |
| | Return optimization.Result.ToArray() |
| |
|
| | End Function |
| |
|
| | End Class |
| |
|
| |
|
| |
|
| | End Namespace |