1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
24 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
25 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
26 | using HeuristicLab.Problems.Programmable;
|
---|
27 | using HeuristicLab.Selection;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Tests {
|
---|
31 | [TestClass]
|
---|
32 | public class GAGroupingProblemSampleTest {
|
---|
33 | private const string SampleFileName = "GA_Grouping";
|
---|
34 |
|
---|
35 | private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
36 |
|
---|
37 | #region Code
|
---|
38 | private const string ProblemCode = @"
|
---|
39 | using System;
|
---|
40 | using System.Linq;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using HeuristicLab.Common;
|
---|
43 | using HeuristicLab.Core;
|
---|
44 | using HeuristicLab.Data;
|
---|
45 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
46 | using HeuristicLab.Optimization;
|
---|
47 | using HeuristicLab.Problems.Programmable;
|
---|
48 |
|
---|
49 | namespace HeuristicLab.Problems.Programmable {
|
---|
50 | public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
|
---|
51 | private const int ProblemSize = 100;
|
---|
52 | public bool Maximization { get { return false; } }
|
---|
53 |
|
---|
54 | private bool[,] adjacencyMatrix;
|
---|
55 |
|
---|
56 | public override void Initialize() {
|
---|
57 | var encoding = new LinearLinkageEncoding(""lle"", length: ProblemSize);
|
---|
58 | adjacencyMatrix = new bool[encoding.Length, encoding.Length];
|
---|
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++)
|
---|
62 | adjacencyMatrix[i, j] = adjacencyMatrix[j, i] = random.Next(2) == 0;
|
---|
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++)
|
---|
73 | if (!adjacencyMatrix[groups[i][j], groups[i][k]]) penalty++;
|
---|
74 | }
|
---|
75 | var result = groups.Count;
|
---|
76 | if (penalty > 0) result += penalty + ProblemSize;
|
---|
77 | return result;
|
---|
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 |
|
---|
95 | [TestMethod]
|
---|
96 | [TestCategory("Samples.Create")]
|
---|
97 | [TestProperty("Time", "medium")]
|
---|
98 | public void CreateGaGroupingProblemSampleTest() {
|
---|
99 | var ga = CreateGaGroupingProblemSample();
|
---|
100 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
101 | serializer.Serialize(ga, path);
|
---|
102 | }
|
---|
103 |
|
---|
104 | [TestMethod]
|
---|
105 | [TestCategory("Samples.Execute")]
|
---|
106 | [TestProperty("Time", "long")]
|
---|
107 | public void RunGaGroupingProblemSampleTest() {
|
---|
108 | var ga = CreateGaGroupingProblemSample();
|
---|
109 | ga.SetSeedRandomly.Value = false;
|
---|
110 | SamplesUtils.RunAlgorithm(ga);
|
---|
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"));
|
---|
114 | Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
115 | }
|
---|
116 |
|
---|
117 | private GeneticAlgorithm CreateGaGroupingProblemSample() {
|
---|
118 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
119 |
|
---|
120 | #region Problem Configuration
|
---|
121 | var problem = new SingleObjectiveProgrammableProblem() {
|
---|
122 | ProblemScript = { Code = ProblemCode }
|
---|
123 | };
|
---|
124 | problem.ProblemScript.Compile();
|
---|
125 | #endregion
|
---|
126 | #region Algorithm Configuration
|
---|
127 | ga.Name = "Genetic Algorithm - Graph Coloring";
|
---|
128 | ga.Description = "A genetic algorithm which solves a graph coloring problem using the linear linkage encoding.";
|
---|
129 | ga.Problem = problem;
|
---|
130 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiLinearLinkageCrossover, MultiLinearLinkageManipulator>(
|
---|
131 | ga, 100, 1, 1000, 0.05, 2);
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | return ga;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|