Exemple #1
0
        static void ExecuteZ3(string fileName, Z3BaseDirective directive)
        {
            SolverContext context = SolverContext.GetContext();
            try
            {
                LoadModel(context, fileName);

                Solution solution = context.Solve(directive);
                Report report = solution.GetReport();
                Console.Write("{0}", report);
            }
            catch (Exception e)
            {
                Console.WriteLine("Skipping unsolvable instance in {0} with error message '{1}'.", fileName, e.Message);
            }
            finally
            {
                context.ClearModel();
            }
        }
Exemple #2
0
        static void ConvertToSMT2(string fileName, Z3BaseDirective directive)
        {
            SolverContext context = SolverContext.GetContext();
            try
            {
                LoadModel(context, fileName);

                if (context.CurrentModel.Goals.Any())
                {
                    directive.SMT2LogFile = Path.ChangeExtension(fileName, ".smt2");
                    context.Solve(() => true, directive);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Skipping unconvertable instance in {0} with error message '{1}'.", fileName, e.Message);
            }
            finally
            {
                context.ClearModel();
            }       
        }
Exemple #3
0
        static void ValidateZ3(string fileName, Z3BaseDirective directive)
        {
            SolverContext context = SolverContext.GetContext();
            try
            {
                LoadModel(context, fileName);

                if (context.CurrentModel.Goals.Any())
                {
                    var msfDirective = (directive is Z3MILPDirective) ? (Directive)new MixedIntegerProgrammingDirective() { TimeLimit = 10000 }
                                            : (Directive)new Directive() { TimeLimit = 10000 };
                    var sol1 = context.Solve(msfDirective);
                    
                    Console.WriteLine("Solved the model using MSF.");
                    Console.Write("{0}", sol1.GetReport());
                    var expectedGoals = sol1.Goals.Select(x => x.ToDouble());
                    context.ClearModel();

                    context.LoadModel(FileFormat.OML, Path.GetFullPath(fileName));
                    directive.SMT2LogFile = Path.ChangeExtension(fileName, ".smt2");
                    var sol2 = context.Solve(directive);
                    //Console.Write("{0}", sol2.GetReport());
                    var actualGoals = sol2.Goals.Select(x => x.ToDouble());

                    Console.WriteLine("Solved the model using Z3.");
                    var goalPairs = expectedGoals.Zip(actualGoals, (expected, actual) => new { expected, actual }).ToArray();
                    bool validated = goalPairs.All(p => Math.Abs(p.expected - p.actual) <= 0.0001);
                    if (validated)
                    {
                        Console.WriteLine("INFO: Two solvers give approximately the same results.");
                    }
                    else
                    {
                        Console.Error.WriteLine("ERROR: Discrepancy found between results.");
                        if (!validated && File.Exists(directive.SMT2LogFile))
                        {
                            var sb = new StringBuilder();                            
                            for(int i = 0; i < goalPairs.Length; i++)
                            {
                                sb.AppendFormat("\n(echo \"Goal {0}: actual |-> {1:0.0000}, expected |-> {2:0.0000}\")",
                                    i + 1, goalPairs[i].actual, goalPairs[i].expected);
                            }
                            Console.Error.WriteLine(sb.ToString());
                            File.AppendAllText(directive.SMT2LogFile, sb.ToString());
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Ignoring this instance without having any goal.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Skipping unsolvable instance in {0} with error message '{1}'.", 
                    fileName, e.Message);
            }
            finally
            {
                context.ClearModel();
            }
        }