Esempio n. 1
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            const double R        = 1e15;
            const double RSquared = R * R;
            var          sw       = new Stopwatch();

            sw.Start();

            var n      = inputStream.ReadInt();
            var points = new Point[n];

            for (int i = 0; i < points.Length; i++)
            {
                var(x, y) = inputStream.ReadValue <int, int>();
                points[i] = new Point(x, y);
            }

            var counts   = new int[n];
            var countSum = 0;
            var rand     = new XorShift();


            while (sw.ElapsedMilliseconds < 1850)
            {
                var min   = double.MaxValue;
                var index = -1;

                unchecked
                {
                    var x = rand.NextDouble() * 2 * R - R;
                    var y = rand.NextDouble() * 2 * R - R;

                    if (x * x + y * y <= RSquared)
                    {
                        for (int i = 0; i < points.Length; i++)
                        {
                            var dist = points[i].GetDistanceSquared(x, y);
                            if (dist < min)
                            {
                                index = i;
                                min   = dist;
                            }
                        }

                        counts[index]++;
                        countSum++;
                    }
                }
            }

            foreach (var c in counts)
            {
                yield return((double)c / countSum);
            }
        }
Esempio n. 2
0
        public void Annealing(Stopwatch sw)
        {
            var random      = new XorShift();
            var count       = 0;
            var startTime   = sw.ElapsedMilliseconds;
            var temperature = CalculateTemp(startTime, startTime, TimeLimit);

            while (true)
            {
                if (count++ % 10000 == 0)
                {
                    temperature = CalculateTemp(sw.ElapsedMilliseconds, startTime, TimeLimit);

                    if (sw.ElapsedMilliseconds >= TimeLimit)
                    {
                        break;
                    }
                }

                var cardA = random.Next(_takeOrderInv.Length);
                var cardB = random.Next(_takeOrderInv.Length);

                if (cardA == cardB)
                {
                    continue;
                }

                var prev = CalculateLocal(cardA, cardB);

                Swap(ref _takeOrderInv[cardA], ref _takeOrderInv[cardB]);
                Swap(ref _takeOrder[_takeOrderInv[cardA]], ref _takeOrder[_takeOrderInv[cardB]]);
                Swap(ref _compressed[cardA], ref _compressed[cardB]);

                var next = CalculateLocal(cardA, cardB);

                // 大きい方が優秀
                var diff = prev - next;

                if (!(diff >= 0 || random.NextDouble() <= Math.Exp(diff / temperature)))
                {
                    Swap(ref _takeOrderInv[cardA], ref _takeOrderInv[cardB]);
                    Swap(ref _takeOrder[_takeOrderInv[cardA]], ref _takeOrder[_takeOrderInv[cardB]]);
                    Swap(ref _compressed[cardA], ref _compressed[cardB]);
                }
            }
        }
Esempio n. 3
0
 private bool IsAcceptableScore(int lastScore, int newScore, double temperature)
 {
     return(newScore < Inf && (newScore >= lastScore || _xorShift.NextDouble() < Math.Exp((newScore - lastScore) / temperature)));
 }
 public override void WithXorShift()
 {
     XorShift.NextDouble();
 }