Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAGroupingProblemSampleTest.cs @ 14821

Last change on this file since 14821 was 13385, checked in by abeham, 9 years ago

#2521: fixed plugin dependencies and updated samples

File size: 5.0 KB
RevLine 
[11450]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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;
23using HeuristicLab.Algorithms.GeneticAlgorithm;
[12743]24using HeuristicLab.Encodings.LinearLinkageEncoding;
[11450]25using HeuristicLab.Persistence.Default.Xml;
[12743]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";
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;
[11450]45
[12743]46namespace HeuristicLab.Problems.Programmable {
[13385]47  public class CompiledSingleObjectiveProblemDefinition : CompiledSingleObjectiveProblemDefinition<LinearLinkageEncoding, LinearLinkage> {
[12743]48    private const int ProblemSize = 100;
[13385]49    public override bool Maximization { get { return false; } }
[12743]50
51    private bool[,] allowedTogether;
[13385]52   
[12743]53    public override void Initialize() {
[13385]54      Encoding.Length = ProblemSize;
55      allowedTogether = new bool[ProblemSize, ProblemSize];
[12743]56      var random = new System.Random(13);
[13385]57      for (var i = 0; i < ProblemSize - 1; i++)
58        for (var j = i + 1; j < ProblemSize; j++)
[12743]59          allowedTogether[i, j] = allowedTogether[j, i] = random.Next(2) == 0;
60    }
61
[13385]62    public override double Evaluate(LinearLinkage solution, IRandom random) {
[12743]63      var penalty = 0;
[13385]64      var groups = solution.GetGroups().ToList();
[12743]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
[13385]74    public override void Analyze(LinearLinkage[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
[12743]75
[13385]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);
[12743]80        yield return neighbor;
81      }
82    }
83  }
84}
85";
86    #endregion
87
[11450]88    [TestMethod]
89    [TestCategory("Samples.Create")]
90    [TestProperty("Time", "medium")]
[12743]91    public void CreateGaGroupingProblemSampleTest() {
92      var ga = CreateGaGroupingProblemSample();
[11514]93      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
94      XmlGenerator.Serialize(ga, path);
[11450]95    }
[11514]96
[11450]97    [TestMethod]
98    [TestCategory("Samples.Execute")]
99    [TestProperty("Time", "long")]
[12743]100    public void RunGaGroupingProblemSampleTest() {
101      var ga = CreateGaGroupingProblemSample();
[11450]102      ga.SetSeedRandomly.Value = false;
103      SamplesUtils.RunAlgorithm(ga);
[12743]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"));
[11450]107      Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
108    }
109
[12743]110    private GeneticAlgorithm CreateGaGroupingProblemSample() {
[11450]111      GeneticAlgorithm ga = new GeneticAlgorithm();
[11514]112
[11450]113      #region Problem Configuration
[13385]114      var problem = new SingleObjectiveLinearLinkageProgrammableProblem() {
[12743]115        ProblemScript = { Code = ProblemCode }
116      };
117      problem.ProblemScript.Compile();
[11450]118      #endregion
119      #region Algorithm Configuration
[12743]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);
[11450]125      #endregion
[11514]126
[11450]127      return ga;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.