1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
24 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
25 | using HeuristicLab.Persistence.Default.Xml;
|
---|
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 | #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;
|
---|
45 |
|
---|
46 | namespace HeuristicLab.Problems.Programmable {
|
---|
47 | public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<LinearLinkageEncoding, LinearLinkage> {
|
---|
48 | private const int ProblemSize = 100;
|
---|
49 | public override bool Maximization { get { return false; } }
|
---|
50 |
|
---|
51 | private bool[,] allowedTogether;
|
---|
52 |
|
---|
53 | public override void Initialize() {
|
---|
54 | Encoding.Length = ProblemSize;
|
---|
55 | allowedTogether = new bool[ProblemSize, ProblemSize];
|
---|
56 | var random = new System.Random(13);
|
---|
57 | for (var i = 0; i < ProblemSize - 1; i++)
|
---|
58 | for (var j = i + 1; j < ProblemSize; j++)
|
---|
59 | allowedTogether[i, j] = allowedTogether[j, i] = random.Next(2) == 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override double Evaluate(LinearLinkage solution, IRandom random) {
|
---|
63 | var penalty = 0;
|
---|
64 | var groups = solution.GetGroups().ToList();
|
---|
65 | for (var i = 0; i < groups.Count; i++) {
|
---|
66 | for (var j = 0; j < groups[i].Count; j++)
|
---|
67 | for (var k = j + 1; k < groups[i].Count; k++)
|
---|
68 | if (!allowedTogether[groups[i][j], groups[i][k]]) penalty++;
|
---|
69 | }
|
---|
70 | if (penalty > 0) return penalty + ProblemSize;
|
---|
71 | else return groups.Count;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override void Analyze(LinearLinkage[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
75 |
|
---|
76 | public override IEnumerable<LinearLinkage> GetNeighbors(LinearLinkage solution, IRandom random) {
|
---|
77 | foreach (var move in ExhaustiveSwap2MoveGenerator.Generate(solution)) {
|
---|
78 | var neighbor = (LinearLinkage)solution.Clone();
|
---|
79 | Swap2MoveMaker.Apply(neighbor, move);
|
---|
80 | yield return neighbor;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | ";
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | [TestMethod]
|
---|
89 | [TestCategory("Samples.Create")]
|
---|
90 | [TestProperty("Time", "medium")]
|
---|
91 | public void CreateGaGroupingProblemSampleTest() {
|
---|
92 | var ga = CreateGaGroupingProblemSample();
|
---|
93 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
94 | XmlGenerator.Serialize(ga, path);
|
---|
95 | }
|
---|
96 |
|
---|
97 | [TestMethod]
|
---|
98 | [TestCategory("Samples.Execute")]
|
---|
99 | [TestProperty("Time", "long")]
|
---|
100 | public void RunGaGroupingProblemSampleTest() {
|
---|
101 | var ga = CreateGaGroupingProblemSample();
|
---|
102 | ga.SetSeedRandomly.Value = false;
|
---|
103 | SamplesUtils.RunAlgorithm(ga);
|
---|
104 | Assert.AreEqual(26, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
|
---|
105 | Assert.AreEqual(27.58, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
106 | Assert.AreEqual(105, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
107 | Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
108 | }
|
---|
109 |
|
---|
110 | private GeneticAlgorithm CreateGaGroupingProblemSample() {
|
---|
111 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
112 |
|
---|
113 | #region Problem Configuration
|
---|
114 | var problem = new SingleObjectiveLinearLinkageProgrammableProblem() {
|
---|
115 | ProblemScript = { Code = ProblemCode }
|
---|
116 | };
|
---|
117 | problem.ProblemScript.Compile();
|
---|
118 | #endregion
|
---|
119 | #region Algorithm Configuration
|
---|
120 | ga.Name = "Genetic Algorithm - Grouping Problem";
|
---|
121 | ga.Description = "A genetic algorithm which solves a grouping problem using the linear linkage encoding.";
|
---|
122 | ga.Problem = problem;
|
---|
123 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiLinearLinkageCrossover, MultiLinearLinkageManipulator>(
|
---|
124 | ga, 100, 1, 1000, 0.05, 2);
|
---|
125 | #endregion
|
---|
126 |
|
---|
127 | return ga;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|