From d4b6069fd541a10826653fc6d3d4bbcb3061c35f Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 10 Jun 2019 11:38:53 -0400 Subject: Initial commit --- autovcv/autovcv.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 autovcv/autovcv.go (limited to 'autovcv/autovcv.go') 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 . +*/ +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 +} -- cgit v1.2.3