-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patht_floats_test.go
190 lines (156 loc) · 4.3 KB
/
t_floats_test.go
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
187
188
189
190
// Copyright 2015 The Goga Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goga
import (
"math"
"testing"
"github.com/cpmech/gosl/chk"
"github.com/cpmech/gosl/io"
"github.com/cpmech/gosl/plt"
)
func Test_flt01(tst *testing.T) {
//verbose()
chk.PrintTitle("flt01. sin⁶(5 π x) multimodal")
// parameters
var opt Optimiser
opt.Default()
opt.Nsol = 20
opt.Ncpu = 1
opt.FltMin = []float64{0}
opt.FltMax = []float64{1}
nf, ng, nh := 1, 0, 0
// initialise optimiser
yfcn := func(x float64) float64 { return math.Pow(math.Sin(5.0*math.Pi*x), 6.0) }
opt.Init(GenTrialSolutions, nil, func(f, g, h, x []float64, y []int, cpu int) {
f[0] = -yfcn(x[0])
}, nf, ng, nh)
// initial solutions
sols0 := opt.GetSolutionsCopy()
// solve
opt.Solve()
// plot
if chk.Verbose {
pp := NewPlotParams(false)
pp.FnKey = "fig-flt01"
pp.YfuncX = yfcn
opt.PlotFltOva(sols0, 0, 0, -1, pp)
}
}
func Test_flt02(tst *testing.T) {
//verbose()
chk.PrintTitle("flt02. quadratic function with inequalities")
// parameters
var opt Optimiser
opt.Default()
opt.Nsol = 20
opt.Ncpu = 1
opt.FltMin = []float64{-2, -2}
opt.FltMax = []float64{2, 2}
nf, ng, nh := 1, 5, 0
// initialise optimiser
opt.Init(GenTrialSolutions, nil, func(f, g, h, x []float64, y []int, cpu int) {
f[0] = x[0]*x[0]/2.0 + x[1]*x[1] - x[0]*x[1] - 2.0*x[0] - 6.0*x[1]
g[0] = 2.0 - x[0] - x[1] // ≥ 0
g[1] = 2.0 + x[0] - 2.0*x[1] // ≥ 0
g[2] = 3.0 - 2.0*x[0] - x[1] // ≥ 0
g[3] = x[0] // ≥ 0
g[4] = x[1] // ≥ 0
}, nf, ng, nh)
// initial solutions
sols0 := opt.GetSolutionsCopy()
// solve
opt.Solve()
// log
io.Pforan("%v\n", opt.LogParams())
// plot
if chk.Verbose {
pp := NewPlotParams(false)
pp.FnKey = "fig-flt02"
opt.PlotFltFltContour(sols0, 0, 1, 0, pp)
}
}
func Test_flt03(tst *testing.T) {
//verbose()
chk.PrintTitle("flt03. circle with equality constraint")
// geometry
xe := 1.0 // centre of circle
le := -0.4 // selected level of f(x)
ys := xe - (1.0+le)/math.Sqrt2 // coordinates of minimum point with level=le
y0 := 2.0*ys + xe // vertical axis intersect of straight line defined by c(x)
xc := []float64{xe, xe} // centre
// parameters
var opt Optimiser
opt.Default()
opt.Nsol = 20
opt.Ncpu = 1
opt.Verbose = false
opt.FltMin = []float64{-1, -1}
opt.FltMax = []float64{3, 3}
nf, ng, nh := 1, 0, 1
// initialise optimiser
opt.Init(GenTrialSolutions, nil, func(f, g, h, x []float64, y []int, cpu int) {
res := 0.0
for i := 0; i < len(x); i++ {
res += (x[i] - xc[i]) * (x[i] - xc[i])
}
f[0] = math.Sqrt(res) - 1.0
h[0] = x[0] + x[1] + xe - y0
}, nf, ng, nh)
// initial solutions
sols0 := opt.GetSolutionsCopy()
// solve
opt.Solve()
// plot
if chk.Verbose {
pp := NewPlotParams(false)
pp.FnKey = "fig-flt03"
pp.AxEqual = true
plt.Reset(false, nil)
opt.PlotFltFltContour(sols0, 0, 1, 0, pp)
}
}
func Test_flt04(tst *testing.T) {
//verbose()
chk.PrintTitle("flt04. two-bar truss. Pareto-optimal")
// data. from Coelho (2007) page 19
ρ := 0.283 // lb/in³
H := 100.0 // in
P := 1e4 // lb
E := 3e7 // lb/in²
σ0 := 2e4 // lb/in²
// parameters
var opt Optimiser
opt.Default()
opt.Nsol = 30
opt.Ncpu = 1
opt.Tmax = 100
opt.LatinDup = 5
opt.FltMin = []float64{0.1, 0.5}
opt.FltMax = []float64{2.25, 2.5}
nf, ng, nh := 2, 2, 0
// initialise optimiser
TSQ2 := 2.0 * math.Sqrt2
opt.Init(GenTrialSolutions, nil, func(f, g, h, x []float64, y []int, cpu int) {
f[0] = 2.0 * ρ * H * x[1] * math.Sqrt(1.0+x[0]*x[0])
f[1] = P * H * math.Pow(1.0+x[0]*x[0], 1.5) * math.Sqrt(1.0+math.Pow(x[0], 4.0)) / (TSQ2 * E * x[0] * x[0] * x[1])
g[0] = σ0 - P*(1.0+x[0])*math.Sqrt(1.0+x[0]*x[0])/(TSQ2*x[0]*x[1])
g[1] = σ0 - P*(1.0-x[0])*math.Sqrt(1.0+x[0]*x[0])/(TSQ2*x[0]*x[1])
}, nf, ng, nh)
// initial solutions
sols0 := opt.GetSolutionsCopy()
// solve
opt.Solve()
// plot
if chk.Verbose {
_, dat := io.ReadTable("data/coelho-fig1.6.dat")
pp := NewPlotParams(false)
pp.FnKey = "fig-flt04"
pp.WithAll = true
pp.Extra = func() {
plt.Plot(dat["f1"], dat["f2"], &plt.A{C: "b", Ms: 3, Mec: "b"})
plt.AxisRange(0, 250, 0, 0.15)
}
opt.PlotOvaOvaPareto(sols0, 0, 1, pp)
}
}