File size: 6,349 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'    Optimization Classes
'    Copyright 2009-2014 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 System.Xml
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports Ciloci.Flee
Imports System.Linq

Namespace Flowsheet.Optimization

    <System.Serializable()> Public Class OptimizationCase

        Implements ICloneable, Interfaces.ICustomXMLSerialization

        Public name As String = ""
        Public description As String = ""
        <Xml.Serialization.XmlIgnore()> Public results As ArrayList
        Public stats As String = ""

        Public solvm As SolvingMethod = SolvingMethod.AL_BRENT
        Public maxits As Integer = 100
        Public tolerance As Double = 0.000001
        Public epsX As Double = 0.001
        Public epsF As Double = 0.001
        Public epsG As Double = 0.001
        Public epsilon As Double = 0.001
        Public barriermultiplier As Double = 0.0001
        Public numdevscheme = 2

        Public boundvariables As Boolean = False

        Public objfunctype As OPTObjectiveFunctionType = OPTObjectiveFunctionType.Variable
        Public type As OPTType = OPTType.Minimization

        Public expression As String = ""
        <System.NonSerialized()> Public exbase As IGenericExpression(Of Double)
        <System.NonSerialized()> Public econtext As ExpressionContext

        Public variables As New Dictionary(Of String, OPTVariable)

        Public Enum SolvingMethod
            AL_BRENT = 0
            AL_BRENT_B = 1
            AL_LBFGS = 2
            AL_LBFGS_B = 3
            DN_TRUNCATED_NEWTON = 4
            DN_NELDERMEAD_SIMPLEX = 5
            DN_LBFGS = 6
            DN_TRUNCATED_NEWTON_B = 7
            DN_NELDERMEAD_SIMPLEX_B = 8
            DN_LBFGS_B = 9
            IPOPT = 10
        End Enum

        Sub New()

            variables = New Dictionary(Of String, OPTVariable)
            results = New ArrayList()

        End Sub

        Public Function Clone() As Object Implements System.ICloneable.Clone

            Return ObjectCopy(Me)

        End Function

        Function ObjectCopy(ByVal obj As Object) As Object

            Dim objMemStream As New MemoryStream(50000)
            Dim objBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))

            objBinaryFormatter.Serialize(objMemStream, obj)
            objMemStream.Seek(0, SeekOrigin.Begin)
            ObjectCopy = objBinaryFormatter.Deserialize(objMemStream)
            objMemStream.Close()

        End Function

        Public Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements Interfaces.ICustomXMLSerialization.LoadData

            XMLSerializer.XMLSerializer.Deserialize(Me, data, True)

            For Each xel As XElement In (From xel2 As XElement In data Select xel2 Where xel2.Name = "Variables").SingleOrDefault.Elements.ToList
                Dim optvar As New OPTVariable
                optvar.LoadData(xel.Elements.ToList)
                variables.Add(xel.@Key, optvar)
            Next

            Return True

        End Function

        Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData

            Dim elements As List(Of XElement) = XMLSerializer.XMLSerializer.Serialize(Me, True)

            With elements
                .Add(New XElement("Variables"))
                For Each kvp As KeyValuePair(Of String, OPTVariable) In variables
                    .Item(.Count - 1).Add(New XElement("Variable", New XAttribute("Key", kvp.Key), kvp.Value.SaveData.ToArray))
                Next
            End With

            Return elements

        End Function

    End Class

    <System.Serializable()> Public Class OPTVariable

        Implements Interfaces.ICustomXMLSerialization

        Public objectID As String = ""
        Public objectTAG As String = ""
        Public propID As String = ""
        Public unit As String = ""
        Public name As String = ""
        Public id As String = ""
        Public lowerlimit As Nullable(Of Double)
        Public upperlimit As Nullable(Of Double)
        Public currentvalue As Double = 0.0#
        Public initialvalue As Double = 0.0#
        Public type As OPTVariableType = OPTVariableType.Independent
        Public boundtype As BoundType = boundtype.None

        Sub New()

        End Sub

        Public Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements Interfaces.ICustomXMLSerialization.LoadData
            XMLSerializer.XMLSerializer.Deserialize(Me, data, True)
            Return True

        End Function

        Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData
            Return XMLSerializer.XMLSerializer.Serialize(Me, True)
        End Function

    End Class

    Public Enum OPTVariableType
        Dependent = 0
        Independent = 1
        Auxiliary = 2
        Constraint = 3
    End Enum

    Public Enum OPTObjectiveFunctionType
        Variable = 0
        Expression = 1
    End Enum

    Public Enum OPTType
        Minimization = 0
        Maximization = 1
    End Enum

    Public Enum BoundType
        None = 0
        Lower = 1
        Upper = 3
        LowerAndUpper = 2
    End Enum

End Namespace