Exemple #1
0
 public Box(double vol, double mas, string num, LandPoint adr, double val, int count1)
 {
     volume = vol;
     number = num;
     mass   = mas;
     adress = adr;
     value  = val;
     count  = count1;
 }
Exemple #2
0
 public Box(double vol, double mas, string num, LandPoint adr)
 {
     volume = vol;
     number = num;
     mass   = mas;
     adress = adr;
     value  = CalcValue() * -1;
     count  = 1;
 }
Exemple #3
0
 public void RandomFill()
 {
     if (Adresses.Count == 0)
     {
         throw new Exception("No source adresses");
     }
     for (int i = 0; i < 50; i++)
     {
         LandPoint adress = Adresses[Program.random.Next(Adresses.Count)];
         double    volume = Math.Round(Program.random.NextDouble() * 50);
         double    mass   = Math.Round(Program.random.NextDouble() * 50);
         Box       newBox = new Box(volume, mass, i.ToString(), adress);
         AddBox(newBox);
     }
 }
Exemple #4
0
        public void FileFill(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            var          args1  = reader.ReadLine().Split(';');

            if ((args1[0] != "Volume") || (args1[1] != "Weight") || (args1[2] != "Adress"))
            {
                reader.Close();
                throw new Exception("Invalid source file");
            }
            int counter = 0;

            while (true)
            {
                string Line = reader.ReadLine();
                if (Line == null)
                {
                    break;
                }
                string[] args = Line.Split(';');

                double    volume   = System.Convert.ToDouble(args[0]);
                double    weight   = System.Convert.ToDouble(args[1]);
                LandPoint location = new LandPoint();
                bool      flag     = false;
                foreach (LandPoint point in Adresses)
                {//trying to find adress in already loaded
                    if (point.adress == args[2])
                    {
                        flag     = true;
                        location = point;
                        break;
                    }
                }
                if (!flag)
                {
                    location = LandPoint.SingleGeocode(args[2]);
                }
                Box temp = new Box(volume, weight, counter.ToString(), location);
                AddBox(temp);
                counter++;
            }
            reader.Close();
        }
Exemple #5
0
        public static void ReadAndGeocode(string path)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            StreamReader reader = new StreamReader(path);

            while (true)
            {
                string adress;
                adress = reader.ReadLine();
                if (adress == null || adress == "")
                {
                    reader.Close();
                    break;
                }
                string         url        = @"https://maps.googleapis.com/maps/api/geocode/xml?address=" + adress + "&key=" + GoogleMapProvider.Instance.ApiKey;
                HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(url);
                WebResponse    response   = request.GetResponse();
                Stream         dataStream = response.GetResponseStream();
                XmlDocument    xmldoc     = new XmlDocument();
                xmldoc.Load(dataStream);
                xmldoc.Save("GeocodeingResponse");
                XmlElement xRoot = xmldoc.DocumentElement;

                if (xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lat") == null)
                {
                    continue;
                }
                string lat      = xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lat").InnerText;
                string lng      = xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lng").InnerText;
                string place_id = xmldoc.SelectSingleNode("GeocodeResponse/result/place_id").InnerText;
                adress = xmldoc.SelectSingleNode("GeocodeResponse/result/formatted_address").InnerText;
                double      latitude  = double.Parse(lat);
                double      longitude = double.Parse(lng);
                PointLatLng point     = new PointLatLng(latitude, longitude);
                LandPoint   temp      = new LandPoint(count, place_id, adress, point);
                Deliverer.Adresses.Add(temp);
                count++;
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
        }
Exemple #6
0
        public static LandPoint SingleGeocode(string adress)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");



            if (adress == null || adress == "")
            {
                throw new Exception("Invalid geocode request");
            }
            string         url        = @"https://maps.googleapis.com/maps/api/geocode/xml?address=" + adress + "&key=" + GoogleMapProvider.Instance.ApiKey;
            HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(url);
            WebResponse    response   = request.GetResponse();
            Stream         dataStream = response.GetResponseStream();
            XmlDocument    xmldoc     = new XmlDocument();

            xmldoc.Load(dataStream);
            xmldoc.Save("GeocodeingResponse");
            XmlElement xRoot = xmldoc.DocumentElement;

            if (xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lat") == null)
            {
                throw new Exception("No geocoding responce");
            }
            string lat      = xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lat").InnerText;
            string lng      = xmldoc.SelectSingleNode("GeocodeResponse/result/geometry/location/lng").InnerText;
            string place_id = xmldoc.SelectSingleNode("GeocodeResponse/result/place_id").InnerText;

            adress = xmldoc.SelectSingleNode("GeocodeResponse/result/formatted_address").InnerText;
            double      latitude  = double.Parse(lat);
            double      longitude = double.Parse(lng);
            PointLatLng point     = new PointLatLng(latitude, longitude);
            LandPoint   temp      = new LandPoint(0, place_id, adress, point);

            Deliverer.Adresses.Add(temp);


            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
            return(temp);
        }
Exemple #7
0
        public static void ReadLandPointFromFile(string path)
        {
            List <LandPoint> landPoints = new List <LandPoint>();
            StreamReader     reader     = new StreamReader(path);

            while (true)
            {
                string adress, coordinates;
                //id = reader.ReadLine();
                adress = reader.ReadLine();
                if (adress == null)
                {
                    reader.Close();
                    return;
                }

                coordinates = reader.ReadLine();
                string[]  args = coordinates.Split(' ');
                LandPoint temp = new LandPoint(count, count.ToString(), adress, Convert.ToDouble(args[0]), Convert.ToDouble(args[1]));
                Deliverer.Adresses.Add(temp);
                count++;
            }
        }
Exemple #8
0
        }//my attemt to create hungarian algorythm; currently works bad

        public static DelivererRoute GetRoute(int [,] Matrix, List <LandPoint> original, LandPoint start, LandPoint last)
        {
            List <LandPoint> points       = new List <LandPoint>(original);
            List <int>       destinations = new List <int>();

            if (start != last)
            {
                throw new Exception("This is not a loop route");
            }
            if (Matrix.GetLength(0) - 1 != points.Count)
            {
                throw new Exception("Unpropriate matrix for a loop route");
            }
            if (points.Count == 1)// special case for an obvious path
            {
                points.Insert(0, start);
                points.Add(last);
                int        length = Matrix[0, 1] * 2;
                List <int> temp   = new List <int>();

                temp.Add(Matrix[0, 1]);
                temp.Add(Matrix[0, 1]);
                return(new DelivererRoute(points, temp));
            }
            int deadline    = 0;
            int cases       = 50;
            int generations = 50;

            int[,] RandVays   = new int[cases, points.Count + 3];
            int[,] Generation = new int[cases, points.Count + 3];
            Random     random     = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            List <int> conformity = new List <int>();

            for (int i = 1; i <= points.Count; i++)
            {
                conformity.Add(i);
            }
            points.Insert(0, start);
            points.Add(last);
            for (int i = 0; i < cases; i++)
            {
                for (int j = 0; j < conformity.Count; j++)// random shuffle
                {
                    int temp = conformity[0];
                    conformity.RemoveAt(0);
                    conformity.Insert(random.Next(conformity.Count), temp);
                }
                RandVays[i, 0] = 0;
                for (int j = 1; j <= conformity.Count; j++)
                {
                    RandVays[i, j] = conformity[j - 1];
                }
                RandVays[i, conformity.Count + 1] = 0;
                int sum = 0;
                for (int j = 0; j <= conformity.Count; j++)
                {
                    sum += Matrix[RandVays[i, j], RandVays[i, j + 1]];
                }
                RandVays[i, conformity.Count + 2] = sum;
            }
            int        end     = points.Count;
            List <int> BestWay = new List <int>();

            for (int i = 0; i <= end; i++)
            {
                BestWay.Add(RandVays[0, i]);
            }
            int bestgeneration = 0;

            for (int generation = 0; generation < generations; generation++)
            {
                for (int i = 0; i < cases; i++) // tournament selection
                {
                    int first  = random.Next(cases);
                    int second = random.Next(cases);
                    if (RandVays[first, end] < RandVays[second, end])
                    {
                        for (int k = 0; k <= end; k++)
                        {
                            Generation[i, k] = RandVays[first, k];
                        }
                    }
                    else
                    {
                        for (int k = 0; k <= end; k++)
                        {
                            Generation[i, k] = RandVays[second, k];
                        }
                    }
                }

                for (int counter = 0; counter < cases; counter += 2) //crossover
                {
                    int   first       = random.Next(cases);          // first parent
                    int   second      = random.Next(cases);          // second parent
                    int   devider     = random.Next(end - 1) + 1;    // random crossover coefficient
                    int[] firstchild  = new int[end + 1];
                    int[] secondchild = new int[end + 1];
                    //List<int> secondchild = new List<int>(end + 1);
                    int sum1 = 0;
                    int sum2 = 0;
                    for (int k = 0; k < devider; k++)
                    {
                        firstchild[k]  = Generation[second, k];
                        secondchild[k] = Generation[first, k];
                    }
                    for (int k = devider; k < end; k++)
                    {
                        firstchild[k]  = Generation[first, k];
                        secondchild[k] = Generation[second, k];
                    }
                    for (int i = devider; i < end; i++)
                    {
                        for (int j = 0; j < devider; j++)
                        {
                            if (firstchild[i] == firstchild[j])// every number has to be unique to correct work of the algorythm
                            {
                                for (int k = devider; k < end; k++)
                                {
                                    for (int m = 0; m < devider; m++)
                                    {
                                        if (secondchild[k] == secondchild[m])
                                        {
                                            int temp = secondchild[m];
                                            secondchild[m] = firstchild[j];
                                            firstchild[j]  = temp;
                                            k = end;
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < end - 1; i++)
                    {
                        sum1 += Matrix[firstchild[i], firstchild[i + 1]];// calculating length of the path
                        sum2 += Matrix[secondchild[i], secondchild[i + 1]];
                    }
                    firstchild[end]  = sum1;
                    secondchild[end] = sum2;
                    for (int k = 0; k <= end; k++)
                    {
                        RandVays[counter, k]     = firstchild[k];
                        RandVays[counter + 1, k] = secondchild[k];
                    }
                }

                for (int i = 0; i < cases / 10; i++)    //increased number of mutations
                {
                    int randnum1 = random.Next(cases);  // mutation
                    int randnum2 = random.Next(end - 2) + 1;
                    int randnum3 = random.Next(end - 2) + 1;
                    int bubble   = RandVays[randnum1, randnum2];
                    RandVays[randnum1, randnum2] = RandVays[randnum1, randnum3];
                    RandVays[randnum1, randnum3] = bubble;
                    RandVays[randnum1, end]      = 0;
                    for (int k = 0; k < end - 1; k++)
                    {
                        RandVays[randnum1, end] += Matrix[RandVays[randnum1, k], RandVays[randnum1, k + 1]];
                    }
                }
                int index  = -1;   // the next process records the best way and kill algoryth if it is needed
                int length = BestWay[end];
                for (int i = 0; i < cases; i++)
                {
                    if (RandVays[i, end] < length)
                    {
                        length = RandVays[i, end];
                        index  = i;
                    }
                }
                if (index != -1)
                {
                    for (int i = 0; i <= end; i++)
                    {
                        BestWay[i]     = RandVays[index, i];
                        bestgeneration = generation;
                    }
                    deadline = 0;
                }
                else
                {
                    deadline++;
                }
                if (deadline == 5)
                {
                    generation = generations;
                    break;
                }
            }
            List <LandPoint> result = new List <LandPoint>();

            for (int i = 0; i < points.Count; i++)
            {
                result.Add(points[BestWay[i]]);
            }
            for (int i = 1; i <= original.Count; i++)
            {
                destinations.Add(Matrix[BestWay[i - 1], i]);
            }
            return(new DelivererRoute(result, destinations));
        }//genetic search of the path