-
Notifications
You must be signed in to change notification settings - Fork 9
/
metrics.go
200 lines (181 loc) · 4.25 KB
/
metrics.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
191
192
193
194
195
196
197
198
199
200
// 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"
"github.com/cpmech/gosl/chk"
"github.com/cpmech/gosl/utl"
)
// Metrics holds metric data such as non-dominated Pareto fronts
type Metrics struct {
prms *Parameters // parameters
Omin []float64 // current min ova
Omax []float64 // current max ova
Fmin []float64 // current min float
Fmax []float64 // current max float
Imin []int // current min int
Imax []int // current max int
Fsizes []int // front sizes
Fronts [][]*Solution // non-dominated fronts
}
// Init initialises Metrics
func (o *Metrics) Init(nsol int, prms *Parameters) {
o.prms = prms
o.Omin = make([]float64, prms.Nova)
o.Omax = make([]float64, prms.Nova)
o.Fmin = make([]float64, prms.Nflt)
o.Fmax = make([]float64, prms.Nflt)
o.Imin = make([]int, prms.Nint)
o.Imax = make([]int, prms.Nint)
o.Fsizes = make([]int, nsol)
o.Fronts = make([][]*Solution, nsol)
for i := 0; i < nsol; i++ {
o.Fronts[i] = make([]*Solution, nsol)
}
}
// Compute computes limits, find non-dominated Pareto fronts, and compute crowd distances
func (o *Metrics) Compute(sols []*Solution) (nfronts int) {
// reset variables and find limits
z := o.Fsizes
nsol := len(sols)
for i, sol := range sols {
// reset values
sol.Nwins = 0
sol.Nlosses = 0
sol.FrontId = 0
sol.DistCrowd = 0
sol.DistNeigh = INF
z[i] = 0
// check oors
for j := 0; j < o.prms.Noor; j++ {
if math.IsNaN(sol.Oor[j]) {
chk.Panic("NaN found in out-of-range value array\n\txFlt = %v\n\txInt = %v\n\tova = %v\n\toor = %v", sol.Flt, sol.Int, sol.Ova, sol.Oor)
}
}
// ovas range
for j := 0; j < o.prms.Nova; j++ {
x := sol.Ova[j]
if math.IsNaN(x) {
chk.Panic("NaN found in objective value array\n\txFlt = %v\n\txInt = %v\n\tova = %v\n\toor = %v", sol.Flt, sol.Int, sol.Ova, sol.Oor)
}
if i == 0 {
o.Omin[j] = x
o.Omax[j] = x
} else {
o.Omin[j] = utl.Min(o.Omin[j], x)
o.Omax[j] = utl.Max(o.Omax[j], x)
}
}
// floats range
for j := 0; j < o.prms.Nflt; j++ {
x := sol.Flt[j]
if i == 0 {
o.Fmin[j] = x
o.Fmax[j] = x
} else {
o.Fmin[j] = utl.Min(o.Fmin[j], x)
o.Fmax[j] = utl.Max(o.Fmax[j], x)
}
}
// ints range
for j := 0; j < o.prms.Nint; j++ {
x := sol.Int[j]
if i == 0 {
o.Imin[j] = x
o.Imax[j] = x
} else {
o.Imin[j] = utl.Imin(o.Imin[j], x)
o.Imax[j] = utl.Imax(o.Imax[j], x)
}
}
}
// compute neighbour distance
for i := 0; i < nsol; i++ {
A := sols[i]
for j := i + 1; j < nsol; j++ {
B := sols[j]
o.closest(A, B)
}
}
// skip if single-objective problem
if o.prms.Nova < 2 {
return
}
// compute wins/losses data
for i := 0; i < nsol; i++ {
A := sols[i]
for j := i + 1; j < nsol; j++ {
B := sols[j]
A_win, B_win := A.Compare(B)
if A_win {
A.WinOver[A.Nwins] = B
A.Nwins++
B.Nlosses++
}
if B_win {
B.WinOver[B.Nwins] = A
B.Nwins++
A.Nlosses++
}
}
}
// first front
for _, sol := range sols {
if sol.Nlosses == 0 {
o.Fronts[0][z[0]] = sol
z[0]++
}
}
// next fronts
for r, front := range o.Fronts {
if z[r] == 0 {
break
}
s := r + 1
nfronts++
for i := 0; i < z[r]; i++ {
A := front[i]
for j := 0; j < A.Nwins; j++ {
B := A.WinOver[j]
B.Nlosses--
if B.Nlosses == 0 { // B belongs to next front
B.FrontId = s
o.Fronts[s][z[s]] = B
z[s]++
}
}
}
}
// crowd distances
for r := 0; r < nfronts; r++ {
l, m := z[r], z[r]-1
if l == 1 {
o.Fronts[r][0].DistCrowd = -1
continue
}
F := o.Fronts[r][:l]
for j := 0; j < o.prms.Nova; j++ {
sortByOva(F, j)
δ := o.Omax[j] - o.Omin[j] + 1e-15
F[0].DistCrowd = INF
F[m].DistCrowd = INF
for i := 1; i < m; i++ {
F[i].DistCrowd += ((F[i].Ova[j] - F[i-1].Ova[j]) / δ) * ((F[i+1].Ova[j] - F[i].Ova[j]) / δ)
}
}
}
return
}
// closest computes distance and set closest neighbours
func (o *Metrics) closest(A, B *Solution) {
dist := A.Distance(B, o.Fmin, o.Fmax, o.Imin, o.Imax)
if dist < A.DistNeigh {
A.DistNeigh = dist
A.Closest = B
}
if dist < B.DistNeigh {
B.DistNeigh = dist
B.Closest = A
}
}