Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/GeneticAlgorithmSamplesTest.cs @ 6451

Last change on this file since 6451 was 6442, checked in by gkronber, 14 years ago

#1553: disabled SubroutineDeleter and ArgumentDeleter operators in the artificial ant sample.

File size: 20.2 KB
Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using HeuristicLab.Algorithms.GeneticAlgorithm;
7using HeuristicLab.Problems.ArtificialAnt;
8using HeuristicLab.Selection;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
11using HeuristicLab.Persistence.Default.Xml;
12using HeuristicLab.Optimization;
13using System.Threading;
14using HeuristicLab.ParallelEngine;
15using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
16using HeuristicLab.Problems.DataAnalysis;
17using HeuristicLab.Problems.DataAnalysis.Symbolic;
18using System.IO;
19using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
20using HeuristicLab.Problems.TravelingSalesman;
21using HeuristicLab.Encodings.PermutationEncoding;
22using HeuristicLab.Problems.VehicleRouting;
23using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
24using HeuristicLab.Problems.VehicleRouting.Encodings;
25using HeuristicLab.Problems.VehicleRouting.Encodings.General;
26
27namespace HeuristicLab_33.Tests {
28  [TestClass]
29  public class GeneticAlgorithmSamplesTest {
30    [TestMethod]
31    public void CreateTSPSample() {
32      GeneticAlgorithm ga = new GeneticAlgorithm();
33      #region problem configuration
34      TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
35      // import and configure TSP data
36      string ch130FileName = Path.GetTempFileName() + ".tsp";// for silly parser constraints
37      using (var writer = File.CreateText(ch130FileName)) {
38        writer.Write(HeuristicLab_33.Tests.Properties.Resources.ch130);
39      }
40      string ch130OptTourFileName = Path.GetTempFileName() + ".opt.tour"; // for silly parser constraints
41      using (var writer = File.CreateText(ch130OptTourFileName)) {
42        writer.Write(HeuristicLab_33.Tests.Properties.Resources.ch130_opt);
43      }
44
45      tspProblem.ImportFromTSPLIB(ch130FileName, ch130OptTourFileName, 6110);
46      tspProblem.Evaluator = new TSPRoundedEuclideanPathEvaluator();
47      tspProblem.SolutionCreator = new RandomPermutationCreator();
48      tspProblem.UseDistanceMatrix.Value = true;
49      tspProblem.Name = "ch130 TSP (imported from TSPLIB)";
50      tspProblem.Description = "130 city problem (Churritz)";
51      #endregion
52      #region algorithm configuration
53      ga.Name = "Genetic Algorithm - TSP";
54      ga.Description = "A genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
55      ga.Problem = tspProblem;
56      ConfigureGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator>(
57        ga, 100, 1, 1000, 0.05);
58
59      ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
60        .OfType<TSPAlleleFrequencyAnalyzer>()
61        .Single(), false);
62      ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
63        .OfType<TSPPopulationDiversityAnalyzer>()
64        .Single(), false);
65      #endregion
66
67      XmlGenerator.Serialize(ga, "../../GA_TSP.hl");
68
69      RunAlgorithm(ga);
70    }
71    [TestMethod]
72    public void CreateVRPSample() {
73      GeneticAlgorithm ga = new GeneticAlgorithm();
74      #region problem configuration
75      VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
76      // import and configure VRP data
77      string c101FileName = Path.GetTempFileName();
78      using (var writer = File.CreateText(c101FileName)) {
79        writer.Write(HeuristicLab_33.Tests.Properties.Resources.C101);
80      }
81      // import and configure VRP data
82      string c101BestSolutionFileName = Path.GetTempFileName();
83      using (var writer = File.CreateText(c101BestSolutionFileName)) {
84        writer.Write(HeuristicLab_33.Tests.Properties.Resources.C101_opt);
85      }
86
87      vrpProblem.ImportFromSolomon(c101FileName);
88      vrpProblem.ImportSolution(c101BestSolutionFileName);
89      vrpProblem.Name = "C101 VRP (imported from Solomon)";
90      vrpProblem.Description = "Represents a Vehicle Routing Problem.";
91      vrpProblem.DistanceFactorParameter.Value.Value = 1;
92      vrpProblem.FleetUsageFactorParameter.Value.Value = 100;
93      vrpProblem.OverloadPenaltyParameter.Value.Value = 100;
94      vrpProblem.TardinessPenaltyParameter.Value.Value = 100;
95      vrpProblem.TimeFactorParameter.Value.Value = 0;
96      vrpProblem.Evaluator = new VRPEvaluator();
97      vrpProblem.MaximizationParameter.Value.Value = false;
98      vrpProblem.SolutionCreator = new RandomCreator();
99      vrpProblem.UseDistanceMatrix.Value = true;
100      vrpProblem.Vehicles.Value = 25;
101      #endregion
102      #region algorithm configuration
103      ga.Name = "Genetic Algorithm - VRP";
104      ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
105      ga.Problem = vrpProblem;
106      ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
107        ga, 100, 1, 1000, 0.05, 3);
108
109      var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
110      foreach (var op in xOver.Operators) {
111        xOver.Operators.SetItemCheckedState(op, false);
112      }
113      xOver.Operators.SetItemCheckedState(xOver.Operators
114        .OfType<PotvinRouteBasedCrossover>()
115        .Single(), true);
116      xOver.Operators.SetItemCheckedState(xOver.Operators
117        .OfType<PotvinSequenceBasedCrossover>()
118        .Single(), true);
119
120      var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
121      foreach (var op in manipulator.Operators) {
122        manipulator.Operators.SetItemCheckedState(op, false);
123      }
124      manipulator.Operators.SetItemCheckedState(manipulator.Operators
125        .OfType<PotvinOneLevelExchangeMainpulator>()
126        .Single(), true);
127      manipulator.Operators.SetItemCheckedState(manipulator.Operators
128        .OfType<PotvinTwoLevelExchangeManipulator>()
129        .Single(), true);
130      #endregion
131
132      XmlGenerator.Serialize(ga, "../../GA_VRP.hl");
133
134      RunAlgorithm(ga);
135    }
136
137    [TestMethod]
138    public void CreateArtificialAntSample() {
139      GeneticAlgorithm ga = new GeneticAlgorithm();
140      #region problem configuration
141      ArtificialAntProblem antProblem = new ArtificialAntProblem();
142      antProblem.BestKnownQuality.Value = 89;
143      antProblem.MaxExpressionDepth.Value = 10;
144      antProblem.MaxExpressionLength.Value = 100;
145      antProblem.MaxFunctionArguments.Value = 3;
146      antProblem.MaxFunctionDefinitions.Value = 3;
147      antProblem.MaxTimeSteps.Value = 600;
148      #endregion
149      #region algorithm configuration
150      ga.Name = "Genetic Programming - Artificial Ant";
151      ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
152      ga.Problem = antProblem;
153      ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
154        ga, 1000, 1, 100, 0.15, 5);
155      var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
156      mutator.Operators.SetItemCheckedState(mutator.Operators
157        .OfType<FullTreeShaker>()
158        .Single(), false);
159      mutator.Operators.SetItemCheckedState(mutator.Operators
160        .OfType<OnePointShaker>()
161        .Single(), false);
162      mutator.Operators.SetItemCheckedState(mutator.Operators
163        .OfType<ArgumentDeleter>()
164        .Single(), false);
165      mutator.Operators.SetItemCheckedState(mutator.Operators
166        .OfType<SubroutineDeleter>()
167        .Single(), false);
168      #endregion
169
170      XmlGenerator.Serialize(ga, "../../SGP_SantaFe.hl");
171
172      RunAlgorithm(ga);
173    }
174
175    [TestMethod]
176    public void CreateSymbolicRegressionSample() {
177      GeneticAlgorithm ga = new GeneticAlgorithm();
178      #region problem configuration
179      SymbolicRegressionSingleObjectiveProblem symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
180      symbRegProblem.Name = "Tower Symbolic Regression Problem";
181      symbRegProblem.Description = "Tower Dataset (downloaded from: http://vanillamodeling.com/realproblems.html)";
182      // import and configure problem data
183      string filename = Path.GetTempFileName();
184      using (var writer = File.CreateText(filename)) {
185        writer.Write(HeuristicLab_33.Tests.Properties.Resources.TowerData);
186      }
187      var towerProblemData = RegressionProblemData.ImportFromFile(filename);
188      towerProblemData.TargetVariableParameter.Value = towerProblemData.TargetVariableParameter.ValidValues
189        .First(v => v.Value == "towerResponse");
190      towerProblemData.InputVariables.SetItemCheckedState(
191        towerProblemData.InputVariables.Single(x => x.Value == "x1"), true);
192      towerProblemData.InputVariables.SetItemCheckedState(
193        towerProblemData.InputVariables.Single(x => x.Value == "x7"), false);
194      towerProblemData.InputVariables.SetItemCheckedState(
195        towerProblemData.InputVariables.Single(x => x.Value == "x11"), false);
196      towerProblemData.InputVariables.SetItemCheckedState(
197        towerProblemData.InputVariables.Single(x => x.Value == "x16"), false);
198      towerProblemData.InputVariables.SetItemCheckedState(
199        towerProblemData.InputVariables.Single(x => x.Value == "x21"), false);
200      towerProblemData.InputVariables.SetItemCheckedState(
201        towerProblemData.InputVariables.Single(x => x.Value == "x25"), false);
202      towerProblemData.InputVariables.SetItemCheckedState(
203        towerProblemData.InputVariables.Single(x => x.Value == "towerResponse"), false);
204      towerProblemData.TrainingPartition.Start = 0;
205      towerProblemData.TrainingPartition.End = 4000;
206      towerProblemData.TestPartition.Start = 4000;
207      towerProblemData.TestPartition.End = 4999;
208      towerProblemData.Name = "Data imported from towerData.txt";
209      towerProblemData.Description = "Chemical concentration at top of distillation tower, dataset downloaded from: http://vanillamodeling.com/realproblems.html, best R² achieved with nu-SVR = 0.97";
210      symbRegProblem.ProblemData = towerProblemData;
211
212      // configure grammar
213      var grammar = new TypeCoherentExpressionGrammar();
214      grammar.Symbols.OfType<Sine>().Single().InitialFrequency = 0.0;
215      grammar.Symbols.OfType<Cosine>().Single().InitialFrequency = 0.0;
216      grammar.Symbols.OfType<Tangent>().Single().InitialFrequency = 0.0;
217      grammar.Symbols.OfType<IfThenElse>().Single().InitialFrequency = 0.0;
218      grammar.Symbols.OfType<GreaterThan>().Single().InitialFrequency = 0.0;
219      grammar.Symbols.OfType<LessThan>().Single().InitialFrequency = 0.0;
220      grammar.Symbols.OfType<And>().Single().InitialFrequency = 0.0;
221      grammar.Symbols.OfType<Or>().Single().InitialFrequency = 0.0;
222      grammar.Symbols.OfType<Not>().Single().InitialFrequency = 0.0;
223      grammar.Symbols.OfType<TimeLag>().Single().InitialFrequency = 0.0;
224      grammar.Symbols.OfType<Integral>().Single().InitialFrequency = 0.0;
225      grammar.Symbols.OfType<Derivative>().Single().InitialFrequency = 0.0;
226      grammar.Symbols.OfType<LaggedVariable>().Single().InitialFrequency = 0.0;
227      grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
228      var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
229      varSymbol.WeightMu = 1.0;
230      varSymbol.WeightSigma = 1.0;
231      varSymbol.WeightManipulatorMu = 0.0;
232      varSymbol.WeightManipulatorSigma = 0.05;
233      varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
234      var constSymbol = grammar.Symbols.OfType<Constant>().Single();
235      constSymbol.MaxValue = 20;
236      constSymbol.MinValue = -20;
237      constSymbol.ManipulatorMu = 0.0;
238      constSymbol.ManipulatorSigma = 1;
239      constSymbol.MultiplicativeManipulatorSigma = 0.03;
240      symbRegProblem.SymbolicExpressionTreeGrammar = grammar;
241
242      // configure remaining problem parameters
243      symbRegProblem.BestKnownQuality.Value = 0.97;
244      symbRegProblem.FitnessCalculationPartition.Start = 0;
245      symbRegProblem.FitnessCalculationPartition.End = 2800;
246      symbRegProblem.ValidationPartition.Start = 2800;
247      symbRegProblem.ValidationPartition.End = 4000;
248      symbRegProblem.RelativeNumberOfEvaluatedSamples.Value = 0.3;
249      symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 150;
250      symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 12;
251      symbRegProblem.MaximumFunctionDefinitions.Value = 0;
252      symbRegProblem.MaximumFunctionArguments.Value = 0;
253
254      symbRegProblem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator();
255      #endregion
256      #region algorithm configuration
257      ga.Problem = symbRegProblem;
258      ga.Name = "Genetic Programming - Symbolic Regression";
259      ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
260      ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
261        ga, 1000, 1, 100, 0.15, 5);
262      var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
263      mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
264      mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
265
266      ga.Analyzer.Operators.SetItemCheckedState(
267        ga.Analyzer.Operators
268        .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
269        .Single(), false);
270      #endregion
271
272      XmlGenerator.Serialize(ga, "../../SGP_SymbReg.hl");
273
274      RunAlgorithm(ga);
275    }
276
277    [TestMethod]
278    public void CreateSymbolicClassificationSample() {
279      GeneticAlgorithm ga = new GeneticAlgorithm();
280      #region problem configuration
281      SymbolicClassificationSingleObjectiveProblem symbClassProblem = new SymbolicClassificationSingleObjectiveProblem();
282      symbClassProblem.Name = "Mammography Classification Problem";
283      symbClassProblem.Description = "Mammography dataset imported from the UCI machine learning repository (http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass)";
284      // import and configure problem data
285      string filename = Path.GetTempFileName();
286      using (var writer = File.CreateText(filename)) {
287        writer.Write(HeuristicLab_33.Tests.Properties.Resources.MammographicMasses);
288      }
289      var mammoData = ClassificationProblemData.ImportFromFile(filename);
290      mammoData.TargetVariableParameter.Value = mammoData.TargetVariableParameter.ValidValues
291        .First(v => v.Value == "Severity");
292      mammoData.InputVariables.SetItemCheckedState(
293        mammoData.InputVariables.Single(x => x.Value == "BI-RADS"), false);
294      mammoData.InputVariables.SetItemCheckedState(
295        mammoData.InputVariables.Single(x => x.Value == "Age"), true);
296      mammoData.InputVariables.SetItemCheckedState(
297        mammoData.InputVariables.Single(x => x.Value == "Shape"), true);
298      mammoData.InputVariables.SetItemCheckedState(
299        mammoData.InputVariables.Single(x => x.Value == "Margin"), true);
300      mammoData.InputVariables.SetItemCheckedState(
301        mammoData.InputVariables.Single(x => x.Value == "Density"), true);
302      mammoData.InputVariables.SetItemCheckedState(
303        mammoData.InputVariables.Single(x => x.Value == "Severity"), false);
304      mammoData.TrainingPartition.Start = 0;
305      mammoData.TrainingPartition.End = 800;
306      mammoData.TestPartition.Start = 800;
307      mammoData.TestPartition.End = 961;
308      mammoData.Name = "Data imported from mammographic_masses.csv";
309      mammoData.Description = "Original dataset: http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass, missing values have been replaced with median values.";
310      symbClassProblem.ProblemData = mammoData;
311
312      // configure grammar
313      var grammar = new TypeCoherentExpressionGrammar();
314      grammar.Symbols.OfType<Sine>().Single().InitialFrequency = 0.0;
315      grammar.Symbols.OfType<Cosine>().Single().InitialFrequency = 0.0;
316      grammar.Symbols.OfType<Tangent>().Single().InitialFrequency = 0.0;
317      grammar.Symbols.OfType<Power>().Single().InitialFrequency = 0.0;
318      grammar.Symbols.OfType<Root>().Single().InitialFrequency = 0.0;
319      grammar.Symbols.OfType<TimeLag>().Single().InitialFrequency = 0.0;
320      grammar.Symbols.OfType<Integral>().Single().InitialFrequency = 0.0;
321      grammar.Symbols.OfType<Derivative>().Single().InitialFrequency = 0.0;
322      grammar.Symbols.OfType<LaggedVariable>().Single().InitialFrequency = 0.0;
323      grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
324      var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
325      varSymbol.WeightMu = 1.0;
326      varSymbol.WeightSigma = 1.0;
327      varSymbol.WeightManipulatorMu = 0.0;
328      varSymbol.WeightManipulatorSigma = 0.05;
329      varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
330      var constSymbol = grammar.Symbols.OfType<Constant>().Single();
331      constSymbol.MaxValue = 20;
332      constSymbol.MinValue = -20;
333      constSymbol.ManipulatorMu = 0.0;
334      constSymbol.ManipulatorSigma = 1;
335      constSymbol.MultiplicativeManipulatorSigma = 0.03;
336      symbClassProblem.SymbolicExpressionTreeGrammar = grammar;
337
338      // configure remaining problem parameters
339      symbClassProblem.BestKnownQuality.Value = 0.0;
340      symbClassProblem.FitnessCalculationPartition.Start = 0;
341      symbClassProblem.FitnessCalculationPartition.End = 400;
342      symbClassProblem.ValidationPartition.Start = 400;
343      symbClassProblem.ValidationPartition.End = 800;
344      symbClassProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
345      symbClassProblem.MaximumSymbolicExpressionTreeLength.Value = 100;
346      symbClassProblem.MaximumSymbolicExpressionTreeDepth.Value = 10;
347      symbClassProblem.MaximumFunctionDefinitions.Value = 0;
348      symbClassProblem.MaximumFunctionArguments.Value = 0;
349      symbClassProblem.EvaluatorParameter.Value = new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator();
350      #endregion
351      #region algorithm configuration
352      ga.Problem = symbClassProblem;
353      ga.Name = "Genetic Programming - Symbolic Classification";
354      ga.Description = "A standard genetic programming algorithm to solve a classification problem (Mammographic+Mass dataset)";
355      ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
356        ga, 1000, 1, 100, 0.15, 5
357        );
358
359      var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
360      mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
361      mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
362
363      ga.Analyzer.Operators.SetItemCheckedState(
364        ga.Analyzer.Operators
365        .OfType<SymbolicClassificationSingleObjectiveOverfittingAnalyzer>()
366        .Single(), false);
367      #endregion
368
369      XmlGenerator.Serialize(ga, "../../SGP_SymbClass.hl");
370
371      RunAlgorithm(ga);
372    }
373
374    private void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
375      where S : ISelector
376      where C : ICrossover
377      where M : IManipulator {
378      ga.Elites.Value = elites;
379      ga.MaximumGenerations.Value = maxGens;
380      ga.MutationProbability.Value = mutationRate;
381      ga.PopulationSize.Value = popSize;
382      ga.Seed.Value = 0;
383      ga.SetSeedRandomly.Value = true;
384      ga.Selector = ga.SelectorParameter.ValidValues
385        .OfType<S>()
386        .Single();
387
388      ga.Crossover = ga.CrossoverParameter.ValidValues
389        .OfType<C>()
390        .Single();
391
392      ga.Mutator = ga.MutatorParameter.ValidValues
393        .OfType<M>()
394        .Single();
395
396      var tSelector = ga.Selector as TournamentSelector;
397      if (tSelector != null) {
398        tSelector.GroupSizeParameter.Value.Value = 5;
399      }
400      ga.Engine = new ParallelEngine();
401    }
402
403
404    private void RunAlgorithm(IAlgorithm a) {
405      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
406      Exception ex = null;
407      a.Stopped += (src, e) => { trigger.Set(); };
408      a.ExceptionOccurred += (src, e) => { ex = e.Value; };
409      a.Prepare();
410      a.Start();
411      trigger.WaitOne();
412
413      Assert.AreEqual(ex, null);
414    }
415  }
416}
Note: See TracBrowser for help on using the repository browser.