Changeset 12952 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 09/17/15 00:31:53 (9 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs
r12951 r12952 22 22 using System; 23 23 using System.Linq; 24 using HeuristicLab.Analysis; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 29 using HeuristicLab.EvolutionTracking; 30 using HeuristicLab.Operators; 31 using HeuristicLab.Optimization.Operators; 29 32 using HeuristicLab.Parameters; 30 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 99 102 var schemaCleanupOperator = new SchemaCleanupOperator(); 100 103 104 var changedTreesReducer = new DataReducer(); 105 changedTreesReducer.ParameterToReduce.ActualName = schemaEvaluator.ChangedTreesParameter.ActualName; 106 changedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); 107 changedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter 108 changedTreesReducer.TargetParameter.ActualName = "NumberOfChangedTrees"; 109 110 var valuesCollector = new DataTableValuesCollector(); 111 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfChangedTrees")); 112 valuesCollector.DataTableParameter.ActualName = "Diversification"; 113 114 var resultCollector = new ResultsCollector(); 115 resultCollector.CollectedValues.Add(new LookupParameter<DataTable>("Diversification")); 116 117 var reduceChangedTrees = ExecutionContext.CreateChildOperation(changedTreesReducer); 118 var collectValues = ExecutionContext.CreateChildOperation(valuesCollector); 119 var collectResults = ExecutionContext.CreateChildOperation(resultCollector); 120 101 121 var oc = new OperationCollection(); 102 122 … … 114 134 var nodesToReplace = indices.Where(x => x >= 2).Select(x => nodes[x]).ToList(); 115 135 for (int i = nodesToReplace.Count - 1; i >= 0; --i) { 136 var node = nodesToReplace[i]; 137 138 // do not replace the node with a wildcard if it would result in a length < MinimumSchemaLength 139 if ((schema.Length - node.GetLength() + 1) < MinimumSchemaLength) 140 continue; 141 116 142 var replacement = anySubtreeSymbol.CreateTreeNode(); 117 ReplaceSubtree(node sToReplace[i], replacement, false);143 ReplaceSubtree(node, replacement, false); 118 144 replaced = true; 119 145 } … … 131 157 var cleanup = ExecutionContext.CreateChildOperation(schemaCleanupOperator); 132 158 // return an operation collection containing all the scope operations + base.Apply() 133 return new OperationCollection { oc, cleanup, base.Apply() };159 return new OperationCollection { oc, reduceChangedTrees, collectValues, collectResults, cleanup, base.Apply() }; 134 160 } 135 161 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs
r12951 r12952 35 35 [StorableClass] 36 36 public class SchemaEvaluator : EvolutionTrackingOperator<ISymbolicExpressionTree> { 37 #region parameter names 37 38 private const string MinimumSchemaFrequencyParameterName = "MinimumSchemaFrequency"; 38 39 private const string MinimumPhenotypicSimilarityParameterName = "MinimumPhenotypicSimilarity"; … … 48 49 private const string MutatorParameterName = "Mutator"; 49 50 private const string RandomReplacementParameterName = "RandomReplacement"; 51 private const string ChangedTreesParameterName = "ChangedTrees"; 52 #endregion 53 54 #region parameters 55 public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>> EvaluatorParameter { 56 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>)Parameters[EvaluatorParameterName]; } 57 } 58 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { 59 get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } 60 } 61 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter { 62 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; } 63 } 64 public ILookupParameter<DoubleLimit> EstimationLimitsParameter { 65 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 66 } 67 public ILookupParameter<BoolValue> ApplyLinearScalingParameter { 68 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 69 } 70 public ILookupParameter<BoolValue> RandomReplacementParameter { 71 get { return (ILookupParameter<BoolValue>)Parameters[RandomReplacementParameterName]; } 72 } 73 public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter { 74 get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; } 75 } 76 public ILookupParameter<IRandom> RandomParameter { 77 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 78 } 79 public ILookupParameter<IntValue> PopulationSizeParameter { 80 get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; } 81 } 82 public ILookupParameter<ISymbolicExpressionTree> SchemaParameter { 83 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SchemaParameterName]; } 84 } 85 public ILookupParameter<PercentValue> MinimumSchemaFrequencyParameter { 86 get { return (ILookupParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; } 87 } 88 public ILookupParameter<PercentValue> ReplacementRatioParameter { 89 get { return (ILookupParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; } 90 } 91 public ILookupParameter<PercentValue> MinimumPhenotypicSimilarityParameter { 92 get { return (ILookupParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; } 93 } 94 public LookupParameter<IntValue> ChangedTreesParameter { 95 get { return (LookupParameter<IntValue>)Parameters[ChangedTreesParameterName]; } 96 } 97 98 #endregion 99 100 #region parameter properties 101 public PercentValue MinimumSchemaFrequency { 102 get { return MinimumSchemaFrequencyParameter.ActualValue; } 103 } 104 105 public PercentValue ReplacementRatio { 106 get { return ReplacementRatioParameter.ActualValue; } 107 } 108 109 public PercentValue MinimumPhenotypicSimilarity { 110 get { return MinimumPhenotypicSimilarityParameter.ActualValue; } 111 } 112 113 public BoolValue RandomReplacement { 114 get { return RandomReplacementParameter.ActualValue; } 115 } 116 #endregion 50 117 51 118 private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator(); 119 private readonly QueryMatch qm; 52 120 53 121 private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer { … … 57 125 }; 58 126 59 private readonly QueryMatch qm; 60 61 #region parameters 62 public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>> EvaluatorParameter { 63 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>)Parameters[EvaluatorParameterName]; } 64 } 65 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { 66 get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } 67 } 68 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter { 69 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; } 70 } 71 public ILookupParameter<DoubleLimit> EstimationLimitsParameter { 72 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 73 } 74 public ILookupParameter<BoolValue> ApplyLinearScalingParameter { 75 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 76 } 77 public ILookupParameter<BoolValue> RandomReplacementParameter { 78 get { return (ILookupParameter<BoolValue>)Parameters[RandomReplacementParameterName]; } 79 } 80 public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter { 81 get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; } 82 } 83 public ILookupParameter<IRandom> RandomParameter { 84 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 85 } 86 public ILookupParameter<IntValue> PopulationSizeParameter { 87 get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; } 88 } 89 public ILookupParameter<ISymbolicExpressionTree> SchemaParameter { 90 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SchemaParameterName]; } 91 } 92 public ILookupParameter<PercentValue> MinimumSchemaFrequencyParameter { 93 get { return (ILookupParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; } 94 } 95 public ILookupParameter<PercentValue> ReplacementRatioParameter { 96 get { return (ILookupParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; } 97 } 98 public ILookupParameter<PercentValue> MinimumPhenotypicSimilarityParameter { 99 get { return (ILookupParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; } 100 } 101 #endregion 102 103 #region parameter properties 104 public PercentValue MinimumSchemaFrequency { 105 get { return MinimumSchemaFrequencyParameter.ActualValue; } 106 } 107 108 public PercentValue ReplacementRatio { 109 get { return ReplacementRatioParameter.ActualValue; } 110 } 111 112 public PercentValue MinimumPhenotypicSimilarity { 113 get { return MinimumPhenotypicSimilarityParameter.ActualValue; } 114 } 115 116 public BoolValue RandomReplacement { 117 get { return RandomReplacementParameter.ActualValue; } 118 } 119 #endregion 127 128 [StorableHook(HookType.AfterDeserialization)] 129 private void AfterDeserialization() { 130 if (!Parameters.ContainsKey(ChangedTreesParameterName)) 131 Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName)); 132 } 120 133 121 134 public SchemaEvaluator() { … … 135 148 Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName)); 136 149 Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName)); 150 Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName)); 137 151 } 138 152 … … 171 185 select ind); 172 186 173 if (matchingIndividuals.Count < MinimumSchemaFrequency.Value * individuals.Count) 187 if (matchingIndividuals.Count < MinimumSchemaFrequency.Value * individuals.Count) { 188 ChangedTreesParameter.ActualValue = new IntValue(0); 174 189 return base.Apply(); 190 } 175 191 176 192 var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator); 177 if (similarity < MinimumPhenotypicSimilarity.Value) 193 if (similarity < MinimumPhenotypicSimilarity.Value) { 194 ChangedTreesParameter.ActualValue = new IntValue(0); 178 195 return base.Apply(); 196 } 179 197 180 198 var oc = new OperationCollection(); 181 182 199 int n = (int)Math.Round(matchingIndividuals.Count * ReplacementRatio.Value); 183 var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n) 184 : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n) ;200 var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList() 201 : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList(); 185 202 foreach (var ind in individualsToReplace) { 186 203 var mutatorOp = ExecutionContext.CreateChildOperation(mutator, ind); … … 191 208 oc.Add(updateEstimatedValuesOp); 192 209 } 193 210 ChangedTreesParameter.ActualValue = new IntValue(individualsToReplace.Count); 194 211 return new OperationCollection(oc, base.Apply()); 195 212 }
Note: See TracChangeset
for help on using the changeset viewer.