예제 #1
0
 protected void Append_Tail(DistanzaStimata tratta)
 {
     Tail = tratta;
     if (Head == null)
         Head = Tail;
     Routes.Add(tratta);
 }
예제 #2
0
 public void Append_Head(DistanzaStimata tratta)
 {
     Head = tratta;
     if (Tail == null)
         Tail = Head;
     Routes.Insert(0, tratta);
 }
예제 #3
0
 public virtual void Append(DistanzaStimata tratta)
 {
     if (Can_Append_Head(tratta))
         Append_Head(tratta);
     else
         if (Can_Append_Tail(tratta))
             Append_Tail(tratta);
 }
예제 #4
0
 protected bool Can_Append_Tail(DistanzaStimata tratta)
 {
     return Routes.Count <= Max && (Tail == null || Tail.To == tratta.From);
     //return (Tail == null || Tail.To == tratta.From);
 }
예제 #5
0
 protected bool Can_Append_Head(DistanzaStimata tratta)
 {
     return Routes.Count <= Max && (Head == null || Head.From == tratta.To);
     //return (Head == null || Head.From == tratta.To);
 }
예제 #6
0
 public virtual bool Can_Append(DistanzaStimata tratta)
 {
     return Can_Append_Head(tratta) || Can_Append_Tail(tratta);
 }
예제 #7
0
 protected Request Build_Request(DistanzaStimata trattaIniziale)
 {
     var request = new RequestDuplicated(Max);
     return request;
 }
예제 #8
0
 public override bool Can_Append(DistanzaStimata tratta)
 {
     var points = Routes.Select(r => r.From).Concat(Routes.Select(r => r.To));
     return Max >= Routes.Count && (Can_Append_Head(tratta) | Can_Append_Tail(tratta) |
         !(points.Contains(tratta.From) || points.Contains(tratta.To)));
 }
예제 #9
0
 public override void Append(DistanzaStimata tratta)
 {
     if (Can_Append_Head(tratta))
         Append_Head(tratta);
     else
     {
         if (Can_Append_Tail(tratta))
             Append_Tail(tratta);
         else
         {
             var fake = new DistanzaStimata { From = tratta.To, To = Head.From };
             Append_Head(fake);
             Append_Head(tratta);
         }
     }
 }
예제 #10
0
        public void Test_Calcolo_Distanze()
        {
            var punti = Points.Select(p => p.Destination.Location).Distinct(new Location_Comparer()).ToList();

            punti.Clear();
            for (int i = 0; i < 100; i++)
                punti.Add(new Location { Latitude = 44 + 0.0001 * i, Longitude = 12 + 0.0001 * i });

            DateTime? start = null;
            var stop = start;
            var distanze = new List<DistanzaStimata>();
            var mre = new ManualResetEvent(false);
            var jobs = new Queue<DistanzaStimata>();

            var route = maps.ServiceHelper.GetRouteService();
            //route.CalculateRouteCompleted += (sender, e) =>
            //{
            //    var d = e.UserState as DistanzaStimata;
            //    if (start == null)
            //        start = DateTime.Now;

            //    if (e.Error == null && d != null)
            //    {
            //        d.Km = e.Result.Result.Summary.Distance;
            //        d.TimeInSeconds = e.Result.Result.Summary.TimeInSeconds;
            //    }
            //    lock (d)
            //        Monitor.Pulse(d);

            //    d.Done();
            //    if (distanze.All(a => a.Completed))
            //    {
            //        stop = DateTime.Now;
            //        mre.Set();
            //    }
            //};

            var combos = new Utilities.Combinatorics.Combinations<Location>(punti, 2);
            foreach (var combinazione in combos)
            {
                var da = combinazione[0];
                var a = combinazione[1];
                var distanza = new DistanzaStimata { From = da, To = a };
                distanze.Add(distanza);
                jobs.Enqueue(distanza);
            }

            //for (int i = 0; i < 10; i++)
            //{
            //    Task.Factory.StartNew(() =>
            //    {
            //        while (jobs.Count > 0)
            //        {
            //            var distanza = jobs.Dequeue();

            //            var points = new[] { distanza.From, distanza.To }.Select(l => new maps.RouteService.Waypoint
            //            {
            //                Location = new maps.RouteService.Location { Latitude = l.Latitude, Longitude = l.Longitude }
            //            }).To_ObservableCollection();

            //            var request = new maps.RouteService.RouteRequest
            //            {
            //                Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
            //                Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
            //                Options = new maps.RouteService.RouteOptions { Optimization = maps.RouteService.RouteOptimization.MinimizeDistance },
            //                Waypoints = points
            //            };

            //            route.CalculateRouteAsync(request, distanza);

            //            lock (distanza)
            //                Monitor.Wait(distanza);

            //        }
            //    });
            //}

            var builder = new Route_Request_Builder(distanze);
            var requests = builder.Get_Requests().To_Queue();
            var test = builder.Get_Requests();
            var q1 = test.SelectMany(r => r.Get_Routes()).Where(r => !r.Completed);

            route.CalculateRouteCompleted += (sender, e) =>
            {
                var d = e.UserState as Request;
                if (start == null)
                    start = DateTime.Now;

                if (e.Error == null && d != null)
                {
                    for (int i = 0; i < e.Result.Result.Legs.Count; i++)
                    {
                        var leg = e.Result.Result.Legs[i];
                        var r = d.Get_Routes().ElementAt(i);
                        r.Km = leg.Summary.Distance;
                        r.TimeInSeconds = leg.Summary.TimeInSeconds;
                        r.Done();
                    }
                    d.Done();
                }
                lock (d)
                    Monitor.Pulse(d);

                if (test.All(a => a.Completed))
                {
                    stop = DateTime.Now;
                    mre.Set();
                }
            };

            for (int i = 0; i < 10; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    while (requests.Count > 0)
                    {
                        var request = requests.Dequeue();

                        var points = request.Get_Locations().Select(l => new maps.RouteService.Waypoint
                        {
                            Location = new maps.RouteService.Location { Latitude = l.Latitude, Longitude = l.Longitude }
                        }).To_ObservableCollection();

                        var routeRequest = new maps.RouteService.RouteRequest
                        {
                            Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
                            Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
                            Options = new maps.RouteService.RouteOptions { Optimization = maps.RouteService.RouteOptimization.MinimizeDistance },
                            Waypoints = points
                        };

                        route.CalculateRouteAsync(routeRequest, request);

                        lock (request)
                            Monitor.Wait(request);

                    }
                });
            }
        }