1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.IO;
|
---|
23 | using System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
26 | using HeuristicLab.Problems.Instances;
|
---|
27 | using HeuristicLab.Problems.Instances.VehicleRouting;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Tests {
|
---|
36 | [TestClass]
|
---|
37 | public class GAVrpSampleTest {
|
---|
38 | private const string SampleFileName = "GA_VRP";
|
---|
39 |
|
---|
40 | private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
41 |
|
---|
42 | [TestMethod]
|
---|
43 | [TestCategory("Samples.Create")]
|
---|
44 | [TestProperty("Time", "medium")]
|
---|
45 | public void CreateGaVrpSampleTest() {
|
---|
46 | var ga = CreateGaVrpSample();
|
---|
47 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
48 | serializer.Serialize(ga, path);
|
---|
49 | }
|
---|
50 |
|
---|
51 | [TestMethod]
|
---|
52 | [TestCategory("Samples.Execute")]
|
---|
53 | [TestProperty("Time", "long")]
|
---|
54 | public void RunGaVrpSampleTest() {
|
---|
55 | var ga = CreateGaVrpSample();
|
---|
56 | ga.SetSeedRandomly.Value = false;
|
---|
57 | SamplesUtils.RunAlgorithm(ga);
|
---|
58 | Assert.AreEqual(1828.9368669428338, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
|
---|
59 | Assert.AreEqual(1830.1444308908331, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
60 | Assert.AreEqual(1871.7128510304112, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
61 | Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
62 | }
|
---|
63 |
|
---|
64 | private GeneticAlgorithm CreateGaVrpSample() {
|
---|
65 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
66 |
|
---|
67 | #region Problem Configuration
|
---|
68 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
69 |
|
---|
70 | SolomonFormatInstanceProvider instanceProvider = new SolomonInstanceProvider();
|
---|
71 | CVRPTWData data = instanceProvider.Import(@"Test Resources\C101.txt", @"Test Resources\C101.opt.txt") as CVRPTWData;
|
---|
72 | vrpProblem.Load(data);
|
---|
73 | vrpProblem.Name = "C101 VRP (imported from Solomon)";
|
---|
74 | vrpProblem.Description = "Represents a Vehicle Routing Problem.";
|
---|
75 | CVRPTWProblemInstance instance = vrpProblem.ProblemInstance as CVRPTWProblemInstance;
|
---|
76 | instance.DistanceFactor.Value = 1;
|
---|
77 | instance.FleetUsageFactor.Value = 100;
|
---|
78 | instance.OverloadPenalty.Value = 100;
|
---|
79 | instance.TardinessPenalty.Value = 100;
|
---|
80 | instance.TimeFactor.Value = 0;
|
---|
81 | vrpProblem.MaximizationParameter.Value.Value = false;
|
---|
82 | instance.UseDistanceMatrix.Value = true;
|
---|
83 | instance.Vehicles.Value = 25;
|
---|
84 | #endregion
|
---|
85 | #region Algorithm Configuration
|
---|
86 | ga.Name = "Genetic Algorithm - VRP";
|
---|
87 | ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
|
---|
88 | ga.Problem = vrpProblem;
|
---|
89 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
|
---|
90 | ga, 100, 1, 1000, 0.05, 3);
|
---|
91 |
|
---|
92 | var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
|
---|
93 | foreach (var op in xOver.Operators) {
|
---|
94 | xOver.Operators.SetItemCheckedState(op, false);
|
---|
95 | }
|
---|
96 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
97 | .OfType<PotvinRouteBasedCrossover>()
|
---|
98 | .Single(), true);
|
---|
99 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
100 | .OfType<PotvinSequenceBasedCrossover>()
|
---|
101 | .Single(), true);
|
---|
102 |
|
---|
103 | var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
|
---|
104 | foreach (var op in manipulator.Operators) {
|
---|
105 | manipulator.Operators.SetItemCheckedState(op, false);
|
---|
106 | }
|
---|
107 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
108 | .OfType<PotvinOneLevelExchangeMainpulator>()
|
---|
109 | .Single(), true);
|
---|
110 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
111 | .OfType<PotvinTwoLevelExchangeManipulator>()
|
---|
112 | .Single(), true);
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | return ga;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|