summaryrefslogtreecommitdiff
path: root/autovcv/autovcv.go
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-06-10 11:38:53 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-06-10 11:38:53 -0400
commitd4b6069fd541a10826653fc6d3d4bbcb3061c35f (patch)
tree6e1e2885ecfc2713e455a4c14fa27dba75578b7c /autovcv/autovcv.go
Initial commit0.0
Diffstat (limited to 'autovcv/autovcv.go')
-rw-r--r--autovcv/autovcv.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/autovcv/autovcv.go b/autovcv/autovcv.go
new file mode 100644
index 0000000..1475892
--- /dev/null
+++ b/autovcv/autovcv.go
@@ -0,0 +1,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
+}