-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.jl
180 lines (153 loc) · 6.57 KB
/
alg.jl
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
include("src/helperFunctions.jl")
function create_algorithm_settings(;
problemType = "Unconstrained",
# problemType = "Constrained",
# problemType = "ECQP",
# method = "AugmentedLagrangian",
# method = "ConjugateGradientDescent",
# method = "GeneticAlgorithm",
# method = "GradientDescent",
# method = "NelderMead",
method="QuasiNewton",
maxiter=Int(1e4),
gtol=1e-12,
dftol=1e-15,
dxtol=1e-10,
lambda=1,
lambdaMax=100,
linesearch="StrongWolfe",
c1=1e-4,
c2=0.9,
progress=100,
kwargs...
)
# Create a dictionary with default values
alg_settings = Dict(
:problemType => problemType,
:method => method,
:maxiter => maxiter,
:gtol => gtol,
:dftol => dftol,
:dxtol => dxtol,
:lambda => lambda,
:lambdaMax => lambdaMax,
:linesearch => linesearch,
:c1 => c1,
:c2 => c2,
:progress => progress
)
# Update dictionary with any keyword arguments provided
for (key, value) in kwargs
alg_settings[key] = value
end
if alg_settings[:problemType] == "Constrained"
alg_settings[:method] = "AugmentedLagrangian"
elseif alg_settings[:problemType] == "LP"
alg_settings[:method] = "LPSolver"
elseif alg_settings[:problemType] == "ECQP"
alg_settings[:method] = "ProjectedGradientCG"
elseif alg_settings[:problemType] == "QP"
alg_settings[:method] = "ActiveSetQP"
elseif alg_settings[:problemType] == "Unconstrained"
# don't change method from whatever I specified
if alg_settings[:method] == "ProjectedGradientCG" # by mistake
alg_settings[:method] = "QuasiNewton"
end
else
@error "Unknown method"
end
# Logic for method-specific modifications, similar to your original struct
if alg_settings[:method] == "ActiveSetQP"
alg_settings[:linesearch] = "NA"
alg_settings[:gtol] = "NA"
alg_settings[:c1] = "NA"
alg_settings[:c2] = "NA"
alg_settings[:lambda] = "NA"
alg_settings[:lambdaMax] = "NA"
alg_settings[:etol] = 1e-8
alg_settings[:itol] = 1e-8
alg_settings[:progress] = 1
alg_settings[:maxiter] = 10
elseif alg_settings[:method] == "AugmentedLagrangian"
alg_settings[:linesearch] = "NA"
# alg_settings[:gtol] = "NA"
# alg_settings[:c1] = "NA"
# alg_settings[:c2] = "NA"
# alg_settings[:lambda] = "NA"
# alg_settings[:lambdaMax] = "NA"
alg_settings[:etol] = 1e-8
alg_settings[:gtol] = 1e-8
alg_settings[:dxtol] = 1e-8
alg_settings[:mutol] = 1e6
alg_settings[:progress] = 1
alg_settings[:maxiter] = 100
elseif alg_settings[:method] == "ConjugateGradientDescent"
# Adjustments for ConjugateGradientDescent
alg_settings[:linesearch] = "StrongWolfe"
alg_settings[:c2] = 0.5
alg_settings[:progress] = 1
elseif alg_settings[:method] == "GradientDescent"
# Specific adjustments for GradientDescent
alg_settings[:progress] = 100
elseif alg_settings[:method] == "GeneticAlgorithm"
alg_settings[:maxiter] = 10000
# alg_settings[:maxiter] = 10
alg_settings[:progress] = alg_settings[:maxiter] / 10
# alg_settings[:progress] = 1
alg_settings[:linesearch] = "NA"
alg_settings[:gtol] = "NA"
alg_settings[:c1] = "NA"
alg_settings[:c2] = "NA"
alg_settings[:lambda] = "NA"
alg_settings[:lambdaMax] = "NA"
alg_settings[:fvalRepeatTol] = alg_settings[:maxiter] / 10
alg_settings[:popSize] = 10
alg_settings[:delta] = 0.3 # probability of mutation for each dimension of a point
alg_settings[:Dist] = randn # probability distribution to choose value from for mutation
# randn() has a mean of zero and a stddev of 1.
alg_settings[:deviation] = 0.1 # magnitude of mutation allowed
alg_settings[:parentsSurvive] = true
# alg_settings[:parentsSurvive] = false
elseif alg_settings[:method] == "LPSolver"
println("Utilizing JuMP+HiGHS to solve the LP Problem.")
elseif alg_settings[:method] == "NelderMead"
# Adjustments for NelderMead
alg_settings[:linesearch] = "NA"
alg_settings[:gtol] = "NA"
alg_settings[:c1] = "NA"
alg_settings[:c2] = "NA"
alg_settings[:lambda] = "NA"
alg_settings[:lambdaMax] = "NA"
alg_settings[:DeltaTol] = 1e-12
alg_settings[:Delta] = 100.0
alg_settings[:progress] = 10
alg_settings[:alpha] = 1.0
alg_settings[:beta] = 0.5
alg_settings[:gamma] = 2.0
alg_settings[:delta] = 0.5
elseif alg_settings[:method] == "ProjectedGradientCG"
alg_settings[:linesearch] = "NA"
alg_settings[:gtol] = "NA"
alg_settings[:c1] = "NA"
alg_settings[:c2] = "NA"
alg_settings[:lambda] = "NA"
alg_settings[:lambdaMax] = "NA"
alg_settings[:etol] = 1e-8
alg_settings[:progress] = 1
elseif alg_settings[:method] == "QuasiNewton"
# Adjustments for QuasiNewton
alg_settings[:lambdaMax] = 1
alg_settings[:linesearch] = "StrongWolfe"
alg_settings[:progress] = 1
elseif alg_settings[:method] == "TrustRegion"
# Adjustments for TrustRegion
alg_settings[:linesearch] = "QuasiNewton-SR1"
alg_settings[:c2] = -1.0
alg_settings[:progress] = 1
else
@warn "Bad condition."
end
return alg_settings
end
# Example usage
# alg = create_algorithm_settings()