public ClEditInfo(ClConstraint cn, ClSlackVariable eplus, ClSlackVariable eminus, double prevEditConstant, int i) { _cn = cn; _clvEditPlus = eplus; _clvEditMinus = eminus; PrevEditConstant = prevEditConstant; _i = i; }
/// <summary> /// Removes contraint. /// </summary> public CElement RemoveConstraint(ClConstraint constraint) { //if (!constraints.Contains(constraint)) //{ // throw new InvalidOperationException($"Constraint {constraint} in not added to {this}"); //} constraints.Remove(constraint); Solver?.RemoveConstraint(constraint); SetNeedsUpdateLayout(); return(this); }
/// <summary> /// Checks if used variables belong to current view tree /// </summary> private void ValidateVariables(ClConstraint constraint) { HashSet <ClVariable> anchors = new HashSet <ClVariable>(Root.allAnchors()); foreach (var var in constraint.Expression.Terms.Keys) { if (!anchors.Contains(var)) { throw new InvalidOperationException($"Constraint {constraint} contains variable {var} from outside current elements tree {Root.NamePrefix()}"); } } }
void Constraint() { ClLinearExpression e1, e2; bool eq = false, geq = false, leq = false; Expression(out e1); if (la.kind == 3) { Get(); eq = true; } else if (la.kind == 2) { Get(); geq = true; } else if (la.kind == 5) { Get(); geq = true; } else if (la.kind == 1) { Get(); leq = true; } else if (la.kind == 4) { Get(); leq = true; } else { SynErr(15); } Expression(out e2); if (eq) { Value = new ClLinearEquation(e1, e2); } else if (geq) { Value = new ClLinearInequality(e1, Cl.GEQ, e2); } else if (leq) { Value = new ClLinearInequality(e1, Cl.LEQ, e2); } }
/// <summary> /// Adds constraint. /// </summary> /// <param name="constraint">the constraint</param> /// <param name="strength">constraints strengh override. Not changed if null</param> /// <remarks>Every variable of the constraint should belong to current view tree</remarks> public CElement AddConstraint(ClConstraint constraint, ClStrength strength = null) { ValidateVariables(constraint); if (strength != null) { constraint.SetStrength(strength); } constraints.Add(constraint); Solver.AddConstraint(constraint); SetNeedsUpdateLayout(); return(this); }
public void Parse() { UTF8Encoding ue = new UTF8Encoding(); byte[] ruleBytes = ue.GetBytes(Rule); MemoryStream ms = new MemoryStream(ruleBytes); Scanner s = new Scanner(ms); Parser p = new Parser(s); p.Context = Context; p.Parse(); _result = p.Value; if (p.errors.count > 0) { throw new ExClParseError(Rule); } }
protected IEnumerable <ClConstraint> GetConstraintsFromFluentLayout(IFluentLayout <T> fluentLayout) { var constraints = new List <ClConstraint>(); ClLinearExpression firstExpression = null; firstExpression = GetExpressionFromViewAndAttribute(fluentLayout.View, fluentLayout.Attribute); ClLinearExpression secondExpression = null; if (fluentLayout.SecondItem != null) { secondExpression = GetExpressionFromViewAndAttribute( fluentLayout.SecondItem.View, fluentLayout.SecondItem.Attribute ); var multiplier = !Cl.Approx(fluentLayout.Multiplier, 0) ? fluentLayout.Multiplier : 1; //make sure to construct the least complicated tableau possible by avoiding needless operations if (!Cl.Approx(multiplier, 1)) { secondExpression = Cl.Plus( Cl.Times(secondExpression, multiplier), new ClLinearExpression(fluentLayout.Constant) ); } else if (!Cl.Approx(fluentLayout.Constant, 0)) { secondExpression = Cl.Plus( secondExpression, new ClLinearExpression(fluentLayout.Constant) ); } } else { secondExpression = new ClLinearExpression(fluentLayout.Constant); } ClConstraint cn = null; var strength = ClStrength.Strong; var priority = fluentLayout.Priority / 1000; switch (fluentLayout.Relation) { case LayoutRelation.Equal: cn = new ClLinearEquation( firstExpression, secondExpression, strength, priority ); break; case LayoutRelation.GreaterThanOrEqual: cn = new ClLinearInequality( firstExpression, Cl.Operator.GreaterThanOrEqualTo, secondExpression, strength, priority ); break; case LayoutRelation.LessThanOrEqual: cn = new ClLinearInequality( firstExpression, Cl.Operator.LessThanOrEqualTo, secondExpression, strength, priority ); break; } constraints.Add(cn); return(constraints); }
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 void AddDel() { const int nCns = 450; const int nVars = 450; const int nResolves = 5000; var timer = new Stopwatch(); const double ineqProb = 0.12; const int maxVars = 3; Console.WriteLine("starting timing test. nCns = " + nCns + ", nVars = " + nVars + ", nResolves = " + nResolves); timer.Start(); var rgpclv = new ClVariable[nVars]; for (var i = 0; i < nVars; i++) { rgpclv[i] = new ClVariable(i, "x"); _solver.AddStay(rgpclv[i]); } var rgpcns = new ClConstraint[nCns]; int j; for (j = 0; j < nCns; j++) { // number of variables in this constraint var nvs = RandomInRange(1, maxVars); var expr = new ClLinearExpression(UniformRandomDiscretized() * 20.0 - 10.0); int k; for (k = 0; k < nvs; k++) { var coeff = UniformRandomDiscretized() * 10 - 5; var 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); } } Console.WriteLine("done building data structures"); Console.WriteLine("time = " + timer.Elapsed); timer.Start(); var cExceptions = 0; for (j = 0; j < nCns; j++) { // add the constraint -- if it's incompatible, just ignore it try { _solver.AddConstraint(rgpcns[j]); } catch (CassowaryRequiredFailureException) { cExceptions++; rgpcns[j] = null; } } Console.WriteLine("done adding constraints [" + cExceptions + " exceptions]"); Console.WriteLine("time = " + timer.Elapsed + "\n"); timer.Start(); var e1Index = (int)(UniformRandomDiscretized() * nVars); var e2Index = (int)(UniformRandomDiscretized() * nVars); Console.WriteLine("indices " + e1Index + ", " + e2Index); var edit1 = new ClEditConstraint(rgpclv[e1Index], ClStrength.Strong); var 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.Elapsed + "\n"); timer.Start(); //for (var 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.Elapsed + "\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.Elapsed + "\n"); timer.Start(); }