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