Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAGroupingProblemSampleTest.cs @ 18127

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

#2875: Removed years in copyrights

File size: 5.2 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;
[17021]23using HEAL.Attic;
[11450]24using HeuristicLab.Algorithms.GeneticAlgorithm;
[12743]25using HeuristicLab.Encodings.LinearLinkageEncoding;
26using HeuristicLab.Problems.Programmable;
[11450]27using HeuristicLab.Selection;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Tests {
31  [TestClass]
[12743]32  public class GAGroupingProblemSampleTest {
33    private const string SampleFileName = "GA_Grouping";
[17021]34
35    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
36
[12743]37    #region Code
38    private const string ProblemCode = @"
39using System;
40using System.Linq;
41using System.Collections.Generic;
42using HeuristicLab.Common;
43using HeuristicLab.Core;
44using HeuristicLab.Data;
45using HeuristicLab.Encodings.LinearLinkageEncoding;
46using HeuristicLab.Optimization;
47using HeuristicLab.Problems.Programmable;
[11450]48
[12743]49namespace HeuristicLab.Problems.Programmable {
50  public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
51    private const int ProblemSize = 100;
52    public bool Maximization { get { return false; } }
53
[14475]54    private bool[,] adjacencyMatrix;
[12743]55     
56    public override void Initialize() {
57      var encoding = new LinearLinkageEncoding(""lle"", length: ProblemSize);
[14475]58      adjacencyMatrix = new bool[encoding.Length, encoding.Length];
[12743]59      var random = new System.Random(13);
60      for (var i = 0; i < encoding.Length - 1; i++)
61        for (var j = i + 1; j < encoding.Length; j++)
[14475]62          adjacencyMatrix[i, j] = adjacencyMatrix[j, i] = random.Next(2) == 0;
[12743]63     
64      Encoding = encoding;
65    }
66
67    public double Evaluate(Individual individual, IRandom random) {
68      var penalty = 0;
69      var groups = individual.LinearLinkage(""lle"").GetGroups().ToList();
70      for (var i = 0; i < groups.Count; i++) {
71        for (var j = 0; j < groups[i].Count; j++)
72          for (var k = j + 1; k < groups[i].Count; k++)
[14475]73            if (!adjacencyMatrix[groups[i][j], groups[i][k]]) penalty++;
[12743]74      }
[14475]75      var result = groups.Count;
76      if (penalty > 0) result += penalty + ProblemSize;
77      return result;
[12743]78    }
79
80    public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
81
82    public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
83      foreach (var move in ExhaustiveSwap2MoveGenerator.Generate(individual.LinearLinkage(""lle""))) {
84        var neighbor = individual.Copy();
85        var lle = neighbor.LinearLinkage(""lle"");
86        Swap2MoveMaker.Apply(lle, move);
87        yield return neighbor;
88      }
89    }
90  }
91}
92";
93    #endregion
94
[11450]95    [TestMethod]
96    [TestCategory("Samples.Create")]
97    [TestProperty("Time", "medium")]
[12743]98    public void CreateGaGroupingProblemSampleTest() {
99      var ga = CreateGaGroupingProblemSample();
[11514]100      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
[17021]101      serializer.Serialize(ga, path);
[11450]102    }
[11514]103
[11450]104    [TestMethod]
105    [TestCategory("Samples.Execute")]
106    [TestProperty("Time", "long")]
[12743]107    public void RunGaGroupingProblemSampleTest() {
108      var ga = CreateGaGroupingProblemSample();
[11450]109      ga.SetSeedRandomly.Value = false;
110      SamplesUtils.RunAlgorithm(ga);
[14475]111      Assert.AreEqual(127, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
112      Assert.AreEqual(129,38, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
113      Assert.AreEqual(132, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
[11450]114      Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
115    }
116
[12743]117    private GeneticAlgorithm CreateGaGroupingProblemSample() {
[11450]118      GeneticAlgorithm ga = new GeneticAlgorithm();
[11514]119
[11450]120      #region Problem Configuration
[12743]121      var problem = new SingleObjectiveProgrammableProblem() {
122        ProblemScript = { Code = ProblemCode }
123      };
124      problem.ProblemScript.Compile();
[11450]125      #endregion
126      #region Algorithm Configuration
[14475]127      ga.Name = "Genetic Algorithm - Graph Coloring";
128      ga.Description = "A genetic algorithm which solves a graph coloring problem using the linear linkage encoding.";
[12743]129      ga.Problem = problem;
130      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiLinearLinkageCrossover, MultiLinearLinkageManipulator>(
131        ga, 100, 1, 1000, 0.05, 2);
[11450]132      #endregion
[11514]133
[11450]134      return ga;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.