-
Notifications
You must be signed in to change notification settings - Fork 9
/
generators.go
149 lines (139 loc) · 3.7 KB
/
generators.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
// 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 (
"github.com/cpmech/gosl/chk"
"github.com/cpmech/gosl/rnd"
)
// GenTrialSolutions generates (initial) trial solutions
func GenTrialSolutions(sols []*Solution, prms *Parameters, reset bool) {
// reset solutions
if reset {
for id, sol := range sols {
sol.Reset(id)
}
}
// floats
n := len(sols) // cannot use Nsol here because subsets of Solutions may be provided; e.g. parallel code
if prms.Nflt > 0 {
// interior points
switch prms.GenType {
case "latin":
K := rnd.LatinIHS(prms.Nflt, n, prms.LatinDup)
for i := 0; i < n; i++ {
for j := 0; j < prms.Nflt; j++ {
sols[i].Flt[j] = prms.FltMin[j] + float64(K[j][i]-1)*prms.DelFlt[j]/float64(n-1)
}
}
case "halton":
H := rnd.HaltonPoints(prms.Nflt, n)
for i := 0; i < n; i++ {
for j := 0; j < prms.Nflt; j++ {
sols[i].Flt[j] = prms.FltMin[j] + H[j][i]*prms.DelFlt[j]
}
}
default:
for i := 0; i < n; i++ {
for j := 0; j < prms.Nflt; j++ {
sols[i].Flt[j] = rnd.Float64(prms.FltMin[j], prms.FltMax[j])
}
}
}
// extra points
if prms.UseMesh {
initX := func(isol int) {
for k := 0; k < prms.Nflt; k++ {
sols[isol].Flt[k] = (prms.FltMin[k] + prms.FltMax[k]) / 2.0
}
}
isol := prms.Nsol - prms.NumExtraSols
for i := 0; i < prms.Nflt-1; i++ {
for j := i + 1; j < prms.Nflt; j++ {
// (min,min) corner
initX(isol)
sols[isol].Flt[i] = prms.FltMin[i]
sols[isol].Flt[j] = prms.FltMin[j]
sols[isol].Fixed = true
isol++
// (min,max) corner
initX(isol)
sols[isol].Flt[i] = prms.FltMin[i]
sols[isol].Flt[j] = prms.FltMax[j]
sols[isol].Fixed = true
isol++
// (max,max) corner
initX(isol)
sols[isol].Flt[i] = prms.FltMax[i]
sols[isol].Flt[j] = prms.FltMax[j]
sols[isol].Fixed = true
isol++
// (max,min) corner
initX(isol)
sols[isol].Flt[i] = prms.FltMax[i]
sols[isol].Flt[j] = prms.FltMin[j]
sols[isol].Fixed = true
isol++
// Xi-min middle points
ndelta := float64(prms.Nbry - 1)
for m := 0; m < prms.Nbry-2; m++ {
initX(isol)
sols[isol].Flt[i] = prms.FltMin[i]
sols[isol].Flt[j] = prms.FltMin[j] + float64(m+1)*prms.DelFlt[j]/ndelta
sols[isol].Fixed = true
isol++
}
// Xi-max middle points
for m := 0; m < prms.Nbry-2; m++ {
initX(isol)
sols[isol].Flt[i] = prms.FltMax[i]
sols[isol].Flt[j] = prms.FltMin[j] + float64(m+1)*prms.DelFlt[j]/ndelta
sols[isol].Fixed = true
isol++
}
// Xj-min middle points
for m := 0; m < prms.Nbry-2; m++ {
initX(isol)
sols[isol].Flt[i] = prms.FltMin[i] + float64(m+1)*prms.DelFlt[i]/ndelta
sols[isol].Flt[j] = prms.FltMin[j]
sols[isol].Fixed = true
isol++
}
// Xj-max middle points
for m := 0; m < prms.Nbry-2; m++ {
initX(isol)
sols[isol].Flt[i] = prms.FltMin[i] + float64(m+1)*prms.DelFlt[i]/ndelta
sols[isol].Flt[j] = prms.FltMax[j]
sols[isol].Fixed = true
isol++
}
}
}
chk.IntAssert(isol, prms.Nsol)
}
}
// skip if there are no ints
if prms.Nint < 2 {
return
}
// binary numbers
if prms.BinInt > 0 {
for i := 0; i < n; i++ {
for j := 0; j < prms.Nint; j++ {
if rnd.FlipCoin(0.5) {
sols[i].Int[j] = 1
} else {
sols[i].Int[j] = 0
}
}
}
return
}
// general integers
L := rnd.LatinIHS(prms.Nint, n, prms.LatinDup)
for i := 0; i < n; i++ {
for j := 0; j < prms.Nint; j++ {
sols[i].Int[j] = prms.IntMin[j] + (L[j][i]-1)*prms.DelInt[j]/(n-1)
}
}
}