Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAGroupingProblemSampleTest.cs

Last change on this file was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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