Changeset 9077 for branches/DataAnalysis.IslandAlgorithms
- Timestamp:
- 12/19/12 10:33:27 (12 years ago)
- 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 109 109 </ItemGroup> 110 110 <ItemGroup> 111 <Compile Include="ISymbolicDataAnalysisIslandGAEvaluator .cs" /> 112 <Compile Include="ScopeTreeAssigner.cs" /> 111 113 <Compile Include="Plugin.cs" /> 112 114 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGAEvaluator.cs
r9051 r9077 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Data; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 27 using HeuristicLab.Operators; 26 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 27 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 31 using HeuristicLab.Problems.DataAnalysis; 29 32 using HeuristicLab.Problems.DataAnalysis.Symbolic; 33 using HeuristicLab.Random; 30 34 31 35 namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic { 32 36 [StorableClass] 33 public sealed class SymbolicDataAnalysisIslandGAEvaluator<T> : SingleSuccessorOperator, IStochasticOperator 37 public sealed class SymbolicDataAnalysisIslandGAEvaluator<T> : SingleSuccessorOperator, IStochasticOperator, ISymbolicDataAnalysisIslandGAEvaluator 34 38 where T : class,IDataAnalysisProblemData { 35 39 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"; 41 48 private const string RandomSamplesParameterName = "RandomSamples"; 42 49 private const string RandomSamplesFitnessWeightParameterName = "RandomSamplesFitnessWeight"; 43 private const string RandomSamplesConsecutiveParameterName = "RandomSamplesConsecutive";44 50 45 51 #region parameter properties … … 47 53 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 48 54 } 49 public I ValueParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {50 get { return (I ValueParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }55 public ILookupParameter<T> ProblemDataParameter { 56 get { return (ILookupParameter<T>)Parameters[ProblemDataParameterName]; } 51 57 } 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]; } 55 60 } 56 public I FixedValueParameter<IntValue> ConstantSamplesParameter {57 get { return (I FixedValueParameter<IntValue>)Parameters[ConstantSamplesParameterName]; }61 public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter { 62 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; } 58 63 } 59 public I FixedValueParameter<DoubleValue> ConstantSamplesFitnessWeightParameter {60 get { return (I FixedValueParameter<DoubleValue>)Parameters[ConstantSamplesFitnessWeightFactorParameterName]; }64 public ILookupParameter<DoubleValue> QualityParameter { 65 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 61 66 } 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]; } 64 81 } 65 82 public IFixedValueParameter<DoubleValue> RandomSamplesFitnessWeightParameter { 66 83 get { return (IFixedValueParameter<DoubleValue>)Parameters[RandomSamplesFitnessWeightParameterName]; } 67 84 } 68 public IFixedValueParameter<BoolValue> RandomSamplesConsecutiveParameter {69 get { return (IFixedValueParameter<BoolValue>)Parameters[RandomSamplesConsecutiveParameterName]; }70 }71 85 #endregion 72 86 73 87 #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; } 89 91 } 90 92 public double RandomSamplesFitnessWeight { 91 93 get { return RandomSamplesFitnessWeightParameter.Value.Value; } 92 94 set { RandomSamplesFitnessWeightParameter.Value.Value = value; } 93 }94 public bool RandomSamplesConsecutive {95 get { return RandomSamplesConsecutiveParameter.Value.Value; }96 set { RandomSamplesConsecutiveParameter.Value.Value = value; }97 95 } 98 96 #endregion … … 109 107 public SymbolicDataAnalysisIslandGAEvaluator() 110 108 : 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))); 111 120 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(); 112 150 } 113 151 } -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGeneticAlgorithm.cs
r9067 r9077 27 27 using HeuristicLab.Core; 28 28 using HeuristicLab.Data; 29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Parameters; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Problems.DataAnalysis; 31 33 using HeuristicLab.Problems.DataAnalysis.Symbolic; 34 using HeuristicLab.Random; 32 35 33 36 namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic { … … 37 40 public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm { 38 41 private const string FixedSamplesParameterName = "NumberOfFixedSamples"; 42 private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition"; 39 43 private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions"; 40 44 private const string RandomSamplesParameterName = "NumberOfRandomSamples"; 45 private const string EvaluatorParameterName = "IslandEvaluator"; 46 private const string ProblemEvaluatorParameterName = "ProblemEvaluator"; 41 47 42 48 #region Problem Properties … … 59 65 public IFixedValueParameter<IntValue> RandomSamplesParameter { 60 66 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]; } 61 73 } 62 74 #endregion … … 95 107 Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0))); 96 108 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; 98 120 99 121 RegisterParameterEvents(); 100 122 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; 101 150 } 102 151 … … 114 163 protected override void Problem_Reset(object sender, EventArgs e) { 115 164 RecalculateFixedSamplesPartitions(); 165 ParameterizeProblemEvaluator(); 116 166 base.Problem_Reset(sender, e); 117 167 } 118 168 119 169 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(); 120 189 base.OnProblemChanged(); 121 Problem.FitnessCalculationPartition.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();122 RecalculateFixedSamplesPartitions();123 190 } 124 191 -
branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs
r9066 r9077 177 177 set { IslandAnalyzerParameter.Value = value; } 178 178 } 179 pr ivateRandomCreator RandomCreator {179 protected RandomCreator RandomCreator { 180 180 get { return (RandomCreator)OperatorGraph.InitialOperator; } 181 181 } 182 pr ivateUniformSubScopesProcessor IslandProcessor {182 protected UniformSubScopesProcessor IslandProcessor { 183 183 get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); } 184 184 } 185 pr ivateSolutionsCreator SolutionsCreator {185 protected SolutionsCreator SolutionsCreator { 186 186 get { return (SolutionsCreator)IslandProcessor.Operator; } 187 187 } 188 pr ivateIslandGeneticAlgorithmMainLoop MainLoop {188 protected IslandGeneticAlgorithmMainLoop MainLoop { 189 189 get { return FindMainLoop(IslandProcessor.Successor); } 190 190 } … … 417 417 } 418 418 } 419 pr ivatevoid ParameterizeSolutionsCreator() {419 protected virtual void ParameterizeSolutionsCreator() { 420 420 SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 421 421 SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 422 422 } 423 pr ivatevoid ParameterizeMainLoop() {423 protected virtual void ParameterizeMainLoop() { 424 424 MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name; 425 425 MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; … … 427 427 MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 428 428 } 429 pr ivatevoid ParameterizeStochasticOperator(IOperator op) {429 protected void ParameterizeStochasticOperator(IOperator op) { 430 430 IStochasticOperator stochasticOp = op as IStochasticOperator; 431 431 if (stochasticOp != null) { … … 434 434 } 435 435 } 436 pr ivatevoid ParameterizeStochasticOperatorForIsland(IOperator op) {436 protected void ParameterizeStochasticOperatorForIsland(IOperator op) { 437 437 IStochasticOperator stochasticOp = op as IStochasticOperator; 438 438 if (stochasticOp != null) {
Note: See TracChangeset
for help on using the changeset viewer.