예제 #1
0
        public static PortSet FindAllPorts(IEnumerable <Signal> rootSignals, bool ignoreHold)
        {
            CollectVisitor visitor = new CollectVisitor();

            AllPortsStrategy.Instance.Traverse(rootSignals, visitor, ignoreHold);
            return(visitor.Ports);
        }
예제 #2
0
        public static PortSet FindAllPorts(Port rootPort, bool ignoreHold)
        {
            CollectVisitor visitor = new CollectVisitor();

            AllPortsStrategy.Instance.Traverse(rootPort, visitor, ignoreHold);
            return(visitor.Ports);
        }
예제 #3
0
        public static SignalSet FindAllSignals(Signal rootSignal, bool ignoreHold)
        {
            CollectVisitor visitor = new CollectVisitor();

            AllSignalsStrategy.Instance.Traverse(rootSignal, visitor, ignoreHold);
            return(visitor.Signals);
        }
예제 #4
0
        public static void FindAll(IEnumerable <Signal> rootSignals, bool ignoreHold, out SignalSet signals, out PortSet ports)
        {
            CollectVisitor visitor = new CollectVisitor();

            AllSpanningTreeStrategy.Instance.Traverse(rootSignals, visitor, ignoreHold);
            signals = visitor.Signals;
            ports   = visitor.Ports;
        }
예제 #5
0
        /// <summary>
        ///   Writes the specified writer.
        /// </summary>
        /// <param name = "writer">The writer.</param>
        public void Write(ProjectWriter writer)
        {
            // 1. Collect all the objects
            CollectVisitor collectVisitor = new CollectVisitor();

            this.RootObject.Accept(collectVisitor);
            IDictionary <IPBXElement, String> map = collectVisitor.Map;

            this.Mapping = map;

            // 2. Ouput the prologue
            writer.WriteLine("// !$*UTF8*$!");
            writer.WriteLine("{");
            writer.WritePBXProperty(1, map, "archiveVersion", this.ArchivedVersion);
            writer.WritePBXProperty(1, map, "classes", new Dictionary <String, String>().ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
            writer.WritePBXProperty(1, map, "objectVersion", (int)this.ObjectVersion);

            // 3. Output the objects by nature
            writer.WriteIndent(1);
            writer.WriteLine("{0} = {{", "objects");
            writer.WriteLine();

            // Iterate and output being/end section before dumping objects
            IEnumerable <KeyValuePair <IPBXElement, String> > list = from entry in map orderby entry.Key.Isa ascending select entry;
            PBXElementType currentType = PBXElementType.None;

            foreach (KeyValuePair <IPBXElement, string> pair in list)
            {
                if (currentType != pair.Key.Nature)
                {
                    if (currentType != PBXElementType.None)
                    {
                        writer.WriteLine("/* End {0} section */", currentType);
                        writer.WriteLine();
                    }
                    currentType = pair.Key.Nature;
                    writer.WriteLine("/* Begin {0} section */", currentType);
                }

                pair.Key.WriteTo(writer, map);
            }

            if (currentType != PBXElementType.None)
            {
                writer.WriteLine("/* End {0} section */", currentType);
                writer.WriteLine();
            }

            writer.WriteIndent(1);
            writer.WriteLine("};");

            // 4. Output the epilogue
            writer.WritePBXProperty(1, map, "rootObject", this.RootObject);
            writer.WriteLine("}");
        }
예제 #6
0
        public static void FindExplorePath2Opt(ExploreCell start_cell, List <ExploreCell> explore_cells, CellsDistancer distances, ref List <ExploreCell> path)
        {
            using (new Profiler($"2-Opt %t"))
            {
                path = path ?? new List <ExploreCell>();
                path.Clear();

                if (start_cell == null)
                {
                    return;
                }

                //List<int> cells_id_in_start_cell_network = distances.GetConnectedTo(start_cell.GlobalId);
                //List<ExploreCell> best_tour_old = explore_cells.FindAll(c => cells_id_in_start_cell_network.Contains(c.GlobalId));

                // reordering cell in breadth-first order seems to help with 2-opt backtracking
                var collect_cells_visitor = new CollectVisitor();
                Algorihms.VisitBreadth(start_cell, visitor: collect_cells_visitor);
                var best_tour = collect_cells_visitor.cells.Select(x => x as ExploreCell).Intersect(explore_cells).ToList();

                best_tour.RemoveAll(c => c.Explored || c.Neighbours.Count == 0);

                if (start_cell.Explored) // re-insert start cell if needed
                {
                    best_tour.Insert(0, start_cell);
                }

                if (best_tour.Count == 1)
                {
                    path.Add(best_tour[0]);
                    return;
                }

                float best_dist = GetTravelDistance(best_tour, distances);


                if (best_tour.Count < 90)
                {
                    // based on http://en.wikipedia.org/wiki/2-opt

                    while (true)
                    {
                        bool better_found = false;

                        for (int i = 1; i < best_tour.Count - 1; ++i)
                        {
                            for (int k = i + 1; k < best_tour.Count; ++k)
                            {
                                float new_dist = Swap2OptDistance(best_tour, distances, i, k);

                                if (new_dist < best_dist)
                                {
                                    Swap2Opt(ref best_tour, i, k);
                                    best_dist = new_dist;

                                    better_found = true;
                                    break;
                                }
                            }

                            if (better_found)
                            {
                                break;
                            }
                        }

                        if (!better_found)
                        {
                            break;
                        }
                    }
                }
                else // greedy
                {
                    // based on http://on-demand.gputechconf.com/gtc/2014/presentations/S4534-high-speed-2-opt-tsp-solver.pdf

                    while (true)
                    {
                        float min_change = 0;

                        int min_i = -1;
                        int min_j = -1;

                        for (int i = 0; i < best_tour.Count - 2; ++i)
                        {
                            for (int j = i + 2; j < best_tour.Count - 1; ++j)
                            {
                                int city_a = best_tour[i].GlobalId;
                                int city_b = best_tour[i + 1].GlobalId;
                                int city_c = best_tour[j].GlobalId;
                                int city_d = best_tour[j + 1].GlobalId;

                                float change = (distances.GetDistance(city_a, city_c) + distances.GetDistance(city_b, city_d)) -
                                               (distances.GetDistance(city_a, city_b) + distances.GetDistance(city_c, city_d));

                                if (change < min_change)
                                {
                                    min_change = change;
                                    min_i      = i + 1;
                                    min_j      = j;
                                }
                            }
                        }

                        if (min_change >= 0)
                        {
                            break;
                        }

                        // apply min_i/min_j move
                        ExploreCell t = best_tour[min_i];
                        best_tour[min_i] = best_tour[min_j];
                        best_tour[min_j] = t;
                    }
                }

                Trace.WriteLine("[FindExplorePath 2-Opt] Final solution distance: " + GetTravelDistance(best_tour, distances));

                foreach (ExploreCell cell in best_tour)
                {
                    path.Add(cell);
                }

                if (start_cell.Explored)
                {
                    path.RemoveAt(0);
                }
            }
        }