NumScopes
/// <summary> /// Try to find a configuration with low weight. /// </summary> /// <param name="sortedRanking">A list of binary options and their weight ordered by their weight.</param> /// <param name="cache">A sat solver cache instance that already contains the constraints of /// size and disallowed features.</param> /// <param name="vm">The variability model of the given system.</param> /// <returns>A configuration that has a small weight.</returns> public static List <BinaryOption> getSmallWeightConfig(List <KeyValuePair <List <BinaryOption>, int> > sortedRanking, Z3Cache cache, VariabilityModel vm) { KeyValuePair <List <BinaryOption>, int>[] ranking = sortedRanking.ToArray(); Microsoft.Z3.Solver solver = cache.GetSolver(); Context z3Context = cache.GetContext(); for (int i = 0; i < ranking.Length; i++) { List <BinaryOption> candidates = ranking[i].Key; solver.Push(); solver.Assert(forceFeatures(candidates, z3Context, cache.GetOptionToTermMapping())); if (solver.Check() == Status.SATISFIABLE) { Model model = solver.Model; solver.Pop(); return(Z3VariantGenerator.RetrieveConfiguration(cache.GetVariables(), model, cache.GetTermToOptionMapping())); } solver.Pop(); } return(null); }
private void ReAssertDecisionAssumptions() { _solver.Pop(); _solver.Push(); foreach (Assumption assumption in _decisionAssumptions.Values) { AssertValueAssumption(assumption); } }
/// <summary> /// Generates all valid combinations of all configuration options in the given model. /// </summary> /// <param name="vm">the variability model containing the binary options and their constraints</param> /// <param name="optionsToConsider">the options that should be considered. All other options are ignored</param> /// <returns>Returns a list of <see cref="Configuration"/></returns> public List <Configuration> GenerateAllVariants(VariabilityModel vm, List <ConfigurationOption> optionsToConsider) { List <Configuration> allConfigurations = new List <Configuration>(); List <BoolExpr> variables; Dictionary <BoolExpr, BinaryOption> termToOption; Dictionary <BinaryOption, BoolExpr> optionToTerm; Tuple <Context, BoolExpr> z3Tuple = Z3Solver.GetInitializedBooleanSolverSystem(out variables, out optionToTerm, out termToOption, vm, this.henard); Context z3Context = z3Tuple.Item1; BoolExpr z3Constraints = z3Tuple.Item2; Microsoft.Z3.Solver solver = z3Context.MkSolver(); // TODO: The following line works for z3Solver version >= 4.6.0 //solver.Set (RANDOM_SEED, z3RandomSeed); Params solverParameter = z3Context.MkParams(); solverParameter.Add(RANDOM_SEED, z3RandomSeed); solver.Parameters = solverParameter; solver.Assert(z3Constraints); while (solver.Check() == Status.SATISFIABLE) { Model model = solver.Model; List <BinaryOption> binOpts = RetrieveConfiguration(variables, model, termToOption, optionsToConsider); Configuration c = new Configuration(binOpts); // Check if the non-boolean constraints are satisfied if (vm.configurationIsValid(c) && !VariantGenerator.IsInConfigurationFile(c, allConfigurations) && VariantGenerator.FulfillsMixedConstraints(c, vm)) { allConfigurations.Add(c); } solver.Push(); solver.Assert(Z3Solver.NegateExpr(z3Context, Z3Solver.ConvertConfiguration(z3Context, binOpts, optionToTerm, vm))); } solver.Push(); solver.Pop(Convert.ToUInt32(allConfigurations.Count() + 1)); return(allConfigurations); }
/// <summary> /// Generates all valid combinations of all configuration options in the given model. /// </summary> /// <param name="vm">the variability model containing the binary options and their constraints</param> /// <param name="optionsToConsider">the options that should be considered. All other options are ignored</param> /// <returns>Returns a list of <see cref="Configuration"/></returns> public List <Configuration> GenerateAllVariants(VariabilityModel vm, List <ConfigurationOption> optionsToConsider) { List <Configuration> allConfigurations = new List <Configuration>(); List <Expr> variables; Dictionary <Expr, ConfigurationOption> termToOption; Dictionary <ConfigurationOption, Expr> optionToTerm; Tuple <Context, BoolExpr> z3Tuple = Z3Solver.GetInitializedSolverSystem(out variables, out optionToTerm, out termToOption, vm); Context z3Context = z3Tuple.Item1; BoolExpr z3Constraints = z3Tuple.Item2; Microsoft.Z3.Solver solver = z3Context.MkSolver(); solver.Set(RANDOM_SEED, z3RandomSeed); solver.Assert(z3Constraints); while (solver.Check() == Status.SATISFIABLE) { Model model = solver.Model; Tuple <List <BinaryOption>, Dictionary <NumericOption, double> > confOpts = RetrieveConfiguration(variables, model, termToOption, optionsToConsider); Configuration c = new Configuration(confOpts.Item1, confOpts.Item2); // Check if the non-boolean constraints are satisfied bool configIsValid = vm.configurationIsValid(c); bool isInConfigurationFile = !VariantGenerator.IsInConfigurationFile(c, allConfigurations); bool fulfillsMixedConstraintrs = VariantGenerator.FulfillsMixedConstraints(c, vm); if (configIsValid && isInConfigurationFile && fulfillsMixedConstraintrs) { allConfigurations.Add(c); } solver.Push(); solver.Assert(Z3Solver.NegateExpr(z3Context, Z3Solver.ConvertConfiguration(z3Context, confOpts.Item1, optionToTerm, vm, numericValues: confOpts.Item2))); } solver.Push(); solver.Pop(Convert.ToUInt32(allConfigurations.Count() + 1)); return(allConfigurations); }
/// <summary> /// Demonstrate how to use <code>Push</code>and <code>Pop</code>to /// control the size of models. /// </summary> /// <remarks>Note: this test is specialized to 32-bit bitvectors.</remarks> public static void CheckSmall(Context ctx, Solver solver, BitVecExpr[] to_minimize) { Sort bv32 = ctx.MkBitVecSort(32); int num_Exprs = to_minimize.Length; UInt32[] upper = new UInt32[num_Exprs]; UInt32[] lower = new UInt32[num_Exprs]; BitVecExpr[] values = new BitVecExpr[num_Exprs]; for (int i = 0; i < upper.Length; ++i) { upper[i] = UInt32.MaxValue; lower[i] = 0; } bool some_work = true; int last_index = -1; UInt32 last_upper = 0; while (some_work) { solver.Push(); bool check_is_sat = true; while (check_is_sat && some_work) { // Assert all feasible bounds. for (int i = 0; i < num_Exprs; ++i) { solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(upper[i], 32))); } check_is_sat = Status.SATISFIABLE == solver.Check(); if (!check_is_sat) { if (last_index != -1) { lower[last_index] = last_upper + 1; } break; } Console.WriteLine("{0}", solver.Model); // narrow the bounds based on the current model. for (int i = 0; i < num_Exprs; ++i) { Expr v = solver.Model.Evaluate(to_minimize[i]); UInt64 ui = ((BitVecNum)v).UInt64; if (ui < upper[i]) { upper[i] = (UInt32)ui; } Console.WriteLine("{0} {1} {2}", i, lower[i], upper[i]); } // find a new bound to add some_work = false; last_index = 0; for (int i = 0; i < num_Exprs; ++i) { if (lower[i] < upper[i]) { last_upper = (upper[i] + lower[i]) / 2; last_index = i; solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(last_upper, 32))); some_work = true; break; } } } solver.Pop(); } }