Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAVrpSampleTest.cs @ 16045

Last change on this file since 16045 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System.IO;
23using System.Linq;
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Persistence.Default.Xml;
26using HeuristicLab.Problems.Instances;
27using HeuristicLab.Problems.Instances.VehicleRouting;
28using HeuristicLab.Problems.VehicleRouting;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
30using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
31using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
32using HeuristicLab.Selection;
33using Microsoft.VisualStudio.TestTools.UnitTesting;
34
35namespace HeuristicLab.Tests {
36  [TestClass]
37  public class GAVrpSampleTest {
38    private const string SampleFileName = "GA_VRP";
39
40    [TestMethod]
41    [TestCategory("Samples.Create")]
42    [TestProperty("Time", "medium")]
43    public void CreateGaVrpSampleTest() {
44      var ga = CreateGaVrpSample();
45      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
46      XmlGenerator.Serialize(ga, path);
47    }
48
49    [TestMethod]
50    [TestCategory("Samples.Execute")]
51    [TestProperty("Time", "long")]
52    public void RunGaVrpSampleTest() {
53      var ga = CreateGaVrpSample();
54      ga.SetSeedRandomly.Value = false;
55      SamplesUtils.RunAlgorithm(ga);
56      Assert.AreEqual(1828.9368669428338, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
57      Assert.AreEqual(1830.1444308908331, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
58      Assert.AreEqual(1871.7128510304112, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
59      Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
60    }
61
62    private GeneticAlgorithm CreateGaVrpSample() {
63      GeneticAlgorithm ga = new GeneticAlgorithm();
64
65      #region Problem Configuration
66      VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
67
68      SolomonFormatInstanceProvider instanceProvider = new SolomonInstanceProvider();
69      CVRPTWData data = instanceProvider.Import(@"Test Resources\C101.txt", @"Test Resources\C101.opt.txt") as CVRPTWData;
70      vrpProblem.Load(data);
71      vrpProblem.Name = "C101 VRP (imported from Solomon)";
72      vrpProblem.Description = "Represents a Vehicle Routing Problem.";
73      CVRPTWProblemInstance instance = vrpProblem.ProblemInstance as CVRPTWProblemInstance;
74      instance.DistanceFactor.Value = 1;
75      instance.FleetUsageFactor.Value = 100;
76      instance.OverloadPenalty.Value = 100;
77      instance.TardinessPenalty.Value = 100;
78      instance.TimeFactor.Value = 0;
79      vrpProblem.MaximizationParameter.Value.Value = false;
80      instance.UseDistanceMatrix.Value = true;
81      instance.Vehicles.Value = 25;
82      #endregion
83      #region Algorithm Configuration
84      ga.Name = "Genetic Algorithm - VRP";
85      ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
86      ga.Problem = vrpProblem;
87      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
88        ga, 100, 1, 1000, 0.05, 3);
89
90      var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
91      foreach (var op in xOver.Operators) {
92        xOver.Operators.SetItemCheckedState(op, false);
93      }
94      xOver.Operators.SetItemCheckedState(xOver.Operators
95        .OfType<PotvinRouteBasedCrossover>()
96        .Single(), true);
97      xOver.Operators.SetItemCheckedState(xOver.Operators
98        .OfType<PotvinSequenceBasedCrossover>()
99        .Single(), true);
100
101      var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
102      foreach (var op in manipulator.Operators) {
103        manipulator.Operators.SetItemCheckedState(op, false);
104      }
105      manipulator.Operators.SetItemCheckedState(manipulator.Operators
106        .OfType<PotvinOneLevelExchangeMainpulator>()
107        .Single(), true);
108      manipulator.Operators.SetItemCheckedState(manipulator.Operators
109        .OfType<PotvinTwoLevelExchangeManipulator>()
110        .Single(), true);
111      #endregion
112
113      return ga;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.