Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Directory.CreateDirectory("pathes");
            foreach (var file in Directory.EnumerateFiles(FileHelper.PatchDirectoryName("clusters.v2")).OrderBy(x => int.Parse(pathRegex.Match(x).Groups[1].Value)))
            {
                var code        = $"{int.Parse(pathRegex.Match(file).Groups[1].Value):D3}";
                var resFileName = Path.Combine(FileHelper.PatchDirectoryName("clusters.v2"), $"prob-{code}.path");
                if (File.Exists(resFileName))
                {
                    continue;
                }

                Console.Out.WriteLine(code);

                var problem = ProblemReader.Read(int.Parse(pathRegex.Match(file).Groups[1].Value));

                var records = File.ReadAllLines(file)
                              .Select(JsonConvert.DeserializeObject <ClusterRecord>)
                              .ToList();
                var startRecord = records.First(r => new V(r.X, r.Y).Equals(problem.Point));
                var hierarchy   = new ClusterHierarchy(records);
                hierarchy.CalculateDistancesBetweenChilds();
                var path = hierarchy.BuildPath(startRecord.cluster_hierarchy, null, new List <int>());

                //File.WriteAllLines($"pathes/prob-{code}", path.Select(p => p.Points[p.Points.Count / 2]).Select(p => $"{p.X}\t{p.Y}"));
                File.WriteAllLines(resFileName, path.Select(p => p.Id.ToString()));
            }
        }
        public ClusterHierarchy(List <ClusterRecord> records)
        {
            Level = records[0].cluster_hierarchy.Length;

            var near             = new V[] { new V(0, 1), new V(0, -1), new V(-1, 0), new V(1, 0) };
            var points           = records.ToDictionary(r => new V(r.X, r.Y));
            var levelIdToCluster = new ConcurrentDictionary <(int, int), ClusterHierarchy>();

            foreach (var record in records)
            {
                ClusterHierarchy parentCluster = this;
                var point = new V(record.X, record.Y);
                for (int i = record.cluster_hierarchy.Length - 1; i >= 0; i--)
                {
                    var cluster = levelIdToCluster.GetOrAdd((i, record.cluster_hierarchy[i]), _ => new ClusterHierarchy(record.cluster_hierarchy[i], i));
                    cluster.Parent = parentCluster;
                    cluster.Points.Add(point);
                    parentCluster.Childs[cluster.Id] = cluster;
                    foreach (var n in near)
                    {
                        var adjancent = point + n;
                        if (!points.TryGetValue(adjancent, out var adjNode))
                        {
                            continue;
                        }
                        var adjCluster = levelIdToCluster.GetOrAdd(
                            (i, adjNode.cluster_hierarchy[i]),
                            _ => new ClusterHierarchy(adjNode.cluster_hierarchy[i], i));
                        cluster.Edges[adjCluster.Id] = adjCluster;
                    }
                    parentCluster = cluster;
                }
            }
        }