public void DvrpSimpleProblemTest() { var loc1 = new Location { Id = 1, X = 1, Y = 2 }; var loc2 = new Location { Id = 2, X = 3, Y = 4 }; var visit1 = new Visit { AvailabilityTime = 0, Demand = 3, Duration = 1, Id = 1, Location = loc2 }; var depot1 = new Depot { EarliestDepartureTime = 0, Id = 1, LatestReturnTime = 10, Location = loc1 }; var depots = new List <Depot>(); depots.Add(depot1); var locations = new List <Location>(); locations.Add(loc1); locations.Add(loc2); var visits = new List <Visit>(); visits.Add(visit1); var vehicleCap = 100; var vehicleNumber = 2; var instance = new DVRPProblemInstance { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, Visits = visits }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(instance); var taskSolver = new DVRPTaskSolver(bytes); var divideProblem = taskSolver.DivideProblem(0); //Assert.AreEqual(1, divideProblem.Length); //solving var solveProblem1 = taskSolver.Solve(divideProblem[0], TimeSpan.Zero); var finalSol = taskSolver.MergeSolution(new[] { solveProblem1 }); //asercje necessary, jutro moze to zrobie }
public void ProblemFromGeneratorParsingTest2() { var parser = new DVRPProblemParser("io2_5_plain_a_D.vrp"); DVRPProblemInstance problem = parser.Parse(); Assert.AreEqual(problem.Visits.Count, 5); Assert.AreEqual(problem.Visits[3].Location.Y, -10); Assert.AreEqual(problem.Visits.First().Duration, 20); Assert.AreEqual(problem.VehicleNumber, 5); Assert.AreEqual(problem.Depots.First().Location.X, 0); }
public void DvrpProblemInstanceSerializationTest() { var problem = new DVRPProblemInstance(); problem.Depots.Add(new Depot() { Id = 5, EarliestDepartureTime = 123 }); problem.Visits.Add(new Visit() { Location = new Location() { X = 5, Y = 5 } }); string xml = _serializer.ToXmlString(problem); DVRPProblemInstance p = (DVRPProblemInstance)_serializer.FromXmlString(xml); Assert.AreEqual(problem.Depots.First().Id, p.Depots.First().Id); Assert.AreEqual(problem.Visits.First().Location.X, p.Visits.First().Location.X); Assert.AreEqual(problem.Visits.First().Location.Y, p.Visits.First().Location.Y); }
public void DvrpReturnToDepotTest() { var locationsArray = new[] { new Location { Id = 0, X = 0, Y = 0 }, new Location { Id = 1, X = 8, Y = 6 }, new Location { Id = 2, X = -8, Y = -6 } }; var depot = new Depot { Id = 0, Location = locationsArray[0], EarliestDepartureTime = 0, LatestReturnTime = 9999 }; var visitsArray = new[] { new Visit { AvailabilityTime = 0, Demand = -99, Duration = 10, Id = 1, Location = locationsArray[1] }, new Visit { AvailabilityTime = 0, Demand = -99, Duration = 10, Id = 2, Location = locationsArray[2] } }; var depots = new List <Depot> { depot }; var locations = new List <Location>(locationsArray); var visits = new List <Visit>(visitsArray); const int vehicleCap = 100; const int vehicleNumber = 1; const int vehicleSpeed = 100; var problem = new DVRPProblemInstance { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, Visits = visits, VehicleSpeed = vehicleSpeed }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(problem); var taskSolver = new DVRPTaskSolver(bytes); var divideProblem = taskSolver.DivideProblem(0); var partialProblems = divideProblem.Select(partialProblem => (DVRPPartialProblemInstance)converter.FromBytesArray(partialProblem)).ToList(); //Assert.AreEqual(1, partialProblems.Count); var solvePartialProblem = new ConcurrentQueue <byte[]>(); Parallel.ForEach(divideProblem, element => { solvePartialProblem.Enqueue(taskSolver.Solve(element, TimeSpan.Zero)); }); var finalSolutionBytes = taskSolver.MergeSolution(solvePartialProblem.ToArray()); var finalSolution = (DVRPPartialProblemInstance)converter.FromBytesArray(finalSolutionBytes); Assert.AreEqual(finalSolution.SolutionResult, SolutionResult.Successful); Assert.AreEqual(40, finalSolution.PartialResult, 0.2f); }
public void DvrpDistantGroupsOfVisits() { var locationsArray = new[] { new Location { Id = 0, X = 0, Y = 0 }, new Location { Id = 1, X = 115, Y = 115 }, new Location { Id = 2, X = 120, Y = 120 }, new Location { Id = 3, X = -100, Y = -100 }, new Location { Id = 4, X = -101, Y = -101 } }; var visitsArray = new[] { new Visit { AvailabilityTime = 0, Demand = -10, Duration = 20, Id = 1, Location = locationsArray[1] }, new Visit { AvailabilityTime = 0, Demand = -10, Duration = 20, Id = 2, Location = locationsArray[2] }, new Visit { AvailabilityTime = 0, Demand = -10, Duration = 20, Id = 3, Location = locationsArray[3] }, new Visit { AvailabilityTime = 0, Demand = -10, Duration = 20, Id = 4, Location = locationsArray[4] } }; var depot = new Depot { Id = 0, Location = locationsArray[0], EarliestDepartureTime = 0, LatestReturnTime = 390 }; var depots = new List <Depot> { depot }; var locations = new List <Location>(locationsArray); var visits = new List <Visit>(visitsArray); const int vehicleCap = 100; const int vehicleNumber = 2; const int vehicleSpeed = 1; var problem = new DVRPProblemInstance { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, VehicleSpeed = vehicleSpeed, Visits = visits }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(problem); var taskSolver = new DVRPTaskSolver(bytes); var divideProblem = taskSolver.DivideProblem(0); var partialProblems = divideProblem.Select(partialProblem => (DVRPPartialProblemInstance)converter.FromBytesArray(partialProblem)).ToList(); //Assert.AreEqual(8, partialProblems.Count); var solvePartialProblem = new ConcurrentQueue <byte[]>(); Parallel.ForEach(divideProblem, element => { solvePartialProblem.Enqueue(taskSolver.Solve(element, TimeSpan.Zero)); }); var finalSolutionBytes = taskSolver.MergeSolution(solvePartialProblem.ToArray()); var finalSolution = (DVRPPartialProblemInstance)converter.FromBytesArray(finalSolutionBytes); Assert.AreEqual(finalSolution.SolutionResult, SolutionResult.Successful); Assert.AreEqual(2, finalSolution.VisitIds.Count(x => x.Length > 0)); Assert.AreEqual(625.08, Round(finalSolution.PartialResult, 2)); Assert.IsTrue(finalSolution.VisitIds.Any(x => x.Contains(3) && x.Contains(4))); }
public void DvrpAlgorithmTest_ForAnotherProblem() { // 5 locations var locationsArray = new[] { new Location { Id = 0, X = 0, Y = 0 }, new Location { Id = 1, X = -67, Y = 14 }, new Location { Id = 2, X = 94, Y = 14 }, new Location { Id = 3, X = -29, Y = -19 }, new Location { Id = 4, X = 43, Y = 24 } }; // 4 visits var visitsArray = new[] { new Visit { AvailabilityTime = 196, Demand = -48, Duration = 20, Id = 1, Location = locationsArray[1] }, new Visit { AvailabilityTime = 443, Demand = -16, Duration = 20, Id = 2, Location = locationsArray[2] }, new Visit { AvailabilityTime = 180, Demand = -35, Duration = 20, Id = 3, Location = locationsArray[3] }, new Visit { AvailabilityTime = 15, Demand = -37, Duration = 20, Id = 4, Location = locationsArray[4] } }; // 1 depot var depot = new Depot { Id = 0, Location = locationsArray[0], EarliestDepartureTime = 0, LatestReturnTime = 480 }; var depots = new List <Depot> { depot }; var locations = new List <Location>(locationsArray); var visits = new List <Visit>(visitsArray); const int vehicleCap = 100; const int vehicleNumber = 4; var problem = new DVRPProblemInstance { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, VehicleSpeed = 7, Visits = visits }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(problem); var taskSolver = new DVRPTaskSolver(bytes); var divideProblem = taskSolver.DivideProblem(0); var partialProblems = divideProblem.Select(partialProblem => (DVRPPartialProblemInstance)converter.FromBytesArray(partialProblem)).ToList(); //Assert.AreEqual(3, partialProblems.Count); var solvePartialProblem = new ConcurrentQueue <byte[]>(); Parallel.ForEach(divideProblem, element => { solvePartialProblem.Enqueue(taskSolver.Solve(element, TimeSpan.Zero)); }); var finalSolutionBytes = taskSolver.MergeSolution(solvePartialProblem.ToArray()); var finalSolution = (DVRPPartialProblemInstance)converter.FromBytesArray(finalSolutionBytes); Assert.AreEqual(SolutionResult.Successful, finalSolution.SolutionResult); Assert.AreEqual(Round(finalSolution.PartialResult, 2), 349.70, 2.5f); }
public void DvrpAlgorithmTest_ForAnotherProblemSample() { // 5 locations var locationsArray = new[] { new Location { Id = 0, X = 0, Y = 0 }, new Location { Id = 1, X = 57, Y = 82 }, new Location { Id = 2, X = -95, Y = 36 }, new Location { Id = 3, X = -20, Y = -78 }, new Location { Id = 4, X = 40, Y = -32 } }; // 4 visits var visitsArray = new[] { new Visit { AvailabilityTime = 37, Demand = -39, Duration = 20, Id = 1, Location = locationsArray[1] }, new Visit { AvailabilityTime = 192, Demand = -17, Duration = 20, Id = 2, Location = locationsArray[2] }, new Visit { AvailabilityTime = 243, Demand = -18, Duration = 20, Id = 3, Location = locationsArray[3] }, new Visit { AvailabilityTime = 151, Demand = -20, Duration = 20, Id = 4, Location = locationsArray[4] } }; // 1 depot var depot = new Depot { Id = 0, Location = locationsArray[0], EarliestDepartureTime = 0, LatestReturnTime = 480 }; var depots = new List <Depot> { depot }; var locations = new List <Location>(locationsArray); var visits = new List <Visit>(visitsArray); const int vehicleCap = 100; const int vehicleNumber = 4; const int vehicleSpeed = 1; var problem = new DVRPProblemInstance { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, VehicleSpeed = vehicleSpeed, Visits = visits }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(problem); var taskSolver = new DVRPTaskSolver(bytes); var divideProblem = taskSolver.DivideProblem(0); var partialProblems = divideProblem.Select(partialProblem => (DVRPPartialProblemInstance)converter.FromBytesArray(partialProblem)).ToList(); //Assert.AreEqual(3, partialProblems.Count); var solvePartialProblem = new ConcurrentQueue <byte[]>(); Parallel.ForEach(divideProblem, element => { solvePartialProblem.Enqueue(taskSolver.Solve(element, TimeSpan.Zero)); }); var finalSolutionBytes = taskSolver.MergeSolution(solvePartialProblem.ToArray()); var finalSolution = (DVRPPartialProblemInstance)converter.FromBytesArray(finalSolutionBytes); Assert.AreEqual(finalSolution.SolutionResult, SolutionResult.Successful); Assert.AreEqual(567.62, Round(finalSolution.PartialResult, 2), 3.5f); var expected = new[] { new [] { 1, 2 }, new int[] { 3, 4 }, new int[] {}, new int[] {} }; for (int j = 0; j < finalSolution.VisitIds.GetLength(0); j++) { Assert.IsTrue( expected.Any(x => { if (x.Length != finalSolution.VisitIds[j].Length) { return(false); } for (var i = 0; i < x.Length; i++) { if (x[i] != finalSolution.VisitIds[j][i]) { return(false); } } return(true); }) ); } }
public void SimpleParseTest() { var loc1 = new Location() { Id = 1, X = 1, Y = 2 }; var loc2 = new Location() { Id = 2, X = 3, Y = 4 }; var visit1 = new Visit() { AvailabilityTime = 2, Demand = 3, Duration = 1, Id = 1, Location = loc2 }; var depot1 = new Depot() { EarliestDepartureTime = 0, Id = 1, LatestReturnTime = 10, Location = loc1 }; var depots = new List <Depot>(); depots.Add(depot1); var locations = new List <Location>(); locations.Add(loc1); locations.Add(loc2); var visits = new List <Visit>(); visits.Add(visit1); var vehicleCap = 100; var vehicleNumber = 10; var instance = new DVRPProblemInstance() { Depots = depots, Locations = locations, VehicleCapacity = vehicleCap, VehicleNumber = vehicleNumber, Visits = visits }; var converter = new ProblemToBytesConverter(); var bytes = converter.ToByteArray(instance); var ret = (DVRPProblemInstance)converter.FromBytesArray(bytes); Assert.AreEqual(loc1.Id, ret.Locations[0].Id); Assert.AreEqual(loc1.X, ret.Locations[0].X); Assert.AreEqual(loc1.Y, ret.Locations[0].Y); Assert.AreEqual(loc2.Id, ret.Locations[1].Id); Assert.AreEqual(loc2.X, ret.Locations[1].X); Assert.AreEqual(loc2.Y, ret.Locations[1].Y); Assert.AreEqual(depot1.Id, ret.Depots[0].Id); Assert.AreEqual(depot1.EarliestDepartureTime, ret.Depots[0].EarliestDepartureTime); Assert.AreEqual(depot1.LatestReturnTime, ret.Depots[0].LatestReturnTime); Assert.AreEqual(depot1.Location.Id, ret.Depots[0].Location.Id); Assert.AreEqual(depot1.Location.X, ret.Depots[0].Location.X); Assert.AreEqual(depot1.Location.Y, ret.Depots[0].Location.Y); Assert.AreEqual(visit1.Id, ret.Visits[0].Id); Assert.AreEqual(visit1.AvailabilityTime, ret.Visits[0].AvailabilityTime); Assert.AreEqual(visit1.Demand, ret.Visits[0].Demand); Assert.AreEqual(visit1.Duration, ret.Visits[0].Duration); Assert.AreEqual(visit1.Location.Id, ret.Visits[0].Location.Id); Assert.AreEqual(visit1.Location.X, ret.Visits[0].Location.X); Assert.AreEqual(visit1.Location.Y, ret.Visits[0].Location.Y); Assert.AreEqual(ret.VehicleCapacity, vehicleCap); Assert.AreEqual(ret.VehicleNumber, vehicleNumber); Assert.AreEqual(1, ret.Visits.Count); Assert.AreEqual(2, ret.Locations.Count); Assert.AreEqual(1, ret.Depots.Count); }
public void ProblemTest3() { #region Init DVRPProblemInstance instance = new DVRPProblemInstance() { Depots = new List <Depot>() { new Depot() { Id = 0, Location = new Location() { Id = 0, X = 0, Y = 0 }, EarliestDepartureTime = 0, LatestReturnTime = 520 } }, Locations = new List <Location>() { new Location() { Id = 0, X = 0, Y = 0 }, new Location() { Id = 1, X = -78, Y = -81 }, new Location() { Id = 2, X = 35, Y = -4 }, new Location() { Id = 3, X = -23, Y = -64 }, new Location() { Id = 4, X = 76, Y = 31 }, new Location() { Id = 5, X = 49, Y = 83 }, new Location() { Id = 6, X = -9, Y = 48 } }, VehicleCapacity = 100, VehicleNumber = 6, VehicleSpeed = 3, Visits = new List <Visit>() { new Visit() { AvailabilityTime = 372, Demand = -18, Duration = 20, Id = 1, Location = new Location() { Id = 1, X = -78, Y = -81 } }, new Visit() { AvailabilityTime = 483, Demand = -44, Duration = 20, Id = 2, Location = new Location() { Id = 2, X = 35, Y = -4 } }, new Visit() { AvailabilityTime = 354, Demand = -25, Duration = 20, Id = 3, Location = new Location() { Id = 3, X = -23, Y = -64 } }, new Visit() { AvailabilityTime = 86, Demand = -25, Duration = 20, Id = 4, Location = new Location() { Id = 4, X = 76, Y = 31 } }, new Visit() { AvailabilityTime = 19, Demand = -28, Duration = 20, Id = 5, Location = new Location() { Id = 5, X = 49, Y = 83 } }, new Visit() { AvailabilityTime = 24, Demand = -22, Duration = 20, Id = 6, Location = new Location() { Id = 6, X = -9, Y = 48 } } } }; #endregion byte[] bytes = _converter.ToByteArray(instance); TaskSolver solver = new DVRPTaskSolver(bytes); var divideProblem = solver.DivideProblem(1); List <byte[]> results = divideProblem.Select(b => solver.Solve(b, TimeSpan.Zero)).ToList(); var final = _converter.FromBytesArray(solver.MergeSolution(results.ToArray())); Assert.IsNotNull(final); Assert.IsInstanceOfType(final, typeof(DVRPPartialProblemInstance)); Assert.AreEqual(SolutionResult.Successful, ((DVRPPartialProblemInstance)final).SolutionResult); Assert.AreEqual(545f, ((DVRPPartialProblemInstance)final).PartialResult, 1); }
public void ProblemTest1() { #region Init DVRPProblemInstance instance = new DVRPProblemInstance() { Depots = new List <Depot>() { new Depot() { Id = 0, Location = new Location() { Id = 0, X = 0, Y = 0 }, EarliestDepartureTime = 0, LatestReturnTime = 500 } }, Locations = new List <Location>() { new Location() { Id = 0, X = 0, Y = 0 }, new Location() { Id = 1, X = 48, Y = -53 }, new Location() { Id = 2, X = 63, Y = 35 }, new Location() { Id = 3, X = -25, Y = 22 }, new Location() { Id = 4, X = -33, Y = 74 }, new Location() { Id = 5, X = -23, Y = -17 } }, VehicleCapacity = 100, VehicleNumber = 5, Visits = new List <Visit>() { new Visit() { AvailabilityTime = 343, Demand = -11, Duration = 20, Id = 1, Location = new Location() { Id = 1, X = 48, Y = -53 } }, new Visit() { AvailabilityTime = 230, Demand = -26, Duration = 20, Id = 2, Location = new Location() { Id = 2, X = 63, Y = 35 } }, new Visit() { AvailabilityTime = 178, Demand = -45, Duration = 20, Id = 3, Location = new Location() { Id = 3, X = -25, Y = 22 } }, new Visit() { AvailabilityTime = 488, Demand = -28, Duration = 20, Id = 4, Location = new Location() { Id = 4, X = -33, Y = 74 } }, new Visit() { AvailabilityTime = 283, Demand = -15, Duration = 20, Id = 5, Location = new Location() { Id = 5, X = -23, Y = -17 } }, } }; #endregion byte[] bytes = _converter.ToByteArray(instance); TaskSolver solver = new DVRPTaskSolver(bytes); var divideProblem = solver.DivideProblem(1); List <byte[]> results = divideProblem.Select(b => solver.Solve(b, TimeSpan.Zero)).ToList(); var final = _converter.FromBytesArray(solver.MergeSolution(results.ToArray())); Assert.IsNotNull(final); Assert.IsInstanceOfType(final, typeof(DVRPPartialProblemInstance)); //Assert.AreEqual(SolutionResult.Successful, ((DVRPPartialProblemInstance)final).SolutionResult); //Assert.AreEqual(434.13, ((DVRPPartialProblemInstance)final).PartialResult); }
public void ProblemTest2() { #region Init DVRPProblemInstance instance = new DVRPProblemInstance() { Depots = new List <Depot>() { new Depot() { Id = 0, Location = new Location() { Id = 0, X = 0, Y = 0 }, EarliestDepartureTime = 0, LatestReturnTime = 500 } }, Locations = new List <Location>() { new Location() { Id = 0, X = 0, Y = 0 }, new Location() { Id = 1, X = 14, Y = 84 }, new Location() { Id = 2, X = 60, Y = -76 }, new Location() { Id = 3, X = -69, Y = -32 }, new Location() { Id = 4, X = 50, Y = -10 }, new Location() { Id = 5, X = -30, Y = -57 } }, VehicleCapacity = 100, VehicleNumber = 5, VehicleSpeed = 1, Visits = new List <Visit>() { new Visit() { AvailabilityTime = 100, Demand = -15, Duration = 20, Id = 1, Location = new Location() { Id = 1, X = 14, Y = 85 } }, new Visit() { AvailabilityTime = 55, Demand = -32, Duration = 20, Id = 2, Location = new Location() { Id = 2, X = 60, Y = -76 } }, new Visit() { AvailabilityTime = 223, Demand = -20, Duration = 20, Id = 3, Location = new Location() { Id = 3, X = -69, Y = -32 } }, new Visit() { AvailabilityTime = 61, Demand = -29, Duration = 20, Id = 4, Location = new Location() { Id = 4, X = 50, Y = -10 } }, new Visit() { AvailabilityTime = 409, Demand = -48, Duration = 20, Id = 5, Location = new Location() { Id = 5, X = -30, Y = -57 } } } }; #endregion byte[] bytes = _converter.ToByteArray(instance); TaskSolver solver = new DVRPTaskSolver(bytes); var divideProblem = solver.DivideProblem(1); List <byte[]> results = divideProblem.Select(b => solver.Solve(b, TimeSpan.Zero)).ToList(); var final = _converter.FromBytesArray(solver.MergeSolution(results.ToArray())); Assert.IsNotNull(final); Assert.IsInstanceOfType(final, typeof(DVRPPartialProblemInstance)); Assert.AreEqual(SolutionResult.Successful, ((DVRPPartialProblemInstance)final).SolutionResult); Assert.AreEqual(536.20, ((DVRPPartialProblemInstance)final).PartialResult, 2.5); }