Exemple #1
0
        public int FindIneqIndex(ListHyp hypermap, Label ineqId)
        {
            Definition      c       = constraints[ineqId.name];
            HypermapElement element = hypermap.Translate(c.domain, ineqId.index);

            return(hypermap.FindElementIndex(c.set, element));
        }
Exemple #2
0
        public int FindIneqIndex(ListHyp hypermap, Label ineqId)
        {
            try
            {
                if (!constraints.ContainsKey(ineqId.name))
                {
                    throw new Exception("Constraint " + ineqId.name + " is not defined in 000.txt");
                }

                Definition      c       = constraints[ineqId.name];
                HypermapElement element = hypermap.Translate(c.domain, ineqId.index);
                if (element == null)
                {
                    throw new Exception(
                              String.Format("Element with the index {0} is not found for the domain {1}", ineqId.index, c.domain));
                }

                return(hypermap.FindElementIndex(c.set, element));
            }
            catch (Exception e)
            {
                throw new Exception(
                          String.Format("Inequality {0} problem: {1}", ineqId.name, e.Message));
            }
        }
Exemple #3
0
        /// <summary>
        /// Processes all Flyspeck linear programs in the current directory
        /// </summary>
        static void ProcessFlyspeckLP()
        {
            ListHypManager hypermaps = InitializeHypermaps();

            // GenerateExamples();
            string[]     files = Directory.GetFiles(".", "*.lp");
            FileStream   fs    = new FileStream("all_tests.hl", FileMode.Create);
            StreamWriter w     = new StreamWriter(fs);


            w.WriteLine("let start = Sys.time();;");

            int i = 1;

            foreach (string file in files)
            {
                string   name     = new FileInfo(file).Name;
                string[] els      = name.Split('.');
                ListHyp  hypermap = hypermaps[els[0]];
                hypermap.ComputeAllSets();

                ProcessLP(els[0], new LpNumber(12), hypermap);

                w.WriteLine("\"Case: {0}/{1}\";;", i++, files.Length);
                w.WriteLine("let _ = needs \"{0}\" in Sys.time() -. start;;", els[0] + "_out.hl");
                // break;
            }

            w.Flush();
            fs.Close();
        }
Exemple #4
0
        /// <summary>
        /// Creates files for producing a formal proof for the given linear program.
        /// The precision is selected automatically
        /// </summary>
        static void ProcessLP(string fname, LpNumber upperBound, ListHyp hypermap, bool infeasible, bool holTerms)
        {
            Console.WriteLine("Processing {0}...", fname);
            for (int precision = 3; ; precision++)
            {
                Console.WriteLine("Precision = {0}", precision);

                if (hypermap != null)
                {
                    if (ProcessLP(fname, upperBound, precision, hypermap, infeasible, holTerms))
                    {
                        break;
                    }
                }
                else
                {
                    if (processLPGeneral(fname, precision, upperBound))
                    {
                        break;
                    }
                }
            }

            Console.WriteLine("done\n");
        }
Exemple #5
0
        /// <summary>
        /// Creates files for producing a formal proof for the given linear program.
        /// The precision is selected automatically
        /// </summary>
        static void ProcessLP(string fname, LpNumber upperBound, ListHyp hypermap)
        {
            Console.WriteLine("Processing {0}...", fname);
            try
            {
                for (int precision = 3; ; precision++)
                {
                    Console.WriteLine("Precision = {0}", precision);

                    if (hypermap != null)
                    {
                        if (ProcessLP(fname, upperBound, precision, hypermap))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (processLPGeneral(fname, precision, upperBound))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("ERROR: {0}", e.Message);
            }

            Console.WriteLine("done\n");
        }
Exemple #6
0
        // Adds an indexed inequality to a dictionary
        private void AddIndexedIneq(Inequality ineq, LpNumber marginal, ListHyp hypermap)
        {
            IndexedInequality newIneq = new IndexedInequality(ineq, marginal, hypermap);
            string            name    = ineq.Id.name;

            // Ad hoc treatment of sums over faces
            if (name == "sol_sum" || name == "tau_sum")
            {
                DartList face = hypermap.Manager.TranslateIneq(hypermap, ineq.Id) as DartList;
                if (face == null)
                {
                    throw new Exception("A face is expected for the constraint " + name);
                }

                name += face.list.Count.ToString();
            }

            if (ineq.type == Inequality.IneqType.Eq && ineq.NegFlag)
            {
                name += "_neg";
            }

            if (dict.ContainsKey(name))
            {
                dict[name].Add(newIneq);
            }
            else
            {
                var list = new List <IndexedInequality>();
                list.Add(newIneq);
                dict.Add(name, list);
                ineqNames.Add(name);
            }
        }
Exemple #7
0
        /// <summary>
        /// Creates files for producing a formal proof for the given linear program
        /// </summary>
        static bool ProcessLP(string fname, LpNumber upperBound, int precision, ListHyp hypermap, bool infeasible, bool holTerms)
        {
            if (precision > 7)
            {
                throw new Exception("Cannot solve the problem: " + fname);
            }

            LP         lp;
            LPSolution sol;

            // Load an LP
            FileStream fs = new FileStream(fname + ".lp", FileMode.Open);

            using (fs)
            {
                StreamReader r       = new StreamReader(fs);
                Scanner      scanner = new Scanner(r, fname + ".lp");

                lp = LP.ParseLP(scanner);
            }

            // Load solutions
            fs = new FileStream(fname + ".txt", FileMode.Open);
            using (fs)
            {
                sol = LPSolution.LoadSolution(new StreamReader(fs), precision, upperBound, infeasible);
            }

            if (!infeasible && sol.Optimal.value > upperBound.value)
            {
                throw new Exception("Optimal value is greater than " + upperBound.value + ": " + fname);
            }

            if (!lp.SetSolution(sol, precision, infeasible))
            {
                return(false);
            }

            // Create a test file containing all inequalities explicitly
//            FileStream test = new FileStream(fname + "_test.hl", FileMode.Create);
//            lp.ConvertToHOL(new StreamWriter(test), precision);
//            test.Close();

            // Create a certificate file
            FileStream main     = new FileStream(fname + "_out.hl", FileMode.Create);
            FileStream main_bin = new FileStream(fname + "_out.bin", FileMode.Create);

            lp.PrintCertificate(new StreamWriter(main), new BinaryWriter(main_bin), precision, hypermap, log, infeasible, holTerms);
            main_bin.Close();
            main.Close();

            // Debug file with text inequalities
//            FileStream text = new FileStream(fname + "_text.hl", FileMode.Create);
//            lp.PrintAllText(new StreamWriter(text), precision, hypermap);
//            text.Close();

            return(true);
        }
Exemple #8
0
        /// <summary>
        /// Changes the index of the variable
        /// </summary>
        /// <param name="hypermap"></param>
        public void Relabel(ListHyp hypermap)
        {
            // No index
            if (Name.index == "")
                return;

            HypermapElement element = hypermap.Manager.TranslateVariable(hypermap, Name);
            Name = new Label(Name.name, element.ToString());
        }
Exemple #9
0
        /// <summary>
        /// Changes the index of the variable
        /// </summary>
        /// <param name="hypermap"></param>
        public void Relabel(ListHyp hypermap)
        {
            // No index
            if (Name.index == "")
            {
                return;
            }

            HypermapElement element = hypermap.Manager.TranslateVariable(hypermap, Name);

            Name = new Label(Name.name, element.ToString());
        }
Exemple #10
0
 public HypermapElement TranslateIneq(ListHyp hypermap, Label ineqId)
 {
     try
     {
         string domain = constraints[ineqId.name].domain;
         return(hypermap.Translate(domain, ineqId.index));
     }
     catch (Exception e)
     {
         Console.WriteLine("TranslateIneq() Error: ineqId = {0}", ineqId);
         throw e;
     }
 }
Exemple #11
0
            /// <summary>
            /// Computes the index automatically
            /// </summary>
            public IndexedInequality(Inequality ineq, LpNumber marginal, ListHyp hypermap)
            {
                this.ineq     = ineq;
                this.marginal = marginal;

                if (ineq.Id.index == null || ineq.Id.index == "")
                {
                    this.index = 0;
                }
                else
                {
                    this.index = hypermap.Manager.FindIneqIndex(hypermap, ineq.Id);
                }
            }
Exemple #12
0
        /// <summary>
        /// Splits the hypermap using the given list of split darts
        /// </summary>
        /// <param name="splitDarts"></param>
        /// <returns></returns>
        public ListHyp Split(List <Dart> splitDarts)
        {
            ListHyp hyp = new ListHyp(Manager);

            hyp.Id = Id;

            var l2 = list;

            foreach (Dart d in splitDarts)
            {
                l2 = Split(l2, d);
            }

            hyp.list.AddRange(l2);
            return(hyp);
        }
Exemple #13
0
        /// <summary>
        /// Processes one Flyspeck LP
        /// </summary>
        /// <param name="fname"></param>
        static string ProcessFlyspeckLP(string fname, ListHypManager hypermaps)
        {
            FileStream info = new FileStream(fname, FileMode.Open);

            try
            {
                string  name;
                bool    infeasible;
                ListHyp hypermap = hypermaps.ComputeHypermap(new StreamReader(info), out name, out infeasible);

                ProcessLP(name, new LpNumber(12), hypermap, infeasible, false);
                return(name);
            }
            finally
            {
                info.Close();
            }
        }
Exemple #14
0
        /// <summary>
        /// Prints the given inequality into a stream
        /// </summary>
        private void WriteIneqText(Inequality ineq, LpNumber marginal, int precision,
                                   StreamWriter writer, ListHyp hypermap)
        {
            // Relabel
            HypermapElement element = hypermap.Manager.TranslateIneq(hypermap, ineq.Id);
            int             index   = hypermap.Manager.FindIneqIndex(hypermap, ineq.Id);

            // Write out
            if (ineq.NegFlag && ineq.type == Inequality.IneqType.Eq)
            {
                writer.Write("-");
            }
            writer.Write(ineq.Id + " & ");
            writer.Write(index + " & ");
            writer.Write(element + ": ");
            writer.Write(ineq.ToHOLString(precision, vars));
            writer.Write("\t (");
            writer.Write(marginal.ToHOLString(precision));
            writer.WriteLine(")");
        }
Exemple #15
0
        // Adds an indexed inequality to a dictionary
        private void AddIndexedIneq(Inequality ineq, LpNumber marginal, ListHyp hypermap)
        {
            IndexedInequality newIneq = new IndexedInequality(ineq, marginal, hypermap);
            string            name    = ineq.Id.name;

            if (ineq.type == Inequality.IneqType.Eq && ineq.NegFlag)
            {
                name += "_neg";
            }

            if (dict.ContainsKey(name))
            {
                dict[name].Add(newIneq);
            }
            else
            {
                var list = new List <IndexedInequality>();
                list.Add(newIneq);
                dict.Add(name, list);
                ineqNames.Add(name);
            }
        }
Exemple #16
0
        /// <summary>
        /// Changes indices of all variables using the given hypermap
        /// </summary>
        /// <param name="hypermap"></param>
        public void RelabelAllVariables(ListHyp hypermap)
        {
            if (relabeledFlag)
            {
                return;
            }

            relabeledFlag = true;

            int n = varList.Count;

            for (int i = 0; i < n; i++)
            {
                try
                {
                    varList[i].Relabel(hypermap);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw e;
                }
            }
        }
Exemple #17
0
        public int FindIneqIndex(ListHyp hypermap, Label ineqId)
        {
            try
            {
                if (!constraints.ContainsKey(ineqId.name))
                    throw new Exception("Constraint " + ineqId.name + " is not defined in 000.txt");

                Definition c = constraints[ineqId.name];
                HypermapElement element = hypermap.Translate(c.domain, ineqId.index);
                if (element == null)
                {
                    throw new Exception(
                           String.Format("Element with the index {0} is not found for the domain {1}", ineqId.index, c.domain));
                }

                return hypermap.FindElementIndex(c.set, element);
            }
            catch (Exception e)
            {
                throw new Exception(
                    String.Format("Inequality {0} problem: {1}", ineqId.name, e.Message));
            }
        }
Exemple #18
0
        /// <summary>
        /// Reads all hypermaps from a file
        /// </summary>
        public ListHypManager(TextReader tame_archive, TextReader definitions)
        {
            // Load all hypermaps
            data = new Dictionary<string, ListHyp>();

            while (tame_archive != null)
            {
                string str = tame_archive.ReadLine();
                if (str == null)
                    break;

                if (str.Length < 2)
                    continue;

                if (str[0] != '"')
                    continue;

                str = str.Substring(1, str.Length - 3);
                ListHyp hyp = new ListHyp(str, this);

                data.Add(hyp.Id, hyp);
            }

            // Load definitions
            Scanner s = new Scanner(definitions);
            Token t;

            // DefaultSets
            t = s.nextToken();
            if (t.StringValue != "DefaultSets")
                throw new Exception("DefaultSets expected: " + t);

            LoadDefaultSets(s);

            // Variable
            t = s.nextToken();
            if (t.StringValue != "Variables")
                throw new Exception("Variables expected: " + t);

            LoadVariables(s);

            // Constraints
            t = s.nextToken();
            if (t.StringValue != "Constraints")
                throw new Exception("Constraints expected: " + t);

            LoadConstraints(s);
            GenerateVariableInequalities();
        }
Exemple #19
0
        /// <summary>
        /// Computes a hypermap from input file data
        /// </summary>
        /// <param name="hypInfo"></param>
        /// <returns></returns>
        public ListHyp ComputeHypermap(TextReader hypInfo, out string name, out bool infeasible)
        {
            name = null;
            infeasible = false;
            string id = null;
            List<List<int>> hypList = null;
            List<Dart> splitDarts = new List<Dart>();
            List<int> facesPermutation = null;

            while (true)
            {
                string str = hypInfo.ReadLine();
                if (str == null)
                    break;

                string[] els = str.Split(':');
                if (els.Length != 2)
                {
                    Console.WriteLine("Bad line: " + str);
                    continue;
                }

                string val = els[1].Trim();

                switch (els[0].Trim())
                {
                    case "infeasible":
                        infeasible = (val == "true");
                        break;

                    case "name":
                        name = val;
                        break;

                    case "id":
                        id = val;
                        break;

                    case "hypermap":
                        if (val == "")
                            break;

                        hypList = new List<List<int>>();
                        els = val.Split(';');
                        foreach (string el in els)
                        {
                            List<int> f = el.Split(',').ToList().map(x => int.Parse(x));
                            hypList.Add(f);
                        }

                        break;

                    case "split":
                        if (val == "")
                            break;

                        els = val.Split(';');
                        splitDarts.Clear();

                        foreach (string el in els)
                        {
                            splitDarts.Add(Dart.Parse(el));
                        }
                        break;

                    case "faces":
                        facesPermutation = val.Split(',').ToList().map(x => int.Parse(x));
                        break;

                    default:
                        Console.WriteLine("Bad line: " + str);
                        break;
                }
            }

            if (name == null || facesPermutation == null)
                throw new Exception("ComputeHypermaps(): name or facesPermutation are not defined");

            if (id == null && hypList == null)
                throw new Exception("ComputeHypermaps(): both id and hypList are not defined");

            ListHyp hyp = null;
            if (hypList != null)
            {
                hyp = new ListHyp(hypList, this).Split(splitDarts);
            }
            else
            {
                hyp = this[id].Split(splitDarts);
            }

            hyp.ComputeAllSets(facesPermutation);
            return hyp;
        }
Exemple #20
0
        /// <summary>
        /// Creates files for producing a formal proof for the given linear program
        /// </summary>
        static bool ProcessLP(string fname, LpNumber upperBound, int precision, ListHyp hypermap)
        {
            if (precision > 5)
                throw new Exception("Cannot solve the problem: " + fname);

            LP lp;
            LPSolution sol;

            // Load an LP
            FileStream fs = new FileStream(fname + ".lp", FileMode.Open);
            using (fs)
            {
                StreamReader r = new StreamReader(fs);
                Scanner scanner = new Scanner(r, fname + ".lp");

                lp = LP.ParseLP(scanner);
            }

            // Load solutions
            fs = new FileStream(fname + ".txt", FileMode.Open);
            using (fs)
            {
                sol = LPSolution.LoadSolution(new StreamReader(fs), precision, upperBound);
            }

            if (sol.Optimal.value > upperBound.value)
                throw new Exception("Optimal value is greater than " + upperBound.value + ": " + fname);

            if (!lp.SetSolution(sol, precision))
                return false;

            // Create a test file containing all inequalities explicitly
            FileStream test = new FileStream(fname + "_test.hl", FileMode.Create);
            lp.ConvertToHOL(new StreamWriter(test), precision);
            test.Close();

            // Create a certificate file
            FileStream main = new FileStream(fname + "_out.hl", FileMode.Create);
            lp.PrintCertificate(new StreamWriter(main), precision, hypermap, log);
            main.Close();

            return true;
        }
Exemple #21
0
        /// <summary>
        /// Splits the hypermap using the given list of split darts
        /// </summary>
        /// <param name="splitDarts"></param>
        /// <returns></returns>
        public ListHyp Split(List<Dart> splitDarts)
        {
            ListHyp hyp = new ListHyp(Manager);
            hyp.Id = Id;

            var l2 = list;
            foreach (Dart d in splitDarts)
            {
                l2 = Split(l2, d);
            }

            hyp.list.AddRange(l2);
            return hyp;
        }
Exemple #22
0
        /// <summary>
        /// Changes indices of all variables using the given hypermap
        /// </summary>
        /// <param name="hypermap"></param>
        public void RelabelAllVariables(ListHyp hypermap)
        {
            if (relabeledFlag)
                return;

            relabeledFlag = true;

            int n = varList.Count;
            for (int i = 0; i < n; i++)
            {
                try
                {
                    varList[i].Relabel(hypermap);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw e;
                }
            }
        }
Exemple #23
0
        /// <summary>
        /// Creates files for producing a formal proof for the given linear program.
        /// The precision is selected automatically
        /// </summary>
        static void ProcessLP(string fname, LpNumber upperBound, ListHyp hypermap)
        {
            Console.WriteLine("Processing {0}...", fname);
            try
            {
                for (int precision = 3; ; precision++)
                {
                    Console.WriteLine("Precision = {0}", precision);

                    if (hypermap != null)
                    {
                        if (ProcessLP(fname, upperBound, precision, hypermap))
                            break;
                    }
                    else
                    {
                        if (processLPGeneral(fname, precision, upperBound))
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("ERROR: {0}", e.Message);
            }

            Console.WriteLine("done\n");
        }
Exemple #24
0
 /// <summary>
 /// Computes the index automatically
 /// </summary>
 public IndexedInequality(Inequality ineq, LpNumber marginal, ListHyp hypermap)
 {
     this.ineq     = ineq;
     this.marginal = marginal;
     this.index    = hypermap.Manager.FindIneqIndex(hypermap, ineq.Id);
 }
Exemple #25
0
        /// <summary>
        /// Prints all inequalities in a text file using HOL syntax
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="precision"></param>
        public void PrintAllText(StreamWriter writer, int precision, ListHyp hypermap)
        {
            // Find target variables
            foreach (var term in objective.Terms)
            {
                vars[term.varName].TargetVariable = true;
            }

            // Compute the precision constant
            decimal mul = 1;

            for (int i = 0; i < precision; i++)
            {
                mul *= 10;
            }

            // Target
            writer.WriteLine("target: " + objective.ToHOLString(precision, vars));

            // Precision
            writer.WriteLine("precision_constant: " + mul);

            // Constraints
            int counter = 0;

            vars.RelabelAllVariables(hypermap);

            for (int i = 0; i < ineqs.Count; i++)
            {
                Inequality ineq = ineqs[i];
                LpNumber   m    = ineq.Marginal;

                if (ineq.Id.name == "lnsum_def")
                {
                    throw new Exception("lnsum_def cannot be active");
                }

                // Ignore zero values
                if (m.IsZero(precision))
                {
                    continue;
                }

                WriteIneqText(ineq * mul, m * mul, precision, writer, hypermap);
                counter++;
            }

            writer.WriteLine();
            writer.WriteLine("Inequalities for constraints: {0}", counter);
            writer.WriteLine("-------------------------------------------------------");
            writer.WriteLine();

            // Variables

            // Sort variables
            varIneqs.Sort(new VarsComparer(vars));

            // Target variables first
            writer.WriteLine("Target variables:");
            counter = 0;
            for (; counter < varIneqs.Count; counter++)
            {
                Inequality ineq = varIneqs[counter];
                if (!vars[ineq.lhs.Terms.First().varName].TargetVariable)
                {
                    break;
                }

                LpNumber m = ineq.Marginal; //varIneqMarginals[i];

                m   *= mul * mul;
                ineq = ineq * mul;

                WriteIneqText(ineq, m, precision, writer, hypermap);
            }

            writer.WriteLine("-------------------------------------------------------");
            writer.WriteLine();

            // All other variables
            writer.WriteLine("Variables:");

            for (int i = counter; i < varIneqs.Count; i++)
            {
                Inequality ineq = varIneqs[i];
                LpNumber   m    = ineq.Marginal; //varIneqMarginals[i];

                if (m.IsZero(precision))
                {
                    throw new Exception("Unexpected zero");
                }

                /*                            if (i < varIneqs.Count - 1)
                 *                          {
                 *                              Inequality ineq2 = varIneqs[i + 1];
                 *                              LpNumber m2 = ineq2.Marginal;
                 *
                 *                              if (m2.IsZero(precision))
                 *                                  throw new Exception("Unexpected zero");
                 *
                 *                              if (ineq.lhs.Terms.First().varName == ineq2.lhs.Terms.First().varName)
                 *                              {
                 *                                  m *= mul * mul;
                 *                                  m2 *= mul * mul;
                 *                                  ineq = ineq * mul;
                 *                                  ineq2 = ineq2 * mul;
                 *
                 *                                  writer.Write("var2_le_transform (");
                 *                                  writer.Write(ineq.ToHOLExplicit(precision, vars));
                 *                                  writer.Write(", ");
                 *                                  writer.Write(m.ToHOLExplicit(precision));
                 *                                  writer.Write(") ");
                 *
                 *                                  writer.Write("(");
                 *                                  writer.Write(ineq2.ToHOLExplicit(precision, vars));
                 *                                  writer.Write(", ");
                 *                                  writer.Write(m2.ToHOLExplicit(precision));
                 *                                  writer.WriteLine(");");
                 *
                 *                                  i++;
                 *                                  continue;
                 *                              }
                 *                          }
                 */
                m   *= mul * mul;
                ineq = ineq * mul;

                WriteIneqText(ineq, m, precision, writer, hypermap);
            }

            writer.WriteLine("Inequalities for variables: {0}", varIneqs.Count);


            writer.Flush();
        }
Exemple #26
0
        public HypermapElement TranslateIneq(ListHyp hypermap, Label ineqId)
        {
            string domain = constraints[ineqId.name].domain;

            return(hypermap.Translate(domain, ineqId.index));
        }
Exemple #27
0
 public int FindIneqIndex(ListHyp hypermap, Label ineqId)
 {
     Definition c = constraints[ineqId.name];
     HypermapElement element = hypermap.Translate(c.domain, ineqId.index);
     return hypermap.FindElementIndex(c.set, element);
 }
Exemple #28
0
        /// <summary>
        /// Returns a hypermap element corresponding to the variable name
        /// </summary>
        /// <param name="varName"></param>
        /// <returns></returns>
        public HypermapElement TranslateVariable(ListHyp hypermap, Label varName)
        {
            string domain = variables[varName.name].domain;

            return(hypermap.Translate(domain, varName.index));
        }
Exemple #29
0
        /// <summary>
        /// Reads all hypermaps from a file
        /// </summary>
        public ListHypManager(TextReader tame_archive, TextReader definitions)
        {
            // Load all hypermaps
            data = new Dictionary <string, ListHyp>();

            while (true)
            {
                string str = tame_archive.ReadLine();
                if (str == null)
                {
                    break;
                }

                if (str.Length < 2)
                {
                    continue;
                }

                if (str[0] != '"')
                {
                    continue;
                }

                str = str.Substring(1, str.Length - 3);
                ListHyp hyp = new ListHyp(str, this);

                data.Add(hyp.Id, hyp);
            }

            // Load definitions
            Scanner s = new Scanner(definitions);
            Token   t;

            // DefaultSets
            t = s.nextToken();
            if (t.StringValue != "DefaultSets")
            {
                throw new Exception("DefaultSets expected: " + t);
            }

            LoadDefaultSets(s);

            // Variable
            t = s.nextToken();
            if (t.StringValue != "Variables")
            {
                throw new Exception("Variables expected: " + t);
            }

            LoadVariables(s);

            // Constraints
            t = s.nextToken();
            if (t.StringValue != "Constraints")
            {
                throw new Exception("Constraints expected: " + t);
            }

            LoadConstraints(s);
            GenerateVariableInequalities();
        }
Exemple #30
0
 public HypermapElement TranslateIneq(ListHyp hypermap, Label ineqId)
 {
     string domain = constraints[ineqId.name].domain;
     return hypermap.Translate(domain, ineqId.index);
 }
Exemple #31
0
 public HypermapElement TranslateIneq(ListHyp hypermap, Label ineqId)
 {
     try
     {
         string domain = constraints[ineqId.name].domain;
         return hypermap.Translate(domain, ineqId.index);
     }
     catch (Exception e)
     {
         Console.WriteLine("TranslateIneq() Error: ineqId = {0}", ineqId);
         throw e;
     }
 }
Exemple #32
0
        /// <summary>
        /// Creates a HOL certificate
        /// </summary>
        public void PrintCertificate(StreamWriter writer, int precision, ListHyp hypermap, StreamWriter log)
        {
            // Find target variables
            foreach (var term in objective.Terms)
            {
                vars[term.varName].TargetVariable = true;
            }

            // Compute the precision constant
            decimal mul = 1;

            for (int i = 0; i < precision; i++)
            {
                mul *= 10;
            }

            // Head
            writer.WriteLine("needs \"nobranching_lp.hl\";;\n");
            writer.WriteLine("module Test_case = struct");

            // Parameters
            writer.WriteLine("let hypermap_string = \"" + hypermap.rawString + "\";;");
            writer.WriteLine("let precision = " + precision + ";;");

            dict.Clear();
            ineqNames.Clear();

            // Constraints
            int counter = 0;

            vars.RelabelAllVariables(hypermap);

            writer.WriteLine("(***************)");
            writer.WriteLine("(* Constraints *)");
            writer.WriteLine("(***************)");

            for (int i = 0; i < ineqs.Count; i++)
            {
                Inequality ineq = ineqs[i];
                LpNumber   m    = ineq.Marginal;

                if (ineq.Id.name == "lnsum_def")
                {
                    throw new Exception("lnsum_def cannot be active");
                }

                // Ignore zero values
                if (m.IsZero(precision))
                {
                    continue;
                }

                ineq *= mul;
                m    *= mul;
                AddIndexedIneq(ineq, m, hypermap);
            }

            writer.WriteLine("let constraints = [");
            SaveIndexedIneqs(writer, precision);
            writer.WriteLine("];;");

            // Variables
            writer.WriteLine();
            writer.WriteLine("(***************)");
            writer.WriteLine("(* Variables   *)");
            writer.WriteLine("(***************)");


            // Sort variables
            varIneqs.Sort(new VarsComparer(vars));

            // Target variables first
            dict.Clear();
            ineqNames.Clear();
            counter = 0;
            for (; counter < varIneqs.Count; counter++)
            {
                Inequality ineq = varIneqs[counter];
                if (!vars[ineq.lhs.Terms.First().varName].TargetVariable)
                {
                    break;
                }

                LpNumber m = ineq.Marginal;

                m   *= mul * mul;
                ineq = ineq * mul;

                AddIndexedIneq(ineq, m, hypermap);
            }

            writer.WriteLine("let target_variables = [");
            SaveIndexedIneqs(writer, precision);
            writer.WriteLine("];;");

            writer.WriteLine();
            writer.WriteLine("(*************************)");
            writer.WriteLine();

            // All other variables
            dict.Clear();
            ineqNames.Clear();

            for (int i = counter; i < varIneqs.Count; i++)
            {
                Inequality ineq = varIneqs[i];
                LpNumber   m    = ineq.Marginal;

                m   *= mul * mul;
                ineq = ineq * mul;

                AddIndexedIneq(ineq, m, hypermap);
            }

            writer.WriteLine("let variable_bounds = [");
            SaveIndexedIneqs(writer, precision);
            writer.WriteLine("];;");

            // Tail
            writer.WriteLine("let result = prove_hypermap_lp hypermap_string precision constraints target_variables variable_bounds;;");
            writer.WriteLine("end;;");
            writer.WriteLine();
            writer.WriteLine("concl (Test_case.result)");

            writer.Flush();

            log.WriteLine("{0}\t{1}\t{2}", hypermap.Id, ineqs.Count + varIneqs.Count, vars.Number);
        }
Exemple #33
0
 /// <summary>
 /// Returns a hypermap element corresponding to the variable name
 /// </summary>
 /// <param name="varName"></param>
 /// <returns></returns>
 public HypermapElement TranslateVariable(ListHyp hypermap, Label varName)
 {
     string domain = variables[varName.name].domain;
     return hypermap.Translate(domain, varName.index);
 }
Exemple #34
0
        /// <summary>
        /// Computes a hypermap from input file data
        /// </summary>
        /// <param name="hypInfo"></param>
        /// <returns></returns>
        public ListHyp ComputeHypermap(TextReader hypInfo, out string name, out bool infeasible)
        {
            name       = null;
            infeasible = false;
            string             id               = null;
            List <List <int> > hypList          = null;
            List <Dart>        splitDarts       = new List <Dart>();
            List <int>         facesPermutation = null;

            while (true)
            {
                string str = hypInfo.ReadLine();
                if (str == null)
                {
                    break;
                }

                string[] els = str.Split(':');
                if (els.Length != 2)
                {
                    Console.WriteLine("Bad line: " + str);
                    continue;
                }

                string val = els[1].Trim();

                switch (els[0].Trim())
                {
                case "infeasible":
                    infeasible = (val == "true");
                    break;

                case "name":
                    name = val;
                    break;

                case "id":
                    id = val;
                    break;

                case "hypermap":
                    if (val == "")
                    {
                        break;
                    }

                    hypList = new List <List <int> >();
                    els     = val.Split(';');
                    foreach (string el in els)
                    {
                        List <int> f = el.Split(',').ToList().map(x => int.Parse(x));
                        hypList.Add(f);
                    }

                    break;

                case "split":
                    if (val == "")
                    {
                        break;
                    }

                    els = val.Split(';');
                    splitDarts.Clear();

                    foreach (string el in els)
                    {
                        splitDarts.Add(Dart.Parse(el));
                    }
                    break;

                case "faces":
                    facesPermutation = val.Split(',').ToList().map(x => int.Parse(x));
                    break;

                default:
                    Console.WriteLine("Bad line: " + str);
                    break;
                }
            }

            if (name == null || facesPermutation == null)
            {
                throw new Exception("ComputeHypermaps(): name or facesPermutation are not defined");
            }

            if (id == null && hypList == null)
            {
                throw new Exception("ComputeHypermaps(): both id and hypList are not defined");
            }

            ListHyp hyp = null;

            if (hypList != null)
            {
                hyp = new ListHyp(hypList, this).Split(splitDarts);
            }
            else
            {
                hyp = this[id].Split(splitDarts);
            }

            hyp.ComputeAllSets(facesPermutation);
            return(hyp);
        }