1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
25 | using HeuristicLab.Persistence.Default.Xml;
|
---|
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 | /// <summary>
|
---|
37 | /// Summary description for GAVrpSampleTest
|
---|
38 | /// </summary>
|
---|
39 | [TestClass]
|
---|
40 | public class GAVrpSampleTest {
|
---|
41 | private const string samplesDirectory = SamplesUtils.Directory;
|
---|
42 | [ClassInitialize]
|
---|
43 | public static void MyClassInitialize(TestContext testContext) {
|
---|
44 | if (!Directory.Exists(samplesDirectory))
|
---|
45 | Directory.CreateDirectory(samplesDirectory);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | [TestMethod]
|
---|
50 | [TestCategory("Samples.Create")]
|
---|
51 | [TestProperty("Time", "medium")]
|
---|
52 | public void CreateGaVrpSampleTest() {
|
---|
53 | var ga = CreateGaVrpSample();
|
---|
54 | XmlGenerator.Serialize(ga, @"Samples\GA_VRP.hl");
|
---|
55 | }
|
---|
56 |
|
---|
57 | [TestMethod]
|
---|
58 | [TestCategory("Samples.Execute")]
|
---|
59 | [TestProperty("Time", "long")]
|
---|
60 | public void RunGaVrpSampleTest() {
|
---|
61 | var ga = CreateGaVrpSample();
|
---|
62 | ga.SetSeedRandomly.Value = false;
|
---|
63 | SamplesUtils.RunAlgorithm(ga);
|
---|
64 | Assert.AreEqual(1828.9368669428338, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
|
---|
65 | Assert.AreEqual(1830.1444308908331, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
66 | Assert.AreEqual(1871.7128510304112, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
67 | Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | private GeneticAlgorithm CreateGaVrpSample() {
|
---|
71 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
72 | #region Problem Configuration
|
---|
73 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
74 |
|
---|
75 | SolomonFormatInstanceProvider instanceProvider = new SolomonInstanceProvider();
|
---|
76 | CVRPTWData data = instanceProvider.Import(@"Test Resources\C101.txt", @"Test Resources\C101.opt.txt") as CVRPTWData;
|
---|
77 | vrpProblem.Load(data);
|
---|
78 | vrpProblem.Name = "C101 VRP (imported from Solomon)";
|
---|
79 | vrpProblem.Description = "Represents a Vehicle Routing Problem.";
|
---|
80 | CVRPTWProblemInstance instance = vrpProblem.ProblemInstance as CVRPTWProblemInstance;
|
---|
81 | instance.DistanceFactor.Value = 1;
|
---|
82 | instance.FleetUsageFactor.Value = 100;
|
---|
83 | instance.OverloadPenalty.Value = 100;
|
---|
84 | instance.TardinessPenalty.Value = 100;
|
---|
85 | instance.TimeFactor.Value = 0;
|
---|
86 | vrpProblem.MaximizationParameter.Value.Value = false;
|
---|
87 | instance.UseDistanceMatrix.Value = true;
|
---|
88 | instance.Vehicles.Value = 25;
|
---|
89 | #endregion
|
---|
90 | #region Algorithm Configuration
|
---|
91 | ga.Name = "Genetic Algorithm - VRP";
|
---|
92 | ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
|
---|
93 | ga.Problem = vrpProblem;
|
---|
94 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
|
---|
95 | ga, 100, 1, 1000, 0.05, 3);
|
---|
96 |
|
---|
97 | var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
|
---|
98 | foreach (var op in xOver.Operators) {
|
---|
99 | xOver.Operators.SetItemCheckedState(op, false);
|
---|
100 | }
|
---|
101 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
102 | .OfType<PotvinRouteBasedCrossover>()
|
---|
103 | .Single(), true);
|
---|
104 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
105 | .OfType<PotvinSequenceBasedCrossover>()
|
---|
106 | .Single(), true);
|
---|
107 |
|
---|
108 | var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
|
---|
109 | foreach (var op in manipulator.Operators) {
|
---|
110 | manipulator.Operators.SetItemCheckedState(op, false);
|
---|
111 | }
|
---|
112 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
113 | .OfType<PotvinOneLevelExchangeMainpulator>()
|
---|
114 | .Single(), true);
|
---|
115 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
116 | .OfType<PotvinTwoLevelExchangeManipulator>()
|
---|
117 | .Single(), true);
|
---|
118 | #endregion
|
---|
119 | return ga;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|