1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.DecisionList {
|
---|
34 | [Item("MDLEvaluator", "Description missing")]
|
---|
35 | [StorableClass]
|
---|
36 | public class MDLEvaluator : SingleSuccessorOperator, IDecisionListEvaluator, IDecisionListOperator, IMDLCalculatorBasedOperator, IIterationBasedOperator, IStochasticOperator {
|
---|
37 |
|
---|
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; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | protected MDLEvaluator(bool deserializing) : base(deserializing) { }
|
---|
83 | protected MDLEvaluator(MDLEvaluator original, Cloner cloner)
|
---|
84 | : base(original, cloner) {
|
---|
85 | }
|
---|
86 | public MDLEvaluator()
|
---|
87 | : base() {
|
---|
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", ""));
|
---|
99 | }
|
---|
100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
101 | return new MDLEvaluator(this, cloner);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public override IOperation Apply() {
|
---|
105 | double penalty = 1;
|
---|
106 |
|
---|
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);
|
---|
126 |
|
---|
127 | if (aliveRules.Count < SizePenaltyMinRulesParameter.ActualValue.Value) {
|
---|
128 | penalty = (1 - 0.025 * (SizePenaltyMinRulesParameter.ActualValue.Value - aliveRules.Count));
|
---|
129 | if (penalty <= 0) penalty = 0.01;
|
---|
130 | penalty *= penalty;
|
---|
131 | }
|
---|
132 |
|
---|
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 | }
|
---|
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, IEnumerable<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);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | } |
---|