private void WriteToFile(VrpProblem problem, string file)
 {
     using (var fs = new FileStream(file, FileMode.Create))
     {
         using (var sw = new StreamWriter(fs))
         {
             sw.WriteLine($"NAME : {problem.Name}");
             sw.WriteLine($"COMMENT: {problem.Comment}");
             sw.WriteLine("TYPE : CVRP");
             sw.WriteLine($"DIMENSION : {problem.Dimension}");
             sw.WriteLine("EDGE_WEIGHT_TYPE : EXPLICIT");
             sw.WriteLine("EDGE_WEIGHT_FORMAT: FULL_MATRIX");
             sw.WriteLine($"CAPACITY : {problem.Capacity}");
             if (problem.Coordinates == null)
             {
                 sw.WriteLine("DISPLAY_DATA_TYPE: NO_DISPLAY");
             }
             else
             {
                 sw.WriteLine("DISPLAY_DATA_TYPE: TWOD_DISPLAY");
                 WriteCoordinates(sw, problem.Coordinates);
             }
             WriteMatrix(sw, problem.Distances);
             WriteDemands(sw, problem.Demands);
             WriteDepot(sw);
             sw.WriteLine("EOF");
         }
     }
 }
        /// <summary>
        /// Generates random map VRP problem instance.
        /// </summary>
        /// <param name="parameters">Problem parameters.</param>
        /// <returns>A task that represents asynchronous operation.</returns>
        public async Task <VrpProblem> Generate(GeneratorParameters parameters)
        {
            int[]           demands = _demandGenerator.GenerateDemands(parameters.Clients);
            List <Location> coords  = new List <Location>(parameters.Clients + 1);

            coords.Add(parameters.Depot);
            var generated = await _clientCoordsGenerator.GenerateClientCoords(parameters.Clients, parameters.Streets);

            coords.AddRange(generated.SelectMany(x => x.Locations));
            var groups = new Dictionary <int, LocationGroup>();

            groups.Add(1, new LocationGroup()
            {
                StreetId = 0, StreetPartId = 0
            });
            int id = 2;

            foreach (var g in generated)
            {
                foreach (var gLocation in g.Locations)
                {
                    groups.Add(id++, g.LocationGroup);
                }
            }
            var matrix = await _distanceMatrixGenerator.GenerateDistanceMatrix(coords);

            var problem = new VrpProblem()
            {
                Capacity       = parameters.Capacity,
                Comment        = parameters.Comment,
                Demands        = demands,
                DepotIndex     = 0,
                Dimension      = parameters.Clients + 1,
                Distances      = matrix,
                Name           = parameters.ProblemName,
                LocationGroups = groups
            };

            if (parameters.IncludeCoords)
            {
                problem.Coordinates = coords.ToArray();
            }

            return(problem);
        }
示例#3
0
        static async Task WriteFiles(IVrpProblemWriter writer, VrpProblem problem, Arguments args, int instanceNum = 1)
        {
            var output = args.OutputPath ?? (args.ProblemName + ".vrp");

            if (args.NumberOfInstances > 1)
            {
                output = AdjustPath(output, instanceNum);
            }
            await writer.Write(problem, output);

            if (!string.IsNullOrEmpty(args.AdditionalInfoFilePath))
            {
                var path = args.AdditionalInfoFilePath;
                if (args.NumberOfInstances > 1)
                {
                    path = AdjustPath(path, instanceNum);
                }
                await writer.WriteWithAdditionalInfo(problem, path);
            }
        }
 /// <summary>
 /// Saves VRP problem instance to a file (it also saves some additional information like locating nodes around the streets).
 /// </summary>
 /// <param name="problem">VRP problem instance.</param>
 /// <param name="file">Path to file.</param>
 /// <returns>A task representing the asynchronous operation.</returns>
 public async Task WriteWithAdditionalInfo(VrpProblem problem, string file)
 {
     await Task.Run(() => WriteToFileWithAdditionalInfo(problem, file));
 }
 /// <summary>
 /// Saves VRP problem instance to a file.
 /// </summary>
 /// <param name="problem">VRP problem instance.</param>
 /// <param name="file">Path to file.</param>
 /// <returns>A task representing the asynchronous operation.</returns>
 public async Task Write(VrpProblem problem, string file)
 {
     await Task.Run(() => WriteToFile(problem, file));
 }