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 ILookupParameter<DoubleValue> LengthParameter {
|
---|
52 | get { return (ILookupParameter<DoubleValue>)Parameters["Length"]; }
|
---|
53 | }
|
---|
54 | public IValueLookupParameter<IDecisionListClassificationProblemData> ProblemDataParameter {
|
---|
55 | get { return (IValueLookupParameter<IDecisionListClassificationProblemData>)Parameters["ProblemData"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public IValueLookupParameter<BoolValue> UseMDLParameter {
|
---|
59 | get { return (IValueLookupParameter<BoolValue>)Parameters["UseMDL"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<MDLCalculator> MDLCalculatorParameter {
|
---|
62 | get { return (ILookupParameter<MDLCalculator>)Parameters["MDLCalculator"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
65 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
68 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public IValueLookupParameter<IntValue> IterationRuleDeletionParameter {
|
---|
72 | get { return (IValueLookupParameter<IntValue>)Parameters["IterationRuleDeletion"]; }
|
---|
73 | }
|
---|
74 | public IValueLookupParameter<IntValue> RuleDeletionMinRulesParameter {
|
---|
75 | get { return (IValueLookupParameter<IntValue>)Parameters["RuleDeletionMinRules"]; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public ILookupParameter<ItemList<ItemList<IntValue>>> StrataParameter {
|
---|
79 | get { return (ILookupParameter<ItemList<ItemList<IntValue>>>)Parameters["Strata"]; }
|
---|
80 | }
|
---|
81 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
82 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | public IRandom Random {
|
---|
87 | get { return RandomParameter.ActualValue; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [StorableConstructor]
|
---|
91 | protected MDLEvaluator(bool deserializing) : base(deserializing) { }
|
---|
92 | protected MDLEvaluator(MDLEvaluator original, Cloner cloner)
|
---|
93 | : base(original, cloner) {
|
---|
94 | }
|
---|
95 | public MDLEvaluator()
|
---|
96 | : base() {
|
---|
97 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random generator to use."));
|
---|
98 | Parameters.Add(new LookupParameter<DecisionList>("DecisionList", ""));
|
---|
99 | Parameters.Add(new ValueLookupParameter<IntValue>("SizePenaltyMinRules", ""));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("Length", ""));
|
---|
102 | Parameters.Add(new ValueLookupParameter<IDecisionListClassificationProblemData>("ProblemData", ""));
|
---|
103 | Parameters.Add(new ValueLookupParameter<BoolValue>("UseMDL", "", new BoolValue(true)));
|
---|
104 | Parameters.Add(new LookupParameter<MDLCalculator>("MDLCalculator", ""));
|
---|
105 | Parameters.Add(new LookupParameter<IntValue>("Iterations", ""));
|
---|
106 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", ""));
|
---|
107 | Parameters.Add(new ValueLookupParameter<IntValue>("IterationRuleDeletion", "", new IntValue(5)));
|
---|
108 | Parameters.Add(new ValueLookupParameter<IntValue>("RuleDeletionMinRules", "", new IntValue(12)));
|
---|
109 | Parameters.Add(new ValueLookupParameter<ItemList<ItemList<IntValue>>>("Strata", ""));
|
---|
110 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "", new BoolValue(false)));
|
---|
111 |
|
---|
112 | UseMDLParameter.Value.ValueChanged += UseMDLParameter_ValueChanged;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void UseMDLParameter_ValueChanged(object sender, System.EventArgs e) {
|
---|
116 | MaximizationParameter.Value.Value = !UseMDLParameter.Value.Value;
|
---|
117 | }
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | return new MDLEvaluator(this, cloner);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override IOperation Apply() {
|
---|
123 | var strata = StrataParameter.ActualValue;
|
---|
124 | int iteration = IterationsParameter.ActualValue.Value;
|
---|
125 | int numberOfStrata = strata.Count;
|
---|
126 | var dl = DecisionListParameter.ActualValue;
|
---|
127 | var problemData = ProblemDataParameter.ActualValue;
|
---|
128 | bool lastIteration = iteration == MaximumIterationsParameter.ActualValue.Value - 1;
|
---|
129 | IEnumerable<int> rows;
|
---|
130 | if (lastIteration) {
|
---|
131 | rows = from s in strata
|
---|
132 | from row in s
|
---|
133 | select row.Value;
|
---|
134 | } else {
|
---|
135 | rows = strata[iteration % numberOfStrata].Select(x => x.Value);
|
---|
136 | }
|
---|
137 | var input = problemData.FetchInput(rows);
|
---|
138 | var actions = problemData.FetchAction(rows);
|
---|
139 | ItemSet<Rule> aliveRules;
|
---|
140 | double theoryLength;
|
---|
141 | var estimated = dl.Evaluate(input, out aliveRules, out theoryLength);
|
---|
142 |
|
---|
143 | double penalty = 1;
|
---|
144 | if (aliveRules.Count < SizePenaltyMinRulesParameter.ActualValue.Value) {
|
---|
145 | penalty = (1 - 0.025 * (SizePenaltyMinRulesParameter.ActualValue.Value - aliveRules.Count));
|
---|
146 | if (penalty <= 0) penalty = 0.01;
|
---|
147 | penalty *= penalty;
|
---|
148 | }
|
---|
149 |
|
---|
150 | double accuracy = DecisionListSolution.CalculateAccuracy(actions, estimated);
|
---|
151 | if (UseMDLParameter.ActualValue.Value) {
|
---|
152 | QualityParameter.ActualValue =
|
---|
153 | new DoubleValue(MDLCalculatorParameter.ActualValue.CalculateFitness(theoryLength, accuracy) / penalty);
|
---|
154 | } else {
|
---|
155 | QualityParameter.ActualValue = new DoubleValue(accuracy * accuracy * penalty);
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (iteration >= IterationRuleDeletionParameter.ActualValue.Value) {
|
---|
159 | if (lastIteration) {
|
---|
160 | DoRuleDeletion(dl, aliveRules, 1);
|
---|
161 | } else {
|
---|
162 | DoRuleDeletion(dl, aliveRules, RuleDeletionMinRulesParameter.ActualValue.Value);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | LengthParameter.ActualValue = new DoubleValue(dl.Length * aliveRules.Count / dl.RuleSetSize);
|
---|
167 | return base.Apply();
|
---|
168 | }
|
---|
169 |
|
---|
170 | // default rule cannot be deleted, but it has to be considered in the rule set size
|
---|
171 | private void DoRuleDeletion(DecisionList dl, IEnumerable<Rule> aliveRules, int minRules) {
|
---|
172 | int ruleSetSize = dl.RuleSetSize;
|
---|
173 | if (ruleSetSize <= minRules) { return; }
|
---|
174 |
|
---|
175 | var deadRules = dl.Rules.Except(aliveRules).ToList();
|
---|
176 | int numberOfDeadRules = deadRules.Count();
|
---|
177 |
|
---|
178 | int keepRules = minRules - (ruleSetSize - numberOfDeadRules);
|
---|
179 |
|
---|
180 | if (keepRules > 0) {
|
---|
181 | for (int i = 0; i < keepRules; i++) {
|
---|
182 | int pos = Random.Next(deadRules.Count);
|
---|
183 | deadRules.RemoveAt(pos);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | dl.RemoveRules(deadRules);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | } |
---|