/// <inheritdoc/> public override void Apply(Minimizer minimizer) { base.Apply(minimizer); if (_strictMirror) { minimizer.AddConstraint(new Squared(Scale) - 1, $"keep scale of {Name}"); } else { minimizer.Minimize += 1e6 * (new Squared(Scale) + 1.0 / new Squared(Scale)); } }
/// <summary> /// Solves the unknowns in this circuit. /// </summary> public void Solve() { var minimizer = new Minimizer(); minimizer.Warning += Warn; // Build the function that needs to be minimized for (var i = 0; i < _wires.Count; i++) { foreach (var length in _wires[i].Lengths) { if (length.IsFixed) { if (length.Value < 0.0) { Warn(this, new WarningEventArgs($"Wire length '{length}' is smaller than 0.")); } continue; } var x = length - WireLength; minimizer.Minimize += 1e3 * (x + new Squared(x) + new Exp(-x)); length.Value = WireLength; minimizer.AddMinimum(length, 0.0); } } foreach (var c in _components) { c.Value.Apply(minimizer); } foreach (var c in _constraints) { if (!c.Key.IsFixed) { minimizer.AddConstraint(c.Key, c.Value); } else { if (!c.Key.Value.IsZero()) { Warn(this, new WarningEventArgs($"Could not {c.Value}")); } } } minimizer.Solve(); Solved = true; minimizer.Warning -= Warn; }
public override void Apply(Minimizer minimizer) { base.Apply(minimizer); minimizer.Minimize += new Squared(NormalX - 1) / 1e4; minimizer.AddConstraint(new Squared(NormalX) + new Squared(NormalY) - 1, $"fix orientation of {Name}"); }