Ejemplo n.º 1
0
        public bool add(T e)
        {
            IPriorityQueueHandle <T> h = null;
            bool r = queue.Add(ref h, e);

            dict.Add(e, h);
            return(r);
        }
Ejemplo n.º 2
0
        private static long Solve()
        {
            Console.Out.Write("Initial generation of latency gains... ");

            long [,] latencyGains = new long[C, V];

            for (var v = 0; v < V; v++)
            {
                if (S[v] > X)
                {
                    continue;
                }
                for (var c = 0; c < C; c++)
                {
                    var totalLatencyGain = 0L;
                    for (var e = 0; e < E; e++)
                    {
                        if (LAT_E_TO_C[e, c] == int.MaxValue)
                        {
                            continue;
                        }
                        var numRequests = ENDPOINT_TO_VIDEO_REQUESTS[e, v];
                        if (numRequests == 0)
                        {
                            continue;
                        }
                        var latencyGain = (long)(LAT_E_TO_D[e] - LAT_E_TO_C[e, c]) * numRequests;
                        totalLatencyGain += latencyGain;
                    }
                    C5.IPriorityQueueHandle <Tuple <double, int, int> > handle = null;
                    sortedLatencyGains.Add(ref handle, new Tuple <double, int, int>(1.0 * totalLatencyGain / S[v], c, v));
                    latencyGainHandles[c, v] = handle;
                    latencyGains[c, v]       = totalLatencyGain;
                }
            }
            Console.Out.WriteLine("Done!");

            while (!sortedLatencyGains.IsEmpty)
            {
                var best = sortedLatencyGains.FindMax();
                sortedLatencyGains.DeleteMax();
                //Console.Out.WriteLine("--> gain: "+best.Item1+" c: "+best.Item2+" v: "+best.Item3);
                var gain = best.Item1;
                var c    = best.Item2;
                var v    = best.Item3;

                if (S[v] + SpaceUsed[c] > X)
                {
                    continue;
                }
                Assignment[c].Add(v);
                SpaceUsed[c] += S[v];

                for (var cx = 0; cx < C; cx++)
                {
                    latencyGains[cx, v] = 0L;
                }

                for (var ex = 0; ex < E; ex++)
                {
                    var bestCache   = -1;
                    var bestLatency = LAT_E_TO_D[ex];

                    foreach (var cx in EDGES_E_TO_C[ex])
                    {
                        if (!Assignment[cx].Contains(v))
                        {
                            continue;
                        }
                        if (LAT_E_TO_C[ex, cx] < bestLatency)
                        {
                            bestCache   = cx;
                            bestLatency = LAT_E_TO_C[ex, cx];
                        }
                    }

                    foreach (var cx in EDGES_E_TO_C[ex])
                    {
                        if (Assignment[cx].Contains(v))
                        {
                            continue;
                        }
                        if (LAT_E_TO_C[ex, cx] < bestLatency)
                        {
                            latencyGains[cx, v] += (bestLatency - LAT_E_TO_C[ex, cx]) * ENDPOINT_TO_VIDEO_REQUESTS[ex, v];
                        }
                    }
                }

                for (var cx = 0; cx < C; cx++)
                {
                    var handle = latencyGainHandles[cx, v];
                    Tuple <double, int, int> item;
                    if (sortedLatencyGains.Find(handle, out item))
                    {
                        sortedLatencyGains.Delete(handle);
                        sortedLatencyGains.Add(ref handle, new Tuple <double, int, int>(1.0 * latencyGains[cx, v] / S[v], cx, v));
                    }
                }
            }

            VerifySolution();
            return(CalculateScore());
        }