Exemple #1
0
        public override void MergeSolution(byte[][] solutions)
        {
            State = TaskSolverState.Merging;
            SolutionDescription bestSolution = SerializeToClass <SolutionDescription>(solutions[0]);

            for (int i = 1; i < solutions.Length; i++)
            {
                SolutionDescription nextSolution = SerializeToClass <SolutionDescription>(solutions[i]);
                if (bestSolution.m_result > nextSolution.m_result)
                {
                    bestSolution = nextSolution;
                }
            }
            Solution = SerializeFromClass <SolutionDescription>(bestSolution);
            State    = TaskSolverState.Idle;
            SolutionsMergingFinished.Invoke(EventArgs.Empty, this);
        }
Exemple #2
0
        public override byte[] Solve(byte[] partialData, TimeSpan timeout)
        {
            if (timeout != TimeSpan.Zero)
            {
                timer          = new Timer(timeout.TotalMilliseconds);
                timer.Elapsed += TimeoutOcurred;
                timer.Start();
            }
            solution = new SolutionDescription()
            {
                m_result = double.MaxValue, m_permutation = new int[0][]
            };
            State = TaskSolverState.Solving;
            var startingDivision = SerializeToClass <List <List <int> > >(partialData);

            PrepareData();
            N = description.clients.Count;
            actualDivision = new int[N][];
            for (int i = 0; i < actualDivision.Length; i++)
            {
                actualDivision[i] = new int[N];
            }
            actualCosts  = new int[N];
            subsetsCount = new int[N];
            int nextNr = 0;

            for (int i = 0; i < startingDivision.Count; i++)
            {
                subsetsCount[i] = startingDivision[i].Count;
                for (int j = 0; j < startingDivision[i].Count; j++)
                {
                    actualDivision[i][j] = startingDivision[i][j];
                    actualCosts[i]      += description.clients[startingDivision[i][j]].demand;
                    nextNr++;
                }
            }
            MakeAllDivisions(nextNr, startingDivision.Count);
            Solution = SerializeFromClass <SolutionDescription>(solution);
            //  ProblemSolvingFinished.Invoke(EventArgs.Empty, this);
            State = TaskSolverState.Idle;
            return(Solution);
        }
Exemple #3
0
        private void ResolveOneDivision(int[][] oneDivision, int setsNr)
        {
            if (State == TaskSolverState.Timeout)
            {
                return;
            }

            /*double downPredictionsSum = 0;
             * double[] downPredictions = new double[setsNr];
             * for (int i = 0; i < setsNr; i++)
             * {
             *  downPredictions[i] = KruskalAlgorithm(oneDivision[i], subsetsCount[i]);
             *  if (downPredictions[i] == double.MaxValue) return;
             *  downPredictionsSum += oneSubsetMinCost;
             *  if (downPredictionsSum > solution.m_result) return;
             * }*/
            double oneDivisionMinCost = 0;

            int[][] oneDivisionMinPermutations = new int[setsNr][];
            for (int i = 0; i < setsNr; i++)
            {
                //double oneSubsetMinCost = double.MaxValue;// 2 * downPredictions[i];
                double oneSubsetMinCost = /*subsetsCount[i] < 5 ?*/ AllPermuationsSimple(oneDivision[i], subsetsCount[i]);/* :*/
                //  AllPermutationsComplicated(oneDivision[i], subsetsCount[i], out oneSubsetMinPermutation);
                if (oneSubsetMinCost == double.MaxValue)
                {
                    return;
                }
                oneDivisionMinCost           += oneSubsetMinCost;
                oneDivisionMinPermutations[i] = oneSubsetMinPermutation;
                if (oneDivisionMinCost > solution.m_result)
                {
                    return;
                }
            }
            if (oneDivisionMinPermutations.Length > description.vehiclesCount)
            {
                List <List <int> > oneDivisionMinPermutationList;
                List <List <int> > oneDiv = new List <List <int> >();
                for (int i = 0; i < setsNr; i++)
                {
                    List <int> subset = new List <int>();
                    for (int j = 0; j < subsetsCount[i]; i++)
                    {
                        subset.Add(oneDivision[i][j]);
                    }
                    oneDiv.Add(subset);
                }
                double value = LittleCarProblem0(oneDiv, out oneDivisionMinPermutationList);
                if (value >= solution.m_result)
                {
                    return;
                }
                oneDivisionMinPermutations = new int[oneDivisionMinPermutationList.Count][];
                for (int i = 0; i < oneDivisionMinPermutations.Length; i++)
                {
                    oneDivisionMinPermutations[i] = new int[oneDivisionMinPermutationList[i].Count];
                    for (int j = 0; j < oneDivisionMinPermutations[i].Length; j++)
                    {
                        oneDivisionMinPermutations[i][j] = oneDivisionMinPermutationList[i][j];
                    }
                }
            }
            solution = new SolutionDescription()
            {
                m_permutation = oneDivisionMinPermutations, m_result = oneDivisionMinCost
            };
        }