Esempio n. 1
0
        public void grouping()
        {
            int c = 0;

            groups = new List <cluster>();
            foreach (location point in points)
            {
                if (point.get_cluster() == -1 && point.closest_place().get_cluster() == -1)
                {
                    if (point.closest_place().closest_place().Equals(point))
                    {
                        groups.Add(new cluster(new location[] { point, point.closest_place() }, c));
                        c++;
                    }
                }
            }

            if (c < 2)
            {
                location new_group = null;
                double   furthest  = 0;
                foreach (location point in points)
                {
                    if (point.get_cluster() == -1)
                    {
                        cluster p = new cluster(point);
                        double  d = 0;
                        foreach (cluster A in groups)
                        {
                            d = p.distance_to(A);
                            if (d > furthest)
                            {
                                furthest  = d;
                                new_group = point;
                            }
                        }
                    }
                }
                if (new_group != null)
                {
                    groups.Add(new cluster(new location[] { new_group }, c));
                }
                else
                {
                    groups.Clear();
                    c = 0;
                    foreach (location point in points)
                    {
                        groups.Add(new cluster(new location[] { point }, c));
                        c++;
                    }
                }
            }

            clustering();
        }
Esempio n. 2
0
 private vehicle find_appropriate_vehicle_to(cluster most_distant_cluster, List <vehicle> vehicles)
 {
     foreach (vehicle v in vehicles)
     {
         if (!v.has_filled_kids() && v.getCapacity() >= most_distant_cluster.get_size())
         {
             return(v);
         }
     }
     return(find_largest_unfilled_car(vehicles));
 }
Esempio n. 3
0
 private void clustering()
 {
     foreach (location point in points)
     {
         if (point.get_cluster() == -1)
         {
             cluster p            = new cluster(point);
             cluster target_group = closest_group_to(p);
             target_group.add(point);
         }
     }
 }
Esempio n. 4
0
        private void pull_appropriate_kids(vehicle proper_car, cluster most_distant_cluster, cluster closest_cluster, int extra_seats)
        {
            List <location> selected_kids = new List <location>();

            //List<location> selected_kids = new List<location>();
            while (extra_seats > 0)
            {
                location closest = find_closest_location(closest_cluster, most_distant_cluster);
                selected_kids.Add(closest);
                closest_cluster.remove(closest);
                extra_seats--;
            }
            proper_car.fill_kids(selected_kids);
        }
Esempio n. 5
0
        private cluster find_most_distant_cluster()
        {
            double  most_distant = 0;
            cluster target       = null;

            foreach (cluster each in groups)
            {
                if (each.get_ave_distance_from_dream_center() > most_distant)
                {
                    most_distant = each.get_ave_distance_from_dream_center();
                    target       = each;
                }
            }
            return(target);
        }
Esempio n. 6
0
 private void merge_groups(cluster A, cluster B)
 {
     groups.Remove(B);
     foreach (cluster each in groups)
     {
         if (each.Equals(A))
         {
             //foreach(location a in A)
             //{
             //a.hebing = true;
             //}
             each.add_range(B.get_cluster());
         }
     }
 }
Esempio n. 7
0
        private location find_closest_location(cluster target_cluster, cluster adjacent_cluster)
        {
            double   d      = 9999;
            location target = null;

            foreach (location each in target_cluster.get_cluster())
            {
                cluster c = new cluster(each);
                if (c.distance_to(adjacent_cluster) < d)
                {
                    d      = c.distance_to(adjacent_cluster);
                    target = each;
                }
            }
            return(target);
        }
Esempio n. 8
0
        public double distance_to(cluster B)
        {
            double total = 0;

            foreach (location a in group)
            {
                double ave_d = 0;
                foreach (location b in B.get_cluster())
                {
                    ave_d += a.distance_to(b);
                }
                ave_d /= B.get_size();
                total += ave_d;
            }
            total /= size;
            return(total);
        }
Esempio n. 9
0
        private void fill_vehicle_with_appropriate_kids(vehicle proper_car, cluster most_distant_cluster, cluster closest_cluster)
        {
            List <location> left_kids = new List <location>();
            //List<location> selected_kids = new List<location>();
            int extra_kids = most_distant_cluster.get_size() - proper_car.getCapacity();

            while (extra_kids > 0)
            {
                location closest = find_closest_location(most_distant_cluster, closest_cluster);
                left_kids.Add(closest);
                most_distant_cluster.remove(closest);
                extra_kids--;
            }
            proper_car.fill_kids(most_distant_cluster.get_cluster());

            most_distant_cluster.remove_all();
            most_distant_cluster.add_range(left_kids);
        }
Esempio n. 10
0
        private cluster closest_group_to(cluster o)
        {
            double  closest       = 9999;
            cluster closest_group = null;

            foreach (cluster group in groups)
            {
                if (!group.Equals(o))
                {
                    double d = o.distance_to(group); //distance_between_groups(o, group);
                    if (d < closest)
                    {
                        closest       = d;
                        closest_group = group;
                    }
                }
            }
            return(closest_group);
        }
Esempio n. 11
0
        public List <kid> suit_to_vehicles(List <vehicle> vehicles)
        {
            vehicles = vehicles.OrderBy(x => x.getCapacity()).ToList();
            compute_ave_distance_to_center_for_clusters();
            compute_ave_distance_between_clusters();

            cluster most_distant_cluster = find_most_distant_cluster();

            while (!has_filled_all_vehicles(vehicles) && most_distant_cluster != null)
            {
                vehicle proper_car = find_appropriate_vehicle_to(most_distant_cluster, vehicles);
                fill_vehicle_and_reset_clusters(proper_car, vehicles, most_distant_cluster);
                most_distant_cluster = find_most_distant_cluster();
            }

            List <kid> unassigned_kids = collect_kids_left();

            return(unassigned_kids);
        }
Esempio n. 12
0
        private void fill_vehicle_and_pick_extra_kids(vehicle proper_car, cluster most_distant_cluster, int total_extra_seats)
        {
            int     extra_seats     = proper_car.getCapacity() - most_distant_cluster.get_size();
            cluster closest_cluster = closest_group_to(most_distant_cluster);

            while (closest_cluster != null && extra_seats >= closest_cluster.get_size())
            {
                extra_seats -= closest_cluster.get_size();
                merge_groups(most_distant_cluster, closest_cluster);
                closest_cluster = closest_group_to(most_distant_cluster);
            }

            fill_vehicle_with_all_kids_in_cluster(proper_car, most_distant_cluster);
            if (extra_seats > 0 && closest_cluster != null && !(extra_seats < total_extra_seats &&
                                                                extra_seats < 0.2 * proper_car.getCapacity() &&
                                                                most_distant_cluster.distance_to(closest_cluster) > ave_distance_in_groups))
            {
                pull_appropriate_kids(proper_car, most_distant_cluster, closest_cluster, extra_seats);
            }
        }
Esempio n. 13
0
        private void fill_vehicle_with_max_capacity(vehicle proper_car, List <vehicle> vehicles, cluster most_distant_cluster)
        {
            //int extra_kids = most_distant_cluster.get_size() - proper_car.getCapacity();
            cluster closest_cluster = closest_group_to(most_distant_cluster);

            if (closest_cluster != null)
            {
                fill_vehicle_with_appropriate_kids(proper_car, most_distant_cluster, closest_cluster);
                //proper_car.fill_kids(most_distant_cluster.get_cluster());
                //most_distant_cluster = find_most_distant_cluster();
            }
            else
            {
                split();
                most_distant_cluster = find_most_distant_cluster();
                //closest_cluster = closest_group_to(most_distant_cluster);
                proper_car = find_appropriate_vehicle_to(most_distant_cluster, vehicles);
                fill_vehicle_and_reset_clusters(proper_car, vehicles, most_distant_cluster);
            }
        }
Esempio n. 14
0
        private void fill_vehicle_and_may_pull_extra_kids(vehicle proper_car, List <vehicle> vehicles, cluster most_distant_cluster)
        {
            int     total_extra_seats = current_total_capacity_of(vehicles) - current_size_of_clusters();
            cluster closest_cluster   = closest_group_to(most_distant_cluster);
            int     extra_seats       = proper_car.getCapacity() - most_distant_cluster.get_size();

            if (closest_cluster == null || extra_seats == 0)
            {
                fill_vehicle_with_all_kids_in_cluster(proper_car, most_distant_cluster);
            }
            else
            {
                if (extra_seats < total_extra_seats && extra_seats < 0.2 * proper_car.getCapacity() &&
                    most_distant_cluster.distance_to(closest_cluster) > ave_distance_in_groups)
                {
                    fill_vehicle_with_all_kids_in_cluster(proper_car, most_distant_cluster);
                }
                else
                {
                    fill_vehicle_and_pick_extra_kids(proper_car, most_distant_cluster, total_extra_seats);
                }
            }
        }
Esempio n. 15
0
 private void fill_vehicle_with_all_kids_in_cluster(vehicle proper_car, cluster most_distant_cluster)
 {
     proper_car.fill_kids(most_distant_cluster.get_cluster());
     groups.Remove(most_distant_cluster);
 }
Esempio n. 16
0
 private void fill_vehicle_and_reset_clusters(vehicle proper_car, List <vehicle> vehicles, cluster most_distant_cluster)
 {
     if (proper_car.getCapacity() < most_distant_cluster.get_size())
     {
         fill_vehicle_with_max_capacity(proper_car, vehicles, most_distant_cluster);
     }
     else
     {
         fill_vehicle_and_may_pull_extra_kids(proper_car, vehicles, most_distant_cluster);
     }
 }