예제 #1
0
        public ProblemData Parse(string fileContent)
        {
            int mNumDepots = 1;
            int mNumVisits = 1;
            int mNumVehicles = 1;
            int mMaxCapacity = 999;

            ProblemData nProblemData = new ProblemData();
            List<Depot> ndepot = new List<Depot>();
            List<Customer> nCustomer = new List<Customer>();
            Dictionary<int, Point> nLocation = new Dictionary<int, Point>();

            string line;
            try
            {
                using (StringReader sr = new StringReader(fileContent))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        checkLine(line);

                        if (line.Contains("COMMENT"))
                            continue;
                        switch (mCurrSect)
                        {
                            case DataSection.MAIN:
                                if (!mIsBeginState)
                                {
                                    string identifier;
                                    identifier = line.Split(':').First();

                                    if (identifier.CompareTo("NAME") == 0)
                                    {
                                        var name = line.Split(' ').Last();
                                    }
                                    else if (identifier.CompareTo("NUM_DEPOTS") == 0)
                                    {
                                        mNumDepots = Convert.ToInt16(line.Split(' ').Last());

                                    }
                                    else if (identifier.CompareTo("NUM_VISITS") == 0)
                                    {
                                        mNumVisits = Convert.ToInt16(line.Split(' ').Last());

                                    }
                                    else if (identifier.CompareTo("NUM_VEHICLES") == 0)
                                    {
                                        mNumVehicles = Convert.ToInt16(line.Split(' ').Last());

                                    }
                                    else if (identifier.CompareTo("CAPACITIES") == 0)
                                    {
                                        mMaxCapacity = Convert.ToInt16(line.Split(' ').Last());
                                        nProblemData.Capacity = mMaxCapacity;
                                    }
                                    else if (line.Split(' ').First().CompareTo("VRPTEST") == 0)
                                    {
                                        nProblemData.Name = line.Split(' ')[1];
                                    }
                                    else
                                    {
                                        // Niepotrzebne linie
                                    }
                                }
                                break;
                            case DataSection.DATA:
                                var sum = mNumDepots + mNumVisits;
                                break;
                            case DataSection.DEPOTS:
                                caseDepots(line, ref ndepot);
                                break;
                            case DataSection.DEMAND:
                                caseDemand(line, ref nCustomer);
                                break;
                            case DataSection.LOCATION_COORD:
                                caseLocationCoord(line, ref nLocation);
                                break;
                            case DataSection.DEPOT_LOCATION:
                                caseDeptLocation(line, ref ndepot, nLocation);
                                break;
                            case DataSection.VISIT_LOCATION:
                                caseVisitLocation(line, ref nCustomer, nLocation);
                                break;
                            case DataSection.DURATION:
                                caseDuration(line, ref nCustomer);
                                break;
                            case DataSection.DEPOT_TIME_WINDOW:
                                caseDepotTime(line, ref ndepot);
                                break;
                            case DataSection.TIME_AVAIL:
                                caseTimeAvail(line, ref nCustomer);
                                break;
                            case DataSection.EOF:
                                break;
                            default:
                                // Niepotrzebne linie
                                break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            nProblemData.VehiclesCount = mNumVehicles;
            nProblemData.Depots = ndepot;
            nProblemData.Customers = nCustomer;
            return nProblemData;
        }
예제 #2
0
 internal ProblemData Clone()
 {
     ProblemData problem = new ProblemData {Capacity = this.Capacity, Customers = this.Customers,
         Depots = this.Depots, Name = this.Name, VehiclesCount = this.VehiclesCount};
     return problem;
 }
예제 #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="partialData"> Dane otrzymane przez Node w ramach pewnego zbioru podproblemów. Dany zbiór może
        /// być 1-elementowy</param>
        /// <param name="timeout"> Czas po jakim obliczenia zostaną zakończone błędem przekroczenia czasu. </param>
        /// <returns> Rozwiązanie (najmniejszy koszt drogi) dla pewnej ilości podproblemów. </returns>
        public override byte[] Solve(byte[] partialData, TimeSpan timeout)
        {
            State = TaskSolverState.Solving;

            int timeoutMs = 0;

            timeoutMs += timeout.Milliseconds;
            timeoutMs += 1000 * timeout.Seconds;
            timeoutMs += 1000 * 60 * timeout.Minutes;
            timeoutMs += 1000 * 60 * 60 * timeout.Hours;
            timeoutMs += 1000 * 60 * 60 * 24 * timeout.Days;

            try
            {
                // Zawartość Compute w nowym wątku
                Compute(() =>
                {
                    var partialProblemData = DataSerialization.BinaryDeserializeObject<ProblemData>(partialData);

                    if (partialProblemData == null) throw new ArgumentNullException("partialProblemData");

                    problem = partialProblemData;
                    List<int> k = problem.PartitionsCount;
                    akt = 0;
                    min = double.MaxValue;

                    List<Point> points = new List<Point>();

                    CutOff();

                    points.AddRange(problem.Depots.Select(x => x.Location));
                    points.AddRange(problem.Customers.Select(x => x.Location));

                    distances = new Distances(points);

                    for (int i = 0; i < k.Count; ++i)
                    {
                        partitions = new List<List<int>>();
                        for (int j = 0; j < k[i]; ++j)
                            partitions.Add(new List<int>());

                        Stirling2(partialProblemData.Customers.Count(), k[i]);
                        Console.WriteLine("Minimalny koszt trasy dla podziału na {0} podzbiorów: {1}", k[i], min);
                    }
                    Console.WriteLine(@"Minimalny koszt dla wszystkich podproblemów w danym node: {0}", min);

                }, timeoutMs);

                if (ProblemSolvingFinished != null) ProblemSolvingFinished(new EventArgs(), this);

                // Jeśli została znaleziona poprawna trasa w danym podziale zwracamy jej koszt, jeśli nie -1
                return min == double.MaxValue ? DataSerialization.BinarySerializeObject(double.MaxValue) : DataSerialization.BinarySerializeObject(min);
            }
            catch (TimeoutException t)
            {
                State = TaskSolverState.Error | TaskSolverState.Idle;
                ErrorOccured(this, new UnhandledExceptionEventArgs(t, true));

                return min == double.MaxValue ? DataSerialization.BinarySerializeObject(double.MaxValue) : DataSerialization.BinarySerializeObject(min);
            }
            catch (ArgumentNullException a)
            {
                Console.WriteLine("partialProblemData may be null: " + a.Message);

                State = TaskSolverState.Error | TaskSolverState.Idle;
                ErrorOccured(this, new UnhandledExceptionEventArgs(a, true));

                return DataSerialization.BinarySerializeObject(double.MaxValue);
            }
            catch (Exception e)
            {
                Console.WriteLine("Other exception: " + e.Message);

                State = TaskSolverState.Error | TaskSolverState.Idle;
                ErrorOccured(this, new UnhandledExceptionEventArgs(e, true));

                return min == double.MaxValue ? DataSerialization.BinarySerializeObject(double.MaxValue) : DataSerialization.BinarySerializeObject(min); ;
            }
        }