public static NumericVariable operator /(NumericVariable v, double d) { NumericVariable result = v.DeriveVariable("Prop/rh", $".{v.VarIndex}/{d}"); ProportionalConstraint.CreateProportionalConstraint(d, result, v); return(result); }
public static NumericVariable operator -(NumericVariable v) { NumericVariable result = v.DeriveVariable("Invrh", $"-.{v.VarIndex}"); IsInverseConstraint.CreateIsInverseConstraint(v, result); return(result); }
public static NumericVariable operator -(NumericVariable left, NumericVariable right) { NumericVariable result = left.DeriveVariable("+Sum0rh", $".{left.VarIndex}-.{right.VarIndex}"); SumIs0Constraint.CreateSumIs0Constraint(-left, right, result); return(result); }
public SimpleConstraintSolver(double eps = 1.0 / 1024.0 / 1024.0 / 1024.0) { Eps = eps; ZERO = CreateConstant("0", 0); ONE = CreateConstant("1", 1); NEGATIVE_INF = CreateConstant("-inf", double.NegativeInfinity); POSITIVE_INF = CreateConstant("+inf", double.PositiveInfinity); }
protected override bool Update(NumericVariable v) { if (v == _variable1) { return(_variable1.RestrictRange(_variable2.Value.Lo, _variable2.Value.Hi, 1 / _d)); } else { return(_variable2.RestrictRange(_variable1.Value.Lo, _variable1.Value.Hi, _d)); } }
protected override bool Update(NumericVariable v) { if (v == _variable1) { // v1 >= v2 return(_variable1.RestrictRange(_variable2.Value.Lo, double.PositiveInfinity)); } else { // v2 <= v1 return(_variable2.RestrictRange(double.NegativeInfinity, _variable1.Value.Hi)); } }
internal NumericVariable GetOrCreateVariable([NotNull] string shortName, [NotNull] string definition, double?lo, double?hi, float interpolate) { if (_solved) { throw new InvalidOperationException($"Solver is solved - no new variable '{shortName}', please! (definition='{definition}')"); } NumericVariable result; if (!_allVariables.TryGetValue(definition, out result)) { _allVariables.Add(definition, result = new NumericVariable(shortName, definition, this, lo, hi, interpolate)); } return(result); }
protected override bool Update(NumericVariable v) { return(v.RestrictRange(_range)); }
private AtLeastConstraint(NumericVariable variable1, NumericVariable variable2) : base(variable1, variable2) { }
public static AtLeastConstraint CreateAtLeastConstraint(NumericVariable variable1, NumericVariable variable2) { return(new AtLeastConstraint(variable1, variable2)); }
protected override bool Update(NumericVariable v) { return(v.RestrictRange(-(v == _variable1 ? _variable2 : _variable1).Value)); }
/// <summary> /// The variable's value is restricted to be equal to the (eventual) value of another variable. /// </summary> /// <returns><c>this</c></returns> public NumericVariable Set(NumericVariable value) { EqualityConstraint.CreateEqualityConstraint(this, value); return(this); }
public VariableVector MaxY(NumericVariable value) { _y.Max(value); return(this); }
public VariableVector MinY(NumericVariable value) { _y.Min(value); return(this); }
protected abstract bool Update(NumericVariable v);
/// <summary> /// The variable's maximum is restricted to the (eventual) value of another variable. /// </summary> /// <returns><c>this</c></returns> public NumericVariable Max(NumericVariable value) { AtLeastConstraint.CreateAtLeastConstraint(value, this); return(this); }
public static ProportionalConstraint CreateProportionalConstraint(double d, NumericVariable variable1, NumericVariable variable2) { return(new ProportionalConstraint(d, variable1, variable2)); }
private RangeConstraint(NumericVariable inputVariable, Range range) : base(inputVariable) { _range = range; }
public static RangeConstraint CreateRangeConstraint(NumericVariable inputVariable, Range range) { return(new RangeConstraint(inputVariable, range)); }
protected UnaryConstraint([NotNull] NumericVariable inputVariable) : base(inputVariable) { _inputVariable = inputVariable; }
private ProportionalConstraint(double d, NumericVariable variable1, NumericVariable variable2) : base(variable1, variable2) { _d = d; }
protected BinaryConstraint([NotNull] NumericVariable variable1, [NotNull] NumericVariable variable2) : base(variable1, variable2) { _variable1 = variable1; _variable2 = variable2; }
//public SumIs0Constraint Plus(DoubleHolder d) { // return new SumIs0Constraint(_plus.Concat(new[] { d })); //} protected override bool Update(NumericVariable v) { bool changed = false; // The following propagation uses these ideas: // For each variable v, take the sum S of all OTHER Lo values. A high value that is higher // than -S will never cancel S to zero, let alone sums that are greater than S. Hence, // v.Hi can be restricted to <= -S. // Likewise, v.Lo can be restricted to >= -S where S is the sum of all other Hi values. // O(n^2) algorithm - a O(n) algorithm is possible ... later double loSum = 0; // OnceExcluded is necessary for a constraint like X + X + Y = 0: When we already know // that X = [100..100] and Y = [-200..-200], this should succeed. However, when during // Upgrade(X) we skip both X's (via w != v), the sum will not be 100-200 == 100, but // -200; and hence // X will then be restricted to [200..+inf], which, together with the previously known // value X = 100 will erroneously imply that there is no solution for X. // By the way, when in X + X + Y = 0, we have X = [-inf...+inf] and Y = [-200..-200] at // the beginning, we will NOT conclude that X = [100..100] - that would be equation // solving, which we do not support at this level. bool onceExcluded = false; foreach (var w in _inputVariables) { if (onceExcluded || w != v) { double wLo = w.Value.Lo; if (double.IsNegativeInfinity(wLo)) { // Nothing can be said about the sum of lows - cancel this computation; goto SKIP_LO; } loSum += wLo; } else { onceExcluded = true; } } changed |= v.RestrictRange(double.NegativeInfinity, -loSum); SKIP_LO: double hiSum = 0; onceExcluded = false; foreach (var w in _inputVariables) { if (onceExcluded || w != v) { double wHi = w.Value.Hi; if (double.IsPositiveInfinity(wHi)) { // Nothing can be said about the sum of his - cancel this computation; goto SKIP_HI; } hiSum += wHi; } else { onceExcluded = true; } } changed |= v.RestrictRange(-hiSum, double.PositiveInfinity); SKIP_HI: return(changed); }
public static EqualityConstraint CreateEqualityConstraint(NumericVariable variable1, NumericVariable variable2) { return(new EqualityConstraint(variable1, variable2)); }
protected override bool Update(NumericVariable v) { throw new NotImplementedException("Update(IEnumerable<NumericVariable>) is overridden"); }
private EqualityConstraint(NumericVariable variable1, NumericVariable variable2) : base(variable1, variable2) { }
public VariableVector SetY(NumericVariable value) { _y.Set(value); return(this); }
public static IsInverseConstraint CreateIsInverseConstraint(NumericVariable variable1, NumericVariable variable2) { return(new IsInverseConstraint(variable1, variable2)); }
public VariableVector(string name, [NotNull] NumericVariable x, [NotNull] NumericVariable y) { Name = name; _x = x; _y = y; }
private IsInverseConstraint(NumericVariable variable1, NumericVariable variable2) : base(variable1, variable2) { }