public void FloatPredeteriminationTest() { var p = new Problem(nameof(GeneralSumConstraintTest)); var dom = new FloatDomain("unit", -1, 1); var x = (FloatVariable)dom.Instantiate("x"); var y = (FloatVariable)dom.Instantiate("y"); y.PredeterminedValue = 1; var sum = x + y; int spuriousHitCount = 0; for (int i = 0; i < 100; i++) { var v = Random.Float(-1, 1); x.PredeterminedValue = v; var s = p.Solve(); // Should always give us the predetermined value Assert.AreEqual(x.Value(s), v); Assert.AreEqual(sum.Value(s), v + 1); if (i % 2 == 0) { x.Reset(); s = p.Solve(); // Should almost never give us the formerly predetermined value // ReSharper disable CompareOfFloatsByEqualityOperator if (x.Value(s) == v || sum.Value(s) == v + 1) { // ReSharper restore CompareOfFloatsByEqualityOperator spuriousHitCount++; } } } Assert.IsTrue(spuriousHitCount < 3); }
public void FloatPredeterminationTest() { var problem = new Problem("test"); var dom = new FloatDomain("signed unit", -1, 1); var x = (FloatVariable)dom.Instantiate("x", problem); var y = (FloatVariable)dom.Instantiate("y", problem); var z = (FloatVariable)dom.Instantiate("z", problem); var bigThresh = 1.5f; var smallThresh = -2.3f; var xBig = x > bigThresh; var ySmall = y < smallThresh; var zBig = z > bigThresh; var zSmall = z < smallThresh; var xLTy = x < y; var xGTz = x > z; var yLTz = y < z; for (var i = 0; i < 100; i++) { var xVal = Random.Float(-1, 1); var yVal = Random.Float(-1, 1); x.PredeterminedValue = xVal; y.PredeterminedValue = yVal; var s = problem.Solve(); Assert.AreEqual(xVal, x.Value(s)); Assert.AreEqual(yVal, y.Value(s)); Assert.IsTrue(problem.IsPredetermined(xBig)); Assert.AreEqual(s[xBig], xVal >= bigThresh); Assert.IsTrue(problem.IsPredetermined(ySmall)); Assert.AreEqual(s[ySmall], yVal <= smallThresh); Assert.IsTrue(problem.IsPredetermined(xLTy)); Assert.AreEqual(s[xLTy], xVal <= yVal); Assert.IsFalse(problem.IsPredetermined(zBig)); Assert.IsFalse(problem.IsPredetermined(zSmall)); Assert.IsFalse(problem.IsPredetermined(xGTz)); Assert.IsFalse(problem.IsPredetermined(yLTz)); } }
public void ConstantProductConstraintTest() { for (int j = 0; j < 100; j++) { var p = new Problem(nameof(ConstantProductConstraintTest)); var dom = new FloatDomain("signed unit", -1, 1); var x = (FloatVariable)dom.Instantiate("x"); var c = Random.Float(-100, 100); var product = c * x; for (int i = 0; i < 100; i++) { var s = p.Solve(); Console.WriteLine(s.Model); Assert.IsTrue(Math.Abs(product.Value(s) / (x.Value(s)) - c) < 0.0001f); } } }
internal bool PickRandom(Queue <Tuple <FloatVariable, bool> > q) { var f = Random.Float(Bounds.Lower, Bounds.Upper); return(BoundAbove(f, q) && BoundBelow(f, q)); }