Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6439


Ignore:
Timestamp:
06/17/11 15:22:27 (13 years ago)
Author:
gkronber
Message:

#1553: implemented unit test to create and run the symbolic regression sample (towerData.txt)

Location:
trunk/sources
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/GeneticAlgorithm.cs

    r6436 r6439  
    6565      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
    6666    }
    67     private ConstrainedValueParameter<ISelector> SelectorParameter {
     67    public ConstrainedValueParameter<ISelector> SelectorParameter {
    6868      get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
    6969    }
    70     private ConstrainedValueParameter<ICrossover> CrossoverParameter {
     70    public ConstrainedValueParameter<ICrossover> CrossoverParameter {
    7171      get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
    7272    }
     
    7474      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
    7575    }
    76     private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
     76    public OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
    7777      get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
    7878    }
     
    105105      set { SelectorParameter.Value = value; }
    106106    }
    107     public IEnumerable<ISelector> ValidSelectors {
    108       get { return SelectorParameter.ValidValues; }
    109     }
    110107    public ICrossover Crossover {
    111108      get { return CrossoverParameter.Value; }
     
    119116      get { return MutatorParameter.Value; }
    120117      set { MutatorParameter.Value = value; }
    121     }
    122     public IEnumerable<IManipulator> ValidMutators {
    123       get { return MutatorParameter.ValidValues; }
    124118    }
    125119    public IntValue Elites {
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/FullTreeShaker.cs

    r5809 r6439  
    3232    private const string ShakingFactorParameterName = "ShakingFactor";
    3333    #region parameter properties
    34     public IValueLookupParameter<DoubleValue> ShakingFactorParameter {
    35       get { return (IValueLookupParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; }
     34    public IValueParameter<DoubleValue> ShakingFactorParameter {
     35      get { return (IValueParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; }
    3636    }
    3737    #endregion
    3838    #region properties
    39     public DoubleValue ShakingFactor {
    40       get { return ShakingFactorParameter.ActualValue; }
     39    public double ShakingFactor {
     40      get { return ShakingFactorParameter.Value.Value; }
     41      set { ShakingFactorParameter.Value.Value = value; }
    4142    }
    4243    #endregion
    43 
    4444    [StorableConstructor]
    4545    private FullTreeShaker(bool deserializing) : base(deserializing) { }
     
    4747    public FullTreeShaker()
    4848      : base() {
    49       Parameters.Add(new ValueLookupParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0)));
     49      Parameters.Add(new FixedValueParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0)));
    5050    }
    5151
     
    5757      tree.Root.ForEachNodePostfix(node => {
    5858        if (node.HasLocalParameters) {
    59           node.ShakeLocalParameters(random, ShakingFactor.Value);
     59          node.ShakeLocalParameters(random, ShakingFactor);
    6060        }
    6161      });
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs

    r5809 r6439  
    3434    private const string ShakingFactorParameterName = "ShakingFactor";
    3535    #region parameter properties
    36     public IValueLookupParameter<DoubleValue> ShakingFactorParameter {
    37       get { return (IValueLookupParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; }
     36    public IValueParameter<DoubleValue> ShakingFactorParameter {
     37      get { return (IValueParameter<DoubleValue>)Parameters[ShakingFactorParameterName]; }
    3838    }
    3939    #endregion
    4040    #region properties
    41     public DoubleValue ShakingFactor {
    42       get { return ShakingFactorParameter.ActualValue; }
     41    public double ShakingFactor {
     42      get { return ShakingFactorParameter.Value.Value; }
     43      set { ShakingFactorParameter.Value.Value = value; }
    4344    }
    4445    #endregion
     
    4849    public OnePointShaker()
    4950      : base() {
    50       Parameters.Add(new ValueLookupParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0)));
     51      Parameters.Add(new FixedValueParameter<DoubleValue>(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0)));
    5152    }
    5253
     
    6263      if (parametricNodes.Count > 0) {
    6364        var selectedPoint = parametricNodes.SelectRandom(random);
    64         selectedPoint.ShakeLocalParameters(random, ShakingFactor.Value);
     65        selectedPoint.ShakeLocalParameters(random, ShakingFactor);
    6566      }
    6667    }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r6238 r6439  
    7777    #endregion
    7878
    79     public IValueParameter<StringValue> TargetVariableParameter {
    80       get { return (IValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
     79    public ConstrainedValueParameter<StringValue> TargetVariableParameter {
     80      get { return (ConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
    8181    }
    8282    public string TargetVariable {
  • trunk/sources/HeuristicLab/3.3/Tests/GeneticProgrammingSamplesTest.cs

    r6436 r6439  
    1313using System.Threading;
    1414using HeuristicLab.ParallelEngine;
     15using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
     16using HeuristicLab.Problems.DataAnalysis;
     17using HeuristicLab.Problems.DataAnalysis.Symbolic;
     18using System.IO;
    1519
    1620namespace HeuristicLab_33.Tests {
     
    1822  public class GeneticProgrammingSamplesTest {
    1923    [TestMethod]
    20     public void ArtificialAntProblemTest() {
     24    public void CreateArtificialAntSample() {
    2125      GeneticAlgorithm ga = new GeneticAlgorithm();
    22       #region algorithm configuration
    23       ga.Name = "Genetic Programming - Artificial Ant";
    24       ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
     26      #region problem configuration
    2527      ArtificialAntProblem antProblem = new ArtificialAntProblem();
    26       ga.Problem = antProblem;
    27       ga.PopulationSize.Value = 1000;
    28       ga.MaximumGenerations.Value = 100;
    29       ga.MutationProbability.Value = 0.15;
    30       ga.Elites.Value = 1;
    31       var tSelector = (TournamentSelector)ga.ValidSelectors
    32         .Single(s => s.Name == "TournamentSelector");
    33       tSelector.GroupSizeParameter.Value = new IntValue(5);
    34       ga.Selector = tSelector;
    35       var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.ValidMutators
    36         .Single(m => m.Name == "MultiSymbolicExpressionTreeArchitectureManipulator");
    37       mutator.Operators.SetItemCheckedState(mutator.Operators.Single(x => x.Name == "FullTreeShaker"), false);
    38       mutator.Operators.SetItemCheckedState(mutator.Operators.Single(x => x.Name == "OnePointShaker"), false);
    39       ga.Mutator = mutator;
    40 
    41       ga.SetSeedRandomly.Value = false;
    42       ga.Seed.Value = 0;
    43       ga.Engine = new ParallelEngine();
    44       #endregion
    45       #region problem configuration
    4628      antProblem.BestKnownQuality.Value = 89;
    4729      antProblem.MaxExpressionDepth.Value = 10;
     
    5133      antProblem.MaxTimeSteps.Value = 600;
    5234      #endregion
     35      #region algorithm configuration
     36      ga.Name = "Genetic Programming - Artificial Ant";
     37      ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
     38      ga.Problem = antProblem;
     39      ga.PopulationSize.Value = 1000;
     40      ga.MaximumGenerations.Value = 100;
     41      ga.MutationProbability.Value = 0.15;
     42      ga.Elites.Value = 1;
     43      var tSelector = ga.SelectorParameter.ValidValues
     44        .OfType<TournamentSelector>()
     45        .Single();
     46      tSelector.GroupSizeParameter.Value = new IntValue(5);
     47      ga.Selector = tSelector;
     48      var mutator = ga.MutatorParameter.ValidValues
     49        .OfType<MultiSymbolicExpressionTreeArchitectureManipulator>()
     50        .Single();
     51      mutator.Operators.SetItemCheckedState(mutator.Operators
     52        .OfType<FullTreeShaker>()
     53        .Single(), false);
     54      mutator.Operators.SetItemCheckedState(mutator.Operators
     55        .OfType<OnePointShaker>()
     56        .Single(), false);
     57      ga.Mutator = mutator;
     58
     59      ga.SetSeedRandomly.Value = false;
     60      ga.Seed.Value = 0;
     61      ga.Engine = new ParallelEngine();
     62      #endregion
    5363
    5464      XmlGenerator.Serialize(ga, "../../SGP_SantaFe.hl");
     
    5666      RunAlgorithm(ga);
    5767    }
     68
     69    [TestMethod]
     70    public void CreateSymbolicRegressionSample() {
     71      GeneticAlgorithm ga = new GeneticAlgorithm();
     72      #region problem configuration
     73      SymbolicRegressionSingleObjectiveProblem symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
     74      // import and configure problem data
     75      string filename = Path.GetTempFileName();
     76      using (var writer = File.CreateText(filename)) {
     77        writer.Write(HeuristicLab_33.Tests.Properties.Resources.TowerData);
     78      }
     79      var towerProblemData = RegressionProblemData.ImportFromFile(filename);
     80      towerProblemData.TargetVariableParameter.Value = towerProblemData.TargetVariableParameter.ValidValues
     81        .First(v => v.Value == "towerResponse");
     82      towerProblemData.InputVariables.SetItemCheckedState(
     83        towerProblemData.InputVariables.Single(x => x.Value == "x1"), true);
     84      towerProblemData.InputVariables.SetItemCheckedState(
     85        towerProblemData.InputVariables.Single(x => x.Value == "x7"), false);
     86      towerProblemData.InputVariables.SetItemCheckedState(
     87        towerProblemData.InputVariables.Single(x => x.Value == "x11"), false);
     88      towerProblemData.InputVariables.SetItemCheckedState(
     89        towerProblemData.InputVariables.Single(x => x.Value == "x16"), false);
     90      towerProblemData.InputVariables.SetItemCheckedState(
     91        towerProblemData.InputVariables.Single(x => x.Value == "x21"), false);
     92      towerProblemData.InputVariables.SetItemCheckedState(
     93        towerProblemData.InputVariables.Single(x => x.Value == "x25"), false);
     94      towerProblemData.InputVariables.SetItemCheckedState(
     95        towerProblemData.InputVariables.Single(x => x.Value == "towerResponse"), false);
     96      towerProblemData.TrainingPartition.Start = 0;
     97      towerProblemData.TrainingPartition.End = 4000;
     98      towerProblemData.TestPartition.Start = 4000;
     99      towerProblemData.TestPartition.End = 4999;
     100      towerProblemData.Name = "Data imported from towerData.txt";
     101      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";
     102      symbRegProblem.ProblemData = towerProblemData;
     103
     104      // configure grammar
     105      var grammar = new TypeCoherentExpressionGrammar();
     106      grammar.Symbols.OfType<Sine>().Single().InitialFrequency = 0.0;
     107      grammar.Symbols.OfType<Cosine>().Single().InitialFrequency = 0.0;
     108      grammar.Symbols.OfType<Tangent>().Single().InitialFrequency = 0.0;
     109      grammar.Symbols.OfType<IfThenElse>().Single().InitialFrequency = 0.0;
     110      grammar.Symbols.OfType<GreaterThan>().Single().InitialFrequency = 0.0;
     111      grammar.Symbols.OfType<LessThan>().Single().InitialFrequency = 0.0;
     112      grammar.Symbols.OfType<And>().Single().InitialFrequency = 0.0;
     113      grammar.Symbols.OfType<Or>().Single().InitialFrequency = 0.0;
     114      grammar.Symbols.OfType<Not>().Single().InitialFrequency = 0.0;
     115      grammar.Symbols.OfType<TimeLag>().Single().InitialFrequency = 0.0;
     116      grammar.Symbols.OfType<Integral>().Single().InitialFrequency = 0.0;
     117      grammar.Symbols.OfType<Derivative>().Single().InitialFrequency = 0.0;
     118      grammar.Symbols.OfType<LaggedVariable>().Single().InitialFrequency = 0.0;
     119      grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
     120      var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
     121      varSymbol.WeightMu = 1.0;
     122      varSymbol.WeightSigma = 1.0;
     123      varSymbol.WeightManipulatorMu = 0.0;
     124      varSymbol.WeightManipulatorSigma = 0.05;
     125      varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
     126      var constSymbol = grammar.Symbols.OfType<Constant>().Single();
     127      constSymbol.MaxValue = 20;
     128      constSymbol.MinValue = -20;
     129      constSymbol.ManipulatorMu = 0.0;
     130      constSymbol.ManipulatorSigma = 1;
     131      constSymbol.MultiplicativeManipulatorSigma = 0.03;
     132      symbRegProblem.SymbolicExpressionTreeGrammar = grammar;
     133
     134      // configure remaining problem parameters
     135      symbRegProblem.BestKnownQuality.Value = 0.97;
     136      symbRegProblem.FitnessCalculationPartition.Start = 0;
     137      symbRegProblem.FitnessCalculationPartition.End = 2800;
     138      symbRegProblem.ValidationPartition.Start = 2800;
     139      symbRegProblem.ValidationPartition.End = 4000;
     140      symbRegProblem.RelativeNumberOfEvaluatedSamples.Value = 0.3;
     141      symbRegProblem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator();
     142      #endregion
     143      #region algorithm configuration
     144      ga.Problem = symbRegProblem;
     145      ga.Name = "Genetic Programming - Symbolic Regression";
     146      ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
     147      ga.Elites.Value = 1;
     148      ga.MaximumGenerations.Value = 100;
     149      ga.MutationProbability.Value = 0.15;
     150      ga.PopulationSize.Value = 1000;
     151      ga.Seed.Value = 0;
     152      ga.SetSeedRandomly.Value = false;
     153      var tSelector = ga.SelectorParameter.ValidValues
     154        .OfType<TournamentSelector>()
     155        .Single();
     156      tSelector.GroupSizeParameter.Value.Value = 5;
     157      ga.Selector = tSelector;
     158      var mutator = ga.MutatorParameter.ValidValues
     159        .OfType<MultiSymbolicExpressionTreeManipulator>()
     160        .Single();
     161      mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
     162      mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
     163      ga.Mutator = mutator;
     164
     165      ga.Analyzer.Operators.SetItemCheckedState(
     166        ga.Analyzer.Operators
     167        .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
     168        .Single(), false);
     169      ga.Engine = new ParallelEngine();
     170      #endregion
     171
     172      XmlGenerator.Serialize(ga, "../../SGP_SymbReg.hl");
     173
     174      RunAlgorithm(ga);
     175    }
     176
    58177
    59178    private void RunAlgorithm(IAlgorithm a) {
  • trunk/sources/HeuristicLab/3.3/Tests/HeuristicLab-3.3.Tests.csproj

    r6436 r6439  
    127127    <Compile Include="ContentViewTests.cs" />
    128128    <Compile Include="GeneticProgrammingSamplesTest.cs" />
     129    <Compile Include="Properties\Resources.Designer.cs">
     130      <AutoGen>True</AutoGen>
     131      <DesignTime>True</DesignTime>
     132      <DependentUpon>Resources.resx</DependentUpon>
     133    </Compile>
    129134    <Compile Include="SymbolicRegressionTest.cs" />
    130135    <Compile Include="PluginDependenciesTest.cs" />
     
    442447      <Project>{3E9E8944-44FF-40BB-A622-3A4A7DD0F198}</Project>
    443448      <Name>HeuristicLab.Problems.DataAnalysis.Views-3.4</Name>
    444     </ProjectReference>
    445     <ProjectReference Include="..\..\..\HeuristicLab.Problems.DataAnalysis\3.3\HeuristicLab.Problems.DataAnalysis-3.3.csproj">
    446       <Project>{70DFD984-B1D9-46FE-8EB7-4DE92D71A9FC}</Project>
    447       <Name>HeuristicLab.Problems.DataAnalysis-3.3</Name>
    448449    </ProjectReference>
    449450    <ProjectReference Include="..\..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
     
    554555    </None>
    555556  </ItemGroup>
     557  <ItemGroup>
     558    <Content Include="towerData.txt">
     559      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     560    </Content>
     561  </ItemGroup>
     562  <ItemGroup>
     563    <EmbeddedResource Include="Properties\Resources.resx">
     564      <Generator>ResXFileCodeGenerator</Generator>
     565      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
     566    </EmbeddedResource>
     567  </ItemGroup>
    556568  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    557569  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Note: See TracChangeset for help on using the changeset viewer.