Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAVrpSampleTest.cs @ 17732

Last change on this file since 17732 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.9 KB
RevLine 
[11450]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11450]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;
[17021]24using HEAL.Attic;
[11450]25using HeuristicLab.Algorithms.GeneticAlgorithm;
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 {
[11514]38    private const string SampleFileName = "GA_VRP";
[11450]39
[17021]40    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
41
[11450]42    [TestMethod]
43    [TestCategory("Samples.Create")]
44    [TestProperty("Time", "medium")]
45    public void CreateGaVrpSampleTest() {
46      var ga = CreateGaVrpSample();
[11514]47      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
[17021]48      serializer.Serialize(ga, path);
[11450]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();
[11514]66
[11450]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
[11514]114
[11450]115      return ga;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.