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
|
/*
Copyright (C) 2019 Leo Tenenbaum
This file is part of AutoVCV.
AutoVCV is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AutoVCV is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AutoVCV. If not, see <https://www.gnu.org/licenses/>.
*/
package autovcv
import (
"math/rand"
"autovcv/vcv"
)
type input struct {
module int
id int
}
func RandomVCV(nModules int, nWires int) *vcv.VCVFile {
var v vcv.VCVFile
// Create a slice of modules
modules := make([]string, len(vcv.Modules))
i := 0
for module, _ := range vcv.Modules {
modules[i] = module
i++
}
for i := 0; i < nModules; i++ {
model := modules[rand.Intn(len(modules))]
info := vcv.Modules[model]
params := make([]float64, info.Params)
for p := 0; p < info.Params; p++ {
b := vcv.ModuleParamBounds[model][p]
params[p] = (b.Max - b.Min) * rand.Float64() + b.Min
}
v.AddModule(model, params, rand.Intn(100), rand.Intn(3))
}
if v.NumberOfModules() == 1 {
// No wires
return &v
}
// Which inputs have been used?
inputsUsed := make(map[input]bool)
for i := 0; i < nWires; i++ {
m1id := 0
m2id := 0
for m1id == m2id {
m1id = rand.Intn(v.NumberOfModules())
m2id = rand.Intn(v.NumberOfModules())
}
m1outputs := vcv.Modules[v.GetModule(m1id).Model].Outputs
m2inputs := vcv.Modules[v.GetModule(m2id).Model].Inputs
outId := rand.Intn(m1outputs)
inId := rand.Intn(m2inputs)
input := input{m2id, inId}
_, used := inputsUsed[input]
if used { continue } // We've already used this input
inputsUsed[input] = true
v.AddWire(m1id, outId, m2id, inId, "#00cc00")
}
return &v
}
|