Esempio n. 1
1
        public static void ReadDecompositions()
        {
            string[] files = Directory.GetFiles("Decompositions/DoubleBFS", "*.bdc", SearchOption.AllDirectories).ToArray();

            ConsoleTable<ExpandoObject> table = new ConsoleTable<ExpandoObject>();
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("Name", "{0}", files.Max(s => s.Length) - "Decompositions\\".Length, s => s.GetOrNull("Name")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("#V", "{0}", 4, s => s.GetOrNull("Vertices")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("BW", "{0:0.00}", 5, s => s.GetOrNull("BooleanWidth")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("LBW", "{0:0.00}", 5, s => s.GetOrNull("LinearBooleanWidth")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("Size", "{0}", 5, s => s.GetOrNull("Size")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("Size right", "{0}", 10, s => s.GetOrNull("RightSize")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("DFS", "{0:0.00}", 5, s => s.GetOrNull("DFS")));
            table.Columns.Add(new ConsoleColumn<ExpandoObject>("DFS Time", "{0}", 20, s => s.GetOrNull("DFSTime")));

            IDictionary<string, Graph> graphs = new Dictionary<string, Graph>();
            dynamic[] expandos = new dynamic[files.Length];
            for(int i = 0; i < files.Length; i++)
            {
                dynamic obj = new ExpandoObject();
                table.Rows.Add(obj);
                obj.FileName = files[i];
                obj.Name = files[i].Substring("Decompositions\\".Length);
                expandos[i] = obj;
                string graph = Path.GetFileNameWithoutExtension(files[i]);
                if (!graphs.ContainsKey(graph))
                {
                    using (
                        StreamReader reader =
                            new StreamReader(
                                File.Open("Graphs/sadiagraphs/" + Path.GetFileNameWithoutExtension(files[i]) + ".dgf",
                                    FileMode.Open)))
                    {
                        graphs[graph] = Graph.Read(reader);
                    }
                }
            }

            Parallel.ForEach(expandos, dyn =>
            //foreach (dynamic dyn in expandos)
            {
                using (StreamReader decompReader = new StreamReader(File.Open(dyn.FileName, FileMode.Open)))
                {
                    Graph graph = graphs[Path.GetFileNameWithoutExtension(dyn.FileName)];
                    dyn.Vertices = graph.Vertices.Count;
                    Decomposition decomposition = Decomposition.Read(decompReader, graph);

                    BinTree tree = (BinTree)decomposition.Tree;
                    tree.Tilt();
                    decomposition = new Decomposition(graph, (Tree)tree);

                    dyn.BooleanWidth = decomposition.BooleanWidth;

                    {
                        int size = 0;
                        BitSet parent = decomposition.Tree.Root;
                        do
                        {
                            size = Math.Max(size, decomposition.Tree.LeftChild[parent].Count);
                        } while (decomposition.Tree.RightChild.TryGetValue(parent, out parent) && parent.Count > 1);
                        dyn.RightSize = size;
                    }

                    {
                        int size = 0;
                        BitSet parent = decomposition.Tree.Root;
                        do
                        {
                            size += decomposition.Tree.LeftChild[parent].Count - 1;
                        } while (decomposition.Tree.RightChild.TryGetValue(parent, out parent) && parent.Count > 1);
                        dyn.Size = size;
                    }

                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();
                        LinearDecomposition ld = SemiLinearDecomposer.ConvertDecomposition(decomposition);
                        sw.Stop();
                        dyn.DFS = ld.BooleanWidth;
                        dyn.DFSTime = sw.Elapsed;
                    }

                    {
                        LinearDecomposition ld = IunHeuristic.Compute(graph, CandidateStrategy.All, InitialVertexStrategy.DoubleBfs);
                        dyn.LinearBooleanWidth = ld.BooleanWidth;
                        SaveDecomposition(ld, Path.GetDirectoryName(dyn.FileName), Path.GetFileNameWithoutExtension(dyn.FileName));
                    }

                    //SaveDecomposition(decomposition, "NewDec", Path.GetFileNameWithoutExtension(dyn.FileName));
                    //dyn.BooleanWidth = decomposition.BooleanWidth;
                }
            });
        }
        public static LinearDecomposition ConvertDecomposition(Decomposition decomposition)
        {
            BinTree tree = (BinTree)decomposition.Tree;
            tree.Tilt();
            BooleanChain chain = BooleanChain.DepthFirstSearch(tree.Left.Item, decomposition.MaxNeighborhoodSize, int.MaxValue, BooleanChain.FromGraph(decomposition.Graph, tree.Left.Item).ToArray());

            BinTree node = tree;
            while (node.Right != null)
            {
                node = node.Right;
                if (node.Left == null)
                {
                    chain = new BooleanChain(chain, node.Item.First());
                }
                else if (node.Left.Item.Count == 1)
                {
                    chain = new BooleanChain(chain, node.Left.Item.First());
                }
                else
                {
                    chain = BooleanChain.DepthFirstSearch(node.Left.Item, decomposition.MaxNeighborhoodSize, int.MaxValue, chain);
                }
            }
            return (LinearDecomposition)chain;
        }
Esempio n. 3
0
        private static void SemiLinear(dynamic expando)
        {
            Graph gr;
            using (TextReader reader = new StreamReader(File.Open(expando.FileName, FileMode.Open)))
            {
                gr = Graph.Read(reader);
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();
            Graph gr2 = gr.Clone();
            IReductionRuleCommand[] commands = GraphPreprocessor.ApplyRules(gr2, new TwinReductionRule(), new PendantReductionRule(), new IsletReductionRule()).Reverse().ToArray();
            expando.Vertices = gr2.Vertices.Count;
            if (commands.Length != 0)
            {
                LinearDecomposition ld = IunHeuristic.Compute(gr2, CandidateStrategy.All, InitialVertexStrategy.All);
                Decomposition dec = Decomposition.FromLinearDecomposition(ld);
                Tree tree = dec.Tree;
                foreach (IReductionRuleCommand command in commands)
                {
                    tree = command.Expand(tree);
                }
                dec = new Decomposition(gr, tree);

                sw.Stop();
                expando.Time = sw.Elapsed;
                //expando.BooleanWidth = dec.BooleanWidth;

                SaveDecomposition(dec, "Decompositions2", Path.GetFileNameWithoutExtension(expando.FileName));
                expando.Written = "Done";
            }
            else
            {
                expando.BooleanWidth = "-";
                expando.Time = "SKIP";
            }
        }
Esempio n. 4
0
        private static void SaveDecomposition(Decomposition decomposition, string folder, string fileName)
        {
            Directory.CreateDirectory(folder);
            using (
                TextWriter writer =
                    new StreamWriter(
                        File.Open(
                            folder + "/" + fileName + ".bdc",
                            FileMode.Create)))
            {
                decomposition.Write(writer);
                writer.Flush();
            }
            Directory.CreateDirectory(folder + "/LaTeX/");
            using (
                TextWriter writer =
                    new StreamWriter(
                        File.Open(
                            folder + "/LaTeX/" + fileName + ".tex",
                            FileMode.Create)))
            {
                decomposition.Tree.WriteLatex(writer);
                writer.Flush();
            }
            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "lualatex";
            p.StartInfo.Arguments = "\"" + folder + "/LaTeX/" + fileName + ".tex" + "\"";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

            if (File.Exists(folder + "/LaTeX/" + fileName + ".pdf"))
            {
                File.Delete(folder + "/LaTeX/" + fileName + ".pdf");
            }
            File.Move(fileName + ".pdf", folder + "/LaTeX/" + fileName + ".pdf");

            File.Delete(fileName + ".log");
            File.Delete(fileName + ".aux");
        }
Esempio n. 5
0
 /*************************/
 // Constructor
 /*************************/
 // Basic constructor
 public GenericSolver(Decomposition decomposition)
 {
     // The graph and binary decomposition tree that we are working on
     _graph = decomposition.Graph;
     _tree = decomposition.Tree;
 }