File: listsearch_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (159 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download
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
// Copyright ©2018 The Gonum 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 optimize

import (
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/floats"
	"gonum.org/v1/gonum/mat"
	"gonum.org/v1/gonum/optimize/functions"
)

func TestListSearch(t *testing.T) {
	t.Parallel()
	rnd := rand.New(rand.NewSource(1))
	for cas, test := range []struct {
		r, c       int
		shortEvals int
		fun        func([]float64) float64
	}{
		{
			r:   100,
			c:   10,
			fun: functions.ExtendedRosenbrock{}.Func,
		},
	} {
		// Generate a random list of items.
		r, c := test.r, test.c
		locs := mat.NewDense(r, c, nil)
		for i := 0; i < r; i++ {
			for j := 0; j < c; j++ {
				locs.Set(i, j, rnd.NormFloat64())
			}
		}

		// Evaluate all of the items in the list and find the minimum value.
		fs := make([]float64, r)
		for i := 0; i < r; i++ {
			fs[i] = test.fun(locs.RawRowView(i))
		}
		minIdx := floats.MinIdx(fs)

		// Check that the global minimum is found under normal conditions.
		p := Problem{Func: test.fun}
		method := &ListSearch{
			Locs: locs,
		}
		settings := &Settings{
			Converger: NeverTerminate{},
		}
		initX := make([]float64, c)
		result, err := Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != MethodConverge {
			t.Errorf("cas %v: status should be MethodConverge", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdx)) {
			t.Errorf("cas %v: did not find minimum of whole list", cas)
		}

		// Check that the optimization works concurrently.
		concurrent := 6
		settings.Concurrent = concurrent
		result, err = Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != MethodConverge {
			t.Errorf("cas %v: status should be MethodConverge", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdx)) {
			t.Errorf("cas %v: did not find minimum of whole list concurrent", cas)
		}

		// Check that the optimization works concurrently with more than the number of samples.
		settings.Concurrent = test.r + concurrent
		result, err = Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != MethodConverge {
			t.Errorf("cas %v: status should be MethodConverge", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdx)) {
			t.Errorf("cas %v: did not find minimum of whole list concurrent", cas)
		}

		// Check that cleanup happens properly by setting the minimum location
		// to the last sample.
		swapSamples(locs, fs, minIdx, test.r-1)
		minIdx = test.r - 1
		settings.Concurrent = concurrent
		result, err = Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != MethodConverge {
			t.Errorf("cas %v: status should be MethodConverge", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdx)) {
			t.Errorf("cas %v: did not find minimum of whole list last sample", cas)
		}

		// Test that the correct optimum is found when the optimization ends early.
		// Note that the above test swapped the list minimum to the last sample,
		// so it's guaranteed that the minimum of the shortened list is not the
		// same as the minimum of the whole list.
		evals := test.r / 3
		minIdxFirst := floats.MinIdx(fs[:evals])
		settings.Concurrent = 0
		settings.FuncEvaluations = evals
		result, err = Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != FunctionEvaluationLimit {
			t.Errorf("cas %v: status was not FunctionEvaluationLimit", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdxFirst)) {
			t.Errorf("cas %v: did not find minimum of shortened list serial", cas)
		}

		// Test the same but concurrently. We can't guarantee a specific number
		// of function evaluations concurrently, so make sure that the list optimum
		// is not between [evals:evals+concurrent]
		for floats.MinIdx(fs[:evals]) != floats.MinIdx(fs[:evals+concurrent]) {
			// Swap the minimum index with a random element.
			minIdxFirst := floats.MinIdx(fs[:evals+concurrent])
			new := rnd.Intn(evals)
			swapSamples(locs, fs, minIdxFirst, new)
		}

		minIdxFirst = floats.MinIdx(fs[:evals])
		settings.Concurrent = concurrent
		result, err = Minimize(p, initX, settings, method)
		if err != nil {
			t.Errorf("cas %v: error optimizing: %s", cas, err)
		}
		if result.Status != FunctionEvaluationLimit {
			t.Errorf("cas %v: status was not FunctionEvaluationLimit", cas)
		}
		if !floats.Equal(result.X, locs.RawRowView(minIdxFirst)) {
			t.Errorf("cas %v: did not find minimum of shortened list concurrent", cas)
		}
	}
}

func swapSamples(m *mat.Dense, f []float64, i, j int) {
	f[i], f[j] = f[j], f[i]
	row := mat.Row(nil, i, m)
	m.SetRow(i, m.RawRowView(j))
	m.SetRow(j, row)
}