Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9077


Ignore:
Timestamp:
12/19/12 10:33:27 (12 years ago)
Author:
mkommend
Message:

#1997: Added first working version of SymbolicDataAnalysisIslandGA.

Location:
branches/DataAnalysis.IslandAlgorithms
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/HeuristicLab.Algorithms.DataAnalysis.Symbolic.csproj

    r9067 r9077  
    109109  </ItemGroup>
    110110  <ItemGroup>
     111    <Compile Include="ISymbolicDataAnalysisIslandGAEvaluator .cs" />
     112    <Compile Include="ScopeTreeAssigner.cs" />
    111113    <Compile Include="Plugin.cs" />
    112114    <Compile Include="Properties\AssemblyInfo.cs" />
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGAEvaluator.cs

    r9051 r9077  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2527using HeuristicLab.Operators;
    2628using HeuristicLab.Optimization;
     29using HeuristicLab.Parameters;
    2730using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2831using HeuristicLab.Problems.DataAnalysis;
    2932using HeuristicLab.Problems.DataAnalysis.Symbolic;
     33using HeuristicLab.Random;
    3034
    3135namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
    3236  [StorableClass]
    33   public sealed class SymbolicDataAnalysisIslandGAEvaluator<T> : SingleSuccessorOperator, IStochasticOperator
     37  public sealed class SymbolicDataAnalysisIslandGAEvaluator<T> : SingleSuccessorOperator, IStochasticOperator, ISymbolicDataAnalysisIslandGAEvaluator
    3438    where T : class,IDataAnalysisProblemData {
    3539    private const string RandomParameterName = "Random";
    36     private const string EvaluatorParameterName = "Evaluator";
    37     private const string IslandNumberParameterName = "IslandNumber";
    38     private const string NumberOfIslandsParameterName = "NumberOfIslands";
    39     private const string ConstantSamplesParameterName = "ConstantSamples";
    40     private const string ConstantSamplesFitnessWeightFactorParameterName = "ConstantSamplesFitnessWeight";
     40    private const string ProblemDataParameterName = "ProblemData";
     41    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
     42    private const string EvaluatorParameterName = "ProblemEvaluator";
     43    private const string QualityParameterName = "Quality";
     44    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
     45    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
     46    private const string FixedSamplesParameterName = "FixedSamples";
     47    private const string FixedSamplesFitnessWeightParameterName = "FixedSamplesFitnessWeight";
    4148    private const string RandomSamplesParameterName = "RandomSamples";
    4249    private const string RandomSamplesFitnessWeightParameterName = "RandomSamplesFitnessWeight";
    43     private const string RandomSamplesConsecutiveParameterName = "RandomSamplesConsecutive";
    4450
    4551    #region parameter properties
     
    4753      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
    4854    }
    49     public IValueParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
    50       get { return (IValueParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
     55    public ILookupParameter<T> ProblemDataParameter {
     56      get { return (ILookupParameter<T>)Parameters[ProblemDataParameterName]; }
    5157    }
    52 
    53     public ILookupParameter<IntValue> NumberOfIslandParameter {
    54       get { return (ILookupParameter<IntValue>)Parameters[NumberOfIslandsParameterName]; }
     58    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     59      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    5560    }
    56     public IFixedValueParameter<IntValue> ConstantSamplesParameter {
    57       get { return (IFixedValueParameter<IntValue>)Parameters[ConstantSamplesParameterName]; }
     61    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
     62      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
    5863    }
    59     public IFixedValueParameter<DoubleValue> ConstantSamplesFitnessWeightParameter {
    60       get { return (IFixedValueParameter<DoubleValue>)Parameters[ConstantSamplesFitnessWeightFactorParameterName]; }
     64    public ILookupParameter<DoubleValue> QualityParameter {
     65      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
    6166    }
    62     public IFixedValueParameter<IntValue> RandomSamplesParameter {
    63       get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
     67    public IValueLookupParameter<IntRange> FitnessCalculationPartitionParameter {
     68      get { return (IValueLookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
     69    }
     70    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
     71      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
     72    }
     73    public ILookupParameter<IntValue> FixedSamplesParameter {
     74      get { return (ILookupParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
     75    }
     76    public IFixedValueParameter<DoubleValue> FixedSamplesFitnessWeightParameter {
     77      get { return (IFixedValueParameter<DoubleValue>)Parameters[FixedSamplesFitnessWeightParameterName]; }
     78    }
     79    public ILookupParameter<IntValue> RandomSamplesParameter {
     80      get { return (ILookupParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
    6481    }
    6582    public IFixedValueParameter<DoubleValue> RandomSamplesFitnessWeightParameter {
    6683      get { return (IFixedValueParameter<DoubleValue>)Parameters[RandomSamplesFitnessWeightParameterName]; }
    6784    }
    68     public IFixedValueParameter<BoolValue> RandomSamplesConsecutiveParameter {
    69       get { return (IFixedValueParameter<BoolValue>)Parameters[RandomSamplesConsecutiveParameterName]; }
    70     }
    7185    #endregion
    7286
    7387    #region properties
    74     public ISymbolicDataAnalysisSingleObjectiveEvaluator<T> Evaluator {
    75       get { return EvaluatorParameter.Value; }
    76       set { EvaluatorParameter.Value = value; }
    77     }
    78     public int ConstantSamples {
    79       get { return ConstantSamplesParameter.Value.Value; }
    80       set { ConstantSamplesParameter.Value.Value = value; }
    81     }
    82     public double ConstantSamplesFitnessWeight {
    83       get { return ConstantSamplesFitnessWeightParameter.Value.Value; }
    84       set { ConstantSamplesFitnessWeightParameter.Value.Value = value; }
    85     }
    86     public int RandomSamples {
    87       get { return RandomSamplesParameter.Value.Value; }
    88       set { RandomSamplesParameter.Value.Value = value; }
     88    public double FixedSamplesFitnessWeight {
     89      get { return FixedSamplesFitnessWeightParameter.Value.Value; }
     90      set { FixedSamplesFitnessWeightParameter.Value.Value = value; }
    8991    }
    9092    public double RandomSamplesFitnessWeight {
    9193      get { return RandomSamplesFitnessWeightParameter.Value.Value; }
    9294      set { RandomSamplesFitnessWeightParameter.Value.Value = value; }
    93     }
    94     public bool RandomSamplesConsecutive {
    95       get { return RandomSamplesConsecutiveParameter.Value.Value; }
    96       set { RandomSamplesConsecutiveParameter.Value.Value = value; }
    9795    }
    9896    #endregion
     
    109107    public SymbolicDataAnalysisIslandGAEvaluator()
    110108      : base() {
     109      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
     110      Parameters.Add(new LookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
     111      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
     112      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
     113      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
     114      Parameters.Add(new ValueLookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
     115      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
     116      Parameters.Add(new LookupParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island."));
     117      Parameters.Add(new FixedValueParameter<DoubleValue>(FixedSamplesFitnessWeightParameterName, "The weight of the fitness obtained on the fixed samples.", new DoubleValue(1)));
     118      Parameters.Add(new LookupParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island."));
     119      Parameters.Add(new FixedValueParameter<DoubleValue>(RandomSamplesFitnessWeightParameterName, "The weight of the fitness obtained on the random samples.", new DoubleValue(1)));
    111120
     121      EvaluatorParameter.Hidden = true;
     122    }
     123
     124    public override IOperation Apply() {
     125      var evaluator = EvaluatorParameter.ActualValue;
     126      //calculate fitness on fixed samples
     127      if (QualityParameter.ActualValue == null) {
     128        var operation = ExecutionContext.CreateOperation(evaluator, ExecutionContext.Scope);
     129        return new OperationCollection() { operation, ExecutionContext.CreateOperation(this) };
     130      }
     131      //calculate fitness on random samples;
     132      var samplesStart = FitnessCalculationPartitionParameter.ActualValue.Start;
     133      var samplesEnd = FitnessCalculationPartitionParameter.ActualValue.End;
     134      var fixedSamplesStart = FixedSamplesPartitionParameter.ActualValue.Start;
     135      var fixedSamplesEnd = FixedSamplesPartitionParameter.ActualValue.End;
     136      var randomSamples = RandomSamplesParameter.ActualValue.Value;
     137      var maxRandomSamples = samplesEnd - samplesStart - fixedSamplesEnd + fixedSamplesStart;
     138
     139      var rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart).Where(r => r < fixedSamplesStart || r >= fixedSamplesEnd);
     140      rows = rows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, maxRandomSamples);
     141
     142      var fixedSamplesFitness = QualityParameter.ActualValue.Value;
     143      var tree = SymbolicExpressionTreeParameter.ActualValue;
     144      var problemData = ProblemDataParameter.ActualValue;
     145
     146      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
     147      var randomSamplesFitness = evaluator.Evaluate(executionContext, tree, problemData, rows);
     148      QualityParameter.ActualValue.Value = fixedSamplesFitness * FixedSamplesFitnessWeight + randomSamplesFitness * RandomSamplesFitnessWeight;
     149      return base.Apply();
    112150    }
    113151  }
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGeneticAlgorithm.cs

    r9067 r9077  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Data;
     29using HeuristicLab.Optimization;
    2930using HeuristicLab.Parameters;
    3031using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.Problems.DataAnalysis;
    3133using HeuristicLab.Problems.DataAnalysis.Symbolic;
     34using HeuristicLab.Random;
    3235
    3336namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
     
    3740  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
    3841    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
     42    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
    3943    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
    4044    private const string RandomSamplesParameterName = "NumberOfRandomSamples";
     45    private const string EvaluatorParameterName = "IslandEvaluator";
     46    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
    4147
    4248    #region Problem Properties
     
    5965    public IFixedValueParameter<IntValue> RandomSamplesParameter {
    6066      get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
     67    }
     68    public IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator> EvaluatorParameter {
     69      get { return (IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>)Parameters[EvaluatorParameterName]; }
     70    }
     71    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
     72      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
    6173    }
    6274    #endregion
     
    95107      Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
    96108      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
    97       Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island..", new IntValue(0)));
     109      Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue(0)));
     110      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
     111      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
     112
     113      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
     114      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
     115      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
     116
     117      RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
     118      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
     119      insertionPoint.Successor = fixedSamplesPartitionCreator;
    98120
    99121      RegisterParameterEvents();
    100122      RecalculateFixedSamplesPartitions();
     123    }
     124
     125    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     126      ParameterizeProblemEvaluator();
     127      base.Problem_EvaluatorChanged(sender, e);
     128    }
     129
     130    private void ParameterizeProblemEvaluator() {
     131      var regresssionEvaluator = Problem.Evaluator as ISymbolicDataAnalysisEvaluator<IRegressionProblemData>;
     132      if (regresssionEvaluator != null) {
     133        regresssionEvaluator.EvaluationPartitionParameter.ActualName = FixedSamplesPartitionParameterName;
     134      }
     135      var classificationEvaluator = Problem.Evaluator as ISymbolicDataAnalysisEvaluator<IClassificationProblemData>;
     136      if (classificationEvaluator != null) {
     137        classificationEvaluator.EvaluationPartitionParameter.ActualName = FixedSamplesPartitionParameterName;
     138      }
     139    }
     140
     141    protected override void ParameterizeSolutionsCreator() {
     142      base.ParameterizeSolutionsCreator();
     143      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
     144    }
     145
     146    protected override void ParameterizeMainLoop() {
     147      base.ParameterizeMainLoop();
     148      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
     149      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
    101150    }
    102151
     
    114163    protected override void Problem_Reset(object sender, EventArgs e) {
    115164      RecalculateFixedSamplesPartitions();
     165      ParameterizeProblemEvaluator();
    116166      base.Problem_Reset(sender, e);
    117167    }
    118168
    119169    protected override void OnProblemChanged() {
     170      Problem.FitnessCalculationPartition.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
     171
     172      ParameterizeProblemEvaluator();
     173      if (Problem is IRegressionProblem) {
     174        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IRegressionProblemData>();
     175        evaluator.FixedSamplesParameter.ActualName = FixedSamplesParameterName;
     176        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
     177        EvaluatorParameter.Value = evaluator;
     178      } else if (Problem is IClassificationProblem) {
     179        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IClassificationProblemData>();
     180        evaluator.FixedSamplesParameter.ActualName = FixedSamplesParameterName;
     181        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
     182        EvaluatorParameter.Value = evaluator;
     183      } else
     184        EvaluatorParameter.Value = null;
     185
     186      ParameterizeStochasticOperatorForIsland(EvaluatorParameter.Value);
     187
     188      RecalculateFixedSamplesPartitions();
    120189      base.OnProblemChanged();
    121       Problem.FitnessCalculationPartition.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
    122       RecalculateFixedSamplesPartitions();
    123190    }
    124191
  • branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs

    r9066 r9077  
    177177      set { IslandAnalyzerParameter.Value = value; }
    178178    }
    179     private RandomCreator RandomCreator {
     179    protected RandomCreator RandomCreator {
    180180      get { return (RandomCreator)OperatorGraph.InitialOperator; }
    181181    }
    182     private UniformSubScopesProcessor IslandProcessor {
     182    protected UniformSubScopesProcessor IslandProcessor {
    183183      get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
    184184    }
    185     private SolutionsCreator SolutionsCreator {
     185    protected SolutionsCreator SolutionsCreator {
    186186      get { return (SolutionsCreator)IslandProcessor.Operator; }
    187187    }
    188     private IslandGeneticAlgorithmMainLoop MainLoop {
     188    protected IslandGeneticAlgorithmMainLoop MainLoop {
    189189      get { return FindMainLoop(IslandProcessor.Successor); }
    190190    }
     
    417417      }
    418418    }
    419     private void ParameterizeSolutionsCreator() {
     419    protected virtual void ParameterizeSolutionsCreator() {
    420420      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    421421      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
    422422    }
    423     private void ParameterizeMainLoop() {
     423    protected virtual void ParameterizeMainLoop() {
    424424      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
    425425      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     
    427427      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    428428    }
    429     private void ParameterizeStochasticOperator(IOperator op) {
     429    protected void ParameterizeStochasticOperator(IOperator op) {
    430430      IStochasticOperator stochasticOp = op as IStochasticOperator;
    431431      if (stochasticOp != null) {
     
    434434      }
    435435    }
    436     private void ParameterizeStochasticOperatorForIsland(IOperator op) {
     436    protected void ParameterizeStochasticOperatorForIsland(IOperator op) {
    437437      IStochasticOperator stochasticOp = op as IStochasticOperator;
    438438      if (stochasticOp != null) {
Note: See TracChangeset for help on using the changeset viewer.