public void Kmeans_Test1()
        {
            List<Point> data = new List<Point>();

            for (int i = 0; i < 10; i++) {
                List<double> coords = new List<double>();
                coords.Add(70);
                coords.Add(i);
                data.Add(new Point(coords));
            }
            for (int i = 0; i < 10; i++)
            {
                List<double> coords = new List<double>();
                coords.Add(-30);
                coords.Add(i);
                data.Add(new Point(coords));
            }

            KMeans clustering = new KMeans(data, 2);
            clustering.Compute();
            Console.Write("stoper");
        }
        public static void FullSolveTest(VRPParser benchmark) 
        {
            /******************* DIVIDE *************************/


            // Combine coords (x, y) and time_avail (z)
            List<Point> data = new List<Point>();
            for (int i = 0; i < benchmark.Num_Visits; i++)
            {
                List<double> point_coords = new List<double>();

                // does not include depots - which is what we want
                int loc_index = benchmark.Visit_Location[i];

                point_coords.Add(benchmark.Location_Coord[loc_index][0]);
                point_coords.Add(benchmark.Location_Coord[loc_index][1]);
                point_coords.Add(benchmark.Time_Avail[loc_index - 1] + benchmark.Duration[loc_index - 1]);

                data.Add(new Point(point_coords));
            }

            // get optimal number of clusters
            PredictionStrength ps = new PredictionStrength(data);
            ps.Compute(true);
            int k = ps.BestK;

            // compute clusters
            KMeans clusters = new KMeans(data, k);
            clusters.Compute();

            // create k benchmarks for k solvers
            VRPParser[] partial_benchmarks = new VRPParser[k];
            for (int i = 0; i < k; i++) 
            {
                VRPParser partial_benchmark = new VRPParser();
                List<int> cluster_indecies = clusters.GetCluterIndecies(i);

                /************ COMMON ****************/
                int num_depots = benchmark.Num_Depots;
                int num_visits = cluster_indecies.Count;
                int num_locations = cluster_indecies.Count + num_depots;

                partial_benchmark.Num_Visits = num_visits;
                partial_benchmark.Num_Depots = num_depots;
                partial_benchmark.Name = benchmark.Name;
                partial_benchmark.Num_Capacities = benchmark.Num_Capacities;
                partial_benchmark.Num_Vehicles = 1;
                partial_benchmark.Capacites = benchmark.Capacites;

                partial_benchmark.Depots_IDs = new int[benchmark.Depots_IDs.Length];
                benchmark.Depots_IDs.CopyTo(partial_benchmark.Depots_IDs, 0);

                partial_benchmark.Depot_Location = new int[benchmark.Depot_Location.Length];
                benchmark.Depot_Location.CopyTo(partial_benchmark.Depot_Location, 0);

                partial_benchmark.Depot_Time_Window = new int[benchmark.Depot_Time_Window.Length][];
                for (int p = 0; p < partial_benchmark.Depot_Time_Window.Length; p++) 
                {
                    partial_benchmark.Depot_Time_Window[p] = new int[benchmark.Depot_Time_Window[p].Length];
                    benchmark.Depot_Time_Window[p].CopyTo(partial_benchmark.Depot_Time_Window[p], 0);
                }

                /************ LOCATION_COORD ****************/
                partial_benchmark.Num_Locations = num_locations;

                int[][] location_coord = new int[num_locations][];
                // get all depots locations
                for (int j = 0; j < num_depots; j++)
                {
                    location_coord[j] = new int[2];
                    location_coord[j][0] = benchmark.Location_Coord[j][0];
                    location_coord[j][1] = benchmark.Location_Coord[j][1];
                }

                // get all partial clients locations
                for (int j = num_depots; j < num_locations; j++) 
                {
                    location_coord[j] = new int[2];
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j - num_depots]];

                    location_coord[j][0] = benchmark.Location_Coord[clientNodeIndex][0];
                    location_coord[j][1] = benchmark.Location_Coord[clientNodeIndex][1];
                }
                partial_benchmark.Location_Coord = location_coord;

                /************ DEMAND ****************/
                int[] demands = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    demands[j] = benchmark.Demands[clientNodeIndex - num_depots];
                }
                partial_benchmark.Demands = demands;

                /************ VISIT_LOCATION ****************/
                int[] visit_location = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]] - num_depots;

                    visit_location[j] = clientNodeIndex;
                    //visit_location[j] = j + num_depots;
                }
                partial_benchmark.Visit_Location = visit_location;

                /************ DURATION ****************/
                int[] duration = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    duration[j] = benchmark.Duration[clientNodeIndex - num_depots];
                }
                partial_benchmark.Duration = duration;

                /************ TIME_AVAIL ****************/
                int[] time_avail = new int[num_visits];
                for (int j = 0; j < num_visits; j++)
                {
                    int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                    time_avail[j] = benchmark.Time_Avail[clientNodeIndex - num_depots];
                }
                partial_benchmark.Time_Avail = time_avail;

                partial_benchmarks[i] = partial_benchmark;
            }

            /******************* SOLVE *************************/
            float CUT_OFF_COEFFICIENT = 0.2f;
            List<Result> results = new List<Result>();
            List<int> nextDay = new List<int>();
            float cutOffTime = CUT_OFF_COEFFICIENT * benchmark.Depot_Time_Window[0][1];
            for (int i = 0; i < partial_benchmarks.Length; i++)
            {
                results.Add(TSPTrianIneq.calculate(partial_benchmarks[i], cutOffTime));
            }
            /******************* MERGE *************************/

            for(int j = 0; j < results.Count; j++) 
            {
                for (int i = partial_benchmarks[j].Num_Depots; i < results[j].route.Length - partial_benchmarks[j].Num_Depots; i++)
                    results[j].route[i] = partial_benchmarks[j].Visit_Location[results[j].route[i] - partial_benchmarks[j].Num_Depots] + partial_benchmarks[j].Num_Depots;
            }

            for (int j = 0; j < results.Count; j++) 
            for (int i = 0; i < results[j].nextDay.Count; i++)
            {
                results[j].nextDay[i] = partial_benchmarks[j].Visit_Location[results[j].nextDay[i] - partial_benchmarks[j].Num_Depots] + partial_benchmarks[j].Num_Depots;
            }


            Console.Write("asd");
        }
        public override byte[][] DivideProblem(int threadCount)
        {
            /****************** DESERIALIZE ************************/

            BinaryFormatter formatter = new BinaryFormatter();
            VRPParser benchmark = (VRPParser)formatter.Deserialize(new MemoryStream(_problemData));

            /******************* DIVIDE *************************/

            // Combine coords (x, y) and time_avail (z)
            List<Point> data = new List<Point>();
            for (int i = 0; i < benchmark.Num_Visits; i++)
            {
                List<double> point_coords = new List<double>();

                // does not include depots - which is what we want
                int loc_index = benchmark.Visit_Location[i];

                point_coords.Add(benchmark.Location_Coord[loc_index][0]);
                point_coords.Add(benchmark.Location_Coord[loc_index][1]);
                point_coords.Add(benchmark.Time_Avail[loc_index - 1] + benchmark.Duration[loc_index - 1]);

                data.Add(new Point(point_coords));
            }

            // get optimal number of clusters
            //PredictionStrength ps = new PredictionStrength(data);
            //ps.Compute(true);
            //int k = ps.BestK;
            
            int max_k = 5;
            int start_k = 1;

            // prepare byte array for all partial solutions
            int solutionsSize = 0;
            for (int i = start_k; i <= max_k; i++)
                solutionsSize += i;

            int temporarySolutionIndex = 0;
            byte[][] temporarySolution = new byte[solutionsSize][];

            for (int k = start_k; k <= max_k; k++)
            {
                // compute clusters
                KMeans clusters = new KMeans(data, k);
                clusters.Compute();

                // create k benchmarks for k solvers
                VRPParser[] partial_benchmarks = new VRPParser[k];
                for (int i = 0; i < k; i++)
                {
                    VRPParser partial_benchmark = new VRPParser();
                    List<int> cluster_indecies = clusters.GetCluterIndecies(i);

                    partial_benchmark.ID = k;

                    /************ COMMON ****************/
                    int num_depots = benchmark.Num_Depots;
                    int num_visits = cluster_indecies.Count;
                    int num_locations = cluster_indecies.Count + num_depots;

                    partial_benchmark.Num_Visits = num_visits;
                    partial_benchmark.Num_Depots = num_depots;
                    partial_benchmark.Name = benchmark.Name;
                    partial_benchmark.Num_Capacities = benchmark.Num_Capacities;
                    partial_benchmark.Num_Vehicles = 1;
                    partial_benchmark.Capacites = benchmark.Capacites;

                    partial_benchmark.Depots_IDs = new int[benchmark.Depots_IDs.Length];
                    benchmark.Depots_IDs.CopyTo(partial_benchmark.Depots_IDs, 0);

                    partial_benchmark.Depot_Location = new int[benchmark.Depot_Location.Length];
                    benchmark.Depot_Location.CopyTo(partial_benchmark.Depot_Location, 0);

                    partial_benchmark.Depot_Time_Window = new int[benchmark.Depot_Time_Window.Length][];
                    for (int p = 0; p < partial_benchmark.Depot_Time_Window.Length; p++)
                    {
                        partial_benchmark.Depot_Time_Window[p] = new int[benchmark.Depot_Time_Window[p].Length];
                        benchmark.Depot_Time_Window[p].CopyTo(partial_benchmark.Depot_Time_Window[p], 0);
                    }

                    /************ LOCATION_COORD ****************/
                    partial_benchmark.Num_Locations = num_locations;

                    int[][] location_coord = new int[num_locations][];
                    // get all depots locations
                    for (int j = 0; j < num_depots; j++)
                    {
                        location_coord[j] = new int[2];
                        location_coord[j][0] = benchmark.Location_Coord[j][0];
                        location_coord[j][1] = benchmark.Location_Coord[j][1];
                    }

                    // get all partial clients locations
                    for (int j = num_depots; j < num_locations; j++)
                    {
                        location_coord[j] = new int[2];
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j - num_depots]];

                        location_coord[j][0] = benchmark.Location_Coord[clientNodeIndex][0];
                        location_coord[j][1] = benchmark.Location_Coord[clientNodeIndex][1];
                    }
                    partial_benchmark.Location_Coord = location_coord;

                    /************ DEMAND ****************/
                    int[] demands = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        demands[j] = benchmark.Demands[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Demands = demands;

                    /************ VISIT_LOCATION ****************/
                    int[] visit_location = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]] - num_depots;

                        visit_location[j] = clientNodeIndex;
                        //visit_location[j] = j + num_depots;
                    }
                    partial_benchmark.Visit_Location = visit_location;

                    /************ DURATION ****************/
                    int[] duration = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        duration[j] = benchmark.Duration[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Duration = duration;

                    /************ TIME_AVAIL ****************/
                    int[] time_avail = new int[num_visits];
                    for (int j = 0; j < num_visits; j++)
                    {
                        int clientNodeIndex = benchmark.Visit_Location[cluster_indecies[j]];
                        time_avail[j] = benchmark.Time_Avail[clientNodeIndex - num_depots];
                    }
                    partial_benchmark.Time_Avail = time_avail;

                    partial_benchmarks[i] = partial_benchmark;
                }

                /************ SERIALIZATION ******************/
                for (int i = 0; i < partial_benchmarks.Count(); i++)
                {
                    temporarySolution[temporarySolutionIndex++] = DataSerialization.ObjectToByteArray(partial_benchmarks[i]);
                }
            }

            return temporarySolution;
        }
        public void Kmeans_Test2()
        {
            List<Point> data = new List<Point>();

            // 0-7
            data.Add(new Point(5, 8));  // 0
            data.Add(new Point(7, 6));  // 1
            data.Add(new Point(4, 6));  // 2
            data.Add(new Point(3, 5));  // 3
            data.Add(new Point(5, 7));  // 0
            data.Add(new Point(7, 5));  // 1
            data.Add(new Point(3, 7));  // 2
            data.Add(new Point(8, 5));  // 3

            // 8-17
            data.Add(new Point(-6, 4)); // 4
            data.Add(new Point(-4, 3)); // 5
            data.Add(new Point(-5, 4)); // 6
            data.Add(new Point(-4, 5)); // 7
            data.Add(new Point(-3, 7)); // 8
            data.Add(new Point(-6, 4)); // 4
            data.Add(new Point(-4, 7)); // 5
            data.Add(new Point(-2, 5)); // 6
            data.Add(new Point(-3, 5)); // 7
            data.Add(new Point(-4, 6)); // 8

            // 18 -
            data.Add(new Point(5, -8)); // 9
            data.Add(new Point(7, -6)); // 10
            data.Add(new Point(4, -6)); // 11
            data.Add(new Point(3, -5)); // 12
            data.Add(new Point(3, -8)); // 9
            data.Add(new Point(5, -4)); // 10
            data.Add(new Point(4, -5)); // 11
            data.Add(new Point(6, -5)); // 12

            KMeans clustering = new KMeans(data, 3);
            clustering.Compute();
            Console.Write("stoper");
        }
        /// <summary>
        ///     Let data be a set of n elements, then co-membership 
        ///     is an n by n matrix with (i,j)th element equal to 1
        ///     if i and j fall into the same cluster from the clusters set, 0 otherwise.
        ///     The clusters and data can come from different samples (of the same population) 
        /// </summary>
        /// <param name="learningCluster"></param>
        /// <param name="testingClusterPoints"></param>
        /// <returns></returns>
        private int[][] co_membership(KMeans learningCluster, List<Point> testingClusterPoints)
        {
            int len = testingClusterPoints.Count;
            int[][] m = new int[len][];
            for (int i = 0; i < len; i++)
            {
                m[i] = new int[len];
            }

            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < len; j++)
                {
                    if (i != j)
                    {
                        for (int k = 0; k < learningCluster.K; k++)
                        {
                            int i_centroid = learningCluster.GetClosestCentroid(testingClusterPoints[i]);

                            int j_centroid = learningCluster.GetClosestCentroid(testingClusterPoints[j]);

                            if (i_centroid == j_centroid){
                                m[i][j] = 1;
                                break;
                            }
                            else
                                m[i][j] = 0;
                        }
                    }
                }
            }

            return m;
        }
        /// <summary>
        ///     Take set of points of each Test cluster and 
        ///     compute its co-membership matrix with respect to
        ///     the set of Learning clusters.
        ///
        ///     Do it for each Test cluster (check of Test points)
        ///     while computing its strength.
        ///     The min of strengths of all clusters will be the
        ///     Predictions strength
        /// </summary>
        /// <param name="learning"></param>
        /// <param name="testing"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        private double psValue(List<Point> learning, List<Point> testing, int k)
        {
            double[] strenghs = new double[k];
            
            // get clusters
            KMeans learningCluster = new KMeans(learning, k);
            KMeans testingCluster = new KMeans(testing, k);

            if(!learningCluster.Compute())
                return 0.0;
            if (!testingCluster.Compute())
                return 0.0;

            // 3) Computes the co-membership of each Test cluster
            for(int i = 0;i<k;i++)
            {
                // Get cluster points from indecies
                List<int> cluster_indicies = testingCluster.GetCluterIndecies(i);
                List<Point> testingClusterPoints = new List<Point>();
                for(int j=0;j<cluster_indicies.Count;j++)
                {
                    testingClusterPoints.Add(testing[cluster_indicies[j]]);
                }

                // compute the co_membership
                int[][] M = co_membership(learningCluster, testingClusterPoints);
                int n = testingClusterPoints.Count;

                // 4.1) Compute strength of each Test cluster
                double sum = 0;
                for (int j = 0; j < M.Length; j++) 
                {
                    for (int l = 0; l < M[j].Length; l++) 
                    {
                        sum += M[j][l];
                    }
                }
                double divider = n * (n - 1);
                divider = divider == 0 ? 1 : divider;
                strenghs[i] = sum / divider;
            }

            // 4.2) Find the minimum strenght value.
            double min = strenghs[0];
            for (int i = 0; i < k; i++) 
            {
                if (min > strenghs[i])
                    min = strenghs[i];
            }

            return min;
        }