public void AddingValueStayConstraintMakesValueNotChange() { var x = new ClVariable("x", 5); _solver.AddStay(x); Assert.IsTrue(Cl.Approx(x, 5)); }
public void AddStayConstraints(IEnumerable <IFluentLayout <T> > constraints) { //add constraints to solver foreach (var fluentLayout in constraints) { foreach (var constraint in GetConstraintsFromFluentLayout(fluentLayout)) { solver.AddConstraint(constraint); } } _constraints.AddRange(constraints); foreach (var constraint in constraints) { if (constraint.SecondItem != null) { throw new Exception("Edit constraint can't have a right side"); } var variable = GetVariableFromViewAndAttribute(constraint.View, constraint.Attribute); if (!isStay.ContainsKey(variable.Name)) { isStay.Add(variable.Name, 0); variable.ChangeValue(0); solver.AddStay(variable); } } }
public static bool JustStay1() { bool okResult = true; ClVariable x = new ClVariable(5); ClVariable y = new ClVariable(10); ClSimplexSolver solver = new ClSimplexSolver(); solver.AddStay(x); solver.AddStay(y); okResult = okResult && Cl.Approx(x, 5); okResult = okResult && Cl.Approx(y, 10); Console.WriteLine("x == " + x.Value); Console.WriteLine("y == " + y.Value); return(okResult); }
public void Playground() { var windowHeight = new ClVariable(1); _solver.AddStay(windowHeight); var doorHeightVariable = new ClVariable(2); _solver.AddStay(doorHeightVariable); var margin = new ClVariable("margin"); // ReSharper disable CompareOfFloatsByEqualityOperator _solver.AddConstraint(windowHeight, doorHeightVariable, margin, (wh, dh, wm) => ((5 - dh) - wh) == wm * 2); // ReSharper restore CompareOfFloatsByEqualityOperator Assert.AreEqual(5 - 2 - 1, margin.Value * 2); }
public void EndEdit_Throws() { var value = new ClVariable("value", 0); var solver = new ClSimplexSolver(); solver.AddStay(value, ClStrength.Strong); solver.BeginEdit(value) .SuggestValue(value, 25) .EndEdit(); // <- Exception raised here }
protected void Init() { solver = new ClSimplexSolver(); solver.AutoSolve = false; var stayVariables = new LayoutAttribute[] { LayoutAttribute.Left, LayoutAttribute.Top }; foreach (var attribute in stayVariables) { var variable = GetVariableFromViewAndAttribute(_rootView, attribute); variable.ChangeValue(0); _stayVariables.Add(attribute, variable); solver.AddStay(variable); } }
void AddEditVar(ClSimplexSolver solver, ClVariable variable) { if (!isStay.ContainsKey(variable.Name)) { isStay.Add(variable.Name, 1); solver.AddStay(variable); solver.AddEditVar(variable, ClStrength.Strong); } else { var status = isStay[variable.Name]; if (status == 0) { solver.AddEditVar(variable); isStay[variable.Name] = 1; } } }
public void EndEdit_NonRequiredVariable_SolvesCorrectly() { //Create a solver var solver = new ClSimplexSolver(); var variable = new ClVariable(0f); //Add a stay, indicating this var should stay at it's current value if possible solver.AddStay(variable, ClStrength.Strong, 1); //Bug Fixing Note: The stay is strong, which overpowers the SuggestValue. If the stay is Medium or Weak then the SuggestValue takes hold and this test does *not* fail! //tl;dr: This bug only happens if the suggest value does nothing? const double EXPECTED_VALUE = 10.0; //Suggest a value for the variable in an edit context var editContext = solver.BeginEdit(variable); editContext.SuggestValue(variable, EXPECTED_VALUE); editContext.EndEdit(); }
bool AddEditVar(ClSimplexSolver solver, ClVariable variable, int value) { if (!isStay.ContainsKey(variable.Name)) { isStay.Add(variable.Name, 0); variable.ChangeValue(value); solver.AddStay(variable); return(false); } else { var status = isStay[variable.Name]; if (status == 0) { solver.AddEditVar(variable); isStay[variable.Name] = 1; } BeginEdit(); solver.SuggestValue(variable, value); return(true); } }
public static bool AddDel(int nCns, int nVars, int nResolves) { Timer timer = new Timer(); double ineqProb = 0.12; int maxVars = 3; Console.WriteLine("starting timing test. nCns = " + nCns + ", nVars = " + nVars + ", nResolves = " + nResolves); timer.Start(); ClSimplexSolver solver = new ClSimplexSolver(); ClVariable[] rgpclv = new ClVariable[nVars]; for (int i = 0; i < nVars; i++) { rgpclv[i] = new ClVariable(i, "x"); solver.AddStay(rgpclv[i]); } ClConstraint[] rgpcns = new ClConstraint[nCns]; int nvs = 0; int k; int j; double coeff; for (j = 0; j < nCns; j++) { // number of variables in this constraint nvs = RandomInRange(1, maxVars); ClLinearExpression expr = new ClLinearExpression(UniformRandomDiscretized() * 20.0 - 10.0); for (k = 0; k < nvs; k++) { coeff = UniformRandomDiscretized() * 10 - 5; int iclv = (int)(UniformRandomDiscretized() * nVars); expr.AddExpression(Cl.Times(rgpclv[iclv], coeff)); } if (UniformRandomDiscretized() < ineqProb) { rgpcns[j] = new ClLinearInequality(expr); } else { rgpcns[j] = new ClLinearEquation(expr); } if (Trace) { TracePrint("Constraint " + j + " is " + rgpcns[j]); } } Console.WriteLine("done building data structures"); Console.WriteLine("time = " + timer.ElapsedTime); timer.Start(); int cExceptions = 0; for (j = 0; j < nCns; j++) { // add the constraint -- if it's incompatible, just ignore it try { solver.AddConstraint(rgpcns[j]); } catch (ExClRequiredFailure) { cExceptions++; if (Trace) { TracePrint("got exception adding " + rgpcns[j]); } rgpcns[j] = null; } } Console.WriteLine("done adding constraints [" + cExceptions + " exceptions]"); Console.WriteLine("time = " + timer.ElapsedTime + "\n"); timer.Start(); int e1Index = (int)(UniformRandomDiscretized() * nVars); int e2Index = (int)(UniformRandomDiscretized() * nVars); Console.WriteLine("indices " + e1Index + ", " + e2Index); ClEditConstraint edit1 = new ClEditConstraint(rgpclv[e1Index], ClStrength.Strong); ClEditConstraint edit2 = new ClEditConstraint(rgpclv[e2Index], ClStrength.Strong); solver .AddConstraint(edit1) .AddConstraint(edit2); Console.WriteLine("done creating edit constraints -- about to start resolves"); Console.WriteLine("time = " + timer.ElapsedTime + "\n"); timer.Start(); for (int m = 0; m < nResolves; m++) { solver.Resolve(rgpclv[e1Index].Value * 1.001, rgpclv[e2Index].Value * 1.001); } Console.WriteLine("done resolves -- now removing constraints"); Console.WriteLine("time = " + timer.ElapsedTime + "\n"); solver.RemoveConstraint(edit1); solver.RemoveConstraint(edit2); timer.Start(); for (j = 0; j < nCns; j++) { if (rgpcns[j] != null) { solver.RemoveConstraint(rgpcns[j]); } } Console.WriteLine("done removing constraints and AddDel timing test"); Console.WriteLine("time = " + timer.ElapsedTime + "\n"); timer.Start(); return(true); }
public static bool Multiedit() { try { bool okResult = true; ClVariable x = new ClVariable("x"); ClVariable y = new ClVariable("y"); ClVariable w = new ClVariable("w"); ClVariable h = new ClVariable("h"); ClSimplexSolver solver = new ClSimplexSolver(); solver .AddStay(x) .AddStay(y) .AddStay(w) .AddStay(h); solver .AddEditVar(x) .AddEditVar(y) .BeginEdit(); solver .SuggestValue(x, 10) .SuggestValue(y, 20) .Resolve(); Console.WriteLine("x = " + x.Value + "; y = " + y.Value); Console.WriteLine("w = " + w.Value + "; h = " + h.Value); okResult = okResult && Cl.Approx(x, 10) && Cl.Approx(y, 20) && Cl.Approx(w, 0) && Cl.Approx(h, 0); solver .AddEditVar(w) .AddEditVar(h) .BeginEdit(); solver .SuggestValue(w, 30) .SuggestValue(h, 40) .EndEdit(); Console.WriteLine("x = " + x.Value + "; y = " + y.Value); Console.WriteLine("w = " + w.Value + "; h = " + h.Value); okResult = okResult && Cl.Approx(x, 10) && Cl.Approx(y, 20) && Cl.Approx(w, 30) && Cl.Approx(h, 40); solver .SuggestValue(x, 50) .SuggestValue(y, 60) .EndEdit(); Console.WriteLine("x = " + x.Value + "; y = " + y.Value); Console.WriteLine("w = " + w.Value + "; h = " + h.Value); okResult = okResult && Cl.Approx(x, 50) && Cl.Approx(y, 60) && Cl.Approx(w, 30) && Cl.Approx(h, 40); return(okResult); } catch (ExClRequiredFailure) { // we want this exception to get thrown Console.WriteLine("-- got the exception"); return(true); } }
private void BuildStayConstraints(ClSimplexSolver solver) { solver.AddStay(_updateHeight); solver.AddStay(_updateWidth); solver.AddStay(_newpostHeight); solver.AddStay(_newpostWidth); solver.AddStay(_quitHeight); solver.AddStay(_quitWidth); solver.AddStay(_lTitleHeight); solver.AddStay(_lTitleWidth); solver.AddStay(_titleHeight); solver.AddStay(_titleWidth); solver.AddStay(_lBodyHeight); solver.AddStay(_lBodyWidth); solver.AddStay(_blogentryHeight); // let's keep blogentry.width in favor of other stay constraints! // remember we later specify title.width to be equal to blogentry.width solver.AddStay(_blogentryWidth, ClStrength.Strong); solver.AddStay(_lRecentHeight); solver.AddStay(_lRecentWidth); solver.AddStay(_articlesHeight); solver.AddStay(_articlesWidth); }