Example #1
0
        private static ISignature FromOverloads(Parameter[] allParameters, Overload[] overloads)
        {
            // Make sure all overloads are distinct
            overloads = overloads.Distinct().ToArray();

            // If there is only one overload: use a Sequence
            if (overloads.Length == 1)
            {
                return(new Sequence(overloads.Single().ToParameterList(allParameters)));
            }

            // If there is an empty overload: use an Option and recurse
            Overload emptyOverload = overloads.FirstOrDefault(overload => overload.All(flag => !flag));

            if (emptyOverload != null)
            {
                return(new Option {
                    Value = FromOverloads(allParameters, overloads.Except(new[] { emptyOverload }).ToArray())
                });
            }

            // Find contiguous areas of parameters that are independent from the rest.
            // Use a Sequence containing the constant parts and recurse for the independent areas.
            Interval[] independentAreas = GetIndependentAreas(new HashSet <Overload>(overloads));
            if (independentAreas.Any())
            {
                var sequence = new Sequence();
                // Pad independent areas with zero-length dummy intervals
                var pairs = independentAreas.Prepend(new Interval()).Concat(new Interval()).Pairwise();
                foreach (Tuple <Interval, Interval> pair in pairs)
                {
                    if (pair.Item1.Length > 0)
                    {
                        sequence.Add(FromArea(allParameters, overloads, pair.Item1));
                    }
                    for (int i = pair.Item1.End; i < pair.Item2.Start; i++)
                    {
                        sequence.Add(allParameters[i]);
                    }
                }
                return(sequence);
            }

            // If there are no independent areas:
            // Find all partitions of the overloads and use the one that yields the shortest representation.
            // Ignore the trivial one-group partition as this will result in an endless recursion.
            IEnumerable <Overload[][]> overloadPartitions = Partitioning.GetAllPartitions(overloads)
                                                            .Where(partition => partition.Length > 1);
            IEnumerable <Choice> possibleRepresentations = overloadPartitions
                                                           .Select(overloadGroups => new Choice(overloadGroups.Select(overloadGroup => FromOverloads(allParameters, overloadGroup.ToArray()))));

            return(possibleRepresentations.MinBy(representation => representation.ParameterCount));
        }
Example #2
0
        private void MergeSolution(List <DvrpSolutionData> datas)
        {
            //DVRP
            partialSolutions = datas.ToArray();
            List <Route> currentSolution = new List <Route>();
            var          set             = new int[problem.Clients.Length];

            for (int i = 0; i < set.Length; ++i)
            {
                set[i] = i;
            }
            var partitions = Partitioning.GetAllPartitions(set, problem.Fleet.count);

            foreach (var partition in partitions)
            {
                double distance = 0;
                currentSolution = new List <Route>();
                foreach (var p in partition)
                {
                    var key = p.ToList();
                    key.Sort();
                    int tag = partialDictionary[key];
                    //tag = datas.Count - tag -1;
                    Route r = datas.First(d => d.Routes[0].Tag == tag).Routes[0];//datas[tag].Routes[0];//
                    distance += r.Distance;
                    if (distance > solution.Distance)
                    {
                        distance = double.MaxValue;
                        break;
                    }
                    currentSolution.Add(r);
                }
                if (distance < solution.Distance)
                {
                    solution.Routes   = currentSolution.ToList();
                    solution.Distance = distance;
                }
            }
            Solution = solution.Serialize();
        }