Changeset 9392 for branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Evaluator
- Timestamp:
- 04/23/13 13:31:29 (12 years ago)
- Location:
- branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3
- Files:
-
- 3 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3
- Property svn:ignore
-
old new 1 1 *.user 2 2 Plugin.cs 3 obj
-
- Property svn:ignore
-
branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Evaluator/MDLEvaluator.cs
r9334 r9392 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 using System.Linq; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Data; 27 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Optimization.Operators.LCS; 25 30 using HeuristicLab.Parameters; 26 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 29 34 [Item("MDLEvaluator", "Description missing")] 30 35 [StorableClass] 31 public class MDLEvaluator : DecisionListEvaluator {36 public class MDLEvaluator : SingleSuccessorOperator, IDecisionListEvaluator, IDecisionListOperator, IMDLCalculatorBasedOperator, IIterationBasedOperator, IStochasticOperator { 32 37 33 public IValueLookupParameter<MDLCalculator> MDLCalculatorParameter { 34 get { return (IValueLookupParameter<MDLCalculator>)Parameters["MDLCalculator"]; } 38 #region Parameter Properties 39 public ILookupParameter<IRandom> RandomParameter { 40 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 41 } 42 public ILookupParameter<DecisionList> DecisionListParameter { 43 get { return (ILookupParameter<DecisionList>)Parameters["DecisionList"]; } 44 } 45 public IValueLookupParameter<IntValue> SizePenaltyMinRulesParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters["SizePenaltyMinRules"]; } 47 } 48 public ILookupParameter<DoubleValue> QualityParameter { 49 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 50 } 51 public IValueLookupParameter<IDecisionListClassificationProblemData> ProblemDataParameter { 52 get { return (IValueLookupParameter<IDecisionListClassificationProblemData>)Parameters["ProblemData"]; } 53 } 54 55 public ILookupParameter<MDLCalculator> MDLCalculatorParameter { 56 get { return (ILookupParameter<MDLCalculator>)Parameters["MDLCalculator"]; } 57 } 58 public ILookupParameter<IntValue> IterationsParameter { 59 get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; } 60 } 61 public IValueLookupParameter<IntValue> MaximumIterationsParameter { 62 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; } 63 } 64 65 public IValueLookupParameter<IntValue> IterationRuleDeletionParameter { 66 get { return (IValueLookupParameter<IntValue>)Parameters["IterationRuleDeletion"]; } 67 } 68 public IValueLookupParameter<IntValue> RuleDeletionMinRulesParameter { 69 get { return (IValueLookupParameter<IntValue>)Parameters["RuleDeletionMinRules"]; } 70 } 71 72 public ILookupParameter<ItemList<ItemList<IntValue>>> StrataParameter { 73 get { return (ILookupParameter<ItemList<ItemList<IntValue>>>)Parameters["Strata"]; } 74 } 75 #endregion 76 77 public IRandom Random { 78 get { return RandomParameter.ActualValue; } 35 79 } 36 80 … … 42 86 public MDLEvaluator() 43 87 : base() { 44 Parameters.Add(new ValueLookupParameter<MDLCalculator>("MDLCalculator", "")); 88 Parameters.Add(new LookupParameter<IRandom>("Random", "The random generator to use.")); 89 Parameters.Add(new LookupParameter<DecisionList>("DecisionList", "")); 90 Parameters.Add(new ValueLookupParameter<IntValue>("SizePenaltyMinRules", "")); 91 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "")); 92 Parameters.Add(new ValueLookupParameter<IDecisionListClassificationProblemData>("ProblemData", "")); 93 Parameters.Add(new LookupParameter<MDLCalculator>("MDLCalculator", "")); 94 Parameters.Add(new LookupParameter<IntValue>("Iterations", "")); 95 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "")); 96 Parameters.Add(new ValueLookupParameter<IntValue>("IterationRuleDeletion", "", new IntValue(5))); 97 Parameters.Add(new ValueLookupParameter<IntValue>("RuleDeletionMinRules", "", new IntValue(12))); 98 Parameters.Add(new ValueLookupParameter<ItemList<ItemList<IntValue>>>("Strata", "")); 45 99 } 46 100 public override IDeepCloneable Clone(Cloner cloner) { … … 51 105 double penalty = 1; 52 106 53 var dls = new DecisionListSolution(DecisionListParameter.ActualValue, ProblemDataParameter.ActualValue); 107 var strata = StrataParameter.ActualValue; 108 int iteration = IterationsParameter.ActualValue.Value; 109 int numberOfStrata = strata.Count; 110 var dl = DecisionListParameter.ActualValue; 111 var problemData = ProblemDataParameter.ActualValue; 112 bool lastIteration = iteration == MaximumIterationsParameter.ActualValue.Value - 1; 113 IEnumerable<int> rows; 114 if (lastIteration) { 115 rows = from s in strata 116 from row in s 117 select row.Value; 118 } else { 119 rows = strata[iteration % numberOfStrata].Select(x => x.Value); 120 } 121 var input = problemData.FetchInput(rows); 122 var actions = problemData.FetchAction(rows); 123 ItemSet<Rule> aliveRules; 124 double theoryLength; 125 var estimated = dl.Evaluate(input, out aliveRules, out theoryLength); 54 126 55 if ( dls.TrainingNumberOfAliveRules< SizePenaltyMinRulesParameter.ActualValue.Value) {56 penalty = (1 - 0.025 * (SizePenaltyMinRulesParameter.ActualValue.Value - dls.TrainingNumberOfAliveRules));127 if (aliveRules.Count < SizePenaltyMinRulesParameter.ActualValue.Value) { 128 penalty = (1 - 0.025 * (SizePenaltyMinRulesParameter.ActualValue.Value - aliveRules.Count)); 57 129 if (penalty <= 0) penalty = 0.01; 58 130 penalty *= penalty; 59 131 } 60 132 61 QualityParameter.ActualValue = new DoubleValue(MDLCalculatorParameter.ActualValue.CalculateFitness(dls) / penalty); 133 double accuracy = DecisionListSolution.CalculateAccuracy(actions, estimated); 134 QualityParameter.ActualValue = new DoubleValue(MDLCalculatorParameter.ActualValue.CalculateFitness(theoryLength, accuracy) / penalty); 135 136 if (iteration >= IterationRuleDeletionParameter.ActualValue.Value) { 137 if (lastIteration) { 138 DoRuleDeletion(dl, aliveRules, 1); 139 } else { 140 DoRuleDeletion(dl, aliveRules, RuleDeletionMinRulesParameter.ActualValue.Value); 141 } 142 } 62 143 return base.Apply(); 144 } 145 146 // default rule cannot be deleted, but it has to be considered in the rule set size 147 private void DoRuleDeletion(DecisionList dl, ItemSet<Rule> aliveRules, int minRules) { 148 int ruleSetSize = dl.RuleSetSize; 149 if (ruleSetSize <= minRules) { return; } 150 151 var deadRules = dl.Rules.Except(aliveRules).ToList(); 152 int numberOfDeadRules = deadRules.Count(); 153 154 int keepRules = minRules - (ruleSetSize - numberOfDeadRules); 155 156 if (keepRules > 0) { 157 for (int i = 0; i < keepRules; i++) { 158 int pos = Random.Next(deadRules.Count); 159 deadRules.RemoveAt(pos); 160 } 161 } 162 163 dl.RemoveRules(deadRules); 63 164 } 64 165 }
Note: See TracChangeset
for help on using the changeset viewer.