- Timestamp:
- 07/11/15 20:27:00 (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GAGroupingProblemSampleTest.cs
r12731 r12743 21 21 22 22 using System.IO; 23 using System.Linq;24 23 using HeuristicLab.Algorithms.GeneticAlgorithm; 25 using HeuristicLab.Encodings. PermutationEncoding;24 using HeuristicLab.Encodings.LinearLinkageEncoding; 26 25 using HeuristicLab.Persistence.Default.Xml; 27 using HeuristicLab.Problems.Instances.TSPLIB; 28 using HeuristicLab.Problems.TravelingSalesman; 26 using HeuristicLab.Problems.Programmable; 29 27 using HeuristicLab.Selection; 30 28 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 32 30 namespace HeuristicLab.Tests { 33 31 [TestClass] 34 public class GATspSampleTest { 35 private const string SampleFileName = "GA_TSP"; 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 : 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 36 90 37 91 [TestMethod] 38 92 [TestCategory("Samples.Create")] 39 93 [TestProperty("Time", "medium")] 40 public void CreateGa TspSampleTest() {41 var ga = CreateGa TspSample();94 public void CreateGaGroupingProblemSampleTest() { 95 var ga = CreateGaGroupingProblemSample(); 42 96 string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension); 43 97 XmlGenerator.Serialize(ga, path); … … 47 101 [TestCategory("Samples.Execute")] 48 102 [TestProperty("Time", "long")] 49 public void RunGa TspSampleTest() {50 var ga = CreateGa TspSample();103 public void RunGaGroupingProblemSampleTest() { 104 var ga = CreateGaGroupingProblemSample(); 51 105 ga.SetSeedRandomly.Value = false; 52 106 SamplesUtils.RunAlgorithm(ga); 53 Assert.AreEqual( 12332, SamplesUtils.GetDoubleResult(ga, "BestQuality"));54 Assert.AreEqual( 13123.2, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));55 Assert.AreEqual(1 4538, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));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")); 56 110 Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions")); 57 111 } 58 112 59 private GeneticAlgorithm CreateGa TspSample() {113 private GeneticAlgorithm CreateGaGroupingProblemSample() { 60 114 GeneticAlgorithm ga = new GeneticAlgorithm(); 61 115 62 116 #region Problem Configuration 63 var provider = new TSPLIBTSPInstanceProvider(); 64 var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single(); 65 TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem(); 66 tspProblem.Load(provider.LoadData(instance)); 67 tspProblem.UseDistanceMatrix.Value = true; 117 var problem = new SingleObjectiveProgrammableProblem() { 118 ProblemScript = { Code = ProblemCode } 119 }; 120 problem.ProblemScript.Compile(); 68 121 #endregion 69 122 #region Algorithm Configuration 70 ga.Name = "Genetic Algorithm - TSP";71 ga.Description = "A genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";72 ga.Problem = tspProblem;73 SamplesUtils.ConfigureGeneticAlgorithmParameters< ProportionalSelector, OrderCrossover2, InversionManipulator>(74 ga, 100, 1, 1000, 0.05 );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); 75 128 #endregion 76 129
Note: See TracChangeset
for help on using the changeset viewer.