File size: 5,838 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'    DotNumerics Simplex Extender
'    Copyright 2015 Gregor Reichert, Daniel Wagner O. de Medeiros
'
'    This file is part of DWSIM.
'
'    DWSIM is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    DWSIM 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 General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with DWSIM.  If not, see <http://www.gnu.org/licenses/>.

Imports DotNumerics.Optimization

Public Module SimplexExtender

    Public Delegate Function ObjectiveFunction(ByVal x() As Double) As Double

    ''' <summary>
    ''' Simplified implementation of Nelder-Mead-Simplex-Downhill algorithm. No "Reduction" and no "Expansion" implemented yet.
    ''' https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method
    ''' </summary>
    ''' <param name="simplexsolver">simplex solver instance</param>
    ''' <param name="objfunc">objective function delegate</param>
    ''' <param name="Var">optimization variables</param>
    ''' <returns>the values of the variables that minimize the objective function value</returns>
    ''' <remarks></remarks>
    <System.Runtime.CompilerServices.Extension()> Public Function ComputeMin2(simplexsolver As Simplex, objfunc As ObjectiveFunction, ByVal Var() As OptBoundVariable) As Double()

        Dim cnt As Integer = 0
        Dim i, k, IdxMax As Integer
        Dim Dx, FVnew, LF, LF1 As Double
        Dim Beta As Double = 0.5
        'Dimension 0: point number
        'Dimension 1: coordinates of points; last column = 1 is indicating "at limiting border"
        Dim Points(Var.Length, Var.Length - 1) As Double
        Dim FuncVal(Var.Length) As Double
        Dim Pt(Var.Length - 1) As Double
        Dim CenterPt(Var.Length - 1) As Double

        'Calculate initial value
        For i = 0 To Var.Length - 1
            'Pt(i) = (Var(i).UpperBound - Var(i).LowerBound) / 2
            'Pt(i) = 0
            Pt(i) = Var(i).InitialGuess
        Next

        'Initialise points
        For k = 0 To Var.Length  'points
            For i = 0 To Var.Length - 1 'coordinates
                Points(k, i) = Pt(i)
            Next
        Next
        For k = 1 To Var.Length
            Dx = (Var(k - 1).UpperBound - Var(k - 1).LowerBound) / 10
            Points(k, k - 1) += Dx
        Next

        'Calculate point values
        For k = 0 To Var.Length
            For i = 0 To Var.Length - 1
                Pt(i) = Points(k, i)
            Next
            FuncVal(k) = objfunc(Pt)
        Next

        Do
            cnt += 1

            'Search worst value e.g. maximum Gibbs Enthalpy
            IdxMax = 0
            For k = 1 To Var.Length
                If FuncVal(k) > FuncVal(IdxMax) Then IdxMax = k
            Next

            'Calculate center point as average from all points except max
            For i = 0 To Var.Length - 1
                CenterPt(i) = 0
            Next
            For k = 0 To Var.Length
                If k <> IdxMax Then
                    For i = 0 To Var.Length - 1
                        CenterPt(i) += Points(k, i) / Var.Length
                    Next
                End If
            Next

            'reflect worst point at center point
            LF = 1
            For i = 0 To Var.Length - 1
                LF1 = 1
                Pt(i) = CenterPt(i) + CenterPt(i) - Points(IdxMax, i)
                'Check if point is inside bounds
                If Pt(i) < Var(i).LowerBound Then
                    LF1 = (Var(i).LowerBound - CenterPt(i)) / (Points(IdxMax, i) - CenterPt(i))
                End If
                If LF1 < LF Then LF = LF1
                If Pt(i) > Var(i).UpperBound Then
                    LF1 = (Var(i).UpperBound - CenterPt(i)) / (Points(IdxMax, i) - CenterPt(i))
                End If
                If LF1 < LF Then LF = -LF1
            Next
            If LF < 1 Then
                For i = 0 To Var.Length - 1
                    Pt(i) = CenterPt(i) + LF * (CenterPt(i) - Points(IdxMax, i))
                Next
            End If
            FVnew = objfunc(Pt)

            If FVnew < FuncVal(IdxMax) Then
                'replace worst point by new one
                FuncVal(IdxMax) = FVnew
                For i = 0 To Var.Length - 1
                    Points(IdxMax, i) = Pt(i)
                Next
            Else
                'contract worst point to center point
                For i = 0 To Var.Length - 1
                    Pt(i) = 0.5 * CenterPt(i) + 0.5 * Points(IdxMax, i)
                Next
                FVnew = objfunc(Pt)

                'check if solution is not improving anymore
                If FVnew > FuncVal(IdxMax) - simplexsolver.Tolerance Or cnt > simplexsolver.MaxFunEvaluations Then Exit Do

                'and replace worst value by contracted value
                FuncVal(IdxMax) = FVnew
                For i = 0 To Var.Length - 1
                    Points(IdxMax, i) = Pt(i)
                Next
            End If
        Loop

        'Search best value to be returned e.g. minimum Gibbs Enthalpy
        IdxMax = 0
        For k = 1 To Var.Length
            If FuncVal(k) < FuncVal(IdxMax) Then IdxMax = k
        Next
        For i = 0 To Var.Length - 1
            Pt(i) = Points(IdxMax, i)
        Next
        Return Pt
    End Function


End Module